taskset.c 3.6 KB

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