iosched_policy.c revision e2fe261d1ac775e9e6501bf976cd4fef4c1cf5dd
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#include <linux/unistd.h>
26
27#ifdef HAVE_SCHED_H
28
29#include <cutils/iosched_policy.h>
30
31extern int ioprio_set(int which, int who, int ioprio);
32
33enum {
34    WHO_PROCESS = 1,
35    WHO_PGRP,
36    WHO_USER,
37};
38
39#define CLASS_SHIFT 13
40#define IOPRIO_NORM 4
41
42int android_set_ioprio(int pid, IoSchedClass clazz, int ioprio) {
43#ifdef HAVE_ANDROID_OS
44    if (ioprio_set(WHO_PROCESS, pid, ioprio | (clazz << CLASS_SHIFT))) {
45        return -1;
46    }
47#endif
48    return 0;
49}
50
51int android_get_ioprio(int pid, IoSchedClass *clazz, int *ioprio) {
52#ifdef HAVE_ANDROID_OS
53    int rc;
54
55    if ((rc = ioprio_get(WHO_PROCESS, pid)) < 0) {
56        return -1;
57    }
58
59    *clazz = (rc >> CLASS_SHIFT);
60    *ioprio = (rc & 0xff);
61#else
62    *clazz = IoSchedClass_NONE;
63    *ioprio = 0;
64#endif
65    return 0;
66}
67
68#endif /* HAVE_SCHED_H */
69