blob: 2712ea3e366e4cc48be3fdbb2dd29e3d832ea209 [file] [log] [blame]
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +00001/* vi: set sw=4 ts=4: */
2/*
3 * chrt - manipulate real-time attributes of a process
Bernhard Reutner-Fischer6c4dade2008-09-25 12:13:34 +00004 * Copyright (c) 2006-2007 Bernhard Reutner-Fischer
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +00005 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02006 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +00007 */
Denys Vlasenkofb4da162016-11-22 23:14:24 +01008//config:config CHRT
Denys Vlasenko4eed2c62017-07-18 22:01:24 +02009//config: bool "chrt (4.4 kb)"
Denys Vlasenkofb4da162016-11-22 23:14:24 +010010//config: default y
11//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020012//config: manipulate real-time attributes of a process.
13//config: This requires sched_{g,s}etparam support in your libc.
Pere Orga5bc8c002011-04-11 03:29:49 +020014
Denys Vlasenko5c527dc2017-08-04 19:55:01 +020015//applet:IF_CHRT(APPLET_NOEXEC(chrt, chrt, BB_DIR_USR_BIN, BB_SUID_DROP, chrt))
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +010016
17//kbuild:lib-$(CONFIG_CHRT) += chrt.o
18
Pere Orga5bc8c002011-04-11 03:29:49 +020019//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"
Pere Orga5bc8c002011-04-11 03:29:49 +020023//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
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000034#include <sched.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000035#include "libbb.h"
Denys Vlasenko922f6f52010-07-01 16:42:27 +020036
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000037static const struct {
Denis Vlasenkob44c7902008-03-17 09:29:43 +000038 int policy;
Denys Vlasenko922f6f52010-07-01 16:42:27 +020039 char name[sizeof("SCHED_OTHER")];
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000040} policies[] = {
41 {SCHED_OTHER, "SCHED_OTHER"},
42 {SCHED_FIFO, "SCHED_FIFO"},
43 {SCHED_RR, "SCHED_RR"}
44};
45
Denys Vlasenko922f6f52010-07-01 16:42:27 +020046//TODO: add
47// -b, SCHED_BATCH
48// -i, SCHED_IDLE
49
Denis Vlasenkoac678ec2007-04-16 22:32:04 +000050static void show_min_max(int pol)
51{
Denys Vlasenko922f6f52010-07-01 16:42:27 +020052 const char *fmt = "%s min/max priority\t: %u/%u\n";
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000053 int max, min;
Denys Vlasenko922f6f52010-07-01 16:42:27 +020054
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000055 max = sched_get_priority_max(pol);
56 min = sched_get_priority_min(pol);
Denys Vlasenko922f6f52010-07-01 16:42:27 +020057 if ((max|min) < 0)
58 fmt = "%s not supported\n";
59 printf(fmt, policies[pol].name, min, max);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000060}
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
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000068int chrt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000069int chrt_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000070{
71 pid_t pid = 0;
72 unsigned opt;
73 struct sched_param sp;
Denis Vlasenkob44c7902008-03-17 09:29:43 +000074 char *pid_str;
75 char *priority = priority; /* for compiler */
76 const char *current_new;
77 int policy = SCHED_RR;
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000078
Denys Vlasenko1393fc12010-12-19 04:07:50 +010079 /* only one policy accepted */
Denys Vlasenko22542ec2017-08-08 21:55:02 +020080 opt = getopt32(argv, "^+" "mprfo" "\0" "r--fo:f--ro:o--rf");
Denys Vlasenko1393fc12010-12-19 04:07:50 +010081 if (opt & OPT_m) { /* print min/max and exit */
82 show_min_max(SCHED_FIFO);
83 show_min_max(SCHED_RR);
84 show_min_max(SCHED_OTHER);
85 fflush_stdout_and_exit(EXIT_SUCCESS);
86 }
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000087 if (opt & OPT_r)
88 policy = SCHED_RR;
89 if (opt & OPT_f)
90 policy = SCHED_FIFO;
91 if (opt & OPT_o)
92 policy = SCHED_OTHER;
Denis Vlasenkob44c7902008-03-17 09:29:43 +000093
Denis Vlasenko42cc3042008-03-24 02:05:58 +000094 argv += optind;
Denys Vlasenko1393fc12010-12-19 04:07:50 +010095 if (!argv[0])
96 bb_show_usage();
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000097 if (opt & OPT_p) {
Denis Vlasenkob44c7902008-03-17 09:29:43 +000098 pid_str = *argv++;
99 if (*argv) { /* "-p <priority> <pid> [...]" */
100 priority = pid_str;
101 pid_str = *argv;
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000102 }
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000103 /* else "-p <pid>", and *argv == NULL */
104 pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000105 } else {
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000106 priority = *argv++;
107 if (!*argv)
108 bb_show_usage();
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000109 }
110
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000111 current_new = "current\0new";
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000112 if (opt & OPT_p) {
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000113 int pol;
114 print_rt_info:
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000115 pol = sched_getscheduler(pid);
116 if (pol < 0)
Denys Vlasenko96c8a6b2010-12-18 02:59:09 +0100117 bb_perror_msg_and_die("can't %cet pid %d's policy", 'g', (int)pid);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000118 printf("pid %d's %s scheduling policy: %s\n",
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000119 pid, current_new, policies[pol].name);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000120 if (sched_getparam(pid, &sp))
Denys Vlasenko96c8a6b2010-12-18 02:59:09 +0100121 bb_perror_msg_and_die("can't get pid %d's attributes", (int)pid);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000122 printf("pid %d's %s scheduling priority: %d\n",
Denys Vlasenko96c8a6b2010-12-18 02:59:09 +0100123 (int)pid, current_new, sp.sched_priority);
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000124 if (!*argv) {
125 /* Either it was just "-p <pid>",
126 * or it was "-p <priority> <pid>" and we came here
127 * for the second time (see goto below) */
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000128 return EXIT_SUCCESS;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000129 }
130 *argv = NULL;
131 current_new += 8;
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000132 }
133
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000134 /* from the manpage of sched_getscheduler:
135 [...] sched_priority can have a value in the range 0 to 99.
136 [...] SCHED_OTHER or SCHED_BATCH must be assigned static priority 0.
137 [...] SCHED_FIFO or SCHED_RR can have static priority in 1..99 range.
138 */
139 sp.sched_priority = xstrtou_range(priority, 0, policy != SCHED_OTHER ? 1 : 0, 99);
140
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000141 if (sched_setscheduler(pid, policy, &sp) < 0)
Denys Vlasenko96c8a6b2010-12-18 02:59:09 +0100142 bb_perror_msg_and_die("can't %cet pid %d's policy", 's', (int)pid);
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000143
Denys Vlasenko41ddd9f2010-06-25 01:46:53 +0200144 if (!argv[0]) /* "-p <priority> <pid> [...]" */
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000145 goto print_rt_info;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000146
Pascal Bellard21e8e8d2010-07-04 00:57:03 +0200147 BB_EXECVP_or_die(argv);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000148}