blob: 2314f3028bb326a42afb14605b10f884b0c800ed [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>
22#include <sys/resource.h>
23#include <sys/time.h>
24#include <unistd.h>
25
26#include <mutex>
27
Martin Stjernholm2e2c45e2019-04-09 20:40:59 +010028#include <android-base/file.h>
Orion Hodson119733d2019-01-30 15:14:41 +000029#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 Stjernholm2e2c45e2019-04-09 20:40:59 +010034#include <tombstoned/tombstoned.h>
Orion Hodson119733d2019-01-30 15:14:41 +000035#include <utils/Thread.h>
36
37#include "palette_system.h"
38
39enum 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//
49static 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
62enum 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
88enum 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 Stjernholm2e2c45e2019-04-09 20:40:59 +0100109enum 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 Stjernholm9b70ee02019-04-15 21:35:13 +0100118 PLOG(ERROR) << "Failed to write tombstoned output";
Martin Stjernholm2e2c45e2019-04-09 20:40:59 +0100119 success = false;
Martin Stjernholmb9f1d3e2019-05-07 18:59:17 +0100120 } 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 Stjernholm9b70ee02019-04-15 21:35:13 +0100123 PLOG(ERROR) << "Failed to fsync tombstoned output";
Martin Stjernholm2e2c45e2019-04-09 20:40:59 +0100124 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 Stjernholm9b70ee02019-04-15 21:35:13 +0100129 PLOG(ERROR) << "Failed to close tombstoned output";
Martin Stjernholm2e2c45e2019-04-09 20:40:59 +0100130 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 Hodson119733d2019-01-30 15:14:41 +0000149enum PaletteStatus PaletteTraceEnabled(/*out*/int32_t* enabled) {
150 *enabled = (ATRACE_ENABLED() != 0) ? 1 : 0;
151 return PaletteStatus::kOkay;
152}
153
154enum PaletteStatus PaletteTraceBegin(const char* name) {
155 ATRACE_BEGIN(name);
156 return PaletteStatus::kOkay;
157}
158
159enum PaletteStatus PaletteTraceEnd() {
160 ATRACE_END();
161 return PaletteStatus::kOkay;
162}
163
164enum PaletteStatus PaletteTraceIntegerValue(const char* name, int32_t value) {
165 ATRACE_INT(name, value);
166 return PaletteStatus::kOkay;
167}