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 | |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 42 | #define OPT_p 1 |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 43 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 44 | int taskset_main(int argc, char** argv); |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 45 | int taskset_main(int argc, char** argv) |
| 46 | { |
| 47 | cpu_set_t mask, new_mask; |
| 48 | pid_t pid = 0; |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 49 | unsigned opt; |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 50 | const char *state = "current\0new"; |
| 51 | char *p_opt = NULL, *aff = NULL; |
| 52 | |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 53 | opt = getopt32(argv, "+p:", &p_opt); |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 54 | |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 55 | if (opt & OPT_p) { |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 56 | if (argc == optind+1) { /* -p <aff> <pid> */ |
| 57 | aff = p_opt; |
| 58 | p_opt = argv[optind]; |
| 59 | } |
| 60 | argv += optind; /* me -p <arg> */ |
| 61 | pid = xatoul_range(p_opt, 1, ULONG_MAX); /* -p <pid> */ |
| 62 | } else |
| 63 | aff = *++argv; /* <aff> <cmd...> */ |
| 64 | if (aff) { |
| 65 | unsigned i = 0; |
Denis Vlasenko | e935602 | 2007-01-29 18:03:54 +0000 | [diff] [blame] | 66 | unsigned long l = xstrtol_range(aff, 0, 1, LONG_MAX); |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 67 | |
| 68 | CPU_ZERO(&new_mask); |
| 69 | while (i < CPU_SETSIZE && l >= (1<<i)) { |
| 70 | if ((1<<i) & l) |
| 71 | CPU_SET(i, &new_mask); |
| 72 | ++i; |
| 73 | } |
| 74 | } |
| 75 | |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 76 | if (opt & OPT_p) { |
| 77 | print_aff: |
| 78 | if (sched_getaffinity(pid, sizeof(mask), &mask) < 0) |
| 79 | bb_perror_msg_and_die("failed to %cet pid %d's affinity", 'g', pid); |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 80 | printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n", |
| 81 | pid, state, from_cpuset(mask)); |
| 82 | if (!*argv) /* no new affinity given or we did print already, done. */ |
| 83 | return EXIT_SUCCESS; |
| 84 | } |
| 85 | |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 86 | if (sched_setaffinity(pid, sizeof(new_mask), &new_mask)) |
| 87 | bb_perror_msg_and_die("failed to %cet pid %d's affinity", 's', pid); |
| 88 | if (opt & OPT_p) { |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 89 | state += 8; |
| 90 | ++argv; |
| 91 | goto print_aff; |
| 92 | } |
| 93 | ++argv; |
Denis Vlasenko | 1d76f43 | 2007-02-06 01:20:12 +0000 | [diff] [blame] | 94 | BB_EXECVP(*argv, argv); |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 95 | bb_perror_msg_and_die("%s", *argv); |
| 96 | } |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 97 | #undef OPT_p |
Bernhard Reutner-Fischer | 32eddff | 2006-11-22 16:39:48 +0000 | [diff] [blame] | 98 | #undef TASKSET_PRINTF_MASK |
| 99 | #undef from_cpuset |