taskset.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 <getopt.h> /* optind */
  10. #include "libbb.h"
  11. #if ENABLE_FEATURE_TASKSET_FANCY
  12. #define TASKSET_PRINTF_MASK "%s"
  13. #define from_cpuset(x) __from_cpuset(&x)
  14. /* craft a string from the mask */
  15. static char *__from_cpuset(cpu_set_t *mask)
  16. {
  17. int i;
  18. char *ret = 0, *str = xzalloc(9);
  19. for (i = CPU_SETSIZE - 4; i >= 0; i -= 4) {
  20. char val = 0;
  21. int off;
  22. for (off = 0; off <= 3; ++off)
  23. if (CPU_ISSET(i+off, mask))
  24. val |= 1<<off;
  25. if (!ret && val)
  26. ret = str;
  27. *str++ = (val-'0'<=9) ? (val+48) : (val+87);
  28. }
  29. return ret;
  30. }
  31. #else
  32. #define TASKSET_PRINTF_MASK "%x"
  33. /* (void*) cast is for battling gcc: */
  34. /* "dereferencing type-punned pointer will break strict-aliasing rules" */
  35. #define from_cpuset(mask) (*(unsigned*)(void*)&(mask))
  36. #endif
  37. #define OPT_p 1
  38. int taskset_main(int argc, char** argv);
  39. int taskset_main(int argc, char** argv)
  40. {
  41. cpu_set_t mask, new_mask;
  42. pid_t pid = 0;
  43. unsigned opt;
  44. const char *state = "current\0new";
  45. char *p_opt = NULL, *aff = NULL;
  46. opt = getopt32(argc, argv, "+p:", &p_opt);
  47. if (opt & OPT_p) {
  48. if (argc == optind+1) { /* -p <aff> <pid> */
  49. aff = p_opt;
  50. p_opt = argv[optind];
  51. }
  52. argv += optind; /* me -p <arg> */
  53. pid = xatoul_range(p_opt, 1, ULONG_MAX); /* -p <pid> */
  54. } else
  55. aff = *++argv; /* <aff> <cmd...> */
  56. if (aff) {
  57. unsigned i = 0;
  58. unsigned long l = xstrtol_range(aff, 0, 1, LONG_MAX);
  59. CPU_ZERO(&new_mask);
  60. while (i < CPU_SETSIZE && l >= (1<<i)) {
  61. if ((1<<i) & l)
  62. CPU_SET(i, &new_mask);
  63. ++i;
  64. }
  65. }
  66. if (opt & OPT_p) {
  67. print_aff:
  68. if (sched_getaffinity(pid, sizeof(mask), &mask) < 0)
  69. bb_perror_msg_and_die("failed to %cet pid %d's affinity", 'g', pid);
  70. printf("pid %d's %s affinity mask: "TASKSET_PRINTF_MASK"\n",
  71. pid, state, from_cpuset(mask));
  72. if (!*argv) /* no new affinity given or we did print already, done. */
  73. return EXIT_SUCCESS;
  74. }
  75. if (sched_setaffinity(pid, sizeof(new_mask), &new_mask))
  76. bb_perror_msg_and_die("failed to %cet pid %d's affinity", 's', pid);
  77. if (opt & OPT_p) {
  78. state += 8;
  79. ++argv;
  80. goto print_aff;
  81. }
  82. ++argv;
  83. BB_EXECVP(*argv, argv);
  84. bb_perror_msg_and_die("%s", *argv);
  85. }
  86. #undef OPT_p
  87. #undef TASKSET_PRINTF_MASK
  88. #undef from_cpuset