• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <filesystem>
25 
26 #ifdef ART_TARGET_ANDROID
27 #include "android-modules-utils/sdk_level.h"
28 #include "android/api-level.h"
29 #endif
30 
31 #include "base/common_art_test.h"
32 #include "gtest/gtest.h"
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 }
53 #endif
54 
55 }  // namespace
56 
57 class PaletteClientTest : public testing::Test {};
58 
TEST_F(PaletteClientTest,SchedPriority)59 TEST_F(PaletteClientTest, SchedPriority) {
60   int32_t tid = GetTid();
61   int32_t saved_priority;
62   EXPECT_EQ(PALETTE_STATUS_OK, PaletteSchedGetPriority(tid, &saved_priority));
63 
64   EXPECT_EQ(PALETTE_STATUS_INVALID_ARGUMENT, PaletteSchedSetPriority(tid, /*java_priority=*/ 0));
65   EXPECT_EQ(PALETTE_STATUS_INVALID_ARGUMENT, PaletteSchedSetPriority(tid, /*java_priority=*/ -1));
66   EXPECT_EQ(PALETTE_STATUS_INVALID_ARGUMENT, PaletteSchedSetPriority(tid, /*java_priority=*/ 11));
67 
68   EXPECT_EQ(PALETTE_STATUS_OK, PaletteSchedSetPriority(tid, /*java_priority=*/ 1));
69   EXPECT_EQ(PALETTE_STATUS_OK, PaletteSchedSetPriority(tid, saved_priority));
70 }
71 
TEST_F(PaletteClientTest,Trace)72 TEST_F(PaletteClientTest, Trace) {
73   bool enabled = false;
74   EXPECT_EQ(PALETTE_STATUS_OK, PaletteTraceEnabled(&enabled));
75   EXPECT_EQ(PALETTE_STATUS_OK, PaletteTraceBegin("Hello world!"));
76   EXPECT_EQ(PALETTE_STATUS_OK, PaletteTraceEnd());
77   EXPECT_EQ(PALETTE_STATUS_OK, PaletteTraceIntegerValue("Beans", /*value=*/ 3));
78 }
79 
TEST_F(PaletteClientTest,Ashmem)80 TEST_F(PaletteClientTest, Ashmem) {
81 #ifndef ART_TARGET_ANDROID
82   GTEST_SKIP() << "ashmem is only supported on Android";
83 #else
84   int fd;
85   EXPECT_EQ(PALETTE_STATUS_OK, PaletteAshmemCreateRegion("ashmem-test", 4096, &fd));
86   EXPECT_EQ(PALETTE_STATUS_OK, PaletteAshmemSetProtRegion(fd, PROT_READ | PROT_EXEC));
87   EXPECT_EQ(0, close(fd));
88 #endif
89 }
90 
91 class PaletteClientJniTest : public art::CommonArtTest {};
92 
TEST_F(PaletteClientJniTest,JniInvocation)93 TEST_F(PaletteClientJniTest, JniInvocation) {
94   bool enabled;
95   EXPECT_EQ(PALETTE_STATUS_OK, PaletteShouldReportJniInvocations(&enabled));
96 
97   std::string boot_class_path_string =
98       GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames());
99   std::string boot_class_path_locations_string =
100       GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations());
101 
102   JavaVMOption options[] = {
103       {.optionString = boot_class_path_string.c_str(), .extraInfo = nullptr},
104       {.optionString = boot_class_path_locations_string.c_str(), .extraInfo = nullptr},
105   };
106   JavaVMInitArgs vm_args = {
107       .version = JNI_VERSION_1_6,
108       .nOptions = std::size(options),
109       .options = options,
110       .ignoreUnrecognized = JNI_TRUE,
111   };
112 
113   JavaVM* jvm = nullptr;
114   JNIEnv* env = nullptr;
115   EXPECT_EQ(JNI_OK, JNI_CreateJavaVM(&jvm, &env, &vm_args));
116   ASSERT_NE(nullptr, env);
117 
118   PaletteNotifyBeginJniInvocation(env);
119   PaletteNotifyEndJniInvocation(env);
120 
121   EXPECT_EQ(JNI_OK, jvm->DestroyJavaVM());
122 }
123 
TEST_F(PaletteClientTest,SetTaskProfiles)124 TEST_F(PaletteClientTest, SetTaskProfiles) {
125 #ifndef ART_TARGET_ANDROID
126   GTEST_SKIP() << "SetTaskProfiles is only supported on Android";
127 #else
128   if (!std::filesystem::exists("/sys/fs/cgroup/cgroup.controllers")) {
129     // This is intended to detect ART chroot setups, where SetTaskProfiles won't work.
130     GTEST_SKIP() << "Kernel cgroup support missing";
131   }
132 
133   const char* profiles[] = {"ProcessCapacityHigh", "TimerSlackNormal"};
134   palette_status_t res = PaletteSetTaskProfiles(GetTid(), &profiles[0], 2);
135   if (PaletteSetTaskProfilesIsSupported(res)) {
136     // SetTaskProfiles will only work fully if we run as root. Otherwise it'll
137     // return false which is mapped to PALETTE_STATUS_FAILED_CHECK_LOG.
138     if (getuid() == 0) {
139       EXPECT_EQ(PALETTE_STATUS_OK, res);
140     } else {
141       EXPECT_EQ(PALETTE_STATUS_FAILED_CHECK_LOG, res);
142     }
143   }
144 #endif
145 }
146 
TEST_F(PaletteClientTest,SetTaskProfilesCpp)147 TEST_F(PaletteClientTest, SetTaskProfilesCpp) {
148 #ifndef ART_TARGET_ANDROID
149   GTEST_SKIP() << "SetTaskProfiles is only supported on Android";
150 #else
151   if (!std::filesystem::exists("/sys/fs/cgroup/cgroup.controllers")) {
152     // This is intended to detect ART chroot setups, where SetTaskProfiles won't work.
153     GTEST_SKIP() << "Kernel cgroup support missing";
154   }
155 
156   std::vector<std::string> profiles = {"ProcessCapacityHigh", "TimerSlackNormal"};
157   palette_status_t res = PaletteSetTaskProfiles(GetTid(), profiles);
158   if (PaletteSetTaskProfilesIsSupported(res)) {
159     // SetTaskProfiles will only work fully if we run as root. Otherwise it'll
160     // return false which is mapped to PALETTE_STATUS_FAILED_CHECK_LOG.
161     if (getuid() == 0) {
162       EXPECT_EQ(PALETTE_STATUS_OK, res);
163     } else {
164       EXPECT_EQ(PALETTE_STATUS_FAILED_CHECK_LOG, res);
165     }
166   }
167 #endif
168 }
169