iosched_policy.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* libs/cutils/iosched_policy.c
  2. **
  3. ** Copyright 2007, The Android Open Source Project
  4. **
  5. ** Licensed under the Apache License, Version 2.0 (the "License");
  6. ** you may not use this file except in compliance with the License.
  7. ** You may obtain a copy of the License at
  8. **
  9. ** http://www.apache.org/licenses/LICENSE-2.0
  10. **
  11. ** Unless required by applicable law or agreed to in writing, software
  12. ** distributed under the License is distributed on an "AS IS" BASIS,
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ** See the License for the specific language governing permissions and
  15. ** limitations under the License.
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <fcntl.h>
  23. #ifdef HAVE_SCHED_H
  24. #include <cutils/iosched_policy.h>
  25. extern int ioprio_set(int which, int who, int ioprio);
  26. enum {
  27. WHO_PROCESS = 1,
  28. WHO_PGRP,
  29. WHO_USER,
  30. };
  31. #define CLASS_SHIFT 13
  32. #define IOPRIO_NORM 4
  33. int android_set_ioprio(int pid, IoSchedClass clazz, int ioprio) {
  34. #ifdef HAVE_ANDROID_OS
  35. if (ioprio_set(WHO_PROCESS, pid, ioprio | (clazz << CLASS_SHIFT))) {
  36. return -1;
  37. }
  38. #endif
  39. return 0;
  40. }
  41. int android_get_ioprio(int pid, IoSchedClass *clazz, int *ioprio) {
  42. #ifdef HAVE_ANDROID_OS
  43. int rc;
  44. if ((rc = ioprio_get(WHO_PROCESS, pid)) < 0) {
  45. return -1;
  46. }
  47. *clazz = (rc >> CLASS_SHIFT);
  48. *ioprio = (rc & 0xff);
  49. #else
  50. *clazz = IoSchedClass_NONE;
  51. *ioprio = 0;
  52. #endif
  53. return 0;
  54. }
  55. #endif /* HAVE_SCHED_H */