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 |
| 4 | * Copyright (c) 2006-2007 Bernhard Fischer |
| 5 | * |
| 6 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 7 | */ |
| 8 | |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 9 | #include <sched.h> |
| 10 | #include <getopt.h> /* optind */ |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 11 | #include "libbb.h" |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 12 | #ifndef _POSIX_PRIORITY_SCHEDULING |
| 13 | #warning your system may be foobared |
| 14 | #endif |
| 15 | static const struct { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame^] | 16 | int policy; |
| 17 | char name[12]; |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 18 | } policies[] = { |
| 19 | {SCHED_OTHER, "SCHED_OTHER"}, |
| 20 | {SCHED_FIFO, "SCHED_FIFO"}, |
| 21 | {SCHED_RR, "SCHED_RR"} |
| 22 | }; |
| 23 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 24 | static void show_min_max(int pol) |
| 25 | { |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 26 | const char *fmt = "%s min/max priority\t: %d/%d\n\0%s not supported?\n"; |
| 27 | int max, min; |
| 28 | max = sched_get_priority_max(pol); |
| 29 | min = sched_get_priority_min(pol); |
| 30 | if (max >= 0 && min >= 0) |
| 31 | printf(fmt, policies[pol].name, min, max); |
| 32 | else { |
| 33 | fmt += 29; |
| 34 | printf(fmt, policies[pol].name); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | #define OPT_m (1<<0) |
| 39 | #define OPT_p (1<<1) |
| 40 | #define OPT_r (1<<2) |
| 41 | #define OPT_f (1<<3) |
| 42 | #define OPT_o (1<<4) |
| 43 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 44 | int chrt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame^] | 45 | int chrt_main(int argc ATTRIBUTE_UNUSED, char **argv) |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 46 | { |
| 47 | pid_t pid = 0; |
| 48 | unsigned opt; |
| 49 | struct sched_param sp; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame^] | 50 | char *pid_str; |
| 51 | char *priority = priority; /* for compiler */ |
| 52 | const char *current_new; |
| 53 | int policy = SCHED_RR; |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 54 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame^] | 55 | /* at least 1 arg; only one policy accepted */ |
| 56 | opt_complementary = "-1:r--fo:f--ro:r--fo"; |
| 57 | opt = getopt32(argv, "+mprfo"); |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 58 | if (opt & OPT_r) |
| 59 | policy = SCHED_RR; |
| 60 | if (opt & OPT_f) |
| 61 | policy = SCHED_FIFO; |
| 62 | if (opt & OPT_o) |
| 63 | policy = SCHED_OTHER; |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 64 | if (opt & OPT_m) { /* print min/max */ |
| 65 | show_min_max(SCHED_FIFO); |
| 66 | show_min_max(SCHED_RR); |
| 67 | show_min_max(SCHED_OTHER); |
| 68 | fflush_stdout_and_exit(EXIT_SUCCESS); |
| 69 | } |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame^] | 70 | |
| 71 | argv += optind; |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 72 | if (opt & OPT_p) { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame^] | 73 | pid_str = *argv++; |
| 74 | if (*argv) { /* "-p <priority> <pid> [...]" */ |
| 75 | priority = pid_str; |
| 76 | pid_str = *argv; |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 77 | } |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame^] | 78 | /* else "-p <pid>", and *argv == NULL */ |
| 79 | 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] | 80 | } else { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame^] | 81 | priority = *argv++; |
| 82 | if (!*argv) |
| 83 | bb_show_usage(); |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame^] | 86 | current_new = "current\0new"; |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 87 | if (opt & OPT_p) { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame^] | 88 | int pol; |
| 89 | print_rt_info: |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 90 | pol = sched_getscheduler(pid); |
| 91 | if (pol < 0) |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame^] | 92 | bb_perror_msg_and_die("can't %cet pid %d's policy", 'g', pid); |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 93 | printf("pid %d's %s scheduling policy: %s\n", |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame^] | 94 | pid, current_new, policies[pol].name); |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 95 | if (sched_getparam(pid, &sp)) |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame^] | 96 | bb_perror_msg_and_die("can't get pid %d's attributes", pid); |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 97 | printf("pid %d's %s scheduling priority: %d\n", |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame^] | 98 | pid, current_new, sp.sched_priority); |
| 99 | if (!*argv) { |
| 100 | /* Either it was just "-p <pid>", |
| 101 | * or it was "-p <priority> <pid>" and we came here |
| 102 | * for the second time (see goto below) */ |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 103 | return EXIT_SUCCESS; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame^] | 104 | } |
| 105 | *argv = NULL; |
| 106 | current_new += 8; |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame^] | 109 | /* from the manpage of sched_getscheduler: |
| 110 | [...] sched_priority can have a value in the range 0 to 99. |
| 111 | [...] SCHED_OTHER or SCHED_BATCH must be assigned static priority 0. |
| 112 | [...] SCHED_FIFO or SCHED_RR can have static priority in 1..99 range. |
| 113 | */ |
| 114 | sp.sched_priority = xstrtou_range(priority, 0, policy != SCHED_OTHER ? 1 : 0, 99); |
| 115 | |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 116 | if (sched_setscheduler(pid, policy, &sp) < 0) |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame^] | 117 | bb_perror_msg_and_die("can't %cet pid %d's policy", 's', pid); |
| 118 | |
| 119 | if (!*argv) /* "-p <priority> <pid> [...]" */ |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 120 | goto print_rt_info; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame^] | 121 | |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 122 | BB_EXECVP(*argv, argv); |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 123 | bb_simple_perror_msg_and_die(*argv); |
Bernhard Reutner-Fischer | 71bc71a | 2007-03-09 16:56:38 +0000 | [diff] [blame] | 124 | } |