ioprio.rst 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. ===================
  2. Block io priorities
  3. ===================
  4. Intro
  5. -----
  6. With the introduction of cfq v3 (aka cfq-ts or time sliced cfq), basic io
  7. priorities are supported for reads on files. This enables users to io nice
  8. processes or process groups, similar to what has been possible with cpu
  9. scheduling for ages. This document mainly details the current possibilities
  10. with cfq; other io schedulers do not support io priorities thus far.
  11. Scheduling classes
  12. ------------------
  13. CFQ implements three generic scheduling classes that determine how io is
  14. served for a process.
  15. IOPRIO_CLASS_RT: This is the realtime io class. This scheduling class is given
  16. higher priority than any other in the system, processes from this class are
  17. given first access to the disk every time. Thus it needs to be used with some
  18. care, one io RT process can starve the entire system. Within the RT class,
  19. there are 8 levels of class data that determine exactly how much time this
  20. process needs the disk for on each service. In the future this might change
  21. to be more directly mappable to performance, by passing in a wanted data
  22. rate instead.
  23. IOPRIO_CLASS_BE: This is the best-effort scheduling class, which is the default
  24. for any process that hasn't set a specific io priority. The class data
  25. determines how much io bandwidth the process will get, it's directly mappable
  26. to the cpu nice levels just more coarsely implemented. 0 is the highest
  27. BE prio level, 7 is the lowest. The mapping between cpu nice level and io
  28. nice level is determined as: io_nice = (cpu_nice + 20) / 5.
  29. IOPRIO_CLASS_IDLE: This is the idle scheduling class, processes running at this
  30. level only get io time when no one else needs the disk. The idle class has no
  31. class data, since it doesn't really apply here.
  32. Tools
  33. -----
  34. See below for a sample ionice tool. Usage::
  35. # ionice -c<class> -n<level> -p<pid>
  36. If pid isn't given, the current process is assumed. IO priority settings
  37. are inherited on fork, so you can use ionice to start the process at a given
  38. level::
  39. # ionice -c2 -n0 /bin/ls
  40. will run ls at the best-effort scheduling class at the highest priority.
  41. For a running process, you can give the pid instead::
  42. # ionice -c1 -n2 -p100
  43. will change pid 100 to run at the realtime scheduling class, at priority 2.
  44. ionice.c tool::
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <errno.h>
  48. #include <getopt.h>
  49. #include <unistd.h>
  50. #include <sys/ptrace.h>
  51. #include <asm/unistd.h>
  52. extern int sys_ioprio_set(int, int, int);
  53. extern int sys_ioprio_get(int, int);
  54. #if defined(__i386__)
  55. #define __NR_ioprio_set 289
  56. #define __NR_ioprio_get 290
  57. #elif defined(__ppc__)
  58. #define __NR_ioprio_set 273
  59. #define __NR_ioprio_get 274
  60. #elif defined(__x86_64__)
  61. #define __NR_ioprio_set 251
  62. #define __NR_ioprio_get 252
  63. #elif defined(__ia64__)
  64. #define __NR_ioprio_set 1274
  65. #define __NR_ioprio_get 1275
  66. #else
  67. #error "Unsupported arch"
  68. #endif
  69. static inline int ioprio_set(int which, int who, int ioprio)
  70. {
  71. return syscall(__NR_ioprio_set, which, who, ioprio);
  72. }
  73. static inline int ioprio_get(int which, int who)
  74. {
  75. return syscall(__NR_ioprio_get, which, who);
  76. }
  77. enum {
  78. IOPRIO_CLASS_NONE,
  79. IOPRIO_CLASS_RT,
  80. IOPRIO_CLASS_BE,
  81. IOPRIO_CLASS_IDLE,
  82. };
  83. enum {
  84. IOPRIO_WHO_PROCESS = 1,
  85. IOPRIO_WHO_PGRP,
  86. IOPRIO_WHO_USER,
  87. };
  88. #define IOPRIO_CLASS_SHIFT 13
  89. const char *to_prio[] = { "none", "realtime", "best-effort", "idle", };
  90. int main(int argc, char *argv[])
  91. {
  92. int ioprio = 4, set = 0, ioprio_class = IOPRIO_CLASS_BE;
  93. int c, pid = 0;
  94. while ((c = getopt(argc, argv, "+n:c:p:")) != EOF) {
  95. switch (c) {
  96. case 'n':
  97. ioprio = strtol(optarg, NULL, 10);
  98. set = 1;
  99. break;
  100. case 'c':
  101. ioprio_class = strtol(optarg, NULL, 10);
  102. set = 1;
  103. break;
  104. case 'p':
  105. pid = strtol(optarg, NULL, 10);
  106. break;
  107. }
  108. }
  109. switch (ioprio_class) {
  110. case IOPRIO_CLASS_NONE:
  111. ioprio_class = IOPRIO_CLASS_BE;
  112. break;
  113. case IOPRIO_CLASS_RT:
  114. case IOPRIO_CLASS_BE:
  115. break;
  116. case IOPRIO_CLASS_IDLE:
  117. ioprio = 7;
  118. break;
  119. default:
  120. printf("bad prio class %d\n", ioprio_class);
  121. return 1;
  122. }
  123. if (!set) {
  124. if (!pid && argv[optind])
  125. pid = strtol(argv[optind], NULL, 10);
  126. ioprio = ioprio_get(IOPRIO_WHO_PROCESS, pid);
  127. printf("pid=%d, %d\n", pid, ioprio);
  128. if (ioprio == -1)
  129. perror("ioprio_get");
  130. else {
  131. ioprio_class = ioprio >> IOPRIO_CLASS_SHIFT;
  132. ioprio = ioprio & 0xff;
  133. printf("%s: prio %d\n", to_prio[ioprio_class], ioprio);
  134. }
  135. } else {
  136. if (ioprio_set(IOPRIO_WHO_PROCESS, pid, ioprio | ioprio_class << IOPRIO_CLASS_SHIFT) == -1) {
  137. perror("ioprio_set");
  138. return 1;
  139. }
  140. if (argv[optind])
  141. execvp(argv[optind], &argv[optind]);
  142. }
  143. return 0;
  144. }
  145. March 11 2005, Jens Axboe <jens.axboe@oracle.com>