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