chrt.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. //usage:#define chrt_trivial_usage
  9. //usage: "[-prfom] [PRIO] [PID | PROG ARGS]"
  10. //usage:#define chrt_full_usage "\n\n"
  11. //usage: "Change scheduling priority and class for a process\n"
  12. //usage: "\n -p Operate on PID"
  13. //usage: "\n -r Set SCHED_RR class"
  14. //usage: "\n -f Set SCHED_FIFO class"
  15. //usage: "\n -o Set SCHED_OTHER class"
  16. //usage: "\n -m Show min/max priorities"
  17. //usage:
  18. //usage:#define chrt_example_usage
  19. //usage: "$ chrt -r 4 sleep 900; x=$!\n"
  20. //usage: "$ chrt -f -p 3 $x\n"
  21. //usage: "You need CAP_SYS_NICE privileges to set scheduling attributes of a process"
  22. #include <sched.h>
  23. #include "libbb.h"
  24. static const struct {
  25. int policy;
  26. char name[sizeof("SCHED_OTHER")];
  27. } policies[] = {
  28. {SCHED_OTHER, "SCHED_OTHER"},
  29. {SCHED_FIFO, "SCHED_FIFO"},
  30. {SCHED_RR, "SCHED_RR"}
  31. };
  32. //TODO: add
  33. // -b, SCHED_BATCH
  34. // -i, SCHED_IDLE
  35. static void show_min_max(int pol)
  36. {
  37. const char *fmt = "%s min/max priority\t: %u/%u\n";
  38. int max, min;
  39. max = sched_get_priority_max(pol);
  40. min = sched_get_priority_min(pol);
  41. if ((max|min) < 0)
  42. fmt = "%s not supported\n";
  43. printf(fmt, policies[pol].name, min, max);
  44. }
  45. #define OPT_m (1<<0)
  46. #define OPT_p (1<<1)
  47. #define OPT_r (1<<2)
  48. #define OPT_f (1<<3)
  49. #define OPT_o (1<<4)
  50. int chrt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  51. int chrt_main(int argc UNUSED_PARAM, char **argv)
  52. {
  53. pid_t pid = 0;
  54. unsigned opt;
  55. struct sched_param sp;
  56. char *pid_str;
  57. char *priority = priority; /* for compiler */
  58. const char *current_new;
  59. int policy = SCHED_RR;
  60. /* only one policy accepted */
  61. opt_complementary = "r--fo:f--ro:o--rf";
  62. opt = getopt32(argv, "+mprfo");
  63. if (opt & OPT_m) { /* print min/max and exit */
  64. show_min_max(SCHED_FIFO);
  65. show_min_max(SCHED_RR);
  66. show_min_max(SCHED_OTHER);
  67. fflush_stdout_and_exit(EXIT_SUCCESS);
  68. }
  69. if (opt & OPT_r)
  70. policy = SCHED_RR;
  71. if (opt & OPT_f)
  72. policy = SCHED_FIFO;
  73. if (opt & OPT_o)
  74. policy = SCHED_OTHER;
  75. argv += optind;
  76. if (!argv[0])
  77. bb_show_usage();
  78. if (opt & OPT_p) {
  79. pid_str = *argv++;
  80. if (*argv) { /* "-p <priority> <pid> [...]" */
  81. priority = pid_str;
  82. pid_str = *argv;
  83. }
  84. /* else "-p <pid>", and *argv == NULL */
  85. pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
  86. } else {
  87. priority = *argv++;
  88. if (!*argv)
  89. bb_show_usage();
  90. }
  91. current_new = "current\0new";
  92. if (opt & OPT_p) {
  93. int pol;
  94. print_rt_info:
  95. pol = sched_getscheduler(pid);
  96. if (pol < 0)
  97. bb_perror_msg_and_die("can't %cet pid %d's policy", 'g', (int)pid);
  98. printf("pid %d's %s scheduling policy: %s\n",
  99. pid, current_new, policies[pol].name);
  100. if (sched_getparam(pid, &sp))
  101. bb_perror_msg_and_die("can't get pid %d's attributes", (int)pid);
  102. printf("pid %d's %s scheduling priority: %d\n",
  103. (int)pid, current_new, sp.sched_priority);
  104. if (!*argv) {
  105. /* Either it was just "-p <pid>",
  106. * or it was "-p <priority> <pid>" and we came here
  107. * for the second time (see goto below) */
  108. return EXIT_SUCCESS;
  109. }
  110. *argv = NULL;
  111. current_new += 8;
  112. }
  113. /* from the manpage of sched_getscheduler:
  114. [...] sched_priority can have a value in the range 0 to 99.
  115. [...] SCHED_OTHER or SCHED_BATCH must be assigned static priority 0.
  116. [...] SCHED_FIFO or SCHED_RR can have static priority in 1..99 range.
  117. */
  118. sp.sched_priority = xstrtou_range(priority, 0, policy != SCHED_OTHER ? 1 : 0, 99);
  119. if (sched_setscheduler(pid, policy, &sp) < 0)
  120. bb_perror_msg_and_die("can't %cet pid %d's policy", 's', (int)pid);
  121. if (!argv[0]) /* "-p <priority> <pid> [...]" */
  122. goto print_rt_info;
  123. BB_EXECVP_or_die(argv);
  124. }