]>
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>
18 static inline int ioprio_set(int which
, int who
, int ioprio
)
20 return syscall(SYS_ioprio_set
, which
, who
, ioprio
);
23 static inline int ioprio_get(int which
, int who
)
25 return syscall(SYS_ioprio_get
, which
, who
);
36 IOPRIO_WHO_PROCESS
= 1,
41 #define IOPRIO_CLASS_SHIFT 13
43 const char *to_prio
[] = { "none", "realtime", "best-effort", "idle", };
45 static void usage(void)
47 printf("Usage: ionice [OPTIONS] [COMMAND [ARG]...]\n");
48 printf("Sets or gets process io scheduling class and priority.\n");
49 printf("\n\t-n\tClass data (typically 0-7, lower being higher prio)\n");
50 printf("\t-c\tScheduling class\n");
51 printf("\t\t\t1: realtime, 2: best-effort, 3: idle\n");
52 printf("\t-p\tProcess pid\n");
53 printf("\t-t\tIgnore failures to set priority, run command unconditionally\n");
54 printf("\t-h\tThis help page\n");
55 printf("\nJens Axboe <axboe@suse.de> (C) 2005\n");
58 int main(int argc
, char *argv
[])
60 int ioprio
= 4, set
= 0, tolerant
= 0, ioprio_class
= IOPRIO_CLASS_BE
;
63 while ((c
= getopt(argc
, argv
, "+n:c:p:th")) != EOF
) {
66 ioprio
= strtol(optarg
, NULL
, 10);
70 ioprio_class
= strtol(optarg
, NULL
, 10);
74 pid
= strtol(optarg
, NULL
, 10);
86 switch (ioprio_class
) {
87 case IOPRIO_CLASS_NONE
:
88 ioprio_class
= IOPRIO_CLASS_BE
;
93 case IOPRIO_CLASS_IDLE
:
95 printf("Ignoring given class data for idle class\n");
99 printf("bad prio class %d\n", ioprio_class
);
104 if (!pid
&& argv
[optind
])
105 pid
= strtol(argv
[optind
], NULL
, 10);
107 ioprio
= ioprio_get(IOPRIO_WHO_PROCESS
, pid
);
110 perror("ioprio_get");
113 ioprio_class
= ioprio
>> IOPRIO_CLASS_SHIFT
;
114 if (ioprio_class
!= IOPRIO_CLASS_IDLE
) {
115 ioprio
= ioprio
& 0xff;
116 printf("%s: prio %d\n", to_prio
[ioprio_class
], ioprio
);
118 printf("%s\n", to_prio
[ioprio_class
]);
121 if (ioprio_set(IOPRIO_WHO_PROCESS
, pid
, ioprio
| ioprio_class
<< IOPRIO_CLASS_SHIFT
) == -1) {
123 perror("ioprio_set");
129 execvp(argv
[optind
], &argv
[optind
]);
130 /* execvp should never return */