blob: bb9841ae66473dcf3c00c555dcc5a0b20c95bb69 [file] [log] [blame]
Orion Hodson119733d2019-01-30 15:14:41 +00001/*
2 * Copyright (C) 2019 The Android Open Source 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 ATRACE_TAG ATRACE_TAG_DALVIK
18
19#include "palette/palette.h"
20
21#include <errno.h>
Nicolas Geoffray2411f492019-06-14 08:54:46 +010022#include <linux/ashmem.h>
Orion Hodson119733d2019-01-30 15:14:41 +000023#include <sys/resource.h>
24#include <sys/time.h>
25#include <unistd.h>
26
27#include <mutex>
28
Martin Stjernholm2e2c45e2019-04-09 20:40:59 +010029#include <android-base/file.h>
Orion Hodson119733d2019-01-30 15:14:41 +000030#include <android-base/logging.h>
31#include <android-base/macros.h>
32#include <cutils/sched_policy.h>
33#include <cutils/trace.h>
34#include <log/event_tag_map.h>
Martin Stjernholm2e2c45e2019-04-09 20:40:59 +010035#include <tombstoned/tombstoned.h>
Orion Hodson119733d2019-01-30 15:14:41 +000036#include <utils/Thread.h>
37
38#include "palette_system.h"
39
40enum PaletteStatus PaletteGetVersion(int32_t* version) {
41 *version = art::palette::kPaletteVersion;
42 return PaletteStatus::kOkay;
43}
44
45// Conversion map for "nice" values.
46//
47// We use Android thread priority constants to be consistent with the rest
48// of the system. In some cases adjacent entries may overlap.
49//
50static const int kNiceValues[art::palette::kNumManagedThreadPriorities] = {
51 ANDROID_PRIORITY_LOWEST, // 1 (MIN_PRIORITY)
52 ANDROID_PRIORITY_BACKGROUND + 6,
53 ANDROID_PRIORITY_BACKGROUND + 3,
54 ANDROID_PRIORITY_BACKGROUND,
55 ANDROID_PRIORITY_NORMAL, // 5 (NORM_PRIORITY)
56 ANDROID_PRIORITY_NORMAL - 2,
57 ANDROID_PRIORITY_NORMAL - 4,
58 ANDROID_PRIORITY_URGENT_DISPLAY + 3,
59 ANDROID_PRIORITY_URGENT_DISPLAY + 2,
60 ANDROID_PRIORITY_URGENT_DISPLAY // 10 (MAX_PRIORITY)
61};
62
63enum PaletteStatus PaletteSchedSetPriority(int32_t tid, int32_t managed_priority) {
64 if (managed_priority < art::palette::kMinManagedThreadPriority ||
65 managed_priority > art::palette::kMaxManagedThreadPriority) {
66 return PaletteStatus::kInvalidArgument;
67 }
68 int new_nice = kNiceValues[managed_priority - art::palette::kMinManagedThreadPriority];
69
70 // TODO: b/18249098 The code below is broken. It uses getpriority() as a proxy for whether a
71 // thread is already in the SP_FOREGROUND cgroup. This is not necessarily true for background
72 // processes, where all threads are in the SP_BACKGROUND cgroup. This means that callers will
73 // have to call setPriority twice to do what they want :
74 //
75 // Thread.setPriority(Thread.MIN_PRIORITY); // no-op wrt to cgroups
76 // Thread.setPriority(Thread.MAX_PRIORITY); // will actually change cgroups.
77 if (new_nice >= ANDROID_PRIORITY_BACKGROUND) {
78 set_sched_policy(tid, SP_BACKGROUND);
79 } else if (getpriority(PRIO_PROCESS, tid) >= ANDROID_PRIORITY_BACKGROUND) {
80 set_sched_policy(tid, SP_FOREGROUND);
81 }
82
83 if (setpriority(PRIO_PROCESS, tid, new_nice) != 0) {
84 return PaletteStatus::kCheckErrno;
85 }
86 return PaletteStatus::kOkay;
87}
88
89enum PaletteStatus PaletteSchedGetPriority(int32_t tid, /*out*/int32_t* managed_priority) {
90 errno = 0;
91 int native_priority = getpriority(PRIO_PROCESS, tid);
92 if (native_priority == -1 && errno != 0) {
93 *managed_priority = art::palette::kNormalManagedThreadPriority;
94 return PaletteStatus::kCheckErrno;
95 }
96
97 for (int p = art::palette::kMinManagedThreadPriority;
98 p <= art::palette::kMaxManagedThreadPriority;
99 p = p + 1) {
100 int index = p - art::palette::kMinManagedThreadPriority;
101 if (native_priority >= kNiceValues[index]) {
102 *managed_priority = p;
103 return PaletteStatus::kOkay;
104 }
105 }
106 *managed_priority = art::palette::kMaxManagedThreadPriority;
107 return PaletteStatus::kOkay;
108}
109
Orion Hodsond3374a02019-05-02 10:56:33 +0100110enum PaletteStatus PaletteWriteCrashThreadStacks(/*in*/const char* stacks, size_t stacks_len) {
Martin Stjernholm2e2c45e2019-04-09 20:40:59 +0100111 android::base::unique_fd tombstone_fd;
112 android::base::unique_fd output_fd;
Orion Hodsond3374a02019-05-02 10:56:33 +0100113
Martin Stjernholm2e2c45e2019-04-09 20:40:59 +0100114 if (!tombstoned_connect(getpid(), &tombstone_fd, &output_fd, kDebuggerdJavaBacktrace)) {
Orion Hodsond3374a02019-05-02 10:56:33 +0100115 // Failure here could be due to file descriptor resource exhaustion
116 // so write the stack trace message to the log in case it helps
117 // debug that.
118 LOG(INFO) << std::string_view(stacks, stacks_len);
119 // tombstoned_connect() logs failure reason.
120 return PaletteStatus::kFailedCheckLog;
Martin Stjernholm2e2c45e2019-04-09 20:40:59 +0100121 }
122
Orion Hodsond3374a02019-05-02 10:56:33 +0100123 PaletteStatus status = PaletteStatus::kOkay;
124 if (!android::base::WriteFully(output_fd, stacks, stacks_len)) {
Martin Stjernholm9b70ee02019-04-15 21:35:13 +0100125 PLOG(ERROR) << "Failed to write tombstoned output";
Orion Hodsond3374a02019-05-02 10:56:33 +0100126 TEMP_FAILURE_RETRY(ftruncate(output_fd, 0));
127 status = PaletteStatus::kFailedCheckLog;
128 }
129
130 if (TEMP_FAILURE_RETRY(fdatasync(output_fd)) == -1 && errno != EINVAL) {
Martin Stjernholmb9f1d3e2019-05-07 18:59:17 +0100131 // Ignore EINVAL so we don't report failure if we just tried to flush a pipe
132 // or socket.
Orion Hodsond3374a02019-05-02 10:56:33 +0100133 if (status == PaletteStatus::kOkay) {
134 PLOG(ERROR) << "Failed to fsync tombstoned output";
135 status = PaletteStatus::kFailedCheckLog;
136 }
137 TEMP_FAILURE_RETRY(ftruncate(output_fd, 0));
138 TEMP_FAILURE_RETRY(fdatasync(output_fd));
139 }
140
141 if (close(output_fd.release()) == -1 && errno != EINTR) {
142 if (status == PaletteStatus::kOkay) {
Martin Stjernholm9b70ee02019-04-15 21:35:13 +0100143 PLOG(ERROR) << "Failed to close tombstoned output";
Orion Hodsond3374a02019-05-02 10:56:33 +0100144 status = PaletteStatus::kFailedCheckLog;
Martin Stjernholm2e2c45e2019-04-09 20:40:59 +0100145 }
146 }
147
Martin Stjernholm2e2c45e2019-04-09 20:40:59 +0100148 if (!tombstoned_notify_completion(tombstone_fd)) {
Orion Hodsond3374a02019-05-02 10:56:33 +0100149 // tombstoned_notify_completion() logs failure.
150 status = PaletteStatus::kFailedCheckLog;
Martin Stjernholm2e2c45e2019-04-09 20:40:59 +0100151 }
152
Orion Hodsond3374a02019-05-02 10:56:33 +0100153 return status;
Martin Stjernholm2e2c45e2019-04-09 20:40:59 +0100154}
155
Orion Hodson119733d2019-01-30 15:14:41 +0000156enum PaletteStatus PaletteTraceEnabled(/*out*/int32_t* enabled) {
157 *enabled = (ATRACE_ENABLED() != 0) ? 1 : 0;
158 return PaletteStatus::kOkay;
159}
160
161enum PaletteStatus PaletteTraceBegin(const char* name) {
162 ATRACE_BEGIN(name);
163 return PaletteStatus::kOkay;
164}
165
166enum PaletteStatus PaletteTraceEnd() {
167 ATRACE_END();
168 return PaletteStatus::kOkay;
169}
170
171enum PaletteStatus PaletteTraceIntegerValue(const char* name, int32_t value) {
172 ATRACE_INT(name, value);
173 return PaletteStatus::kOkay;
174}
Nicolas Geoffray2411f492019-06-14 08:54:46 +0100175
176enum PaletteStatus PaletteAshmemCreateRegion(const char* name, size_t size, int* fd) {
177 // We implement our own ashmem creation, as the libcutils implementation does
178 // a binder call, and our only use of ashmem in ART is for zygote, which
179 // cannot communicate to binder.
180 *fd = TEMP_FAILURE_RETRY(open("/dev/ashmem", O_RDWR | O_CLOEXEC));
181 if (*fd == -1) {
182 return PaletteStatus::kCheckErrno;
183 }
184
185 if (TEMP_FAILURE_RETRY(ioctl(*fd, ASHMEM_SET_SIZE, size)) < 0) {
186 goto error;
187 }
188
189 if (name != nullptr) {
190 char buf[ASHMEM_NAME_LEN] = {0};
191 strlcpy(buf, name, sizeof(buf));
192 if (TEMP_FAILURE_RETRY(ioctl(*fd, ASHMEM_SET_NAME, buf)) < 0) {
193 goto error;
194 }
195 }
196
197 return PaletteStatus::kOkay;
198
199error:
200 // Save errno before closing.
201 int save_errno = errno;
202 close(*fd);
203 errno = save_errno;
204 return PaletteStatus::kCheckErrno;
205}
206
207enum PaletteStatus PaletteAshmemSetProtRegion(int fd, int prot) {
208 if (TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_PROT_MASK, prot)) < 0) {
209 return PaletteStatus::kCheckErrno;
210 }
211 return PaletteStatus::kOkay;
212}