1IOPRIO_SET(2) Linux Programmer's Manual IOPRIO_SET(2)
2
3
4
6 ioprio_get, ioprio_set - get/set I/O scheduling class and priority
7
9 #include <linux/ioprio.h> /* Definition of IOPRIO_* constants */
10 #include <sys/syscall.h> /* Definition of SYS_* constants */
11 #include <unistd.h>
12
13 int syscall(SYS_ioprio_get, int which, int who);
14 int syscall(SYS_ioprio_set, int which, int who, int ioprio);
15
16 Note: glibc provides no wrappers for these system calls, necessitating
17 the use of syscall(2).
18
20 The ioprio_get() and ioprio_set() system calls get and set the I/O
21 scheduling class and priority of one or more threads.
22
23 The which and who arguments identify the thread(s) on which the system
24 calls operate. The which argument determines how who is interpreted,
25 and has one of the following values:
26
27 IOPRIO_WHO_PROCESS
28 who is a process ID or thread ID identifying a single process or
29 thread. If who is 0, then operate on the calling thread.
30
31 IOPRIO_WHO_PGRP
32 who is a process group ID identifying all the members of a
33 process group. If who is 0, then operate on the process group
34 of which the caller is a member.
35
36 IOPRIO_WHO_USER
37 who is a user ID identifying all of the processes that have a
38 matching real UID.
39
40 If which is specified as IOPRIO_WHO_PGRP or IOPRIO_WHO_USER when call‐
41 ing ioprio_get(), and more than one process matches who, then the re‐
42 turned priority will be the highest one found among all of the matching
43 processes. One priority is said to be higher than another one if it
44 belongs to a higher priority class (IOPRIO_CLASS_RT is the highest pri‐
45 ority class; IOPRIO_CLASS_IDLE is the lowest) or if it belongs to the
46 same priority class as the other process but has a higher priority
47 level (a lower priority number means a higher priority level).
48
49 The ioprio argument given to ioprio_set() is a bit mask that specifies
50 both the scheduling class and the priority to be assigned to the target
51 process(es). The following macros are used for assembling and dissect‐
52 ing ioprio values:
53
54 IOPRIO_PRIO_VALUE(class, data)
55 Given a scheduling class and priority (data), this macro com‐
56 bines the two values to produce an ioprio value, which is re‐
57 turned as the result of the macro.
58
59 IOPRIO_PRIO_CLASS(mask)
60 Given mask (an ioprio value), this macro returns its I/O class
61 component, that is, one of the values IOPRIO_CLASS_RT, IO‐
62 PRIO_CLASS_BE, or IOPRIO_CLASS_IDLE.
63
64 IOPRIO_PRIO_DATA(mask)
65 Given mask (an ioprio value), this macro returns its priority
66 (data) component.
67
68 See the NOTES section for more information on scheduling classes and
69 priorities, as well as the meaning of specifying ioprio as 0.
70
71 I/O priorities are supported for reads and for synchronous (O_DIRECT,
72 O_SYNC) writes. I/O priorities are not supported for asynchronous
73 writes because they are issued outside the context of the program
74 dirtying the memory, and thus program-specific priorities do not apply.
75
77 On success, ioprio_get() returns the ioprio value of the process with
78 highest I/O priority of any of the processes that match the criteria
79 specified in which and who. On error, -1 is returned, and errno is set
80 to indicate the error.
81
82 On success, ioprio_set() returns 0. On error, -1 is returned, and er‐
83 rno is set to indicate the error.
84
86 EINVAL Invalid value for which or ioprio. Refer to the NOTES section
87 for available scheduler classes and priority levels for ioprio.
88
89 EPERM The calling process does not have the privilege needed to assign
90 this ioprio to the specified process(es). See the NOTES section
91 for more information on required privileges for ioprio_set().
92
93 ESRCH No process(es) could be found that matched the specification in
94 which and who.
95
97 These system calls have been available on Linux since kernel 2.6.13.
98
100 These system calls are Linux-specific.
101
103 Two or more processes or threads can share an I/O context. This will
104 be the case when clone(2) was called with the CLONE_IO flag. However,
105 by default, the distinct threads of a process will not share the same
106 I/O context. This means that if you want to change the I/O priority of
107 all threads in a process, you may need to call ioprio_set() on each of
108 the threads. The thread ID that you would need for this operation is
109 the one that is returned by gettid(2) or clone(2).
110
111 These system calls have an effect only when used in conjunction with an
112 I/O scheduler that supports I/O priorities. As at kernel 2.6.17 the
113 only such scheduler is the Completely Fair Queuing (CFQ) I/O scheduler.
114
115 If no I/O scheduler has been set for a thread, then by default the I/O
116 priority will follow the CPU nice value (setpriority(2)). In Linux
117 kernels before version 2.6.24, once an I/O priority had been set using
118 ioprio_set(), there was no way to reset the I/O scheduling behavior to
119 the default. Since Linux 2.6.24, specifying ioprio as 0 can be used to
120 reset to the default I/O scheduling behavior.
121
122 Selecting an I/O scheduler
123 I/O schedulers are selected on a per-device basis via the special file
124 /sys/block/<device>/queue/scheduler.
125
126 One can view the current I/O scheduler via the /sys filesystem. For
127 example, the following command displays a list of all schedulers cur‐
128 rently loaded in the kernel:
129
130 $ cat /sys/block/sda/queue/scheduler
131 noop anticipatory deadline [cfq]
132
133 The scheduler surrounded by brackets is the one actually in use for the
134 device (sda in the example). Setting another scheduler is done by
135 writing the name of the new scheduler to this file. For example, the
136 following command will set the scheduler for the sda device to cfq:
137
138 $ su
139 Password:
140 # echo cfq > /sys/block/sda/queue/scheduler
141
142 The Completely Fair Queuing (CFQ) I/O scheduler
143 Since version 3 (also known as CFQ Time Sliced), CFQ implements I/O
144 nice levels similar to those of CPU scheduling. These nice levels are
145 grouped into three scheduling classes, each one containing one or more
146 priority levels:
147
148 IOPRIO_CLASS_RT (1)
149 This is the real-time I/O class. This scheduling class is given
150 higher priority than any other class: processes from this class
151 are given first access to the disk every time. Thus, this I/O
152 class needs to be used with some care: one I/O real-time process
153 can starve the entire system. Within the real-time class, there
154 are 8 levels of class data (priority) that determine exactly how
155 much time this process needs the disk for on each service. The
156 highest real-time priority level is 0; the lowest is 7. In the
157 future, this might change to be more directly mappable to per‐
158 formance, by passing in a desired data rate instead.
159
160 IOPRIO_CLASS_BE (2)
161 This is the best-effort scheduling class, which is the default
162 for any process that hasn't set a specific I/O priority. The
163 class data (priority) determines how much I/O bandwidth the
164 process will get. Best-effort priority levels are analogous to
165 CPU nice values (see getpriority(2)). The priority level deter‐
166 mines a priority relative to other processes in the best-effort
167 scheduling class. Priority levels range from 0 (highest) to 7
168 (lowest).
169
170 IOPRIO_CLASS_IDLE (3)
171 This is the idle scheduling class. Processes running at this
172 level get I/O time only when no one else needs the disk. The
173 idle class has no class data. Attention is required when as‐
174 signing this priority class to a process, since it may become
175 starved if higher priority processes are constantly accessing
176 the disk.
177
178 Refer to the kernel source file Documentation/block/ioprio.txt for more
179 information on the CFQ I/O Scheduler and an example program.
180
181 Required permissions to set I/O priorities
182 Permission to change a process's priority is granted or denied based on
183 two criteria:
184
185 Process ownership
186 An unprivileged process may set the I/O priority only for a
187 process whose real UID matches the real or effective UID of the
188 calling process. A process which has the CAP_SYS_NICE capabil‐
189 ity can change the priority of any process.
190
191 What is the desired priority
192 Attempts to set very high priorities (IOPRIO_CLASS_RT) require
193 the CAP_SYS_ADMIN capability. Kernel versions up to 2.6.24 also
194 required CAP_SYS_ADMIN to set a very low priority (IO‐
195 PRIO_CLASS_IDLE), but since Linux 2.6.25, this is no longer re‐
196 quired.
197
198 A call to ioprio_set() must follow both rules, or the call will fail
199 with the error EPERM.
200
202 Glibc does not yet provide a suitable header file defining the function
203 prototypes and macros described on this page. Suitable definitions can
204 be found in linux/ioprio.h.
205
207 ionice(1), getpriority(2), open(2), capabilities(7), cgroups(7)
208
209 Documentation/block/ioprio.txt in the Linux kernel source tree
210
212 This page is part of release 5.13 of the Linux man-pages project. A
213 description of the project, information about reporting bugs, and the
214 latest version of this page, can be found at
215 https://www.kernel.org/doc/man-pages/.
216
217
218
219Linux 2021-06-20 IOPRIO_SET(2)