cdown / rpms / util-linux

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