Add module for cgroup processing

sh/rc-cgroup.sh.in: new script to handle cgroup processing
sh/rc-cgroup.sh.in: do not use grep or cut (modification by William Hubbs)
sh/runscript.sh.in: use the cgroup script
This commit is contained in:
Alexander Vershilov 2013-02-03 16:01:16 -06:00 committed by William Hubbs
parent 86dbd757e9
commit b46747f998
4 changed files with 67 additions and 23 deletions

1
sh/.gitignore vendored
View File

@ -8,6 +8,7 @@ init.sh
init-early.sh init-early.sh
ifwatchd-carrier.sh ifwatchd-carrier.sh
ifwatchd-nocarrier.sh ifwatchd-nocarrier.sh
rc-cgroup.sh
udhcpc-hook.sh udhcpc-hook.sh
tmpfiles.sh tmpfiles.sh
migrate-to-run.sh migrate-to-run.sh

View File

@ -13,9 +13,9 @@ SRCS-FreeBSD=
BIN-FreeBSD= BIN-FreeBSD=
SRCS-Linux= cgroup-release-agent.sh.in init-early.sh.in migrate-to-run.sh.in \ SRCS-Linux= cgroup-release-agent.sh.in init-early.sh.in migrate-to-run.sh.in \
udhcpc-hook.sh.in rc-cgroup.sh.in udhcpc-hook.sh.in
BIN-Linux= cgroup-release-agent.sh init-early.sh migrate-to-run.sh \ BIN-Linux= cgroup-release-agent.sh init-early.sh migrate-to-run.sh \
udhcpc-hook.sh rc-cgroup.sh udhcpc-hook.sh
SRCS-NetBSD= ifwatchd-carrier.sh.in ifwatchd-nocarrier.sh.in SRCS-NetBSD= ifwatchd-carrier.sh.in ifwatchd-nocarrier.sh.in
BIN-NetBSD= ifwatchd-carrier.sh ifwatchd-nocarrier.sh BIN-NetBSD= ifwatchd-carrier.sh ifwatchd-nocarrier.sh

59
sh/rc-cgroup.sh.in Normal file
View File

@ -0,0 +1,59 @@
#@SHELL@
# Copyright (c) 2012 Alexander Vershilov <qnikst@gentoo.org>
# Released under the 2-clause BSD license.
cgroup_find_path()
{
local OIFS n name dir result
[ -n "$1" ] || return 0
OIFS="$IFS"
IFS=":"
while read n name dir; do
[ "$name" = "$1" ] && result="$dir"
done < /proc/1/cgroup
IFS="$OIFS"
echo $result
}
# prepare values to be attached inside cgroups
cgroup_prepare()
{
local h=$(cgroup_find_path "$1")
cgroup="/sys/fs/cgroup/${1}${h}openrc_${RC_SVCNAME}"
[ -d ${cgroup} ] || mkdir -p ${cgroup}
return 0
}
cgroup_set_value()
{
[ -f "$cgroup/${1}" -a -n "$2" ] && echo $2 > "${cgroup}/${1}"
return 0
}
cgroup_add_process()
{
[ -f "${cgroup}"/tasks ] && echo 0 > "${cgroup}"/tasks
return 0
}
cgroup_set_limits()
{
openrc_cgroup=/sys/fs/cgroup/openrc
if [ -d ${openrc_cgroup} ]; then
cgroup=${openrc_cgroup}/${RC_SVCNAME}
mkdir -p ${cgroup}
[ -f "${cgroup}"/tasks ] && echo 0 > "${cgroup}"/tasks
fi
if [ -d /sys/fs/cgroup/cpu ]; then
local share
share=${rc_cgroup_cpu_shares:-$RC_CGROUP_CPU_SHARES}
if [ -n "$share" ]; then
cgroup_prepare "cpu"
cgroup_set_value "cpu.shares" $share
cgroup_add_process
fi
fi
return 0
}

View File

@ -34,6 +34,7 @@ sourcex()
sourcex "@LIBEXECDIR@/sh/functions.sh" sourcex "@LIBEXECDIR@/sh/functions.sh"
sourcex "@LIBEXECDIR@/sh/rc-functions.sh" sourcex "@LIBEXECDIR@/sh/rc-functions.sh"
[ "$RC_SYS" != "PREFIX" ] && sourcex -e "@LIBEXECDIR@/sh/rc-cgroup.sh"
# Support LiveCD foo # Support LiveCD foo
if sourcex -e "/sbin/livecd-functions.sh"; then if sourcex -e "/sbin/livecd-functions.sh"; then
@ -204,30 +205,13 @@ unset _conf_d
# Load any system overrides # Load any system overrides
sourcex -e "@SYSCONFDIR@/rc.conf" sourcex -e "@SYSCONFDIR@/rc.conf"
if [ "$RC_UNAME" = "Linux" -a "$RC_SYS" != "PREFIX" -a "$1" = "start" ]; then
openrc_cgroup=/sys/fs/cgroup/openrc
if [ -d ${openrc_cgroup} ]; then
cgroup=${openrc_cgroup}/${RC_SVCNAME}
mkdir -p ${cgroup}
[ -f "${cgroup}"/tasks ] && echo 0 > "${cgroup}"/tasks
fi
shares="${rc_cgroup_cpu_shares:-$RC_CGROUP_CPU_SHARES}"
if [ -n "${shares}" -a -d /sys/fs/cgroup/cpu ]; then
cgroup=/sys/fs/cgroup/cpu/openrc_${RC_SVCNAME}
if [ ! -d ${cgroup} ]; then
mkdir -p ${cgroup}
fi
[ -f "${cgroup}"/cpu.shares ] && echo ${shares} > "${cgroup}"/cpu.shares
[ -f "${cgroup}"/tasks ] && echo 0 >> "${cgroup}"/tasks
fi
#todo: add processes to cgroups based on settings in conf.d
fi
# Apply any ulimit defined # Apply any ulimit defined
[ -n "${rc_ulimit:-$RC_ULIMIT}" ] && ulimit ${rc_ulimit:-$RC_ULIMIT} [ -n "${rc_ulimit:-$RC_ULIMIT}" ] && ulimit ${rc_ulimit:-$RC_ULIMIT}
# Apply cgroups settings if defined
[ "$(command -v cgroup_set_limits)" = "cgroup_set_limits" ] && \
cgroup_set_limits
# Load our script # Load our script
sourcex "$RC_SERVICE" sourcex "$RC_SERVICE"