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
28 #include <android-base/file.h>
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>
34 #include <tombstoned/tombstoned.h>
35 #include <utils/Thread.h>
36
37 #include "palette_system.h"
38
PaletteGetVersion(int32_t * version)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
PaletteSchedSetPriority(int32_t tid,int32_t managed_priority)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
PaletteSchedGetPriority(int32_t tid,int32_t * managed_priority)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
PaletteWriteCrashThreadStacks(const char * stacks,size_t stacks_len)109 enum PaletteStatus PaletteWriteCrashThreadStacks(/*in*/const char* stacks, size_t stacks_len) {
110 android::base::unique_fd tombstone_fd;
111 android::base::unique_fd output_fd;
112
113 if (!tombstoned_connect(getpid(), &tombstone_fd, &output_fd, kDebuggerdJavaBacktrace)) {
114 // Failure here could be due to file descriptor resource exhaustion
115 // so write the stack trace message to the log in case it helps
116 // debug that.
117 LOG(INFO) << std::string_view(stacks, stacks_len);
118 // tombstoned_connect() logs failure reason.
119 return PaletteStatus::kFailedCheckLog;
120 }
121
122 PaletteStatus status = PaletteStatus::kOkay;
123 if (!android::base::WriteFully(output_fd, stacks, stacks_len)) {
124 PLOG(ERROR) << "Failed to write tombstoned output";
125 TEMP_FAILURE_RETRY(ftruncate(output_fd, 0));
126 status = PaletteStatus::kFailedCheckLog;
127 }
128
129 if (TEMP_FAILURE_RETRY(fdatasync(output_fd)) == -1 && errno != EINVAL) {
130 // Ignore EINVAL so we don't report failure if we just tried to flush a pipe
131 // or socket.
132 if (status == PaletteStatus::kOkay) {
133 PLOG(ERROR) << "Failed to fsync tombstoned output";
134 status = PaletteStatus::kFailedCheckLog;
135 }
136 TEMP_FAILURE_RETRY(ftruncate(output_fd, 0));
137 TEMP_FAILURE_RETRY(fdatasync(output_fd));
138 }
139
140 if (close(output_fd.release()) == -1 && errno != EINTR) {
141 if (status == PaletteStatus::kOkay) {
142 PLOG(ERROR) << "Failed to close tombstoned output";
143 status = PaletteStatus::kFailedCheckLog;
144 }
145 }
146
147 if (!tombstoned_notify_completion(tombstone_fd)) {
148 // tombstoned_notify_completion() logs failure.
149 status = PaletteStatus::kFailedCheckLog;
150 }
151
152 return status;
153 }
154
PaletteTraceEnabled(int32_t * enabled)155 enum PaletteStatus PaletteTraceEnabled(/*out*/int32_t* enabled) {
156 *enabled = (ATRACE_ENABLED() != 0) ? 1 : 0;
157 return PaletteStatus::kOkay;
158 }
159
PaletteTraceBegin(const char * name)160 enum PaletteStatus PaletteTraceBegin(const char* name) {
161 ATRACE_BEGIN(name);
162 return PaletteStatus::kOkay;
163 }
164
PaletteTraceEnd()165 enum PaletteStatus PaletteTraceEnd() {
166 ATRACE_END();
167 return PaletteStatus::kOkay;
168 }
169
PaletteTraceIntegerValue(const char * name,int32_t value)170 enum PaletteStatus PaletteTraceIntegerValue(const char* name, int32_t value) {
171 ATRACE_INT(name, value);
172 return PaletteStatus::kOkay;
173 }
174