1 /*
2 * This file is part of sched_xetaffinity strace test.
3 *
4 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include "tests.h"
31 #include <asm/unistd.h>
32 #include <sched.h>
33
34 #if defined __NR_sched_getaffinity && defined __NR_sched_setaffinity \
35 && defined CPU_ISSET_S && defined CPU_ZERO_S && defined CPU_SET_S
36
37 # include <assert.h>
38 # include <errno.h>
39 # include <stdio.h>
40 # include <unistd.h>
41
42 static const char *errstr;
43
44 static int
getaffinity(unsigned long pid,unsigned long size,void * set)45 getaffinity(unsigned long pid, unsigned long size, void *set)
46 {
47 int rc = syscall(__NR_sched_getaffinity, pid, size, set);
48 errstr = sprintrc(rc);
49 return rc;
50 }
51
52 static int
setaffinity(unsigned long pid,unsigned long size,void * set)53 setaffinity(unsigned long pid, unsigned long size, void *set)
54 {
55 int rc = syscall(__NR_sched_setaffinity, pid, size, set);
56 errstr = sprintrc(rc);
57 return rc;
58 }
59
60 int
main(void)61 main(void)
62 {
63 unsigned int cpuset_size = 1;
64 const pid_t pid = getpid();
65
66 while (cpuset_size) {
67 assert(getaffinity(pid, cpuset_size, NULL) == -1);
68 if (EFAULT == errno)
69 break;
70 if (EINVAL != errno)
71 perror_msg_and_skip("sched_getaffinity");
72 printf("sched_getaffinity(%d, %u, NULL) = %s\n",
73 pid, cpuset_size, errstr);
74 cpuset_size <<= 1;
75 }
76 assert(cpuset_size);
77 printf("sched_getaffinity(%d, %u, NULL) = %s\n",
78 pid, cpuset_size, errstr);
79
80 cpu_set_t *cpuset = tail_alloc(cpuset_size);
81 getaffinity(pid, cpuset_size, cpuset + 1);
82 printf("sched_getaffinity(%d, %u, %p) = %s\n",
83 pid, cpuset_size, cpuset + 1, errstr);
84
85 int ret_size = getaffinity(pid, cpuset_size, cpuset);
86 if (ret_size < 0)
87 perror_msg_and_fail("sched_getaffinity(%d, %u, %p) = %s\n",
88 pid, (unsigned) cpuset_size, cpuset, errstr);
89 assert(ret_size <= (int) cpuset_size);
90
91 printf("sched_getaffinity(%d, %u, [", pid, cpuset_size);
92 const char *sep;
93 unsigned int i, cpu;
94 for (i = 0, cpu = 0, sep = ""; i < (unsigned) ret_size * 8; ++i) {
95 if (CPU_ISSET_S(i, (unsigned) ret_size, cpuset)) {
96 printf("%s%u", sep, i);
97 sep = ", ";
98 cpu = i;
99 }
100 }
101 printf("]) = %s\n", errstr);
102
103 CPU_ZERO_S(cpuset_size, cpuset);
104 CPU_SET_S(cpu, cpuset_size, cpuset);
105 if (setaffinity(pid, cpuset_size, cpuset))
106 perror_msg_and_skip("sched_setaffinity");
107 printf("sched_setaffinity(%d, %u, [%u]) = 0\n",
108 pid, cpuset_size, cpu);
109
110 const unsigned int big_size = cpuset_size < 128 ? 128 : cpuset_size * 2;
111 cpuset = tail_alloc(big_size);
112 ret_size = getaffinity(pid, big_size, cpuset);
113 if (ret_size < 0)
114 perror_msg_and_fail("sched_getaffinity(%d, %u, %p) = %s\n",
115 pid, big_size, cpuset, errstr);
116 assert(ret_size <= (int) big_size);
117 printf("sched_getaffinity(%d, %u, [", pid, big_size);
118 for (i = 0, sep = ""; i < (unsigned) ret_size * 8; ++i) {
119 if (CPU_ISSET_S(i, (unsigned) ret_size, cpuset)) {
120 printf("%s%u", sep, i);
121 sep = ", ";
122 }
123 }
124 printf("]) = %s\n", errstr);
125
126 puts("+++ exited with 0 +++");
127 return 0;
128 }
129
130 #else
131
132 SKIP_MAIN_UNDEFINED("__NR_sched_getaffinity && __NR_sched_setaffinity"
133 " && CPU_ISSET_S && CPU_ZERO_S && CPU_SET_S")
134
135 #endif
136