taskset.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 (4.2 kb)"
  10. //config: default y
  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: Needed for machines with more than 32-64 CPUs:
  21. //config: affinity parameter 0xHHHHHHHHHHHHHHHHHHHH can be arbitrarily long
  22. //config: in this case. Otherwise, it is limited to sizeof(long).
  23. //applet:IF_TASKSET(APPLET_NOEXEC(taskset, taskset, BB_DIR_USR_BIN, BB_SUID_DROP, taskset))
  24. //kbuild:lib-$(CONFIG_TASKSET) += taskset.o
  25. //usage:#define taskset_trivial_usage
  26. //usage: "[-p] [HEXMASK] 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. * needs to get TIDs from /proc/PID/task/ and use _them_ as "pid" in sched_setaffinity(pid)
  45. * -c/--cpu-list (specify CPUs via "1,3,5-7")
  46. */
  47. #include <sched.h>
  48. #include "libbb.h"
  49. typedef unsigned long ul;
  50. #define SZOF_UL (unsigned)(sizeof(ul))
  51. #define BITS_UL (unsigned)(sizeof(ul)*8)
  52. #define MASK_UL (unsigned)(sizeof(ul)*8 - 1)
  53. #if ENABLE_FEATURE_TASKSET_FANCY
  54. #define TASKSET_PRINTF_MASK "%s"
  55. /* craft a string from the mask */
  56. static char *from_mask(const ul *mask, unsigned sz_in_bytes)
  57. {
  58. char *str = xzalloc((sz_in_bytes+1) * 2); /* we will leak it */
  59. char *p = str;
  60. for (;;) {
  61. ul v = *mask++;
  62. if (SZOF_UL == 4)
  63. p += sprintf(p, "%08lx", v);
  64. if (SZOF_UL == 8)
  65. p += sprintf(p, "%016lx", v);
  66. if (SZOF_UL == 16)
  67. p += sprintf(p, "%032lx", v); /* :) */
  68. sz_in_bytes -= SZOF_UL;
  69. if ((int)sz_in_bytes <= 0)
  70. break;
  71. }
  72. while (str[0] == '0' && str[1])
  73. str++;
  74. return str;
  75. }
  76. #else
  77. #define TASKSET_PRINTF_MASK "%lx"
  78. static unsigned long long from_mask(ul *mask, unsigned sz_in_bytes UNUSED_PARAM)
  79. {
  80. return *mask;
  81. }
  82. #endif
  83. static unsigned long *get_aff(int pid, unsigned *sz)
  84. {
  85. int r;
  86. unsigned long *mask = NULL;
  87. unsigned sz_in_bytes = *sz;
  88. for (;;) {
  89. mask = xrealloc(mask, sz_in_bytes);
  90. r = sched_getaffinity(pid, sz_in_bytes, (void*)mask);
  91. if (r == 0)
  92. break;
  93. sz_in_bytes *= 2;
  94. if (errno == EINVAL && (int)sz_in_bytes > 0)
  95. continue;
  96. bb_perror_msg_and_die("can't %cet pid %d's affinity", 'g', pid);
  97. }
  98. //bb_error_msg("get mask[0]:%lx sz_in_bytes:%d", mask[0], sz_in_bytes);
  99. *sz = sz_in_bytes;
  100. return mask;
  101. }
  102. int taskset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  103. int taskset_main(int argc UNUSED_PARAM, char **argv)
  104. {
  105. ul *mask;
  106. unsigned mask_size_in_bytes;
  107. pid_t pid = 0;
  108. unsigned opt_p;
  109. const char *current_new;
  110. char *aff;
  111. /* NB: we mimic util-linux's taskset: -p does not take
  112. * an argument, i.e., "-pN" is NOT valid, only "-p N"!
  113. * Indeed, util-linux-2.13-pre7 uses:
  114. * getopt_long(argc, argv, "+pchV", ...), not "...p:..." */
  115. opt_p = getopt32(argv, "^+" "p" "\0" "-1" /* at least 1 arg */);
  116. argv += optind;
  117. aff = *argv++;
  118. if (opt_p) {
  119. char *pid_str = aff;
  120. if (*argv) { /* "-p <aff> <pid> ...rest.is.ignored..." */
  121. pid_str = *argv; /* NB: *argv != NULL in this case */
  122. }
  123. /* else it was just "-p <pid>", and *argv == NULL */
  124. pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
  125. } else {
  126. /* <aff> <cmd...> */
  127. if (!*argv)
  128. bb_show_usage();
  129. }
  130. mask_size_in_bytes = SZOF_UL;
  131. current_new = "current";
  132. print_aff:
  133. mask = get_aff(pid, &mask_size_in_bytes);
  134. if (opt_p) {
  135. printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n",
  136. pid, current_new, from_mask(mask, mask_size_in_bytes));
  137. if (*argv == NULL) {
  138. /* Either it was just "-p <pid>",
  139. * or it was "-p <aff> <pid>" and we came here
  140. * for the second time (see goto below) */
  141. return EXIT_SUCCESS;
  142. }
  143. *argv = NULL;
  144. current_new = "new";
  145. }
  146. memset(mask, 0, mask_size_in_bytes);
  147. /* Affinity was specified, translate it into mask */
  148. /* it is always in hex, skip "0x" if it exists */
  149. if (aff[0] == '0' && (aff[1]|0x20) == 'x')
  150. aff += 2;
  151. if (!ENABLE_FEATURE_TASKSET_FANCY) {
  152. mask[0] = xstrtoul(aff, 16);
  153. } else {
  154. unsigned i;
  155. char *last_char;
  156. i = 0; /* bit pos in mask[] */
  157. /* aff is ASCII hex string, accept very long masks in this form.
  158. * Process hex string AABBCCDD... to ulong mask[]
  159. * from the rightmost nibble, which is least-significant.
  160. * Bits not fitting into mask[] are ignored: (example: 1234
  161. * in 12340000000000000000000000000000000000000ff)
  162. */
  163. last_char = strchrnul(aff, '\0');
  164. while (last_char > aff) {
  165. char c;
  166. ul val;
  167. last_char--;
  168. c = *last_char;
  169. if (isdigit(c))
  170. val = c - '0';
  171. else if ((c|0x20) >= 'a' && (c|0x20) <= 'f')
  172. val = (c|0x20) - ('a' - 10);
  173. else
  174. bb_error_msg_and_die("bad affinity '%s'", aff);
  175. if (i < mask_size_in_bytes * 8) {
  176. mask[i / BITS_UL] |= val << (i & MASK_UL);
  177. //bb_error_msg("bit %d set", i);
  178. }
  179. /* else:
  180. * We can error out here, but we don't.
  181. * For one, kernel itself ignores bits in mask[]
  182. * which do not map to any CPUs:
  183. * if mask[] has one 32-bit long element,
  184. * but you have only 8 CPUs, all bits beyond first 8
  185. * are ignored, silently.
  186. * No point in making bits past 31th to be errors.
  187. */
  188. i += 4;
  189. }
  190. }
  191. /* Set pid's or our own (pid==0) affinity */
  192. if (sched_setaffinity(pid, mask_size_in_bytes, (void*)mask))
  193. bb_perror_msg_and_die("can't %cet pid %d's affinity", 's', pid);
  194. //bb_error_msg("set mask[0]:%lx", mask[0]);
  195. if (!argv[0]) /* "-p <aff> <pid> [...ignored...]" */
  196. goto print_aff; /* print new affinity and exit */
  197. BB_EXECVP_or_die(argv);
  198. }