taskset.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. //usage:#define taskset_trivial_usage
  9. //usage: "[-p] [MASK] [PID | PROG ARGS]"
  10. //usage:#define taskset_full_usage "\n\n"
  11. //usage: "Set or get CPU affinity\n"
  12. //usage: "\n -p Operate on an existing PID"
  13. //usage:
  14. //usage:#define taskset_example_usage
  15. //usage: "$ taskset 0x7 ./dgemm_test&\n"
  16. //usage: "$ taskset -p 0x1 $!\n"
  17. //usage: "pid 4790's current affinity mask: 7\n"
  18. //usage: "pid 4790's new affinity mask: 1\n"
  19. //usage: "$ taskset 0x7 /bin/sh -c './taskset -p 0x1 $$'\n"
  20. //usage: "pid 6671's current affinity mask: 1\n"
  21. //usage: "pid 6671's new affinity mask: 1\n"
  22. //usage: "$ taskset -p 1\n"
  23. //usage: "pid 1's current affinity mask: 3\n"
  24. #include <sched.h>
  25. #include "libbb.h"
  26. #if ENABLE_FEATURE_TASKSET_FANCY
  27. #define TASKSET_PRINTF_MASK "%s"
  28. /* craft a string from the mask */
  29. static char *from_cpuset(cpu_set_t *mask)
  30. {
  31. int i;
  32. char *ret = NULL;
  33. char *str = xzalloc((CPU_SETSIZE / 4) + 1); /* we will leak it */
  34. for (i = CPU_SETSIZE - 4; i >= 0; i -= 4) {
  35. int val = 0;
  36. int off;
  37. for (off = 0; off <= 3; ++off)
  38. if (CPU_ISSET(i + off, mask))
  39. val |= 1 << off;
  40. if (!ret && val)
  41. ret = str;
  42. *str++ = bb_hexdigits_upcase[val] | 0x20;
  43. }
  44. return ret;
  45. }
  46. #else
  47. #define TASKSET_PRINTF_MASK "%llx"
  48. static unsigned long long from_cpuset(cpu_set_t *mask)
  49. {
  50. struct BUG_CPU_SETSIZE_is_too_small {
  51. char BUG_CPU_SETSIZE_is_too_small[
  52. CPU_SETSIZE < sizeof(int) ? -1 : 1];
  53. };
  54. char *p = (void*)mask;
  55. /* Take the least significant bits. Careful!
  56. * Consider both CPU_SETSIZE=4 and CPU_SETSIZE=1024 cases
  57. */
  58. #if BB_BIG_ENDIAN
  59. /* For big endian, it means LAST bits */
  60. if (CPU_SETSIZE < sizeof(long))
  61. p += CPU_SETSIZE - sizeof(int);
  62. else if (CPU_SETSIZE < sizeof(long long))
  63. p += CPU_SETSIZE - sizeof(long);
  64. else
  65. p += CPU_SETSIZE - sizeof(long long);
  66. #endif
  67. if (CPU_SETSIZE < sizeof(long))
  68. return *(unsigned*)p;
  69. if (CPU_SETSIZE < sizeof(long long))
  70. return *(unsigned long*)p;
  71. return *(unsigned long long*)p;
  72. }
  73. #endif
  74. int taskset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  75. int taskset_main(int argc UNUSED_PARAM, char **argv)
  76. {
  77. cpu_set_t mask;
  78. pid_t pid = 0;
  79. unsigned opt_p;
  80. const char *current_new;
  81. char *pid_str;
  82. char *aff = aff; /* for compiler */
  83. /* NB: we mimic util-linux's taskset: -p does not take
  84. * an argument, i.e., "-pN" is NOT valid, only "-p N"!
  85. * Indeed, util-linux-2.13-pre7 uses:
  86. * getopt_long(argc, argv, "+pchV", ...), not "...p:..." */
  87. opt_complementary = "-1"; /* at least 1 arg */
  88. opt_p = getopt32(argv, "+p");
  89. argv += optind;
  90. if (opt_p) {
  91. pid_str = *argv++;
  92. if (*argv) { /* "-p <aff> <pid> ...rest.is.ignored..." */
  93. aff = pid_str;
  94. pid_str = *argv; /* NB: *argv != NULL in this case */
  95. }
  96. /* else it was just "-p <pid>", and *argv == NULL */
  97. pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
  98. } else {
  99. aff = *argv++; /* <aff> <cmd...> */
  100. if (!*argv)
  101. bb_show_usage();
  102. }
  103. current_new = "current\0new";
  104. if (opt_p) {
  105. print_aff:
  106. if (sched_getaffinity(pid, sizeof(mask), &mask) < 0)
  107. bb_perror_msg_and_die("can't %cet pid %d's affinity", 'g', pid);
  108. printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n",
  109. pid, current_new, from_cpuset(&mask));
  110. if (!*argv) {
  111. /* Either it was just "-p <pid>",
  112. * or it was "-p <aff> <pid>" and we came here
  113. * for the second time (see goto below) */
  114. return EXIT_SUCCESS;
  115. }
  116. *argv = NULL;
  117. current_new += 8; /* "new" */
  118. }
  119. { /* Affinity was specified, translate it into cpu_set_t */
  120. unsigned i;
  121. /* Do not allow zero mask: */
  122. unsigned long long m = xstrtoull_range(aff, 0, 1, ULLONG_MAX);
  123. enum { CNT_BIT = CPU_SETSIZE < sizeof(m)*8 ? CPU_SETSIZE : sizeof(m)*8 };
  124. CPU_ZERO(&mask);
  125. for (i = 0; i < CNT_BIT; i++) {
  126. unsigned long long bit = (1ULL << i);
  127. if (bit & m)
  128. CPU_SET(i, &mask);
  129. }
  130. }
  131. /* Set pid's or our own (pid==0) affinity */
  132. if (sched_setaffinity(pid, sizeof(mask), &mask))
  133. bb_perror_msg_and_die("can't %cet pid %d's affinity", 's', pid);
  134. if (!argv[0]) /* "-p <aff> <pid> [...ignored...]" */
  135. goto print_aff; /* print new affinity and exit */
  136. BB_EXECVP_or_die(argv);
  137. }