blob: 5d90a01e7bc4df91bbb74db39e8efaa410afa23f [file] [log] [blame]
Mark Salyzyn3bca6062014-01-10 15:37:50 -08001/*
San Mehat10d469b2010-02-25 14:02:55 -08002**
Mark Salyzyn3bca6062014-01-10 15:37:50 -08003** Copyright 2007-2014, The Android Open Source Project
San Mehat10d469b2010-02-25 14:02:55 -08004**
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
18#include <stdio.h>
19#include <stdlib.h>
20#include <unistd.h>
21#include <string.h>
22#include <errno.h>
23#include <fcntl.h>
San Mehat10d469b2010-02-25 14:02:55 -080024
25#ifdef HAVE_SCHED_H
26
27#include <cutils/iosched_policy.h>
28
Mark Salyzyn3bca6062014-01-10 15:37:50 -080029#ifdef HAVE_ANDROID_OS
30/* #include <linux/ioprio.h> */
San Mehat10d469b2010-02-25 14:02:55 -080031extern int ioprio_set(int which, int who, int ioprio);
Mark Salyzyn3bca6062014-01-10 15:37:50 -080032extern int ioprio_get(int which, int who);
33#endif
San Mehat10d469b2010-02-25 14:02:55 -080034
35enum {
36 WHO_PROCESS = 1,
37 WHO_PGRP,
38 WHO_USER,
39};
40
41#define CLASS_SHIFT 13
42#define IOPRIO_NORM 4
43
44int android_set_ioprio(int pid, IoSchedClass clazz, int ioprio) {
San Mehate2fe2612010-02-26 10:57:17 -080045#ifdef HAVE_ANDROID_OS
San Mehat10d469b2010-02-25 14:02:55 -080046 if (ioprio_set(WHO_PROCESS, pid, ioprio | (clazz << CLASS_SHIFT))) {
47 return -1;
48 }
San Mehate2fe2612010-02-26 10:57:17 -080049#endif
San Mehat10d469b2010-02-25 14:02:55 -080050 return 0;
51}
52
53int android_get_ioprio(int pid, IoSchedClass *clazz, int *ioprio) {
San Mehate2fe2612010-02-26 10:57:17 -080054#ifdef HAVE_ANDROID_OS
San Mehat10d469b2010-02-25 14:02:55 -080055 int rc;
56
57 if ((rc = ioprio_get(WHO_PROCESS, pid)) < 0) {
58 return -1;
59 }
60
61 *clazz = (rc >> CLASS_SHIFT);
62 *ioprio = (rc & 0xff);
San Mehate2fe2612010-02-26 10:57:17 -080063#else
64 *clazz = IoSchedClass_NONE;
65 *ioprio = 0;
66#endif
San Mehat10d469b2010-02-25 14:02:55 -080067 return 0;
68}
69
70#endif /* HAVE_SCHED_H */