dianlujitao | 1334e75 | 2017-11-29 15:13:23 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The LineageOS Project |
| 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 | |
| 17 | #define LOG_TAG "bfqio" |
| 18 | |
| 19 | #include <fcntl.h> |
| 20 | #include <cutils/iosched_policy.h> |
| 21 | #include <cutils/log.h> |
| 22 | #include <pthread.h> |
| 23 | #include <sys/stat.h> |
| 24 | |
| 25 | static int __rtio_cgroup_supported = -1; |
| 26 | static pthread_once_t __rtio_init_once = PTHREAD_ONCE_INIT; |
| 27 | |
| 28 | static void __initialize_rtio(void) { |
| 29 | if (!access("/dev/bfqio/tasks", W_OK) || !access("/dev/bfqio/rt-display/tasks", W_OK)) { |
| 30 | __rtio_cgroup_supported = 1; |
| 31 | } else { |
| 32 | __rtio_cgroup_supported = 0; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | int android_set_rt_ioprio(int tid, int rt) { |
| 37 | int fd = -1, rc = -1; |
| 38 | |
| 39 | pthread_once(&__rtio_init_once, __initialize_rtio); |
| 40 | if (__rtio_cgroup_supported != 1) { |
| 41 | return -1; |
| 42 | } |
| 43 | |
| 44 | if (rt) { |
| 45 | fd = open("/dev/bfqio/rt-display/tasks", O_WRONLY | O_CLOEXEC); |
| 46 | } else { |
| 47 | fd = open("/dev/bfqio/tasks", O_WRONLY | O_CLOEXEC); |
| 48 | } |
| 49 | |
| 50 | if (fd < 0) { |
| 51 | return -1; |
| 52 | } |
| 53 | |
| 54 | #ifdef HAVE_GETTID |
| 55 | if (tid == 0) { |
| 56 | tid = gettid(); |
| 57 | } |
| 58 | #endif |
| 59 | |
| 60 | // specialized itoa -- works for tid > 0 |
| 61 | char text[22]; |
| 62 | char *end = text + sizeof(text) - 1; |
| 63 | char *ptr = end; |
| 64 | *ptr = '\0'; |
| 65 | while (tid > 0) { |
| 66 | *--ptr = '0' + (tid % 10); |
| 67 | tid = tid / 10; |
| 68 | } |
| 69 | |
| 70 | rc = write(fd, ptr, end - ptr); |
| 71 | if (rc < 0) { |
| 72 | /* |
| 73 | * If the thread is in the process of exiting, |
| 74 | * don't flag an error |
| 75 | */ |
| 76 | if (errno == ESRCH) { |
| 77 | rc = 0; |
| 78 | } else { |
| 79 | SLOGV("android_set_rt_ioprio failed to write '%s' (%s); fd=%d\n", |
| 80 | ptr, strerror(errno), fd); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | close(fd); |
| 85 | return rc; |
| 86 | } |