9 With the introduction of cfq v3 (aka cfq-ts or time sliced cfq), basic io
10 priorities are supported for reads on files. This enables users to io nice
11 processes or process groups, similar to what has been possible with cpu
12 scheduling for ages. This document mainly details the current possibilities
13 with cfq; other io schedulers do not support io priorities thus far.
18 CFQ implements three generic scheduling classes that determine how io is
21 IOPRIO_CLASS_RT: This is the realtime io class. This scheduling class is given
22 higher priority than any other in the system, processes from this class are
23 given first access to the disk every time. Thus it needs to be used with some
24 care, one io RT process can starve the entire system. Within the RT class,
25 there are 8 levels of class data that determine exactly how much time this
26 process needs the disk for on each service. In the future this might change
27 to be more directly mappable to performance, by passing in a wanted data
30 IOPRIO_CLASS_BE: This is the best-effort scheduling class, which is the default
31 for any process that hasn't set a specific io priority. The class data
32 determines how much io bandwidth the process will get, it's directly mappable
33 to the cpu nice levels just more coarsely implemented. 0 is the highest
34 BE prio level, 7 is the lowest. The mapping between cpu nice level and io
35 nice level is determined as: io_nice = (cpu_nice + 20) / 5.
37 IOPRIO_CLASS_IDLE: This is the idle scheduling class, processes running at this
38 level only get io time when no one else needs the disk. The idle class has no
39 class data, since it doesn't really apply here.
44 See below for a sample ionice tool. Usage::
46 # ionice -c<class> -n<level> -p<pid>
48 If pid isn't given, the current process is assumed. IO priority settings
49 are inherited on fork, so you can use ionice to start the process at a given
52 # ionice -c2 -n0 /bin/ls
54 will run ls at the best-effort scheduling class at the highest priority.
55 For a running process, you can give the pid instead::
57 # ionice -c1 -n2 -p100
59 will change pid 100 to run at the realtime scheduling class, at priority 2.
68 #include <sys/ptrace.h>
69 #include <asm/unistd.h>
71 extern int sys_ioprio_set(int, int, int);
72 extern int sys_ioprio_get(int, int);
75 #define __NR_ioprio_set 289
76 #define __NR_ioprio_get 290
77 #elif defined(__ppc__)
78 #define __NR_ioprio_set 273
79 #define __NR_ioprio_get 274
80 #elif defined(__x86_64__)
81 #define __NR_ioprio_set 251
82 #define __NR_ioprio_get 252
83 #elif defined(__ia64__)
84 #define __NR_ioprio_set 1274
85 #define __NR_ioprio_get 1275
87 #error "Unsupported arch"
90 static inline int ioprio_set(int which, int who, int ioprio)
92 return syscall(__NR_ioprio_set, which, who, ioprio);
95 static inline int ioprio_get(int which, int who)
97 return syscall(__NR_ioprio_get, which, who);
108 IOPRIO_WHO_PROCESS = 1,
113 #define IOPRIO_CLASS_SHIFT 13
115 const char *to_prio[] = { "none", "realtime", "best-effort", "idle", };
117 int main(int argc, char *argv[])
119 int ioprio = 4, set = 0, ioprio_class = IOPRIO_CLASS_BE;
122 while ((c = getopt(argc, argv, "+n:c:p:")) != EOF) {
125 ioprio = strtol(optarg, NULL, 10);
129 ioprio_class = strtol(optarg, NULL, 10);
133 pid = strtol(optarg, NULL, 10);
138 switch (ioprio_class) {
139 case IOPRIO_CLASS_NONE:
140 ioprio_class = IOPRIO_CLASS_BE;
142 case IOPRIO_CLASS_RT:
143 case IOPRIO_CLASS_BE:
145 case IOPRIO_CLASS_IDLE:
149 printf("bad prio class %d\n", ioprio_class);
154 if (!pid && argv[optind])
155 pid = strtol(argv[optind], NULL, 10);
157 ioprio = ioprio_get(IOPRIO_WHO_PROCESS, pid);
159 printf("pid=%d, %d\n", pid, ioprio);
162 perror("ioprio_get");
164 ioprio_class = ioprio >> IOPRIO_CLASS_SHIFT;
165 ioprio = ioprio & 0xff;
166 printf("%s: prio %d\n", to_prio[ioprio_class], ioprio);
169 if (ioprio_set(IOPRIO_WHO_PROCESS, pid, ioprio | ioprio_class << IOPRIO_CLASS_SHIFT) == -1) {
170 perror("ioprio_set");
175 execvp(argv[optind], &argv[optind]);
182 March 11 2005, Jens Axboe <jens.axboe@oracle.com>