[PATCH 4/4] chrt: don't rely on exact values of SCHED_* defines
--- util-linux/chrt.c | 29 - 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/util-linux/chrt.c b/util-linux/chrt.c index 5c5513aeb..8fd4b7bea 100644 --- a/util-linux/chrt.c +++ b/util-linux/chrt.c @@ -36,17 +36,20 @@ #include #include "libbb.h" -static const struct { - int policy; - char name[sizeof("SCHED_OTHER")]; -} policies[] = { - {SCHED_OTHER, "SCHED_OTHER"}, - {SCHED_FIFO, "SCHED_FIFO"}, - {SCHED_RR, "SCHED_RR"}, - {SCHED_BATCH, "SCHED_BATCH"}, - {0 /* unused */, ""}, - {SCHED_IDLE, "SCHED_IDLE"} -}; +static const char* get_policy_name(int pol) +{ + if (pol == SCHED_OTHER) + return "SCHED_OTHER"; + if (pol == SCHED_FIFO) + return "SCHED_FIFO"; + if (pol == SCHED_RR) + return "SCHED_RR"; + if (pol == SCHED_BATCH) + return "SCHED_BATCH"; + if (pol == SCHED_IDLE) + return "SCHED_IDLE"; + return ""; +} static void show_min_max(int pol) { @@ -57,7 +60,7 @@ static void show_min_max(int pol) min = sched_get_priority_min(pol); if ((max|min) < 0) fmt = "%s not supported\n"; - printf(fmt, policies[pol].name, min, max); + printf(fmt, get_policy_name(pol), min, max); } #define OPT_m (1<<0) @@ -127,7 +130,7 @@ int chrt_main(int argc UNUSED_PARAM, char **argv) if (pol < 0) bb_perror_msg_and_die("can't %cet pid %d's policy", 'g', (int)pid); printf("pid %d's %s scheduling policy: %s\n", - pid, current_new, policies[pol].name); + pid, current_new, get_policy_name(pol)); if (sched_getparam(pid, &sp)) bb_perror_msg_and_die("can't get pid %d's attributes", (int)pid); printf("pid %d's %s scheduling priority: %d\n", -- 2.14.1 ___ busybox mailing list busybox@busybox.net http://lists.busybox.net/mailman/listinfo/busybox
[PATCH 0/4] Add SCHED_BATCH and SCHED_IDLE support to chrt
Hi, The following patches add SCHED_BATCH and SCHED_IDLE support to chrt. The priority limits are fixed to follow the specification. The last patch avoids hardcoding the values of SCHED_* macros as array indices. Perhaps counter-intuitively, this leads to binary size reduction of 90 bytes on x86-64. However the patch series as a whole still increase the binary size by 173 bytes on x86-64. Regards, Povilas ___ busybox mailing list busybox@busybox.net http://lists.busybox.net/mailman/listinfo/busybox
[PATCH 1/4] chrt: add support for SCHED_BATCH
--- util-linux/chrt.c | 16 +++- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/util-linux/chrt.c b/util-linux/chrt.c index 2712ea3e3..e7abd4f5a 100644 --- a/util-linux/chrt.c +++ b/util-linux/chrt.c @@ -17,13 +17,14 @@ //kbuild:lib-$(CONFIG_CHRT) += chrt.o //usage:#define chrt_trivial_usage -//usage: "[-prfom] [PRIO] [PID | PROG ARGS]" +//usage: "[-prfomb] [PRIO] [PID | PROG ARGS]" //usage:#define chrt_full_usage "\n\n" //usage: "Change scheduling priority and class for a process\n" //usage: "\n -p Operate on PID" //usage: "\n -r Set SCHED_RR class" //usage: "\n -f Set SCHED_FIFO class" //usage: "\n -o Set SCHED_OTHER class" +//usage: "\n -b Set SCHED_BATCH class" //usage: "\n -m Show min/max priorities" //usage: //usage:#define chrt_example_usage @@ -40,11 +41,11 @@ static const struct { } policies[] = { {SCHED_OTHER, "SCHED_OTHER"}, {SCHED_FIFO, "SCHED_FIFO"}, - {SCHED_RR, "SCHED_RR"} + {SCHED_RR, "SCHED_RR"}, + {SCHED_BATCH, "SCHED_BATCH"} }; //TODO: add -// -b, SCHED_BATCH // -i, SCHED_IDLE static void show_min_max(int pol) @@ -64,6 +65,7 @@ static void show_min_max(int pol) #define OPT_r (1<<2) #define OPT_f (1<<3) #define OPT_o (1<<4) +#define OPT_b (1<<5) int chrt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int chrt_main(int argc UNUSED_PARAM, char **argv) @@ -77,11 +79,12 @@ int chrt_main(int argc UNUSED_PARAM, char **argv) int policy = SCHED_RR; /* only one policy accepted */ - opt = getopt32(argv, "^+" "mprfo" "\0" "r--fo:f--ro:o--rf"); + opt = getopt32(argv, "^+" "mprfob" "\0" "r--fob:f--rob:o--rfb:b--rfo"); if (opt & OPT_m) { /* print min/max and exit */ show_min_max(SCHED_FIFO); show_min_max(SCHED_RR); show_min_max(SCHED_OTHER); + show_min_max(SCHED_BATCH); fflush_stdout_and_exit(EXIT_SUCCESS); } if (opt & OPT_r) @@ -90,6 +93,8 @@ int chrt_main(int argc UNUSED_PARAM, char **argv) policy = SCHED_FIFO; if (opt & OPT_o) policy = SCHED_OTHER; + if (opt & OPT_b) + policy = SCHED_BATCH; argv += optind; if (!argv[0]) @@ -136,7 +141,8 @@ int chrt_main(int argc UNUSED_PARAM, char **argv) [...] SCHED_OTHER or SCHED_BATCH must be assigned static priority 0. [...] SCHED_FIFO or SCHED_RR can have static priority in 1..99 range. */ - sp.sched_priority = xstrtou_range(priority, 0, policy != SCHED_OTHER ? 1 : 0, 99); + sp.sched_priority = xstrtou_range(priority, 0, + (policy != SCHED_OTHER && policy != SCHED_BATCH) ? 1 : 0, 99); if (sched_setscheduler(pid, policy, &sp) < 0) bb_perror_msg_and_die("can't %cet pid %d's policy", 's', (int)pid); -- 2.14.1 ___ busybox mailing list busybox@busybox.net http://lists.busybox.net/mailman/listinfo/busybox
[PATCH 2/4] chrt: add support for SCHED_IDLE
--- util-linux/chrt.c | 21 + 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/util-linux/chrt.c b/util-linux/chrt.c index e7abd4f5a..bbd6e2deb 100644 --- a/util-linux/chrt.c +++ b/util-linux/chrt.c @@ -17,7 +17,7 @@ //kbuild:lib-$(CONFIG_CHRT) += chrt.o //usage:#define chrt_trivial_usage -//usage: "[-prfomb] [PRIO] [PID | PROG ARGS]" +//usage: "[-prfombi] [PRIO] [PID | PROG ARGS]" //usage:#define chrt_full_usage "\n\n" //usage: "Change scheduling priority and class for a process\n" //usage: "\n -p Operate on PID" @@ -25,6 +25,7 @@ //usage: "\n -f Set SCHED_FIFO class" //usage: "\n -o Set SCHED_OTHER class" //usage: "\n -b Set SCHED_BATCH class" +//usage: "\n -i Set SCHED_IDLE class" //usage: "\n -m Show min/max priorities" //usage: //usage:#define chrt_example_usage @@ -42,12 +43,11 @@ static const struct { {SCHED_OTHER, "SCHED_OTHER"}, {SCHED_FIFO, "SCHED_FIFO"}, {SCHED_RR, "SCHED_RR"}, - {SCHED_BATCH, "SCHED_BATCH"} + {SCHED_BATCH, "SCHED_BATCH"}, + {0 /* unused */, ""}, + {SCHED_IDLE, "SCHED_IDLE"} }; -//TODO: add -// -i, SCHED_IDLE - static void show_min_max(int pol) { const char *fmt = "%s min/max priority\t: %u/%u\n"; @@ -66,6 +66,7 @@ static void show_min_max(int pol) #define OPT_f (1<<3) #define OPT_o (1<<4) #define OPT_b (1<<5) +#define OPT_i (1<<6) int chrt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int chrt_main(int argc UNUSED_PARAM, char **argv) @@ -79,12 +80,13 @@ int chrt_main(int argc UNUSED_PARAM, char **argv) int policy = SCHED_RR; /* only one policy accepted */ - opt = getopt32(argv, "^+" "mprfob" "\0" "r--fob:f--rob:o--rfb:b--rfo"); + opt = getopt32(argv, "^+" "mprfobi" "\0" "r--fobi:f--robi:o--rfbi:b--rfoi:i--rfob"); if (opt & OPT_m) { /* print min/max and exit */ show_min_max(SCHED_FIFO); show_min_max(SCHED_RR); show_min_max(SCHED_OTHER); show_min_max(SCHED_BATCH); + show_min_max(SCHED_IDLE); fflush_stdout_and_exit(EXIT_SUCCESS); } if (opt & OPT_r) @@ -95,6 +97,8 @@ int chrt_main(int argc UNUSED_PARAM, char **argv) policy = SCHED_OTHER; if (opt & OPT_b) policy = SCHED_BATCH; + if (opt & OPT_i) + policy = SCHED_IDLE; argv += optind; if (!argv[0]) @@ -138,11 +142,12 @@ int chrt_main(int argc UNUSED_PARAM, char **argv) /* from the manpage of sched_getscheduler: [...] sched_priority can have a value in the range 0 to 99. - [...] SCHED_OTHER or SCHED_BATCH must be assigned static priority 0. + [...] SCHED_OTHER, SCHED_BATCH or SCHED_IDLE must be assigned static + priority 0. [...] SCHED_FIFO or SCHED_RR can have static priority in 1..99 range. */ sp.sched_priority = xstrtou_range(priority, 0, - (policy != SCHED_OTHER && policy != SCHED_BATCH) ? 1 : 0, 99); + (policy != SCHED_OTHER && policy != SCHED_BATCH && policy != SCHED_IDLE) ? 1 : 0, 99); if (sched_setscheduler(pid, policy, &sp) < 0) bb_perror_msg_and_die("can't %cet pid %d's policy", 's', (int)pid); -- 2.14.1 ___ busybox mailing list busybox@busybox.net http://lists.busybox.net/mailman/listinfo/busybox
[PATCH 3/4] chrt: fix incorrect maximum priority on SCHED_{OTHER,BATCH,IDLE}
--- util-linux/chrt.c | 14 -- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/util-linux/chrt.c b/util-linux/chrt.c index bbd6e2deb..5c5513aeb 100644 --- a/util-linux/chrt.c +++ b/util-linux/chrt.c @@ -78,6 +78,8 @@ int chrt_main(int argc UNUSED_PARAM, char **argv) char *priority = priority; /* for compiler */ const char *current_new; int policy = SCHED_RR; + int priority_min; + int priority_max; /* only one policy accepted */ opt = getopt32(argv, "^+" "mprfobi" "\0" "r--fobi:f--robi:o--rfbi:b--rfoi:i--rfob"); @@ -146,8 +148,16 @@ int chrt_main(int argc UNUSED_PARAM, char **argv) priority 0. [...] SCHED_FIFO or SCHED_RR can have static priority in 1..99 range. */ - sp.sched_priority = xstrtou_range(priority, 0, - (policy != SCHED_OTHER && policy != SCHED_BATCH && policy != SCHED_IDLE) ? 1 : 0, 99); + if (policy == SCHED_OTHER || policy == SCHED_BATCH +|| policy == SCHED_IDLE + ) { + priority_min = 0; + priority_max = 0; + } else { + priority_min = 1; + priority_max = 99; + } + sp.sched_priority = xstrtou_range(priority, 0, priority_min, priority_max); if (sched_setscheduler(pid, policy, &sp) < 0) bb_perror_msg_and_die("can't %cet pid %d's policy", 's', (int)pid); -- 2.14.1 ___ busybox mailing list busybox@busybox.net http://lists.busybox.net/mailman/listinfo/busybox
Re: [PATCH 4/4] chrt: don't rely on exact values of SCHED_* defines
On Sun, Jan 14, 2018 at 5:30 AM, Povilas Kanapickas wrote: > --- a/util-linux/chrt.c > +++ b/util-linux/chrt.c > @@ -36,17 +36,20 @@ > #include > #include "libbb.h" > > -static const struct { > - int policy; > - char name[sizeof("SCHED_OTHER")]; > -} policies[] = { > - {SCHED_OTHER, "SCHED_OTHER"}, > - {SCHED_FIFO, "SCHED_FIFO"}, > - {SCHED_RR, "SCHED_RR"}, > - {SCHED_BATCH, "SCHED_BATCH"}, > - {0 /* unused */, ""}, > - {SCHED_IDLE, "SCHED_IDLE"} > -}; > +static const char* get_policy_name(int pol) > +{ > + if (pol == SCHED_OTHER) > + return "SCHED_OTHER"; > + if (pol == SCHED_FIFO) > + return "SCHED_FIFO"; > + if (pol == SCHED_RR) > + return "SCHED_RR"; > + if (pol == SCHED_BATCH) > + return "SCHED_BATCH"; > + if (pol == SCHED_IDLE) > + return "SCHED_IDLE"; > + return ""; > +} > > static void show_min_max(int pol) > { I don't like the chain of "if"s here. Have you checked the size changes on the compiled code? Since this SCHED policy constants are just enum values, what about using "switch-case" statement? The problem of if-chains is that some compilers are not smart at figuring out that the comparisons are just enums and can be optimized. The switch-case statement will make it more clear. Sorry for nit-picking the code. ___ busybox mailing list busybox@busybox.net http://lists.busybox.net/mailman/listinfo/busybox