2006-12-06 17:26:54 -06:00
|
|
|
/*
|
|
|
|
* chrt.c - chrt
|
|
|
|
* Command-line utility for manipulating a task's real-time attributes
|
|
|
|
*
|
|
|
|
* Robert Love <rml@tech9.net>
|
|
|
|
* 27-Apr-2002: initial version
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License, v2, as
|
|
|
|
* published by the Free Software Foundation
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004 Robert Love
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <errno.h>
|
2008-09-23 16:17:36 -05:00
|
|
|
#include <err.h>
|
|
|
|
|
|
|
|
#include "nls.h"
|
2006-12-06 17:26:54 -06:00
|
|
|
|
2007-04-11 16:18:08 -05:00
|
|
|
/* the SCHED_BATCH is supported since Linux 2.6.16
|
|
|
|
* -- temporary workaround for people with old glibc headers
|
|
|
|
*/
|
2009-04-22 02:18:28 -05:00
|
|
|
#if defined (__linux__) && !defined(SCHED_BATCH)
|
2007-04-11 16:18:08 -05:00
|
|
|
# define SCHED_BATCH 3
|
|
|
|
#endif
|
|
|
|
|
2008-09-23 07:23:03 -05:00
|
|
|
/* the SCHED_IDLE is supported since Linux 2.6.23
|
|
|
|
* commit id 0e6aca43e08a62a48d6770e9a159dbec167bf4c6
|
|
|
|
* -- temporary workaround for people with old glibc headers
|
|
|
|
*/
|
2009-04-22 02:18:28 -05:00
|
|
|
#if defined (__linux__) && !defined(SCHED_IDLE)
|
2008-09-23 07:23:03 -05:00
|
|
|
# define SCHED_IDLE 5
|
|
|
|
#endif
|
|
|
|
|
2008-09-23 16:17:36 -05:00
|
|
|
#ifndef ARRAY_SIZE
|
|
|
|
# define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void show_usage(int rc)
|
2006-12-06 17:26:54 -06:00
|
|
|
{
|
2008-09-23 16:17:36 -05:00
|
|
|
fprintf(stdout, _(
|
|
|
|
"\nchrt - manipulate real-time attributes of a process.\n"
|
|
|
|
"\nSet policy:\n"
|
|
|
|
" chrt [options] <policy> <priority> {<pid> | <command> [<arg> ...]}\n"
|
|
|
|
"\nGet policy:\n"
|
|
|
|
" chrt [options] {<pid> | <command> [<arg> ...]}\n\n"
|
|
|
|
"\nScheduling policies:\n"
|
|
|
|
" -b | --batch set policy to SCHED_BATCH\n"
|
|
|
|
" -f | --fifo set policy to SCHED_FIFO\n"
|
|
|
|
" -i | --idle set policy to SCHED_IDLE\n"
|
|
|
|
" -o | --other set policy to SCHED_OTHER\n"
|
|
|
|
" -r | --rr set policy to SCHED_RR (default)\n"
|
|
|
|
"\nOptions:\n"
|
|
|
|
" -h | --help display this help\n"
|
|
|
|
" -p | --pid operate on existing given pid\n"
|
|
|
|
" -m | --max show min and max valid priorities\n"
|
|
|
|
" -v | --verbose display status information\n"
|
|
|
|
" -V | --version output version information\n\n"));
|
|
|
|
|
|
|
|
exit(rc);
|
2006-12-06 17:26:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void show_rt_info(const char *what, pid_t pid)
|
|
|
|
{
|
|
|
|
struct sched_param sp;
|
|
|
|
int policy;
|
|
|
|
|
|
|
|
/* don't display "pid 0" as that is confusing */
|
|
|
|
if (!pid)
|
|
|
|
pid = getpid();
|
|
|
|
|
|
|
|
policy = sched_getscheduler(pid);
|
2008-09-23 16:17:36 -05:00
|
|
|
if (policy == -1)
|
|
|
|
err(EXIT_FAILURE, _("failed to get pid %d's policy"), pid);
|
2006-12-06 17:26:54 -06:00
|
|
|
|
2008-09-23 16:17:36 -05:00
|
|
|
printf(_("pid %d's %s scheduling policy: "), pid, what);
|
2006-12-06 17:26:54 -06:00
|
|
|
switch (policy) {
|
|
|
|
case SCHED_OTHER:
|
|
|
|
printf("SCHED_OTHER\n");
|
|
|
|
break;
|
|
|
|
case SCHED_FIFO:
|
|
|
|
printf("SCHED_FIFO\n");
|
|
|
|
break;
|
2009-04-22 02:18:28 -05:00
|
|
|
#ifdef SCHED_IDLE
|
2008-09-23 07:23:03 -05:00
|
|
|
case SCHED_IDLE:
|
|
|
|
printf("SCHED_IDLE\n");
|
2009-02-03 03:22:07 -06:00
|
|
|
break;
|
2009-04-22 02:18:28 -05:00
|
|
|
#endif
|
2006-12-06 17:26:54 -06:00
|
|
|
case SCHED_RR:
|
|
|
|
printf("SCHED_RR\n");
|
|
|
|
break;
|
2009-04-22 02:18:28 -05:00
|
|
|
#ifdef SCHED_BATCH
|
2007-01-16 08:24:13 -06:00
|
|
|
case SCHED_BATCH:
|
|
|
|
printf("SCHED_BATCH\n");
|
|
|
|
break;
|
2009-04-22 02:18:28 -05:00
|
|
|
#endif
|
2006-12-06 17:26:54 -06:00
|
|
|
default:
|
2008-09-23 16:17:36 -05:00
|
|
|
printf(_("unknown\n"));
|
2006-12-06 17:26:54 -06:00
|
|
|
}
|
|
|
|
|
2008-09-23 16:17:36 -05:00
|
|
|
if (sched_getparam(pid, &sp))
|
|
|
|
err(EXIT_FAILURE, _("failed to get pid %d's attributes"), pid);
|
2006-12-06 17:26:54 -06:00
|
|
|
|
2008-09-23 16:17:36 -05:00
|
|
|
printf(_("pid %d's %s scheduling priority: %d\n"),
|
2006-12-06 17:26:54 -06:00
|
|
|
pid, what, sp.sched_priority);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void show_min_max(void)
|
|
|
|
{
|
2008-09-23 16:17:36 -05:00
|
|
|
int i;
|
|
|
|
int policies[] = { SCHED_OTHER, SCHED_FIFO, SCHED_RR,
|
2009-04-22 02:18:28 -05:00
|
|
|
#ifdef SCHED_BATCH
|
|
|
|
SCHED_BATCH,
|
|
|
|
#endif
|
|
|
|
#ifdef SCHED_IDLE
|
|
|
|
SCHED_IDLE,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
const char *names[] = { "OTHER", "FIFO", "RR",
|
|
|
|
#ifdef SCHED_BATCH
|
|
|
|
"BATCH",
|
|
|
|
#endif
|
|
|
|
#ifdef SCHED_IDLE
|
|
|
|
"IDLE",
|
|
|
|
#endif
|
|
|
|
};
|
2008-09-23 16:17:36 -05:00
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(policies); i++) {
|
|
|
|
int max = sched_get_priority_max(policies[i]);
|
|
|
|
int min = sched_get_priority_min(policies[i]);
|
|
|
|
|
|
|
|
if (max >= 0 && min >= 0)
|
|
|
|
printf(_("SCHED_%s min/max priority\t: %d/%d\n"),
|
|
|
|
names[i], min, max);
|
|
|
|
else
|
|
|
|
printf(_("SCHED_%s not supported?\n"), names[i]);
|
|
|
|
}
|
2006-12-06 17:26:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int i, policy = SCHED_RR, priority = 0, verbose = 0;
|
|
|
|
struct sched_param sp;
|
2007-07-16 06:10:59 -05:00
|
|
|
pid_t pid = -1;
|
2006-12-06 17:26:54 -06:00
|
|
|
|
|
|
|
struct option longopts[] = {
|
2007-01-16 08:24:13 -06:00
|
|
|
{ "batch", 0, NULL, 'b' },
|
2006-12-06 17:26:54 -06:00
|
|
|
{ "fifo", 0, NULL, 'f' },
|
2008-09-23 07:23:03 -05:00
|
|
|
{ "idle", 0, NULL, 'i' },
|
2006-12-06 17:26:54 -06:00
|
|
|
{ "pid", 0, NULL, 'p' },
|
|
|
|
{ "help", 0, NULL, 'h' },
|
|
|
|
{ "max", 0, NULL, 'm' },
|
|
|
|
{ "other", 0, NULL, 'o' },
|
|
|
|
{ "rr", 0, NULL, 'r' },
|
|
|
|
{ "verbose", 0, NULL, 'v' },
|
|
|
|
{ "version", 0, NULL, 'V' },
|
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
2008-09-23 16:17:36 -05:00
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE);
|
|
|
|
|
2008-09-23 07:23:03 -05:00
|
|
|
while((i = getopt_long(argc, argv, "+bfiphmorvV", longopts, NULL)) != -1)
|
2006-12-06 17:26:54 -06:00
|
|
|
{
|
2008-09-23 16:17:36 -05:00
|
|
|
int ret = EXIT_FAILURE;
|
2006-12-06 17:26:54 -06:00
|
|
|
|
|
|
|
switch (i) {
|
2007-01-16 08:24:13 -06:00
|
|
|
case 'b':
|
2009-04-22 02:18:28 -05:00
|
|
|
#ifdef SCHED_BATCH
|
2007-01-16 08:24:13 -06:00
|
|
|
policy = SCHED_BATCH;
|
2009-04-22 02:18:28 -05:00
|
|
|
#endif
|
2007-01-16 08:24:13 -06:00
|
|
|
break;
|
2006-12-06 17:26:54 -06:00
|
|
|
case 'f':
|
|
|
|
policy = SCHED_FIFO;
|
|
|
|
break;
|
2008-09-23 07:23:03 -05:00
|
|
|
case 'i':
|
2009-04-22 02:18:28 -05:00
|
|
|
#ifdef SCHED_IDLE
|
2008-09-23 07:23:03 -05:00
|
|
|
policy = SCHED_IDLE;
|
2009-04-22 02:18:28 -05:00
|
|
|
#endif
|
2008-09-23 07:23:03 -05:00
|
|
|
break;
|
2006-12-06 17:26:54 -06:00
|
|
|
case 'm':
|
|
|
|
show_min_max();
|
|
|
|
return 0;
|
|
|
|
case 'o':
|
|
|
|
policy = SCHED_OTHER;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
errno = 0;
|
|
|
|
pid = strtol(argv[argc - 1], NULL, 10);
|
2008-09-23 16:17:36 -05:00
|
|
|
if (errno)
|
|
|
|
err(EXIT_FAILURE, _("failed to parse pid"));
|
2006-12-06 17:26:54 -06:00
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
policy = SCHED_RR;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verbose = 1;
|
|
|
|
break;
|
|
|
|
case 'V':
|
2006-12-06 17:26:58 -06:00
|
|
|
printf("chrt (%s)\n", PACKAGE_STRING);
|
2006-12-06 17:26:54 -06:00
|
|
|
return 0;
|
|
|
|
case 'h':
|
2008-09-23 16:17:36 -05:00
|
|
|
ret = EXIT_SUCCESS;
|
2006-12-06 17:26:54 -06:00
|
|
|
default:
|
2008-09-23 16:17:36 -05:00
|
|
|
show_usage(ret);
|
2006-12-06 17:26:54 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-23 16:17:36 -05:00
|
|
|
if (((pid > -1) && argc - optind < 1) || ((pid == -1) && argc - optind < 2))
|
|
|
|
show_usage(EXIT_FAILURE);
|
2006-12-06 17:26:54 -06:00
|
|
|
|
2007-07-16 06:10:59 -05:00
|
|
|
if ((pid > -1) && (verbose || argc - optind == 1)) {
|
2008-09-23 16:17:36 -05:00
|
|
|
show_rt_info(_("current"), pid);
|
2006-12-06 17:26:54 -06:00
|
|
|
if (argc - optind == 1)
|
2008-09-23 16:17:36 -05:00
|
|
|
return EXIT_SUCCESS;
|
2006-12-06 17:26:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
priority = strtol(argv[optind], NULL, 10);
|
2008-09-23 16:17:36 -05:00
|
|
|
if (errno)
|
|
|
|
err(EXIT_FAILURE, _("failed to parse priority"));
|
2006-12-06 17:26:54 -06:00
|
|
|
|
2007-07-16 06:10:59 -05:00
|
|
|
if (pid == -1)
|
|
|
|
pid = 0;
|
2006-12-06 17:26:54 -06:00
|
|
|
sp.sched_priority = priority;
|
2008-09-23 16:17:36 -05:00
|
|
|
if (sched_setscheduler(pid, policy, &sp) == -1)
|
|
|
|
err(EXIT_FAILURE, _("failed to set pid %d's policy"), pid);
|
2006-12-06 17:26:54 -06:00
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
show_rt_info("new", pid);
|
|
|
|
|
|
|
|
if (!pid) {
|
|
|
|
argv += optind + 1;
|
|
|
|
execvp(argv[0], argv);
|
|
|
|
perror("execvp");
|
2008-09-23 16:17:36 -05:00
|
|
|
err(EXIT_FAILURE, _("failed to execute %s"), argv[0]);
|
2006-12-06 17:26:54 -06:00
|
|
|
}
|
|
|
|
|
2008-09-23 16:17:36 -05:00
|
|
|
return EXIT_SUCCESS;
|
2006-12-06 17:26:54 -06:00
|
|
|
}
|