blob: da7669da6b5ae3c16836e3d529f6eb2305d52a10 [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
Stanislav Kholmanskikh87156a02013-10-01 17:44:50 +040025#include "config.h"
subrata_modak5e975a52009-04-22 10:48:10 +000026#include <sched.h>
27#include <unistd.h>
28#include <stdlib.h>
29#include <stdio.h>
30#include <signal.h>
31#include <err.h>
32#include <limits.h>
33#include <getopt.h>
34#include <string.h>
35#include <fcntl.h>
36#include <sys/types.h>
37#include <sys/stat.h>
38#include <sys/mman.h>
39#include <sys/shm.h>
40#include <syscall.h>
yaberauneya0e768172009-11-04 05:21:36 +000041#include <inttypes.h>
Stanislav Kholmanskikh87156a02013-10-01 17:44:50 +040042#if HAVE_NUMA_H
43#include <numa.h>
44#endif
45#if HAVE_NUMAIF_H
46#include <numaif.h>
47#endif
48
yaberauneya0e768172009-11-04 05:21:36 +000049#include "test.h"
Garrett Cooper1e587382011-03-15 05:02:48 -070050
51char *TCID = "cpuset_syscall_test";
Stanislav Kholmanskikh87156a02013-10-01 17:44:50 +040052int TST_TOTAL = 1;
Garrett Cooper1e587382011-03-15 05:02:48 -070053
Stanislav Kholmanskikh87156a02013-10-01 17:44:50 +040054#if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
55 && HAVE_MPOL_CONSTANTS
subrata_modak5e975a52009-04-22 10:48:10 +000056
subrata_modak71b936e2009-04-25 18:10:58 +000057#include "../cpuset_lib/cpuset.h"
58#include "../cpuset_lib/bitmask.h"
59
Stanislav Kholmanskikh87156a02013-10-01 17:44:50 +040060static unsigned long mask;
61static int test = -1;
62static int flag_exit;
63static int ret;
subrata_modak5e975a52009-04-22 10:48:10 +000064
65#define OPT_setaffinity (SCHAR_MAX + 1)
66#define OPT_getaffinity (SCHAR_MAX + 2)
67#define OPT_mbind (SCHAR_MAX + 3)
68#define OPT_set_mempolicy (SCHAR_MAX + 4)
69#define OPT_get_mempolicy (SCHAR_MAX + 5)
70
71const struct option long_opts[] = {
Wanlong Gao354ebb42012-12-07 10:10:04 +080072 {"setaffinity", 1, NULL, OPT_setaffinity},
73 {"getaffinity", 0, NULL, OPT_getaffinity},
74 {"mbind", 1, NULL, OPT_mbind},
75 {"set_mempolicy", 1, NULL, OPT_set_mempolicy},
76 {"get_mempolicy", 0, NULL, OPT_get_mempolicy},
77 {NULL, 0, NULL, 0},
subrata_modak5e975a52009-04-22 10:48:10 +000078};
79
80void process_options(int argc, char *argv[])
81{
82 int c;
83 char *end;
subrata_modak5e975a52009-04-22 10:48:10 +000084
85 while (1) {
86 c = getopt_long(argc, argv, "", long_opts, NULL);
87 if (c == -1)
88 break;
89
90 switch (c) {
91 case OPT_setaffinity:
92 test = 0;
93 mask = strtoul(optarg, &end, 10);
94 if (*end != '\0')
95 errx(1, "wrong -s argument!");
96 break;
97 case OPT_getaffinity:
98 test = 1;
99 break;
100 case OPT_mbind:
101 test = 2;
102 mask = strtoul(optarg, &end, 10);
103 if (*end != '\0')
104 errx(1, "wrong -s argument!");
105 break;
106 case OPT_set_mempolicy:
107 test = 3;
108 mask = strtoul(optarg, &end, 10);
109 if (*end != '\0')
110 errx(1, "wrong -s argument!");
111 break;
112 case OPT_get_mempolicy:
113 test = 4;
114 break;
115 default:
116 errx(1, "unknown option!\n");
117 break;
118 }
119 }
120}
121
Wanlong Gao354ebb42012-12-07 10:10:04 +0800122void sigint_handler(int __attribute__ ((unused)) signo)
subrata_modak5e975a52009-04-22 10:48:10 +0000123{
124 flag_exit = 1;
125}
126
127void test_setaffinity(void)
128{
129 cpu_set_t tmask;
Jan Stancek6fb91172014-11-10 08:44:59 -0500130 unsigned int i;
subrata_modak5e975a52009-04-22 10:48:10 +0000131 CPU_ZERO(&tmask);
132 for (i = 0; i < 8 * sizeof(mask); i++) {
133 if ((1 << i) & mask)
134 CPU_SET(i, &tmask);
135 }
136 ret = sched_setaffinity(0, sizeof(tmask), &tmask);
137}
138
139void test_getaffinity(void)
140{
141 cpu_set_t tmask;
Jan Stancek6fb91172014-11-10 08:44:59 -0500142 unsigned int i;
subrata_modak5e975a52009-04-22 10:48:10 +0000143 CPU_ZERO(&tmask);
144 ret = sched_getaffinity(0, sizeof(tmask), &tmask);
145 for (i = 0; i < 8 * sizeof(mask); i++) {
146 if (CPU_ISSET(i, &tmask))
147 printf("%d,", i);
148 }
149}
150
151void test_mbind(void)
152{
153 void *addr;
154 int len = 10 * 1024 * 1024;
155 addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
156 if (addr == MAP_FAILED) {
157 ret = 1;
158 return;
159 }
yaberauneyaf12a19a2009-12-19 05:21:23 +0000160 printf("%p\n", addr);
subrata_modak5e975a52009-04-22 10:48:10 +0000161 ret = mbind(addr, len, MPOL_BIND, &mask, 8 * sizeof(mask), 0);
162}
163
164void test_set_mempolicy(void)
165{
166 ret = set_mempolicy(MPOL_BIND, &mask, 8 * sizeof(mask));
167}
168
169void test_get_mempolicy(void)
170{
subrata_modak71b936e2009-04-25 18:10:58 +0000171 int nbits;
172 struct bitmask *nmask;
173 char str[256];
174
175 nbits = cpuset_mems_nbits();
176 if (nbits <= 0) {
177 warn("get the nbits of nodes failed");
178 ret = 1;
179 return;
180 }
181
182 nmask = bitmask_alloc(nbits);
183 if (nmask == NULL) {
184 warn("alloc bitmask failed");
185 ret = 1;
186 return;
187 }
Jan Stancek14f1d232014-01-07 14:46:16 +0100188#if HAVE_DECL_MPOL_F_MEMS_ALLOWED
subrata_modak71b936e2009-04-25 18:10:58 +0000189 ret = get_mempolicy(NULL, bitmask_mask(nmask), bitmask_nbits(nmask), 0,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800190 MPOL_F_MEMS_ALLOWED);
Jan Stancek14f1d232014-01-07 14:46:16 +0100191#else
Jan Stancek6fb91172014-11-10 08:44:59 -0500192 tst_resm(TCONF, "don't have MPOL_F_MEMS_ALLOWED");
193 ret = TCONF;
Jan Stancek14f1d232014-01-07 14:46:16 +0100194#endif
subrata_modak71b936e2009-04-25 18:10:58 +0000195
196 bitmask_displaylist(str, 256, nmask);
yaberauneya397ae832010-01-19 08:04:00 +0000197 puts(str);
subrata_modak5e975a52009-04-22 10:48:10 +0000198}
199
Wanlong Gao354ebb42012-12-07 10:10:04 +0800200void sigusr_handler(int __attribute__ ((unused)) signo)
subrata_modak5e975a52009-04-22 10:48:10 +0000201{
202 switch (test) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800203 case 0:
204 test_setaffinity();
205 break;
206 case 1:
207 test_getaffinity();
208 break;
209 case 2:
210 test_mbind();
211 break;
212 case 3:
213 test_set_mempolicy();
214 break;
215 case 4:
216 test_get_mempolicy();
217 break;
subrata_modak5e975a52009-04-22 10:48:10 +0000218 default:;
219 }
220 test = -1;
221}
222
223int main(int argc, char *argv[])
224{
225 struct sigaction sigint_action;
226 struct sigaction sigusr_action;
227
228 memset(&sigint_action, 0, sizeof(sigint_action));
229 sigint_action.sa_handler = &sigint_handler;
230 sigaction(SIGINT, &sigint_action, NULL);
231
232 memset(&sigusr_action, 0, sizeof(sigusr_action));
233 sigusr_action.sa_handler = &sigusr_handler;
234 sigaction(SIGUSR1, &sigusr_action, NULL);
235
236 process_options(argc, argv);
237
238 while (!flag_exit)
239 sleep(1);
240
241 return ret;
242}
yaberauneya397ae832010-01-19 08:04:00 +0000243#else
Wanlong Gao354ebb42012-12-07 10:10:04 +0800244int main(void)
245{
Jan Stancek6fb91172014-11-10 08:44:59 -0500246 tst_brkm(TCONF, NULL, "System doesn't have required mempolicy support");
yaberauneya397ae832010-01-19 08:04:00 +0000247}
Garrett Cooper1e587382011-03-15 05:02:48 -0700248#endif