blob: 8efb6fe7af24a50426af16d32dcf198d41a67cd8 [file] [log] [blame]
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -07001/*
2 * Copyright 2020 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#undef LOG_TAG
18#define LOG_TAG "gpuservice_unittest"
19
20#include <android-base/stringprintf.h>
Maciej Żenczykowski842ec2d2022-06-16 18:52:17 +000021#define BPF_MAP_MAKE_VISIBLE_FOR_TESTING
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -070022#include <bpf/BpfMap.h>
23#include <gmock/gmock.h>
24#include <gpumem/GpuMem.h>
25#include <gtest/gtest.h>
26#include <inttypes.h>
27#include <utils/String16.h>
28#include <utils/Vector.h>
29
30#include "TestableGpuMem.h"
31
32namespace android {
33namespace {
34
35using base::StringPrintf;
36using testing::HasSubstr;
37
38constexpr uint32_t TEST_MAP_SIZE = 10;
39constexpr uint64_t TEST_GLOBAL_KEY = 0;
40constexpr uint64_t TEST_GLOBAL_VAL = 123;
41constexpr uint64_t TEST_PROC_KEY_1 = 1;
42constexpr uint64_t TEST_PROC_VAL_1 = 234;
43constexpr uint64_t TEST_PROC_KEY_2 = 4294967298; // (1 << 32) + 2
44constexpr uint64_t TEST_PROC_VAL_2 = 345;
Yiwei Zhang643a4f62020-08-13 17:39:51 -070045constexpr uint32_t TEST_KEY_MASK = 0x1 | 0x2 | 0x4;
46constexpr uint32_t TEST_KEY_COUNT = 3;
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -070047
48class GpuMemTest : public testing::Test {
49public:
50 GpuMemTest() {
51 const ::testing::TestInfo* const test_info =
52 ::testing::UnitTest::GetInstance()->current_test_info();
53 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
54 }
55
56 ~GpuMemTest() {
57 const ::testing::TestInfo* const test_info =
58 ::testing::UnitTest::GetInstance()->current_test_info();
59 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
60 }
61
62 void SetUp() override {
Yiwei Zhang6f63ce02020-09-28 10:46:36 -070063 bpf::setrlimitForTest();
Yiwei Zhangf3337152020-06-12 15:30:54 -070064
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -070065 mGpuMem = std::make_unique<GpuMem>();
66 mTestableGpuMem = TestableGpuMem(mGpuMem.get());
Yiwei Zhanga3dae232020-06-25 22:02:29 -070067 mTestableGpuMem.setInitialized();
Yiwei Zhangf3337152020-06-12 15:30:54 -070068 errno = 0;
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -070069 mTestMap = bpf::BpfMap<uint64_t, uint64_t>(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE,
70 BPF_F_NO_PREALLOC);
71
Yiwei Zhangf3337152020-06-12 15:30:54 -070072 EXPECT_EQ(0, errno);
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -070073 EXPECT_LE(0, mTestMap.getMap().get());
74 EXPECT_TRUE(mTestMap.isValid());
75 }
76
77 std::string dumpsys() {
78 std::string result;
79 Vector<String16> args;
80 mGpuMem->dump(args, &result);
81 return result;
82 }
83
84 std::unique_ptr<GpuMem> mGpuMem;
85 TestableGpuMem mTestableGpuMem;
86 bpf::BpfMap<uint64_t, uint64_t> mTestMap;
87};
88
89TEST_F(GpuMemTest, validGpuMemTotalBpfPaths) {
90 EXPECT_EQ(mTestableGpuMem.getGpuMemTraceGroup(), "gpu_mem");
91 EXPECT_EQ(mTestableGpuMem.getGpuMemTotalTracepoint(), "gpu_mem_total");
92 EXPECT_EQ(mTestableGpuMem.getGpuMemTotalProgPath(),
93 "/sys/fs/bpf/prog_gpu_mem_tracepoint_gpu_mem_gpu_mem_total");
94 EXPECT_EQ(mTestableGpuMem.getGpuMemTotalMapPath(), "/sys/fs/bpf/map_gpu_mem_gpu_mem_total_map");
95}
96
97TEST_F(GpuMemTest, bpfInitializationFailed) {
98 EXPECT_EQ(dumpsys(), "Failed to initialize GPU memory eBPF\n");
99}
100
101TEST_F(GpuMemTest, gpuMemTotalMapEmpty) {
102 mTestableGpuMem.setGpuMemTotalMap(mTestMap);
103
104 EXPECT_EQ(dumpsys(), "GPU memory total usage map is empty\n");
105}
106
107TEST_F(GpuMemTest, globalMemTotal) {
108 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_GLOBAL_KEY, TEST_GLOBAL_VAL, BPF_ANY));
109 mTestableGpuMem.setGpuMemTotalMap(mTestMap);
110
111 EXPECT_THAT(dumpsys(), HasSubstr(StringPrintf("Global total: %" PRIu64 "\n", TEST_GLOBAL_VAL)));
112}
113
114TEST_F(GpuMemTest, missingGlobalMemTotal) {
115 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_1, TEST_PROC_VAL_1, BPF_ANY));
116 mTestableGpuMem.setGpuMemTotalMap(mTestMap);
117
118 EXPECT_THAT(dumpsys(), HasSubstr("Global total: N/A"));
119}
120
121TEST_F(GpuMemTest, procMemTotal) {
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -0700122 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_1, TEST_PROC_VAL_1, BPF_ANY));
123 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_2, TEST_PROC_VAL_2, BPF_ANY));
124 mTestableGpuMem.setGpuMemTotalMap(mTestMap);
125
126 EXPECT_THAT(dumpsys(),
127 HasSubstr(StringPrintf("Memory snapshot for GPU %u:\n",
128 (uint32_t)(TEST_PROC_KEY_1 >> 32))));
129 EXPECT_THAT(dumpsys(),
130 HasSubstr(StringPrintf("Proc %u total: %" PRIu64 "\n", (uint32_t)TEST_PROC_KEY_1,
131 TEST_PROC_VAL_1)));
132 EXPECT_THAT(dumpsys(),
133 HasSubstr(StringPrintf("Memory snapshot for GPU %u:\n",
134 (uint32_t)(TEST_PROC_KEY_2 >> 32))));
135 EXPECT_THAT(dumpsys(),
136 HasSubstr(StringPrintf("Proc %u total: %" PRIu64 "\n", (uint32_t)TEST_PROC_KEY_2,
137 TEST_PROC_VAL_2)));
138}
139
Yiwei Zhang643a4f62020-08-13 17:39:51 -0700140TEST_F(GpuMemTest, traverseGpuMemTotals) {
Yiwei Zhang643a4f62020-08-13 17:39:51 -0700141 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_GLOBAL_KEY, TEST_GLOBAL_VAL, BPF_ANY));
142 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_1, TEST_PROC_VAL_1, BPF_ANY));
143 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_2, TEST_PROC_VAL_2, BPF_ANY));
144 mTestableGpuMem.setGpuMemTotalMap(mTestMap);
145
146 static uint32_t sMask = 0;
147 static uint32_t sCount = 0;
148 mGpuMem->traverseGpuMemTotals([](int64_t, uint32_t gpuId, uint32_t pid, uint64_t size) {
149 const uint64_t key = ((uint64_t)gpuId << 32) | pid;
150 switch (key) {
151 case TEST_GLOBAL_KEY:
152 EXPECT_EQ(size, TEST_GLOBAL_VAL);
153 sMask |= 0x1;
154 break;
155 case TEST_PROC_KEY_1:
156 EXPECT_EQ(size, TEST_PROC_VAL_1);
157 sMask |= 0x2;
158 break;
159 case TEST_PROC_KEY_2:
160 EXPECT_EQ(size, TEST_PROC_VAL_2);
161 sMask |= 0x4;
162 break;
163 }
164 sCount++;
165 });
166
167 EXPECT_EQ(sMask, TEST_KEY_MASK);
168 EXPECT_EQ(sCount, TEST_KEY_COUNT);
169}
170
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -0700171} // namespace
172} // namespace android