2 * Copyright (c) 2002-2004 Roland McGrath <roland@redhat.com>
3 * Copyright (c) 2009-2018 Dmitry V. Levin <ldv@altlinux.org>
6 * SPDX-License-Identifier: LGPL-2.1-or-later
15 static unsigned int cpuset_size;
19 * If the cpuset size passed to sched_getaffinity is less
20 * than necessary to store the bitmask, the kernel does not
21 * look at the mask pointer and fails with EINVAL.
23 * If the cpuset size is large enough, the kernel fails with
24 * EFAULT on inaccessible mask pointers.
26 * This undocumented kernel feature can be used to probe
27 * the kernel and find out the minimal valid cpuset size
28 * without allocating any memory for the CPU affinity mask.
32 sched_getaffinity(0, cpuset_size, NULL) == -1 &&
44 print_affinitylist(struct tcb *const tcp, const kernel_ulong_t addr,
45 const unsigned int len)
47 const unsigned int max_size = get_cpuset_size();
48 const unsigned int umove_size = len < max_size ? len : max_size;
49 const unsigned int size =
50 (umove_size + current_wordsize - 1) & -current_wordsize;
51 const unsigned int ncpu = size * 8;
54 if (!verbose(tcp) || (exiting(tcp) && syserror(tcp)) ||
55 !addr || !len || !(cpu = calloc(size, 1))) {
60 if (!umoven_or_printaddr(tcp, addr, umove_size, cpu)) {
66 i = next_set_bit(cpu, i, ncpu);
69 tprintf("%s%d", sep, i);
73 tprintf("%s...", sep);
80 SYS_FUNC(sched_setaffinity)
82 const int pid = tcp->u_arg[0];
83 const unsigned int len = tcp->u_arg[1];
85 tprintf("%d, %u, ", pid, len);
86 print_affinitylist(tcp, tcp->u_arg[2], len);
91 SYS_FUNC(sched_getaffinity)
93 const int pid = tcp->u_arg[0];
94 const unsigned int len = tcp->u_arg[1];
97 tprintf("%d, %u, ", pid, len);
99 print_affinitylist(tcp, tcp->u_arg[2], tcp->u_rval);