chrt.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * chrt - manipulate real-time attributes of a process
  4. * Copyright (c) 2006-2007 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. #ifndef _POSIX_PRIORITY_SCHEDULING
  11. #warning your system may be foobared
  12. #endif
  13. static const struct {
  14. int policy;
  15. char name[sizeof("SCHED_OTHER")];
  16. } policies[] = {
  17. {SCHED_OTHER, "SCHED_OTHER"},
  18. {SCHED_FIFO, "SCHED_FIFO"},
  19. {SCHED_RR, "SCHED_RR"}
  20. };
  21. //TODO: add
  22. // -b, SCHED_BATCH
  23. // -i, SCHED_IDLE
  24. static void show_min_max(int pol)
  25. {
  26. const char *fmt = "%s min/max priority\t: %u/%u\n";
  27. int max, min;
  28. max = sched_get_priority_max(pol);
  29. min = sched_get_priority_min(pol);
  30. if ((max|min) < 0)
  31. fmt = "%s not supported\n";
  32. printf(fmt, policies[pol].name, min, max);
  33. }
  34. #define OPT_m (1<<0)
  35. #define OPT_p (1<<1)
  36. #define OPT_r (1<<2)
  37. #define OPT_f (1<<3)
  38. #define OPT_o (1<<4)
  39. int chrt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  40. int chrt_main(int argc UNUSED_PARAM, char **argv)
  41. {
  42. pid_t pid = 0;
  43. unsigned opt;
  44. struct sched_param sp;
  45. char *pid_str;
  46. char *priority = priority; /* for compiler */
  47. const char *current_new;
  48. int policy = SCHED_RR;
  49. /* at least 1 arg; only one policy accepted */
  50. opt_complementary = "-1:r--fo:f--ro:r--fo";
  51. opt = getopt32(argv, "+mprfo");
  52. if (opt & OPT_r)
  53. policy = SCHED_RR;
  54. if (opt & OPT_f)
  55. policy = SCHED_FIFO;
  56. if (opt & OPT_o)
  57. policy = SCHED_OTHER;
  58. if (opt & OPT_m) { /* print min/max */
  59. show_min_max(SCHED_FIFO);
  60. show_min_max(SCHED_RR);
  61. show_min_max(SCHED_OTHER);
  62. fflush_stdout_and_exit(EXIT_SUCCESS);
  63. }
  64. argv += optind;
  65. if (opt & OPT_p) {
  66. pid_str = *argv++;
  67. if (*argv) { /* "-p <priority> <pid> [...]" */
  68. priority = pid_str;
  69. pid_str = *argv;
  70. }
  71. /* else "-p <pid>", and *argv == NULL */
  72. pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
  73. } else {
  74. priority = *argv++;
  75. if (!*argv)
  76. bb_show_usage();
  77. }
  78. current_new = "current\0new";
  79. if (opt & OPT_p) {
  80. int pol;
  81. print_rt_info:
  82. pol = sched_getscheduler(pid);
  83. if (pol < 0)
  84. bb_perror_msg_and_die("can't %cet pid %d's policy", 'g', pid);
  85. printf("pid %d's %s scheduling policy: %s\n",
  86. pid, current_new, policies[pol].name);
  87. if (sched_getparam(pid, &sp))
  88. bb_perror_msg_and_die("can't get pid %d's attributes", pid);
  89. printf("pid %d's %s scheduling priority: %d\n",
  90. pid, current_new, sp.sched_priority);
  91. if (!*argv) {
  92. /* Either it was just "-p <pid>",
  93. * or it was "-p <priority> <pid>" and we came here
  94. * for the second time (see goto below) */
  95. return EXIT_SUCCESS;
  96. }
  97. *argv = NULL;
  98. current_new += 8;
  99. }
  100. /* from the manpage of sched_getscheduler:
  101. [...] sched_priority can have a value in the range 0 to 99.
  102. [...] SCHED_OTHER or SCHED_BATCH must be assigned static priority 0.
  103. [...] SCHED_FIFO or SCHED_RR can have static priority in 1..99 range.
  104. */
  105. sp.sched_priority = xstrtou_range(priority, 0, policy != SCHED_OTHER ? 1 : 0, 99);
  106. if (sched_setscheduler(pid, policy, &sp) < 0)
  107. bb_perror_msg_and_die("can't %cet pid %d's policy", 's', pid);
  108. if (!argv[0]) /* "-p <priority> <pid> [...]" */
  109. goto print_rt_info;
  110. BB_EXECVP_or_die(argv);
  111. }