blob: c40277b39309c62d854eb9fbb43aaa379c150f9b [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 */
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +00008#include <sched.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +00009#include "libbb.h"
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000010#ifndef _POSIX_PRIORITY_SCHEDULING
11#warning your system may be foobared
12#endif
Denys Vlasenko922f6f52010-07-01 16:42:27 +020013
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000014static const struct {
Denis Vlasenkob44c7902008-03-17 09:29:43 +000015 int policy;
Denys Vlasenko922f6f52010-07-01 16:42:27 +020016 char name[sizeof("SCHED_OTHER")];
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000017} policies[] = {
18 {SCHED_OTHER, "SCHED_OTHER"},
19 {SCHED_FIFO, "SCHED_FIFO"},
20 {SCHED_RR, "SCHED_RR"}
21};
22
Denys Vlasenko922f6f52010-07-01 16:42:27 +020023//TODO: add
24// -b, SCHED_BATCH
25// -i, SCHED_IDLE
26
Denis Vlasenkoac678ec2007-04-16 22:32:04 +000027static void show_min_max(int pol)
28{
Denys Vlasenko922f6f52010-07-01 16:42:27 +020029 const char *fmt = "%s min/max priority\t: %u/%u\n";
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000030 int max, min;
Denys Vlasenko922f6f52010-07-01 16:42:27 +020031
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000032 max = sched_get_priority_max(pol);
33 min = sched_get_priority_min(pol);
Denys Vlasenko922f6f52010-07-01 16:42:27 +020034 if ((max|min) < 0)
35 fmt = "%s not supported\n";
36 printf(fmt, policies[pol].name, min, max);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000037}
38
39#define OPT_m (1<<0)
40#define OPT_p (1<<1)
41#define OPT_r (1<<2)
42#define OPT_f (1<<3)
43#define OPT_o (1<<4)
44
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000045int chrt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000046int chrt_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000047{
48 pid_t pid = 0;
49 unsigned opt;
50 struct sched_param sp;
Denis Vlasenkob44c7902008-03-17 09:29:43 +000051 char *pid_str;
52 char *priority = priority; /* for compiler */
53 const char *current_new;
54 int policy = SCHED_RR;
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000055
Denis Vlasenkob44c7902008-03-17 09:29:43 +000056 /* at least 1 arg; only one policy accepted */
57 opt_complementary = "-1:r--fo:f--ro:r--fo";
58 opt = getopt32(argv, "+mprfo");
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000059 if (opt & OPT_r)
60 policy = SCHED_RR;
61 if (opt & OPT_f)
62 policy = SCHED_FIFO;
63 if (opt & OPT_o)
64 policy = SCHED_OTHER;
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000065 if (opt & OPT_m) { /* print min/max */
66 show_min_max(SCHED_FIFO);
67 show_min_max(SCHED_RR);
68 show_min_max(SCHED_OTHER);
69 fflush_stdout_and_exit(EXIT_SUCCESS);
70 }
Denis Vlasenkob44c7902008-03-17 09:29:43 +000071
Denis Vlasenko42cc3042008-03-24 02:05:58 +000072 argv += optind;
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000073 if (opt & OPT_p) {
Denis Vlasenkob44c7902008-03-17 09:29:43 +000074 pid_str = *argv++;
75 if (*argv) { /* "-p <priority> <pid> [...]" */
76 priority = pid_str;
77 pid_str = *argv;
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000078 }
Denis Vlasenkob44c7902008-03-17 09:29:43 +000079 /* else "-p <pid>", and *argv == NULL */
80 pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000081 } else {
Denis Vlasenkob44c7902008-03-17 09:29:43 +000082 priority = *argv++;
83 if (!*argv)
84 bb_show_usage();
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000085 }
86
Denis Vlasenkob44c7902008-03-17 09:29:43 +000087 current_new = "current\0new";
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000088 if (opt & OPT_p) {
Denis Vlasenkob44c7902008-03-17 09:29:43 +000089 int pol;
90 print_rt_info:
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000091 pol = sched_getscheduler(pid);
92 if (pol < 0)
Denis Vlasenkob44c7902008-03-17 09:29:43 +000093 bb_perror_msg_and_die("can't %cet pid %d's policy", 'g', pid);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000094 printf("pid %d's %s scheduling policy: %s\n",
Denis Vlasenkob44c7902008-03-17 09:29:43 +000095 pid, current_new, policies[pol].name);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000096 if (sched_getparam(pid, &sp))
Denis Vlasenkob44c7902008-03-17 09:29:43 +000097 bb_perror_msg_and_die("can't get pid %d's attributes", pid);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +000098 printf("pid %d's %s scheduling priority: %d\n",
Denis Vlasenkob44c7902008-03-17 09:29:43 +000099 pid, current_new, sp.sched_priority);
100 if (!*argv) {
101 /* Either it was just "-p <pid>",
102 * or it was "-p <priority> <pid>" and we came here
103 * for the second time (see goto below) */
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000104 return EXIT_SUCCESS;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000105 }
106 *argv = NULL;
107 current_new += 8;
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000108 }
109
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000110 /* from the manpage of sched_getscheduler:
111 [...] sched_priority can have a value in the range 0 to 99.
112 [...] SCHED_OTHER or SCHED_BATCH must be assigned static priority 0.
113 [...] SCHED_FIFO or SCHED_RR can have static priority in 1..99 range.
114 */
115 sp.sched_priority = xstrtou_range(priority, 0, policy != SCHED_OTHER ? 1 : 0, 99);
116
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000117 if (sched_setscheduler(pid, policy, &sp) < 0)
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000118 bb_perror_msg_and_die("can't %cet pid %d's policy", 's', pid);
119
Denys Vlasenko41ddd9f2010-06-25 01:46:53 +0200120 if (!argv[0]) /* "-p <priority> <pid> [...]" */
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000121 goto print_rt_info;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000122
Pascal Bellard21e8e8d2010-07-04 00:57:03 +0200123 BB_EXECVP_or_die(argv);
Bernhard Reutner-Fischer71bc71a2007-03-09 16:56:38 +0000124}