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