Systemd/src/shared/cpu-set-util.c
Zbigniew Jędrzejewski-Szmek d284b82b3e Move various files that don't need to be in basic/ to shared/
This doesn't have much effect on the final build, because we link libbasic.a
into libsystemd-shared.so, so in the end, all the object built from basic/
end up in libsystemd-shared. And when the static library is linked into binaries,
any objects that are included in it but are not used are trimmed. Hence, the
size of output artifacts doesn't change:

$ du -sb /var/tmp/inst*
54181861	/var/tmp/inst1    (old)
54207441	/var/tmp/inst1s   (old split-usr)
54182477	/var/tmp/inst2    (new)
54208041	/var/tmp/inst2s   (new split-usr)

(The negligible change in size is because libsystemd-shared.so is bigger
by a few hundred bytes. I guess it's because symbols are named differently
or something like that.)

The effect is on the build process, in particular partial builds. This change
effectively moves the requirements on some build steps toward the leaves of the
dependency tree. Two effects:
- when building items that do not depend on libsystemd-shared, we
  build less stuff for libbasic.a (which wouldn't be used anyway,
  so it's a net win).
- when building items that do depend on libshared, we reduce libbasic.a as a
  synchronization point, possibly allowing better parallelism.

Method:
1. copy list of .h files from src/basic/meson.build to /tmp/basic
2. $ for i in $(grep '.h$' /tmp/basic); do echo $i; git --no-pager grep "include \"$i\"" src/basic/ 'src/lib*' 'src/nss-*' 'src/journal/sd-journal.c' |grep -v "${i%.h}.c";echo ;done | less
2018-11-20 07:27:37 +01:00

100 lines
3 KiB
C

/* SPDX-License-Identifier: LGPL-2.1+ */
#include <errno.h>
#include <stddef.h>
#include <syslog.h>
#include "alloc-util.h"
#include "cpu-set-util.h"
#include "extract-word.h"
#include "log.h"
#include "macro.h"
#include "parse-util.h"
#include "string-util.h"
cpu_set_t* cpu_set_malloc(unsigned *ncpus) {
cpu_set_t *c;
unsigned n = 1024;
/* Allocates the cpuset in the right size */
for (;;) {
c = CPU_ALLOC(n);
if (!c)
return NULL;
if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), c) >= 0) {
CPU_ZERO_S(CPU_ALLOC_SIZE(n), c);
if (ncpus)
*ncpus = n;
return c;
}
CPU_FREE(c);
if (errno != EINVAL)
return NULL;
n *= 2;
}
}
int parse_cpu_set_internal(
const char *rvalue,
cpu_set_t **cpu_set,
bool warn,
const char *unit,
const char *filename,
unsigned line,
const char *lvalue) {
_cleanup_cpu_free_ cpu_set_t *c = NULL;
const char *p = rvalue;
unsigned ncpus = 0;
assert(rvalue);
for (;;) {
_cleanup_free_ char *word = NULL;
unsigned cpu, cpu_lower, cpu_upper;
int r;
r = extract_first_word(&p, &word, WHITESPACE ",", EXTRACT_QUOTES);
if (r == -ENOMEM)
return warn ? log_oom() : -ENOMEM;
if (r < 0)
return warn ? log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, rvalue) : r;
if (r == 0)
break;
if (!c) {
c = cpu_set_malloc(&ncpus);
if (!c)
return warn ? log_oom() : -ENOMEM;
}
r = parse_range(word, &cpu_lower, &cpu_upper);
if (r < 0)
return warn ? log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CPU affinity '%s'", word) : r;
if (cpu_lower >= ncpus || cpu_upper >= ncpus)
return warn ? log_syntax(unit, LOG_ERR, filename, line, EINVAL, "CPU out of range '%s' ncpus is %u", word, ncpus) : -EINVAL;
if (cpu_lower > cpu_upper) {
if (warn)
log_syntax(unit, LOG_WARNING, filename, line, 0, "Range '%s' is invalid, %u > %u, ignoring", word, cpu_lower, cpu_upper);
continue;
}
for (cpu = cpu_lower; cpu <= cpu_upper; cpu++)
CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c);
}
/* On success, sets *cpu_set and returns ncpus for the system. */
if (c)
*cpu_set = TAKE_PTR(c);
return (int) ncpus;
}