blob: 065cf147ade2155e19e07f4718c6e819f9b2f696 [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
Maciej Żenczykowski842ec2d2022-06-16 18:52:17 +000020#define BPF_MAP_MAKE_VISIBLE_FOR_TESTING
Adithya Srinivasan134332b2020-12-12 03:31:50 +000021#include <bpf/BpfMap.h>
22#include <gpumem/GpuMem.h>
23#include <gtest/gtest.h>
24#include <perfetto/trace/trace.pb.h>
25#include <tracing/GpuMemTracer.h>
26
27#include "TestableGpuMem.h"
28
29namespace android {
30
31constexpr uint32_t TEST_MAP_SIZE = 10;
32constexpr uint64_t TEST_GLOBAL_KEY = 0;
33constexpr uint32_t TEST_GLOBAL_PID = 0;
34constexpr uint64_t TEST_GLOBAL_VAL = 123;
35constexpr uint32_t TEST_GLOBAL_GPU_ID = 0;
36constexpr uint64_t TEST_PROC_KEY_1 = 1;
37constexpr uint32_t TEST_PROC_PID_1 = 1;
38constexpr uint64_t TEST_PROC_VAL_1 = 234;
39constexpr uint32_t TEST_PROC_1_GPU_ID = 0;
40constexpr uint64_t TEST_PROC_KEY_2 = 4294967298; // (1 << 32) + 2
41constexpr uint32_t TEST_PROC_PID_2 = 2;
42constexpr uint64_t TEST_PROC_VAL_2 = 345;
43constexpr uint32_t TEST_PROC_2_GPU_ID = 1;
44
45class GpuMemTracerTest : public testing::Test {
46public:
47 GpuMemTracerTest() {
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 ~GpuMemTracerTest() {
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 {
Adithya Srinivasan134332b2020-12-12 03:31:50 +000060 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 Srinivasan45583462020-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 Srinivasan134332b2020-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) {
Adithya Srinivasan134332b2020-12-12 03:31:50 +0000125 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_GLOBAL_KEY, TEST_GLOBAL_VAL, BPF_ANY));
126 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_1, TEST_PROC_VAL_1, BPF_ANY));
127 ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_2, TEST_PROC_VAL_2, BPF_ANY));
128 mTestableGpuMem.setGpuMemTotalMap(mTestMap);
129 mTestableGpuMem.setInitialized();
130
131 // Only 1 tracer thread should be existing for test.
132 EXPECT_EQ(getTracerThreadCount(), 1);
133 auto tracingSession = mGpuMemTracer->getTracingSessionForTest();
134
135 tracingSession->StartBlocking();
136 // Sleep for a short time to let the tracer thread finish its work
137 sleep(1);
138 tracingSession->StopBlocking();
139
140 // The test tracer thread should have finished its execution by now.
141 EXPECT_EQ(getTracerThreadCount(), 0);
142
Adithya Srinivasan45583462020-12-21 23:36:42 +0000143 auto packets = readGpuMemTotalPacketsBlocking(tracingSession.get());
Adithya Srinivasan134332b2020-12-12 03:31:50 +0000144 EXPECT_EQ(packets.size(), 3);
145
146 const auto& packet0 = packets[0];
147 ASSERT_TRUE(packet0.has_timestamp());
148 ASSERT_TRUE(packet0.has_gpu_mem_total_event());
149 const auto& gpuMemEvent0 = packet0.gpu_mem_total_event();
150 ASSERT_TRUE(gpuMemEvent0.has_pid());
151 const auto& pid0 = gpuMemEvent0.pid();
152 ASSERT_TRUE(gpuMemEvent0.has_size());
153 EXPECT_EQ(gpuMemEvent0.size(), getSizeForPid(pid0));
154 ASSERT_TRUE(gpuMemEvent0.has_gpu_id());
155 EXPECT_EQ(gpuMemEvent0.gpu_id(), getGpuIdForPid(pid0));
156
157 const auto& packet1 = packets[1];
158 ASSERT_TRUE(packet1.has_timestamp());
159 ASSERT_TRUE(packet1.has_gpu_mem_total_event());
160 const auto& gpuMemEvent1 = packet1.gpu_mem_total_event();
161 ASSERT_TRUE(gpuMemEvent1.has_pid());
162 const auto& pid1 = gpuMemEvent1.pid();
163 ASSERT_TRUE(gpuMemEvent1.has_size());
164 EXPECT_EQ(gpuMemEvent1.size(), getSizeForPid(pid1));
165 ASSERT_TRUE(gpuMemEvent1.has_gpu_id());
166 EXPECT_EQ(gpuMemEvent1.gpu_id(), getGpuIdForPid(pid1));
167
168 const auto& packet2 = packets[2];
169 ASSERT_TRUE(packet2.has_timestamp());
170 ASSERT_TRUE(packet2.has_gpu_mem_total_event());
171 const auto& gpuMemEvent2 = packet2.gpu_mem_total_event();
172 ASSERT_TRUE(gpuMemEvent2.has_pid());
173 const auto& pid2 = gpuMemEvent2.pid();
174 ASSERT_TRUE(gpuMemEvent2.has_size());
175 EXPECT_EQ(gpuMemEvent2.size(), getSizeForPid(pid2));
176 ASSERT_TRUE(gpuMemEvent2.has_gpu_id());
177 EXPECT_EQ(gpuMemEvent2.gpu_id(), getGpuIdForPid(pid2));
178}
179
180TEST_F(GpuMemTracerTest, noTracingWithoutGpuMemInitialize) {
181 // Only 1 tracer thread should be existing for test.
182 EXPECT_EQ(getTracerThreadCount(), 1);
183
184 auto tracingSession = mGpuMemTracer->getTracingSessionForTest();
185
186 tracingSession->StartBlocking();
187 // Sleep for a short time to let the tracer thread finish its work
188 sleep(1);
189 tracingSession->StopBlocking();
190
191 // The test tracer thread should have finished its execution by now.
192 EXPECT_EQ(getTracerThreadCount(), 0);
193
Adithya Srinivasan45583462020-12-21 23:36:42 +0000194 auto packets = readGpuMemTotalPacketsBlocking(tracingSession.get());
Adithya Srinivasan134332b2020-12-12 03:31:50 +0000195 EXPECT_EQ(packets.size(), 0);
196}
197} // namespace android