Mark Salyzyn | 3bca606 | 2014-01-10 15:37:50 -0800 | [diff] [blame] | 1 | /* |
Mark Salyzyn | 3bca606 | 2014-01-10 15:37:50 -0800 | [diff] [blame] | 2 | ** Copyright 2007-2014, The Android Open Source Project |
San Mehat | 10d469b | 2010-02-25 14:02:55 -0800 | [diff] [blame] | 3 | ** |
| 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 Mehat | 10d469b | 2010-02-25 14:02:55 -0800 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include <fcntl.h> |
Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <unistd.h> |
San Mehat | 10d469b | 2010-02-25 14:02:55 -0800 | [diff] [blame] | 23 | |
| 24 | #ifdef HAVE_SCHED_H |
| 25 | |
| 26 | #include <cutils/iosched_policy.h> |
| 27 | |
Mark Salyzyn | 3bca606 | 2014-01-10 15:37:50 -0800 | [diff] [blame] | 28 | #ifdef HAVE_ANDROID_OS |
| 29 | /* #include <linux/ioprio.h> */ |
San Mehat | 10d469b | 2010-02-25 14:02:55 -0800 | [diff] [blame] | 30 | extern int ioprio_set(int which, int who, int ioprio); |
Mark Salyzyn | 3bca606 | 2014-01-10 15:37:50 -0800 | [diff] [blame] | 31 | extern int ioprio_get(int which, int who); |
Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 32 | #define __android_unused |
| 33 | #else |
| 34 | #define __android_unused __attribute__((__unused__)) |
Mark Salyzyn | 3bca606 | 2014-01-10 15:37:50 -0800 | [diff] [blame] | 35 | #endif |
San Mehat | 10d469b | 2010-02-25 14:02:55 -0800 | [diff] [blame] | 36 | |
| 37 | enum { |
| 38 | WHO_PROCESS = 1, |
| 39 | WHO_PGRP, |
| 40 | WHO_USER, |
| 41 | }; |
| 42 | |
| 43 | #define CLASS_SHIFT 13 |
| 44 | #define IOPRIO_NORM 4 |
| 45 | |
Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 46 | int android_set_ioprio(int pid __android_unused, IoSchedClass clazz __android_unused, int ioprio __android_unused) { |
San Mehat | e2fe261 | 2010-02-26 10:57:17 -0800 | [diff] [blame] | 47 | #ifdef HAVE_ANDROID_OS |
San Mehat | 10d469b | 2010-02-25 14:02:55 -0800 | [diff] [blame] | 48 | if (ioprio_set(WHO_PROCESS, pid, ioprio | (clazz << CLASS_SHIFT))) { |
| 49 | return -1; |
| 50 | } |
San Mehat | e2fe261 | 2010-02-26 10:57:17 -0800 | [diff] [blame] | 51 | #endif |
San Mehat | 10d469b | 2010-02-25 14:02:55 -0800 | [diff] [blame] | 52 | return 0; |
| 53 | } |
| 54 | |
Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 55 | int android_get_ioprio(int pid __android_unused, IoSchedClass *clazz, int *ioprio) { |
San Mehat | e2fe261 | 2010-02-26 10:57:17 -0800 | [diff] [blame] | 56 | #ifdef HAVE_ANDROID_OS |
San Mehat | 10d469b | 2010-02-25 14:02:55 -0800 | [diff] [blame] | 57 | 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 Mehat | e2fe261 | 2010-02-26 10:57:17 -0800 | [diff] [blame] | 65 | #else |
| 66 | *clazz = IoSchedClass_NONE; |
| 67 | *ioprio = 0; |
| 68 | #endif |
San Mehat | 10d469b | 2010-02-25 14:02:55 -0800 | [diff] [blame] | 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | #endif /* HAVE_SCHED_H */ |