Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * chrt - manipulate real-time attributes of a process |
Bernhard Reutner-Fischer | 6c4dade | 2008-09-25 12:13:34 +0000 | [diff] [blame] | 4 | * Copyright (c) 2006-2007 Bernhard Reutner-Fischer |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 5 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 6 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 7 | */ |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 8 | //config:config CHRT |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 9 | //config: bool "chrt (4.4 kb)" |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 10 | //config: default y |
| 11 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 12 | //config: manipulate real-time attributes of a process. |
| 13 | //config: This requires sched_{g,s}etparam support in your libc. |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 14 | |
Denys Vlasenko | 5c527dc | 2017-08-04 19:55:01 +0200 | [diff] [blame] | 15 | //applet:IF_CHRT(APPLET_NOEXEC(chrt, chrt, BB_DIR_USR_BIN, BB_SUID_DROP, chrt)) |
Denys Vlasenko | f88e3bf | 2016-11-22 23:54:17 +0100 | [diff] [blame] | 16 | |
| 17 | //kbuild:lib-$(CONFIG_CHRT) += chrt.o |
| 18 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 19 | //usage:#define chrt_trivial_usage |
Povilas Kanapickas | 0f6e435 | 2018-01-13 23:27:37 +0200 | [diff] [blame] | 20 | //usage: "[-prfombi] [PRIO] [PID | PROG ARGS]" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 21 | //usage:#define chrt_full_usage "\n\n" |
| 22 | //usage: "Change scheduling priority and class for a process\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 23 | //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 Kanapickas | 1616fb8 | 2018-01-13 23:26:39 +0200 | [diff] [blame] | 27 | //usage: "\n -b Set SCHED_BATCH class" |
Povilas Kanapickas | 0f6e435 | 2018-01-13 23:27:37 +0200 | [diff] [blame] | 28 | //usage: "\n -i Set SCHED_IDLE class" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 29 | //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-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 36 | #include <sched.h> |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 37 | #include "libbb.h" |
Denys Vlasenko | 66ad9be | 2018-01-23 16:01:13 +0100 | [diff] [blame^] | 38 | #ifndef SCHED_IDLE |
| 39 | # define SCHED_IDLE 5 |
| 40 | #endif |
Denys Vlasenko | 922f6f5 | 2010-07-01 16:42:27 +0200 | [diff] [blame] | 41 | |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 42 | static const struct { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 43 | int policy; |
Denys Vlasenko | 922f6f5 | 2010-07-01 16:42:27 +0200 | [diff] [blame] | 44 | char name[sizeof("SCHED_OTHER")]; |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 45 | } policies[] = { |
| 46 | {SCHED_OTHER, "SCHED_OTHER"}, |
| 47 | {SCHED_FIFO, "SCHED_FIFO"}, |
Povilas Kanapickas | 1616fb8 | 2018-01-13 23:26:39 +0200 | [diff] [blame] | 48 | {SCHED_RR, "SCHED_RR"}, |
Povilas Kanapickas | 0f6e435 | 2018-01-13 23:27:37 +0200 | [diff] [blame] | 49 | {SCHED_BATCH, "SCHED_BATCH"}, |
| 50 | {0 /* unused */, ""}, |
| 51 | {SCHED_IDLE, "SCHED_IDLE"} |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 54 | static void show_min_max(int pol) |
| 55 | { |
Denys Vlasenko | 922f6f5 | 2010-07-01 16:42:27 +0200 | [diff] [blame] | 56 | const char *fmt = "%s min/max priority\t: %u/%u\n"; |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 57 | int max, min; |
Denys Vlasenko | 922f6f5 | 2010-07-01 16:42:27 +0200 | [diff] [blame] | 58 | |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 59 | max = sched_get_priority_max(pol); |
| 60 | min = sched_get_priority_min(pol); |
Denys Vlasenko | 922f6f5 | 2010-07-01 16:42:27 +0200 | [diff] [blame] | 61 | if ((max|min) < 0) |
| 62 | fmt = "%s not supported\n"; |
| 63 | printf(fmt, policies[pol].name, min, max); |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 64 | } |
| 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 Kanapickas | 1616fb8 | 2018-01-13 23:26:39 +0200 | [diff] [blame] | 71 | #define OPT_b (1<<5) |
Povilas Kanapickas | 0f6e435 | 2018-01-13 23:27:37 +0200 | [diff] [blame] | 72 | #define OPT_i (1<<6) |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 73 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 74 | int chrt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 75 | int chrt_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 76 | { |
| 77 | pid_t pid = 0; |
| 78 | unsigned opt; |
| 79 | struct sched_param sp; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 80 | char *pid_str; |
| 81 | char *priority = priority; /* for compiler */ |
| 82 | const char *current_new; |
| 83 | int policy = SCHED_RR; |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 84 | |
Denys Vlasenko | 66ad9be | 2018-01-23 16:01:13 +0100 | [diff] [blame^] | 85 | 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 Vlasenko | 1393fc1 | 2010-12-19 04:07:50 +0100 | [diff] [blame] | 91 | if (opt & OPT_m) { /* print min/max and exit */ |
Denys Vlasenko | 66ad9be | 2018-01-23 16:01:13 +0100 | [diff] [blame^] | 92 | show_min_max(SCHED_OTHER); |
Denys Vlasenko | 1393fc1 | 2010-12-19 04:07:50 +0100 | [diff] [blame] | 93 | show_min_max(SCHED_FIFO); |
| 94 | show_min_max(SCHED_RR); |
Povilas Kanapickas | 1616fb8 | 2018-01-13 23:26:39 +0200 | [diff] [blame] | 95 | show_min_max(SCHED_BATCH); |
Povilas Kanapickas | 0f6e435 | 2018-01-13 23:27:37 +0200 | [diff] [blame] | 96 | show_min_max(SCHED_IDLE); |
Denys Vlasenko | 1393fc1 | 2010-12-19 04:07:50 +0100 | [diff] [blame] | 97 | fflush_stdout_and_exit(EXIT_SUCCESS); |
| 98 | } |
Denys Vlasenko | 66ad9be | 2018-01-23 16:01:13 +0100 | [diff] [blame^] | 99 | //if (opt & OPT_r) |
| 100 | // policy = SCHED_RR; - default, already set |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 101 | if (opt & OPT_f) |
| 102 | policy = SCHED_FIFO; |
| 103 | if (opt & OPT_o) |
| 104 | policy = SCHED_OTHER; |
Povilas Kanapickas | 1616fb8 | 2018-01-13 23:26:39 +0200 | [diff] [blame] | 105 | if (opt & OPT_b) |
| 106 | policy = SCHED_BATCH; |
Povilas Kanapickas | 0f6e435 | 2018-01-13 23:27:37 +0200 | [diff] [blame] | 107 | if (opt & OPT_i) |
| 108 | policy = SCHED_IDLE; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 109 | |
Denis Vlasenko | 42cc304 | 2008-03-24 02:05:58 +0000 | [diff] [blame] | 110 | argv += optind; |
Denys Vlasenko | 1393fc1 | 2010-12-19 04:07:50 +0100 | [diff] [blame] | 111 | if (!argv[0]) |
| 112 | bb_show_usage(); |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 113 | if (opt & OPT_p) { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 114 | pid_str = *argv++; |
| 115 | if (*argv) { /* "-p <priority> <pid> [...]" */ |
| 116 | priority = pid_str; |
| 117 | pid_str = *argv; |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 118 | } |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 119 | /* else "-p <pid>", and *argv == NULL */ |
| 120 | pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1); |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 121 | } else { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 122 | priority = *argv++; |
| 123 | if (!*argv) |
| 124 | bb_show_usage(); |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 127 | current_new = "current\0new"; |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 128 | if (opt & OPT_p) { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 129 | int pol; |
| 130 | print_rt_info: |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 131 | pol = sched_getscheduler(pid); |
| 132 | if (pol < 0) |
Denys Vlasenko | 96c8a6b | 2010-12-18 02:59:09 +0100 | [diff] [blame] | 133 | bb_perror_msg_and_die("can't %cet pid %d's policy", 'g', (int)pid); |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 134 | printf("pid %d's %s scheduling policy: %s\n", |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 135 | pid, current_new, policies[pol].name); |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 136 | if (sched_getparam(pid, &sp)) |
Denys Vlasenko | 96c8a6b | 2010-12-18 02:59:09 +0100 | [diff] [blame] | 137 | bb_perror_msg_and_die("can't get pid %d's attributes", (int)pid); |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 138 | printf("pid %d's %s scheduling priority: %d\n", |
Denys Vlasenko | 96c8a6b | 2010-12-18 02:59:09 +0100 | [diff] [blame] | 139 | (int)pid, current_new, sp.sched_priority); |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 140 | 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-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 144 | return EXIT_SUCCESS; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 145 | } |
| 146 | *argv = NULL; |
| 147 | current_new += 8; |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Povilas Kanapickas | 1616fb8 | 2018-01-13 23:26:39 +0200 | [diff] [blame] | 150 | sp.sched_priority = xstrtou_range(priority, 0, |
Denys Vlasenko | 66ad9be | 2018-01-23 16:01:13 +0100 | [diff] [blame^] | 151 | sched_get_priority_min(policy), sched_get_priority_max(policy) |
| 152 | ); |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 153 | |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 154 | if (sched_setscheduler(pid, policy, &sp) < 0) |
Denys Vlasenko | 96c8a6b | 2010-12-18 02:59:09 +0100 | [diff] [blame] | 155 | bb_perror_msg_and_die("can't %cet pid %d's policy", 's', (int)pid); |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 156 | |
Denys Vlasenko | 41ddd9f | 2010-06-25 01:46:53 +0200 | [diff] [blame] | 157 | if (!argv[0]) /* "-p <priority> <pid> [...]" */ |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 158 | goto print_rt_info; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 159 | |
Pascal Bellard | 21e8e8d | 2010-07-04 00:57:03 +0200 | [diff] [blame] | 160 | BB_EXECVP_or_die(argv); |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 161 | } |