blob: 77fc8643d5eba73bca5a613de9bb09db1eae80ba [file] [log] [blame]
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +00001/* vi: set sw=4 ts=4: */
2/*
3 * taskset - retrieve or set a processes' CPU affinity
Bernhard Reutner-Fischer6c4dade2008-09-25 12:13:34 +00004 * Copyright (c) 2006 Bernhard Reutner-Fischer
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +00005 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02006 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +00007 */
8
Pere Orga5bc8c002011-04-11 03:29:49 +02009//usage:#define taskset_trivial_usage
10//usage: "[-p] [MASK] [PID | PROG ARGS]"
11//usage:#define taskset_full_usage "\n\n"
12//usage: "Set or get CPU affinity\n"
13//usage: "\nOptions:"
14//usage: "\n -p Operate on an existing PID"
15//usage:
16//usage:#define taskset_example_usage
17//usage: "$ taskset 0x7 ./dgemm_test&\n"
18//usage: "$ taskset -p 0x1 $!\n"
19//usage: "pid 4790's current affinity mask: 7\n"
20//usage: "pid 4790's new affinity mask: 1\n"
21//usage: "$ taskset 0x7 /bin/sh -c './taskset -p 0x1 $$'\n"
22//usage: "pid 6671's current affinity mask: 1\n"
23//usage: "pid 6671's new affinity mask: 1\n"
24//usage: "$ taskset -p 1\n"
25//usage: "pid 1's current affinity mask: 3\n"
26
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000027#include <sched.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000028#include "libbb.h"
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000029
30#if ENABLE_FEATURE_TASKSET_FANCY
31#define TASKSET_PRINTF_MASK "%s"
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000032/* craft a string from the mask */
Denis Vlasenko0e525412008-07-11 13:57:08 +000033static char *from_cpuset(cpu_set_t *mask)
Denis Vlasenko3bba5452006-12-30 17:57:03 +000034{
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000035 int i;
Denis Vlasenko0e525412008-07-11 13:57:08 +000036 char *ret = NULL;
37 char *str = xzalloc((CPU_SETSIZE / 4) + 1); /* we will leak it */
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000038
39 for (i = CPU_SETSIZE - 4; i >= 0; i -= 4) {
Denis Vlasenko0e525412008-07-11 13:57:08 +000040 int val = 0;
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000041 int off;
42 for (off = 0; off <= 3; ++off)
Denis Vlasenko0e525412008-07-11 13:57:08 +000043 if (CPU_ISSET(i + off, mask))
44 val |= 1 << off;
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000045 if (!ret && val)
46 ret = str;
Denis Vlasenko0e525412008-07-11 13:57:08 +000047 *str++ = bb_hexdigits_upcase[val] | 0x20;
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000048 }
49 return ret;
50}
51#else
Denis Vlasenko0e525412008-07-11 13:57:08 +000052#define TASKSET_PRINTF_MASK "%llx"
53static unsigned long long from_cpuset(cpu_set_t *mask)
54{
55 struct BUG_CPU_SETSIZE_is_too_small {
56 char BUG_CPU_SETSIZE_is_too_small[
57 CPU_SETSIZE < sizeof(int) ? -1 : 1];
58 };
59 char *p = (void*)mask;
60
61 /* Take the least significant bits. Careful!
62 * Consider both CPU_SETSIZE=4 and CPU_SETSIZE=1024 cases
63 */
64#if BB_BIG_ENDIAN
65 /* For big endian, it means LAST bits */
66 if (CPU_SETSIZE < sizeof(long))
67 p += CPU_SETSIZE - sizeof(int);
68 else if (CPU_SETSIZE < sizeof(long long))
69 p += CPU_SETSIZE - sizeof(long);
70 else
71 p += CPU_SETSIZE - sizeof(long long);
72#endif
73 if (CPU_SETSIZE < sizeof(long))
74 return *(unsigned*)p;
75 if (CPU_SETSIZE < sizeof(long long))
76 return *(unsigned long*)p;
77 return *(unsigned long long*)p;
78}
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000079#endif
80
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000081
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000082int taskset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000083int taskset_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000084{
Denis Vlasenkob44c7902008-03-17 09:29:43 +000085 cpu_set_t mask;
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000086 pid_t pid = 0;
Denis Vlasenkob44c7902008-03-17 09:29:43 +000087 unsigned opt_p;
88 const char *current_new;
89 char *pid_str;
90 char *aff = aff; /* for compiler */
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000091
Denis Vlasenko45ecfc22008-03-22 17:46:16 +000092 /* NB: we mimic util-linux's taskset: -p does not take
93 * an argument, i.e., "-pN" is NOT valid, only "-p N"!
94 * Indeed, util-linux-2.13-pre7 uses:
95 * getopt_long(argc, argv, "+pchV", ...), not "...p:..." */
96
Denis Vlasenkob44c7902008-03-17 09:29:43 +000097 opt_complementary = "-1"; /* at least 1 arg */
98 opt_p = getopt32(argv, "+p");
99 argv += optind;
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000100
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000101 if (opt_p) {
102 pid_str = *argv++;
103 if (*argv) { /* "-p <aff> <pid> ...rest.is.ignored..." */
104 aff = pid_str;
105 pid_str = *argv; /* NB: *argv != NULL in this case */
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000106 }
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000107 /* else it was just "-p <pid>", and *argv == NULL */
108 pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
109 } else {
110 aff = *argv++; /* <aff> <cmd...> */
111 if (!*argv)
112 bb_show_usage();
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000113 }
114
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000115 current_new = "current\0new";
116 if (opt_p) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +0000117 print_aff:
118 if (sched_getaffinity(pid, sizeof(mask), &mask) < 0)
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000119 bb_perror_msg_and_die("can't %cet pid %d's affinity", 'g', pid);
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000120 printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n",
Denis Vlasenko0e525412008-07-11 13:57:08 +0000121 pid, current_new, from_cpuset(&mask));
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000122 if (!*argv) {
123 /* Either it was just "-p <pid>",
124 * or it was "-p <aff> <pid>" and we came here
125 * for the second time (see goto below) */
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000126 return EXIT_SUCCESS;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000127 }
128 *argv = NULL;
129 current_new += 8; /* "new" */
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000130 }
131
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000132 { /* Affinity was specified, translate it into cpu_set_t */
133 unsigned i;
134 /* Do not allow zero mask: */
135 unsigned long long m = xstrtoull_range(aff, 0, 1, ULLONG_MAX);
Denis Vlasenko44f08212008-04-25 17:01:06 +0000136 enum { CNT_BIT = CPU_SETSIZE < sizeof(m)*8 ? CPU_SETSIZE : sizeof(m)*8 };
137
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000138 CPU_ZERO(&mask);
Denis Vlasenko44f08212008-04-25 17:01:06 +0000139 for (i = 0; i < CNT_BIT; i++) {
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000140 unsigned long long bit = (1ULL << i);
141 if (bit & m)
142 CPU_SET(i, &mask);
143 }
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000144 }
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000145
146 /* Set pid's or our own (pid==0) affinity */
147 if (sched_setaffinity(pid, sizeof(mask), &mask))
148 bb_perror_msg_and_die("can't %cet pid %d's affinity", 's', pid);
149
Denys Vlasenko41ddd9f2010-06-25 01:46:53 +0200150 if (!argv[0]) /* "-p <aff> <pid> [...ignored...]" */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000151 goto print_aff; /* print new affinity and exit */
152
Pascal Bellard21e8e8d2010-07-04 00:57:03 +0200153 BB_EXECVP_or_die(argv);
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000154}