blob: 4dd78dabfca41ccbb2976796e148dd35795dc597 [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 Vlasenkob097a842018-12-28 03:20:17 +01009//config: bool "chrt (4.7 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
Denys Vlasenkoae5ca6f2019-04-05 12:03:48 +020020//usage: "-m | -p [PRIO] PID | [-rfobi] PRIO 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"
Denys Vlasenkoae5ca6f2019-04-05 12:03:48 +020023//usage: "\n -m Show min/max priorities"
Pere Orga5bc8c002011-04-11 03:29:49 +020024//usage: "\n -p Operate on PID"
25//usage: "\n -r Set SCHED_RR class"
26//usage: "\n -f Set SCHED_FIFO class"
27//usage: "\n -o Set SCHED_OTHER class"
Povilas Kanapickas1616fb82018-01-13 23:26:39 +020028//usage: "\n -b Set SCHED_BATCH class"
Povilas Kanapickas0f6e4352018-01-13 23:27:37 +020029//usage: "\n -i Set SCHED_IDLE class"
Pere Orga5bc8c002011-04-11 03:29:49 +020030//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
Denys Vlasenkoae5ca6f2019-04-05 12:03:48 +020042static const char *policy_name(int pol)
43{
44 if (pol > 6)
45 return utoa(pol);
46 return nth_string(
47 "OTHER" "\0" /* 0:SCHED_OTHER */
48 "FIFO" "\0" /* 1:SCHED_FIFO */
49 "RR" "\0" /* 2:SCHED_RR */
50 "BATCH" "\0" /* 3:SCHED_BATCH */
51 "ISO" "\0" /* 4:SCHED_ISO */
52 "IDLE" "\0" /* 5:SCHED_IDLE */
53 "DEADLINE", /* 6:SCHED_DEADLINE */
54 pol
55 );
56}
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000057
Denis Vlasenkoac678ec2007-04-16 22:32:04 +000058static void show_min_max(int pol)
59{
Denys Vlasenkoae5ca6f2019-04-05 12:03:48 +020060 const char *fmt = "SCHED_%s min/max priority\t: %u/%u\n";
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000061 int max, min;
Denys Vlasenko922f6f52010-07-01 16:42:27 +020062
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000063 max = sched_get_priority_max(pol);
64 min = sched_get_priority_min(pol);
Denys Vlasenko922f6f52010-07-01 16:42:27 +020065 if ((max|min) < 0)
Denys Vlasenkoae5ca6f2019-04-05 12:03:48 +020066 fmt = "SCHED_%s not supported\n";
67 printf(fmt, policy_name(pol), min, max);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000068}
69
70#define OPT_m (1<<0)
71#define OPT_p (1<<1)
72#define OPT_r (1<<2)
73#define OPT_f (1<<3)
74#define OPT_o (1<<4)
Povilas Kanapickas1616fb82018-01-13 23:26:39 +020075#define OPT_b (1<<5)
Povilas Kanapickas0f6e4352018-01-13 23:27:37 +020076#define OPT_i (1<<6)
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000077
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000078int chrt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000079int chrt_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000080{
81 pid_t pid = 0;
82 unsigned opt;
83 struct sched_param sp;
Denis Vlasenkob44c7902008-03-17 09:29:43 +000084 char *pid_str;
85 char *priority = priority; /* for compiler */
86 const char *current_new;
87 int policy = SCHED_RR;
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000088
Denys Vlasenko66ad9be2018-01-23 16:01:13 +010089 opt = getopt32(argv, "^"
90 "+" "mprfobi"
91 "\0"
92 /* only one policy accepted: */
93 "r--fobi:f--robi:o--rfbi:b--rfoi:i--rfob"
94 );
Denys Vlasenko1393fc12010-12-19 04:07:50 +010095 if (opt & OPT_m) { /* print min/max and exit */
Denys Vlasenko66ad9be2018-01-23 16:01:13 +010096 show_min_max(SCHED_OTHER);
Denys Vlasenko1393fc12010-12-19 04:07:50 +010097 show_min_max(SCHED_FIFO);
98 show_min_max(SCHED_RR);
Povilas Kanapickas1616fb82018-01-13 23:26:39 +020099 show_min_max(SCHED_BATCH);
Povilas Kanapickas0f6e4352018-01-13 23:27:37 +0200100 show_min_max(SCHED_IDLE);
Denys Vlasenko1393fc12010-12-19 04:07:50 +0100101 fflush_stdout_and_exit(EXIT_SUCCESS);
102 }
Denys Vlasenko66ad9be2018-01-23 16:01:13 +0100103 //if (opt & OPT_r)
104 // policy = SCHED_RR; - default, already set
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000105 if (opt & OPT_f)
106 policy = SCHED_FIFO;
107 if (opt & OPT_o)
108 policy = SCHED_OTHER;
Povilas Kanapickas1616fb82018-01-13 23:26:39 +0200109 if (opt & OPT_b)
110 policy = SCHED_BATCH;
Povilas Kanapickas0f6e4352018-01-13 23:27:37 +0200111 if (opt & OPT_i)
112 policy = SCHED_IDLE;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000113
Denis Vlasenko42cc3042008-03-24 02:05:58 +0000114 argv += optind;
Denys Vlasenko1393fc12010-12-19 04:07:50 +0100115 if (!argv[0])
116 bb_show_usage();
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000117 if (opt & OPT_p) {
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000118 pid_str = *argv++;
Denys Vlasenkoae5ca6f2019-04-05 12:03:48 +0200119 if (*argv) { /* "-p PRIO PID [...]" */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000120 priority = pid_str;
121 pid_str = *argv;
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000122 }
Denys Vlasenkoae5ca6f2019-04-05 12:03:48 +0200123 /* else "-p PID", and *argv == NULL */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000124 pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000125 } else {
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000126 priority = *argv++;
127 if (!*argv)
128 bb_show_usage();
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000129 }
130
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000131 current_new = "current\0new";
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000132 if (opt & OPT_p) {
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000133 int pol;
134 print_rt_info:
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000135 pol = sched_getscheduler(pid);
136 if (pol < 0)
Denys Vlasenkoae5ca6f2019-04-05 12:03:48 +0200137 bb_perror_msg_and_die("can't %cet pid %u's policy", 'g', (int)pid);
Denys Vlasenko43d09e72019-04-05 16:59:07 +0200138#ifdef SCHED_RESET_ON_FORK
139 /* "Since Linux 2.6.32, the SCHED_RESET_ON_FORK flag
140 * can be ORed in policy when calling sched_setscheduler().
141 * As a result of including this flag, children created by
142 * fork(2) do not inherit privileged scheduling policies"
143 *
144 * This bit is also returned by sched_getscheduler()!
145 * (TODO: do we want to show it?)
146 */
147 pol &= ~SCHED_RESET_ON_FORK;
148#endif
Denys Vlasenkoae5ca6f2019-04-05 12:03:48 +0200149 printf("pid %u's %s scheduling policy: SCHED_%s\n",
150 pid, current_new, policy_name(pol)
151 );
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000152 if (sched_getparam(pid, &sp))
Denys Vlasenkoae5ca6f2019-04-05 12:03:48 +0200153 bb_perror_msg_and_die("can't get pid %u's attributes", (int)pid);
154 printf("pid %u's %s scheduling priority: %d\n",
155 (int)pid, current_new, sp.sched_priority
156 );
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000157 if (!*argv) {
Denys Vlasenkoae5ca6f2019-04-05 12:03:48 +0200158 /* Either it was just "-p PID",
159 * or it was "-p PRIO PID" and we came here
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000160 * for the second time (see goto below) */
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000161 return EXIT_SUCCESS;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000162 }
163 *argv = NULL;
164 current_new += 8;
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000165 }
166
Povilas Kanapickas1616fb82018-01-13 23:26:39 +0200167 sp.sched_priority = xstrtou_range(priority, 0,
Denys Vlasenko66ad9be2018-01-23 16:01:13 +0100168 sched_get_priority_min(policy), sched_get_priority_max(policy)
169 );
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000170
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000171 if (sched_setscheduler(pid, policy, &sp) < 0)
Denys Vlasenkoae5ca6f2019-04-05 12:03:48 +0200172 bb_perror_msg_and_die("can't %cet pid %u's policy", 's', (int)pid);
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000173
Denys Vlasenkoae5ca6f2019-04-05 12:03:48 +0200174 if (!argv[0]) /* "-p PRIO PID [...]" */
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000175 goto print_rt_info;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000176
Pascal Bellard21e8e8d2010-07-04 00:57:03 +0200177 BB_EXECVP_or_die(argv);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000178}