| 1 |
/* |
| 2 |
* ionice: set or get process io scheduling class and priority |
| 3 |
* |
| 4 |
* Copyright (C) 2005 Jens Axboe <[email protected]> |
| 5 |
* |
| 6 |
* Released under the terms of the GNU General Public License version 2 |
| 7 |
* |
| 8 |
*/ |
| 9 |
#include <stdio.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <errno.h> |
| 12 |
#include <getopt.h> |
| 13 |
#include <unistd.h> |
| 14 |
#ifdef HAVE_SYS_SYSCALL_H |
| 15 |
#include <sys/syscall.h> |
| 16 |
#endif |
| 17 |
#include <ctype.h> |
| 18 |
|
| 19 |
#include "nls.h" |
| 20 |
#include "strutils.h" |
| 21 |
#include "c.h" |
| 22 |
#include "cctype.h" |
| 23 |
#include "closestream.h" |
| 24 |
|
| 25 |
static int tolerant; |
| 26 |
|
| 27 |
static inline int ioprio_set(int which, int who, int ioprio) |
| 28 |
{ |
| 29 |
return syscall(SYS_ioprio_set, which, who, ioprio); |
| 30 |
} |
| 31 |
|
| 32 |
static inline int ioprio_get(int which, int who) |
| 33 |
{ |
| 34 |
return syscall(SYS_ioprio_get, which, who); |
| 35 |
} |
| 36 |
|
| 37 |
enum { |
| 38 |
IOPRIO_CLASS_NONE, |
| 39 |
IOPRIO_CLASS_RT, |
| 40 |
IOPRIO_CLASS_BE, |
| 41 |
IOPRIO_CLASS_IDLE, |
| 42 |
}; |
| 43 |
|
| 44 |
enum { |
| 45 |
IOPRIO_WHO_PROCESS = 1, |
| 46 |
IOPRIO_WHO_PGRP, |
| 47 |
IOPRIO_WHO_USER, |
| 48 |
}; |
| 49 |
|
| 50 |
#define IOPRIO_CLASS_SHIFT (13) |
| 51 |
#define IOPRIO_PRIO_MASK ((1UL << IOPRIO_CLASS_SHIFT) - 1) |
| 52 |
|
| 53 |
#define IOPRIO_PRIO_CLASS(mask) ((mask) >> IOPRIO_CLASS_SHIFT) |
| 54 |
#define IOPRIO_PRIO_DATA(mask) ((mask) & IOPRIO_PRIO_MASK) |
| 55 |
#define IOPRIO_PRIO_VALUE(class, data) (((class) << IOPRIO_CLASS_SHIFT) | data) |
| 56 |
|
| 57 |
static const char *const to_prio[] = { |
| 58 |
[IOPRIO_CLASS_NONE] = "none", |
| 59 |
[IOPRIO_CLASS_RT] = "realtime", |
| 60 |
[IOPRIO_CLASS_BE] = "best-effort", |
| 61 |
[IOPRIO_CLASS_IDLE] = "idle" |
| 62 |
}; |
| 63 |
|
| 64 |
static int parse_ioclass(const char *str) |
| 65 |
{ |
| 66 |
size_t i; |
| 67 |
|
| 68 |
for (i = 0; i < ARRAY_SIZE(to_prio); i++) |
| 69 |
if (!c_strcasecmp(str, to_prio[i])) |
| 70 |
return i; |
| 71 |
return -1; |
| 72 |
} |
| 73 |
|
| 74 |
static void ioprio_print(int pid, int who) |
| 75 |
{ |
| 76 |
int ioprio = ioprio_get(who, pid); |
| 77 |
|
| 78 |
if (ioprio == -1) |
| 79 |
err(EXIT_FAILURE, _("ioprio_get failed")); |
| 80 |
else { |
| 81 |
int ioclass = IOPRIO_PRIO_CLASS(ioprio); |
| 82 |
const char *name = _("unknown"); |
| 83 |
|
| 84 |
if (ioclass >= 0 && (size_t) ioclass < ARRAY_SIZE(to_prio)) |
| 85 |
name = to_prio[ioclass]; |
| 86 |
|
| 87 |
if (ioclass != IOPRIO_CLASS_IDLE) |
| 88 |
printf(_("%s: prio %lu\n"), name, |
| 89 |
IOPRIO_PRIO_DATA(ioprio)); |
| 90 |
else |
| 91 |
printf("%s\n", name); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
static void ioprio_setid(int which, int ioclass, int data, int who) |
| 96 |
{ |
| 97 |
int rc = ioprio_set(who, which, |
| 98 |
IOPRIO_PRIO_VALUE(ioclass, data)); |
| 99 |
|
| 100 |
if (rc == -1 && !tolerant) |
| 101 |
err(EXIT_FAILURE, _("ioprio_set failed")); |
| 102 |
} |
| 103 |
|
| 104 |
static void __attribute__((__noreturn__)) usage(void) |
| 105 |
{ |
| 106 |
FILE *out = stdout; |
| 107 |
fputs(USAGE_HEADER, out); |
| 108 |
fprintf(out, _(" %1$s [options] -p <pid>...\n" |
| 109 |
" %1$s [options] -P <pgid>...\n" |
| 110 |
" %1$s [options] -u <uid>...\n" |
| 111 |
" %1$s [options] <command>\n"), program_invocation_short_name); |
| 112 |
|
| 113 |
fputs(USAGE_SEPARATOR, out); |
| 114 |
fputs(_("Show or change the I/O-scheduling class and priority of a process.\n"), out); |
| 115 |
|
| 116 |
fputs(USAGE_OPTIONS, out); |
| 117 |
fputs(_(" -c, --class <class> name or number of scheduling class,\n" |
| 118 |
" 0: none, 1: realtime, 2: best-effort, 3: idle\n"), out); |
| 119 |
fputs(_(" -n, --classdata <num> priority (0..7) in the specified scheduling class,\n" |
| 120 |
" only for the realtime and best-effort classes\n"), out); |
| 121 |
fputs(_(" -p, --pid <pid>... act on these already running processes\n"), out); |
| 122 |
fputs(_(" -P, --pgid <pgrp>... act on already running processes in these groups\n"), out); |
| 123 |
fputs(_(" -t, --ignore ignore failures\n"), out); |
| 124 |
fputs(_(" -u, --uid <uid>... act on already running processes owned by these users\n"), out); |
| 125 |
|
| 126 |
fputs(USAGE_SEPARATOR, out); |
| 127 |
fprintf(out, USAGE_HELP_OPTIONS(24)); |
| 128 |
|
| 129 |
fprintf(out, USAGE_MAN_TAIL("ionice(1)")); |
| 130 |
|
| 131 |
exit(EXIT_SUCCESS); |
| 132 |
} |
| 133 |
|
| 134 |
int main(int argc, char **argv) |
| 135 |
{ |
| 136 |
int data = 4, set = 0, ioclass = IOPRIO_CLASS_BE, c; |
| 137 |
int which = 0, who = 0; |
| 138 |
const char *invalid_msg = NULL; |
| 139 |
|
| 140 |
static const struct option longopts[] = { |
| 141 |
{ "classdata", required_argument, NULL, 'n' }, |
| 142 |
{ "class", required_argument, NULL, 'c' }, |
| 143 |
{ "help", no_argument, NULL, 'h' }, |
| 144 |
{ "ignore", no_argument, NULL, 't' }, |
| 145 |
{ "pid", required_argument, NULL, 'p' }, |
| 146 |
{ "pgid", required_argument, NULL, 'P' }, |
| 147 |
{ "uid", required_argument, NULL, 'u' }, |
| 148 |
{ "version", no_argument, NULL, 'V' }, |
| 149 |
{ NULL, 0, NULL, 0 } |
| 150 |
}; |
| 151 |
|
| 152 |
setlocale(LC_ALL, ""); |
| 153 |
bindtextdomain(PACKAGE, LOCALEDIR); |
| 154 |
textdomain(PACKAGE); |
| 155 |
close_stdout_atexit(); |
| 156 |
|
| 157 |
while ((c = getopt_long(argc, argv, "+n:c:p:P:u:tVh", longopts, NULL)) != EOF) |
| 158 |
switch (c) { |
| 159 |
case 'n': |
| 160 |
data = strtos32_or_err(optarg, _("invalid class data argument")); |
| 161 |
set |= 1; |
| 162 |
break; |
| 163 |
case 'c': |
| 164 |
if (isdigit(*optarg)) |
| 165 |
ioclass = strtos32_or_err(optarg, |
| 166 |
_("invalid class argument")); |
| 167 |
else { |
| 168 |
ioclass = parse_ioclass(optarg); |
| 169 |
if (ioclass < 0) |
| 170 |
errx(EXIT_FAILURE, |
| 171 |
_("unknown scheduling class: '%s'"), |
| 172 |
optarg); |
| 173 |
} |
| 174 |
set |= 2; |
| 175 |
break; |
| 176 |
case 'p': |
| 177 |
if (who) |
| 178 |
errx(EXIT_FAILURE, |
| 179 |
_("can handle only one of pid, pgid or uid at once")); |
| 180 |
invalid_msg = _("invalid PID argument"); |
| 181 |
which = strtos32_or_err(optarg, invalid_msg); |
| 182 |
who = IOPRIO_WHO_PROCESS; |
| 183 |
break; |
| 184 |
case 'P': |
| 185 |
if (who) |
| 186 |
errx(EXIT_FAILURE, |
| 187 |
_("can handle only one of pid, pgid or uid at once")); |
| 188 |
invalid_msg = _("invalid PGID argument"); |
| 189 |
which = strtos32_or_err(optarg, invalid_msg); |
| 190 |
who = IOPRIO_WHO_PGRP; |
| 191 |
break; |
| 192 |
case 'u': |
| 193 |
if (who) |
| 194 |
errx(EXIT_FAILURE, |
| 195 |
_("can handle only one of pid, pgid or uid at once")); |
| 196 |
invalid_msg = _("invalid UID argument"); |
| 197 |
which = strtos32_or_err(optarg, invalid_msg); |
| 198 |
who = IOPRIO_WHO_USER; |
| 199 |
break; |
| 200 |
case 't': |
| 201 |
tolerant = 1; |
| 202 |
break; |
| 203 |
|
| 204 |
case 'V': |
| 205 |
print_version(EXIT_SUCCESS); |
| 206 |
case 'h': |
| 207 |
usage(); |
| 208 |
default: |
| 209 |
errtryhelp(EXIT_FAILURE); |
| 210 |
} |
| 211 |
|
| 212 |
switch (ioclass) { |
| 213 |
case IOPRIO_CLASS_NONE: |
| 214 |
if ((set & 1) && !tolerant) |
| 215 |
warnx(_("ignoring given class data for none class")); |
| 216 |
data = 0; |
| 217 |
break; |
| 218 |
case IOPRIO_CLASS_RT: |
| 219 |
case IOPRIO_CLASS_BE: |
| 220 |
break; |
| 221 |
case IOPRIO_CLASS_IDLE: |
| 222 |
if ((set & 1) && !tolerant) |
| 223 |
warnx(_("ignoring given class data for idle class")); |
| 224 |
data = 7; |
| 225 |
break; |
| 226 |
default: |
| 227 |
if (!tolerant) |
| 228 |
warnx(_("unknown prio class %d"), ioclass); |
| 229 |
break; |
| 230 |
} |
| 231 |
|
| 232 |
if (!set && !which && optind == argc) |
| 233 |
/* |
| 234 |
* ionice without options, print the current ioprio |
| 235 |
*/ |
| 236 |
ioprio_print(0, IOPRIO_WHO_PROCESS); |
| 237 |
else if (!set && who) { |
| 238 |
/* |
| 239 |
* ionice -p|-P|-u ID [ID ...] |
| 240 |
*/ |
| 241 |
ioprio_print(which, who); |
| 242 |
|
| 243 |
for(; argv[optind]; ++optind) { |
| 244 |
which = strtos32_or_err(argv[optind], invalid_msg); |
| 245 |
ioprio_print(which, who); |
| 246 |
} |
| 247 |
} else if (set && who) { |
| 248 |
/* |
| 249 |
* ionice -c CLASS -p|-P|-u ID [ID ...] |
| 250 |
*/ |
| 251 |
ioprio_setid(which, ioclass, data, who); |
| 252 |
|
| 253 |
for(; argv[optind]; ++optind) { |
| 254 |
which = strtos32_or_err(argv[optind], invalid_msg); |
| 255 |
ioprio_setid(which, ioclass, data, who); |
| 256 |
} |
| 257 |
} else if (argv[optind]) { |
| 258 |
/* |
| 259 |
* ionice [-c CLASS] COMMAND |
| 260 |
*/ |
| 261 |
ioprio_setid(0, ioclass, data, IOPRIO_WHO_PROCESS); |
| 262 |
execvp(argv[optind], &argv[optind]); |
| 263 |
errexec(argv[optind]); |
| 264 |
} else { |
| 265 |
warnx(_("bad usage")); |
| 266 |
errtryhelp(EXIT_FAILURE); |
| 267 |
} |
| 268 |
|
| 269 |
return EXIT_SUCCESS; |
| 270 |
} |