Move libartpalette-system to system/libartpalette
libartpalette-system provides an implementation of the ART platform
abstraction layer. It provides system services to ART through the
narrow libartpalette interface.
Bug: 142944508
Test: art/test/testrunner/testrunner.py {--host,--target} --64 -r -g
Change-Id: Id8b699a3289a0f1230ba71ab37e4ea0e1e97aec7
diff --git a/Android.bp b/Android.bp
new file mode 100644
index 0000000..65bcaf1
--- /dev/null
+++ b/Android.bp
@@ -0,0 +1,53 @@
+//
+// Copyright (C) 2019 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+// libartpalette-system is the implementation of the platform
+// abstraction layer for ART. It is intended to be dynamically loaded by
+// ART which now ships as an APEX and not part of the system.
+cc_library {
+ name: "libartpalette-system",
+ compile_multilib: "both",
+ header_libs: ["libartpalette-headers"],
+ host_supported: true,
+ target: {
+ android: {
+ srcs: ["palette_android.cc"],
+ header_libs: ["libbase_headers"],
+ shared_libs: [
+ "libbase",
+ "libcutils",
+ "liblog",
+ "libprocessgroup",
+ "libtombstoned_client",
+ ],
+ },
+ host: {
+ header_libs: ["libbase_headers"],
+ srcs: ["palette_fake.cc"],
+ shared_libs: ["libbase"],
+ },
+ darwin: {
+ enabled: false,
+ },
+ windows: {
+ enabled: false,
+ },
+ },
+ static: {
+ enabled: false,
+ },
+ version_script: "libartpalette.map.txt",
+}
diff --git a/OWNERS b/OWNERS
new file mode 100644
index 0000000..df77c6c
--- /dev/null
+++ b/OWNERS
@@ -0,0 +1,4 @@
+mast@google.com
+ngeoffray@google.com
+oth@google.com
+rpl@google.com
\ No newline at end of file
diff --git a/libartpalette.map.txt b/libartpalette.map.txt
new file mode 100644
index 0000000..d2c90d5
--- /dev/null
+++ b/libartpalette.map.txt
@@ -0,0 +1,33 @@
+#
+# Copyright (C) 2019 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+LIBARTPALETTE_1 {
+ global:
+ # --- VERSION 01 API ---
+ PaletteGetVersion;
+ PaletteSchedSetPriority;
+ PaletteSchedGetPriority;
+ PaletteWriteCrashThreadStacks;
+ PaletteTraceEnabled;
+ PaletteTraceBegin;
+ PaletteTraceEnd;
+ PaletteTraceIntegerValue;
+ PaletteAshmemCreateRegion;
+ PaletteAshmemSetProtRegion;
+
+ local:
+ *;
+};
diff --git a/palette_android.cc b/palette_android.cc
new file mode 100644
index 0000000..e2d0036
--- /dev/null
+++ b/palette_android.cc
@@ -0,0 +1,232 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define ATRACE_TAG ATRACE_TAG_DALVIK
+
+#include "palette/palette.h"
+
+#include <errno.h>
+#include <sys/resource.h>
+#include <sys/time.h>
+#include <unistd.h>
+
+#include <mutex>
+
+#include <android-base/file.h>
+#include <android-base/logging.h>
+#include <android-base/macros.h>
+#include <cutils/ashmem.h>
+#include <cutils/sched_policy.h>
+#include <cutils/trace.h>
+#include <log/event_tag_map.h>
+#include <tombstoned/tombstoned.h>
+#include <utils/Thread.h>
+
+#include "palette_system.h"
+
+enum PaletteStatus PaletteGetVersion(int32_t* version) {
+ *version = art::palette::kPaletteVersion;
+ return PaletteStatus::kOkay;
+}
+
+// Conversion map for "nice" values.
+//
+// We use Android thread priority constants to be consistent with the rest
+// of the system. In some cases adjacent entries may overlap.
+//
+static const int kNiceValues[art::palette::kNumManagedThreadPriorities] = {
+ ANDROID_PRIORITY_LOWEST, // 1 (MIN_PRIORITY)
+ ANDROID_PRIORITY_BACKGROUND + 6,
+ ANDROID_PRIORITY_BACKGROUND + 3,
+ ANDROID_PRIORITY_BACKGROUND,
+ ANDROID_PRIORITY_NORMAL, // 5 (NORM_PRIORITY)
+ ANDROID_PRIORITY_NORMAL - 2,
+ ANDROID_PRIORITY_NORMAL - 4,
+ ANDROID_PRIORITY_URGENT_DISPLAY + 3,
+ ANDROID_PRIORITY_URGENT_DISPLAY + 2,
+ ANDROID_PRIORITY_URGENT_DISPLAY // 10 (MAX_PRIORITY)
+};
+
+enum PaletteStatus PaletteSchedSetPriority(int32_t tid, int32_t managed_priority) {
+ if (managed_priority < art::palette::kMinManagedThreadPriority ||
+ managed_priority > art::palette::kMaxManagedThreadPriority) {
+ return PaletteStatus::kInvalidArgument;
+ }
+ int new_nice = kNiceValues[managed_priority - art::palette::kMinManagedThreadPriority];
+
+ // TODO: b/18249098 The code below is broken. It uses getpriority() as a proxy for whether a
+ // thread is already in the SP_FOREGROUND cgroup. This is not necessarily true for background
+ // processes, where all threads are in the SP_BACKGROUND cgroup. This means that callers will
+ // have to call setPriority twice to do what they want :
+ //
+ // Thread.setPriority(Thread.MIN_PRIORITY); // no-op wrt to cgroups
+ // Thread.setPriority(Thread.MAX_PRIORITY); // will actually change cgroups.
+ if (new_nice >= ANDROID_PRIORITY_BACKGROUND) {
+ set_sched_policy(tid, SP_BACKGROUND);
+ } else if (getpriority(PRIO_PROCESS, tid) >= ANDROID_PRIORITY_BACKGROUND) {
+ set_sched_policy(tid, SP_FOREGROUND);
+ }
+
+ if (setpriority(PRIO_PROCESS, tid, new_nice) != 0) {
+ return PaletteStatus::kCheckErrno;
+ }
+ return PaletteStatus::kOkay;
+}
+
+enum PaletteStatus PaletteSchedGetPriority(int32_t tid, /*out*/ int32_t* managed_priority) {
+ errno = 0;
+ int native_priority = getpriority(PRIO_PROCESS, tid);
+ if (native_priority == -1 && errno != 0) {
+ *managed_priority = art::palette::kNormalManagedThreadPriority;
+ return PaletteStatus::kCheckErrno;
+ }
+
+ for (int p = art::palette::kMinManagedThreadPriority;
+ p <= art::palette::kMaxManagedThreadPriority; p = p + 1) {
+ int index = p - art::palette::kMinManagedThreadPriority;
+ if (native_priority >= kNiceValues[index]) {
+ *managed_priority = p;
+ return PaletteStatus::kOkay;
+ }
+ }
+ *managed_priority = art::palette::kMaxManagedThreadPriority;
+ return PaletteStatus::kOkay;
+}
+
+enum PaletteStatus PaletteWriteCrashThreadStacks(/*in*/ const char* stacks, size_t stacks_len) {
+ android::base::unique_fd tombstone_fd;
+ android::base::unique_fd output_fd;
+
+ if (!tombstoned_connect(getpid(), &tombstone_fd, &output_fd, kDebuggerdJavaBacktrace)) {
+ // Failure here could be due to file descriptor resource exhaustion
+ // so write the stack trace message to the log in case it helps
+ // debug that.
+ LOG(INFO) << std::string_view(stacks, stacks_len);
+ // tombstoned_connect() logs failure reason.
+ return PaletteStatus::kFailedCheckLog;
+ }
+
+ PaletteStatus status = PaletteStatus::kOkay;
+ if (!android::base::WriteFully(output_fd, stacks, stacks_len)) {
+ PLOG(ERROR) << "Failed to write tombstoned output";
+ TEMP_FAILURE_RETRY(ftruncate(output_fd, 0));
+ status = PaletteStatus::kFailedCheckLog;
+ }
+
+ if (TEMP_FAILURE_RETRY(fdatasync(output_fd)) == -1 && errno != EINVAL) {
+ // Ignore EINVAL so we don't report failure if we just tried to flush a pipe
+ // or socket.
+ if (status == PaletteStatus::kOkay) {
+ PLOG(ERROR) << "Failed to fsync tombstoned output";
+ status = PaletteStatus::kFailedCheckLog;
+ }
+ TEMP_FAILURE_RETRY(ftruncate(output_fd, 0));
+ TEMP_FAILURE_RETRY(fdatasync(output_fd));
+ }
+
+ if (close(output_fd.release()) == -1 && errno != EINTR) {
+ if (status == PaletteStatus::kOkay) {
+ PLOG(ERROR) << "Failed to close tombstoned output";
+ status = PaletteStatus::kFailedCheckLog;
+ }
+ }
+
+ if (!tombstoned_notify_completion(tombstone_fd)) {
+ // tombstoned_notify_completion() logs failure.
+ status = PaletteStatus::kFailedCheckLog;
+ }
+
+ return status;
+}
+
+enum PaletteStatus PaletteTraceEnabled(/*out*/ int32_t* enabled) {
+ *enabled = (ATRACE_ENABLED() != 0) ? 1 : 0;
+ return PaletteStatus::kOkay;
+}
+
+enum PaletteStatus PaletteTraceBegin(const char* name) {
+ ATRACE_BEGIN(name);
+ return PaletteStatus::kOkay;
+}
+
+enum PaletteStatus PaletteTraceEnd() {
+ ATRACE_END();
+ return PaletteStatus::kOkay;
+}
+
+enum PaletteStatus PaletteTraceIntegerValue(const char* name, int32_t value) {
+ ATRACE_INT(name, value);
+ return PaletteStatus::kOkay;
+}
+
+// Flag whether to use legacy ashmem or current (b/139855428)
+static std::atomic_bool g_assume_legacy_ashmemd(false);
+
+enum PaletteStatus PaletteAshmemCreateRegion(const char* name, size_t size, int* fd) {
+ if (g_assume_legacy_ashmemd.load(std::memory_order_acquire) == false) {
+ // Current platform behaviour which open ashmem fd in process (b/139855428)
+ *fd = ashmem_create_region(name, size);
+ if (*fd >= 0) {
+ return PaletteStatus::kOkay;
+ }
+ }
+
+ // Try legacy behavior just required for ART build bots which may be running tests on older
+ // platform builds.
+
+ // We implement our own ashmem creation, as the libcutils implementation does
+ // a binder call, and our only use of ashmem in ART is for zygote, which
+ // cannot communicate to binder.
+ *fd = TEMP_FAILURE_RETRY(open("/dev/ashmem", O_RDWR | O_CLOEXEC));
+ if (*fd == -1) {
+ return PaletteStatus::kCheckErrno;
+ }
+
+ if (TEMP_FAILURE_RETRY(ioctl(*fd, ASHMEM_SET_SIZE, size)) < 0) {
+ goto error;
+ }
+
+ if (name != nullptr) {
+ char buf[ASHMEM_NAME_LEN] = {0};
+ strlcpy(buf, name, sizeof(buf));
+ if (TEMP_FAILURE_RETRY(ioctl(*fd, ASHMEM_SET_NAME, buf)) < 0) {
+ goto error;
+ }
+ }
+
+ g_assume_legacy_ashmemd.store(true, std::memory_order_release);
+ return PaletteStatus::kOkay;
+
+error:
+ // Save errno before closing.
+ int save_errno = errno;
+ close(*fd);
+ errno = save_errno;
+ return PaletteStatus::kCheckErrno;
+}
+
+enum PaletteStatus PaletteAshmemSetProtRegion(int fd, int prot) {
+ if (!g_assume_legacy_ashmemd.load(std::memory_order_acquire)) {
+ if (ashmem_set_prot_region(fd, prot) < 0) {
+ return PaletteStatus::kCheckErrno;
+ }
+ } else if (TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_PROT_MASK, prot)) < 0) {
+ // Legacy behavior just required for ART build bots which may be running tests on older
+ // platform builds.
+ return PaletteStatus::kCheckErrno;
+ }
+ return PaletteStatus::kOkay;
+}
diff --git a/palette_fake.cc b/palette_fake.cc
new file mode 100644
index 0000000..17e3d00
--- /dev/null
+++ b/palette_fake.cc
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "palette/palette.h"
+
+#include <map>
+#include <mutex>
+
+#include <android-base/logging.h>
+#include <android-base/macros.h> // For ATTRIBUTE_UNUSED
+
+#include "palette_system.h"
+
+enum PaletteStatus PaletteGetVersion(int32_t* version) {
+ *version = art::palette::kPaletteVersion;
+ return PaletteStatus::kOkay;
+}
+
+// Cached thread priority for testing. No thread priorities are ever affected.
+static std::mutex g_tid_priority_map_mutex;
+static std::map<int32_t, int32_t> g_tid_priority_map;
+
+enum PaletteStatus PaletteSchedSetPriority(int32_t tid, int32_t priority) {
+ if (priority < art::palette::kMinManagedThreadPriority ||
+ priority > art::palette::kMaxManagedThreadPriority) {
+ return PaletteStatus::kInvalidArgument;
+ }
+ std::lock_guard guard(g_tid_priority_map_mutex);
+ g_tid_priority_map[tid] = priority;
+ return PaletteStatus::kOkay;
+}
+
+enum PaletteStatus PaletteSchedGetPriority(int32_t tid,
+ /*out*/ int32_t* priority) {
+ std::lock_guard guard(g_tid_priority_map_mutex);
+ if (g_tid_priority_map.find(tid) == g_tid_priority_map.end()) {
+ g_tid_priority_map[tid] = art::palette::kNormalManagedThreadPriority;
+ }
+ *priority = g_tid_priority_map[tid];
+ return PaletteStatus::kOkay;
+}
+
+enum PaletteStatus PaletteWriteCrashThreadStacks(/*in*/ const char* stacks, size_t stacks_len) {
+ LOG(INFO) << std::string_view(stacks, stacks_len);
+ return PaletteStatus::kOkay;
+}
+
+enum PaletteStatus PaletteTraceEnabled(/*out*/ int32_t* enabled) {
+ *enabled = 0;
+ return PaletteStatus::kOkay;
+}
+
+enum PaletteStatus PaletteTraceBegin(const char* name ATTRIBUTE_UNUSED) {
+ return PaletteStatus::kOkay;
+}
+
+enum PaletteStatus PaletteTraceEnd() {
+ return PaletteStatus::kOkay;
+}
+
+enum PaletteStatus PaletteTraceIntegerValue(const char* name ATTRIBUTE_UNUSED,
+ int32_t value ATTRIBUTE_UNUSED) {
+ return PaletteStatus::kOkay;
+}
+
+enum PaletteStatus PaletteAshmemCreateRegion(const char* name ATTRIBUTE_UNUSED,
+ size_t size ATTRIBUTE_UNUSED, int* fd) {
+ *fd = -1;
+ return PaletteStatus::kNotSupported;
+}
+
+enum PaletteStatus PaletteAshmemSetProtRegion(int fd ATTRIBUTE_UNUSED, int prot ATTRIBUTE_UNUSED) {
+ return PaletteStatus::kNotSupported;
+}
diff --git a/palette_system.h b/palette_system.h
new file mode 100644
index 0000000..e238113
--- /dev/null
+++ b/palette_system.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ART_LIBARTPALETTE_SYSTEM_PALETTE_SYSTEM_H_
+#define ART_LIBARTPALETTE_SYSTEM_PALETTE_SYSTEM_H_
+
+#include <stdint.h>
+
+namespace art {
+namespace palette {
+
+static constexpr int32_t kPaletteVersion = 1;
+
+// Managed thread definitions
+static constexpr int32_t kNormalManagedThreadPriority = 5;
+static constexpr int32_t kMinManagedThreadPriority = 1;
+static constexpr int32_t kMaxManagedThreadPriority = 10;
+static constexpr int32_t kNumManagedThreadPriorities =
+ kMaxManagedThreadPriority - kMinManagedThreadPriority + 1;
+
+} // namespace palette
+} // namespace art
+
+#endif // ART_LIBARTPALETTE_SYSTEM_PALETTE_SYSTEM_H_