taskset.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * taskset - retrieve or set a processes' CPU affinity
  4. * Copyright (c) 2006 Bernhard Reutner-Fischer
  5. *
  6. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  7. */
  8. //config:config TASKSET
  9. //config: bool "taskset"
  10. //config: default n # doesn't build on some non-x86 targets (m68k)
  11. //config: help
  12. //config: Retrieve or set a processes's CPU affinity.
  13. //config: This requires sched_{g,s}etaffinity support in your libc.
  14. //config:
  15. //config:config FEATURE_TASKSET_FANCY
  16. //config: bool "Fancy output"
  17. //config: default y
  18. //config: depends on TASKSET
  19. //config: help
  20. //config: Add code for fancy output. This merely silences a compiler-warning
  21. //config: and adds about 135 Bytes. May be needed for machines with alot
  22. //config: of CPUs.
  23. //applet:IF_TASKSET(APPLET(taskset, BB_DIR_USR_BIN, BB_SUID_DROP))
  24. //kbuild:lib-$(CONFIG_TASKSET) += taskset.o
  25. //usage:#define taskset_trivial_usage
  26. //usage: "[-p] [MASK] [PID | PROG ARGS]"
  27. //usage:#define taskset_full_usage "\n\n"
  28. //usage: "Set or get CPU affinity\n"
  29. //usage: "\n -p Operate on an existing PID"
  30. //usage:
  31. //usage:#define taskset_example_usage
  32. //usage: "$ taskset 0x7 ./dgemm_test&\n"
  33. //usage: "$ taskset -p 0x1 $!\n"
  34. //usage: "pid 4790's current affinity mask: 7\n"
  35. //usage: "pid 4790's new affinity mask: 1\n"
  36. //usage: "$ taskset 0x7 /bin/sh -c './taskset -p 0x1 $$'\n"
  37. //usage: "pid 6671's current affinity mask: 1\n"
  38. //usage: "pid 6671's new affinity mask: 1\n"
  39. //usage: "$ taskset -p 1\n"
  40. //usage: "pid 1's current affinity mask: 3\n"
  41. /*
  42. Not yet implemented:
  43. * -a/--all-tasks (affect all threads)
  44. * -c/--cpu-list (specify CPUs via "1,3,5-7")
  45. */
  46. #include <sched.h>
  47. #include "libbb.h"
  48. #if ENABLE_FEATURE_TASKSET_FANCY
  49. #define TASKSET_PRINTF_MASK "%s"
  50. /* craft a string from the mask */
  51. static char *from_cpuset(cpu_set_t *mask)
  52. {
  53. int i;
  54. char *ret = NULL;
  55. char *str = xzalloc((CPU_SETSIZE / 4) + 1); /* we will leak it */
  56. for (i = CPU_SETSIZE - 4; i >= 0; i -= 4) {
  57. int val = 0;
  58. int off;
  59. for (off = 0; off <= 3; ++off)
  60. if (CPU_ISSET(i + off, mask))
  61. val |= 1 << off;
  62. if (!ret && val)
  63. ret = str;
  64. *str++ = bb_hexdigits_upcase[val] | 0x20;
  65. }
  66. return ret;
  67. }
  68. #else
  69. #define TASKSET_PRINTF_MASK "%llx"
  70. static unsigned long long from_cpuset(cpu_set_t *mask)
  71. {
  72. struct BUG_CPU_SETSIZE_is_too_small {
  73. char BUG_CPU_SETSIZE_is_too_small[
  74. CPU_SETSIZE < sizeof(int) ? -1 : 1];
  75. };
  76. char *p = (void*)mask;
  77. /* Take the least significant bits. Careful!
  78. * Consider both CPU_SETSIZE=4 and CPU_SETSIZE=1024 cases
  79. */
  80. #if BB_BIG_ENDIAN
  81. /* For big endian, it means LAST bits */
  82. if (CPU_SETSIZE < sizeof(long))
  83. p += CPU_SETSIZE - sizeof(int);
  84. else if (CPU_SETSIZE < sizeof(long long))
  85. p += CPU_SETSIZE - sizeof(long);
  86. else
  87. p += CPU_SETSIZE - sizeof(long long);
  88. #endif
  89. if (CPU_SETSIZE < sizeof(long))
  90. return *(unsigned*)p;
  91. if (CPU_SETSIZE < sizeof(long long))
  92. return *(unsigned long*)p;
  93. return *(unsigned long long*)p;
  94. }
  95. #endif
  96. int taskset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  97. int taskset_main(int argc UNUSED_PARAM, char **argv)
  98. {
  99. cpu_set_t mask;
  100. pid_t pid = 0;
  101. unsigned opt_p;
  102. const char *current_new;
  103. char *pid_str;
  104. char *aff = aff; /* for compiler */
  105. /* NB: we mimic util-linux's taskset: -p does not take
  106. * an argument, i.e., "-pN" is NOT valid, only "-p N"!
  107. * Indeed, util-linux-2.13-pre7 uses:
  108. * getopt_long(argc, argv, "+pchV", ...), not "...p:..." */
  109. opt_complementary = "-1"; /* at least 1 arg */
  110. opt_p = getopt32(argv, "+p");
  111. argv += optind;
  112. if (opt_p) {
  113. pid_str = *argv++;
  114. if (*argv) { /* "-p <aff> <pid> ...rest.is.ignored..." */
  115. aff = pid_str;
  116. pid_str = *argv; /* NB: *argv != NULL in this case */
  117. }
  118. /* else it was just "-p <pid>", and *argv == NULL */
  119. pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
  120. } else {
  121. aff = *argv++; /* <aff> <cmd...> */
  122. if (!*argv)
  123. bb_show_usage();
  124. }
  125. current_new = "current\0new";
  126. if (opt_p) {
  127. print_aff:
  128. if (sched_getaffinity(pid, sizeof(mask), &mask) < 0)
  129. bb_perror_msg_and_die("can't %cet pid %d's affinity", 'g', pid);
  130. printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n",
  131. pid, current_new, from_cpuset(&mask));
  132. if (!*argv) {
  133. /* Either it was just "-p <pid>",
  134. * or it was "-p <aff> <pid>" and we came here
  135. * for the second time (see goto below) */
  136. return EXIT_SUCCESS;
  137. }
  138. *argv = NULL;
  139. current_new += 8; /* "new" */
  140. }
  141. /* Affinity was specified, translate it into cpu_set_t */
  142. CPU_ZERO(&mask);
  143. if (!ENABLE_FEATURE_TASKSET_FANCY) {
  144. unsigned i;
  145. unsigned long long m;
  146. /* Do not allow zero mask: */
  147. m = xstrtoull_range(aff, 0, 1, ULLONG_MAX);
  148. i = 0;
  149. do {
  150. if (m & 1)
  151. CPU_SET(i, &mask);
  152. i++;
  153. m >>= 1;
  154. } while (m != 0);
  155. } else {
  156. unsigned i;
  157. char *last_byte;
  158. char *bin;
  159. uint8_t bit_in_byte;
  160. /* Cheap way to get "long enough" buffer */
  161. bin = xstrdup(aff);
  162. if (aff[0] != '0' || (aff[1]|0x20) != 'x') {
  163. /* TODO: decimal/octal masks are still limited to 2^64 */
  164. unsigned long long m = xstrtoull_range(aff, 0, 1, ULLONG_MAX);
  165. bin += strlen(bin);
  166. last_byte = bin - 1;
  167. while (m) {
  168. *--bin = m & 0xff;
  169. m >>= 8;
  170. }
  171. } else {
  172. /* aff is "0x.....", we accept very long masks in this form */
  173. last_byte = hex2bin(bin, aff + 2, INT_MAX);
  174. if (!last_byte) {
  175. bad_aff:
  176. bb_error_msg_and_die("bad affinity '%s'", aff);
  177. }
  178. last_byte--; /* now points to the last byte */
  179. }
  180. i = 0;
  181. bit_in_byte = 1;
  182. while (last_byte >= bin) {
  183. if (bit_in_byte & *last_byte) {
  184. if (i >= CPU_SETSIZE)
  185. goto bad_aff;
  186. CPU_SET(i, &mask);
  187. //bb_error_msg("bit %d set", i);
  188. }
  189. i++;
  190. /* bit_in_byte is uint8_t! & 0xff is implied */
  191. bit_in_byte = (bit_in_byte << 1);
  192. if (!bit_in_byte) {
  193. bit_in_byte = 1;
  194. last_byte--;
  195. }
  196. }
  197. }
  198. /* Set pid's or our own (pid==0) affinity */
  199. if (sched_setaffinity(pid, sizeof(mask), &mask))
  200. bb_perror_msg_and_die("can't %cet pid %d's affinity", 's', pid);
  201. if (!argv[0]) /* "-p <aff> <pid> [...ignored...]" */
  202. goto print_aff; /* print new affinity and exit */
  203. BB_EXECVP_or_die(argv);
  204. }