blob: cd8e19c01daef77d0ca64359b7705d67748d007b [file] [log] [blame]
Adithya Srinivasanf031e972020-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
Adithya Srinivasanaa516512020-12-21 23:36:42 +000078 std::vector<perfetto::protos::TracePacket> readGpuMemTotalPacketsBlocking(
79 perfetto::TracingSession* tracingSession) {
80 std::vector<char> raw_trace = tracingSession->ReadTraceBlocking();
81 perfetto::protos::Trace trace;
82 trace.ParseFromArray(raw_trace.data(), int(raw_trace.size()));
83
84 std::vector<perfetto::protos::TracePacket> packets;
85 for (const auto& packet : trace.packet()) {
86 if (!packet.has_gpu_mem_total_event()) {
87 continue;
88 }
89 packets.emplace_back(packet);
90 }
91 return packets;
92 }
93
Adithya Srinivasanf031e972020-12-12 03:31:50 +000094 std::shared_ptr<GpuMem> mGpuMem;
95 TestableGpuMem mTestableGpuMem;
96 std::unique_ptr<GpuMemTracer> mGpuMemTracer;
97 bpf::BpfMap<uint64_t, uint64_t> mTestMap;
98};
99
100static constexpr uint64_t getSizeForPid(uint32_t pid) {
101 switch (pid) {
102 case TEST_GLOBAL_PID:
103 return TEST_GLOBAL_VAL;
104 case TEST_PROC_PID_1:
105 return TEST_PROC_VAL_1;
106 case TEST_PROC_PID_2:
107 return TEST_PROC_VAL_2;
108 }
109 return 0;
110}
111
112static constexpr uint32_t getGpuIdForPid(uint32_t pid) {
113 switch (pid) {
114 case TEST_GLOBAL_PID:
115 return TEST_GLOBAL_GPU_ID;
116 case TEST_PROC_PID_1:
117 return TEST_PROC_1_GPU_ID;
118 case TEST_PROC_PID_2:
119 return TEST_PROC_2_GPU_ID;
120 }
121 return 0;
122}
123
124TEST_F(GpuMemTracerTest, traceInitialCountersAfterGpuMemInitialize) {
125 SKIP_IF_BPF_NOT_SUPPORTED;
126 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_GLOBAL_KEY, TEST_GLOBAL_VAL, BPF_ANY));
127 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 mTestableGpuMem.setInitialized();
131
132 // Only 1 tracer thread should be existing for test.
133 EXPECT_EQ(getTracerThreadCount(), 1);
134 auto tracingSession = mGpuMemTracer->getTracingSessionForTest();
135
136 tracingSession->StartBlocking();
137 // Sleep for a short time to let the tracer thread finish its work
138 sleep(1);
139 tracingSession->StopBlocking();
140
141 // The test tracer thread should have finished its execution by now.
142 EXPECT_EQ(getTracerThreadCount(), 0);
143
Adithya Srinivasanaa516512020-12-21 23:36:42 +0000144 auto packets = readGpuMemTotalPacketsBlocking(tracingSession.get());
Adithya Srinivasanf031e972020-12-12 03:31:50 +0000145 EXPECT_EQ(packets.size(), 3);
146
147 const auto& packet0 = packets[0];
148 ASSERT_TRUE(packet0.has_timestamp());
149 ASSERT_TRUE(packet0.has_gpu_mem_total_event());
150 const auto& gpuMemEvent0 = packet0.gpu_mem_total_event();
151 ASSERT_TRUE(gpuMemEvent0.has_pid());
152 const auto& pid0 = gpuMemEvent0.pid();
153 ASSERT_TRUE(gpuMemEvent0.has_size());
154 EXPECT_EQ(gpuMemEvent0.size(), getSizeForPid(pid0));
155 ASSERT_TRUE(gpuMemEvent0.has_gpu_id());
156 EXPECT_EQ(gpuMemEvent0.gpu_id(), getGpuIdForPid(pid0));
157
158 const auto& packet1 = packets[1];
159 ASSERT_TRUE(packet1.has_timestamp());
160 ASSERT_TRUE(packet1.has_gpu_mem_total_event());
161 const auto& gpuMemEvent1 = packet1.gpu_mem_total_event();
162 ASSERT_TRUE(gpuMemEvent1.has_pid());
163 const auto& pid1 = gpuMemEvent1.pid();
164 ASSERT_TRUE(gpuMemEvent1.has_size());
165 EXPECT_EQ(gpuMemEvent1.size(), getSizeForPid(pid1));
166 ASSERT_TRUE(gpuMemEvent1.has_gpu_id());
167 EXPECT_EQ(gpuMemEvent1.gpu_id(), getGpuIdForPid(pid1));
168
169 const auto& packet2 = packets[2];
170 ASSERT_TRUE(packet2.has_timestamp());
171 ASSERT_TRUE(packet2.has_gpu_mem_total_event());
172 const auto& gpuMemEvent2 = packet2.gpu_mem_total_event();
173 ASSERT_TRUE(gpuMemEvent2.has_pid());
174 const auto& pid2 = gpuMemEvent2.pid();
175 ASSERT_TRUE(gpuMemEvent2.has_size());
176 EXPECT_EQ(gpuMemEvent2.size(), getSizeForPid(pid2));
177 ASSERT_TRUE(gpuMemEvent2.has_gpu_id());
178 EXPECT_EQ(gpuMemEvent2.gpu_id(), getGpuIdForPid(pid2));
179}
180
181TEST_F(GpuMemTracerTest, noTracingWithoutGpuMemInitialize) {
182 // Only 1 tracer thread should be existing for test.
183 EXPECT_EQ(getTracerThreadCount(), 1);
184
185 auto tracingSession = mGpuMemTracer->getTracingSessionForTest();
186
187 tracingSession->StartBlocking();
188 // Sleep for a short time to let the tracer thread finish its work
189 sleep(1);
190 tracingSession->StopBlocking();
191
192 // The test tracer thread should have finished its execution by now.
193 EXPECT_EQ(getTracerThreadCount(), 0);
194
Adithya Srinivasanaa516512020-12-21 23:36:42 +0000195 auto packets = readGpuMemTotalPacketsBlocking(tracingSession.get());
Adithya Srinivasanf031e972020-12-12 03:31:50 +0000196 EXPECT_EQ(packets.size(), 0);
197}
198} // namespace android