2007-03-09 22:26:38 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* chrt - manipulate real-time attributes of a process
|
2008-09-25 17:43:34 +05:30
|
|
|
* Copyright (c) 2006-2007 Bernhard Reutner-Fischer
|
2007-03-09 22:26:38 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2007-03-09 22:26:38 +05:30
|
|
|
*/
|
2016-11-23 03:44:24 +05:30
|
|
|
//config:config CHRT
|
2018-12-28 07:50:17 +05:30
|
|
|
//config: bool "chrt (4.7 kb)"
|
2016-11-23 03:44:24 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2018-01-25 14:04:23 +05:30
|
|
|
//config: Manipulate real-time attributes of a process.
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: This requires sched_{g,s}etparam support in your libc.
|
2011-04-11 06:59:49 +05:30
|
|
|
|
2017-08-04 23:25:01 +05:30
|
|
|
//applet:IF_CHRT(APPLET_NOEXEC(chrt, chrt, BB_DIR_USR_BIN, BB_SUID_DROP, chrt))
|
2016-11-23 04:24:17 +05:30
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_CHRT) += chrt.o
|
|
|
|
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define chrt_trivial_usage
|
2019-04-05 15:33:48 +05:30
|
|
|
//usage: "-m | -p [PRIO] PID | [-rfobi] PRIO PROG [ARGS]"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define chrt_full_usage "\n\n"
|
|
|
|
//usage: "Change scheduling priority and class for a process\n"
|
2019-04-05 15:33:48 +05:30
|
|
|
//usage: "\n -m Show min/max priorities"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage: "\n -p Operate on PID"
|
|
|
|
//usage: "\n -r Set SCHED_RR class"
|
|
|
|
//usage: "\n -f Set SCHED_FIFO class"
|
|
|
|
//usage: "\n -o Set SCHED_OTHER class"
|
2018-01-14 02:56:39 +05:30
|
|
|
//usage: "\n -b Set SCHED_BATCH class"
|
2018-01-14 02:57:37 +05:30
|
|
|
//usage: "\n -i Set SCHED_IDLE class"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:
|
|
|
|
//usage:#define chrt_example_usage
|
|
|
|
//usage: "$ chrt -r 4 sleep 900; x=$!\n"
|
|
|
|
//usage: "$ chrt -f -p 3 $x\n"
|
|
|
|
//usage: "You need CAP_SYS_NICE privileges to set scheduling attributes of a process"
|
|
|
|
|
2007-03-09 22:26:38 +05:30
|
|
|
#include <sched.h>
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2018-01-23 20:31:13 +05:30
|
|
|
#ifndef SCHED_IDLE
|
|
|
|
# define SCHED_IDLE 5
|
|
|
|
#endif
|
2010-07-01 20:12:27 +05:30
|
|
|
|
2019-04-05 15:33:48 +05:30
|
|
|
static const char *policy_name(int pol)
|
|
|
|
{
|
|
|
|
if (pol > 6)
|
|
|
|
return utoa(pol);
|
|
|
|
return nth_string(
|
|
|
|
"OTHER" "\0" /* 0:SCHED_OTHER */
|
|
|
|
"FIFO" "\0" /* 1:SCHED_FIFO */
|
|
|
|
"RR" "\0" /* 2:SCHED_RR */
|
|
|
|
"BATCH" "\0" /* 3:SCHED_BATCH */
|
|
|
|
"ISO" "\0" /* 4:SCHED_ISO */
|
|
|
|
"IDLE" "\0" /* 5:SCHED_IDLE */
|
|
|
|
"DEADLINE", /* 6:SCHED_DEADLINE */
|
|
|
|
pol
|
|
|
|
);
|
|
|
|
}
|
2007-03-09 22:26:38 +05:30
|
|
|
|
2007-04-17 04:02:04 +05:30
|
|
|
static void show_min_max(int pol)
|
|
|
|
{
|
2019-04-05 15:33:48 +05:30
|
|
|
const char *fmt = "SCHED_%s min/max priority\t: %u/%u\n";
|
2007-03-09 22:26:38 +05:30
|
|
|
int max, min;
|
2010-07-01 20:12:27 +05:30
|
|
|
|
2007-03-09 22:26:38 +05:30
|
|
|
max = sched_get_priority_max(pol);
|
|
|
|
min = sched_get_priority_min(pol);
|
2010-07-01 20:12:27 +05:30
|
|
|
if ((max|min) < 0)
|
2019-04-05 15:33:48 +05:30
|
|
|
fmt = "SCHED_%s not supported\n";
|
|
|
|
printf(fmt, policy_name(pol), min, max);
|
2007-03-09 22:26:38 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#define OPT_m (1<<0)
|
|
|
|
#define OPT_p (1<<1)
|
|
|
|
#define OPT_r (1<<2)
|
|
|
|
#define OPT_f (1<<3)
|
|
|
|
#define OPT_o (1<<4)
|
2018-01-14 02:56:39 +05:30
|
|
|
#define OPT_b (1<<5)
|
2018-01-14 02:57:37 +05:30
|
|
|
#define OPT_i (1<<6)
|
2007-03-09 22:26:38 +05:30
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int chrt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int chrt_main(int argc UNUSED_PARAM, char **argv)
|
2007-03-09 22:26:38 +05:30
|
|
|
{
|
|
|
|
pid_t pid = 0;
|
|
|
|
unsigned opt;
|
|
|
|
struct sched_param sp;
|
2008-03-17 14:59:43 +05:30
|
|
|
char *pid_str;
|
|
|
|
char *priority = priority; /* for compiler */
|
|
|
|
const char *current_new;
|
|
|
|
int policy = SCHED_RR;
|
2007-03-09 22:26:38 +05:30
|
|
|
|
2018-01-23 20:31:13 +05:30
|
|
|
opt = getopt32(argv, "^"
|
|
|
|
"+" "mprfobi"
|
|
|
|
"\0"
|
|
|
|
/* only one policy accepted: */
|
|
|
|
"r--fobi:f--robi:o--rfbi:b--rfoi:i--rfob"
|
|
|
|
);
|
2010-12-19 08:37:50 +05:30
|
|
|
if (opt & OPT_m) { /* print min/max and exit */
|
2018-01-23 20:31:13 +05:30
|
|
|
show_min_max(SCHED_OTHER);
|
2010-12-19 08:37:50 +05:30
|
|
|
show_min_max(SCHED_FIFO);
|
|
|
|
show_min_max(SCHED_RR);
|
2018-01-14 02:56:39 +05:30
|
|
|
show_min_max(SCHED_BATCH);
|
2018-01-14 02:57:37 +05:30
|
|
|
show_min_max(SCHED_IDLE);
|
2010-12-19 08:37:50 +05:30
|
|
|
fflush_stdout_and_exit(EXIT_SUCCESS);
|
|
|
|
}
|
2018-01-23 20:31:13 +05:30
|
|
|
//if (opt & OPT_r)
|
|
|
|
// policy = SCHED_RR; - default, already set
|
2007-03-09 22:26:38 +05:30
|
|
|
if (opt & OPT_f)
|
|
|
|
policy = SCHED_FIFO;
|
|
|
|
if (opt & OPT_o)
|
|
|
|
policy = SCHED_OTHER;
|
2018-01-14 02:56:39 +05:30
|
|
|
if (opt & OPT_b)
|
|
|
|
policy = SCHED_BATCH;
|
2018-01-14 02:57:37 +05:30
|
|
|
if (opt & OPT_i)
|
|
|
|
policy = SCHED_IDLE;
|
2008-03-17 14:59:43 +05:30
|
|
|
|
2008-03-24 07:35:58 +05:30
|
|
|
argv += optind;
|
2010-12-19 08:37:50 +05:30
|
|
|
if (!argv[0])
|
|
|
|
bb_show_usage();
|
2007-03-09 22:26:38 +05:30
|
|
|
if (opt & OPT_p) {
|
2008-03-17 14:59:43 +05:30
|
|
|
pid_str = *argv++;
|
2019-04-05 15:33:48 +05:30
|
|
|
if (*argv) { /* "-p PRIO PID [...]" */
|
2008-03-17 14:59:43 +05:30
|
|
|
priority = pid_str;
|
|
|
|
pid_str = *argv;
|
2007-03-09 22:26:38 +05:30
|
|
|
}
|
2019-04-05 15:33:48 +05:30
|
|
|
/* else "-p PID", and *argv == NULL */
|
2008-03-17 14:59:43 +05:30
|
|
|
pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
|
2007-03-09 22:26:38 +05:30
|
|
|
} else {
|
2008-03-17 14:59:43 +05:30
|
|
|
priority = *argv++;
|
|
|
|
if (!*argv)
|
|
|
|
bb_show_usage();
|
2007-03-09 22:26:38 +05:30
|
|
|
}
|
|
|
|
|
2008-03-17 14:59:43 +05:30
|
|
|
current_new = "current\0new";
|
2007-03-09 22:26:38 +05:30
|
|
|
if (opt & OPT_p) {
|
2008-03-17 14:59:43 +05:30
|
|
|
int pol;
|
|
|
|
print_rt_info:
|
2007-03-09 22:26:38 +05:30
|
|
|
pol = sched_getscheduler(pid);
|
|
|
|
if (pol < 0)
|
2019-04-05 15:33:48 +05:30
|
|
|
bb_perror_msg_and_die("can't %cet pid %u's policy", 'g', (int)pid);
|
2019-04-05 20:29:07 +05:30
|
|
|
#ifdef SCHED_RESET_ON_FORK
|
|
|
|
/* "Since Linux 2.6.32, the SCHED_RESET_ON_FORK flag
|
|
|
|
* can be ORed in policy when calling sched_setscheduler().
|
|
|
|
* As a result of including this flag, children created by
|
|
|
|
* fork(2) do not inherit privileged scheduling policies"
|
|
|
|
*
|
|
|
|
* This bit is also returned by sched_getscheduler()!
|
|
|
|
* (TODO: do we want to show it?)
|
|
|
|
*/
|
|
|
|
pol &= ~SCHED_RESET_ON_FORK;
|
|
|
|
#endif
|
2019-04-05 15:33:48 +05:30
|
|
|
printf("pid %u's %s scheduling policy: SCHED_%s\n",
|
|
|
|
pid, current_new, policy_name(pol)
|
|
|
|
);
|
2007-03-09 22:26:38 +05:30
|
|
|
if (sched_getparam(pid, &sp))
|
2019-04-05 15:33:48 +05:30
|
|
|
bb_perror_msg_and_die("can't get pid %u's attributes", (int)pid);
|
|
|
|
printf("pid %u's %s scheduling priority: %d\n",
|
|
|
|
(int)pid, current_new, sp.sched_priority
|
|
|
|
);
|
2008-03-17 14:59:43 +05:30
|
|
|
if (!*argv) {
|
2019-04-05 15:33:48 +05:30
|
|
|
/* Either it was just "-p PID",
|
|
|
|
* or it was "-p PRIO PID" and we came here
|
2008-03-17 14:59:43 +05:30
|
|
|
* for the second time (see goto below) */
|
2007-03-09 22:26:38 +05:30
|
|
|
return EXIT_SUCCESS;
|
2008-03-17 14:59:43 +05:30
|
|
|
}
|
|
|
|
*argv = NULL;
|
|
|
|
current_new += 8;
|
2007-03-09 22:26:38 +05:30
|
|
|
}
|
|
|
|
|
2018-01-14 02:56:39 +05:30
|
|
|
sp.sched_priority = xstrtou_range(priority, 0,
|
2018-01-23 20:31:13 +05:30
|
|
|
sched_get_priority_min(policy), sched_get_priority_max(policy)
|
|
|
|
);
|
2008-03-17 14:59:43 +05:30
|
|
|
|
2007-03-09 22:26:38 +05:30
|
|
|
if (sched_setscheduler(pid, policy, &sp) < 0)
|
2019-04-05 15:33:48 +05:30
|
|
|
bb_perror_msg_and_die("can't %cet pid %u's policy", 's', (int)pid);
|
2008-03-17 14:59:43 +05:30
|
|
|
|
2019-04-05 15:33:48 +05:30
|
|
|
if (!argv[0]) /* "-p PRIO PID [...]" */
|
2007-03-09 22:26:38 +05:30
|
|
|
goto print_rt_info;
|
2008-03-17 14:59:43 +05:30
|
|
|
|
2010-07-04 04:27:03 +05:30
|
|
|
BB_EXECVP_or_die(argv);
|
2007-03-09 22:26:38 +05:30
|
|
|
}
|