X Tutup
Skip to content

Commit e2b2fb7

Browse files
committed
core: add support for setting CPUAffinity= to special "numa" value
systemd will automatically derive CPU affinity mask from NUMA node mask. Fixes systemd#13248
1 parent 1808f76 commit e2b2fb7

File tree

9 files changed

+125
-13
lines changed

9 files changed

+125
-13
lines changed

man/systemd.exec.xml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -774,10 +774,11 @@ CapabilityBoundingSet=~CAP_B CAP_C</programlisting>
774774
<term><varname>CPUAffinity=</varname></term>
775775

776776
<listitem><para>Controls the CPU affinity of the executed processes. Takes a list of CPU indices or ranges
777-
separated by either whitespace or commas. CPU ranges are specified by the lower and upper CPU indices separated
778-
by a dash. This option may be specified more than once, in which case the specified CPU affinity masks are
779-
merged. If the empty string is assigned, the mask is reset, all assignments prior to this will have no
780-
effect. See
777+
separated by either whitespace or commas. Alternatively, takes a special "numa" value in which case systemd
778+
automatically derives allowed CPU range based on the value of <varname>NUMAMask=</varname> option. CPU ranges
779+
are specified by the lower and upper CPU indices separated by a dash. This option may be specified more than
780+
once, in which case the specified CPU affinity masks are merged. If the empty string is assigned, the mask
781+
is reset, all assignments prior to this will have no effect. See
781782
<citerefentry><refentrytitle>sched_setaffinity</refentrytitle><manvolnum>2</manvolnum></citerefentry> for
782783
details.</para></listitem>
783784
</varlistentry>

src/core/dbus-execute.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ static BUS_DEFINE_PROPERTY_GET2(property_get_ioprio_priority, "i", ExecContext,
5656
static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_empty_string, "s", NULL);
5757
static BUS_DEFINE_PROPERTY_GET_REF(property_get_syslog_level, "i", int, LOG_PRI);
5858
static BUS_DEFINE_PROPERTY_GET_REF(property_get_syslog_facility, "i", int, LOG_FAC);
59+
static BUS_DEFINE_PROPERTY_GET(property_get_cpu_affinity_from_numa, "b", ExecContext, exec_context_get_cpu_affinity_from_numa);
60+
5961

6062
static int property_get_environment_files(
6163
sd_bus *bus,
@@ -213,14 +215,24 @@ static int property_get_cpu_affinity(
213215
sd_bus_error *error) {
214216

215217
ExecContext *c = userdata;
218+
_cleanup_(cpu_set_reset) CPUSet s = {};
216219
_cleanup_free_ uint8_t *array = NULL;
217220
size_t allocated;
218221

219222
assert(bus);
220223
assert(reply);
221224
assert(c);
222225

223-
(void) cpu_set_to_dbus(&c->cpu_set, &array, &allocated);
226+
if (c->cpu_affinity_from_numa) {
227+
int r;
228+
229+
r = numa_to_cpu_set(&c->numa_policy, &s);
230+
if (r < 0)
231+
return r;
232+
}
233+
234+
(void) cpu_set_to_dbus(c->cpu_affinity_from_numa ? &s : &c->cpu_set, &array, &allocated);
235+
224236
return sd_bus_message_append_array(reply, 'y', array, allocated);
225237
}
226238

@@ -741,6 +753,7 @@ const sd_bus_vtable bus_exec_vtable[] = {
741753
SD_BUS_PROPERTY("CPUSchedulingPolicy", "i", property_get_cpu_sched_policy, 0, SD_BUS_VTABLE_PROPERTY_CONST),
742754
SD_BUS_PROPERTY("CPUSchedulingPriority", "i", property_get_cpu_sched_priority, 0, SD_BUS_VTABLE_PROPERTY_CONST),
743755
SD_BUS_PROPERTY("CPUAffinity", "ay", property_get_cpu_affinity, 0, SD_BUS_VTABLE_PROPERTY_CONST),
756+
SD_BUS_PROPERTY("CPUAffinityFromNUMA", "b", property_get_cpu_affinity_from_numa, 0, SD_BUS_VTABLE_PROPERTY_CONST),
744757
SD_BUS_PROPERTY("NUMAPolicy", "i", property_get_numa_policy, 0, SD_BUS_VTABLE_PROPERTY_CONST),
745758
SD_BUS_PROPERTY("NUMAMask", "ay", property_get_numa_mask, 0, SD_BUS_VTABLE_PROPERTY_CONST),
746759
SD_BUS_PROPERTY("TimerSlackNSec", "t", property_get_timer_slack_nsec, 0, SD_BUS_VTABLE_PROPERTY_CONST),
@@ -1770,6 +1783,20 @@ int bus_exec_context_set_transient_property(
17701783

17711784
return 1;
17721785

1786+
} else if (streq(name, "CPUAffinityFromNUMA")) {
1787+
int q;
1788+
1789+
r = sd_bus_message_read_basic(message, 'b', &q);
1790+
if (r < 0)
1791+
return r;
1792+
1793+
if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
1794+
c->cpu_affinity_from_numa = q;
1795+
unit_write_settingf(u, flags, name, "%s=%s", "CPUAffinity", "numa");
1796+
}
1797+
1798+
return 1;
1799+
17731800
} else if (streq(name, "NUMAPolicy")) {
17741801
int32_t type;
17751802

@@ -1784,6 +1811,7 @@ int bus_exec_context_set_transient_property(
17841811
c->numa_policy.type = type;
17851812

17861813
return 1;
1814+
17871815
} else if (streq(name, "Nice")) {
17881816
int32_t q;
17891817

src/core/execute.c

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3021,6 +3021,33 @@ static int exec_parameters_get_cgroup_path(const ExecParameters *params, char **
30213021
return using_subcgroup;
30223022
}
30233023

3024+
static int exec_context_cpu_affinity_from_numa(const ExecContext *c, CPUSet *ret) {
3025+
_cleanup_(cpu_set_reset) CPUSet s = {};
3026+
int r;
3027+
3028+
assert(c);
3029+
assert(ret);
3030+
3031+
if (!c->numa_policy.nodes.set) {
3032+
log_debug("Can't derive CPU affinity mask from NUMA mask because NUMA mask is not set, ignoring");
3033+
return 0;
3034+
}
3035+
3036+
r = numa_to_cpu_set(&c->numa_policy, &s);
3037+
if (r < 0)
3038+
return r;
3039+
3040+
cpu_set_reset(ret);
3041+
3042+
return cpu_set_add_all(ret, &s);
3043+
}
3044+
3045+
bool exec_context_get_cpu_affinity_from_numa(const ExecContext *c) {
3046+
assert(c);
3047+
3048+
return c->cpu_affinity_from_numa;
3049+
}
3050+
30243051
static int exec_child(
30253052
Unit *unit,
30263053
const ExecCommand *command,
@@ -3318,11 +3345,26 @@ static int exec_child(
33183345
}
33193346
}
33203347

3321-
if (context->cpu_set.set)
3322-
if (sched_setaffinity(0, context->cpu_set.allocated, context->cpu_set.set) < 0) {
3348+
if (context->cpu_affinity_from_numa || context->cpu_set.set) {
3349+
_cleanup_(cpu_set_reset) CPUSet converted_cpu_set = {};
3350+
const CPUSet *cpu_set;
3351+
3352+
if (context->cpu_affinity_from_numa) {
3353+
r = exec_context_cpu_affinity_from_numa(context, &converted_cpu_set);
3354+
if (r < 0) {
3355+
*exit_status = EXIT_CPUAFFINITY;
3356+
return log_unit_error_errno(unit, r, "Failed to derive CPU affinity mask from NUMA mask: %m");
3357+
}
3358+
3359+
cpu_set = &converted_cpu_set;
3360+
} else
3361+
cpu_set = &context->cpu_set;
3362+
3363+
if (sched_setaffinity(0, cpu_set->allocated, cpu_set->set) < 0) {
33233364
*exit_status = EXIT_CPUAFFINITY;
33243365
return log_unit_error_errno(unit, errno, "Failed to set up CPU affinity: %m");
33253366
}
3367+
}
33263368

33273369
if (mpol_is_valid(numa_policy_get_type(&context->numa_policy))) {
33283370
r = apply_numa_policy(&context->numa_policy);

src/core/execute.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ struct ExecContext {
182182

183183
CPUSet cpu_set;
184184
NUMAPolicy numa_policy;
185+
bool cpu_affinity_from_numa;
185186

186187
ExecInput std_input;
187188
ExecOutput std_output;
@@ -406,6 +407,8 @@ void exec_runtime_vacuum(Manager *m);
406407

407408
void exec_params_clear(ExecParameters *p);
408409

410+
bool exec_context_get_cpu_affinity_from_numa(const ExecContext *c);
411+
409412
const char* exec_output_to_string(ExecOutput i) _const_;
410413
ExecOutput exec_output_from_string(const char *s) _pure_;
411414

src/core/load-fragment.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1330,13 +1330,25 @@ int config_parse_exec_cpu_affinity(const char *unit,
13301330
void *userdata) {
13311331

13321332
ExecContext *c = data;
1333+
int r;
13331334

13341335
assert(filename);
13351336
assert(lvalue);
13361337
assert(rvalue);
13371338
assert(data);
13381339

1339-
return parse_cpu_set_extend(rvalue, &c->cpu_set, true, unit, filename, line, lvalue);
1340+
if (streq(rvalue, "numa")) {
1341+
c->cpu_affinity_from_numa = true;
1342+
cpu_set_reset(&c->cpu_set);
1343+
1344+
return 0;
1345+
}
1346+
1347+
r = parse_cpu_set_extend(rvalue, &c->cpu_set, true, unit, filename, line, lvalue);
1348+
if (r >= 0)
1349+
c->cpu_affinity_from_numa = false;
1350+
1351+
return r;
13401352
}
13411353

13421354
int config_parse_capability_set(

src/shared/bus-unit-util.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "signal-util.h"
3030
#include "socket-util.h"
3131
#include "sort-util.h"
32+
#include "stdio-util.h"
3233
#include "string-util.h"
3334
#include "syslog-util.h"
3435
#include "terminal-util.h"
@@ -1103,6 +1104,13 @@ static int bus_append_execute_property(sd_bus_message *m, const char *field, con
11031104
_cleanup_free_ uint8_t *array = NULL;
11041105
size_t allocated;
11051106

1107+
if (eq && streq(eq, "numa")) {
1108+
r = sd_bus_message_append(m, "(sv)", "CPUAffinityFromNUMA", "b", true);
1109+
if (r < 0)
1110+
return bus_log_create_error(r);
1111+
return r;
1112+
}
1113+
11061114
r = parse_cpu_set(eq, &cpuset);
11071115
if (r < 0)
11081116
return log_error_errno(r, "Failed to parse %s value: %s", field, eq);

src/shared/cpu-set-util.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ int cpu_set_add_all(CPUSet *a, const CPUSet *b) {
131131
return r;
132132
}
133133

134-
return 0;
134+
return 1;
135135
}
136136

137137
int parse_cpu_set_full(
@@ -216,7 +216,7 @@ int parse_cpu_set_extend(
216216
if (!old->set) {
217217
*old = cpuset;
218218
cpuset = (CPUSet) {};
219-
return 0;
219+
return 1;
220220
}
221221

222222
return cpu_set_add_all(old, &cpuset);

src/test/test-cpu-set-util.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,12 @@ static void test_parse_cpu_set_extend(void) {
216216

217217
log_info("/* %s */", __func__);
218218

219-
assert_se(parse_cpu_set_extend("1 3", &c, true, NULL, "fake", 1, "CPUAffinity") == 0);
219+
assert_se(parse_cpu_set_extend("1 3", &c, true, NULL, "fake", 1, "CPUAffinity") == 1);
220220
assert_se(CPU_COUNT_S(c.allocated, c.set) == 2);
221221
assert_se(s1 = cpu_set_to_string(&c));
222222
log_info("cpu_set_to_string: %s", s1);
223223

224-
assert_se(parse_cpu_set_extend("4", &c, true, NULL, "fake", 1, "CPUAffinity") == 0);
224+
assert_se(parse_cpu_set_extend("4", &c, true, NULL, "fake", 1, "CPUAffinity") == 1);
225225
assert_se(CPU_COUNT_S(c.allocated, c.set) == 3);
226226
assert_se(s2 = cpu_set_to_string(&c));
227227
log_info("cpu_set_to_string: %s", s2);
@@ -238,7 +238,7 @@ static void test_cpu_set_to_from_dbus(void) {
238238

239239
log_info("/* %s */", __func__);
240240

241-
assert_se(parse_cpu_set_extend("1 3 8 100-200", &c, true, NULL, "fake", 1, "CPUAffinity") == 0);
241+
assert_se(parse_cpu_set_extend("1 3 8 100-200", &c, true, NULL, "fake", 1, "CPUAffinity") == 1);
242242
assert_se(s = cpu_set_to_string(&c));
243243
log_info("cpu_set_to_string: %s", s);
244244
assert_se(CPU_COUNT_S(c.allocated, c.set) == 104);

test/TEST-36-NUMAPOLICY/testsuite.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,18 @@ else
279279
# Maks must be ignored
280280
grep -E "set_mempolicy\((MPOL_LOCAL|0x4 [^,]*), NULL" $straceLog
281281

282+
echo "Unit file CPUAffinity=NUMA support"
283+
writeTestUnitNUMAPolicy "bind" "0"
284+
echo "CPUAffinity=numa" >> $testUnitNUMAConf
285+
systemctl daemon-reload
286+
systemctl start $testUnit
287+
systemctlCheckNUMAProperties $testUnit "bind" "0"
288+
pid=$(systemctl show --value -p MainPID $testUnit)
289+
cpulist=$(cat /sys/devices/system/node/node0/cpulist)
290+
affinity_systemd=$(systemctl show --value -p CPUAffinity $testUnit)
291+
[ $cpulist = $affinity_systemd ]
292+
pid1StopUnit $testUnit
293+
282294
echo "systemd-run NUMAPolicy support"
283295
runUnit='numa-systemd-run-test.service'
284296

@@ -309,6 +321,12 @@ else
309321
systemd-run -p NUMAPolicy=local -p NUMAMask=0 --unit $runUnit sleep 1000
310322
systemctlCheckNUMAProperties $runUnit "local" ""
311323
pid1StopUnit $runUnit
324+
325+
systemd-run -p NUMAPolicy=local -p NUMAMask=0 -p CPUAffinity=numa --unit $runUnit sleep 1000
326+
systemctlCheckNUMAProperties $runUnit "local" ""
327+
systemctl cat $runUnit | grep -q 'CPUAffinity=numa'
328+
pid1StopUnit $runUnit
329+
312330
fi
313331

314332
# Cleanup

0 commit comments

Comments
 (0)
X Tutup