aboutsummaryrefslogtreecommitdiff
path: root/libbb/alloc_affinity.c
diff options
context:
space:
mode:
Diffstat (limited to 'libbb/alloc_affinity.c')
-rw-r--r--libbb/alloc_affinity.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/libbb/alloc_affinity.c b/libbb/alloc_affinity.c
new file mode 100644
index 000000000..b6d9f649a
--- /dev/null
+++ b/libbb/alloc_affinity.c
@@ -0,0 +1,29 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) 2024 Denys Vlasenko
6 *
7 * Licensed under GPLv2, see file LICENSE in this source tree.
8 */
9#include <sched.h>
10#include "libbb.h"
11
12unsigned long* FAST_FUNC get_malloc_cpu_affinity(int pid, unsigned *sz)
13{
14 unsigned long *mask = NULL;
15 unsigned sz_in_bytes = *sz;
16
17 for (;;) {
18 mask = xrealloc(mask, sz_in_bytes);
19 if (sched_getaffinity(pid, sz_in_bytes, (void*)mask) == 0)
20 break; /* got it */
21 sz_in_bytes *= 2;
22 if (errno == EINVAL && (int)sz_in_bytes > 0)
23 continue;
24 bb_perror_msg_and_die("can't %cet pid %d's affinity", 'g', pid);
25 }
26 //bb_error_msg("get mask[0]:%lx sz_in_bytes:%d", mask[0], sz_in_bytes);
27 *sz = sz_in_bytes;
28 return mask;
29}