[PATCH] chrt: make threads aware

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Davidlohr Bueso <dave@xxxxxxx>
Date: Wed, 4 May 2011 15:14:35 -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-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

Signed-off-by: Davidlohr Bueso <dave@xxxxxxx>
---
 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..f024b83 100644
--- a/schedutils/chrt.c
+++ b/schedutils/chrt.c
@@ -4,6 +4,7 @@
  *
  * Robert Love <rml@xxxxxxxxx>
  * 27-Apr-2002: initial version
+ * 04-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
@@ -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
@@ -82,6 +84,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 changes to all the thread for a given pid\n"
 	"  -v | --verbose       display status information\n"
 	"  -V | --version       output version information\n\n"));
 
@@ -185,7 +188,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 +197,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 +212,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 +245,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 +289,22 @@ 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) {
+		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 pid %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);
-- 
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


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux