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
|
2017-07-19 01:31:24 +05:30
|
|
|
//config: bool "chrt (4.4 kb)"
|
2016-11-23 03:44:24 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: manipulate real-time attributes of a process.
|
|
|
|
//config: This requires sched_{g,s}etparam support in your libc.
|
2011-04-11 06:59:49 +05:30
|
|
|
|
2016-11-23 04:24:17 +05:30
|
|
|
//applet:IF_CHRT(APPLET(chrt, BB_DIR_USR_BIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_CHRT) += chrt.o
|
|
|
|
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define chrt_trivial_usage
|
|
|
|
//usage: "[-prfom] [PRIO] [PID | PROG ARGS]"
|
|
|
|
//usage:#define chrt_full_usage "\n\n"
|
|
|
|
//usage: "Change scheduling priority and class for a process\n"
|
|
|
|
//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"
|
|
|
|
//usage: "\n -m Show min/max priorities"
|
|
|
|
//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"
|
2010-07-01 20:12:27 +05:30
|
|
|
|
2007-03-09 22:26:38 +05:30
|
|
|
static const struct {
|
2008-03-17 14:59:43 +05:30
|
|
|
int policy;
|
2010-07-01 20:12:27 +05:30
|
|
|
char name[sizeof("SCHED_OTHER")];
|
2007-03-09 22:26:38 +05:30
|
|
|
} policies[] = {
|
|
|
|
{SCHED_OTHER, "SCHED_OTHER"},
|
|
|
|
{SCHED_FIFO, "SCHED_FIFO"},
|
|
|
|
{SCHED_RR, "SCHED_RR"}
|
|
|
|
};
|
|
|
|
|
2010-07-01 20:12:27 +05:30
|
|
|
//TODO: add
|
|
|
|
// -b, SCHED_BATCH
|
|
|
|
// -i, SCHED_IDLE
|
|
|
|
|
2007-04-17 04:02:04 +05:30
|
|
|
static void show_min_max(int pol)
|
|
|
|
{
|
2010-07-01 20:12:27 +05:30
|
|
|
const char *fmt = "%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)
|
|
|
|
fmt = "%s not supported\n";
|
|
|
|
printf(fmt, policies[pol].name, 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)
|
|
|
|
|
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
|
|
|
|
2010-12-19 08:37:50 +05:30
|
|
|
/* only one policy accepted */
|
|
|
|
opt_complementary = "r--fo:f--ro:o--rf";
|
2008-03-17 14:59:43 +05:30
|
|
|
opt = getopt32(argv, "+mprfo");
|
2010-12-19 08:37:50 +05:30
|
|
|
if (opt & OPT_m) { /* print min/max and exit */
|
|
|
|
show_min_max(SCHED_FIFO);
|
|
|
|
show_min_max(SCHED_RR);
|
|
|
|
show_min_max(SCHED_OTHER);
|
|
|
|
fflush_stdout_and_exit(EXIT_SUCCESS);
|
|
|
|
}
|
2007-03-09 22:26:38 +05:30
|
|
|
if (opt & OPT_r)
|
|
|
|
policy = SCHED_RR;
|
|
|
|
if (opt & OPT_f)
|
|
|
|
policy = SCHED_FIFO;
|
|
|
|
if (opt & OPT_o)
|
|
|
|
policy = SCHED_OTHER;
|
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++;
|
|
|
|
if (*argv) { /* "-p <priority> <pid> [...]" */
|
|
|
|
priority = pid_str;
|
|
|
|
pid_str = *argv;
|
2007-03-09 22:26:38 +05:30
|
|
|
}
|
2008-03-17 14:59:43 +05:30
|
|
|
/* else "-p <pid>", and *argv == NULL */
|
|
|
|
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)
|
2010-12-18 07:29:09 +05:30
|
|
|
bb_perror_msg_and_die("can't %cet pid %d's policy", 'g', (int)pid);
|
2007-03-09 22:26:38 +05:30
|
|
|
printf("pid %d's %s scheduling policy: %s\n",
|
2008-03-17 14:59:43 +05:30
|
|
|
pid, current_new, policies[pol].name);
|
2007-03-09 22:26:38 +05:30
|
|
|
if (sched_getparam(pid, &sp))
|
2010-12-18 07:29:09 +05:30
|
|
|
bb_perror_msg_and_die("can't get pid %d's attributes", (int)pid);
|
2007-03-09 22:26:38 +05:30
|
|
|
printf("pid %d's %s scheduling priority: %d\n",
|
2010-12-18 07:29:09 +05:30
|
|
|
(int)pid, current_new, sp.sched_priority);
|
2008-03-17 14:59:43 +05:30
|
|
|
if (!*argv) {
|
|
|
|
/* Either it was just "-p <pid>",
|
|
|
|
* or it was "-p <priority> <pid>" and we came here
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
2008-03-17 14:59:43 +05:30
|
|
|
/* from the manpage of sched_getscheduler:
|
|
|
|
[...] sched_priority can have a value in the range 0 to 99.
|
|
|
|
[...] SCHED_OTHER or SCHED_BATCH must be assigned static priority 0.
|
|
|
|
[...] SCHED_FIFO or SCHED_RR can have static priority in 1..99 range.
|
|
|
|
*/
|
|
|
|
sp.sched_priority = xstrtou_range(priority, 0, policy != SCHED_OTHER ? 1 : 0, 99);
|
|
|
|
|
2007-03-09 22:26:38 +05:30
|
|
|
if (sched_setscheduler(pid, policy, &sp) < 0)
|
2010-12-18 07:29:09 +05:30
|
|
|
bb_perror_msg_and_die("can't %cet pid %d's policy", 's', (int)pid);
|
2008-03-17 14:59:43 +05:30
|
|
|
|
2010-06-25 05:16:53 +05:30
|
|
|
if (!argv[0]) /* "-p <priority> <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
|
|
|
}
|