3
0
forked from pool/util-linux
util-linux/util-linux-2.12q-ionice.diff

250 lines
6.5 KiB
Diff

--- util-linux-2.12q/sys-utils/Makefile
+++ util-linux-2.12q/sys-utils/Makefile
@@ -8,7 +8,7 @@
# Where to put man pages?
-MAN1= arch.1 flock.1 readprofile.1
+MAN1= arch.1 flock.1 readprofile.1 ionice.1
MAN8= ctrlaltdel.8 cytune.8 dmesg.8 \
ipcrm.8 ipcs.8 renice.8 \
@@ -19,7 +19,7 @@
BIN= arch dmesg
-USRBIN= cytune flock ipcrm ipcs renice setsid
+USRBIN= cytune flock ipcrm ipcs renice setsid ionice
USRSBIN= readprofile tunelp
@@ -71,6 +71,7 @@
ctrlaltdel: ctrlaltdel.o $(LIB)/my_reboot.o
cytune.o: cytune.c cyclades.h
cytune: cytune.o
+ionice: ionice.o
ipcrm: ipcrm.o
ipcs: ipcs.o
rdev: rdev.o
--- util-linux-2.12q/sys-utils/ionice.1
+++ util-linux-2.12q/sys-utils/ionice.1
@@ -0,0 +1,71 @@
+.TH ionice "1" "August 2005" ionice
+.SH NAME
+ionice \- get/set program io scheduling class and priority
+.SH SYNOPSIS
+.B ionice
+[\fI-c\fR] \fI[-n\fR] [\fI-p\fR] [COMMAND [ARG...]]
+
+.SH DESCRIPTION
+This program sets the io scheduling class and priority for a program. As of
+this writing, Linux supports 3 scheduling classes:
+
+\fBIdle\fR.
+A program running with idle io priority will only get disk time when no other
+program has asked for disk io for a defined grace period. The impact of idle
+io processes on normal system activity should be zero. This scheduling
+class does not take a priority argument.
+
+\fBBest effort\fR.
+This is the default scheduling class for any process that hasn't asked for
+a specific io priority. Programs inherit the CPU nice setting for io
+priorities. This class takes a priority argument from \fI0-7\fR, with lower
+number being higher priority. Programs running at the same best effort
+priority are served in a round-robin fashion.
+
+\fBReal time\fR.
+The RT scheduling class is given first access to the disk, regardless of
+what else is going on in the system. Thus the RT class needs to be used with
+some care, as it can starve other processes. As with the best effort class,
+8 priority levels are defined denoting how big a time slice a given process
+will receive on each scheduling window.
+
+If no arguments or just \fI-p\fR is given, \fIionice\fR will query the
+current io scheduling class and priority for that process.
+
+.SH OPTIONS
+.LP
+.TP 7
+\fB-c\fP
+The scheduling class. 1 for real time, 2 for best-effort, 3 for idle.
+.TP 7
+\fB-n\fP
+The scheduling class data. This defines the class data, if the class
+accepts an argument. For real time and best-effort, \fI0-7\fR is valid
+data.
+.TP 7
+\fB-p\fP
+Pass in a process pid to change an already running process. If this argument
+is not given, \fBionice\fP will run the listed program with the given
+parameters.
+
+.SH EXAMPLES
+.LP
+.TP 7
+# \fBionice\fP -c3 -p89
+.TP 7
+Sets process with PID 89 as an idle io process.
+.TP 7
+# \fBionice\fP -c2 -n0 bash
+.TP 7
+Runs 'bash' as a best-effort program with highest priority.
+.TP 7
+# \fBionice\fP -p89
+.TP 7
+Returns the class and priority of the process with PID 89.
+
+.SH NOTES
+Linux supports io scheduling priorities and classes since 2.6.13 with the CFQ
+io scheduler.
+
+.SH AUTHORS
+Jens Axboe <axboe@suse.de>
--- util-linux-2.12q/sys-utils/ionice.c
+++ util-linux-2.12q/sys-utils/ionice.c
@@ -0,0 +1,144 @@
+/*
+ * ionice: set or get process io scheduling class and priority
+ *
+ * Copyright (C) 2005 Jens Axboe <axboe@suse.de> SUSE Labs
+ *
+ * Released under the terms of the GNU General Public License version 2
+ *
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <getopt.h>
+#include <unistd.h>
+#include <sys/ptrace.h>
+#include <sys/syscall.h>
+#include <asm/unistd.h>
+
+#if defined(__i386__)
+#define __NR_ioprio_set 289
+#define __NR_ioprio_get 290
+#elif defined(__powerpc__) || defined(__powerpc64__)
+#define __NR_ioprio_set 273
+#define __NR_ioprio_get 274
+#elif defined(__x86_64__)
+#define __NR_ioprio_set 251
+#define __NR_ioprio_get 252
+#elif defined(__ia64__)
+#define __NR_ioprio_set 1274
+#define __NR_ioprio_get 1275
+#elif defined(__alpha__)
+#define __NR_ioprio_set 442
+#define __NR_ioprio_get 443
+#elif defined(__s390__) || defined(__s390x__)
+#define __NR_ioprio_set 282
+#define __NR_ioprio_get 283
+#elif defined(__arm__)
+#define __NR_ioprio_set 314
+#define __NR_ioprio_get 315
+#else
+#error "Unsupported arch"
+#endif
+
+static int ioprio_set(int which, int who, int ioprio)
+{
+ return syscall(__NR_ioprio_set, which, who, ioprio);
+}
+
+static int ioprio_get(int which, int who)
+{
+ return syscall(__NR_ioprio_get, which, who);
+}
+
+enum {
+ IOPRIO_CLASS_NONE,
+ IOPRIO_CLASS_RT,
+ IOPRIO_CLASS_BE,
+ IOPRIO_CLASS_IDLE,
+};
+
+enum {
+ IOPRIO_WHO_PROCESS = 1,
+ IOPRIO_WHO_PGRP,
+ IOPRIO_WHO_USER,
+};
+
+#define IOPRIO_CLASS_SHIFT 13
+
+const char *to_prio[] = { "none", "realtime", "best-effort", "idle", };
+
+static void usage(void)
+{
+ printf("Usage: ionice [OPTIONS] [COMMAND [ARG]...]\n");
+ printf("Sets or gets process io scheduling class and priority.\n");
+ printf("\n\t-n\tClass data (typically 0-7, lower being higher prio)\n");
+ printf("\t-c\tScheduling class\n");
+ printf("\t\t\t1: realtime, 2: best-effort, 3: idle\n");
+ printf("\t-p\tProcess pid\n");
+ printf("\t-h\tThis help page\n");
+ printf("\nJens Axboe <axboe@suse.de> (C) 2005\n");
+}
+
+int main(int argc, char *argv[])
+{
+ int ioprio = 4, set = 0, ioprio_class = IOPRIO_CLASS_BE;
+ int c, pid = 0;
+
+ while ((c = getopt(argc, argv, "+n:c:p:h")) != EOF) {
+ switch (c) {
+ case 'n':
+ ioprio = strtol(optarg, NULL, 10);
+ set = 1;
+ break;
+ case 'c':
+ ioprio_class = strtol(optarg, NULL, 10);
+ set = 1;
+ break;
+ case 'p':
+ pid = strtol(optarg, NULL, 10);
+ break;
+ case 'h':
+ default:
+ usage();
+ exit(0);
+ }
+ }
+
+ switch (ioprio_class) {
+ case IOPRIO_CLASS_NONE:
+ ioprio_class = IOPRIO_CLASS_BE;
+ break;
+ case IOPRIO_CLASS_RT:
+ case IOPRIO_CLASS_BE:
+ break;
+ case IOPRIO_CLASS_IDLE:
+ ioprio = 7;
+ break;
+ default:
+ printf("bad prio class %d\n", ioprio_class);
+ return 1;
+ }
+
+ if (!set) {
+ if (!pid && argv[optind])
+ pid = strtol(argv[optind], NULL, 10);
+
+ ioprio = ioprio_get(IOPRIO_WHO_PROCESS, pid);
+
+ if (ioprio == -1)
+ perror("ioprio_get");
+ else {
+ ioprio_class = ioprio >> IOPRIO_CLASS_SHIFT;
+ ioprio = ioprio & 0xff;
+ printf("%s: prio %d\n", to_prio[ioprio_class], ioprio);
+ }
+ } else {
+ if (ioprio_set(IOPRIO_WHO_PROCESS, pid, ioprio | ioprio_class << IOPRIO_CLASS_SHIFT) == -1)
+ perror("ioprio_set");
+
+ if (argv[optind])
+ execvp(argv[optind], &argv[optind]);
+ }
+
+ return 0;
+}