blob: 0a4a21d3a0332b9b712cddfa8779310fada35e3b [file] [log] [blame]
dianlujitao1334e752017-11-29 15:13:23 +08001/*
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
Rashed Abdel-Tawabaf316642018-08-07 10:45:02 -070019#include <errno.h>
dianlujitao1334e752017-11-29 15:13:23 +080020#include <fcntl.h>
21#include <cutils/iosched_policy.h>
LuK1337d3904642018-06-27 00:35:37 +020022#include <log/log.h>
dianlujitao1334e752017-11-29 15:13:23 +080023#include <pthread.h>
Rashed Abdel-Tawabaf316642018-08-07 10:45:02 -070024#include <string.h>
dianlujitao1334e752017-11-29 15:13:23 +080025#include <sys/stat.h>
Ethan Chene5e321d2018-01-26 09:48:19 -080026#include <unistd.h>
dianlujitao1334e752017-11-29 15:13:23 +080027
28static int __rtio_cgroup_supported = -1;
29static pthread_once_t __rtio_init_once = PTHREAD_ONCE_INIT;
30
31static void __initialize_rtio(void) {
32 if (!access("/dev/bfqio/tasks", W_OK) || !access("/dev/bfqio/rt-display/tasks", W_OK)) {
33 __rtio_cgroup_supported = 1;
34 } else {
35 __rtio_cgroup_supported = 0;
36 }
37}
38
39int android_set_rt_ioprio(int tid, int rt) {
40 int fd = -1, rc = -1;
41
42 pthread_once(&__rtio_init_once, __initialize_rtio);
43 if (__rtio_cgroup_supported != 1) {
44 return -1;
45 }
46
47 if (rt) {
48 fd = open("/dev/bfqio/rt-display/tasks", O_WRONLY | O_CLOEXEC);
49 } else {
50 fd = open("/dev/bfqio/tasks", O_WRONLY | O_CLOEXEC);
51 }
52
53 if (fd < 0) {
54 return -1;
55 }
56
57#ifdef HAVE_GETTID
58 if (tid == 0) {
59 tid = gettid();
60 }
61#endif
62
63 // specialized itoa -- works for tid > 0
64 char text[22];
65 char *end = text + sizeof(text) - 1;
66 char *ptr = end;
67 *ptr = '\0';
68 while (tid > 0) {
69 *--ptr = '0' + (tid % 10);
70 tid = tid / 10;
71 }
72
73 rc = write(fd, ptr, end - ptr);
74 if (rc < 0) {
75 /*
76 * If the thread is in the process of exiting,
77 * don't flag an error
78 */
79 if (errno == ESRCH) {
80 rc = 0;
81 } else {
82 SLOGV("android_set_rt_ioprio failed to write '%s' (%s); fd=%d\n",
83 ptr, strerror(errno), fd);
84 }
85 }
86
87 close(fd);
88 return rc;
89}