Skip to content

Commit b8b0b0b

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 44f5a72 commit b8b0b0b

2 files changed

Lines changed: 49 additions & 9 deletions

File tree

etc/rc.conf

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

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

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

sh/rc-cgroup.sh

Lines changed: 24 additions & 9 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
@@ -53,9 +64,16 @@ cgroup_set_values()
5364
local controller h
5465
controller="$1"
5566
h=$(cgroup_find_path "$1")
56-
cgroup="/sys/fs/cgroup/${1}${h}openrc_${RC_SVCNAME}"
67+
cgroup="/sys/fs/cgroup/${1}${h}$(cgroup_name)"
5768
[ -d "$cgroup" ] || mkdir -p "$cgroup"
5869

70+
if [ -w "$cgroup/tasks" ]; then
71+
veinfo "$RC_SVCNAME: adding to $cgroup/tasks"
72+
printf "%d" 0 > "$cgroup/tasks"
73+
fi
74+
75+
[ "$2" = "add" ] && return 0
76+
5977
set -- $2
6078
local name val
6179
while [ -n "$1" ] && [ "$controller" != "cpuacct" ]; do
@@ -82,11 +100,6 @@ cgroup_set_values()
82100
printf "%s" "$val" > "$cgroup/$name"
83101
fi
84102

85-
if [ -w "$cgroup/tasks" ]; then
86-
veinfo "$RC_SVCNAME: adding to $cgroup/tasks"
87-
printf "%d" 0 > "$cgroup/tasks"
88-
fi
89-
90103
return 0
91104
}
92105

@@ -156,10 +169,12 @@ cgroup2_find_path()
156169

157170
cgroup2_remove()
158171
{
172+
yesno "${rc_cgroup_keep:-no}" && return 0
173+
159174
local cgroup_path rc_cgroup_path
160175
cgroup_path="$(cgroup2_find_path)"
161176
[ -z "${cgroup_path}" ] && return 0
162-
rc_cgroup_path="${cgroup_path}/${RC_SVCNAME}"
177+
rc_cgroup_path="${cgroup_path}/$(cgroup_name)"
163178
[ ! -d "${rc_cgroup_path}" ] ||
164179
[ ! -e "${rc_cgroup_path}"/cgroup.events ] &&
165180
return 0
@@ -182,8 +197,8 @@ cgroup2_set_limits()
182197
local cgroup_path
183198
cgroup_path="$(cgroup2_find_path)"
184199
[ -d "${cgroup_path}" ] || return 0
185-
rc_cgroup_path="${cgroup_path}/${RC_SVCNAME}"
186-
[ ! -d "${rc_cgroup_path}" ] && mkdir "${rc_cgroup_path}"
200+
rc_cgroup_path="${cgroup_path}/$(cgroup_name)"
201+
[ ! -d "${rc_cgroup_path}" ] && mkdir -p "${rc_cgroup_path}"
187202
[ -f "${rc_cgroup_path}"/cgroup.procs ] &&
188203
printf 0 > "${rc_cgroup_path}"/cgroup.procs
189204
[ -z "${rc_cgroup_settings}" ] && return 0

0 commit comments

Comments
 (0)