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