blob: 45e836762eae9e1a8fb5547fb8a441fd4b94850e [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>
21#include <bpf/BpfMap.h>
22#include <gmock/gmock.h>
23#include <gpumem/GpuMem.h>
24#include <gtest/gtest.h>
25#include <inttypes.h>
26#include <utils/String16.h>
27#include <utils/Vector.h>
28
29#include "TestableGpuMem.h"
30
31namespace android {
32namespace {
33
34using base::StringPrintf;
35using testing::HasSubstr;
36
37constexpr uint32_t TEST_MAP_SIZE = 10;
38constexpr uint64_t TEST_GLOBAL_KEY = 0;
39constexpr uint64_t TEST_GLOBAL_VAL = 123;
40constexpr uint64_t TEST_PROC_KEY_1 = 1;
41constexpr uint64_t TEST_PROC_VAL_1 = 234;
42constexpr uint64_t TEST_PROC_KEY_2 = 4294967298; // (1 << 32) + 2
43constexpr uint64_t TEST_PROC_VAL_2 = 345;
Yiwei Zhang643a4f62020-08-13 17:39:51 -070044constexpr uint32_t TEST_KEY_MASK = 0x1 | 0x2 | 0x4;
45constexpr uint32_t TEST_KEY_COUNT = 3;
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -070046
47class GpuMemTest : public testing::Test {
48public:
49 GpuMemTest() {
50 const ::testing::TestInfo* const test_info =
51 ::testing::UnitTest::GetInstance()->current_test_info();
52 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
53 }
54
55 ~GpuMemTest() {
56 const ::testing::TestInfo* const test_info =
57 ::testing::UnitTest::GetInstance()->current_test_info();
58 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
59 }
60
61 void SetUp() override {
Yiwei Zhangf3337152020-06-12 15:30:54 -070062 SKIP_IF_BPF_NOT_SUPPORTED;
63 ASSERT_EQ(0, bpf::setrlimitForTest());
64
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) {
Yiwei Zhangf3337152020-06-12 15:30:54 -070090 SKIP_IF_BPF_NOT_SUPPORTED;
91
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -070092 EXPECT_EQ(mTestableGpuMem.getGpuMemTraceGroup(), "gpu_mem");
93 EXPECT_EQ(mTestableGpuMem.getGpuMemTotalTracepoint(), "gpu_mem_total");
94 EXPECT_EQ(mTestableGpuMem.getGpuMemTotalProgPath(),
95 "/sys/fs/bpf/prog_gpu_mem_tracepoint_gpu_mem_gpu_mem_total");
96 EXPECT_EQ(mTestableGpuMem.getGpuMemTotalMapPath(), "/sys/fs/bpf/map_gpu_mem_gpu_mem_total_map");
97}
98
99TEST_F(GpuMemTest, bpfInitializationFailed) {
Yiwei Zhangf3337152020-06-12 15:30:54 -0700100 SKIP_IF_BPF_NOT_SUPPORTED;
101
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -0700102 EXPECT_EQ(dumpsys(), "Failed to initialize GPU memory eBPF\n");
103}
104
105TEST_F(GpuMemTest, gpuMemTotalMapEmpty) {
Yiwei Zhangf3337152020-06-12 15:30:54 -0700106 SKIP_IF_BPF_NOT_SUPPORTED;
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -0700107 mTestableGpuMem.setGpuMemTotalMap(mTestMap);
108
109 EXPECT_EQ(dumpsys(), "GPU memory total usage map is empty\n");
110}
111
112TEST_F(GpuMemTest, globalMemTotal) {
Yiwei Zhangf3337152020-06-12 15:30:54 -0700113 SKIP_IF_BPF_NOT_SUPPORTED;
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -0700114 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_GLOBAL_KEY, TEST_GLOBAL_VAL, BPF_ANY));
115 mTestableGpuMem.setGpuMemTotalMap(mTestMap);
116
117 EXPECT_THAT(dumpsys(), HasSubstr(StringPrintf("Global total: %" PRIu64 "\n", TEST_GLOBAL_VAL)));
118}
119
120TEST_F(GpuMemTest, missingGlobalMemTotal) {
Yiwei Zhangf3337152020-06-12 15:30:54 -0700121 SKIP_IF_BPF_NOT_SUPPORTED;
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -0700122 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_1, TEST_PROC_VAL_1, BPF_ANY));
123 mTestableGpuMem.setGpuMemTotalMap(mTestMap);
124
125 EXPECT_THAT(dumpsys(), HasSubstr("Global total: N/A"));
126}
127
128TEST_F(GpuMemTest, procMemTotal) {
Yiwei Zhangf3337152020-06-12 15:30:54 -0700129 SKIP_IF_BPF_NOT_SUPPORTED;
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -0700130 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_1, TEST_PROC_VAL_1, BPF_ANY));
131 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_2, TEST_PROC_VAL_2, BPF_ANY));
132 mTestableGpuMem.setGpuMemTotalMap(mTestMap);
133
134 EXPECT_THAT(dumpsys(),
135 HasSubstr(StringPrintf("Memory snapshot for GPU %u:\n",
136 (uint32_t)(TEST_PROC_KEY_1 >> 32))));
137 EXPECT_THAT(dumpsys(),
138 HasSubstr(StringPrintf("Proc %u total: %" PRIu64 "\n", (uint32_t)TEST_PROC_KEY_1,
139 TEST_PROC_VAL_1)));
140 EXPECT_THAT(dumpsys(),
141 HasSubstr(StringPrintf("Memory snapshot for GPU %u:\n",
142 (uint32_t)(TEST_PROC_KEY_2 >> 32))));
143 EXPECT_THAT(dumpsys(),
144 HasSubstr(StringPrintf("Proc %u total: %" PRIu64 "\n", (uint32_t)TEST_PROC_KEY_2,
145 TEST_PROC_VAL_2)));
146}
147
Yiwei Zhang643a4f62020-08-13 17:39:51 -0700148TEST_F(GpuMemTest, traverseGpuMemTotals) {
149 SKIP_IF_BPF_NOT_SUPPORTED;
150 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_GLOBAL_KEY, TEST_GLOBAL_VAL, BPF_ANY));
151 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_1, TEST_PROC_VAL_1, BPF_ANY));
152 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_2, TEST_PROC_VAL_2, BPF_ANY));
153 mTestableGpuMem.setGpuMemTotalMap(mTestMap);
154
155 static uint32_t sMask = 0;
156 static uint32_t sCount = 0;
157 mGpuMem->traverseGpuMemTotals([](int64_t, uint32_t gpuId, uint32_t pid, uint64_t size) {
158 const uint64_t key = ((uint64_t)gpuId << 32) | pid;
159 switch (key) {
160 case TEST_GLOBAL_KEY:
161 EXPECT_EQ(size, TEST_GLOBAL_VAL);
162 sMask |= 0x1;
163 break;
164 case TEST_PROC_KEY_1:
165 EXPECT_EQ(size, TEST_PROC_VAL_1);
166 sMask |= 0x2;
167 break;
168 case TEST_PROC_KEY_2:
169 EXPECT_EQ(size, TEST_PROC_VAL_2);
170 sMask |= 0x4;
171 break;
172 }
173 sCount++;
174 });
175
176 EXPECT_EQ(sMask, TEST_KEY_MASK);
177 EXPECT_EQ(sCount, TEST_KEY_COUNT);
178}
179
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -0700180} // namespace
181} // namespace android