taskset.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. BUILD_BUG_ON(CPU_SETSIZE < 8*sizeof(int));
  73. /* Take the least significant bits. Assume cpu_set_t is
  74. * implemented as an array of unsigned long or unsigned
  75. * int.
  76. */
  77. if (CPU_SETSIZE < 8*sizeof(long))
  78. return *(unsigned*)mask;
  79. if (CPU_SETSIZE < 8*sizeof(long long))
  80. return *(unsigned long*)mask;
  81. # if BB_BIG_ENDIAN
  82. if (sizeof(long long) > sizeof(long)) {
  83. /* We can put two long in the long long, but they have to
  84. * be swapped: the least significant word comes first in the
  85. * array */
  86. unsigned long *p = (void*)mask;
  87. return p[0] + ((unsigned long long)p[1] << (8*sizeof(long)));
  88. }
  89. # endif
  90. return *(unsigned long long*)mask;
  91. }
  92. #endif
  93. int taskset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  94. int taskset_main(int argc UNUSED_PARAM, char **argv)
  95. {
  96. cpu_set_t mask;
  97. pid_t pid = 0;
  98. unsigned opt_p;
  99. const char *current_new;
  100. char *pid_str;
  101. char *aff = aff; /* for compiler */
  102. /* NB: we mimic util-linux's taskset: -p does not take
  103. * an argument, i.e., "-pN" is NOT valid, only "-p N"!
  104. * Indeed, util-linux-2.13-pre7 uses:
  105. * getopt_long(argc, argv, "+pchV", ...), not "...p:..." */
  106. opt_complementary = "-1"; /* at least 1 arg */
  107. opt_p = getopt32(argv, "+p");
  108. argv += optind;
  109. if (opt_p) {
  110. pid_str = *argv++;
  111. if (*argv) { /* "-p <aff> <pid> ...rest.is.ignored..." */
  112. aff = pid_str;
  113. pid_str = *argv; /* NB: *argv != NULL in this case */
  114. }
  115. /* else it was just "-p <pid>", and *argv == NULL */
  116. pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
  117. } else {
  118. aff = *argv++; /* <aff> <cmd...> */
  119. if (!*argv)
  120. bb_show_usage();
  121. }
  122. current_new = "current\0new";
  123. if (opt_p) {
  124. print_aff:
  125. if (sched_getaffinity(pid, sizeof(mask), &mask) < 0)
  126. bb_perror_msg_and_die("can't %cet pid %d's affinity", 'g', pid);
  127. printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n",
  128. pid, current_new, from_cpuset(&mask));
  129. if (!*argv) {
  130. /* Either it was just "-p <pid>",
  131. * or it was "-p <aff> <pid>" and we came here
  132. * for the second time (see goto below) */
  133. return EXIT_SUCCESS;
  134. }
  135. *argv = NULL;
  136. current_new += 8; /* "new" */
  137. }
  138. /* Affinity was specified, translate it into cpu_set_t */
  139. CPU_ZERO(&mask);
  140. if (!ENABLE_FEATURE_TASKSET_FANCY) {
  141. unsigned i;
  142. unsigned long long m;
  143. /* Do not allow zero mask: */
  144. m = xstrtoull_range(aff, 0, 1, ULLONG_MAX);
  145. i = 0;
  146. do {
  147. if (m & 1)
  148. CPU_SET(i, &mask);
  149. i++;
  150. m >>= 1;
  151. } while (m != 0);
  152. } else {
  153. unsigned i;
  154. char *last_byte;
  155. char *bin;
  156. uint8_t bit_in_byte;
  157. /* Cheap way to get "long enough" buffer */
  158. bin = xstrdup(aff);
  159. if (aff[0] != '0' || (aff[1]|0x20) != 'x') {
  160. /* TODO: decimal/octal masks are still limited to 2^64 */
  161. unsigned long long m = xstrtoull_range(aff, 0, 1, ULLONG_MAX);
  162. bin += strlen(bin);
  163. last_byte = bin - 1;
  164. while (m) {
  165. *--bin = m & 0xff;
  166. m >>= 8;
  167. }
  168. } else {
  169. /* aff is "0x.....", we accept very long masks in this form */
  170. last_byte = hex2bin(bin, aff + 2, INT_MAX);
  171. if (!last_byte) {
  172. bad_aff:
  173. bb_error_msg_and_die("bad affinity '%s'", aff);
  174. }
  175. last_byte--; /* now points to the last byte */
  176. }
  177. i = 0;
  178. bit_in_byte = 1;
  179. while (last_byte >= bin) {
  180. if (bit_in_byte & *last_byte) {
  181. if (i >= CPU_SETSIZE)
  182. goto bad_aff;
  183. CPU_SET(i, &mask);
  184. //bb_error_msg("bit %d set", i);
  185. }
  186. i++;
  187. /* bit_in_byte is uint8_t! & 0xff is implied */
  188. bit_in_byte = (bit_in_byte << 1);
  189. if (!bit_in_byte) {
  190. bit_in_byte = 1;
  191. last_byte--;
  192. }
  193. }
  194. }
  195. /* Set pid's or our own (pid==0) affinity */
  196. if (sched_setaffinity(pid, sizeof(mask), &mask))
  197. bb_perror_msg_and_die("can't %cet pid %d's affinity", 's', pid);
  198. if (!argv[0]) /* "-p <aff> <pid> [...ignored...]" */
  199. goto print_aff; /* print new affinity and exit */
  200. BB_EXECVP_or_die(argv);
  201. }