blob: 91b5397c48f36c2c5552858597c4e6ee6a632f5d [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 */
Pere Orga5bc8c002011-04-11 03:29:49 +02008
9//usage:#define chrt_trivial_usage
10//usage: "[-prfom] [PRIO] [PID | PROG ARGS]"
11//usage:#define chrt_full_usage "\n\n"
12//usage: "Change scheduling priority and class for a process\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020013//usage: "\n -p Operate on PID"
14//usage: "\n -r Set SCHED_RR class"
15//usage: "\n -f Set SCHED_FIFO class"
16//usage: "\n -o Set SCHED_OTHER class"
17//usage: "\n -m Show min/max priorities"
18//usage:
19//usage:#define chrt_example_usage
20//usage: "$ chrt -r 4 sleep 900; x=$!\n"
21//usage: "$ chrt -f -p 3 $x\n"
22//usage: "You need CAP_SYS_NICE privileges to set scheduling attributes of a process"
23
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000024#include <sched.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000025#include "libbb.h"
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000026#ifndef _POSIX_PRIORITY_SCHEDULING
27#warning your system may be foobared
28#endif
Denys Vlasenko922f6f52010-07-01 16:42:27 +020029
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000030static const struct {
Denis Vlasenkob44c7902008-03-17 09:29:43 +000031 int policy;
Denys Vlasenko922f6f52010-07-01 16:42:27 +020032 char name[sizeof("SCHED_OTHER")];
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000033} policies[] = {
34 {SCHED_OTHER, "SCHED_OTHER"},
35 {SCHED_FIFO, "SCHED_FIFO"},
36 {SCHED_RR, "SCHED_RR"}
37};
38
Denys Vlasenko922f6f52010-07-01 16:42:27 +020039//TODO: add
40// -b, SCHED_BATCH
41// -i, SCHED_IDLE
42
Denis Vlasenkoac678ec2007-04-16 22:32:04 +000043static void show_min_max(int pol)
44{
Denys Vlasenko922f6f52010-07-01 16:42:27 +020045 const char *fmt = "%s min/max priority\t: %u/%u\n";
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000046 int max, min;
Denys Vlasenko922f6f52010-07-01 16:42:27 +020047
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000048 max = sched_get_priority_max(pol);
49 min = sched_get_priority_min(pol);
Denys Vlasenko922f6f52010-07-01 16:42:27 +020050 if ((max|min) < 0)
51 fmt = "%s not supported\n";
52 printf(fmt, policies[pol].name, min, max);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000053}
54
55#define OPT_m (1<<0)
56#define OPT_p (1<<1)
57#define OPT_r (1<<2)
58#define OPT_f (1<<3)
59#define OPT_o (1<<4)
60
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000061int chrt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000062int chrt_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000063{
64 pid_t pid = 0;
65 unsigned opt;
66 struct sched_param sp;
Denis Vlasenkob44c7902008-03-17 09:29:43 +000067 char *pid_str;
68 char *priority = priority; /* for compiler */
69 const char *current_new;
70 int policy = SCHED_RR;
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000071
Denys Vlasenko1393fc12010-12-19 04:07:50 +010072 /* only one policy accepted */
73 opt_complementary = "r--fo:f--ro:o--rf";
Denis Vlasenkob44c7902008-03-17 09:29:43 +000074 opt = getopt32(argv, "+mprfo");
Denys Vlasenko1393fc12010-12-19 04:07:50 +010075 if (opt & OPT_m) { /* print min/max and exit */
76 show_min_max(SCHED_FIFO);
77 show_min_max(SCHED_RR);
78 show_min_max(SCHED_OTHER);
79 fflush_stdout_and_exit(EXIT_SUCCESS);
80 }
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000081 if (opt & OPT_r)
82 policy = SCHED_RR;
83 if (opt & OPT_f)
84 policy = SCHED_FIFO;
85 if (opt & OPT_o)
86 policy = SCHED_OTHER;
Denis Vlasenkob44c7902008-03-17 09:29:43 +000087
Denis Vlasenko42cc3042008-03-24 02:05:58 +000088 argv += optind;
Denys Vlasenko1393fc12010-12-19 04:07:50 +010089 if (!argv[0])
90 bb_show_usage();
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000091 if (opt & OPT_p) {
Denis Vlasenkob44c7902008-03-17 09:29:43 +000092 pid_str = *argv++;
93 if (*argv) { /* "-p <priority> <pid> [...]" */
94 priority = pid_str;
95 pid_str = *argv;
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000096 }
Denis Vlasenkob44c7902008-03-17 09:29:43 +000097 /* else "-p <pid>", and *argv == NULL */
98 pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000099 } else {
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000100 priority = *argv++;
101 if (!*argv)
102 bb_show_usage();
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000103 }
104
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000105 current_new = "current\0new";
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000106 if (opt & OPT_p) {
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000107 int pol;
108 print_rt_info:
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000109 pol = sched_getscheduler(pid);
110 if (pol < 0)
Denys Vlasenko96c8a6b2010-12-18 02:59:09 +0100111 bb_perror_msg_and_die("can't %cet pid %d's policy", 'g', (int)pid);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000112 printf("pid %d's %s scheduling policy: %s\n",
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000113 pid, current_new, policies[pol].name);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000114 if (sched_getparam(pid, &sp))
Denys Vlasenko96c8a6b2010-12-18 02:59:09 +0100115 bb_perror_msg_and_die("can't get pid %d's attributes", (int)pid);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000116 printf("pid %d's %s scheduling priority: %d\n",
Denys Vlasenko96c8a6b2010-12-18 02:59:09 +0100117 (int)pid, current_new, sp.sched_priority);
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000118 if (!*argv) {
119 /* Either it was just "-p <pid>",
120 * or it was "-p <priority> <pid>" and we came here
121 * for the second time (see goto below) */
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000122 return EXIT_SUCCESS;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000123 }
124 *argv = NULL;
125 current_new += 8;
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000126 }
127
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000128 /* from the manpage of sched_getscheduler:
129 [...] sched_priority can have a value in the range 0 to 99.
130 [...] SCHED_OTHER or SCHED_BATCH must be assigned static priority 0.
131 [...] SCHED_FIFO or SCHED_RR can have static priority in 1..99 range.
132 */
133 sp.sched_priority = xstrtou_range(priority, 0, policy != SCHED_OTHER ? 1 : 0, 99);
134
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000135 if (sched_setscheduler(pid, policy, &sp) < 0)
Denys Vlasenko96c8a6b2010-12-18 02:59:09 +0100136 bb_perror_msg_and_die("can't %cet pid %d's policy", 's', (int)pid);
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000137
Denys Vlasenko41ddd9f2010-06-25 01:46:53 +0200138 if (!argv[0]) /* "-p <priority> <pid> [...]" */
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000139 goto print_rt_info;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000140
Pascal Bellard21e8e8d2010-07-04 00:57:03 +0200141 BB_EXECVP_or_die(argv);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000142}