blob: 45459d940b140b8d50ba9088cd5d5453c6176849 [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 Vlasenkoa4972312018-01-25 09:34:23 +010012//config: Manipulate real-time attributes of a process.
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//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
Povilas Kanapickas0f6e4352018-01-13 23:27:37 +020020//usage: "[-prfombi] [PRIO] [PID | PROG ARGS]"
Pere Orga5bc8c002011-04-11 03:29:49 +020021//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"
Povilas Kanapickas1616fb82018-01-13 23:26:39 +020027//usage: "\n -b Set SCHED_BATCH class"
Povilas Kanapickas0f6e4352018-01-13 23:27:37 +020028//usage: "\n -i Set SCHED_IDLE class"
Pere Orga5bc8c002011-04-11 03:29:49 +020029//usage: "\n -m Show min/max priorities"
30//usage:
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"
35
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000036#include <sched.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000037#include "libbb.h"
Denys Vlasenko66ad9be2018-01-23 16:01:13 +010038#ifndef SCHED_IDLE
39# define SCHED_IDLE 5
40#endif
Denys Vlasenko922f6f52010-07-01 16:42:27 +020041
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000042static const struct {
Denys Vlasenko922f6f52010-07-01 16:42:27 +020043 char name[sizeof("SCHED_OTHER")];
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000044} policies[] = {
Denys Vlasenkoa4972312018-01-25 09:34:23 +010045 { "SCHED_OTHER" }, /* 0:SCHED_OTHER */
46 { "SCHED_FIFO" }, /* 1:SCHED_FIFO */
47 { "SCHED_RR" }, /* 2:SCHED_RR */
48 { "SCHED_BATCH" }, /* 3:SCHED_BATCH */
49 { "" }, /* 4:SCHED_ISO */
50 { "SCHED_IDLE" }, /* 5:SCHED_IDLE */
51 /* 6:SCHED_DEADLINE */
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000052};
53
Denis Vlasenkoac678ec2007-04-16 22:32:04 +000054static void show_min_max(int pol)
55{
Denys Vlasenko922f6f52010-07-01 16:42:27 +020056 const char *fmt = "%s min/max priority\t: %u/%u\n";
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000057 int max, min;
Denys Vlasenko922f6f52010-07-01 16:42:27 +020058
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000059 max = sched_get_priority_max(pol);
60 min = sched_get_priority_min(pol);
Denys Vlasenko922f6f52010-07-01 16:42:27 +020061 if ((max|min) < 0)
62 fmt = "%s not supported\n";
63 printf(fmt, policies[pol].name, min, max);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000064}
65
66#define OPT_m (1<<0)
67#define OPT_p (1<<1)
68#define OPT_r (1<<2)
69#define OPT_f (1<<3)
70#define OPT_o (1<<4)
Povilas Kanapickas1616fb82018-01-13 23:26:39 +020071#define OPT_b (1<<5)
Povilas Kanapickas0f6e4352018-01-13 23:27:37 +020072#define OPT_i (1<<6)
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000073
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000074int chrt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000075int chrt_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000076{
77 pid_t pid = 0;
78 unsigned opt;
79 struct sched_param sp;
Denis Vlasenkob44c7902008-03-17 09:29:43 +000080 char *pid_str;
81 char *priority = priority; /* for compiler */
82 const char *current_new;
83 int policy = SCHED_RR;
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000084
Denys Vlasenko66ad9be2018-01-23 16:01:13 +010085 opt = getopt32(argv, "^"
86 "+" "mprfobi"
87 "\0"
88 /* only one policy accepted: */
89 "r--fobi:f--robi:o--rfbi:b--rfoi:i--rfob"
90 );
Denys Vlasenko1393fc12010-12-19 04:07:50 +010091 if (opt & OPT_m) { /* print min/max and exit */
Denys Vlasenko66ad9be2018-01-23 16:01:13 +010092 show_min_max(SCHED_OTHER);
Denys Vlasenko1393fc12010-12-19 04:07:50 +010093 show_min_max(SCHED_FIFO);
94 show_min_max(SCHED_RR);
Povilas Kanapickas1616fb82018-01-13 23:26:39 +020095 show_min_max(SCHED_BATCH);
Povilas Kanapickas0f6e4352018-01-13 23:27:37 +020096 show_min_max(SCHED_IDLE);
Denys Vlasenko1393fc12010-12-19 04:07:50 +010097 fflush_stdout_and_exit(EXIT_SUCCESS);
98 }
Denys Vlasenko66ad9be2018-01-23 16:01:13 +010099 //if (opt & OPT_r)
100 // policy = SCHED_RR; - default, already set
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000101 if (opt & OPT_f)
102 policy = SCHED_FIFO;
103 if (opt & OPT_o)
104 policy = SCHED_OTHER;
Povilas Kanapickas1616fb82018-01-13 23:26:39 +0200105 if (opt & OPT_b)
106 policy = SCHED_BATCH;
Povilas Kanapickas0f6e4352018-01-13 23:27:37 +0200107 if (opt & OPT_i)
108 policy = SCHED_IDLE;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000109
Denis Vlasenko42cc3042008-03-24 02:05:58 +0000110 argv += optind;
Denys Vlasenko1393fc12010-12-19 04:07:50 +0100111 if (!argv[0])
112 bb_show_usage();
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000113 if (opt & OPT_p) {
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000114 pid_str = *argv++;
115 if (*argv) { /* "-p <priority> <pid> [...]" */
116 priority = pid_str;
117 pid_str = *argv;
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000118 }
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000119 /* else "-p <pid>", and *argv == NULL */
120 pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000121 } else {
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000122 priority = *argv++;
123 if (!*argv)
124 bb_show_usage();
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000125 }
126
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000127 current_new = "current\0new";
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000128 if (opt & OPT_p) {
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000129 int pol;
130 print_rt_info:
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000131 pol = sched_getscheduler(pid);
132 if (pol < 0)
Denys Vlasenko96c8a6b2010-12-18 02:59:09 +0100133 bb_perror_msg_and_die("can't %cet pid %d's policy", 'g', (int)pid);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000134 printf("pid %d's %s scheduling policy: %s\n",
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000135 pid, current_new, policies[pol].name);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000136 if (sched_getparam(pid, &sp))
Denys Vlasenko96c8a6b2010-12-18 02:59:09 +0100137 bb_perror_msg_and_die("can't get pid %d's attributes", (int)pid);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000138 printf("pid %d's %s scheduling priority: %d\n",
Denys Vlasenko96c8a6b2010-12-18 02:59:09 +0100139 (int)pid, current_new, sp.sched_priority);
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000140 if (!*argv) {
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) */
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000144 return EXIT_SUCCESS;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000145 }
146 *argv = NULL;
147 current_new += 8;
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000148 }
149
Povilas Kanapickas1616fb82018-01-13 23:26:39 +0200150 sp.sched_priority = xstrtou_range(priority, 0,
Denys Vlasenko66ad9be2018-01-23 16:01:13 +0100151 sched_get_priority_min(policy), sched_get_priority_max(policy)
152 );
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000153
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000154 if (sched_setscheduler(pid, policy, &sp) < 0)
Denys Vlasenko96c8a6b2010-12-18 02:59:09 +0100155 bb_perror_msg_and_die("can't %cet pid %d's policy", 's', (int)pid);
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000156
Denys Vlasenko41ddd9f2010-06-25 01:46:53 +0200157 if (!argv[0]) /* "-p <priority> <pid> [...]" */
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000158 goto print_rt_info;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000159
Pascal Bellard21e8e8d2010-07-04 00:57:03 +0200160 BB_EXECVP_or_die(argv);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000161}