]> granicus.if.org Git - strace/blob - tests/sched_xetaffinity.c
Fix and enhance decoding of sched_[gs]etaffinity syscalls
[strace] / tests / sched_xetaffinity.c
1 /*
2  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
15  *
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.
26  */
27
28 #include "tests.h"
29 #include <sys/syscall.h>
30 #include <sched.h>
31
32 #if defined __NR_sched_getaffinity && defined __NR_sched_setaffinity \
33  && defined CPU_ISSET_S && defined CPU_ZERO_S && defined CPU_SET_S
34
35 # include <assert.h>
36 # include <errno.h>
37 # include <stdio.h>
38 # include <unistd.h>
39
40 static int
41 getaffinity(unsigned long pid, unsigned long size, void *set)
42 {
43         return syscall(__NR_sched_getaffinity, pid, size, set);
44 }
45
46 static int
47 setaffinity(unsigned long pid, unsigned long size, void *set)
48 {
49         return syscall(__NR_sched_setaffinity, pid, size, set);
50 }
51
52 int
53 main(void)
54 {
55         unsigned int cpuset_size = 1;
56         pid_t pid = getpid();
57
58         while (cpuset_size) {
59                 assert(getaffinity(pid, cpuset_size, NULL) == -1);
60                 if (EFAULT == errno)
61                         break;
62                 if (EINVAL != errno)
63                         perror_msg_and_skip("sched_getaffinity");
64                 printf("sched_getaffinity(%d, %u, NULL) = -1 EINVAL (%m)\n",
65                        pid, cpuset_size);
66                 cpuset_size <<= 1;
67         }
68         assert(cpuset_size);
69         printf("sched_getaffinity(%d, %u, NULL) = -1 EFAULT (%m)\n",
70                pid, cpuset_size);
71
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);
75         const char *sep;
76         unsigned int i, cpu;
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);
80                         sep = " ";
81                         cpu = i;
82                 }
83         }
84         printf("]) = %u\n", cpuset_size);
85
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);
92
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);
101                         sep = " ";
102                 }
103         }
104         printf("]) = %d\n", ret_size);
105
106         puts("+++ exited with 0 +++");
107         return 0;
108 }
109
110 #else
111
112 SKIP_MAIN_UNDEFINED("__NR_sched_getaffinity && __NR_sched_setaffinity"
113                     " && CPU_ISSET_S && CPU_ZERO_S && CPU_SET_S")
114
115 #endif