2 * Copyright (c) 2015 Adrian Chadd <adrian@FreeBSD.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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/cpuset.h>
32 #include <sys/domainset.h>
33 #include <sys/resource.h>
49 static struct option longopts[] = {
50 { "tid", required_argument, NULL, 't' },
51 { "pid", required_argument, NULL, 'p' },
52 { "memdomain", required_argument, NULL, 'm' },
53 { "cpudomain", required_argument, NULL, 'c' },
54 { "mempolicy", required_argument, NULL, 'l' },
55 { "set", no_argument, NULL, 's' },
56 { "get", no_argument, NULL, 'g' },
61 policy_to_str(int policy)
65 case DOMAINSET_POLICY_INVALID:
67 case DOMAINSET_POLICY_ROUNDROBIN:
68 return ("round-robin");
69 case DOMAINSET_POLICY_FIRSTTOUCH:
70 return ("first-touch");
71 case DOMAINSET_POLICY_PREFER:
79 domain_print(domainset_t *mask)
84 for (once = 0, bit = 0; bit < DOMAINSET_SETSIZE; bit++) {
85 if (DOMAINSET_ISSET(bit, mask)) {
97 parse_policy(int *policy, const char *str)
100 if (strcmp(str, "rr") == 0) {
101 *policy = DOMAINSET_POLICY_ROUNDROBIN;
105 if (strcmp(str, "first-touch-rr") == 0) {
106 *policy = DOMAINSET_POLICY_FIRSTTOUCH;
110 if (strcmp(str, "first-touch") == 0) {
111 *policy = DOMAINSET_POLICY_FIRSTTOUCH;
115 if (strcmp(str, "fixed-domain") == 0) {
116 *policy = DOMAINSET_POLICY_PREFER;
120 if (strcmp(str, "fixed-domain-rr") == 0) {
121 *policy = DOMAINSET_POLICY_PREFER;
132 printf("usage: numactl --get [--tid/-t <tid>] [--pid/-p <pid>]\n");
133 printf(" numactl --set [--tid=<tid>] [--pid/-p<pid>]\n");
134 printf(" [--mempolicy/-l <policy>] [--memdomain/"
136 printf(" [--cpudomain/-c <domain>]\n");
137 printf(" numactl [--mempolicy/-l <policy>] [--memdomain/-m "
139 printf(" [--cpudomain/-c <domain>] <cmd> ...\n");
145 set_numa_domain_cpuaffinity(int cpu_domain, cpuwhich_t which, id_t id)
150 error = cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_DOMAIN,
151 cpu_domain, sizeof(set), &set);
153 err(1, "cpuset_getaffinity");
154 error = cpuset_setaffinity(CPU_LEVEL_WHICH, which, id, sizeof(set),
157 err(1, "cpuset_setaffinity");
163 * Attempt to maintain compatability with old style syscalls.
166 numa_setaffinity(cpuwhich_t which, id_t id, int policy, int domain)
171 DOMAINSET_ZERO(&mask);
172 if (policy == DOMAINSET_POLICY_PREFER)
173 DOMAINSET_SET(domain, &mask);
174 else if (cpuset_getdomain(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
175 sizeof(mask), &mask, &p) != 0)
176 err(EXIT_FAILURE, "getdomain");
177 return cpuset_setdomain(CPU_LEVEL_WHICH, which, id, sizeof(mask),
182 main(int argc, char *argv[])
205 policy = DOMAINSET_POLICY_INVALID;
207 while ((ch = getopt_long(argc, argv, "c:gl:m:p:st:", longopts,
211 cpu_domain = atoi(optarg);
217 if (parse_policy(&policy, optarg) != 0) {
219 "Could not parse policy: '%s'\n", optarg);
225 domain = atoi(optarg);
243 /* Handle the user wishing to run a command */
245 /* Ensure that a policy was set */
246 if (mem_policy_set == 0) {
247 fprintf(stderr, "Error: no policy given\n");
251 /* Set current memory process policy, will be inherited */
252 if (numa_setaffinity(CPU_WHICH_PID, -1, policy, domain) != 0)
253 err(1, "numa_setaffinity");
255 /* If a CPU domain policy was given, include that too */
256 if (cpu_domain != -1)
257 (void) set_numa_domain_cpuaffinity(cpu_domain,
262 err(errno == ENOENT ? 127 : 126, "%s", *argv);
265 /* Figure out which */
267 which = CPU_WHICH_TID;
269 } else if (pid != -1) {
270 which = CPU_WHICH_PID;
273 fprintf(stderr, "Error: one of tid or pid must be given\n");
278 if (is_set && is_get) {
279 fprintf(stderr, "Error: can't set both 'set' and 'get'\n");
283 if (is_set && ! mem_policy_set) {
284 fprintf(stderr, "Error: --set given, but no policy\n");
288 /* If it's get, then get the policy and return */
292 error = cpuset_getdomain(CPU_LEVEL_WHICH, which, id,
293 sizeof(mask), &mask, &policy);
295 err(1, "cpuset_getdomain");
296 printf(" Policy: %s; domain: ",
297 policy_to_str(policy));
302 /* Assume it's set */
305 error = numa_setaffinity(which, id, policy, domain);
307 err(1, "numa_setaffinity");
309 /* If a CPU domain policy was given, include that too */
310 if (cpu_domain != -1)
311 (void) set_numa_domain_cpuaffinity(cpu_domain, which, id);