blob: a0bbf0aa190e320942b42f4b6d5a0a3b4a8048e3 [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 *
6 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
7 */
8
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +00009#include <sched.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000010#include "libbb.h"
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000011
12#if ENABLE_FEATURE_TASKSET_FANCY
13#define TASKSET_PRINTF_MASK "%s"
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000014/* craft a string from the mask */
Denis Vlasenko0e525412008-07-11 13:57:08 +000015static char *from_cpuset(cpu_set_t *mask)
Denis Vlasenko3bba5452006-12-30 17:57:03 +000016{
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000017 int i;
Denis Vlasenko0e525412008-07-11 13:57:08 +000018 char *ret = NULL;
19 char *str = xzalloc((CPU_SETSIZE / 4) + 1); /* we will leak it */
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000020
21 for (i = CPU_SETSIZE - 4; i >= 0; i -= 4) {
Denis Vlasenko0e525412008-07-11 13:57:08 +000022 int val = 0;
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000023 int off;
24 for (off = 0; off <= 3; ++off)
Denis Vlasenko0e525412008-07-11 13:57:08 +000025 if (CPU_ISSET(i + off, mask))
26 val |= 1 << off;
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000027 if (!ret && val)
28 ret = str;
Denis Vlasenko0e525412008-07-11 13:57:08 +000029 *str++ = bb_hexdigits_upcase[val] | 0x20;
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000030 }
31 return ret;
32}
33#else
Denis Vlasenko0e525412008-07-11 13:57:08 +000034#define TASKSET_PRINTF_MASK "%llx"
35static unsigned long long from_cpuset(cpu_set_t *mask)
36{
37 struct BUG_CPU_SETSIZE_is_too_small {
38 char BUG_CPU_SETSIZE_is_too_small[
39 CPU_SETSIZE < sizeof(int) ? -1 : 1];
40 };
41 char *p = (void*)mask;
42
43 /* Take the least significant bits. Careful!
44 * Consider both CPU_SETSIZE=4 and CPU_SETSIZE=1024 cases
45 */
46#if BB_BIG_ENDIAN
47 /* For big endian, it means LAST bits */
48 if (CPU_SETSIZE < sizeof(long))
49 p += CPU_SETSIZE - sizeof(int);
50 else if (CPU_SETSIZE < sizeof(long long))
51 p += CPU_SETSIZE - sizeof(long);
52 else
53 p += CPU_SETSIZE - sizeof(long long);
54#endif
55 if (CPU_SETSIZE < sizeof(long))
56 return *(unsigned*)p;
57 if (CPU_SETSIZE < sizeof(long long))
58 return *(unsigned long*)p;
59 return *(unsigned long long*)p;
60}
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000061#endif
62
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000063
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000064int taskset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000065int taskset_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000066{
Denis Vlasenkob44c7902008-03-17 09:29:43 +000067 cpu_set_t mask;
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000068 pid_t pid = 0;
Denis Vlasenkob44c7902008-03-17 09:29:43 +000069 unsigned opt_p;
70 const char *current_new;
71 char *pid_str;
72 char *aff = aff; /* for compiler */
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000073
Denis Vlasenko45ecfc22008-03-22 17:46:16 +000074 /* NB: we mimic util-linux's taskset: -p does not take
75 * an argument, i.e., "-pN" is NOT valid, only "-p N"!
76 * Indeed, util-linux-2.13-pre7 uses:
77 * getopt_long(argc, argv, "+pchV", ...), not "...p:..." */
78
Denis Vlasenkob44c7902008-03-17 09:29:43 +000079 opt_complementary = "-1"; /* at least 1 arg */
80 opt_p = getopt32(argv, "+p");
81 argv += optind;
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000082
Denis Vlasenkob44c7902008-03-17 09:29:43 +000083 if (opt_p) {
84 pid_str = *argv++;
85 if (*argv) { /* "-p <aff> <pid> ...rest.is.ignored..." */
86 aff = pid_str;
87 pid_str = *argv; /* NB: *argv != NULL in this case */
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000088 }
Denis Vlasenkob44c7902008-03-17 09:29:43 +000089 /* else it was just "-p <pid>", and *argv == NULL */
90 pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
91 } else {
92 aff = *argv++; /* <aff> <cmd...> */
93 if (!*argv)
94 bb_show_usage();
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +000095 }
96
Denis Vlasenkob44c7902008-03-17 09:29:43 +000097 current_new = "current\0new";
98 if (opt_p) {
Denis Vlasenko3bba5452006-12-30 17:57:03 +000099 print_aff:
100 if (sched_getaffinity(pid, sizeof(mask), &mask) < 0)
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000101 bb_perror_msg_and_die("can't %cet pid %d's affinity", 'g', pid);
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000102 printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n",
Denis Vlasenko0e525412008-07-11 13:57:08 +0000103 pid, current_new, from_cpuset(&mask));
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000104 if (!*argv) {
105 /* Either it was just "-p <pid>",
106 * or it was "-p <aff> <pid>" and we came here
107 * for the second time (see goto below) */
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000108 return EXIT_SUCCESS;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000109 }
110 *argv = NULL;
111 current_new += 8; /* "new" */
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000112 }
113
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000114 { /* Affinity was specified, translate it into cpu_set_t */
115 unsigned i;
116 /* Do not allow zero mask: */
117 unsigned long long m = xstrtoull_range(aff, 0, 1, ULLONG_MAX);
Denis Vlasenko44f08212008-04-25 17:01:06 +0000118 enum { CNT_BIT = CPU_SETSIZE < sizeof(m)*8 ? CPU_SETSIZE : sizeof(m)*8 };
119
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000120 CPU_ZERO(&mask);
Denis Vlasenko44f08212008-04-25 17:01:06 +0000121 for (i = 0; i < CNT_BIT; i++) {
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000122 unsigned long long bit = (1ULL << i);
123 if (bit & m)
124 CPU_SET(i, &mask);
125 }
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000126 }
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000127
128 /* Set pid's or our own (pid==0) affinity */
129 if (sched_setaffinity(pid, sizeof(mask), &mask))
130 bb_perror_msg_and_die("can't %cet pid %d's affinity", 's', pid);
131
132 if (!*argv) /* "-p <aff> <pid> [...ignored...]" */
133 goto print_aff; /* print new affinity and exit */
134
Denis Vlasenko1d76f432007-02-06 01:20:12 +0000135 BB_EXECVP(*argv, argv);
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000136 bb_simple_perror_msg_and_die(*argv);
Bernhard Reutner-Fischer32eddff2006-11-22 16:39:48 +0000137}