Home | History | Annotate | Download | only in toolbox
      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <unistd.h>
      4 #include <errno.h>
      5 
      6 #include <cutils/iosched_policy.h>
      7 
      8 static char *classes[] = {"none", "rt", "be", "idle", NULL};
      9 
     10 int ionice_main(int argc, char *argv[])
     11 {
     12     IoSchedClass clazz = IoSchedClass_NONE;
     13     int ioprio = 0;
     14     int pid;
     15 
     16     if(argc != 2 && argc != 4) {
     17         fprintf(stderr, "usage: ionice <pid> [none|rt|be|idle] [prio]\n");
     18         return 1;
     19     }
     20 
     21     if (!(pid = atoi(argv[1]))) {
     22         fprintf(stderr, "Invalid pid specified\n");
     23         return 1;
     24     }
     25 
     26     if (argc == 2) {
     27         if (android_get_ioprio(pid, &clazz, &ioprio)) {
     28             fprintf(stderr, "Failed to read priority (%s)\n", strerror(errno));
     29             return 1;
     30         }
     31         fprintf(stdout, "Pid %d, class %s (%d), prio %d\n", pid, classes[clazz], clazz, ioprio);
     32         return 0;
     33     }
     34 
     35     if (!strcmp(argv[2], "none")) {
     36         clazz = IoSchedClass_NONE;
     37     } else if (!strcmp(argv[2], "rt")) {
     38         clazz = IoSchedClass_RT;
     39     } else if (!strcmp(argv[2], "be")) {
     40         clazz = IoSchedClass_BE;
     41     } else if (!strcmp(argv[2], "idle")) {
     42         clazz = IoSchedClass_IDLE;
     43     } else {
     44         fprintf(stderr, "Unsupported class '%s'\n", argv[2]);
     45         return 1;
     46     }
     47 
     48     ioprio = atoi(argv[3]);
     49 
     50     printf("Setting pid %d i/o class to %d, prio %d\n", pid, clazz, ioprio);
     51     if (android_set_ioprio(pid, clazz, ioprio)) {
     52         fprintf(stderr, "Failed to set priority (%s)\n", strerror(errno));
     53         return 1;
     54     }
     55 
     56     return 0;
     57 }
     58