From: Davidlohr Bueso <dave@xxxxxxx> Date: Mon, 2 May 2011 21:59:25 -0300 Currently this program works only with the master thread. Add a '-t' option to propagate changes to the entire group of threads. Example: root@offworld:~/projects/util-lin/schedutils# ls /proc/2015/task/ 2015 2017 2116 2121 2149 2150 2178 2179 root@offworld:~/projects/util-lin/schedutils# ./chrt -p 2015 pid 2015's current scheduling policy: SCHED_RR pid 2015's current scheduling priority: 1 root@offworld:~/projects/util-lin/schedutils# ./chrt -t -p 2 2015 root@offworld:~/projects/util-lin/schedutils# ./chrt -p 2015 pid 2015's current scheduling policy: SCHED_RR pid 2015's current scheduling priority: 2 root@offworld:~/projects/util-lin/schedutils# ./chrt -p 2179 pid 2179's current scheduling policy: SCHED_RR pid 2179's current scheduling priority: 2 root@offworld:~/projects/util-lin/schedutils# ./chrt -p 2149 pid 2149's current scheduling policy: SCHED_RR pid 2149's current scheduling priority: 2 Signed-off-by: Davidlohr Bueso <dave@xxxxxxx> --- Similar patches on the way for other schedutil programs. schedutils/Makefile.am | 2 +- schedutils/chrt.1 | 3 +++ schedutils/chrt.c | 29 +++++++++++++++++++++++++---- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/schedutils/Makefile.am b/schedutils/Makefile.am index dc33175..91dc905 100644 --- a/schedutils/Makefile.am +++ b/schedutils/Makefile.am @@ -2,7 +2,7 @@ include $(top_srcdir)/config/include-Makefile.am if BUILD_SCHEDUTILS -srcs_common = $(top_srcdir)/lib/strutils.c +srcs_common = $(top_srcdir)/lib/strutils.c $(top_srcdir)/lib/procutils.c usrbin_exec_PROGRAMS = chrt dist_man_MANS = chrt.1 diff --git a/schedutils/chrt.1 b/schedutils/chrt.1 index e22d69d..131d457 100644 --- a/schedutils/chrt.1 +++ b/schedutils/chrt.1 @@ -65,6 +65,9 @@ since Linux 2.6.31. .B -p, --pid operate on an existing PID and do not launch a new task .TP +.B -t, --thread +propagate changes to all the threads for a given PID +.TP .B -b, --batch set scheduling policy to .BR SCHED_BATCH diff --git a/schedutils/chrt.c b/schedutils/chrt.c index ce5d2ab..d68f087 100644 --- a/schedutils/chrt.c +++ b/schedutils/chrt.c @@ -5,6 +5,8 @@ * Robert Love <rml@xxxxxxxxx> * 27-Apr-2002: initial version * + * 02-May-2011: make thread aware - Davidlohr Bueso <dave@xxxxxxx> + * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License, v2, as * published by the Free Software Foundation @@ -32,6 +34,7 @@ #include "nls.h" #include "strutils.h" +#include "procutils.h" /* the SCHED_BATCH is supported since Linux 2.6.16 * -- temporary workaround for people with old glibc headers @@ -82,6 +85,7 @@ static void __attribute__((__noreturn__)) show_usage(int rc) " -h | --help display this help\n" " -m | --max show min and max valid priorities\n" " -p | --pid operate on existing given pid\n" + " -t | --thread propagate change to all threads for a given pid\n" " -v | --verbose display status information\n" " -V | --version output version information\n\n")); @@ -185,7 +189,7 @@ static void show_min_max(void) int main(int argc, char *argv[]) { - int i, policy = SCHED_RR, priority = 0, verbose = 0, policy_flag = 0; + int i, policy = SCHED_RR, priority = 0, verbose = 0, policy_flag = 0, inc_threads = 0; struct sched_param sp; pid_t pid = -1; @@ -194,6 +198,7 @@ int main(int argc, char *argv[]) { "fifo", 0, NULL, 'f' }, { "idle", 0, NULL, 'i' }, { "pid", 0, NULL, 'p' }, + { "thread", 0, NULL, 't' }, { "help", 0, NULL, 'h' }, { "max", 0, NULL, 'm' }, { "other", 0, NULL, 'o' }, @@ -208,7 +213,7 @@ int main(int argc, char *argv[]) bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); - while((i = getopt_long(argc, argv, "+bfiphmoRrvV", longopts, NULL)) != -1) + while((i = getopt_long(argc, argv, "+bfipthmoRrvV", longopts, NULL)) != -1) { int ret = EXIT_FAILURE; @@ -241,6 +246,9 @@ int main(int argc, char *argv[]) errno = 0; pid = strtol_or_err(argv[argc - 1], _("failed to parse pid")); break; + case 't': + inc_threads = 1; + break; case 'r': policy = SCHED_RR; break; @@ -282,8 +290,21 @@ int main(int argc, char *argv[]) if (pid == -1) pid = 0; sp.sched_priority = priority; - if (sched_setscheduler(pid, policy, &sp) == -1) - err(EXIT_FAILURE, _("failed to set pid %d's policy"), pid); + if (inc_threads) { + i = 0; + pid_t *threads = proc_getthreads(pid); + if (!threads) + err(EXIT_FAILURE, _("failed to get threads for pid %d"), pid); + while (threads[i]) { + if (sched_setscheduler(threads[i], policy, &sp) == -1) + err(EXIT_FAILURE, _("failed to set pid %d's policy"), threads[i]); + i++; + } + free(threads); + } + else + if (sched_setscheduler(pid, policy, &sp) == -1) + err(EXIT_FAILURE, _("failed to set pid %d's policy"), pid); if (verbose) show_rt_info(pid, TRUE); -- 1.7.1 -- To unsubscribe from this list: send the line "unsubscribe util-linux" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html