ionice.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * ionice implementation for busybox based on linux-utils-ng 2.14
  4. *
  5. * Copyright (C) 2008 by <u173034@informatik.uni-oldenburg.de>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. //config:config IONICE
  10. //config: bool "ionice (3.8 kb)"
  11. //config: default y
  12. //config: help
  13. //config: Set/set program io scheduling class and priority
  14. //config: Requires kernel >= 2.6.13
  15. //applet:IF_IONICE(APPLET_NOEXEC(ionice, ionice, BB_DIR_BIN, BB_SUID_DROP, ionice))
  16. //kbuild:lib-$(CONFIG_IONICE) += ionice.o
  17. //usage:#define ionice_trivial_usage
  18. //usage: "[-c 1-3] [-n 0-7] [-p PID] [PROG ARGS]"
  19. //usage:#define ionice_full_usage "\n\n"
  20. //usage: "Change I/O priority and class\n"
  21. //usage: "\n -c N Class. 1:realtime 2:best-effort 3:idle"
  22. //usage: "\n -n N Priority"
  23. #include <sys/syscall.h>
  24. #include <asm/unistd.h>
  25. #include "libbb.h"
  26. static int ioprio_set(int which, int who, int ioprio)
  27. {
  28. return syscall(SYS_ioprio_set, which, who, ioprio);
  29. }
  30. static int ioprio_get(int which, int who)
  31. {
  32. return syscall(SYS_ioprio_get, which, who);
  33. }
  34. enum {
  35. IOPRIO_WHO_PROCESS = 1,
  36. IOPRIO_WHO_PGRP,
  37. IOPRIO_WHO_USER
  38. };
  39. enum {
  40. IOPRIO_CLASS_NONE,
  41. IOPRIO_CLASS_RT,
  42. IOPRIO_CLASS_BE,
  43. IOPRIO_CLASS_IDLE
  44. };
  45. static const char to_prio[] ALIGN1 = "none\0realtime\0best-effort\0idle";
  46. #define IOPRIO_CLASS_SHIFT 13
  47. int ionice_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  48. int ionice_main(int argc UNUSED_PARAM, char **argv)
  49. {
  50. /* Defaults */
  51. int ioclass = 0;
  52. int pri = 0;
  53. int pid = 0; /* affect own process */
  54. int opt;
  55. enum {
  56. OPT_n = 1,
  57. OPT_c = 2,
  58. OPT_p = 4,
  59. };
  60. /* Numeric params */
  61. /* '+': stop at first non-option */
  62. opt = getopt32(argv, "+n:+c:+p:+", &pri, &ioclass, &pid);
  63. argv += optind;
  64. if (opt & OPT_c) {
  65. if (ioclass > 3)
  66. bb_error_msg_and_die("bad class %d", ioclass);
  67. // Do we need this (compat?)?
  68. // if (ioclass == IOPRIO_CLASS_NONE)
  69. // ioclass = IOPRIO_CLASS_BE;
  70. // if (ioclass == IOPRIO_CLASS_IDLE) {
  71. // //if (opt & OPT_n)
  72. // // bb_error_msg("ignoring priority for idle class");
  73. // pri = 7;
  74. // }
  75. }
  76. if (!(opt & (OPT_n|OPT_c))) {
  77. if (!(opt & OPT_p) && *argv)
  78. pid = xatoi_positive(*argv);
  79. pri = ioprio_get(IOPRIO_WHO_PROCESS, pid);
  80. if (pri == -1)
  81. bb_perror_msg_and_die("ioprio_%cet", 'g');
  82. ioclass = (pri >> IOPRIO_CLASS_SHIFT) & 0x3;
  83. pri &= 0xff;
  84. printf((ioclass == IOPRIO_CLASS_IDLE) ? "%s\n" : "%s: prio %d\n",
  85. nth_string(to_prio, ioclass), pri);
  86. } else {
  87. //printf("pri=%d class=%d val=%x\n",
  88. //pri, ioclass, pri | (ioclass << IOPRIO_CLASS_SHIFT));
  89. pri |= (ioclass << IOPRIO_CLASS_SHIFT);
  90. if (ioprio_set(IOPRIO_WHO_PROCESS, pid, pri) == -1)
  91. bb_perror_msg_and_die("ioprio_%cet", 's');
  92. if (argv[0]) {
  93. BB_EXECVP_or_die(argv);
  94. }
  95. }
  96. return EXIT_SUCCESS;
  97. }