3
0

taskset.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * taskset - retrieve or set a processes' CPU affinity
  4. * Copyright (c) 2006 Bernhard Fischer
  5. *
  6. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  7. */
  8. #include <sched.h>
  9. #include "libbb.h"
  10. #if ENABLE_FEATURE_TASKSET_FANCY
  11. #define TASKSET_PRINTF_MASK "%s"
  12. #define from_cpuset(x) __from_cpuset(&x)
  13. /* craft a string from the mask */
  14. static char *__from_cpuset(cpu_set_t *mask)
  15. {
  16. int i;
  17. char *ret = 0, *str = xzalloc(9);
  18. for (i = CPU_SETSIZE - 4; i >= 0; i -= 4) {
  19. char 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++ = (val-'0'<=9) ? (val+48) : (val+87);
  27. }
  28. return ret;
  29. }
  30. #else
  31. #define TASKSET_PRINTF_MASK "%x"
  32. /* (void*) cast is for battling gcc: */
  33. /* "dereferencing type-punned pointer will break strict-aliasing rules" */
  34. #define from_cpuset(mask) ({ void *__vp = &(mask); *(unsigned*)__vp; })
  35. /* gcc 4.3.0 still complains: #define from_cpuset(mask) (*(unsigned*)(void*)&(mask)) */
  36. #endif
  37. int taskset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  38. int taskset_main(int argc ATTRIBUTE_UNUSED, char **argv)
  39. {
  40. cpu_set_t mask;
  41. pid_t pid = 0;
  42. unsigned opt_p;
  43. const char *current_new;
  44. char *pid_str;
  45. char *aff = aff; /* for compiler */
  46. /* NB: we mimic util-linux's taskset: -p does not take
  47. * an argument, i.e., "-pN" is NOT valid, only "-p N"!
  48. * Indeed, util-linux-2.13-pre7 uses:
  49. * getopt_long(argc, argv, "+pchV", ...), not "...p:..." */
  50. opt_complementary = "-1"; /* at least 1 arg */
  51. opt_p = getopt32(argv, "+p");
  52. argv += optind;
  53. if (opt_p) {
  54. pid_str = *argv++;
  55. if (*argv) { /* "-p <aff> <pid> ...rest.is.ignored..." */
  56. aff = pid_str;
  57. pid_str = *argv; /* NB: *argv != NULL in this case */
  58. }
  59. /* else it was just "-p <pid>", and *argv == NULL */
  60. pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
  61. } else {
  62. aff = *argv++; /* <aff> <cmd...> */
  63. if (!*argv)
  64. bb_show_usage();
  65. }
  66. current_new = "current\0new";
  67. if (opt_p) {
  68. print_aff:
  69. if (sched_getaffinity(pid, sizeof(mask), &mask) < 0)
  70. bb_perror_msg_and_die("can't %cet pid %d's affinity", 'g', pid);
  71. printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n",
  72. pid, current_new, from_cpuset(mask));
  73. if (!*argv) {
  74. /* Either it was just "-p <pid>",
  75. * or it was "-p <aff> <pid>" and we came here
  76. * for the second time (see goto below) */
  77. return EXIT_SUCCESS;
  78. }
  79. *argv = NULL;
  80. current_new += 8; /* "new" */
  81. }
  82. { /* Affinity was specified, translate it into cpu_set_t */
  83. unsigned i;
  84. /* Do not allow zero mask: */
  85. unsigned long long m = xstrtoull_range(aff, 0, 1, ULLONG_MAX);
  86. enum { CNT_BIT = CPU_SETSIZE < sizeof(m)*8 ? CPU_SETSIZE : sizeof(m)*8 };
  87. CPU_ZERO(&mask);
  88. for (i = 0; i < CNT_BIT; i++) {
  89. unsigned long long bit = (1ULL << i);
  90. if (bit & m)
  91. CPU_SET(i, &mask);
  92. }
  93. }
  94. /* Set pid's or our own (pid==0) affinity */
  95. if (sched_setaffinity(pid, sizeof(mask), &mask))
  96. bb_perror_msg_and_die("can't %cet pid %d's affinity", 's', pid);
  97. if (!*argv) /* "-p <aff> <pid> [...ignored...]" */
  98. goto print_aff; /* print new affinity and exit */
  99. BB_EXECVP(*argv, argv);
  100. bb_simple_perror_msg_and_die(*argv);
  101. }