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 |
| 4 | * Copyright (c) 2006 Bernhard Fischer |
| 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> |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 10 | #include <getopt.h> /* optind */ |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 11 | #include "libbb.h" |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 12 | |
| 13 | #if ENABLE_FEATURE_TASKSET_FANCY |
| 14 | #define TASKSET_PRINTF_MASK "%s" |
| 15 | #define from_cpuset(x) __from_cpuset(&x) |
| 16 | /* craft a string from the mask */ |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 17 | static char *__from_cpuset(cpu_set_t *mask) |
| 18 | { |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 19 | int i; |
| 20 | char *ret = 0, *str = xzalloc(9); |
| 21 | |
| 22 | for (i = CPU_SETSIZE - 4; i >= 0; i -= 4) { |
| 23 | char val = 0; |
| 24 | int off; |
| 25 | for (off = 0; off <= 3; ++off) |
| 26 | if (CPU_ISSET(i+off, mask)) |
| 27 | val |= 1<<off; |
| 28 | |
| 29 | if (!ret && val) |
| 30 | ret = str; |
| 31 | *str++ = (val-'0'<=9) ? (val+48) : (val+87); |
| 32 | } |
| 33 | return ret; |
| 34 | } |
| 35 | #else |
| 36 | #define TASKSET_PRINTF_MASK "%x" |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 37 | /* (void*) cast is for battling gcc: */ |
| 38 | /* "dereferencing type-punned pointer will break strict-aliasing rules" */ |
| 39 | #define from_cpuset(mask) (*(unsigned*)(void*)&(mask)) |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 40 | #endif |
| 41 | |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 42 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 43 | int taskset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 44 | int taskset_main(int argc ATTRIBUTE_UNUSED, char **argv) |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 45 | { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 46 | cpu_set_t mask; |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 47 | pid_t pid = 0; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 48 | unsigned opt_p; |
| 49 | const char *current_new; |
| 50 | char *pid_str; |
| 51 | char *aff = aff; /* for compiler */ |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 52 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 53 | opt_complementary = "-1"; /* at least 1 arg */ |
| 54 | opt_p = getopt32(argv, "+p"); |
| 55 | argv += optind; |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 56 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 57 | if (opt_p) { |
| 58 | pid_str = *argv++; |
| 59 | if (*argv) { /* "-p <aff> <pid> ...rest.is.ignored..." */ |
| 60 | aff = pid_str; |
| 61 | pid_str = *argv; /* NB: *argv != NULL in this case */ |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 62 | } |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 63 | /* else it was just "-p <pid>", and *argv == NULL */ |
| 64 | pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1); |
| 65 | } else { |
| 66 | aff = *argv++; /* <aff> <cmd...> */ |
| 67 | if (!*argv) |
| 68 | bb_show_usage(); |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 71 | current_new = "current\0new"; |
| 72 | if (opt_p) { |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 73 | print_aff: |
| 74 | if (sched_getaffinity(pid, sizeof(mask), &mask) < 0) |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 75 | 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] | 76 | printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n", |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 77 | pid, current_new, from_cpuset(mask)); |
| 78 | if (!*argv) { |
| 79 | /* Either it was just "-p <pid>", |
| 80 | * or it was "-p <aff> <pid>" and we came here |
| 81 | * for the second time (see goto below) */ |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 82 | return EXIT_SUCCESS; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 83 | } |
| 84 | *argv = NULL; |
| 85 | current_new += 8; /* "new" */ |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 88 | { /* Affinity was specified, translate it into cpu_set_t */ |
| 89 | unsigned i; |
| 90 | /* Do not allow zero mask: */ |
| 91 | unsigned long long m = xstrtoull_range(aff, 0, 1, ULLONG_MAX); |
| 92 | CPU_ZERO(&mask); |
| 93 | for (i = 0; i < CPU_SETSIZE; i++) { |
| 94 | unsigned long long bit = (1ULL << i); |
| 95 | if (bit & m) |
| 96 | CPU_SET(i, &mask); |
| 97 | } |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 98 | } |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 99 | |
| 100 | /* Set pid's or our own (pid==0) affinity */ |
| 101 | if (sched_setaffinity(pid, sizeof(mask), &mask)) |
| 102 | bb_perror_msg_and_die("can't %cet pid %d's affinity", 's', pid); |
| 103 | |
| 104 | if (!*argv) /* "-p <aff> <pid> [...ignored...]" */ |
| 105 | goto print_aff; /* print new affinity and exit */ |
| 106 | |
Denis Vlasenko | 1d76f43 | 2007-02-06 01:20:12 +0000 | [diff] [blame] | 107 | BB_EXECVP(*argv, argv); |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 108 | bb_simple_perror_msg_and_die(*argv); |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 109 | } |