Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * taskset - retrieve or set a processes' CPU affinity |
Bernhard Reutner-Fischer | 6c4dade | 2008-09-25 12:13:34 +0000 | [diff] [blame] | 4 | * Copyright (c) 2006 Bernhard Reutner-Fischer |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 5 | * |
| 6 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 7 | */ |
| 8 | |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 9 | #include <sched.h> |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 11 | |
| 12 | #if ENABLE_FEATURE_TASKSET_FANCY |
| 13 | #define TASKSET_PRINTF_MASK "%s" |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 14 | /* craft a string from the mask */ |
Denis Vlasenko | 0e52541 | 2008-07-11 13:57:08 +0000 | [diff] [blame] | 15 | static char *from_cpuset(cpu_set_t *mask) |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 16 | { |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 17 | int i; |
Denis Vlasenko | 0e52541 | 2008-07-11 13:57:08 +0000 | [diff] [blame] | 18 | char *ret = NULL; |
| 19 | char *str = xzalloc((CPU_SETSIZE / 4) + 1); /* we will leak it */ |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 20 | |
| 21 | for (i = CPU_SETSIZE - 4; i >= 0; i -= 4) { |
Denis Vlasenko | 0e52541 | 2008-07-11 13:57:08 +0000 | [diff] [blame] | 22 | int val = 0; |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 23 | int off; |
| 24 | for (off = 0; off <= 3; ++off) |
Denis Vlasenko | 0e52541 | 2008-07-11 13:57:08 +0000 | [diff] [blame] | 25 | if (CPU_ISSET(i + off, mask)) |
| 26 | val |= 1 << off; |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 27 | if (!ret && val) |
| 28 | ret = str; |
Denis Vlasenko | 0e52541 | 2008-07-11 13:57:08 +0000 | [diff] [blame] | 29 | *str++ = bb_hexdigits_upcase[val] | 0x20; |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 30 | } |
| 31 | return ret; |
| 32 | } |
| 33 | #else |
Denis Vlasenko | 0e52541 | 2008-07-11 13:57:08 +0000 | [diff] [blame] | 34 | #define TASKSET_PRINTF_MASK "%llx" |
| 35 | static 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-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 61 | #endif |
| 62 | |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 63 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 64 | int taskset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 65 | int taskset_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 66 | { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 67 | cpu_set_t mask; |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 68 | pid_t pid = 0; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 69 | unsigned opt_p; |
| 70 | const char *current_new; |
| 71 | char *pid_str; |
| 72 | char *aff = aff; /* for compiler */ |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 73 | |
Denis Vlasenko | 45ecfc2 | 2008-03-22 17:46:16 +0000 | [diff] [blame] | 74 | /* 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 Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 79 | opt_complementary = "-1"; /* at least 1 arg */ |
| 80 | opt_p = getopt32(argv, "+p"); |
| 81 | argv += optind; |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 82 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 83 | 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-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 88 | } |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 89 | /* 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-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 97 | current_new = "current\0new"; |
| 98 | if (opt_p) { |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 99 | print_aff: |
| 100 | if (sched_getaffinity(pid, sizeof(mask), &mask) < 0) |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 101 | bb_perror_msg_and_die("can't %cet pid %d's affinity", 'g', pid); |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 102 | printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n", |
Denis Vlasenko | 0e52541 | 2008-07-11 13:57:08 +0000 | [diff] [blame] | 103 | pid, current_new, from_cpuset(&mask)); |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 104 | 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-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 108 | return EXIT_SUCCESS; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 109 | } |
| 110 | *argv = NULL; |
| 111 | current_new += 8; /* "new" */ |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 114 | { /* 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 Vlasenko | 44f0821 | 2008-04-25 17:01:06 +0000 | [diff] [blame] | 118 | enum { CNT_BIT = CPU_SETSIZE < sizeof(m)*8 ? CPU_SETSIZE : sizeof(m)*8 }; |
| 119 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 120 | CPU_ZERO(&mask); |
Denis Vlasenko | 44f0821 | 2008-04-25 17:01:06 +0000 | [diff] [blame] | 121 | for (i = 0; i < CNT_BIT; i++) { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 122 | unsigned long long bit = (1ULL << i); |
| 123 | if (bit & m) |
| 124 | CPU_SET(i, &mask); |
| 125 | } |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 126 | } |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 127 | |
| 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 | |
Denys Vlasenko | 41ddd9f | 2010-06-25 01:46:53 +0200 | [diff] [blame] | 132 | if (!argv[0]) /* "-p <aff> <pid> [...ignored...]" */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 133 | goto print_aff; /* print new affinity and exit */ |
| 134 | |
Pascal Bellard | 21e8e8d | 2010-07-04 00:57:03 +0200 | [diff] [blame] | 135 | BB_EXECVP_or_die(argv); |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 136 | } |