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> |
| 31 | #include <cutils/sched_policy.h> |
| 32 | #include <cutils/trace.h> |
| 33 | #include <log/event_tag_map.h> |
Martin Stjernholm | 2e2c45e | 2019-04-09 20:40:59 +0100 | [diff] [blame] | 34 | #include <tombstoned/tombstoned.h> |
Orion Hodson | 119733d | 2019-01-30 15:14:41 +0000 | [diff] [blame] | 35 | #include <utils/Thread.h> |
| 36 | |
| 37 | #include "palette_system.h" |
| 38 | |
| 39 | enum PaletteStatus PaletteGetVersion(int32_t* version) { |
| 40 | *version = art::palette::kPaletteVersion; |
| 41 | return PaletteStatus::kOkay; |
| 42 | } |
| 43 | |
| 44 | // Conversion map for "nice" values. |
| 45 | // |
| 46 | // We use Android thread priority constants to be consistent with the rest |
| 47 | // of the system. In some cases adjacent entries may overlap. |
| 48 | // |
| 49 | static const int kNiceValues[art::palette::kNumManagedThreadPriorities] = { |
| 50 | ANDROID_PRIORITY_LOWEST, // 1 (MIN_PRIORITY) |
| 51 | ANDROID_PRIORITY_BACKGROUND + 6, |
| 52 | ANDROID_PRIORITY_BACKGROUND + 3, |
| 53 | ANDROID_PRIORITY_BACKGROUND, |
| 54 | ANDROID_PRIORITY_NORMAL, // 5 (NORM_PRIORITY) |
| 55 | ANDROID_PRIORITY_NORMAL - 2, |
| 56 | ANDROID_PRIORITY_NORMAL - 4, |
| 57 | ANDROID_PRIORITY_URGENT_DISPLAY + 3, |
| 58 | ANDROID_PRIORITY_URGENT_DISPLAY + 2, |
| 59 | ANDROID_PRIORITY_URGENT_DISPLAY // 10 (MAX_PRIORITY) |
| 60 | }; |
| 61 | |
| 62 | enum PaletteStatus PaletteSchedSetPriority(int32_t tid, int32_t managed_priority) { |
| 63 | if (managed_priority < art::palette::kMinManagedThreadPriority || |
| 64 | managed_priority > art::palette::kMaxManagedThreadPriority) { |
| 65 | return PaletteStatus::kInvalidArgument; |
| 66 | } |
| 67 | int new_nice = kNiceValues[managed_priority - art::palette::kMinManagedThreadPriority]; |
| 68 | |
| 69 | // TODO: b/18249098 The code below is broken. It uses getpriority() as a proxy for whether a |
| 70 | // thread is already in the SP_FOREGROUND cgroup. This is not necessarily true for background |
| 71 | // processes, where all threads are in the SP_BACKGROUND cgroup. This means that callers will |
| 72 | // have to call setPriority twice to do what they want : |
| 73 | // |
| 74 | // Thread.setPriority(Thread.MIN_PRIORITY); // no-op wrt to cgroups |
| 75 | // Thread.setPriority(Thread.MAX_PRIORITY); // will actually change cgroups. |
| 76 | if (new_nice >= ANDROID_PRIORITY_BACKGROUND) { |
| 77 | set_sched_policy(tid, SP_BACKGROUND); |
| 78 | } else if (getpriority(PRIO_PROCESS, tid) >= ANDROID_PRIORITY_BACKGROUND) { |
| 79 | set_sched_policy(tid, SP_FOREGROUND); |
| 80 | } |
| 81 | |
| 82 | if (setpriority(PRIO_PROCESS, tid, new_nice) != 0) { |
| 83 | return PaletteStatus::kCheckErrno; |
| 84 | } |
| 85 | return PaletteStatus::kOkay; |
| 86 | } |
| 87 | |
| 88 | enum PaletteStatus PaletteSchedGetPriority(int32_t tid, /*out*/int32_t* managed_priority) { |
| 89 | errno = 0; |
| 90 | int native_priority = getpriority(PRIO_PROCESS, tid); |
| 91 | if (native_priority == -1 && errno != 0) { |
| 92 | *managed_priority = art::palette::kNormalManagedThreadPriority; |
| 93 | return PaletteStatus::kCheckErrno; |
| 94 | } |
| 95 | |
| 96 | for (int p = art::palette::kMinManagedThreadPriority; |
| 97 | p <= art::palette::kMaxManagedThreadPriority; |
| 98 | p = p + 1) { |
| 99 | int index = p - art::palette::kMinManagedThreadPriority; |
| 100 | if (native_priority >= kNiceValues[index]) { |
| 101 | *managed_priority = p; |
| 102 | return PaletteStatus::kOkay; |
| 103 | } |
| 104 | } |
| 105 | *managed_priority = art::palette::kMaxManagedThreadPriority; |
| 106 | return PaletteStatus::kOkay; |
| 107 | } |
| 108 | |
Martin Stjernholm | 2e2c45e | 2019-04-09 20:40:59 +0100 | [diff] [blame] | 109 | enum PaletteStatus PaletteTombstonedMessage(/*in*/const char* msg, size_t msg_len) { |
| 110 | android::base::unique_fd tombstone_fd; |
| 111 | android::base::unique_fd output_fd; |
| 112 | if (!tombstoned_connect(getpid(), &tombstone_fd, &output_fd, kDebuggerdJavaBacktrace)) { |
| 113 | return PaletteStatus::kCheckErrno; |
| 114 | } |
| 115 | |
| 116 | bool success = true; |
| 117 | if (!android::base::WriteFully(output_fd, msg, msg_len)) { |
Martin Stjernholm | 9b70ee0 | 2019-04-15 21:35:13 +0100 | [diff] [blame] | 118 | PLOG(ERROR) << "Failed to write tombstoned output"; |
Martin Stjernholm | 2e2c45e | 2019-04-09 20:40:59 +0100 | [diff] [blame] | 119 | success = false; |
Martin Stjernholm | b9f1d3e | 2019-05-07 18:59:17 +0100 | [diff] [blame^] | 120 | } else if (TEMP_FAILURE_RETRY(fsync(output_fd)) == -1 && errno != EINVAL) { |
| 121 | // Ignore EINVAL so we don't report failure if we just tried to flush a pipe |
| 122 | // or socket. |
Martin Stjernholm | 9b70ee0 | 2019-04-15 21:35:13 +0100 | [diff] [blame] | 123 | PLOG(ERROR) << "Failed to fsync tombstoned output"; |
Martin Stjernholm | 2e2c45e | 2019-04-09 20:40:59 +0100 | [diff] [blame] | 124 | success = false; |
| 125 | } else if (close(output_fd.release()) == -1) { |
| 126 | // Shouldn't retry close after EINTR because the fd has been closed anyway, |
| 127 | // but don't count it as a failure either. |
| 128 | if (errno != EINTR) { |
Martin Stjernholm | 9b70ee0 | 2019-04-15 21:35:13 +0100 | [diff] [blame] | 129 | PLOG(ERROR) << "Failed to close tombstoned output"; |
Martin Stjernholm | 2e2c45e | 2019-04-09 20:40:59 +0100 | [diff] [blame] | 130 | success = false; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | if (!success) { |
| 135 | int saved_errno = errno; |
| 136 | TEMP_FAILURE_RETRY(ftruncate(output_fd, 0)); |
| 137 | TEMP_FAILURE_RETRY(fsync(output_fd)); |
| 138 | close(output_fd.release()); |
| 139 | errno = saved_errno; |
| 140 | } |
| 141 | |
| 142 | if (!tombstoned_notify_completion(tombstone_fd)) { |
| 143 | success = false; |
| 144 | } |
| 145 | |
| 146 | return success ? PaletteStatus::kOkay : PaletteStatus::kCheckErrno; |
| 147 | } |
| 148 | |
Orion Hodson | 119733d | 2019-01-30 15:14:41 +0000 | [diff] [blame] | 149 | enum PaletteStatus PaletteTraceEnabled(/*out*/int32_t* enabled) { |
| 150 | *enabled = (ATRACE_ENABLED() != 0) ? 1 : 0; |
| 151 | return PaletteStatus::kOkay; |
| 152 | } |
| 153 | |
| 154 | enum PaletteStatus PaletteTraceBegin(const char* name) { |
| 155 | ATRACE_BEGIN(name); |
| 156 | return PaletteStatus::kOkay; |
| 157 | } |
| 158 | |
| 159 | enum PaletteStatus PaletteTraceEnd() { |
| 160 | ATRACE_END(); |
| 161 | return PaletteStatus::kOkay; |
| 162 | } |
| 163 | |
| 164 | enum PaletteStatus PaletteTraceIntegerValue(const char* name, int32_t value) { |
| 165 | ATRACE_INT(name, value); |
| 166 | return PaletteStatus::kOkay; |
| 167 | } |