blob: 7fa75e13f8788fdde690f25aac7c079dbba88393 [file] [log] [blame]
Adithya Srinivasan134332b2020-12-12 03:31:50 +00001/*
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 <bpf/BpfMap.h>
21#include <gpumem/GpuMem.h>
22#include <gtest/gtest.h>
23#include <perfetto/trace/trace.pb.h>
24#include <tracing/GpuMemTracer.h>
25
26#include "TestableGpuMem.h"
27
28namespace android {
29
30constexpr uint32_t TEST_MAP_SIZE = 10;
31constexpr uint64_t TEST_GLOBAL_KEY = 0;
32constexpr uint32_t TEST_GLOBAL_PID = 0;
33constexpr uint64_t TEST_GLOBAL_VAL = 123;
34constexpr uint32_t TEST_GLOBAL_GPU_ID = 0;
35constexpr uint64_t TEST_PROC_KEY_1 = 1;
36constexpr uint32_t TEST_PROC_PID_1 = 1;
37constexpr uint64_t TEST_PROC_VAL_1 = 234;
38constexpr uint32_t TEST_PROC_1_GPU_ID = 0;
39constexpr uint64_t TEST_PROC_KEY_2 = 4294967298; // (1 << 32) + 2
40constexpr uint32_t TEST_PROC_PID_2 = 2;
41constexpr uint64_t TEST_PROC_VAL_2 = 345;
42constexpr uint32_t TEST_PROC_2_GPU_ID = 1;
43
44class GpuMemTracerTest : public testing::Test {
45public:
46 GpuMemTracerTest() {
47 const ::testing::TestInfo* const test_info =
48 ::testing::UnitTest::GetInstance()->current_test_info();
49 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
50 }
51
52 ~GpuMemTracerTest() {
53 const ::testing::TestInfo* const test_info =
54 ::testing::UnitTest::GetInstance()->current_test_info();
55 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
56 }
57
58 void SetUp() override {
59 SKIP_IF_BPF_NOT_SUPPORTED;
60 bpf::setrlimitForTest();
61
62 mGpuMem = std::make_shared<GpuMem>();
63 mGpuMemTracer = std::make_unique<GpuMemTracer>();
64 mGpuMemTracer->initializeForTest(mGpuMem);
65 mTestableGpuMem = TestableGpuMem(mGpuMem.get());
66
67 errno = 0;
68 mTestMap = bpf::BpfMap<uint64_t, uint64_t>(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE,
69 BPF_F_NO_PREALLOC);
70
71 EXPECT_EQ(0, errno);
72 EXPECT_LE(0, mTestMap.getMap().get());
73 EXPECT_TRUE(mTestMap.isValid());
74 }
75
76 int getTracerThreadCount() { return mGpuMemTracer->tracerThreadCount; }
77
78 std::shared_ptr<GpuMem> mGpuMem;
79 TestableGpuMem mTestableGpuMem;
80 std::unique_ptr<GpuMemTracer> mGpuMemTracer;
81 bpf::BpfMap<uint64_t, uint64_t> mTestMap;
82};
83
84static constexpr uint64_t getSizeForPid(uint32_t pid) {
85 switch (pid) {
86 case TEST_GLOBAL_PID:
87 return TEST_GLOBAL_VAL;
88 case TEST_PROC_PID_1:
89 return TEST_PROC_VAL_1;
90 case TEST_PROC_PID_2:
91 return TEST_PROC_VAL_2;
92 }
93 return 0;
94}
95
96static constexpr uint32_t getGpuIdForPid(uint32_t pid) {
97 switch (pid) {
98 case TEST_GLOBAL_PID:
99 return TEST_GLOBAL_GPU_ID;
100 case TEST_PROC_PID_1:
101 return TEST_PROC_1_GPU_ID;
102 case TEST_PROC_PID_2:
103 return TEST_PROC_2_GPU_ID;
104 }
105 return 0;
106}
107
108TEST_F(GpuMemTracerTest, traceInitialCountersAfterGpuMemInitialize) {
109 SKIP_IF_BPF_NOT_SUPPORTED;
110 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_GLOBAL_KEY, TEST_GLOBAL_VAL, BPF_ANY));
111 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_1, TEST_PROC_VAL_1, BPF_ANY));
112 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_2, TEST_PROC_VAL_2, BPF_ANY));
113 mTestableGpuMem.setGpuMemTotalMap(mTestMap);
114 mTestableGpuMem.setInitialized();
115
116 // Only 1 tracer thread should be existing for test.
117 EXPECT_EQ(getTracerThreadCount(), 1);
118 auto tracingSession = mGpuMemTracer->getTracingSessionForTest();
119
120 tracingSession->StartBlocking();
121 // Sleep for a short time to let the tracer thread finish its work
122 sleep(1);
123 tracingSession->StopBlocking();
124
125 // The test tracer thread should have finished its execution by now.
126 EXPECT_EQ(getTracerThreadCount(), 0);
127
128 auto packets = mGpuMemTracer->readGpuMemTotalPacketsForTestBlocking(tracingSession.get());
129 EXPECT_EQ(packets.size(), 3);
130
131 const auto& packet0 = packets[0];
132 ASSERT_TRUE(packet0.has_timestamp());
133 ASSERT_TRUE(packet0.has_gpu_mem_total_event());
134 const auto& gpuMemEvent0 = packet0.gpu_mem_total_event();
135 ASSERT_TRUE(gpuMemEvent0.has_pid());
136 const auto& pid0 = gpuMemEvent0.pid();
137 ASSERT_TRUE(gpuMemEvent0.has_size());
138 EXPECT_EQ(gpuMemEvent0.size(), getSizeForPid(pid0));
139 ASSERT_TRUE(gpuMemEvent0.has_gpu_id());
140 EXPECT_EQ(gpuMemEvent0.gpu_id(), getGpuIdForPid(pid0));
141
142 const auto& packet1 = packets[1];
143 ASSERT_TRUE(packet1.has_timestamp());
144 ASSERT_TRUE(packet1.has_gpu_mem_total_event());
145 const auto& gpuMemEvent1 = packet1.gpu_mem_total_event();
146 ASSERT_TRUE(gpuMemEvent1.has_pid());
147 const auto& pid1 = gpuMemEvent1.pid();
148 ASSERT_TRUE(gpuMemEvent1.has_size());
149 EXPECT_EQ(gpuMemEvent1.size(), getSizeForPid(pid1));
150 ASSERT_TRUE(gpuMemEvent1.has_gpu_id());
151 EXPECT_EQ(gpuMemEvent1.gpu_id(), getGpuIdForPid(pid1));
152
153 const auto& packet2 = packets[2];
154 ASSERT_TRUE(packet2.has_timestamp());
155 ASSERT_TRUE(packet2.has_gpu_mem_total_event());
156 const auto& gpuMemEvent2 = packet2.gpu_mem_total_event();
157 ASSERT_TRUE(gpuMemEvent2.has_pid());
158 const auto& pid2 = gpuMemEvent2.pid();
159 ASSERT_TRUE(gpuMemEvent2.has_size());
160 EXPECT_EQ(gpuMemEvent2.size(), getSizeForPid(pid2));
161 ASSERT_TRUE(gpuMemEvent2.has_gpu_id());
162 EXPECT_EQ(gpuMemEvent2.gpu_id(), getGpuIdForPid(pid2));
163}
164
165TEST_F(GpuMemTracerTest, noTracingWithoutGpuMemInitialize) {
166 // Only 1 tracer thread should be existing for test.
167 EXPECT_EQ(getTracerThreadCount(), 1);
168
169 auto tracingSession = mGpuMemTracer->getTracingSessionForTest();
170
171 tracingSession->StartBlocking();
172 // Sleep for a short time to let the tracer thread finish its work
173 sleep(1);
174 tracingSession->StopBlocking();
175
176 // The test tracer thread should have finished its execution by now.
177 EXPECT_EQ(getTracerThreadCount(), 0);
178
179 auto packets = mGpuMemTracer->readGpuMemTotalPacketsForTestBlocking(tracingSession.get());
180 EXPECT_EQ(packets.size(), 0);
181}
182} // namespace android