ionice.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* ionice.c - set or get process I/O scheduling class and priority
  2. *
  3. * Copyright 2015 Rob Landley <rob@landley.net>
  4. *
  5. * It would be really nice if there was a standard, but no. There is
  6. * Documentation/block/ioprio.txt in the linux source.
  7. USE_IONICE(NEWTOY(ionice, "^tc#<0>3=2n#<0>7=5p#", TOYFLAG_USR|TOYFLAG_BIN))
  8. USE_IORENICE(NEWTOY(iorenice, "<1>3", TOYFLAG_USR|TOYFLAG_BIN))
  9. config IONICE
  10. bool "ionice"
  11. default y
  12. help
  13. usage: ionice [-t] [-c CLASS] [-n LEVEL] [COMMAND...|-p PID]
  14. Change the I/O scheduling priority of a process. With no arguments
  15. (or just -p), display process' existing I/O class/priority.
  16. -c CLASS = 1-3: 1(realtime), 2(best-effort, default), 3(when-idle)
  17. -n LEVEL = 0-7: (0 is highest priority, default = 5)
  18. -p Affect existing PID instead of spawning new child
  19. -t Ignore failure to set I/O priority
  20. System default iopriority is generally -c 2 -n 4.
  21. config IORENICE
  22. bool "iorenice"
  23. default y
  24. help
  25. usage: iorenice PID [CLASS] [PRIORITY]
  26. Display or change I/O priority of existing process. CLASS can be
  27. "rt" for realtime, "be" for best effort, "idle" for only when idle, or
  28. "none" to leave it alone. PRIORITY can be 0-7 (0 is highest, default 4).
  29. */
  30. #define FOR_ionice
  31. #include "toys.h"
  32. GLOBALS(
  33. long p, n, c;
  34. )
  35. static int ioprio_get(void)
  36. {
  37. return syscall(__NR_ioprio_get, 1, (int)TT.p);
  38. }
  39. static int ioprio_set(void)
  40. {
  41. int prio = ((int)TT.c << 13) | (int)TT.n;
  42. return syscall(__NR_ioprio_set, 1, (int)TT.p, prio);
  43. }
  44. void ionice_main(void)
  45. {
  46. if (!TT.p && !toys.optc) error_exit("Need -p or COMMAND");
  47. if (toys.optflags == FLAG_p) {
  48. int p = ioprio_get();
  49. xprintf("%s: prio %d\n",
  50. (char *[]){"unknown", "Realtime", "Best-effort", "Idle"}[(p>>13)&3],
  51. p&7);
  52. } else {
  53. if (-1 == ioprio_set() && !FLAG(t)) perror_exit("set");
  54. if (!TT.p) xexec(toys.optargs);
  55. }
  56. }
  57. void iorenice_main(void)
  58. {
  59. char *classes[] = {"none", "rt", "be", "idle"};
  60. TT.p = atolx(*toys.optargs);
  61. if (toys.optc == 1) {
  62. int p = ioprio_get();
  63. if (p == -1) perror_exit("read priority");
  64. TT.c = (p>>13)&3;
  65. p &= 7;
  66. xprintf("Pid %ld, class %s (%ld), prio %d\n", TT.p, classes[TT.c], TT.c, p);
  67. return;
  68. }
  69. for (TT.c = 0; TT.c<4; TT.c++)
  70. if (!strcmp(toys.optargs[toys.optc-1], classes[TT.c])) break;
  71. if (toys.optc == 3 || TT.c == 4) TT.n = atolx(toys.optargs[1]);
  72. else TT.n = 4;
  73. TT.c &= 3;
  74. if (-1 == ioprio_set()) perror_exit("set");
  75. }