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