blob: abaf30ab8a891038b2493292f63dbd30f82f1dcb [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;
44
45class GpuMemTest : public testing::Test {
46public:
47 GpuMemTest() {
48 const ::testing::TestInfo* const test_info =
49 ::testing::UnitTest::GetInstance()->current_test_info();
50 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
51 }
52
53 ~GpuMemTest() {
54 const ::testing::TestInfo* const test_info =
55 ::testing::UnitTest::GetInstance()->current_test_info();
56 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
57 }
58
59 void SetUp() override {
Yiwei Zhangf3337152020-06-12 15:30:54 -070060 SKIP_IF_BPF_NOT_SUPPORTED;
61 ASSERT_EQ(0, bpf::setrlimitForTest());
62
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -070063 mGpuMem = std::make_unique<GpuMem>();
64 mTestableGpuMem = TestableGpuMem(mGpuMem.get());
Yiwei Zhanga3dae232020-06-25 22:02:29 -070065 mTestableGpuMem.setInitialized();
Yiwei Zhangf3337152020-06-12 15:30:54 -070066 errno = 0;
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -070067 mTestMap = bpf::BpfMap<uint64_t, uint64_t>(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE,
68 BPF_F_NO_PREALLOC);
69
Yiwei Zhangf3337152020-06-12 15:30:54 -070070 EXPECT_EQ(0, errno);
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -070071 EXPECT_LE(0, mTestMap.getMap().get());
72 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) {
Yiwei Zhangf3337152020-06-12 15:30:54 -070088 SKIP_IF_BPF_NOT_SUPPORTED;
89
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -070090 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) {
Yiwei Zhangf3337152020-06-12 15:30:54 -070098 SKIP_IF_BPF_NOT_SUPPORTED;
99
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -0700100 EXPECT_EQ(dumpsys(), "Failed to initialize GPU memory eBPF\n");
101}
102
103TEST_F(GpuMemTest, gpuMemTotalMapEmpty) {
Yiwei Zhangf3337152020-06-12 15:30:54 -0700104 SKIP_IF_BPF_NOT_SUPPORTED;
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -0700105 mTestableGpuMem.setGpuMemTotalMap(mTestMap);
106
107 EXPECT_EQ(dumpsys(), "GPU memory total usage map is empty\n");
108}
109
110TEST_F(GpuMemTest, globalMemTotal) {
Yiwei Zhangf3337152020-06-12 15:30:54 -0700111 SKIP_IF_BPF_NOT_SUPPORTED;
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -0700112 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_GLOBAL_KEY, TEST_GLOBAL_VAL, BPF_ANY));
113 mTestableGpuMem.setGpuMemTotalMap(mTestMap);
114
115 EXPECT_THAT(dumpsys(), HasSubstr(StringPrintf("Global total: %" PRIu64 "\n", TEST_GLOBAL_VAL)));
116}
117
118TEST_F(GpuMemTest, missingGlobalMemTotal) {
Yiwei Zhangf3337152020-06-12 15:30:54 -0700119 SKIP_IF_BPF_NOT_SUPPORTED;
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -0700120 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_1, TEST_PROC_VAL_1, BPF_ANY));
121 mTestableGpuMem.setGpuMemTotalMap(mTestMap);
122
123 EXPECT_THAT(dumpsys(), HasSubstr("Global total: N/A"));
124}
125
126TEST_F(GpuMemTest, procMemTotal) {
Yiwei Zhangf3337152020-06-12 15:30:54 -0700127 SKIP_IF_BPF_NOT_SUPPORTED;
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -0700128 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_1, TEST_PROC_VAL_1, BPF_ANY));
129 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_2, TEST_PROC_VAL_2, BPF_ANY));
130 mTestableGpuMem.setGpuMemTotalMap(mTestMap);
131
132 EXPECT_THAT(dumpsys(),
133 HasSubstr(StringPrintf("Memory snapshot for GPU %u:\n",
134 (uint32_t)(TEST_PROC_KEY_1 >> 32))));
135 EXPECT_THAT(dumpsys(),
136 HasSubstr(StringPrintf("Proc %u total: %" PRIu64 "\n", (uint32_t)TEST_PROC_KEY_1,
137 TEST_PROC_VAL_1)));
138 EXPECT_THAT(dumpsys(),
139 HasSubstr(StringPrintf("Memory snapshot for GPU %u:\n",
140 (uint32_t)(TEST_PROC_KEY_2 >> 32))));
141 EXPECT_THAT(dumpsys(),
142 HasSubstr(StringPrintf("Proc %u total: %" PRIu64 "\n", (uint32_t)TEST_PROC_KEY_2,
143 TEST_PROC_VAL_2)));
144}
145
146} // namespace
147} // namespace android