]> git.ipfire.org Git - thirdparty/util-linux.git/blame - schedutils/ionice.c
libblkid: cache is incorrectly revalidated
[thirdparty/util-linux.git] / schedutils / ionice.c
CommitLineData
48d7b13a
KZ
1/*
2 * ionice: set or get process io scheduling class and priority
3 *
357e7c3b 4 * Copyright (C) 2005 Jens Axboe <jens@axboe.dk>
48d7b13a
KZ
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#include <sys/ptrace.h>
15#include <sys/syscall.h>
16#include <asm/unistd.h>
5bd11d0d
KZ
17#include <err.h>
18
19#include "nls.h"
48d7b13a 20
24295096
KZ
21#include "schedutils.h"
22
ef0fe2e8
SM
23static int tolerant;
24
48d7b13a
KZ
25static inline int ioprio_set(int which, int who, int ioprio)
26{
4dfadf9c 27 return syscall(SYS_ioprio_set, which, who, ioprio);
48d7b13a
KZ
28}
29
30static inline int ioprio_get(int which, int who)
31{
4dfadf9c 32 return syscall(SYS_ioprio_get, which, who);
48d7b13a
KZ
33}
34
35enum {
36 IOPRIO_CLASS_NONE,
37 IOPRIO_CLASS_RT,
38 IOPRIO_CLASS_BE,
39 IOPRIO_CLASS_IDLE,
40};
41
42enum {
43 IOPRIO_WHO_PROCESS = 1,
44 IOPRIO_WHO_PGRP,
45 IOPRIO_WHO_USER,
46};
47
48#define IOPRIO_CLASS_SHIFT 13
49
50const char *to_prio[] = { "none", "realtime", "best-effort", "idle", };
51
ef0fe2e8
SM
52static void ioprio_print(int pid)
53{
54 int ioprio, ioclass;
55
56 ioprio = ioprio_get(IOPRIO_WHO_PROCESS, pid);
57
58 if (ioprio == -1)
59 err(EXIT_FAILURE, _("ioprio_get failed"));
60 else {
61 ioclass = ioprio >> IOPRIO_CLASS_SHIFT;
62 if (ioclass != IOPRIO_CLASS_IDLE) {
63 ioprio = ioprio & 0xff;
64 printf("%s: prio %d\n", to_prio[ioclass], ioprio);
65 } else
66 printf("%s\n", to_prio[ioclass]);
67 }
68}
69
70
71static void ioprio_setpid(pid_t pid, int ioprio, int ioclass)
72{
73 int rc = ioprio_set(IOPRIO_WHO_PROCESS, pid,
74 ioprio | ioclass << IOPRIO_CLASS_SHIFT);
75
76 if (rc == -1 && !tolerant)
77 err(EXIT_FAILURE, _("ioprio_set failed"));
78}
79
5bd11d0d 80static void usage(int rc)
48d7b13a 81{
5bd11d0d 82 fprintf(stdout, _(
030d18fd
KZ
83 "\nionice - sets or gets process io scheduling class and priority.\n"
84 "\nUsage:\n"
85 " ionice [ options ] -p <pid> [<pid> ...]\n"
4880a6f1 86 " ionice [ options ] <command> [<arg> ...]\n"
030d18fd 87 "\nOptions:\n"
5bd11d0d
KZ
88 " -n <classdata> class data (0-7, lower being higher prio)\n"
89 " -c <class> scheduling class\n"
5dc9371c 90 " 0: none, 1: realtime, 2: best-effort, 3: idle\n"
030d18fd 91 " -t ignore failures\n"
5bd11d0d 92 " -h this help\n\n"));
5bd11d0d 93 exit(rc);
48d7b13a
KZ
94}
95
96int main(int argc, char *argv[])
97{
030d18fd
KZ
98 int ioprio = 4, set = 0, ioclass = IOPRIO_CLASS_BE, c;
99 pid_t pid = 0;
48d7b13a 100
5bd11d0d
KZ
101 setlocale(LC_ALL, "");
102 bindtextdomain(PACKAGE, LOCALEDIR);
103 textdomain(PACKAGE);
104
4d125dfc 105 while ((c = getopt(argc, argv, "+n:c:p:th")) != EOF) {
48d7b13a
KZ
106 switch (c) {
107 case 'n':
24295096 108 ioprio = getnum(optarg, _("failed to parse class data"));
8dbfe5a1 109 set |= 1;
48d7b13a
KZ
110 break;
111 case 'c':
24295096 112 ioclass = getnum(optarg, _("failed to parse class"));
8dbfe5a1 113 set |= 2;
48d7b13a
KZ
114 break;
115 case 'p':
24295096 116 pid = getnum(optarg, _("failed to parse pid"));
48d7b13a 117 break;
4d125dfc
LK
118 case 't':
119 tolerant = 1;
120 break;
48d7b13a 121 case 'h':
5bd11d0d 122 usage(EXIT_SUCCESS);
48d7b13a 123 default:
5bd11d0d 124 usage(EXIT_FAILURE);
48d7b13a
KZ
125 }
126 }
127
ef0fe2e8 128 switch (ioclass) {
48d7b13a 129 case IOPRIO_CLASS_NONE:
5dc9371c
JU
130 if (set & 1)
131 warnx(_("ignoring given class data for none class"));
132 ioprio = 0;
48d7b13a
KZ
133 break;
134 case IOPRIO_CLASS_RT:
135 case IOPRIO_CLASS_BE:
136 break;
137 case IOPRIO_CLASS_IDLE:
8dbfe5a1 138 if (set & 1)
5bd11d0d 139 warnx(_("ignoring given class data for idle class"));
48d7b13a
KZ
140 ioprio = 7;
141 break;
142 default:
ef0fe2e8 143 errx(EXIT_FAILURE, _("bad prio class %d"), ioclass);
48d7b13a
KZ
144 }
145
146 if (!set) {
ef0fe2e8 147 ioprio_print(pid);
48d7b13a 148
ef0fe2e8 149 for(; argv[optind]; ++optind) {
24295096 150 pid = getnum(argv[optind], _("failed to parse pid"));
ef0fe2e8 151 ioprio_print(pid);
48d7b13a
KZ
152 }
153 } else {
ef0fe2e8
SM
154 if (pid) {
155 ioprio_setpid(pid, ioprio, ioclass);
156
157 for(; argv[optind]; ++optind)
158 {
24295096 159 pid = getnum(argv[optind], _("failed to parse pid"));
ef0fe2e8
SM
160 ioprio_setpid(pid, ioprio, ioclass);
161 }
030d18fd
KZ
162 }
163 else if (argv[optind]) {
ef0fe2e8 164 ioprio_setpid(0, ioprio, ioclass);
030d18fd
KZ
165 execvp(argv[optind], &argv[optind]);
166 /* execvp should never return */
048b81c9 167 err(EXIT_FAILURE, _("executing %s failed"), argv[optind]);
2b258413 168 }
48d7b13a
KZ
169 }
170
2b258413 171 exit(EXIT_SUCCESS);
48d7b13a 172}