chrt.c 4.0 KB

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