xref: /aosp_15_r20/system/extras/simpleperf/environment_test.cpp (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1 /*
2  * Copyright (C) 2015 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 <gtest/gtest.h>
18 
19 #include <filesystem>
20 
21 #include <android-base/file.h>
22 
23 #include "dso.h"
24 #include "environment.h"
25 #include "get_test_data.h"
26 #include "test_util.h"
27 #include "thread_tree.h"
28 
29 namespace fs = std::filesystem;
30 using namespace simpleperf;
31 
32 // @CddTest = 6.1/C-0-2
TEST(environment,PrepareVdsoFile)33 TEST(environment, PrepareVdsoFile) {
34   std::string content;
35   ASSERT_TRUE(android::base::ReadFileToString("/proc/self/maps", &content));
36   if (content.find("[vdso]") == std::string::npos) {
37     // Vdso isn't used, no need to test.
38     return;
39   }
40   TemporaryDir tmpdir;
41   auto scoped_temp_files = ScopedTempFiles::Create(tmpdir.path);
42   ASSERT_TRUE(scoped_temp_files);
43   PrepareVdsoFile();
44   std::unique_ptr<Dso> dso =
45       Dso::CreateDso(DSO_ELF_FILE, "[vdso]", sizeof(size_t) == sizeof(uint64_t));
46   ASSERT_TRUE(dso != nullptr);
47   ASSERT_NE(dso->GetDebugFilePath(), "[vdso]");
48 }
49 
50 // @CddTest = 6.1/C-0-2
TEST(environment,GetHardwareFromCpuInfo)51 TEST(environment, GetHardwareFromCpuInfo) {
52   std::string cpu_info =
53       "CPU revision : 10\n\n"
54       "Hardware : Symbol i.MX6 Freeport_Plat Quad/DualLite (Device Tree)\n";
55   ASSERT_EQ("Symbol i.MX6 Freeport_Plat Quad/DualLite (Device Tree)",
56             GetHardwareFromCpuInfo(cpu_info));
57 }
58 
59 // @CddTest = 6.1/C-0-2
TEST(environment,MappedFileOnlyExistInMemory)60 TEST(environment, MappedFileOnlyExistInMemory) {
61   ASSERT_TRUE(MappedFileOnlyExistInMemory(""));
62   ASSERT_TRUE(MappedFileOnlyExistInMemory("[stack]"));
63   ASSERT_TRUE(MappedFileOnlyExistInMemory("[anon:.bss]"));
64   ASSERT_FALSE(MappedFileOnlyExistInMemory("[vdso]"));
65   ASSERT_TRUE(MappedFileOnlyExistInMemory("/dev/__properties__/u:object_r"));
66   ASSERT_TRUE(MappedFileOnlyExistInMemory("//anon"));
67   ASSERT_TRUE(MappedFileOnlyExistInMemory("/memfd:/jit-cache"));
68   ASSERT_FALSE(MappedFileOnlyExistInMemory("./TemporaryFile-12345"));
69   ASSERT_FALSE(MappedFileOnlyExistInMemory("/system/lib64/libc.so"));
70 }
71 
72 // @CddTest = 6.1/C-0-2
TEST(environment,SetPerfEventLimits)73 TEST(environment, SetPerfEventLimits) {
74 #if defined(__ANDROID__)
75   if (GetAndroidVersion() <= kAndroidVersionP) {
76     return;
77   }
78   uint64_t orig_freq = 100000;
79   size_t orig_percent = 25;
80   uint64_t orig_mlock_kb = 516;
81   bool has_freq = GetMaxSampleFrequency(&orig_freq);
82   bool has_percent = GetCpuTimeMaxPercent(&orig_percent);
83   bool has_mlock_kb = GetPerfEventMlockKb(&orig_mlock_kb);
84 
85   ASSERT_TRUE(SetPerfEventLimits(orig_freq + 1, orig_percent + 1, orig_mlock_kb + 1));
86   if (has_freq) {
87     uint64_t value;
88     ASSERT_TRUE(GetMaxSampleFrequency(&value));
89     ASSERT_EQ(value, orig_freq + 1);
90   }
91   if (has_percent) {
92     size_t value;
93     ASSERT_TRUE(GetCpuTimeMaxPercent(&value));
94     ASSERT_EQ(value, orig_percent + 1);
95   }
96   if (has_mlock_kb) {
97     uint64_t value;
98     ASSERT_TRUE(GetPerfEventMlockKb(&value));
99     ASSERT_EQ(value, orig_mlock_kb + 1);
100   }
101   // Restore the environment.
102   ASSERT_TRUE(SetPerfEventLimits(orig_freq, orig_percent, orig_mlock_kb));
103 #else  // !defined(__ANDROID__)
104   GTEST_LOG_(INFO) << "This test tests setting properties on Android.";
105 #endif
106 }
107 
108 // @CddTest = 6.1/C-0-2
TEST(environment,GetKernelVersion)109 TEST(environment, GetKernelVersion) {
110   ASSERT_TRUE(GetKernelVersion());
111 }
112 
113 // @CddTest = 6.1/C-0-2
TEST(environment,GetModuleBuildId)114 TEST(environment, GetModuleBuildId) {
115   BuildId build_id;
116   fs::path dir(GetTestData("sysfs/module/fake_kernel_module/notes"));
117   ASSERT_TRUE(fs::copy_file(dir / "note.gnu.build-id", dir / ".note.gnu.build-id",
118                             fs::copy_options::overwrite_existing));
119   ASSERT_TRUE(GetModuleBuildId("fake_kernel_module", &build_id, GetTestData("sysfs")));
120   ASSERT_EQ(build_id, BuildId("3e0ba155286f3454"));
121 }
122 
123 // @CddTest = 6.1/C-0-2
TEST(environment,GetKernelAndModuleMmaps)124 TEST(environment, GetKernelAndModuleMmaps) {
125   TEST_REQUIRE_ROOT();
126   KernelMmap kernel_mmap;
127   std::vector<KernelMmap> module_mmaps;
128   GetKernelAndModuleMmaps(&kernel_mmap, &module_mmaps);
129   // The kernel map should contain the kernel start address.
130   ASSERT_EQ(kernel_mmap.name, std::string(DEFAULT_KERNEL_MMAP_NAME) + "_stext");
131   ASSERT_GT(kernel_mmap.start_addr, 0);
132 }
133 
134 // @CddTest = 6.1/C-0-2
TEST(environment,GetProcessUid)135 TEST(environment, GetProcessUid) {
136   std::optional<uid_t> uid = GetProcessUid(getpid());
137   ASSERT_TRUE(uid.has_value());
138   ASSERT_EQ(uid.value(), getuid());
139 }
140 
141 // @CddTest = 6.1/C-0-2
TEST(environment,GetAppType)142 TEST(environment, GetAppType) {
143   TEST_REQUIRE_APPS();
144   ASSERT_EQ(GetAppType("com.android.simpleperf.debuggable"), "debuggable");
145   ASSERT_EQ(GetAppType("com.android.simpleperf.profileable"), "profileable");
146   ASSERT_EQ(GetAppType("com.android.simpleperf.app_not_exist"), "not_exist");
147 }
148 
149 // @CddTest = 6.1/C-0-2
TEST(environment,GetMemorySize)150 TEST(environment, GetMemorySize) {
151   auto value = GetMemorySize();
152   ASSERT_GT(value, 0);
153 }
154 
155 // @CddTest = 6.1/C-0-2
TEST(environment,GetCpuModels)156 TEST(environment, GetCpuModels) {
157 #if defined(__ANDROID__)
158   auto models = GetCpuModels();
159   ASSERT_FALSE(models.empty());
160   ASSERT_FALSE(models[0].cpus.empty());
161   ASSERT_EQ(models[0].cpus[0], 0);
162 #endif  // defined(__ANDROID__)
163 }
164