blob: 5c30763915e4482ec3d3ef815d140ccd8f60905f [file] [log] [blame]
subrata_modak5e975a52009-04-22 10:48:10 +00001/******************************************************************************/
2/* */
3/* Copyright (c) 2009 FUJITSU LIMITED */
4/* */
5/* This program is free software; you can redistribute it and/or modify */
6/* it under the terms of the GNU General Public License as published by */
7/* the Free Software Foundation; either version 2 of the License, or */
8/* (at your option) any later version. */
9/* */
10/* This program is distributed in the hope that it will be useful, */
11/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
12/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See */
13/* the GNU General Public License for more details. */
14/* */
15/* You should have received a copy of the GNU General Public License */
16/* along with this program; if not, write to the Free Software */
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
subrata_modak5e975a52009-04-22 10:48:10 +000018/* */
19/* Author: Miao Xie <miaox@cn.fujitsu.com> */
20/* */
21/******************************************************************************/
22
23#define _GNU_SOURCE
24
25#include <sched.h>
26#include <unistd.h>
27#include <stdlib.h>
28#include <stdio.h>
29#include <signal.h>
30#include <err.h>
31#include <limits.h>
32#include <getopt.h>
33#include <string.h>
34#include <fcntl.h>
35#include <sys/types.h>
36#include <sys/stat.h>
37#include <sys/mman.h>
38#include <sys/shm.h>
39#include <syscall.h>
yaberauneya0e768172009-11-04 05:21:36 +000040#include <inttypes.h>
yaberauneyaef772532009-10-09 17:55:43 +000041#include "config.h"
yaberauneya0e768172009-11-04 05:21:36 +000042#include "linux_syscall_numbers.h"
43#include "test.h"
44#include "usctest.h"
Garrett Cooper1e587382011-03-15 05:02:48 -070045
46char *TCID = "cpuset_syscall_test";
47
yaberauneyaef772532009-10-09 17:55:43 +000048#if HAVE_LINUX_MEMPOLICY_H
subrata_modak5e975a52009-04-22 10:48:10 +000049#include <linux/mempolicy.h>
50
subrata_modak71b936e2009-04-25 18:10:58 +000051#include "../cpuset_lib/cpuset.h"
52#include "../cpuset_lib/bitmask.h"
53
yaberauneyac5d88052009-11-30 08:41:37 +000054int TST_TOTAL = 1;
55
subrata_modak5e975a52009-04-22 10:48:10 +000056unsigned long mask;
57int test = -1;
58int flag_exit;
59int ret;
60
Garrett Cooper6c4d1c62010-12-17 17:32:45 -080061#if HAVE_DECL_MPOL_F_MEMS_ALLOWED
subrata_modak5e975a52009-04-22 10:48:10 +000062static int get_mempolicy(int *policy, unsigned long *nmask,
Wanlong Gao354ebb42012-12-07 10:10:04 +080063 unsigned long maxnode, void *addr, int flags)
subrata_modak5e975a52009-04-22 10:48:10 +000064{
Jan Stancek359980f2013-02-15 10:16:05 +010065 return ltp_syscall(__NR_get_mempolicy, policy, nmask, maxnode, addr,
66 flags);
subrata_modak5e975a52009-04-22 10:48:10 +000067}
Garrett Cooper6c4d1c62010-12-17 17:32:45 -080068#endif
subrata_modak5e975a52009-04-22 10:48:10 +000069
Garrett Cooper6c4d1c62010-12-17 17:32:45 -080070#if HAVE_DECL_MPOL_BIND
Wanlong Gao354ebb42012-12-07 10:10:04 +080071static int mbind(void *start, unsigned long len, int policy,
72 unsigned long *nodemask, unsigned long maxnode, unsigned flags)
subrata_modak5e975a52009-04-22 10:48:10 +000073{
Jan Stancek359980f2013-02-15 10:16:05 +010074 return ltp_syscall(__NR_mbind, start, len, policy, nodemask, maxnode,
Wanlong Gao354ebb42012-12-07 10:10:04 +080075 flags);
subrata_modak5e975a52009-04-22 10:48:10 +000076}
77
Wanlong Gao354ebb42012-12-07 10:10:04 +080078static int set_mempolicy(int policy, unsigned long *nodemask,
79 unsigned long maxnode)
subrata_modak5e975a52009-04-22 10:48:10 +000080{
Jan Stancek359980f2013-02-15 10:16:05 +010081 return ltp_syscall(__NR_set_mempolicy, policy, nodemask, maxnode);
subrata_modak5e975a52009-04-22 10:48:10 +000082}
Garrett Cooper6c4d1c62010-12-17 17:32:45 -080083#endif
subrata_modak5e975a52009-04-22 10:48:10 +000084
85#define OPT_setaffinity (SCHAR_MAX + 1)
86#define OPT_getaffinity (SCHAR_MAX + 2)
87#define OPT_mbind (SCHAR_MAX + 3)
88#define OPT_set_mempolicy (SCHAR_MAX + 4)
89#define OPT_get_mempolicy (SCHAR_MAX + 5)
90
91const struct option long_opts[] = {
Wanlong Gao354ebb42012-12-07 10:10:04 +080092 {"setaffinity", 1, NULL, OPT_setaffinity},
93 {"getaffinity", 0, NULL, OPT_getaffinity},
94 {"mbind", 1, NULL, OPT_mbind},
95 {"set_mempolicy", 1, NULL, OPT_set_mempolicy},
96 {"get_mempolicy", 0, NULL, OPT_get_mempolicy},
97 {NULL, 0, NULL, 0},
subrata_modak5e975a52009-04-22 10:48:10 +000098};
99
100void process_options(int argc, char *argv[])
101{
102 int c;
103 char *end;
subrata_modak5e975a52009-04-22 10:48:10 +0000104
105 while (1) {
106 c = getopt_long(argc, argv, "", long_opts, NULL);
107 if (c == -1)
108 break;
109
110 switch (c) {
111 case OPT_setaffinity:
112 test = 0;
113 mask = strtoul(optarg, &end, 10);
114 if (*end != '\0')
115 errx(1, "wrong -s argument!");
116 break;
117 case OPT_getaffinity:
118 test = 1;
119 break;
120 case OPT_mbind:
121 test = 2;
122 mask = strtoul(optarg, &end, 10);
123 if (*end != '\0')
124 errx(1, "wrong -s argument!");
125 break;
126 case OPT_set_mempolicy:
127 test = 3;
128 mask = strtoul(optarg, &end, 10);
129 if (*end != '\0')
130 errx(1, "wrong -s argument!");
131 break;
132 case OPT_get_mempolicy:
133 test = 4;
134 break;
135 default:
136 errx(1, "unknown option!\n");
137 break;
138 }
139 }
140}
141
Wanlong Gao354ebb42012-12-07 10:10:04 +0800142void sigint_handler(int __attribute__ ((unused)) signo)
subrata_modak5e975a52009-04-22 10:48:10 +0000143{
144 flag_exit = 1;
145}
146
147void test_setaffinity(void)
148{
149 cpu_set_t tmask;
150 int i;
151 CPU_ZERO(&tmask);
152 for (i = 0; i < 8 * sizeof(mask); i++) {
153 if ((1 << i) & mask)
154 CPU_SET(i, &tmask);
155 }
156 ret = sched_setaffinity(0, sizeof(tmask), &tmask);
157}
158
159void test_getaffinity(void)
160{
161 cpu_set_t tmask;
162 int i;
163 CPU_ZERO(&tmask);
164 ret = sched_getaffinity(0, sizeof(tmask), &tmask);
165 for (i = 0; i < 8 * sizeof(mask); i++) {
166 if (CPU_ISSET(i, &tmask))
167 printf("%d,", i);
168 }
169}
170
171void test_mbind(void)
172{
173 void *addr;
174 int len = 10 * 1024 * 1024;
175 addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
176 if (addr == MAP_FAILED) {
177 ret = 1;
178 return;
179 }
yaberauneyaf12a19a2009-12-19 05:21:23 +0000180 printf("%p\n", addr);
yaberauneya618f41a2010-01-14 08:18:41 +0000181#if HAVE_DECL_MPOL_BIND
subrata_modak5e975a52009-04-22 10:48:10 +0000182 ret = mbind(addr, len, MPOL_BIND, &mask, 8 * sizeof(mask), 0);
yaberauneya8990a202010-01-14 07:53:11 +0000183#else
184 ret = 1;
185#endif
subrata_modak5e975a52009-04-22 10:48:10 +0000186}
187
188void test_set_mempolicy(void)
189{
yaberauneya618f41a2010-01-14 08:18:41 +0000190#if HAVE_DECL_MPOL_BIND
subrata_modak5e975a52009-04-22 10:48:10 +0000191 ret = set_mempolicy(MPOL_BIND, &mask, 8 * sizeof(mask));
yaberauneya8990a202010-01-14 07:53:11 +0000192#else
193 ret = -1;
194#endif
subrata_modak5e975a52009-04-22 10:48:10 +0000195}
196
197void test_get_mempolicy(void)
198{
subrata_modak71b936e2009-04-25 18:10:58 +0000199 int nbits;
200 struct bitmask *nmask;
201 char str[256];
202
203 nbits = cpuset_mems_nbits();
204 if (nbits <= 0) {
205 warn("get the nbits of nodes failed");
206 ret = 1;
207 return;
208 }
209
210 nmask = bitmask_alloc(nbits);
211 if (nmask == NULL) {
212 warn("alloc bitmask failed");
213 ret = 1;
214 return;
215 }
yaberauneya618f41a2010-01-14 08:18:41 +0000216#if HAVE_DECL_MPOL_F_MEMS_ALLOWED
subrata_modak71b936e2009-04-25 18:10:58 +0000217 ret = get_mempolicy(NULL, bitmask_mask(nmask), bitmask_nbits(nmask), 0,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800218 MPOL_F_MEMS_ALLOWED);
yaberauneya8990a202010-01-14 07:53:11 +0000219#else
220 ret = -1;
221#endif
subrata_modak71b936e2009-04-25 18:10:58 +0000222
223 bitmask_displaylist(str, 256, nmask);
yaberauneya397ae832010-01-19 08:04:00 +0000224 puts(str);
subrata_modak5e975a52009-04-22 10:48:10 +0000225}
226
Wanlong Gao354ebb42012-12-07 10:10:04 +0800227void sigusr_handler(int __attribute__ ((unused)) signo)
subrata_modak5e975a52009-04-22 10:48:10 +0000228{
229 switch (test) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800230 case 0:
231 test_setaffinity();
232 break;
233 case 1:
234 test_getaffinity();
235 break;
236 case 2:
237 test_mbind();
238 break;
239 case 3:
240 test_set_mempolicy();
241 break;
242 case 4:
243 test_get_mempolicy();
244 break;
subrata_modak5e975a52009-04-22 10:48:10 +0000245 default:;
246 }
247 test = -1;
248}
249
250int main(int argc, char *argv[])
251{
252 struct sigaction sigint_action;
253 struct sigaction sigusr_action;
254
255 memset(&sigint_action, 0, sizeof(sigint_action));
256 sigint_action.sa_handler = &sigint_handler;
257 sigaction(SIGINT, &sigint_action, NULL);
258
259 memset(&sigusr_action, 0, sizeof(sigusr_action));
260 sigusr_action.sa_handler = &sigusr_handler;
261 sigaction(SIGUSR1, &sigusr_action, NULL);
262
263 process_options(argc, argv);
264
265 while (!flag_exit)
266 sleep(1);
267
268 return ret;
269}
yaberauneya397ae832010-01-19 08:04:00 +0000270#else
Wanlong Gao354ebb42012-12-07 10:10:04 +0800271int main(void)
272{
yaberauneya397ae832010-01-19 08:04:00 +0000273 printf("System doesn't have required mempolicy support\n");
Garrett Cooper2c282152010-12-16 00:55:50 -0800274 tst_exit();
yaberauneya397ae832010-01-19 08:04:00 +0000275}
Garrett Cooper1e587382011-03-15 05:02:48 -0700276#endif