dcavalca / rpms / util-linux

Forked from rpms/util-linux 2 years ago
Clone
05ad79
From 674769893bb8d5f1991c6a3e5d96337b37aeb86f Mon Sep 17 00:00:00 2001
05ad79
From: Karel Zak <kzak@redhat.com>
05ad79
Date: Mon, 27 Jun 2016 14:14:28 +0200
05ad79
Subject: [PATCH 81/86] chrt: backport DEADLINE scheduler support
05ad79
05ad79
Backport upstream commits:
05ad79
05ad79
2e31d1c chrt: validate priority before trying to use it
05ad79
b3a5067 chrt: make --sched-* short options to require an argument
05ad79
a03eac5 chrt: restore removed ifdef SCHED_RESET_ON_FORK
05ad79
59e4a38 chrt: fix case SCHED_RR
05ad79
acde3a0 chrt: use sched_getattr()
05ad79
1a7e639 chrt: add support for SCHED_DEADLINE
05ad79
1516758 chrt: use sched_setattr() if available
05ad79
a6fec53 chrt: make usage more readable
05ad79
4820a73 chrt: set function refactoring
05ad79
a30cf65 chrt: output function refactoring
05ad79
7a4ea56 chrt: add control struct
05ad79
9acbe2a chrt: slice up the usage text and normalize its layout
05ad79
4e4bc0c chrt: make the usage synopsis clearer
05ad79
3fabc36 chrt: fix --help inconsistency
05ad79
451dbcf textual: add a docstring to most of the utilities
05ad79
a587cc5 textual: use manual tail usage() macro
05ad79
f627750 textual: use version printing macro everywhere
05ad79
a7560c0 textual: make the license of chrt and taskset slightly more explicit
05ad79
4ce393f textual: fix several typos and angular brackets in messages
05ad79
6f27e44 chrt: add fallback to be usable on kernels without sched_{get,set}attr
05ad79
05ad79
* Fri Jul 01 2016 re-spin [kzak]:
05ad79
- add fallback for old glibc-headers without SYS_sched_{set,get}attr
05ad79
05ad79
* Tue Jul 12 016 re-spin [kzak]:
05ad79
- add runtime fallback for systems without sched_{get,set}attr syscalls
05ad79
05ad79
Addresses: http://bugzilla.redhat.com/show_bug.cgi?id=1298384
05ad79
Signed-off-by: Karel Zak <kzak@redhat.com>
05ad79
---
05ad79
 configure.ac      |  17 +-
05ad79
 m4/ul.m4          |   8 -
05ad79
 schedutils/chrt.c | 547 ++++++++++++++++++++++++++++++++++++++----------------
05ad79
 3 files changed, 400 insertions(+), 172 deletions(-)
05ad79
05ad79
diff --git a/configure.ac b/configure.ac
05ad79
index fe0a011..266ef08 100644
05ad79
--- a/configure.ac
05ad79
+++ b/configure.ac
05ad79
@@ -338,6 +338,8 @@ AC_CHECK_FUNCS([ \
05ad79
 	scandirat \
05ad79
 	setresgid \
05ad79
 	setresuid \
05ad79
+	sched_setattr \
05ad79
+	sched_setscheduler \
05ad79
 	sigqueue \
05ad79
 	srandom \
05ad79
 	strnchr \
05ad79
@@ -1383,6 +1385,20 @@ UL_REQUIRES_SYSCALL_CHECK([taskset],
05ad79
 AM_CONDITIONAL(BUILD_TASKSET, test "x$build_taskset" = xyes)
05ad79
 
05ad79
 
05ad79
+have_schedsetter=no
05ad79
+AS_IF([test "x$ac_cv_func_sched_setscheduler" = xyes], [have_schedsetter=yes],
05ad79
+      [test "x$ac_cv_func_sched_setattr" = xyes], [have_schedsetter=yes])
05ad79
+
05ad79
+UL_BUILD_INIT([chrt], [check])
05ad79
+UL_REQUIRES_BUILD([chrt], [schedutils])
05ad79
+UL_REQUIRES_HAVE([chrt], [schedsetter], [sched_set functions])
05ad79
+AM_CONDITIONAL([BUILD_CHRT], [test "x$build_chrt" = xyes])
05ad79
+
05ad79
+AS_IF([test "x$build_chrt" = xyes], [
05ad79
+	UL_CHECK_SYSCALL([sched_setattr])
05ad79
+])
05ad79
+
05ad79
+
05ad79
 AC_ARG_ENABLE([wall],
05ad79
   AS_HELP_STRING([--disable-wall], [do not build wall]),
05ad79
   [], enable_wall=yes
05ad79
@@ -1562,7 +1578,6 @@ AC_ARG_VAR([SOLIB_LDFLAGS],
05ad79
 
05ad79
 LIBS=""
05ad79
 
05ad79
-
05ad79
 AC_CONFIG_HEADERS(config.h)
05ad79
 
05ad79
 #
05ad79
diff --git a/m4/ul.m4 b/m4/ul.m4
05ad79
index c0082d0..db44589 100644
05ad79
--- a/m4/ul.m4
05ad79
+++ b/m4/ul.m4
05ad79
@@ -92,7 +92,6 @@ AC_DEFUN([UL_CHECK_SYSCALL], [
05ad79
       ])
05ad79
     ul_cv_syscall_$1=$syscall
05ad79
     ])
05ad79
-  AM_CONDITIONAL([HAVE_]m4_toupper($1), [test "x$ul_cv_syscall_$1" != xno])
05ad79
   case $ul_cv_syscall_$1 in #(
05ad79
   no) AC_MSG_WARN([Unable to detect syscall $1.]) ;;
05ad79
   SYS_*) ;;
05ad79
@@ -266,13 +265,6 @@ AC_DEFUN([UL_REQUIRES_SYSCALL_CHECK], [
05ad79
   m4_define([suffix], m4_default([$4],$1))
05ad79
   m4_define([callname], m4_default([$3],$1))
05ad79
 
05ad79
-  dnl This is default, $3 will redefine the condition
05ad79
-  dnl
05ad79
-  dnl TODO: remove this junk, AM_CONDITIONAL should not be used for any HAVE_*
05ad79
-  dnl       variables, all we need is BUILD_* only.
05ad79
-  dnl
05ad79
-  AM_CONDITIONAL([HAVE_]m4_toupper(callname), [false])
05ad79
-
05ad79
   if test "x$[build_]suffix" != xno; then
05ad79
     if test "x$[enable_]suffix" = xno; then
05ad79
       [build_]suffix=no
05ad79
diff --git a/schedutils/chrt.c b/schedutils/chrt.c
05ad79
index 20df6fa..edae0d9 100644
05ad79
--- a/schedutils/chrt.c
05ad79
+++ b/schedutils/chrt.c
05ad79
@@ -1,13 +1,11 @@
05ad79
 /*
05ad79
- * chrt.c - chrt
05ad79
- * Command-line utility for manipulating a task's real-time attributes 
05ad79
+ * chrt.c - manipulate a task's real-time attributes
05ad79
  *
05ad79
- * Robert Love <rml@tech9.net>
05ad79
- * 27-Apr-2002: initial version
05ad79
- * 04-May-2011: make thread aware - Davidlohr Bueso <dave@gnu.org>
05ad79
+ * 27-Apr-2002: initial version - Robert Love <rml@tech9.net>
05ad79
+ * 04-May-2011: make it thread-aware - Davidlohr Bueso <dave@gnu.org>
05ad79
  *
05ad79
  * This program is free software; you can redistribute it and/or modify
05ad79
- * it under the terms of the GNU General Public License, v2, as
05ad79
+ * it under the terms of the GNU General Public License, version 2, as
05ad79
  * published by the Free Software Foundation
05ad79
  *
05ad79
  * This program is distributed in the hope that it will be useful,
05ad79
@@ -50,108 +48,260 @@
05ad79
 # define SCHED_IDLE 5
05ad79
 #endif
05ad79
 
05ad79
+/* flag by sched_getscheduler() */
05ad79
 #if defined(__linux__) && !defined(SCHED_RESET_ON_FORK)
05ad79
-#define SCHED_RESET_ON_FORK 0x40000000
05ad79
+# define SCHED_RESET_ON_FORK 0x40000000
05ad79
 #endif
05ad79
 
05ad79
+/* flag by sched_getattr() */
05ad79
+#if defined(__linux__) && !defined(SCHED_FLAG_RESET_ON_FORK)
05ad79
+# define SCHED_FLAG_RESET_ON_FORK 0x01
05ad79
+#endif
05ad79
 
05ad79
-static void __attribute__((__noreturn__)) show_usage(int rc)
05ad79
-{
05ad79
-	FILE *out = rc == EXIT_SUCCESS ? stdout : stderr;
05ad79
+#if defined (__linux__) && !defined(HAVE_SCHED_SETATTR)
05ad79
+# include <sys/syscall.h>
05ad79
+#endif
05ad79
 
05ad79
-	fprintf(out, _(
05ad79
-	"\nchrt - manipulate real-time attributes of a process\n"
05ad79
-	"\nSet policy:\n"
05ad79
-	"  chrt [options] [-policy] <priority> [-p <pid> | <command> <arg> ...]\n"
05ad79
-	"\nGet policy:\n"
05ad79
-	"  chrt [options] -p <pid>\n"));
05ad79
-
05ad79
-	fprintf(out, _(
05ad79
-	"\nScheduling policies:\n"
05ad79
-	"  -b | --batch         set policy to SCHED_BATCH\n"
05ad79
-	"  -f | --fifo          set policy to SCHED_FIFO\n"
05ad79
-	"  -i | --idle          set policy to SCHED_IDLE\n"
05ad79
-	"  -o | --other         set policy to SCHED_OTHER\n"
05ad79
-	"  -r | --rr            set policy to SCHED_RR (default)\n"));
05ad79
+/* usable kernel-headers, but old glibc-headers */
05ad79
+#if defined (__linux__) && !defined(SYS_sched_setattr) && defined(__NR_sched_setattr)
05ad79
+# define SYS_sched_setattr __NR_sched_setattr
05ad79
+#endif
05ad79
 
05ad79
-#ifdef SCHED_RESET_ON_FORK
05ad79
-	fprintf(out, _(
05ad79
-	"\nScheduling flags:\n"
05ad79
-	"  -R | --reset-on-fork set SCHED_RESET_ON_FORK for FIFO or RR\n"));
05ad79
+#if defined (__linux__) && !defined(SYS_sched_getattr) && defined(__NR_sched_getattr)
05ad79
+# define SYS_sched_getattr __NR_sched_getattr
05ad79
 #endif
05ad79
-	fprintf(out, _(
05ad79
-	"\nOptions:\n"
05ad79
-	"  -a | --all-tasks     operate on all the tasks (threads) for a given pid\n"
05ad79
-	"  -h | --help          display this help\n"
05ad79
-	"  -m | --max           show min and max valid priorities\n"
05ad79
-	"  -p | --pid           operate on existing given pid\n"
05ad79
-	"  -v | --verbose       display status information\n"
05ad79
-	"  -V | --version       output version information\n\n"));
05ad79
 
05ad79
-	exit(rc);
05ad79
+#if defined (__linux__) && !defined(HAVE_SCHED_SETATTR) && defined(SYS_sched_setattr)
05ad79
+# define HAVE_SCHED_SETATTR
05ad79
+
05ad79
+struct sched_attr {
05ad79
+	uint32_t size;
05ad79
+	uint32_t sched_policy;
05ad79
+	uint64_t sched_flags;
05ad79
+
05ad79
+	/* SCHED_NORMAL, SCHED_BATCH */
05ad79
+	int32_t sched_nice;
05ad79
+
05ad79
+	/* SCHED_FIFO, SCHED_RR */
05ad79
+	uint32_t sched_priority;
05ad79
+
05ad79
+	/* SCHED_DEADLINE (nsec) */
05ad79
+	uint64_t sched_runtime;
05ad79
+	uint64_t sched_deadline;
05ad79
+	uint64_t sched_period;
05ad79
+};
05ad79
+
05ad79
+static int sched_setattr(pid_t pid, const struct sched_attr *attr, unsigned int flags)
05ad79
+{
05ad79
+	return syscall(SYS_sched_setattr, pid, attr, flags);
05ad79
 }
05ad79
 
05ad79
-static void show_rt_info(pid_t pid, int isnew)
05ad79
+static int sched_getattr(pid_t pid, struct sched_attr *attr, unsigned int size, unsigned int flags)
05ad79
 {
05ad79
-	struct sched_param sp;
05ad79
-	int policy;
05ad79
+	return syscall(SYS_sched_getattr, pid, attr, size, flags);
05ad79
+}
05ad79
+#endif
05ad79
 
05ad79
-	/* don't display "pid 0" as that is confusing */
05ad79
-	if (!pid)
05ad79
-		pid = getpid();
05ad79
+/* the SCHED_DEADLINE is supported since Linux 3.14
05ad79
+ * commit id aab03e05e8f7e26f51dee792beddcb5cca9215a5
05ad79
+ * -- sched_setattr() is required for this policy!
05ad79
+ */
05ad79
+#if defined (__linux__) && !defined(SCHED_DEADLINE) && defined(HAVE_SCHED_SETATTR)
05ad79
+# define SCHED_DEADLINE 6
05ad79
+#endif
05ad79
 
05ad79
-	policy = sched_getscheduler(pid);
05ad79
-	if (policy == -1)
05ad79
-		err(EXIT_FAILURE, _("failed to get pid %d's policy"), pid);
05ad79
+/* control struct */
05ad79
+struct chrt_ctl {
05ad79
+	pid_t	pid;
05ad79
+	int	policy;				/* SCHED_* */
05ad79
+	int	priority;
05ad79
 
05ad79
-	if (isnew)
05ad79
-		printf(_("pid %d's new scheduling policy: "), pid);
05ad79
-	else
05ad79
-		printf(_("pid %d's current scheduling policy: "), pid);
05ad79
+	uint64_t runtime;			/* --sched-* options */
05ad79
+	uint64_t deadline;
05ad79
+	uint64_t period;
05ad79
+
05ad79
+	unsigned int all_tasks : 1,		/* all threads of the PID */
05ad79
+		     reset_on_fork : 1,		/* SCHED_RESET_ON_FORK */
05ad79
+		     altered : 1,		/* sched_set**() used */
05ad79
+		     verbose : 1;		/* verbose output */
05ad79
+};
05ad79
+
05ad79
+static void __attribute__((__noreturn__)) show_usage(int rc)
05ad79
+{
05ad79
+	FILE *out = rc == EXIT_SUCCESS ? stdout : stderr;
05ad79
 
05ad79
+	fputs(_("Show or change the real-time scheduling attributes of a process.\n"), out);
05ad79
+	fputs(USAGE_SEPARATOR, out);
05ad79
+	fputs(_("Set policy:\n"
05ad79
+	" chrt [options] <priority> <command> [<arg>...]\n"
05ad79
+	" chrt [options] --pid <priority> <pid>\n"), out);
05ad79
+	fputs(USAGE_SEPARATOR, out);
05ad79
+	fputs(_("Get policy:\n"
05ad79
+	" chrt [options] -p <pid>\n"), out);
05ad79
+
05ad79
+	fputs(USAGE_SEPARATOR, out);
05ad79
+	fputs(_("Policy options:\n"), out);
05ad79
+	fputs(_(" -b, --batch          set policy to SCHED_BATCH\n"), out);
05ad79
+	fputs(_(" -d, --deadline       set policy to SCHED_DEADLINE\n"), out);
05ad79
+	fputs(_(" -f, --fifo           set policy to SCHED_FIFO\n"), out);
05ad79
+	fputs(_(" -i, --idle           set policy to SCHED_IDLE\n"), out);
05ad79
+	fputs(_(" -o, --other          set policy to SCHED_OTHER\n"), out);
05ad79
+	fputs(_(" -r, --rr             set policy to SCHED_RR (default)\n"), out);
05ad79
+
05ad79
+	fputs(USAGE_SEPARATOR, out);
05ad79
+	fputs(_("Scheduling options:\n"), out);
05ad79
+	fputs(_(" -R, --reset-on-fork       set SCHED_RESET_ON_FORK for FIFO or RR\n"), out);
05ad79
+	fputs(_(" -T, --sched-runtime <ns>  runtime parameter for DEADLINE\n"), out);
05ad79
+	fputs(_(" -P, --sched-period <ns>   period parameter for DEADLINE\n"), out);
05ad79
+	fputs(_(" -D, --sched-deadline <ns> deadline parameter for DEADLINE\n"), out);
05ad79
+
05ad79
+	fputs(USAGE_SEPARATOR, out);
05ad79
+	fputs(_("Other options:\n"), out);
05ad79
+	fputs(_(" -a, --all-tasks      operate on all the tasks (threads) for a given pid\n"), out);
05ad79
+	fputs(_(" -m, --max            show min and max valid priorities\n"), out);
05ad79
+	fputs(_(" -p, --pid            operate on existing given pid\n"), out);
05ad79
+	fputs(_(" -v, --verbose        display status information\n"), out);
05ad79
+
05ad79
+	fputs(USAGE_SEPARATOR, out);
05ad79
+	fputs(USAGE_HELP, out);
05ad79
+	fputs(USAGE_VERSION, out);
05ad79
+
05ad79
+	fprintf(out, USAGE_MAN_TAIL("chrt(1)"));
05ad79
+	exit(rc);
05ad79
+}
05ad79
+
05ad79
+static const char *get_policy_name(int policy)
05ad79
+{
05ad79
 	switch (policy) {
05ad79
 	case SCHED_OTHER:
05ad79
-		printf("SCHED_OTHER\n");
05ad79
-		break;
05ad79
+		return "SCHED_OTHER";
05ad79
 	case SCHED_FIFO:
05ad79
-		printf("SCHED_FIFO\n");
05ad79
-		break;
05ad79
 #ifdef SCHED_RESET_ON_FORK
05ad79
 	case SCHED_FIFO | SCHED_RESET_ON_FORK:
05ad79
-		printf("SCHED_FIFO|SCHED_RESET_ON_FORK\n");
05ad79
-		break;
05ad79
 #endif
05ad79
+		return "SCHED_FIFO";
05ad79
 #ifdef SCHED_IDLE
05ad79
 	case SCHED_IDLE:
05ad79
-		printf("SCHED_IDLE\n");
05ad79
-		break;
05ad79
+		return "SCHED_IDLE";
05ad79
 #endif
05ad79
 	case SCHED_RR:
05ad79
-		printf("SCHED_RR\n");
05ad79
-		break;
05ad79
 #ifdef SCHED_RESET_ON_FORK
05ad79
 	case SCHED_RR | SCHED_RESET_ON_FORK:
05ad79
-		printf("SCHED_RR|SCHED_RESET_ON_FORK\n");
05ad79
-		break;
05ad79
 #endif
05ad79
+		return "SCHED_RR";
05ad79
 #ifdef SCHED_BATCH
05ad79
 	case SCHED_BATCH:
05ad79
-		printf("SCHED_BATCH\n");
05ad79
-		break;
05ad79
+		return "SCHED_BATCH";
05ad79
+#endif
05ad79
+#ifdef SCHED_DEADLINE
05ad79
+	case SCHED_DEADLINE:
05ad79
+		return "SCHED_DEADLINE";
05ad79
 #endif
05ad79
 	default:
05ad79
-		warnx(_("unknown scheduling policy"));
05ad79
+		break;
05ad79
+	}
05ad79
+
05ad79
+	return _("unknown");
05ad79
+}
05ad79
+
05ad79
+static void show_sched_pid_info(struct chrt_ctl *ctl, pid_t pid)
05ad79
+{
05ad79
+	int policy, reset_on_fork = 0, prio = 0;
05ad79
+#ifdef SCHED_DEADLINE
05ad79
+	uint64_t deadline = 0, runtime = 0, period = 0;
05ad79
+#endif
05ad79
+
05ad79
+	/* don't display "pid 0" as that is confusing */
05ad79
+	if (!pid)
05ad79
+		pid = getpid();
05ad79
+
05ad79
+	errno = 0;
05ad79
+
05ad79
+	/*
05ad79
+	 * New way
05ad79
+	 */
05ad79
+#ifdef HAVE_SCHED_SETATTR
05ad79
+	{
05ad79
+		struct sched_attr sa;
05ad79
+
05ad79
+		if (sched_getattr(pid, &sa, sizeof(sa), 0) != 0) {
05ad79
+			if (errno == ENOSYS)
05ad79
+				goto fallback;
05ad79
+			err(EXIT_FAILURE, _("failed to get pid %d's policy"), pid);
05ad79
+		}
05ad79
+
05ad79
+		policy = sa.sched_policy;
05ad79
+		prio = sa.sched_priority;
05ad79
+		reset_on_fork = sa.sched_flags & SCHED_FLAG_RESET_ON_FORK;
05ad79
+		deadline = sa.sched_deadline;
05ad79
+		runtime = sa.sched_runtime;
05ad79
+		period = sa.sched_period;
05ad79
 	}
05ad79
+#endif
05ad79
+
05ad79
+	/*
05ad79
+	 * Old way
05ad79
+	 */
05ad79
+fallback:
05ad79
+	if (errno == ENOSYS) {
05ad79
+		struct sched_param sp;
05ad79
 
05ad79
-	if (sched_getparam(pid, &sp))
05ad79
-		err(EXIT_FAILURE, _("failed to get pid %d's attributes"), pid);
05ad79
+		policy = sched_getscheduler(pid);
05ad79
+		if (policy == -1)
05ad79
+			err(EXIT_FAILURE, _("failed to get pid %d's policy"), pid);
05ad79
 
05ad79
-	if (isnew)
05ad79
-		printf(_("pid %d's new scheduling priority: %d\n"),
05ad79
-		       pid, sp.sched_priority);
05ad79
+		if (sched_getparam(pid, &sp) != 0)
05ad79
+			err(EXIT_FAILURE, _("failed to get pid %d's attributes"), pid);
05ad79
+		else
05ad79
+			prio = sp.sched_priority;
05ad79
+# ifdef SCHED_RESET_ON_FORK
05ad79
+		if (policy == (SCHED_FIFO|SCHED_RESET_ON_FORK) || policy == (SCHED_BATCH|SCHED_RESET_ON_FORK))
05ad79
+			reset_on_fork = 1;
05ad79
+# endif
05ad79
+	}
05ad79
+
05ad79
+	if (ctl->altered)
05ad79
+		printf(_("pid %d's new scheduling policy: %s"), pid, get_policy_name(policy));
05ad79
+	else
05ad79
+		printf(_("pid %d's current scheduling policy: %s"), pid, get_policy_name(policy));
05ad79
+
05ad79
+	if (reset_on_fork)
05ad79
+		printf("|SCHED_RESET_ON_FORK");
05ad79
+	putchar('\n');
05ad79
+
05ad79
+	if (ctl->altered)
05ad79
+		printf(_("pid %d's new scheduling priority: %d\n"), pid, prio);
05ad79
 	else
05ad79
-		printf(_("pid %d's current scheduling priority: %d\n"),
05ad79
-		       pid, sp.sched_priority);
05ad79
+		printf(_("pid %d's current scheduling priority: %d\n"), pid, prio);
05ad79
+
05ad79
+#ifdef SCHED_DEADLINE
05ad79
+	if (policy == SCHED_DEADLINE) {
05ad79
+		if (ctl->altered)
05ad79
+			printf(_("pid %d's new runtime/deadline/period parameters: %ju/%ju/%ju\n"),
05ad79
+					pid, runtime, deadline, period);
05ad79
+		else
05ad79
+			printf(_("pid %d's current runtime/deadline/period parameters: %ju/%ju/%ju\n"),
05ad79
+					pid, runtime, deadline, period);
05ad79
+	}
05ad79
+#endif
05ad79
+}
05ad79
+
05ad79
+
05ad79
+static void show_sched_info(struct chrt_ctl *ctl)
05ad79
+{
05ad79
+	if (ctl->all_tasks) {
05ad79
+		pid_t tid;
05ad79
+		struct proc_tasks *ts = proc_open_tasks(ctl->pid);
05ad79
+
05ad79
+		if (!ts)
05ad79
+			err(EXIT_FAILURE, _("cannot obtain the list of tasks"));
05ad79
+
05ad79
+		while (!proc_next_tid(ts, &tid))
05ad79
+			show_sched_pid_info(ctl, tid);
05ad79
+
05ad79
+		proc_close_tasks(ts);
05ad79
+	} else
05ad79
+		show_sched_pid_info(ctl, ctl->pid);
05ad79
 }
05ad79
 
05ad79
 static void show_min_max(void)
05ad79
@@ -167,52 +317,116 @@ static void show_min_max(void)
05ad79
 #ifdef SCHED_IDLE
05ad79
 		SCHED_IDLE,
05ad79
 #endif
05ad79
-	};
05ad79
-	const char *names[] = {
05ad79
-		"OTHER",
05ad79
-		"FIFO",
05ad79
-		"RR",
05ad79
-#ifdef SCHED_BATCH
05ad79
-		"BATCH",
05ad79
-#endif
05ad79
-#ifdef SCHED_IDLE
05ad79
-		"IDLE",
05ad79
+#ifdef SCHED_DEADLINE
05ad79
+		SCHED_DEADLINE,
05ad79
 #endif
05ad79
 	};
05ad79
 
05ad79
 	for (i = 0; i < ARRAY_SIZE(policies); i++) {
05ad79
-		int max = sched_get_priority_max(policies[i]);
05ad79
-		int min = sched_get_priority_min(policies[i]);
05ad79
+		int plc = policies[i];
05ad79
+		int max = sched_get_priority_max(plc);
05ad79
+		int min = sched_get_priority_min(plc);
05ad79
 
05ad79
 		if (max >= 0 && min >= 0)
05ad79
-			printf(_("SCHED_%s min/max priority\t: %d/%d\n"),
05ad79
-					names[i], min, max);
05ad79
+			printf(_("%s min/max priority\t: %d/%d\n"),
05ad79
+					get_policy_name(plc), min, max);
05ad79
 		else
05ad79
-			printf(_("SCHED_%s not supported?\n"), names[i]);
05ad79
+			printf(_("%s not supported?\n"), get_policy_name(plc));
05ad79
 	}
05ad79
 }
05ad79
 
05ad79
+static int set_sched_one_by_setscheduler(struct chrt_ctl *ctl, pid_t pid)
05ad79
+{
05ad79
+	struct sched_param sp = { .sched_priority = ctl->priority };
05ad79
+	int policy = ctl->policy;
05ad79
+
05ad79
+# ifdef SCHED_RESET_ON_FORK
05ad79
+	if (ctl->reset_on_fork)
05ad79
+		policy |= SCHED_RESET_ON_FORK;
05ad79
+# endif
05ad79
+	return sched_setscheduler(pid, policy, &sp);
05ad79
+}
05ad79
+
05ad79
+
05ad79
+#ifndef HAVE_SCHED_SETATTR
05ad79
+static int set_sched_one(struct chrt_ctl *ctl, pid_t pid)
05ad79
+{
05ad79
+	return set_sched_one_by_setscheduler(ctl, pid);
05ad79
+}
05ad79
+
05ad79
+#else /* !HAVE_SCHED_SETATTR */
05ad79
+static int set_sched_one(struct chrt_ctl *ctl, pid_t pid)
05ad79
+{
05ad79
+	/* use main() to check if the setting makes sense */
05ad79
+	struct sched_attr sa = {
05ad79
+		.size		= sizeof(struct sched_attr),
05ad79
+		.sched_policy	= ctl->policy,
05ad79
+		.sched_priority	= ctl->priority,
05ad79
+		.sched_runtime  = ctl->runtime,
05ad79
+		.sched_period   = ctl->period,
05ad79
+		.sched_deadline = ctl->deadline
05ad79
+	};
05ad79
+	int rc;
05ad79
+
05ad79
+# ifdef SCHED_RESET_ON_FORK
05ad79
+	if (ctl->reset_on_fork)
05ad79
+		sa.sched_flags |= SCHED_RESET_ON_FORK;
05ad79
+# endif
05ad79
+	errno = 0;
05ad79
+	rc = sched_setattr(pid, &sa, 0);
05ad79
+
05ad79
+	if (rc != 0 && errno == ENOSYS && ctl->policy != SCHED_DEADLINE)
05ad79
+		/* fallback -- build with new kernel/libc, but executed on old kernels */
05ad79
+		rc = set_sched_one_by_setscheduler(ctl, pid);
05ad79
+
05ad79
+	return rc;
05ad79
+}
05ad79
+#endif /* HAVE_SCHED_SETATTR */
05ad79
+
05ad79
+static void set_sched(struct chrt_ctl *ctl)
05ad79
+{
05ad79
+	if (ctl->all_tasks) {
05ad79
+		pid_t tid;
05ad79
+		struct proc_tasks *ts = proc_open_tasks(ctl->pid);
05ad79
+
05ad79
+		if (!ts)
05ad79
+			err(EXIT_FAILURE, _("cannot obtain the list of tasks"));
05ad79
+
05ad79
+		while (!proc_next_tid(ts, &tid))
05ad79
+			if (set_sched_one(ctl, tid) == -1)
05ad79
+				err(EXIT_FAILURE, _("failed to set tid %d's policy"), tid);
05ad79
+
05ad79
+		proc_close_tasks(ts);
05ad79
+
05ad79
+	} else if (set_sched_one(ctl, ctl->pid) == -1)
05ad79
+		err(EXIT_FAILURE, _("failed to set pid %d's policy"), ctl->pid);
05ad79
+
05ad79
+	ctl->altered = 1;
05ad79
+}
05ad79
+
05ad79
 int main(int argc, char **argv)
05ad79
 {
05ad79
-	int i, policy = SCHED_RR, priority = 0, verbose = 0, policy_flag = 0,
05ad79
-	    all_tasks = 0;
05ad79
-	struct sched_param sp;
05ad79
-	pid_t pid = -1;
05ad79
+	struct chrt_ctl _ctl = { .pid = -1 }, *ctl = &_ctl;
05ad79
+	int c;
05ad79
 
05ad79
 	static const struct option longopts[] = {
05ad79
-		{ "all-tasks",  0, NULL, 'a' },
05ad79
-		{ "batch",	0, NULL, 'b' },
05ad79
-		{ "fifo",	0, NULL, 'f' },
05ad79
-		{ "idle",	0, NULL, 'i' },
05ad79
-		{ "pid",	0, NULL, 'p' },
05ad79
-		{ "help",	0, NULL, 'h' },
05ad79
-		{ "max",        0, NULL, 'm' },
05ad79
-		{ "other",	0, NULL, 'o' },
05ad79
-		{ "rr",		0, NULL, 'r' },
05ad79
-		{ "reset-on-fork", 0, NULL, 'R' },
05ad79
-		{ "verbose",	0, NULL, 'v' },
05ad79
-		{ "version",	0, NULL, 'V' },
05ad79
-		{ NULL,		0, NULL, 0 }
05ad79
+		{ "all-tasks",  no_argument, NULL, 'a' },
05ad79
+		{ "batch",	no_argument, NULL, 'b' },
05ad79
+		{ "deadline",   no_argument, NULL, 'd' },
05ad79
+		{ "fifo",	no_argument, NULL, 'f' },
05ad79
+		{ "idle",	no_argument, NULL, 'i' },
05ad79
+		{ "pid",	no_argument, NULL, 'p' },
05ad79
+		{ "help",	no_argument, NULL, 'h' },
05ad79
+		{ "max",        no_argument, NULL, 'm' },
05ad79
+		{ "other",	no_argument, NULL, 'o' },
05ad79
+		{ "rr",		no_argument, NULL, 'r' },
05ad79
+		{ "sched-runtime",  required_argument, NULL, 'T' },
05ad79
+		{ "sched-period",   required_argument, NULL, 'P' },
05ad79
+		{ "sched-deadline", required_argument, NULL, 'D' },
05ad79
+		{ "reset-on-fork",  no_argument,       NULL, 'R' },
05ad79
+		{ "verbose",	no_argument, NULL, 'v' },
05ad79
+		{ "version",	no_argument, NULL, 'V' },
05ad79
+		{ NULL,		no_argument, NULL, 0 }
05ad79
 	};
05ad79
 
05ad79
 	setlocale(LC_ALL, "");
05ad79
@@ -220,51 +434,63 @@ int main(int argc, char **argv)
05ad79
 	textdomain(PACKAGE);
05ad79
 	atexit(close_stdout);
05ad79
 
05ad79
-	while((i = getopt_long(argc, argv, "+abfiphmoRrvV", longopts, NULL)) != -1)
05ad79
+	while((c = getopt_long(argc, argv, "+abdD:fiphmoP:T:rRvV", longopts, NULL)) != -1)
05ad79
 	{
05ad79
 		int ret = EXIT_FAILURE;
05ad79
 
05ad79
-		switch (i) {
05ad79
+		switch (c) {
05ad79
 		case 'a':
05ad79
-			all_tasks = 1;
05ad79
+			ctl->all_tasks = 1;
05ad79
 			break;
05ad79
 		case 'b':
05ad79
 #ifdef SCHED_BATCH
05ad79
-			policy = SCHED_BATCH;
05ad79
+			ctl->policy = SCHED_BATCH;
05ad79
+#endif
05ad79
+			break;
05ad79
+
05ad79
+		case 'd':
05ad79
+#ifdef SCHED_DEADLINE
05ad79
+			ctl->policy = SCHED_DEADLINE;
05ad79
 #endif
05ad79
 			break;
05ad79
 		case 'f':
05ad79
-			policy = SCHED_FIFO;
05ad79
+			ctl->policy = SCHED_FIFO;
05ad79
 			break;
05ad79
 		case 'R':
05ad79
-#ifdef SCHED_RESET_ON_FORK
05ad79
-			policy_flag |= SCHED_RESET_ON_FORK;
05ad79
-#endif
05ad79
+			ctl->reset_on_fork = 1;
05ad79
 			break;
05ad79
 		case 'i':
05ad79
 #ifdef SCHED_IDLE
05ad79
-			policy = SCHED_IDLE;
05ad79
+			ctl->policy = SCHED_IDLE;
05ad79
 #endif
05ad79
 			break;
05ad79
 		case 'm':
05ad79
 			show_min_max();
05ad79
 			return EXIT_SUCCESS;
05ad79
 		case 'o':
05ad79
-			policy = SCHED_OTHER;
05ad79
+			ctl->policy = SCHED_OTHER;
05ad79
 			break;
05ad79
 		case 'p':
05ad79
 			errno = 0;
05ad79
-			pid = strtos32_or_err(argv[argc - 1], _("invalid PID argument"));
05ad79
+			ctl->pid = strtos32_or_err(argv[argc - 1], _("invalid PID argument"));
05ad79
 			break;
05ad79
 		case 'r':
05ad79
-			policy = SCHED_RR;
05ad79
+			ctl->policy = SCHED_RR;
05ad79
 			break;
05ad79
 		case 'v':
05ad79
-			verbose = 1;
05ad79
+			ctl->verbose = 1;
05ad79
+			break;
05ad79
+		case 'T':
05ad79
+			ctl->runtime = strtou64_or_err(optarg, _("invalid runtime argument"));
05ad79
+			break;
05ad79
+		case 'P':
05ad79
+			ctl->period = strtou64_or_err(optarg, _("invalid period argument"));
05ad79
+			break;
05ad79
+		case 'D':
05ad79
+			ctl->deadline = strtou64_or_err(optarg, _("invalid deadline argument"));
05ad79
 			break;
05ad79
 		case 'V':
05ad79
-			printf(_("%s from %s\n"), program_invocation_short_name,
05ad79
-			       PACKAGE_STRING);
05ad79
+			printf(UTIL_LINUX_VERSION);
05ad79
 			return EXIT_SUCCESS;
05ad79
 		case 'h':
05ad79
 			ret = EXIT_SUCCESS;
05ad79
@@ -274,61 +500,56 @@ int main(int argc, char **argv)
05ad79
 		}
05ad79
 	}
05ad79
 
05ad79
-	if (((pid > -1) && argc - optind < 1) ||
05ad79
-	    ((pid == -1) && argc - optind < 2))
05ad79
+	if (((ctl->pid > -1) && argc - optind < 1) ||
05ad79
+	    ((ctl->pid == -1) && argc - optind < 2))
05ad79
 		show_usage(EXIT_FAILURE);
05ad79
 
05ad79
-	if ((pid > -1) && (verbose || argc - optind == 1)) {
05ad79
-		if (all_tasks) {
05ad79
-			pid_t tid;
05ad79
-			struct proc_tasks *ts = proc_open_tasks(pid);
05ad79
-
05ad79
-			if (!ts)
05ad79
-				err(EXIT_FAILURE, _("cannot obtain the list of tasks"));
05ad79
-			while (!proc_next_tid(ts, &tid))
05ad79
-				show_rt_info(tid, FALSE);
05ad79
-			proc_close_tasks(ts);
05ad79
-		} else
05ad79
-			show_rt_info(pid, FALSE);
05ad79
-
05ad79
+	if ((ctl->pid > -1) && (ctl->verbose || argc - optind == 1)) {
05ad79
+		show_sched_info(ctl);
05ad79
 		if (argc - optind == 1)
05ad79
 			return EXIT_SUCCESS;
05ad79
 	}
05ad79
 
05ad79
 	errno = 0;
05ad79
-	priority = strtos32_or_err(argv[optind], _("invalid priority argument"));
05ad79
+	ctl->priority = strtos32_or_err(argv[optind], _("invalid priority argument"));
05ad79
 
05ad79
 #ifdef SCHED_RESET_ON_FORK
05ad79
-	/* sanity check */
05ad79
-	if ((policy_flag & SCHED_RESET_ON_FORK) &&
05ad79
-	    !(policy == SCHED_FIFO || policy == SCHED_RR))
05ad79
-		errx(EXIT_FAILURE, _("SCHED_RESET_ON_FORK flag is supported for "
05ad79
+	if (ctl->reset_on_fork && ctl->policy != SCHED_FIFO && ctl->policy != SCHED_RR)
05ad79
+		errx(EXIT_FAILURE, _("--reset-on-fork option is supported for "
05ad79
 				     "SCHED_FIFO and SCHED_RR policies only"));
05ad79
 #endif
05ad79
-
05ad79
-	policy |= policy_flag;
05ad79
-
05ad79
-	if (pid == -1)
05ad79
-		pid = 0;
05ad79
-	sp.sched_priority = priority;
05ad79
-
05ad79
-	if (all_tasks) {
05ad79
-		pid_t tid;
05ad79
-		struct proc_tasks *ts = proc_open_tasks(pid);
05ad79
-
05ad79
-		if (!ts)
05ad79
-			err(EXIT_FAILURE, _("cannot obtain the list of tasks"));
05ad79
-		while (!proc_next_tid(ts, &tid))
05ad79
-			if (sched_setscheduler(tid, policy, &sp) == -1)
05ad79
-				err(EXIT_FAILURE, _("failed to set tid %d's policy"), tid);
05ad79
-		proc_close_tasks(ts);
05ad79
-	} else if (sched_setscheduler(pid, policy, &sp) == -1)
05ad79
-		err(EXIT_FAILURE, _("failed to set pid %d's policy"), pid);
05ad79
-
05ad79
-	if (verbose)
05ad79
-		show_rt_info(pid, TRUE);
05ad79
-
05ad79
-	if (!pid) {
05ad79
+#ifdef SCHED_DEADLINE
05ad79
+	if ((ctl->runtime || ctl->deadline || ctl->period) && ctl->policy != SCHED_DEADLINE)
05ad79
+		errx(EXIT_FAILURE, _("--sched-{runtime,deadline,period} options "
05ad79
+				     "are supported for SCHED_DEADLINE only"));
05ad79
+	if (ctl->policy == SCHED_DEADLINE) {
05ad79
+		/* The basic rule is runtime <= deadline <= period, so we can
05ad79
+		 * make deadline and runtime optional on command line. Note we
05ad79
+		 * don't check any values or set any defaults, it's kernel
05ad79
+		 * responsibility.
05ad79
+		 */
05ad79
+		if (ctl->deadline == 0)
05ad79
+			ctl->deadline = ctl->period;
05ad79
+		if (ctl->runtime == 0)
05ad79
+			ctl->runtime = ctl->deadline;
05ad79
+	}
05ad79
+#else
05ad79
+	if (ctl->runtime || ctl->deadline || ctl->period)
05ad79
+		errx(EXIT_FAILURE, _("SCHED_DEADLINE is unsupported"));
05ad79
+#endif
05ad79
+	if (ctl->pid == -1)
05ad79
+		ctl->pid = 0;
05ad79
+	if (ctl->priority < sched_get_priority_min(ctl->policy) ||
05ad79
+	    sched_get_priority_max(ctl->policy) < ctl->priority)
05ad79
+		errx(EXIT_FAILURE,
05ad79
+		     _("unsupported priority value for the policy: %d: see --max for valid range"),
05ad79
+		     ctl->priority);
05ad79
+	set_sched(ctl);
05ad79
+
05ad79
+	if (ctl->verbose)
05ad79
+		show_sched_info(ctl);
05ad79
+
05ad79
+	if (!ctl->pid) {
05ad79
 		argv += optind + 1;
05ad79
 		execvp(argv[0], argv);
05ad79
 		err(EXIT_FAILURE, _("failed to execute %s"), argv[0]);
05ad79
-- 
05ad79
2.7.4
05ad79