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