chrt.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 (4.4 kb)"
  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_NOEXEC(chrt, chrt, BB_DIR_USR_BIN, BB_SUID_DROP, chrt))
  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 = getopt32(argv, "^+" "mprfo" "\0" "r--fo:f--ro:o--rf");
  70. if (opt & OPT_m) { /* print min/max and exit */
  71. show_min_max(SCHED_FIFO);
  72. show_min_max(SCHED_RR);
  73. show_min_max(SCHED_OTHER);
  74. fflush_stdout_and_exit(EXIT_SUCCESS);
  75. }
  76. if (opt & OPT_r)
  77. policy = SCHED_RR;
  78. if (opt & OPT_f)
  79. policy = SCHED_FIFO;
  80. if (opt & OPT_o)
  81. policy = SCHED_OTHER;
  82. argv += optind;
  83. if (!argv[0])
  84. bb_show_usage();
  85. if (opt & OPT_p) {
  86. pid_str = *argv++;
  87. if (*argv) { /* "-p <priority> <pid> [...]" */
  88. priority = pid_str;
  89. pid_str = *argv;
  90. }
  91. /* else "-p <pid>", and *argv == NULL */
  92. pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
  93. } else {
  94. priority = *argv++;
  95. if (!*argv)
  96. bb_show_usage();
  97. }
  98. current_new = "current\0new";
  99. if (opt & OPT_p) {
  100. int pol;
  101. print_rt_info:
  102. pol = sched_getscheduler(pid);
  103. if (pol < 0)
  104. bb_perror_msg_and_die("can't %cet pid %d's policy", 'g', (int)pid);
  105. printf("pid %d's %s scheduling policy: %s\n",
  106. pid, current_new, policies[pol].name);
  107. if (sched_getparam(pid, &sp))
  108. bb_perror_msg_and_die("can't get pid %d's attributes", (int)pid);
  109. printf("pid %d's %s scheduling priority: %d\n",
  110. (int)pid, current_new, sp.sched_priority);
  111. if (!*argv) {
  112. /* Either it was just "-p <pid>",
  113. * or it was "-p <priority> <pid>" and we came here
  114. * for the second time (see goto below) */
  115. return EXIT_SUCCESS;
  116. }
  117. *argv = NULL;
  118. current_new += 8;
  119. }
  120. /* from the manpage of sched_getscheduler:
  121. [...] sched_priority can have a value in the range 0 to 99.
  122. [...] SCHED_OTHER or SCHED_BATCH must be assigned static priority 0.
  123. [...] SCHED_FIFO or SCHED_RR can have static priority in 1..99 range.
  124. */
  125. sp.sched_priority = xstrtou_range(priority, 0, policy != SCHED_OTHER ? 1 : 0, 99);
  126. if (sched_setscheduler(pid, policy, &sp) < 0)
  127. bb_perror_msg_and_die("can't %cet pid %d's policy", 's', (int)pid);
  128. if (!argv[0]) /* "-p <priority> <pid> [...]" */
  129. goto print_rt_info;
  130. BB_EXECVP_or_die(argv);
  131. }