2 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/syscall.h>
32 #if defined __NR_sched_getaffinity && defined __NR_sched_setaffinity \
33 && defined CPU_ISSET_S && defined CPU_ZERO_S && defined CPU_SET_S
41 getaffinity(unsigned long pid, unsigned long size, void *set)
43 return syscall(__NR_sched_getaffinity, pid, size, set);
47 setaffinity(unsigned long pid, unsigned long size, void *set)
49 return syscall(__NR_sched_setaffinity, pid, size, set);
55 unsigned int cpuset_size = 1;
59 assert(getaffinity(pid, cpuset_size, NULL) == -1);
63 perror_msg_and_skip("sched_getaffinity");
64 printf("sched_getaffinity(%d, %u, NULL) = -1 EINVAL (%m)\n",
69 printf("sched_getaffinity(%d, %u, NULL) = -1 EFAULT (%m)\n",
72 cpu_set_t *cpuset = tail_alloc(cpuset_size);
73 assert(getaffinity(pid, cpuset_size, cpuset) == (int) cpuset_size);
74 printf("sched_getaffinity(%d, %u, [", pid, cpuset_size);
77 for (i = 0, cpu = 0, sep = ""; i < cpuset_size * 8; ++i) {
78 if (CPU_ISSET_S(i, cpuset_size, cpuset)) {
79 printf("%s%u", sep, i);
84 printf("]) = %u\n", cpuset_size);
86 CPU_ZERO_S(cpuset_size, cpuset);
87 CPU_SET_S(cpu, cpuset_size, cpuset);
88 if (setaffinity(pid, cpuset_size, cpuset))
89 perror_msg_and_skip("sched_setaffinity");
90 printf("sched_setaffinity(%d, %u, [%u]) = 0\n",
91 pid, cpuset_size, cpu);
93 const unsigned int big_size = cpuset_size < 128 ? 128 : cpuset_size * 2;
94 cpuset = tail_alloc(big_size);
95 const int ret_size = getaffinity(pid, big_size, cpuset);
96 assert(ret_size >= (int) cpuset_size && ret_size <= (int) big_size);
97 printf("sched_getaffinity(%d, %u, [", pid, big_size);
98 for (i = 0, sep = ""; i < (unsigned) ret_size * 8; ++i) {
99 if (CPU_ISSET_S(i, (unsigned) ret_size, cpuset)) {
100 printf("%s%u", sep, i);
104 printf("]) = %d\n", ret_size);
106 puts("+++ exited with 0 +++");
112 SKIP_MAIN_UNDEFINED("__NR_sched_getaffinity && __NR_sched_setaffinity"
113 " && CPU_ISSET_S && CPU_ZERO_S && CPU_SET_S")