2 * ionice: set or get process io scheduling class and priority
4 * Copyright (C) 2005 Jens Axboe <jens@axboe.dk>
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>
23 static inline int ioprio_set(int which, int who, int ioprio)
25 return syscall(SYS_ioprio_set, which, who, ioprio);
28 static inline int ioprio_get(int which, int who)
30 return syscall(SYS_ioprio_get, which, who);
41 IOPRIO_WHO_PROCESS = 1,
46 #define IOPRIO_CLASS_SHIFT 13
48 const char *to_prio[] = { "none", "realtime", "best-effort", "idle", };
50 static void ioprio_print(int pid)
54 ioprio = ioprio_get(IOPRIO_WHO_PROCESS, pid);
57 err(EXIT_FAILURE, _("ioprio_get failed"));
59 ioclass = ioprio >> IOPRIO_CLASS_SHIFT;
60 if (ioclass != IOPRIO_CLASS_IDLE) {
61 ioprio = ioprio & 0xff;
62 printf("%s: prio %d\n", to_prio[ioclass], ioprio);
64 printf("%s\n", to_prio[ioclass]);
69 static void ioprio_setpid(pid_t pid, int ioprio, int ioclass)
71 int rc = ioprio_set(IOPRIO_WHO_PROCESS, pid,
72 ioprio | ioclass << IOPRIO_CLASS_SHIFT);
74 if (rc == -1 && !tolerant)
75 err(EXIT_FAILURE, _("ioprio_set failed"));
78 static void usage(int rc)
81 "\nionice - sets or gets process io scheduling class and priority.\n"
83 " ionice [ options ] -p <pid> [<pid> ...]\n"
84 " ionoce [ options ] <command> [<arg> ...]\n"
86 " -n <classdata> class data (0-7, lower being higher prio)\n"
87 " -c <class> scheduling class\n"
88 " 0: none, 1: realtime, 2: best-effort, 3: idle\n"
89 " -t ignore failures\n"
90 " -h this help\n\n"));
94 static long getnum(const char *str)
99 if (str == NULL || *str == '\0')
102 num = strtol(str, &end, 10);
104 if (errno || (end && *end))
110 err(EXIT_SUCCESS, _("cannot parse number '%s'"), str);
112 errx(EXIT_SUCCESS, _("cannot parse number '%s'"), str);
116 int main(int argc, char *argv[])
118 int ioprio = 4, set = 0, ioclass = IOPRIO_CLASS_BE, c;
121 setlocale(LC_ALL, "");
122 bindtextdomain(PACKAGE, LOCALEDIR);
125 while ((c = getopt(argc, argv, "+n:c:p:th")) != EOF) {
128 ioprio = getnum(optarg);
132 ioclass = getnum(optarg);
136 pid = getnum(optarg);
149 case IOPRIO_CLASS_NONE:
151 warnx(_("ignoring given class data for none class"));
154 case IOPRIO_CLASS_RT:
155 case IOPRIO_CLASS_BE:
157 case IOPRIO_CLASS_IDLE:
159 warnx(_("ignoring given class data for idle class"));
163 errx(EXIT_FAILURE, _("bad prio class %d"), ioclass);
169 for(; argv[optind]; ++optind) {
170 pid = getnum(argv[optind]);
175 ioprio_setpid(pid, ioprio, ioclass);
177 for(; argv[optind]; ++optind)
179 pid = getnum(argv[optind]);
180 ioprio_setpid(pid, ioprio, ioclass);
183 else if (argv[optind]) {
184 ioprio_setpid(0, ioprio, ioclass);
185 execvp(argv[optind], &argv[optind]);
186 /* execvp should never return */
187 err(EXIT_FAILURE, _("execvp failed"));