blob: 511f18440838d5149fffaf757b03f2e9731e0199 [file] [log] [blame]
Yabin Cui323e9452015-04-20 18:07:17 -07001/*
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
Yabin Cuiea105c82020-07-22 13:01:31 -070019#include <filesystem>
20
Yabin Cui63a1c3d2017-05-19 12:57:44 -070021#include <android-base/file.h>
22
23#include "dso.h"
Yabin Cui9759e1b2015-04-28 15:54:13 -070024#include "environment.h"
Yabin Cuiea105c82020-07-22 13:01:31 -070025#include "get_test_data.h"
Yabin Cuic13ff892020-11-10 13:11:01 -080026#include "test_util.h"
27#include "thread_tree.h"
Yabin Cuiea105c82020-07-22 13:01:31 -070028
29namespace fs = std::filesystem;
Yabin Cuifaa7b922021-01-11 17:35:57 -080030using namespace simpleperf;
Yabin Cui323e9452015-04-20 18:07:17 -070031
Yabin Cui63a1c3d2017-05-19 12:57:44 -070032TEST(environment, PrepareVdsoFile) {
33 std::string content;
34 ASSERT_TRUE(android::base::ReadFileToString("/proc/self/maps", &content));
35 if (content.find("[vdso]") == std::string::npos) {
36 // Vdso isn't used, no need to test.
37 return;
38 }
Yabin Cui47ba6562018-01-08 11:48:12 -080039 TemporaryDir tmpdir;
Yabin Cui554f3bb2021-05-05 13:34:59 -070040 auto scoped_temp_files = ScopedTempFiles::Create(tmpdir.path);
41 ASSERT_TRUE(scoped_temp_files);
Yabin Cui63a1c3d2017-05-19 12:57:44 -070042 PrepareVdsoFile();
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +020043 std::unique_ptr<Dso> dso =
44 Dso::CreateDso(DSO_ELF_FILE, "[vdso]", sizeof(size_t) == sizeof(uint64_t));
Yabin Cui63a1c3d2017-05-19 12:57:44 -070045 ASSERT_TRUE(dso != nullptr);
Yabin Cui47ba6562018-01-08 11:48:12 -080046 ASSERT_NE(dso->GetDebugFilePath(), "[vdso]");
Yabin Cui63a1c3d2017-05-19 12:57:44 -070047}
Yabin Cui8faa6152018-06-07 15:52:57 -070048
49TEST(environment, GetHardwareFromCpuInfo) {
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +020050 std::string cpu_info =
51 "CPU revision : 10\n\n"
Yabin Cui8faa6152018-06-07 15:52:57 -070052 "Hardware : Symbol i.MX6 Freeport_Plat Quad/DualLite (Device Tree)\n";
53 ASSERT_EQ("Symbol i.MX6 Freeport_Plat Quad/DualLite (Device Tree)",
54 GetHardwareFromCpuInfo(cpu_info));
55}
Yabin Cui2db05b42018-07-16 14:04:49 -070056
57TEST(environment, MappedFileOnlyExistInMemory) {
58 ASSERT_TRUE(MappedFileOnlyExistInMemory(""));
59 ASSERT_TRUE(MappedFileOnlyExistInMemory("[stack]"));
60 ASSERT_TRUE(MappedFileOnlyExistInMemory("[anon:.bss]"));
61 ASSERT_FALSE(MappedFileOnlyExistInMemory("[vdso]"));
62 ASSERT_TRUE(MappedFileOnlyExistInMemory("/dev/__properties__/u:object_r"));
63 ASSERT_TRUE(MappedFileOnlyExistInMemory("//anon"));
Yabin Cui739b1542018-10-17 17:19:56 -070064 ASSERT_TRUE(MappedFileOnlyExistInMemory("/memfd:/jit-cache"));
Yabin Cui2db05b42018-07-16 14:04:49 -070065 ASSERT_FALSE(MappedFileOnlyExistInMemory("./TemporaryFile-12345"));
66 ASSERT_FALSE(MappedFileOnlyExistInMemory("/system/lib64/libc.so"));
67}
Yabin Cui6e173a42018-08-13 15:58:25 -070068
69TEST(environment, SetPerfEventLimits) {
70#if defined(__ANDROID__)
71 if (GetAndroidVersion() <= kAndroidVersionP) {
72 return;
73 }
74 uint64_t orig_freq = 100000;
75 size_t orig_percent = 25;
76 uint64_t orig_mlock_kb = 516;
77 bool has_freq = GetMaxSampleFrequency(&orig_freq);
78 bool has_percent = GetCpuTimeMaxPercent(&orig_percent);
79 bool has_mlock_kb = GetPerfEventMlockKb(&orig_mlock_kb);
80
81 ASSERT_TRUE(SetPerfEventLimits(orig_freq + 1, orig_percent + 1, orig_mlock_kb + 1));
82 if (has_freq) {
83 uint64_t value;
84 ASSERT_TRUE(GetMaxSampleFrequency(&value));
85 ASSERT_EQ(value, orig_freq + 1);
86 }
87 if (has_percent) {
88 size_t value;
89 ASSERT_TRUE(GetCpuTimeMaxPercent(&value));
90 ASSERT_EQ(value, orig_percent + 1);
91 }
92 if (has_mlock_kb) {
93 uint64_t value;
94 ASSERT_TRUE(GetPerfEventMlockKb(&value));
95 ASSERT_EQ(value, orig_mlock_kb + 1);
96 }
97 // Restore the environment.
98 ASSERT_TRUE(SetPerfEventLimits(orig_freq, orig_percent, orig_mlock_kb));
99#else // !defined(__ANDROID__)
100 GTEST_LOG_(INFO) << "This test tests setting properties on Android.";
101#endif
102}
Yabin Cui84c55ff2020-07-09 14:14:22 -0700103
104TEST(environment, GetKernelVersion) {
Yabin Cuid703bb32021-01-05 16:45:52 -0800105 ASSERT_TRUE(GetKernelVersion());
Yabin Cui84c55ff2020-07-09 14:14:22 -0700106}
Yabin Cuiea105c82020-07-22 13:01:31 -0700107
108TEST(environment, GetModuleBuildId) {
109 BuildId build_id;
110 fs::path dir(GetTestData("sysfs/module/fake_kernel_module/notes"));
111 ASSERT_TRUE(fs::copy_file(dir / "note.gnu.build-id", dir / ".note.gnu.build-id",
112 fs::copy_options::overwrite_existing));
113 ASSERT_TRUE(GetModuleBuildId("fake_kernel_module", &build_id, GetTestData("sysfs")));
114 ASSERT_EQ(build_id, BuildId("3e0ba155286f3454"));
115}
Yabin Cuic13ff892020-11-10 13:11:01 -0800116
117TEST(environment, GetKernelAndModuleMmaps) {
118 TEST_REQUIRE_ROOT();
119 KernelMmap kernel_mmap;
120 std::vector<KernelMmap> module_mmaps;
121 GetKernelAndModuleMmaps(&kernel_mmap, &module_mmaps);
122 // The kernel map should contain the kernel start address.
123 ASSERT_EQ(kernel_mmap.name, std::string(DEFAULT_KERNEL_MMAP_NAME) + "_stext");
124 ASSERT_GT(kernel_mmap.start_addr, 0);
125}
Yabin Cuia89a3742021-02-11 13:14:54 -0800126
127TEST(environment, GetProcessUid) {
128 std::optional<uid_t> uid = GetProcessUid(getpid());
129 ASSERT_TRUE(uid.has_value());
130 ASSERT_EQ(uid.value(), getuid());
131}
Yabin Cuidb19d6d2021-05-12 17:36:02 -0700132
133TEST(environment, GetAppType) {
134 TEST_REQUIRE_APPS();
135 ASSERT_EQ(GetAppType("com.android.simpleperf.debuggable"), "debuggable");
136 ASSERT_EQ(GetAppType("com.android.simpleperf.profileable"), "profileable");
137 ASSERT_EQ(GetAppType("com.android.simpleperf.app_not_exist"), "not_exist");
138}
Yabin Cuicfdc7832023-03-02 15:35:01 -0800139
140TEST(environment, GetMemorySize) {
141 auto value = GetMemorySize();
142 ASSERT_TRUE(value);
143 ASSERT_GT(value.value(), 0);
144}