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