2009-02-01 03:15:57 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* ionice implementation for busybox based on linux-utils-ng 2.14
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 by <u173034@informatik.uni-oldenburg.de>
|
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2009-02-01 03:15:57 +05:30
|
|
|
*/
|
2016-11-23 03:44:24 +05:30
|
|
|
//config:config IONICE
|
2018-12-28 07:50:17 +05:30
|
|
|
//config: bool "ionice (3.8 kb)"
|
2016-11-23 03:44:24 +05:30
|
|
|
//config: default y
|
|
|
|
//config: select PLATFORM_LINUX
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Set/set program io scheduling class and priority
|
|
|
|
//config: Requires kernel >= 2.6.13
|
2009-02-01 03:15:57 +05:30
|
|
|
|
2017-08-04 23:25:01 +05:30
|
|
|
//applet:IF_IONICE(APPLET_NOEXEC(ionice, ionice, BB_DIR_BIN, BB_SUID_DROP, ionice))
|
2016-11-23 04:24:17 +05:30
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_IONICE) += ionice.o
|
|
|
|
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define ionice_trivial_usage
|
|
|
|
//usage: "[-c 1-3] [-n 0-7] [-p PID] [PROG]"
|
|
|
|
//usage:#define ionice_full_usage "\n\n"
|
|
|
|
//usage: "Change I/O priority and class\n"
|
|
|
|
//usage: "\n -c Class. 1:realtime 2:best-effort 3:idle"
|
|
|
|
//usage: "\n -n Priority"
|
|
|
|
|
2009-02-01 03:15:57 +05:30
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <asm/unistd.h>
|
|
|
|
#include "libbb.h"
|
|
|
|
|
|
|
|
static int ioprio_set(int which, int who, int ioprio)
|
|
|
|
{
|
|
|
|
return syscall(SYS_ioprio_set, which, who, ioprio);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ioprio_get(int which, int who)
|
|
|
|
{
|
|
|
|
return syscall(SYS_ioprio_get, which, who);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum {
|
|
|
|
IOPRIO_WHO_PROCESS = 1,
|
|
|
|
IOPRIO_WHO_PGRP,
|
|
|
|
IOPRIO_WHO_USER
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
IOPRIO_CLASS_NONE,
|
|
|
|
IOPRIO_CLASS_RT,
|
|
|
|
IOPRIO_CLASS_BE,
|
|
|
|
IOPRIO_CLASS_IDLE
|
|
|
|
};
|
|
|
|
|
2016-04-22 21:39:21 +05:30
|
|
|
static const char to_prio[] ALIGN1 = "none\0realtime\0best-effort\0idle";
|
2009-02-01 03:15:57 +05:30
|
|
|
|
|
|
|
#define IOPRIO_CLASS_SHIFT 13
|
|
|
|
|
|
|
|
int ionice_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int ionice_main(int argc UNUSED_PARAM, char **argv)
|
|
|
|
{
|
|
|
|
/* Defaults */
|
|
|
|
int ioclass = 0;
|
|
|
|
int pri = 0;
|
|
|
|
int pid = 0; /* affect own porcess */
|
|
|
|
int opt;
|
|
|
|
enum {
|
|
|
|
OPT_n = 1,
|
|
|
|
OPT_c = 2,
|
|
|
|
OPT_p = 4,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Numeric params */
|
|
|
|
/* '+': stop at first non-option */
|
2016-07-07 01:28:02 +05:30
|
|
|
opt = getopt32(argv, "+n:+c:+p:+", &pri, &ioclass, &pid);
|
2009-02-01 03:15:57 +05:30
|
|
|
argv += optind;
|
2009-04-01 16:54:04 +05:30
|
|
|
|
2009-02-01 03:15:57 +05:30
|
|
|
if (opt & OPT_c) {
|
|
|
|
if (ioclass > 3)
|
|
|
|
bb_error_msg_and_die("bad class %d", ioclass);
|
|
|
|
// Do we need this (compat?)?
|
|
|
|
// if (ioclass == IOPRIO_CLASS_NONE)
|
|
|
|
// ioclass = IOPRIO_CLASS_BE;
|
|
|
|
// if (ioclass == IOPRIO_CLASS_IDLE) {
|
|
|
|
// //if (opt & OPT_n)
|
|
|
|
// // bb_error_msg("ignoring priority for idle class");
|
|
|
|
// pri = 7;
|
|
|
|
// }
|
|
|
|
}
|
2009-04-01 16:54:04 +05:30
|
|
|
|
2009-02-01 03:15:57 +05:30
|
|
|
if (!(opt & (OPT_n|OPT_c))) {
|
|
|
|
if (!(opt & OPT_p) && *argv)
|
2010-08-12 17:44:45 +05:30
|
|
|
pid = xatoi_positive(*argv);
|
2009-02-01 03:15:57 +05:30
|
|
|
|
|
|
|
pri = ioprio_get(IOPRIO_WHO_PROCESS, pid);
|
|
|
|
if (pri == -1)
|
|
|
|
bb_perror_msg_and_die("ioprio_%cet", 'g');
|
|
|
|
|
|
|
|
ioclass = (pri >> IOPRIO_CLASS_SHIFT) & 0x3;
|
|
|
|
pri &= 0xff;
|
|
|
|
printf((ioclass == IOPRIO_CLASS_IDLE) ? "%s\n" : "%s: prio %d\n",
|
|
|
|
nth_string(to_prio, ioclass), pri);
|
|
|
|
} else {
|
|
|
|
//printf("pri=%d class=%d val=%x\n",
|
|
|
|
//pri, ioclass, pri | (ioclass << IOPRIO_CLASS_SHIFT));
|
|
|
|
pri |= (ioclass << IOPRIO_CLASS_SHIFT);
|
|
|
|
if (ioprio_set(IOPRIO_WHO_PROCESS, pid, pri) == -1)
|
|
|
|
bb_perror_msg_and_die("ioprio_%cet", 's');
|
2010-06-25 05:16:53 +05:30
|
|
|
if (argv[0]) {
|
2010-07-04 04:27:03 +05:30
|
|
|
BB_EXECVP_or_die(argv);
|
2009-02-01 03:15:57 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|