]>
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 <jens@axboe.dk>
6 * Released under the terms of the GNU General Public License version 2
14 #include <sys/syscall.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 " ionice [ 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 int main(int argc
, char *argv
[])
96 int ioprio
= 4, set
= 0, ioclass
= IOPRIO_CLASS_BE
, c
;
99 setlocale(LC_ALL
, "");
100 bindtextdomain(PACKAGE
, LOCALEDIR
);
103 while ((c
= getopt(argc
, argv
, "+n:c:p:th")) != EOF
) {
106 ioprio
= strtol_or_err(optarg
, _("failed to parse class data"));
110 ioclass
= strtol_or_err(optarg
, _("failed to parse class"));
114 pid
= strtol_or_err(optarg
, _("failed to parse pid"));
127 case IOPRIO_CLASS_NONE
:
129 warnx(_("ignoring given class data for none class"));
132 case IOPRIO_CLASS_RT
:
133 case IOPRIO_CLASS_BE
:
135 case IOPRIO_CLASS_IDLE
:
137 warnx(_("ignoring given class data for idle class"));
141 errx(EXIT_FAILURE
, _("bad prio class %d"), ioclass
);
147 for(; argv
[optind
]; ++optind
) {
148 pid
= strtol_or_err(argv
[optind
], _("failed to parse pid"));
153 ioprio_setpid(pid
, ioprio
, ioclass
);
155 for(; argv
[optind
]; ++optind
)
157 pid
= strtol_or_err(argv
[optind
], _("failed to parse pid"));
158 ioprio_setpid(pid
, ioprio
, ioclass
);
161 else if (argv
[optind
]) {
162 ioprio_setpid(0, ioprio
, ioclass
);
163 execvp(argv
[optind
], &argv
[optind
]);
164 /* execvp should never return */
165 err(EXIT_FAILURE
, _("executing %s failed"), argv
[optind
]);