blob: 6ba304c9a99e9bf737f7b3fd05f4f70df89a8a5d [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 Zhangf3337152020-06-12 15:30:54 -070065 errno = 0;
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -070066 mTestMap = bpf::BpfMap<uint64_t, uint64_t>(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE,
67 BPF_F_NO_PREALLOC);
68
Yiwei Zhangf3337152020-06-12 15:30:54 -070069 EXPECT_EQ(0, errno);
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -070070 EXPECT_LE(0, mTestMap.getMap().get());
71 EXPECT_TRUE(mTestMap.isValid());
72 }
73
74 std::string dumpsys() {
75 std::string result;
76 Vector<String16> args;
77 mGpuMem->dump(args, &result);
78 return result;
79 }
80
81 std::unique_ptr<GpuMem> mGpuMem;
82 TestableGpuMem mTestableGpuMem;
83 bpf::BpfMap<uint64_t, uint64_t> mTestMap;
84};
85
86TEST_F(GpuMemTest, validGpuMemTotalBpfPaths) {
Yiwei Zhangf3337152020-06-12 15:30:54 -070087 SKIP_IF_BPF_NOT_SUPPORTED;
88
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -070089 EXPECT_EQ(mTestableGpuMem.getGpuMemTraceGroup(), "gpu_mem");
90 EXPECT_EQ(mTestableGpuMem.getGpuMemTotalTracepoint(), "gpu_mem_total");
91 EXPECT_EQ(mTestableGpuMem.getGpuMemTotalProgPath(),
92 "/sys/fs/bpf/prog_gpu_mem_tracepoint_gpu_mem_gpu_mem_total");
93 EXPECT_EQ(mTestableGpuMem.getGpuMemTotalMapPath(), "/sys/fs/bpf/map_gpu_mem_gpu_mem_total_map");
94}
95
96TEST_F(GpuMemTest, bpfInitializationFailed) {
Yiwei Zhangf3337152020-06-12 15:30:54 -070097 SKIP_IF_BPF_NOT_SUPPORTED;
98
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -070099 EXPECT_EQ(dumpsys(), "Failed to initialize GPU memory eBPF\n");
100}
101
102TEST_F(GpuMemTest, gpuMemTotalMapEmpty) {
Yiwei Zhangf3337152020-06-12 15:30:54 -0700103 SKIP_IF_BPF_NOT_SUPPORTED;
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -0700104 mTestableGpuMem.setGpuMemTotalMap(mTestMap);
105
106 EXPECT_EQ(dumpsys(), "GPU memory total usage map is empty\n");
107}
108
109TEST_F(GpuMemTest, globalMemTotal) {
Yiwei Zhangf3337152020-06-12 15:30:54 -0700110 SKIP_IF_BPF_NOT_SUPPORTED;
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -0700111 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_GLOBAL_KEY, TEST_GLOBAL_VAL, BPF_ANY));
112 mTestableGpuMem.setGpuMemTotalMap(mTestMap);
113
114 EXPECT_THAT(dumpsys(), HasSubstr(StringPrintf("Global total: %" PRIu64 "\n", TEST_GLOBAL_VAL)));
115}
116
117TEST_F(GpuMemTest, missingGlobalMemTotal) {
Yiwei Zhangf3337152020-06-12 15:30:54 -0700118 SKIP_IF_BPF_NOT_SUPPORTED;
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -0700119 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_1, TEST_PROC_VAL_1, BPF_ANY));
120 mTestableGpuMem.setGpuMemTotalMap(mTestMap);
121
122 EXPECT_THAT(dumpsys(), HasSubstr("Global total: N/A"));
123}
124
125TEST_F(GpuMemTest, procMemTotal) {
Yiwei Zhangf3337152020-06-12 15:30:54 -0700126 SKIP_IF_BPF_NOT_SUPPORTED;
Yiwei Zhang9feb4ae2020-04-30 10:48:35 -0700127 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_1, TEST_PROC_VAL_1, BPF_ANY));
128 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_2, TEST_PROC_VAL_2, BPF_ANY));
129 mTestableGpuMem.setGpuMemTotalMap(mTestMap);
130
131 EXPECT_THAT(dumpsys(),
132 HasSubstr(StringPrintf("Memory snapshot for GPU %u:\n",
133 (uint32_t)(TEST_PROC_KEY_1 >> 32))));
134 EXPECT_THAT(dumpsys(),
135 HasSubstr(StringPrintf("Proc %u total: %" PRIu64 "\n", (uint32_t)TEST_PROC_KEY_1,
136 TEST_PROC_VAL_1)));
137 EXPECT_THAT(dumpsys(),
138 HasSubstr(StringPrintf("Memory snapshot for GPU %u:\n",
139 (uint32_t)(TEST_PROC_KEY_2 >> 32))));
140 EXPECT_THAT(dumpsys(),
141 HasSubstr(StringPrintf("Proc %u total: %" PRIu64 "\n", (uint32_t)TEST_PROC_KEY_2,
142 TEST_PROC_VAL_2)));
143}
144
145} // namespace
146} // namespace android