Orion Hodson | 119733d | 2019-01-30 15:14:41 +0000 | [diff] [blame] | 1 | /* |
| 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> |
| 22 | #include <sys/resource.h> |
| 23 | #include <sys/time.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | #include <mutex> |
| 27 | |
Martin Stjernholm | 2e2c45e | 2019-04-09 20:40:59 +0100 | [diff] [blame] | 28 | #include <android-base/file.h> |
Orion Hodson | 119733d | 2019-01-30 15:14:41 +0000 | [diff] [blame] | 29 | #include <android-base/logging.h> |
| 30 | #include <android-base/macros.h> |
Orion Hodson | 765b87d | 2019-10-15 21:25:58 +0100 | [diff] [blame] | 31 | #include <cutils/ashmem.h> |
Orion Hodson | 119733d | 2019-01-30 15:14:41 +0000 | [diff] [blame] | 32 | #include <cutils/sched_policy.h> |
| 33 | #include <cutils/trace.h> |
| 34 | #include <log/event_tag_map.h> |
Martin Stjernholm | 2e2c45e | 2019-04-09 20:40:59 +0100 | [diff] [blame] | 35 | #include <tombstoned/tombstoned.h> |
Orion Hodson | 119733d | 2019-01-30 15:14:41 +0000 | [diff] [blame] | 36 | #include <utils/Thread.h> |
| 37 | |
| 38 | #include "palette_system.h" |
| 39 | |
| 40 | enum 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 | // |
| 50 | static 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 | |
| 63 | enum 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 | |
| 89 | enum 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 Hodson | d3374a0 | 2019-05-02 10:56:33 +0100 | [diff] [blame] | 110 | enum PaletteStatus PaletteWriteCrashThreadStacks(/*in*/const char* stacks, size_t stacks_len) { |
Martin Stjernholm | 2e2c45e | 2019-04-09 20:40:59 +0100 | [diff] [blame] | 111 | android::base::unique_fd tombstone_fd; |
| 112 | android::base::unique_fd output_fd; |
Orion Hodson | d3374a0 | 2019-05-02 10:56:33 +0100 | [diff] [blame] | 113 | |
Martin Stjernholm | 2e2c45e | 2019-04-09 20:40:59 +0100 | [diff] [blame] | 114 | if (!tombstoned_connect(getpid(), &tombstone_fd, &output_fd, kDebuggerdJavaBacktrace)) { |
Orion Hodson | d3374a0 | 2019-05-02 10:56:33 +0100 | [diff] [blame] | 115 | // 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 Stjernholm | 2e2c45e | 2019-04-09 20:40:59 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Orion Hodson | d3374a0 | 2019-05-02 10:56:33 +0100 | [diff] [blame] | 123 | PaletteStatus status = PaletteStatus::kOkay; |
| 124 | if (!android::base::WriteFully(output_fd, stacks, stacks_len)) { |
Martin Stjernholm | 9b70ee0 | 2019-04-15 21:35:13 +0100 | [diff] [blame] | 125 | PLOG(ERROR) << "Failed to write tombstoned output"; |
Orion Hodson | d3374a0 | 2019-05-02 10:56:33 +0100 | [diff] [blame] | 126 | 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 Stjernholm | b9f1d3e | 2019-05-07 18:59:17 +0100 | [diff] [blame] | 131 | // Ignore EINVAL so we don't report failure if we just tried to flush a pipe |
| 132 | // or socket. |
Orion Hodson | d3374a0 | 2019-05-02 10:56:33 +0100 | [diff] [blame] | 133 | 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 Stjernholm | 9b70ee0 | 2019-04-15 21:35:13 +0100 | [diff] [blame] | 143 | PLOG(ERROR) << "Failed to close tombstoned output"; |
Orion Hodson | d3374a0 | 2019-05-02 10:56:33 +0100 | [diff] [blame] | 144 | status = PaletteStatus::kFailedCheckLog; |
Martin Stjernholm | 2e2c45e | 2019-04-09 20:40:59 +0100 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Martin Stjernholm | 2e2c45e | 2019-04-09 20:40:59 +0100 | [diff] [blame] | 148 | if (!tombstoned_notify_completion(tombstone_fd)) { |
Orion Hodson | d3374a0 | 2019-05-02 10:56:33 +0100 | [diff] [blame] | 149 | // tombstoned_notify_completion() logs failure. |
| 150 | status = PaletteStatus::kFailedCheckLog; |
Martin Stjernholm | 2e2c45e | 2019-04-09 20:40:59 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Orion Hodson | d3374a0 | 2019-05-02 10:56:33 +0100 | [diff] [blame] | 153 | return status; |
Martin Stjernholm | 2e2c45e | 2019-04-09 20:40:59 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Orion Hodson | 119733d | 2019-01-30 15:14:41 +0000 | [diff] [blame] | 156 | enum PaletteStatus PaletteTraceEnabled(/*out*/int32_t* enabled) { |
| 157 | *enabled = (ATRACE_ENABLED() != 0) ? 1 : 0; |
| 158 | return PaletteStatus::kOkay; |
| 159 | } |
| 160 | |
| 161 | enum PaletteStatus PaletteTraceBegin(const char* name) { |
| 162 | ATRACE_BEGIN(name); |
| 163 | return PaletteStatus::kOkay; |
| 164 | } |
| 165 | |
| 166 | enum PaletteStatus PaletteTraceEnd() { |
| 167 | ATRACE_END(); |
| 168 | return PaletteStatus::kOkay; |
| 169 | } |
| 170 | |
| 171 | enum PaletteStatus PaletteTraceIntegerValue(const char* name, int32_t value) { |
| 172 | ATRACE_INT(name, value); |
| 173 | return PaletteStatus::kOkay; |
| 174 | } |
Nicolas Geoffray | 2411f49 | 2019-06-14 08:54:46 +0100 | [diff] [blame] | 175 | |
Orion Hodson | 765b87d | 2019-10-15 21:25:58 +0100 | [diff] [blame] | 176 | // Flag whether to use legacy ashmem or current (b/139855428) |
| 177 | static std::atomic_bool g_assume_legacy_ashmemd(false); |
| 178 | |
Nicolas Geoffray | 2411f49 | 2019-06-14 08:54:46 +0100 | [diff] [blame] | 179 | enum PaletteStatus PaletteAshmemCreateRegion(const char* name, size_t size, int* fd) { |
Orion Hodson | 765b87d | 2019-10-15 21:25:58 +0100 | [diff] [blame] | 180 | if (g_assume_legacy_ashmemd.load(std::memory_order_acquire) == false) { |
| 181 | // Current platform behaviour which open ashmem fd in process (b/139855428) |
| 182 | *fd = ashmem_create_region(name, size); |
| 183 | if (*fd >= 0) { |
| 184 | return PaletteStatus::kOkay; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // Try legacy behavior just required for ART build bots which may be running tests on older |
| 189 | // platform builds. |
| 190 | |
Orion Hodson | af47ca0 | 2019-10-15 09:09:34 +0000 | [diff] [blame] | 191 | // We implement our own ashmem creation, as the libcutils implementation does |
| 192 | // a binder call, and our only use of ashmem in ART is for zygote, which |
| 193 | // cannot communicate to binder. |
| 194 | *fd = TEMP_FAILURE_RETRY(open("/dev/ashmem", O_RDWR | O_CLOEXEC)); |
| 195 | if (*fd == -1) { |
Nicolas Geoffray | 2411f49 | 2019-06-14 08:54:46 +0100 | [diff] [blame] | 196 | return PaletteStatus::kCheckErrno; |
| 197 | } |
Orion Hodson | af47ca0 | 2019-10-15 09:09:34 +0000 | [diff] [blame] | 198 | |
| 199 | if (TEMP_FAILURE_RETRY(ioctl(*fd, ASHMEM_SET_SIZE, size)) < 0) { |
| 200 | goto error; |
| 201 | } |
| 202 | |
| 203 | if (name != nullptr) { |
| 204 | char buf[ASHMEM_NAME_LEN] = {0}; |
| 205 | strlcpy(buf, name, sizeof(buf)); |
| 206 | if (TEMP_FAILURE_RETRY(ioctl(*fd, ASHMEM_SET_NAME, buf)) < 0) { |
| 207 | goto error; |
| 208 | } |
| 209 | } |
| 210 | |
Orion Hodson | 765b87d | 2019-10-15 21:25:58 +0100 | [diff] [blame] | 211 | g_assume_legacy_ashmemd.store(true, std::memory_order_release); |
Nicolas Geoffray | 2411f49 | 2019-06-14 08:54:46 +0100 | [diff] [blame] | 212 | return PaletteStatus::kOkay; |
Orion Hodson | af47ca0 | 2019-10-15 09:09:34 +0000 | [diff] [blame] | 213 | |
| 214 | error: |
| 215 | // Save errno before closing. |
| 216 | int save_errno = errno; |
| 217 | close(*fd); |
| 218 | errno = save_errno; |
| 219 | return PaletteStatus::kCheckErrno; |
Nicolas Geoffray | 2411f49 | 2019-06-14 08:54:46 +0100 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | enum PaletteStatus PaletteAshmemSetProtRegion(int fd, int prot) { |
Orion Hodson | 765b87d | 2019-10-15 21:25:58 +0100 | [diff] [blame] | 223 | if (!g_assume_legacy_ashmemd.load(std::memory_order_acquire)) { |
| 224 | if (ashmem_set_prot_region(fd, prot) < 0) { |
| 225 | return PaletteStatus::kCheckErrno; |
| 226 | } |
| 227 | } else if (TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_PROT_MASK, prot)) < 0) { |
| 228 | // Legacy behavior just required for ART build bots which may be running tests on older |
| 229 | // platform builds. |
Nicolas Geoffray | 2411f49 | 2019-06-14 08:54:46 +0100 | [diff] [blame] | 230 | return PaletteStatus::kCheckErrno; |
| 231 | } |
| 232 | return PaletteStatus::kOkay; |
| 233 | } |