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
|
|
|
*
|
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sched.h>
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2007-03-09 22:26:38 +05:30
|
|
|
#ifndef _POSIX_PRIORITY_SCHEDULING
|
|
|
|
#warning your system may be foobared
|
|
|
|
#endif
|
|
|
|
static const struct {
|
2008-03-17 14:59:43 +05:30
|
|
|
int policy;
|
|
|
|
char name[12];
|
2007-03-09 22:26:38 +05:30
|
|
|
} policies[] = {
|
|
|
|
{SCHED_OTHER, "SCHED_OTHER"},
|
|
|
|
{SCHED_FIFO, "SCHED_FIFO"},
|
|
|
|
{SCHED_RR, "SCHED_RR"}
|
|
|
|
};
|
|
|
|
|
2007-04-17 04:02:04 +05:30
|
|
|
static void show_min_max(int pol)
|
|
|
|
{
|
2007-03-09 22:26:38 +05:30
|
|
|
const char *fmt = "%s min/max priority\t: %d/%d\n\0%s not supported?\n";
|
|
|
|
int max, min;
|
|
|
|
max = sched_get_priority_max(pol);
|
|
|
|
min = sched_get_priority_min(pol);
|
|
|
|
if (max >= 0 && min >= 0)
|
|
|
|
printf(fmt, policies[pol].name, min, max);
|
|
|
|
else {
|
|
|
|
fmt += 29;
|
|
|
|
printf(fmt, policies[pol].name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
|
|
|
2008-03-17 14:59:43 +05:30
|
|
|
/* at least 1 arg; only one policy accepted */
|
|
|
|
opt_complementary = "-1:r--fo:f--ro:r--fo";
|
|
|
|
opt = getopt32(argv, "+mprfo");
|
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;
|
|
|
|
if (opt & OPT_m) { /* print min/max */
|
|
|
|
show_min_max(SCHED_FIFO);
|
|
|
|
show_min_max(SCHED_RR);
|
|
|
|
show_min_max(SCHED_OTHER);
|
|
|
|
fflush_stdout_and_exit(EXIT_SUCCESS);
|
|
|
|
}
|
2008-03-17 14:59:43 +05:30
|
|
|
|
2008-03-24 07:35:58 +05:30
|
|
|
argv += optind;
|
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)
|
2008-03-17 14:59:43 +05:30
|
|
|
bb_perror_msg_and_die("can't %cet pid %d's policy", 'g', 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))
|
2008-03-17 14:59:43 +05:30
|
|
|
bb_perror_msg_and_die("can't get pid %d's attributes", pid);
|
2007-03-09 22:26:38 +05:30
|
|
|
printf("pid %d's %s scheduling priority: %d\n",
|
2008-03-17 14:59:43 +05:30
|
|
|
pid, current_new, sp.sched_priority);
|
|
|
|
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)
|
2008-03-17 14:59:43 +05:30
|
|
|
bb_perror_msg_and_die("can't %cet pid %d's policy", 's', pid);
|
|
|
|
|
|
|
|
if (!*argv) /* "-p <priority> <pid> [...]" */
|
2007-03-09 22:26:38 +05:30
|
|
|
goto print_rt_info;
|
2008-03-17 14:59:43 +05:30
|
|
|
|
2007-03-09 22:26:38 +05:30
|
|
|
BB_EXECVP(*argv, argv);
|
2007-10-01 17:28:38 +05:30
|
|
|
bb_simple_perror_msg_and_die(*argv);
|
2007-03-09 22:26:38 +05:30
|
|
|
}
|