blob: 1f5b2889d17a3805930de4f20f6b4a87214eb140 [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;
Maciej Żenczykowskic969fb82023-09-28 05:30:24 +000069 mTestMap.resetMap(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC);
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -070070
Yiwei Zhangf3337152020-06-12 15:30:54 -070071 EXPECT_EQ(0, errno);
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -070072 EXPECT_TRUE(mTestMap.isValid());
73 }
74
75 std::string dumpsys() {
76 std::string result;
77 Vector<String16> args;
78 mGpuMem->dump(args, &result);
79 return result;
80 }
81
82 std::unique_ptr<GpuMem> mGpuMem;
83 TestableGpuMem mTestableGpuMem;
84 bpf::BpfMap<uint64_t, uint64_t> mTestMap;
85};
86
87TEST_F(GpuMemTest, validGpuMemTotalBpfPaths) {
88 EXPECT_EQ(mTestableGpuMem.getGpuMemTraceGroup(), "gpu_mem");
89 EXPECT_EQ(mTestableGpuMem.getGpuMemTotalTracepoint(), "gpu_mem_total");
90 EXPECT_EQ(mTestableGpuMem.getGpuMemTotalProgPath(),
Ken Chen440cf722022-07-08 19:08:48 +080091 "/sys/fs/bpf/prog_gpuMem_tracepoint_gpu_mem_gpu_mem_total");
92 EXPECT_EQ(mTestableGpuMem.getGpuMemTotalMapPath(), "/sys/fs/bpf/map_gpuMem_gpu_mem_total_map");
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -070093}
94
95TEST_F(GpuMemTest, bpfInitializationFailed) {
96 EXPECT_EQ(dumpsys(), "Failed to initialize GPU memory eBPF\n");
97}
98
99TEST_F(GpuMemTest, gpuMemTotalMapEmpty) {
100 mTestableGpuMem.setGpuMemTotalMap(mTestMap);
101
102 EXPECT_EQ(dumpsys(), "GPU memory total usage map is empty\n");
103}
104
105TEST_F(GpuMemTest, globalMemTotal) {
106 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_GLOBAL_KEY, TEST_GLOBAL_VAL, BPF_ANY));
107 mTestableGpuMem.setGpuMemTotalMap(mTestMap);
108
109 EXPECT_THAT(dumpsys(), HasSubstr(StringPrintf("Global total: %" PRIu64 "\n", TEST_GLOBAL_VAL)));
110}
111
112TEST_F(GpuMemTest, missingGlobalMemTotal) {
113 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_1, TEST_PROC_VAL_1, BPF_ANY));
114 mTestableGpuMem.setGpuMemTotalMap(mTestMap);
115
116 EXPECT_THAT(dumpsys(), HasSubstr("Global total: N/A"));
117}
118
119TEST_F(GpuMemTest, procMemTotal) {
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -0700120 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_1, TEST_PROC_VAL_1, BPF_ANY));
121 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_2, TEST_PROC_VAL_2, BPF_ANY));
122 mTestableGpuMem.setGpuMemTotalMap(mTestMap);
123
124 EXPECT_THAT(dumpsys(),
125 HasSubstr(StringPrintf("Memory snapshot for GPU %u:\n",
126 (uint32_t)(TEST_PROC_KEY_1 >> 32))));
127 EXPECT_THAT(dumpsys(),
128 HasSubstr(StringPrintf("Proc %u total: %" PRIu64 "\n", (uint32_t)TEST_PROC_KEY_1,
129 TEST_PROC_VAL_1)));
130 EXPECT_THAT(dumpsys(),
131 HasSubstr(StringPrintf("Memory snapshot for GPU %u:\n",
132 (uint32_t)(TEST_PROC_KEY_2 >> 32))));
133 EXPECT_THAT(dumpsys(),
134 HasSubstr(StringPrintf("Proc %u total: %" PRIu64 "\n", (uint32_t)TEST_PROC_KEY_2,
135 TEST_PROC_VAL_2)));
136}
137
Yiwei Zhang643a4f62020-08-13 17:39:51 -0700138TEST_F(GpuMemTest, traverseGpuMemTotals) {
Yiwei Zhang643a4f62020-08-13 17:39:51 -0700139 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_GLOBAL_KEY, TEST_GLOBAL_VAL, BPF_ANY));
140 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_1, TEST_PROC_VAL_1, BPF_ANY));
141 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_2, TEST_PROC_VAL_2, BPF_ANY));
142 mTestableGpuMem.setGpuMemTotalMap(mTestMap);
143
144 static uint32_t sMask = 0;
145 static uint32_t sCount = 0;
146 mGpuMem->traverseGpuMemTotals([](int64_t, uint32_t gpuId, uint32_t pid, uint64_t size) {
147 const uint64_t key = ((uint64_t)gpuId << 32) | pid;
148 switch (key) {
149 case TEST_GLOBAL_KEY:
150 EXPECT_EQ(size, TEST_GLOBAL_VAL);
151 sMask |= 0x1;
152 break;
153 case TEST_PROC_KEY_1:
154 EXPECT_EQ(size, TEST_PROC_VAL_1);
155 sMask |= 0x2;
156 break;
157 case TEST_PROC_KEY_2:
158 EXPECT_EQ(size, TEST_PROC_VAL_2);
159 sMask |= 0x4;
160 break;
161 }
162 sCount++;
163 });
164
165 EXPECT_EQ(sMask, TEST_KEY_MASK);
166 EXPECT_EQ(sCount, TEST_KEY_COUNT);
167}
168
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -0700169} // namespace
170} // namespace android