]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
chrt: make threads aware
authorDavidlohr Bueso <dave@gnu.org>
Thu, 5 May 2011 12:02:34 +0000 (14:02 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 5 May 2011 12:02:34 +0000 (14:02 +0200)
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-linux/schedutils# ls /proc/2111/task/
2111  2112  2119  2121  2138  2139  2159  2160
root@offworld:~/projects/util-linux/schedutils# ./chrt -p 2111
pid 2111's current scheduling policy: SCHED_RR
pid 2111's current scheduling priority: 3
root@offworld:~/projects/util-linux/schedutils# ./chrt -t -p 2 2111
root@offworld:~/projects/util-linux/schedutils# ./chrt -p 2112
pid 2112's current scheduling policy: SCHED_RR
pid 2112's current scheduling priority: 2
root@offworld:~/projects/util-linux/schedutils# ./chrt -p 2111
pid 2111's current scheduling policy: SCHED_RR
pid 2111's current scheduling priority: 2

[kzak@redhat.com: - rename -t/--thread to -a/--all-tasks]

Signed-off-by: Davidlohr Bueso <dave@gnu.org>
Signed-off-by: Karel Zak <kzak@redhat.com>
schedutils/Makefile.am
schedutils/chrt.1
schedutils/chrt.c

index dc33175376f688611b8432a70a5344ff9479e2da..91dc9054dcca54bdff23cb2f2c52f2cea6eab49c 100644 (file)
@@ -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
index e22d69d53ef32d9531fb0e31a77c2df183b6b977..c5d87f97d9571c1a3feb48a301937ee3d3d1dbb3 100644 (file)
@@ -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 -a, --all-tasks
+propagate changes to all the tasks (threads) for a given PID.
+.TP
 .B -b, --batch
 set scheduling policy to
 .BR SCHED_BATCH
index ce5d2ab71a81bf89c641d71a169291ed18d26df8..3669ba6a8591804024322395603c44cccdde673e 100644 (file)
@@ -4,6 +4,7 @@
  *
  * Robert Love <rml@tech9.net>
  * 27-Apr-2002: initial version
+ * 04-May-2011: make thread aware - Davidlohr Bueso <dave@gnu.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License, v2, as
@@ -32,6 +33,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
@@ -79,6 +81,7 @@ static void __attribute__((__noreturn__)) show_usage(int rc)
 #endif
        fprintf(out, _(
        "\nOptions:\n"
+       "  -a | --all-tasks     propagate changes to all the tasks for a given pid\n"
        "  -h | --help          display this help\n"
        "  -m | --max           show min and max valid priorities\n"
        "  -p | --pid           operate on existing given pid\n"
@@ -185,11 +188,12 @@ 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, all_tasks = 0;
        struct sched_param sp;
        pid_t pid = -1;
 
        static const struct option longopts[] = {
+               { "all-tasks",  0, NULL, 'a' },
                { "batch",      0, NULL, 'b' },
                { "fifo",       0, NULL, 'f' },
                { "idle",       0, NULL, 'i' },
@@ -208,11 +212,14 @@ 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, "+abfiphmoRrvV", longopts, NULL)) != -1)
        {
                int ret = EXIT_FAILURE;
 
                switch (i) {
+               case 'a':
+                       all_tasks = 1;
+                       break;
                case 'b':
 #ifdef SCHED_BATCH
                        policy = SCHED_BATCH;
@@ -282,8 +289,23 @@ 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 (all_tasks) {
+               pid_t tid;
+               struct proc_tasks *ts = proc_open_tasks(pid);
+
+               if (!ts)
+                       err(EXIT_FAILURE, "cannot obtain the list of tasks");
+
+               while (!proc_next_tid(ts, &tid))
+                       if (sched_setscheduler(tid, policy, &sp) == -1)
+                               err(EXIT_FAILURE, _("failed to set tid %d's policy"), tid);
+
+               proc_close_tasks(ts);
+       }
+       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);