]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - schedutils/chrt.c
2 * chrt.c - manipulate a task's real-time attributes
4 * 27-Apr-2002: initial version - Robert Love <rml@tech9.net>
5 * 04-May-2011: make it thread-aware - Davidlohr Bueso <dave@gnu.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License, version 2, as
9 * published by the Free Software Foundation
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 * Copyright (C) 2004 Robert Love
32 #include "closestream.h"
34 #include "procutils.h"
36 /* the SCHED_BATCH is supported since Linux 2.6.16
37 * -- temporary workaround for people with old glibc headers
39 #if defined (__linux__) && !defined(SCHED_BATCH)
40 # define SCHED_BATCH 3
43 /* the SCHED_IDLE is supported since Linux 2.6.23
44 * commit id 0e6aca43e08a62a48d6770e9a159dbec167bf4c6
45 * -- temporary workaround for people with old glibc headers
47 #if defined (__linux__) && !defined(SCHED_IDLE)
51 #if defined(__linux__) && !defined(SCHED_RESET_ON_FORK)
52 #define SCHED_RESET_ON_FORK 0x40000000
56 static void __attribute__((__noreturn__
)) show_usage(int rc
)
58 FILE *out
= rc
== EXIT_SUCCESS
? stdout
: stderr
;
60 fputs(_("Show or change the real-time scheduling attributes of a process.\n"), out
);
63 " chrt [options] <priority> <command> [<arg>...]\n"
64 " chrt [options] -p <priority> <pid>\n"
66 " chrt [options] -p <pid>\n"));
69 "\nScheduling policies:\n"
70 " -b | --batch set policy to SCHED_BATCH\n"
71 " -f | --fifo set policy to SCHED_FIFO\n"
72 " -i | --idle set policy to SCHED_IDLE\n"
73 " -o | --other set policy to SCHED_OTHER\n"
74 " -r | --rr set policy to SCHED_RR (default)\n"));
76 #ifdef SCHED_RESET_ON_FORK
78 "\nScheduling flags:\n"
79 " -R | --reset-on-fork set SCHED_RESET_ON_FORK for FIFO or RR\n"));
83 " -a | --all-tasks operate on all the tasks (threads) for a given pid\n"
84 " -h | --help display this help\n"
85 " -m | --max show min and max valid priorities\n"
86 " -p | --pid operate on existing given pid\n"
87 " -v | --verbose display status information\n"
88 " -V | --version output version information\n\n"));
90 fprintf(out
, USAGE_MAN_TAIL("chrt(1)"));
94 static void show_rt_info(pid_t pid
, int isnew
)
96 struct sched_param sp
;
99 /* don't display "pid 0" as that is confusing */
103 policy
= sched_getscheduler(pid
);
105 err(EXIT_FAILURE
, _("failed to get pid %d's policy"), pid
);
108 printf(_("pid %d's new scheduling policy: "), pid
);
110 printf(_("pid %d's current scheduling policy: "), pid
);
114 printf("SCHED_OTHER\n");
117 printf("SCHED_FIFO\n");
119 #ifdef SCHED_RESET_ON_FORK
120 case SCHED_FIFO
| SCHED_RESET_ON_FORK
:
121 printf("SCHED_FIFO|SCHED_RESET_ON_FORK\n");
126 printf("SCHED_IDLE\n");
130 printf("SCHED_RR\n");
132 #ifdef SCHED_RESET_ON_FORK
133 case SCHED_RR
| SCHED_RESET_ON_FORK
:
134 printf("SCHED_RR|SCHED_RESET_ON_FORK\n");
139 printf("SCHED_BATCH\n");
143 warnx(_("unknown scheduling policy"));
146 if (sched_getparam(pid
, &sp
))
147 err(EXIT_FAILURE
, _("failed to get pid %d's attributes"), pid
);
150 printf(_("pid %d's new scheduling priority: %d\n"),
151 pid
, sp
.sched_priority
);
153 printf(_("pid %d's current scheduling priority: %d\n"),
154 pid
, sp
.sched_priority
);
157 static void show_min_max(void)
171 const char *names
[] = {
183 for (i
= 0; i
< ARRAY_SIZE(policies
); i
++) {
184 int max
= sched_get_priority_max(policies
[i
]);
185 int min
= sched_get_priority_min(policies
[i
]);
187 if (max
>= 0 && min
>= 0)
188 printf(_("SCHED_%s min/max priority\t: %d/%d\n"),
191 printf(_("SCHED_%s not supported?\n"), names
[i
]);
195 int main(int argc
, char **argv
)
197 int i
, policy
= SCHED_RR
, priority
= 0, verbose
= 0, policy_flag
= 0,
199 struct sched_param sp
;
202 static const struct option longopts
[] = {
203 { "all-tasks", 0, NULL
, 'a' },
204 { "batch", 0, NULL
, 'b' },
205 { "fifo", 0, NULL
, 'f' },
206 { "idle", 0, NULL
, 'i' },
207 { "pid", 0, NULL
, 'p' },
208 { "help", 0, NULL
, 'h' },
209 { "max", 0, NULL
, 'm' },
210 { "other", 0, NULL
, 'o' },
211 { "rr", 0, NULL
, 'r' },
212 { "reset-on-fork", 0, NULL
, 'R' },
213 { "verbose", 0, NULL
, 'v' },
214 { "version", 0, NULL
, 'V' },
218 setlocale(LC_ALL
, "");
219 bindtextdomain(PACKAGE
, LOCALEDIR
);
221 atexit(close_stdout
);
223 while((i
= getopt_long(argc
, argv
, "+abfiphmoRrvV", longopts
, NULL
)) != -1)
225 int ret
= EXIT_FAILURE
;
233 policy
= SCHED_BATCH
;
240 #ifdef SCHED_RESET_ON_FORK
241 policy_flag
|= SCHED_RESET_ON_FORK
;
253 policy
= SCHED_OTHER
;
257 pid
= strtos32_or_err(argv
[argc
- 1], _("invalid PID argument"));
266 printf(UTIL_LINUX_VERSION
);
276 if (((pid
> -1) && argc
- optind
< 1) ||
277 ((pid
== -1) && argc
- optind
< 2))
278 show_usage(EXIT_FAILURE
);
280 if ((pid
> -1) && (verbose
|| argc
- optind
== 1)) {
283 struct proc_tasks
*ts
= proc_open_tasks(pid
);
286 err(EXIT_FAILURE
, _("cannot obtain the list of tasks"));
287 while (!proc_next_tid(ts
, &tid
))
288 show_rt_info(tid
, FALSE
);
289 proc_close_tasks(ts
);
291 show_rt_info(pid
, FALSE
);
293 if (argc
- optind
== 1)
298 priority
= strtos32_or_err(argv
[optind
], _("invalid priority argument"));
300 #ifdef SCHED_RESET_ON_FORK
302 if ((policy_flag
& SCHED_RESET_ON_FORK
) &&
303 !(policy
== SCHED_FIFO
|| policy
== SCHED_RR
))
304 errx(EXIT_FAILURE
, _("SCHED_RESET_ON_FORK flag is supported for "
305 "SCHED_FIFO and SCHED_RR policies only"));
308 policy
|= policy_flag
;
312 sp
.sched_priority
= priority
;
316 struct proc_tasks
*ts
= proc_open_tasks(pid
);
319 err(EXIT_FAILURE
, _("cannot obtain the list of tasks"));
320 while (!proc_next_tid(ts
, &tid
))
321 if (sched_setscheduler(tid
, policy
, &sp
) == -1)
322 err(EXIT_FAILURE
, _("failed to set tid %d's policy"), tid
);
323 proc_close_tasks(ts
);
324 } else if (sched_setscheduler(pid
, policy
, &sp
) == -1)
325 err(EXIT_FAILURE
, _("failed to set pid %d's policy"), pid
);
328 show_rt_info(pid
, TRUE
);
332 execvp(argv
[0], argv
);
333 err(EXIT_FAILURE
, _("failed to execute %s"), argv
[0]);