]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - schedutils/ionice.c
2 * ionice: set or get process io scheduling class and priority
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de> SUSE Labs
6 * Released under the terms of the GNU General Public License version 2
14 #include <sys/ptrace.h>
15 #include <sys/syscall.h>
16 #include <asm/unistd.h>
21 static inline int ioprio_set(int which
, int who
, int ioprio
)
23 return syscall(SYS_ioprio_set
, which
, who
, ioprio
);
26 static inline int ioprio_get(int which
, int who
)
28 return syscall(SYS_ioprio_get
, which
, who
);
39 IOPRIO_WHO_PROCESS
= 1,
44 #define IOPRIO_CLASS_SHIFT 13
46 const char *to_prio
[] = { "none", "realtime", "best-effort", "idle", };
48 static void usage(int rc
)
51 "\nionice - sets or gets process io scheduling class and priority.\n\n"
52 "Usage: ionice [OPTIONS] [COMMAND [ARG]...]\n\n"
53 " -n <classdata> class data (0-7, lower being higher prio)\n"
54 " -c <class> scheduling class\n"
55 " 1: realtime, 2: best-effort, 3: idle\n"
56 " -p <pid> process pid\n"
57 " -t ignore failures, run command unconditionally\n"
58 " -h this help\n\n"));
63 int main(int argc
, char *argv
[])
65 int ioprio
= 4, set
= 0, tolerant
= 0, ioprio_class
= IOPRIO_CLASS_BE
;
68 setlocale(LC_ALL
, "");
69 bindtextdomain(PACKAGE
, LOCALEDIR
);
72 while ((c
= getopt(argc
, argv
, "+n:c:p:th")) != EOF
) {
75 ioprio
= strtol(optarg
, NULL
, 10);
79 ioprio_class
= strtol(optarg
, NULL
, 10);
83 pid
= strtol(optarg
, NULL
, 10);
95 switch (ioprio_class
) {
96 case IOPRIO_CLASS_NONE
:
97 ioprio_class
= IOPRIO_CLASS_BE
;
100 case IOPRIO_CLASS_BE
:
102 case IOPRIO_CLASS_IDLE
:
104 warnx(_("ignoring given class data for idle class"));
108 errx(EXIT_FAILURE
, _("bad prio class %d"), ioprio_class
);
112 if (!pid
&& argv
[optind
])
113 pid
= strtol(argv
[optind
], NULL
, 10);
115 ioprio
= ioprio_get(IOPRIO_WHO_PROCESS
, pid
);
118 err(EXIT_FAILURE
, _("ioprio_get failed"));
120 ioprio_class
= ioprio
>> IOPRIO_CLASS_SHIFT
;
121 if (ioprio_class
!= IOPRIO_CLASS_IDLE
) {
122 ioprio
= ioprio
& 0xff;
123 printf("%s: prio %d\n", to_prio
[ioprio_class
], ioprio
);
125 printf("%s\n", to_prio
[ioprio_class
]);
128 if (ioprio_set(IOPRIO_WHO_PROCESS
, pid
, ioprio
| ioprio_class
<< IOPRIO_CLASS_SHIFT
) == -1) {
130 err(EXIT_FAILURE
, _("ioprio_set failed"));
134 execvp(argv
[optind
], &argv
[optind
]);
135 /* execvp should never return */
136 err(EXIT_FAILURE
, _("execvp failed"));