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