Skip to content

Commit b9f0285

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 6b475ab commit b9f0285

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_cggroup_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.in

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

15+
cgroup_name()
16+
{
17+
if [ -z "${rc_cgroup_name}" ]; then
18+
rc_cgroup_name="openrc_${RC_SVCNAME}"
19+
[ -n "${rc_cgroup_name_prefix}" ] &&
20+
rc_cgroup_name="${rc_cgroup_name_prefix}/${rc_cgroup_name}"
21+
fi
22+
23+
printf "%s" "${rc_cgroup_name}"
24+
}
25+
1526
cgroup_find_path()
1627
{
1728
local OIFS name dir result
@@ -54,9 +65,16 @@ cgroup_set_values()
5465
local controller h
5566
controller="$1"
5667
h=$(cgroup_find_path "$1")
57-
cgroup="/sys/fs/cgroup/${1}${h}openrc_${RC_SVCNAME}"
68+
cgroup="/sys/fs/cgroup/${1}${h}$(cgroup_name)"
5869
[ -d "$cgroup" ] || mkdir -p "$cgroup"
5970

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

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

@@ -157,10 +170,12 @@ cgroup2_find_path()
157170

158171
cgroup2_remove()
159172
{
173+
yesno "${rc_cgroup_keep:-no}" && return 0
174+
160175
local cgroup_path rc_cgroup_path
161176
cgroup_path="$(cgroup2_find_path)"
162177
[ -z "${cgroup_path}" ] && return 0
163-
rc_cgroup_path="${cgroup_path}/${RC_SVCNAME}"
178+
rc_cgroup_path="${cgroup_path}/$(cgroup_name)"
164179
[ ! -d "${rc_cgroup_path}" ] ||
165180
[ ! -e "${rc_cgroup_path}"/cgroup.events ] &&
166181
return 0
@@ -183,8 +198,8 @@ cgroup2_set_limits()
183198
local cgroup_path
184199
cgroup_path="$(cgroup2_find_path)"
185200
[ -d "${cgroup_path}" ] || return 0
186-
rc_cgroup_path="${cgroup_path}/${RC_SVCNAME}"
187-
[ ! -d "${rc_cgroup_path}" ] && mkdir "${rc_cgroup_path}"
201+
rc_cgroup_path="${cgroup_path}/$(cgroup_name)"
202+
[ ! -d "${rc_cgroup_path}" ] && mkdir -p "${rc_cgroup_path}"
188203
[ -f "${rc_cgroup_path}"/cgroup.procs ] &&
189204
printf 0 > "${rc_cgroup_path}"/cgroup.procs
190205
[ -z "${rc_cgroup_settings}" ] && return 0

0 commit comments

Comments
 (0)