Skip to content

Commit c297f67

Browse files
cgroups: enhance support
Add a possibility to: * Set a cgroup name for a service, * Set a cgroup name prefix for services that don't have an explicit cgroup name set by the previous feature. This allows defining, for example, a root "services" cgroup, under which children cgroups for particular services are kept, * Add a service to a controller without needing to set any of its settings, * Keep a service cgroup when it becomes empty (for example to keep between service restarts some cgroup settings that were set outside OpenRC). These functionalities allow configuration of a service cgroup name for v1 controllers and v2 (unified) hierarchy; the service name in the "openrc" hierarchy is not affected by these settings. The default values are set in a way so not to result in any change of services cgroup behavior unless specifically configured in a different way by an user.
1 parent 0f8fe2a commit c297f67

2 files changed

Lines changed: 53 additions & 13 deletions

File tree

etc/rc.conf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,19 @@ rc_tty_number=12
233233
# It must be set to yes in this file if you want this functionality.
234234
#rc_cgroup_memory_use_hierarchy="NO"
235235

236+
# Sets cgroup name for a service (for cgroups v1 controllers and cgroups v2).
237+
# If empty, the service name will be used with a "openrc_" prefix (the whole
238+
# name then optionally prefixed by rc_cgroup_name_prefix).
239+
# You'll most certainly want a different name for each service, so set this
240+
# setting in /etc/conf.d/foo (for a service named foo) instead of doing it here.
241+
# Could contain intermediate cgroups in the path, like "services/foo".
242+
#rc_cgroup_name=""
243+
244+
# Sets cgroup name prefix for services that have rc_cgroup_name unset.
245+
# For example, with rc_cgroup_name_prefix="services" some service named "foo"
246+
# would get a cgroup name of "services/openrc_foo".
247+
#rc_cgroup_name_prefix=""
248+
236249
# The following settings allow you to set up values for the cgroups version 1
237250
# controllers for your services.
238251
# They can be set in this file;, however, if you do this, the settings
@@ -247,6 +260,11 @@ rc_tty_number=12
247260
# cpu.shares 512
248261
# "
249262
#
263+
# You can also set a variable to a special "add" value, which will add
264+
# the service to the relevant controller without changing any of its settings.
265+
# For example, to just add your service to the cpu controller use:
266+
# rc_cgroup_cpu="add"
267+
#
250268
# For more information about the adjustments that can be made with
251269
# cgroups version 1, see Documentation/cgroups-v1/* in the linux kernel
252270
# source tree.
@@ -281,6 +299,13 @@ rc_tty_number=12
281299
# Set the pids controller settings for this service.
282300
#rc_cgroup_pids=""
283301

302+
# Set this to YES if you do not want a service cgroup removed when it
303+
# becomes empty (for example if you want to keep between service restarts
304+
# some settings that were set outside OpenRC).
305+
# If you want a different setting for each service, place the setting in
306+
# /etc/conf.d/foo for service foo.
307+
# rc_cgroup_keep="NO"
308+
284309
# Set this to YES if you want all of the processes in a service's cgroup
285310
# killed when the service is stopped or restarted.
286311
# Be aware that setting this to yes means all of a service's

sh/rc-cgroup.sh

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@
1111
extra_stopped_commands="${extra_stopped_commands} cgroup_cleanup"
1212
description_cgroup_cleanup="Kill all processes in the cgroup"
1313

14+
cgroup_name()
15+
{
16+
if [ -z "${rc_cgroup_name}" ]; then
17+
rc_cgroup_name="openrc_${RC_SVCNAME}"
18+
[ -n "${rc_cgroup_name_prefix}" ] &&
19+
rc_cgroup_name="${rc_cgroup_name_prefix}/${rc_cgroup_name}"
20+
fi
21+
22+
printf "%s" "${rc_cgroup_name}"
23+
}
24+
1425
cgroup_find_path()
1526
{
1627
local OIFS name dir result
@@ -35,7 +46,7 @@ cgroup_get_pids()
3546
cgroup_pids=
3647
cgroup_procs="$(cgroup2_find_path)"
3748
if [ -n "${cgroup_procs}" ]; then
38-
cgroup_procs="${cgroup_procs}/${RC_SVCNAME}/cgroup.procs"
49+
cgroup_procs="${cgroup_procs}/$(cgroup_name)/cgroup.procs"
3950
else
4051
cgroup_procs="/sys/fs/cgroup/openrc/${RC_SVCNAME}/tasks"
4152
fi
@@ -49,8 +60,8 @@ cgroup_get_pids()
4960

5061
cgroup_running()
5162
{
52-
[ -d "/sys/fs/cgroup/unified/${RC_SVCNAME}" ] ||
53-
[ -d "/sys/fs/cgroup/${RC_SVCNAME}" ] ||
63+
[ -d "/sys/fs/cgroup/unified/$(cgroup_name)" ] ||
64+
[ -d "/sys/fs/cgroup/$(cgroup_name)" ] ||
5465
[ -d "/sys/fs/cgroup/openrc/${RC_SVCNAME}" ]
5566
}
5667

@@ -61,9 +72,16 @@ cgroup_set_values()
6172
local controller h
6273
controller="$1"
6374
h=$(cgroup_find_path "$1")
64-
cgroup="/sys/fs/cgroup/${1}${h}openrc_${RC_SVCNAME}"
75+
cgroup="/sys/fs/cgroup/${1}${h}$(cgroup_name)"
6576
[ -d "$cgroup" ] || mkdir -p "$cgroup"
6677

78+
if [ -w "$cgroup/tasks" ]; then
79+
veinfo "$RC_SVCNAME: adding to $cgroup/tasks"
80+
printf "%d" 0 > "$cgroup/tasks"
81+
fi
82+
83+
[ "$2" = "add" ] && return 0
84+
6785
set -- $2
6886
local name val
6987
while [ -n "$1" ] && [ "$controller" != "cpuacct" ]; do
@@ -90,11 +108,6 @@ cgroup_set_values()
90108
printf "%s" "$val" > "$cgroup/$name"
91109
fi
92110

93-
if [ -w "$cgroup/tasks" ]; then
94-
veinfo "$RC_SVCNAME: adding to $cgroup/tasks"
95-
printf "%d" 0 > "$cgroup/tasks"
96-
fi
97-
98111
return 0
99112
}
100113

@@ -164,10 +177,12 @@ cgroup2_find_path()
164177

165178
cgroup2_remove()
166179
{
180+
yesno "${rc_cgroup_keep:-no}" && return 0
181+
167182
local cgroup_path rc_cgroup_path
168183
cgroup_path="$(cgroup2_find_path)"
169184
[ -z "${cgroup_path}" ] && return 0
170-
rc_cgroup_path="${cgroup_path}/${RC_SVCNAME}"
185+
rc_cgroup_path="${cgroup_path}/$(cgroup_name)"
171186
[ ! -d "${rc_cgroup_path}" ] ||
172187
[ ! -e "${rc_cgroup_path}"/cgroup.events ] &&
173188
return 0
@@ -191,8 +206,8 @@ cgroup2_set_limits()
191206
cgroup_path="$(cgroup2_find_path)"
192207
[ -z "${cgroup_path}" ] && return 0
193208
mountinfo -q "${cgroup_path}"|| return 0
194-
rc_cgroup_path="${cgroup_path}/${RC_SVCNAME}"
195-
[ ! -d "${rc_cgroup_path}" ] && mkdir "${rc_cgroup_path}"
209+
rc_cgroup_path="${cgroup_path}/$(cgroup_name)"
210+
[ ! -d "${rc_cgroup_path}" ] && mkdir -p "${rc_cgroup_path}"
196211
[ -f "${rc_cgroup_path}"/cgroup.procs ] &&
197212
printf 0 > "${rc_cgroup_path}"/cgroup.procs
198213
[ -z "${rc_cgroup_settings}" ] && return 0
@@ -210,7 +225,7 @@ cgroup2_kill_cgroup() {
210225
local cgroup_path
211226
cgroup_path="$(cgroup2_find_path)"
212227
[ -z "${cgroup_path}" ] && return 1
213-
rc_cgroup_path="${cgroup_path}/${RC_SVCNAME}"
228+
rc_cgroup_path="${cgroup_path}/$(cgroup_name)"
214229
if [ -f "${rc_cgroup_path}"/cgroup.kill ]; then
215230
printf "%d" 1 > "${rc_cgroup_path}"/cgroup.kill
216231
fi

0 commit comments

Comments
 (0)