blob: 012c537ac6884ec93768aa2956f06f76f6314215 [file] [log] [blame]
Mark Salyzyn3bca6062014-01-10 15:37:50 -08001/*
Elliott Hughesaf98efb2015-04-02 13:36:54 -07002** Copyright 2007, 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
Elliott Hughes8e9aeb92017-11-10 10:22:07 -080017#include <cutils/iosched_policy.h>
18
San Mehat10d469b2010-02-25 14:02:55 -080019#include <errno.h>
20#include <fcntl.h>
Mark Salyzyn12717162014-04-29 15:49:14 -070021#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
San Mehat10d469b2010-02-25 14:02:55 -080025
Elliott Hughes9b828ad2015-07-30 08:47:35 -070026#if defined(__ANDROID__)
Christopher Ferris84421d82016-07-18 16:19:25 -070027#define IOPRIO_WHO_PROCESS (1)
28#define IOPRIO_CLASS_SHIFT (13)
Dan Albert5e541bf2014-06-12 21:06:58 -070029#include <sys/syscall.h>
Mark Salyzyn12717162014-04-29 15:49:14 -070030#define __android_unused
31#else
32#define __android_unused __attribute__((__unused__))
Mark Salyzyn3bca6062014-01-10 15:37:50 -080033#endif
San Mehat10d469b2010-02-25 14:02:55 -080034
Mark Salyzyn12717162014-04-29 15:49:14 -070035int android_set_ioprio(int pid __android_unused, IoSchedClass clazz __android_unused, int ioprio __android_unused) {
Elliott Hughes9b828ad2015-07-30 08:47:35 -070036#if defined(__ANDROID__)
Dan Albert167c0ed2014-06-12 17:53:03 -070037 if (syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, pid, ioprio | (clazz << IOPRIO_CLASS_SHIFT))) {
San Mehat10d469b2010-02-25 14:02:55 -080038 return -1;
39 }
San Mehate2fe2612010-02-26 10:57:17 -080040#endif
San Mehat10d469b2010-02-25 14:02:55 -080041 return 0;
42}
43
Mark Salyzyn12717162014-04-29 15:49:14 -070044int android_get_ioprio(int pid __android_unused, IoSchedClass *clazz, int *ioprio) {
Elliott Hughes9b828ad2015-07-30 08:47:35 -070045#if defined(__ANDROID__)
San Mehat10d469b2010-02-25 14:02:55 -080046 int rc;
47
Dan Albert167c0ed2014-06-12 17:53:03 -070048 if ((rc = syscall(SYS_ioprio_get, IOPRIO_WHO_PROCESS, pid)) < 0) {
San Mehat10d469b2010-02-25 14:02:55 -080049 return -1;
50 }
51
Elliott Hughes8e9aeb92017-11-10 10:22:07 -080052 *clazz = static_cast<IoSchedClass>(rc >> IOPRIO_CLASS_SHIFT);
San Mehat10d469b2010-02-25 14:02:55 -080053 *ioprio = (rc & 0xff);
San Mehate2fe2612010-02-26 10:57:17 -080054#else
55 *clazz = IoSchedClass_NONE;
56 *ioprio = 0;
57#endif
San Mehat10d469b2010-02-25 14:02:55 -080058 return 0;
59}