blob: 5b9664d257ded402263dce5f54c9666a78fb53a4 [file] [log] [blame]
Denis Vlasenko4acdb462009-01-31 21:45:57 +00001/* vi: set sw=4 ts=4: */
2/*
3 * ionice implementation for busybox based on linux-utils-ng 2.14
4 *
5 * Copyright (C) 2008 by <u173034@informatik.uni-oldenburg.de>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenko4acdb462009-01-31 21:45:57 +00008 */
Denys Vlasenkofb4da162016-11-22 23:14:24 +01009//config:config IONICE
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020010//config: bool "ionice (3.6 kb)"
Denys Vlasenkofb4da162016-11-22 23:14:24 +010011//config: default y
12//config: select PLATFORM_LINUX
13//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020014//config: Set/set program io scheduling class and priority
15//config: Requires kernel >= 2.6.13
Denis Vlasenko4acdb462009-01-31 21:45:57 +000016
Denys Vlasenko5c527dc2017-08-04 19:55:01 +020017//applet:IF_IONICE(APPLET_NOEXEC(ionice, ionice, BB_DIR_BIN, BB_SUID_DROP, ionice))
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +010018
19//kbuild:lib-$(CONFIG_IONICE) += ionice.o
20
Pere Orga5bc8c002011-04-11 03:29:49 +020021//usage:#define ionice_trivial_usage
22//usage: "[-c 1-3] [-n 0-7] [-p PID] [PROG]"
23//usage:#define ionice_full_usage "\n\n"
24//usage: "Change I/O priority and class\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020025//usage: "\n -c Class. 1:realtime 2:best-effort 3:idle"
26//usage: "\n -n Priority"
27
Denis Vlasenko4acdb462009-01-31 21:45:57 +000028#include <sys/syscall.h>
29#include <asm/unistd.h>
30#include "libbb.h"
31
32static int ioprio_set(int which, int who, int ioprio)
33{
34 return syscall(SYS_ioprio_set, which, who, ioprio);
35}
36
37static int ioprio_get(int which, int who)
38{
39 return syscall(SYS_ioprio_get, which, who);
40}
41
42enum {
43 IOPRIO_WHO_PROCESS = 1,
44 IOPRIO_WHO_PGRP,
45 IOPRIO_WHO_USER
46};
47
48enum {
49 IOPRIO_CLASS_NONE,
50 IOPRIO_CLASS_RT,
51 IOPRIO_CLASS_BE,
52 IOPRIO_CLASS_IDLE
53};
54
Denys Vlasenko3e134eb2016-04-22 18:09:21 +020055static const char to_prio[] ALIGN1 = "none\0realtime\0best-effort\0idle";
Denis Vlasenko4acdb462009-01-31 21:45:57 +000056
57#define IOPRIO_CLASS_SHIFT 13
58
59int ionice_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
60int ionice_main(int argc UNUSED_PARAM, char **argv)
61{
62 /* Defaults */
63 int ioclass = 0;
64 int pri = 0;
65 int pid = 0; /* affect own porcess */
66 int opt;
67 enum {
68 OPT_n = 1,
69 OPT_c = 2,
70 OPT_p = 4,
71 };
72
73 /* Numeric params */
Denis Vlasenko4acdb462009-01-31 21:45:57 +000074 /* '+': stop at first non-option */
Denys Vlasenko237bedd2016-07-06 21:58:02 +020075 opt = getopt32(argv, "+n:+c:+p:+", &pri, &ioclass, &pid);
Denis Vlasenko4acdb462009-01-31 21:45:57 +000076 argv += optind;
Denis Vlasenko3266aa92009-04-01 11:24:04 +000077
Denis Vlasenko4acdb462009-01-31 21:45:57 +000078 if (opt & OPT_c) {
79 if (ioclass > 3)
80 bb_error_msg_and_die("bad class %d", ioclass);
81// Do we need this (compat?)?
82// if (ioclass == IOPRIO_CLASS_NONE)
83// ioclass = IOPRIO_CLASS_BE;
84// if (ioclass == IOPRIO_CLASS_IDLE) {
85// //if (opt & OPT_n)
86// // bb_error_msg("ignoring priority for idle class");
87// pri = 7;
88// }
89 }
Denis Vlasenko3266aa92009-04-01 11:24:04 +000090
Denis Vlasenko4acdb462009-01-31 21:45:57 +000091 if (!(opt & (OPT_n|OPT_c))) {
92 if (!(opt & OPT_p) && *argv)
Denys Vlasenko77832482010-08-12 14:14:45 +020093 pid = xatoi_positive(*argv);
Denis Vlasenko4acdb462009-01-31 21:45:57 +000094
95 pri = ioprio_get(IOPRIO_WHO_PROCESS, pid);
96 if (pri == -1)
97 bb_perror_msg_and_die("ioprio_%cet", 'g');
98
99 ioclass = (pri >> IOPRIO_CLASS_SHIFT) & 0x3;
100 pri &= 0xff;
101 printf((ioclass == IOPRIO_CLASS_IDLE) ? "%s\n" : "%s: prio %d\n",
102 nth_string(to_prio, ioclass), pri);
103 } else {
104//printf("pri=%d class=%d val=%x\n",
105//pri, ioclass, pri | (ioclass << IOPRIO_CLASS_SHIFT));
106 pri |= (ioclass << IOPRIO_CLASS_SHIFT);
107 if (ioprio_set(IOPRIO_WHO_PROCESS, pid, pri) == -1)
108 bb_perror_msg_and_die("ioprio_%cet", 's');
Denys Vlasenko41ddd9f2010-06-25 01:46:53 +0200109 if (argv[0]) {
Pascal Bellard21e8e8d2010-07-04 00:57:03 +0200110 BB_EXECVP_or_die(argv);
Denis Vlasenko4acdb462009-01-31 21:45:57 +0000111 }
112 }
113
114 return EXIT_SUCCESS;
115}