blob: 67e101decbaf4ffcd6598697f3af789ad4b3a7d8 [file] [log] [blame]
Mark Salyzyn3bca6062014-01-10 15:37:50 -08001/*
Mark Salyzyn3bca6062014-01-10 15:37:50 -08002** Copyright 2007-2014, The Android Open Source Project
San Mehat10d469b2010-02-25 14:02:55 -08003**
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
San Mehat10d469b2010-02-25 14:02:55 -080017#include <errno.h>
18#include <fcntl.h>
Mark Salyzyn12717162014-04-29 15:49:14 -070019#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
San Mehat10d469b2010-02-25 14:02:55 -080023
24#ifdef HAVE_SCHED_H
25
26#include <cutils/iosched_policy.h>
27
Mark Salyzyn3bca6062014-01-10 15:37:50 -080028#ifdef HAVE_ANDROID_OS
29/* #include <linux/ioprio.h> */
San Mehat10d469b2010-02-25 14:02:55 -080030extern int ioprio_set(int which, int who, int ioprio);
Mark Salyzyn3bca6062014-01-10 15:37:50 -080031extern int ioprio_get(int which, int who);
Mark Salyzyn12717162014-04-29 15:49:14 -070032#define __android_unused
33#else
34#define __android_unused __attribute__((__unused__))
Mark Salyzyn3bca6062014-01-10 15:37:50 -080035#endif
San Mehat10d469b2010-02-25 14:02:55 -080036
37enum {
38 WHO_PROCESS = 1,
39 WHO_PGRP,
40 WHO_USER,
41};
42
43#define CLASS_SHIFT 13
44#define IOPRIO_NORM 4
45
Mark Salyzyn12717162014-04-29 15:49:14 -070046int android_set_ioprio(int pid __android_unused, IoSchedClass clazz __android_unused, int ioprio __android_unused) {
San Mehate2fe2612010-02-26 10:57:17 -080047#ifdef HAVE_ANDROID_OS
San Mehat10d469b2010-02-25 14:02:55 -080048 if (ioprio_set(WHO_PROCESS, pid, ioprio | (clazz << CLASS_SHIFT))) {
49 return -1;
50 }
San Mehate2fe2612010-02-26 10:57:17 -080051#endif
San Mehat10d469b2010-02-25 14:02:55 -080052 return 0;
53}
54
Mark Salyzyn12717162014-04-29 15:49:14 -070055int android_get_ioprio(int pid __android_unused, IoSchedClass *clazz, int *ioprio) {
San Mehate2fe2612010-02-26 10:57:17 -080056#ifdef HAVE_ANDROID_OS
San Mehat10d469b2010-02-25 14:02:55 -080057 int rc;
58
59 if ((rc = ioprio_get(WHO_PROCESS, pid)) < 0) {
60 return -1;
61 }
62
63 *clazz = (rc >> CLASS_SHIFT);
64 *ioprio = (rc & 0xff);
San Mehate2fe2612010-02-26 10:57:17 -080065#else
66 *clazz = IoSchedClass_NONE;
67 *ioprio = 0;
68#endif
San Mehat10d469b2010-02-25 14:02:55 -080069 return 0;
70}
71
72#endif /* HAVE_SCHED_H */