1 /* vi: set sw=4 ts=4: */
3 * chrt - manipulate real-time attributes of a process
4 * Copyright (c) 2006-2007 Bernhard Reutner-Fischer
6 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9 //config: bool "chrt (4.4 kb)"
12 //config: manipulate real-time attributes of a process.
13 //config: This requires sched_{g,s}etparam support in your libc.
15 //applet:IF_CHRT(APPLET_NOEXEC(chrt, chrt, BB_DIR_USR_BIN, BB_SUID_DROP, chrt))
17 //kbuild:lib-$(CONFIG_CHRT) += chrt.o
19 //usage:#define chrt_trivial_usage
20 //usage: "[-prfombi] [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 -i Set SCHED_IDLE class"
29 //usage: "\n -m Show min/max priorities"
31 //usage:#define chrt_example_usage
32 //usage: "$ chrt -r 4 sleep 900; x=$!\n"
33 //usage: "$ chrt -f -p 3 $x\n"
34 //usage: "You need CAP_SYS_NICE privileges to set scheduling attributes of a process"
44 char name[sizeof("SCHED_OTHER")];
46 {SCHED_OTHER, "SCHED_OTHER"},
47 {SCHED_FIFO, "SCHED_FIFO"},
48 {SCHED_RR, "SCHED_RR"},
49 {SCHED_BATCH, "SCHED_BATCH"},
51 {SCHED_IDLE, "SCHED_IDLE"}
54 static void show_min_max(int pol)
56 const char *fmt = "%s min/max priority\t: %u/%u\n";
59 max = sched_get_priority_max(pol);
60 min = sched_get_priority_min(pol);
62 fmt = "%s not supported\n";
63 printf(fmt, policies[pol].name, min, max);
74 int chrt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
75 int chrt_main(int argc UNUSED_PARAM, char **argv)
79 struct sched_param sp;
81 char *priority = priority; /* for compiler */
82 const char *current_new;
83 int policy = SCHED_RR;
85 opt = getopt32(argv, "^"
88 /* only one policy accepted: */
89 "r--fobi:f--robi:o--rfbi:b--rfoi:i--rfob"
91 if (opt & OPT_m) { /* print min/max and exit */
92 show_min_max(SCHED_OTHER);
93 show_min_max(SCHED_FIFO);
94 show_min_max(SCHED_RR);
95 show_min_max(SCHED_BATCH);
96 show_min_max(SCHED_IDLE);
97 fflush_stdout_and_exit(EXIT_SUCCESS);
100 // policy = SCHED_RR; - default, already set
104 policy = SCHED_OTHER;
106 policy = SCHED_BATCH;
115 if (*argv) { /* "-p <priority> <pid> [...]" */
119 /* else "-p <pid>", and *argv == NULL */
120 pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
127 current_new = "current\0new";
131 pol = sched_getscheduler(pid);
133 bb_perror_msg_and_die("can't %cet pid %d's policy", 'g', (int)pid);
134 printf("pid %d's %s scheduling policy: %s\n",
135 pid, current_new, policies[pol].name);
136 if (sched_getparam(pid, &sp))
137 bb_perror_msg_and_die("can't get pid %d's attributes", (int)pid);
138 printf("pid %d's %s scheduling priority: %d\n",
139 (int)pid, current_new, sp.sched_priority);
141 /* Either it was just "-p <pid>",
142 * or it was "-p <priority> <pid>" and we came here
143 * for the second time (see goto below) */
150 sp.sched_priority = xstrtou_range(priority, 0,
151 sched_get_priority_min(policy), sched_get_priority_max(policy)
154 if (sched_setscheduler(pid, policy, &sp) < 0)
155 bb_perror_msg_and_die("can't %cet pid %d's policy", 's', (int)pid);
157 if (!argv[0]) /* "-p <priority> <pid> [...]" */
160 BB_EXECVP_or_die(argv);