xref: /aosp_15_r20/art/libartpalette/apex/palette_test.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
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 #include "palette/palette.h"
18 
19 #include <jni.h>
20 #include <sys/mman.h>
21 #include <sys/syscall.h>
22 #include <unistd.h>
23 
24 #include <cstring>
25 
26 #include "base/common_art_test.h"
27 #include "gtest/gtest.h"
28 
29 #ifdef ART_TARGET_ANDROID
30 #include "android-modules-utils/sdk_level.h"
31 #include "android/api-level.h"
32 #endif
33 
34 namespace {
35 
GetTid()36 pid_t GetTid() {
37 #ifdef __BIONIC__
38   return gettid();
39 #else  // __BIONIC__
40   return syscall(__NR_gettid);
41 #endif  // __BIONIC__
42 }
43 
44 #ifdef ART_TARGET_ANDROID
PaletteSetTaskProfilesIsSupported(palette_status_t res)45 bool PaletteSetTaskProfilesIsSupported(palette_status_t res) {
46   if (android::modules::sdklevel::IsAtLeastU()) {
47     return true;
48   }
49   EXPECT_EQ(PALETTE_STATUS_NOT_SUPPORTED, res)
50       << "Device API level: " << android_get_device_api_level();
51   return false;
52 }
PaletteDebugStoreIsSupported()53 bool PaletteDebugStoreIsSupported() {
54   // TODO(b/345433959): Switch to android::modules::sdklevel::IsAtLeastW
55   return android_get_device_api_level() >= 36;
56 }
57 #endif
58 
59 }  // namespace
60 
61 class PaletteClientTest : public testing::Test {};
62 
TEST_F(PaletteClientTest,SchedPriority)63 TEST_F(PaletteClientTest, SchedPriority) {
64   int32_t tid = GetTid();
65   int32_t saved_priority;
66   EXPECT_EQ(PALETTE_STATUS_OK, PaletteSchedGetPriority(tid, &saved_priority));
67 
68   EXPECT_EQ(PALETTE_STATUS_INVALID_ARGUMENT, PaletteSchedSetPriority(tid, /*java_priority=*/ 0));
69   EXPECT_EQ(PALETTE_STATUS_INVALID_ARGUMENT, PaletteSchedSetPriority(tid, /*java_priority=*/ -1));
70   EXPECT_EQ(PALETTE_STATUS_INVALID_ARGUMENT, PaletteSchedSetPriority(tid, /*java_priority=*/ 11));
71 
72   EXPECT_EQ(PALETTE_STATUS_OK, PaletteSchedSetPriority(tid, /*java_priority=*/ 1));
73   EXPECT_EQ(PALETTE_STATUS_OK, PaletteSchedSetPriority(tid, saved_priority));
74 }
75 
TEST_F(PaletteClientTest,Trace)76 TEST_F(PaletteClientTest, Trace) {
77   bool enabled = false;
78   EXPECT_EQ(PALETTE_STATUS_OK, PaletteTraceEnabled(&enabled));
79   EXPECT_EQ(PALETTE_STATUS_OK, PaletteTraceBegin("Hello world!"));
80   EXPECT_EQ(PALETTE_STATUS_OK, PaletteTraceEnd());
81   EXPECT_EQ(PALETTE_STATUS_OK, PaletteTraceIntegerValue("Beans", /*value=*/ 3));
82 }
83 
TEST_F(PaletteClientTest,Ashmem)84 TEST_F(PaletteClientTest, Ashmem) {
85 #ifndef ART_TARGET_ANDROID
86   GTEST_SKIP() << "ashmem is only supported on Android";
87 #else
88   int fd;
89   EXPECT_EQ(PALETTE_STATUS_OK, PaletteAshmemCreateRegion("ashmem-test", 4096, &fd));
90   EXPECT_EQ(PALETTE_STATUS_OK, PaletteAshmemSetProtRegion(fd, PROT_READ | PROT_EXEC));
91   EXPECT_EQ(0, close(fd));
92 #endif
93 }
94 
95 class PaletteClientJniTest : public art::CommonArtTest {};
96 
TEST_F(PaletteClientJniTest,JniInvocation)97 TEST_F(PaletteClientJniTest, JniInvocation) {
98   bool enabled;
99   EXPECT_EQ(PALETTE_STATUS_OK, PaletteShouldReportJniInvocations(&enabled));
100 
101   std::string boot_class_path_string =
102       GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames());
103   std::string boot_class_path_locations_string =
104       GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations());
105 
106   JavaVMOption options[] = {
107       {.optionString = boot_class_path_string.c_str(), .extraInfo = nullptr},
108       {.optionString = boot_class_path_locations_string.c_str(), .extraInfo = nullptr},
109   };
110   JavaVMInitArgs vm_args = {
111       .version = JNI_VERSION_1_6,
112       .nOptions = std::size(options),
113       .options = options,
114       .ignoreUnrecognized = JNI_TRUE,
115   };
116 
117   JavaVM* jvm = nullptr;
118   JNIEnv* env = nullptr;
119   EXPECT_EQ(JNI_OK, JNI_CreateJavaVM(&jvm, &env, &vm_args));
120   ASSERT_NE(nullptr, env);
121 
122   PaletteNotifyBeginJniInvocation(env);
123   PaletteNotifyEndJniInvocation(env);
124 
125   EXPECT_EQ(JNI_OK, jvm->DestroyJavaVM());
126 }
127 
TEST_F(PaletteClientTest,SetTaskProfiles)128 TEST_F(PaletteClientTest, SetTaskProfiles) {
129 #ifndef ART_TARGET_ANDROID
130   GTEST_SKIP() << "SetTaskProfiles is only supported on Android";
131 #else
132   const char* profiles[] = {"ProcessCapacityHigh", "TimerSlackNormal"};
133   palette_status_t res = PaletteSetTaskProfiles(GetTid(), &profiles[0], 2);
134   if (PaletteSetTaskProfilesIsSupported(res)) {
135     // SetTaskProfiles will only work fully if we run as root. Otherwise it'll
136     // return false which is mapped to PALETTE_STATUS_FAILED_CHECK_LOG.
137     if (getuid() == 0) {
138       EXPECT_EQ(PALETTE_STATUS_OK, res);
139     } else {
140       EXPECT_EQ(PALETTE_STATUS_FAILED_CHECK_LOG, res);
141     }
142   }
143 #endif
144 }
145 
TEST_F(PaletteClientTest,SetTaskProfilesCpp)146 TEST_F(PaletteClientTest, SetTaskProfilesCpp) {
147 #ifndef ART_TARGET_ANDROID
148   GTEST_SKIP() << "SetTaskProfiles is only supported on Android";
149 #else
150   std::vector<std::string> profiles = {"ProcessCapacityHigh", "TimerSlackNormal"};
151   palette_status_t res = PaletteSetTaskProfiles(GetTid(), profiles);
152   if (PaletteSetTaskProfilesIsSupported(res)) {
153     // SetTaskProfiles will only work fully if we run as root. Otherwise it'll
154     // return false which is mapped to PALETTE_STATUS_FAILED_CHECK_LOG.
155     if (getuid() == 0) {
156       EXPECT_EQ(PALETTE_STATUS_OK, res);
157     } else {
158       EXPECT_EQ(PALETTE_STATUS_FAILED_CHECK_LOG, res);
159     }
160   }
161 #endif
162 }
163 
TEST_F(PaletteClientTest,DebugStore)164 TEST_F(PaletteClientTest, DebugStore) {
165 #ifndef ART_TARGET_ANDROID
166   GTEST_SKIP() << "DebugStore is only supported on Android";
167 #else
168   std::array<char, 20> result{};
169   // Make sure the we are on a correct API level.
170   if (!PaletteDebugStoreIsSupported()) {
171     GTEST_SKIP() << "DebugStore is only supported on API 36+";
172   }
173   palette_status_t pstatus = PaletteDebugStoreGetString(result.data(), result.size());
174   EXPECT_EQ(PALETTE_STATUS_OK, pstatus);
175 
176   size_t len = strnlen(result.data(), result.size());
177   EXPECT_TRUE(len < result.size());
178 
179   const char* start = "1,0,";
180   const char* end = "::";
181   EXPECT_TRUE(len > strlen(start) + strlen(end));
182   EXPECT_EQ(strncmp(result.data() + len - strlen(end), end, strlen(end)), 0);
183 #endif
184 }
185