1 /*
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 <unistd.h>
21 #include <binder/ProcessState.h>
22 #include <cutils/properties.h>
23 #include <gmock/gmock.h>
24 #include <gpustats/GpuStats.h>
25 #include <gtest/gtest.h>
26 #include <stats_pull_atom_callback.h>
27 #include <statslog.h>
28 #include <utils/Looper.h>
29 #include <utils/String16.h>
30 #include <utils/Vector.h>
31 
32 #include "TestableGpuStats.h"
33 
34 namespace android {
35 namespace {
36 
37 using testing::HasSubstr;
38 
39 // clang-format off
40 #define BUILTIN_DRIVER_PKG_NAME   "system"
41 #define BUILTIN_DRIVER_VER_NAME   "0"
42 #define BUILTIN_DRIVER_VER_CODE   0
43 #define BUILTIN_DRIVER_BUILD_TIME 123
44 #define UPDATED_DRIVER_PKG_NAME   "updated"
45 #define UPDATED_DRIVER_VER_NAME   "1"
46 #define UPDATED_DRIVER_VER_CODE   1
47 #define UPDATED_DRIVER_BUILD_TIME 234
48 #define VULKAN_VERSION            345
49 #define VULKAN_ENGINE_NAME_1      "testVulkanEngine1"
50 #define VULKAN_ENGINE_NAME_2      "testVulkanEngine2"
51 #define APP_PKG_NAME_1            "testapp1"
52 #define APP_PKG_NAME_2            "testapp2"
53 #define DRIVER_LOADING_TIME_1     678
54 #define DRIVER_LOADING_TIME_2     789
55 #define DRIVER_LOADING_TIME_3     891
56 
57 constexpr uint64_t VULKAN_FEATURES_MASK = 0x600D;
58 constexpr uint32_t VULKAN_API_VERSION = 0x400000;
59 constexpr int32_t VULKAN_INSTANCE_EXTENSION_1 = 0x1234;
60 constexpr int32_t VULKAN_INSTANCE_EXTENSION_2 = 0x8765;
61 constexpr int32_t VULKAN_DEVICE_EXTENSION_1 = 0x9012;
62 constexpr int32_t VULKAN_DEVICE_EXTENSION_2 = 0x3456;
63 
64 enum InputCommand : int32_t {
65     DUMP_ALL               = 0,
66     DUMP_GLOBAL            = 1,
67     DUMP_APP               = 2,
68     DUMP_ALL_THEN_CLEAR    = 3,
69     DUMP_GLOBAL_THEN_CLEAR = 4,
70     DUMP_APP_THEN_CLEAR    = 5,
71 };
72 // clang-format on
73 
74 class GpuStatsTest : public testing::Test {
75     sp<android::Looper> looper;
76 public:
GpuStatsTest()77     GpuStatsTest() : looper(Looper::prepare(0 /* opts */)) {
78         const ::testing::TestInfo* const test_info =
79                 ::testing::UnitTest::GetInstance()->current_test_info();
80         ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
81     }
82 
~GpuStatsTest()83     ~GpuStatsTest() {
84         const ::testing::TestInfo* const test_info =
85                 ::testing::UnitTest::GetInstance()->current_test_info();
86         ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
87 
88         // This is required for test due to GpuStats instance spawns binder transactions
89         // in its destructor. After the gtest destructor test exits immidiatelly.
90         // It results in binder thread not able to process above binder transactions and memory leak
91         // occures. Binder thread needs time to process callbacks transactions.
92         // It leads to GpuStats instance destructor needs to be called in advance.
93         mGpuStats.reset(nullptr);
94         // performs all pending callbacks until all data has been consumed
95         // gives time to process binder transactions by thread pool
96         looper->pollAll(1000);
97     }
98 
99     std::string inputCommand(InputCommand cmd);
100 
SetUp()101     void SetUp() override {
102         mCpuVulkanVersion = property_get_int32("ro.cpuvulkan.version", 0);
103         mGlesVersion = property_get_int32("ro.opengles.version", 0);
104 
105         // start the thread pool
106         sp<ProcessState> ps(ProcessState::self());
107         ps->startThreadPool();
108     }
109 
110     std::unique_ptr<GpuStats> mGpuStats = std::make_unique<GpuStats>();
111     int32_t mCpuVulkanVersion = 0;
112     int32_t mGlesVersion = 0;
113 };
114 
inputCommand(InputCommand cmd)115 std::string GpuStatsTest::inputCommand(InputCommand cmd) {
116     std::string result;
117     Vector<String16> args;
118 
119     switch (cmd) {
120         case InputCommand::DUMP_ALL:
121             break;
122         case InputCommand::DUMP_GLOBAL:
123             args.push_back(String16("--global"));
124             break;
125         case InputCommand::DUMP_APP:
126             args.push_back(String16("--app"));
127             break;
128         case InputCommand::DUMP_ALL_THEN_CLEAR:
129             args.push_back(String16("--clear"));
130             break;
131         case InputCommand::DUMP_GLOBAL_THEN_CLEAR:
132             args.push_back(String16("--global"));
133             args.push_back(String16("--clear"));
134             break;
135         case InputCommand::DUMP_APP_THEN_CLEAR:
136             args.push_back(String16("--app"));
137             args.push_back(String16("--clear"));
138             break;
139     }
140 
141     mGpuStats->dump(args, &result);
142     return result;
143 }
144 
TEST_F(GpuStatsTest,statsEmptyByDefault)145 TEST_F(GpuStatsTest, statsEmptyByDefault) {
146     ASSERT_TRUE(inputCommand(InputCommand::DUMP_ALL).empty());
147 }
148 
TEST_F(GpuStatsTest,canInsertBuiltinDriverStats)149 TEST_F(GpuStatsTest, canInsertBuiltinDriverStats) {
150     mGpuStats->insertDriverStats(BUILTIN_DRIVER_PKG_NAME, BUILTIN_DRIVER_VER_NAME,
151                                  BUILTIN_DRIVER_VER_CODE, BUILTIN_DRIVER_BUILD_TIME, APP_PKG_NAME_1,
152                                  VULKAN_VERSION, GpuStatsInfo::Driver::GL, true,
153                                  DRIVER_LOADING_TIME_1);
154 
155     std::string expectedResult = "driverPackageName = " + std::string(BUILTIN_DRIVER_PKG_NAME);
156     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr(expectedResult));
157     expectedResult = "driverVersionName = " + std::string(BUILTIN_DRIVER_VER_NAME);
158     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr(expectedResult));
159     expectedResult = "driverVersionCode = " + std::to_string(BUILTIN_DRIVER_VER_CODE);
160     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr(expectedResult));
161     expectedResult = "driverBuildTime = " + std::to_string(BUILTIN_DRIVER_BUILD_TIME);
162     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr(expectedResult));
163     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr("glLoadingCount = 1"));
164     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr("glLoadingFailureCount = 0"));
165     expectedResult = "appPackageName = " + std::string(APP_PKG_NAME_1);
166     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr(expectedResult));
167     expectedResult = "driverVersionCode = " + std::to_string(BUILTIN_DRIVER_VER_CODE);
168     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr(expectedResult));
169     expectedResult = "glDriverLoadingTime: " + std::to_string(DRIVER_LOADING_TIME_1);
170     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr(expectedResult));
171 }
172 
TEST_F(GpuStatsTest,canInsertUpdatedDriverStats)173 TEST_F(GpuStatsTest, canInsertUpdatedDriverStats) {
174     mGpuStats->insertDriverStats(UPDATED_DRIVER_PKG_NAME, UPDATED_DRIVER_VER_NAME,
175                                  UPDATED_DRIVER_VER_CODE, UPDATED_DRIVER_BUILD_TIME, APP_PKG_NAME_2,
176                                  VULKAN_VERSION, GpuStatsInfo::Driver::VULKAN_UPDATED, false,
177                                  DRIVER_LOADING_TIME_2);
178 
179     std::string expectedResult = "driverPackageName = " + std::string(UPDATED_DRIVER_PKG_NAME);
180     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr(expectedResult));
181     expectedResult = "driverVersionName = " + std::string(UPDATED_DRIVER_VER_NAME);
182     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr(expectedResult));
183     expectedResult = "driverVersionCode = " + std::to_string(UPDATED_DRIVER_VER_CODE);
184     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr(expectedResult));
185     expectedResult = "driverBuildTime = " + std::to_string(UPDATED_DRIVER_BUILD_TIME);
186     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr(expectedResult));
187     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr("vkLoadingCount = 1"));
188     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr("vkLoadingFailureCount = 1"));
189     expectedResult = "appPackageName = " + std::string(APP_PKG_NAME_2);
190     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr(expectedResult));
191     expectedResult = "driverVersionCode = " + std::to_string(UPDATED_DRIVER_VER_CODE);
192     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr(expectedResult));
193     expectedResult = "vkDriverLoadingTime: " + std::to_string(DRIVER_LOADING_TIME_2);
194     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr(expectedResult));
195 }
196 
TEST_F(GpuStatsTest,canInsertAngleDriverStats)197 TEST_F(GpuStatsTest, canInsertAngleDriverStats) {
198     mGpuStats->insertDriverStats(UPDATED_DRIVER_PKG_NAME, UPDATED_DRIVER_VER_NAME,
199                                  UPDATED_DRIVER_VER_CODE, UPDATED_DRIVER_BUILD_TIME, APP_PKG_NAME_2,
200                                  VULKAN_VERSION, GpuStatsInfo::Driver::ANGLE, true,
201                                  DRIVER_LOADING_TIME_3);
202 
203     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr("angleLoadingCount = 1"));
204     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr("angleLoadingFailureCount = 0"));
205     std::string expectedResult = "angleDriverLoadingTime: " + std::to_string(DRIVER_LOADING_TIME_3);
206     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr(expectedResult));
207 }
208 
TEST_F(GpuStatsTest,canDump3dApiVersion)209 TEST_F(GpuStatsTest, canDump3dApiVersion) {
210     mGpuStats->insertDriverStats(BUILTIN_DRIVER_PKG_NAME, BUILTIN_DRIVER_VER_NAME,
211                                  BUILTIN_DRIVER_VER_CODE, BUILTIN_DRIVER_BUILD_TIME, APP_PKG_NAME_1,
212                                  VULKAN_VERSION, GpuStatsInfo::Driver::GL, true,
213                                  DRIVER_LOADING_TIME_1);
214 
215     std::string expectedResult = "vulkanVersion = " + std::to_string(VULKAN_VERSION);
216     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr(expectedResult));
217     expectedResult = "cpuVulkanVersion = " + std::to_string(mCpuVulkanVersion);
218     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr(expectedResult));
219     expectedResult = "glesVersion = " + std::to_string(mGlesVersion);
220     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr(expectedResult));
221 }
222 
TEST_F(GpuStatsTest,canNotInsertTargetStatsBeforeProperSetup)223 TEST_F(GpuStatsTest, canNotInsertTargetStatsBeforeProperSetup) {
224     mGpuStats->insertTargetStats(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
225                                  GpuStatsInfo::Stats::CPU_VULKAN_IN_USE, 0);
226     mGpuStats->insertTargetStats(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
227                                  GpuStatsInfo::Stats::FALSE_PREROTATION, 0);
228     mGpuStats->insertTargetStats(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
229                                  GpuStatsInfo::Stats::GLES_1_IN_USE, 0);
230     mGpuStats->insertTargetStats(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
231                                  GpuStatsInfo::Stats::CREATED_GLES_CONTEXT, 0);
232     mGpuStats->insertTargetStats(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
233                                  GpuStatsInfo::Stats::CREATED_VULKAN_API_VERSION,
234                                  VULKAN_API_VERSION);
235     mGpuStats->insertTargetStats(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
236                                  GpuStatsInfo::Stats::CREATED_VULKAN_DEVICE, 0);
237     mGpuStats->insertTargetStats(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
238                                  GpuStatsInfo::Stats::CREATED_VULKAN_SWAPCHAIN, 0);
239     mGpuStats->insertTargetStats(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
240                                  GpuStatsInfo::Stats::VULKAN_DEVICE_FEATURES_ENABLED,
241                                  VULKAN_FEATURES_MASK);
242     mGpuStats->insertTargetStats(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
243                                  GpuStatsInfo::Stats::VULKAN_INSTANCE_EXTENSION,
244                                  VULKAN_INSTANCE_EXTENSION_1);
245     mGpuStats->insertTargetStats(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
246                                  GpuStatsInfo::Stats::VULKAN_DEVICE_EXTENSION,
247                                  VULKAN_DEVICE_EXTENSION_1);
248     mGpuStats->addVulkanEngineName(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
249                              VULKAN_ENGINE_NAME_1);
250 
251     EXPECT_TRUE(inputCommand(InputCommand::DUMP_APP).empty());
252 }
253 
TEST_F(GpuStatsTest,canInsertTargetStatsAfterProperSetup)254 TEST_F(GpuStatsTest, canInsertTargetStatsAfterProperSetup) {
255     mGpuStats->insertDriverStats(BUILTIN_DRIVER_PKG_NAME, BUILTIN_DRIVER_VER_NAME,
256                                  BUILTIN_DRIVER_VER_CODE, BUILTIN_DRIVER_BUILD_TIME, APP_PKG_NAME_1,
257                                  VULKAN_VERSION, GpuStatsInfo::Driver::GL, true,
258                                  DRIVER_LOADING_TIME_1);
259     mGpuStats->insertTargetStats(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
260                                  GpuStatsInfo::Stats::CPU_VULKAN_IN_USE, 0);
261     mGpuStats->insertTargetStats(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
262                                  GpuStatsInfo::Stats::FALSE_PREROTATION, 0);
263     mGpuStats->insertTargetStats(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
264                                  GpuStatsInfo::Stats::GLES_1_IN_USE, 0);
265     mGpuStats->insertTargetStats(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
266                                  GpuStatsInfo::Stats::CREATED_GLES_CONTEXT, 0);
267     mGpuStats->insertTargetStats(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
268                                  GpuStatsInfo::Stats::CREATED_VULKAN_API_VERSION,
269                                  VULKAN_API_VERSION);
270     mGpuStats->insertTargetStats(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
271                                  GpuStatsInfo::Stats::CREATED_VULKAN_DEVICE, 0);
272     mGpuStats->insertTargetStats(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
273                                  GpuStatsInfo::Stats::CREATED_VULKAN_SWAPCHAIN, 0);
274     mGpuStats->insertTargetStats(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
275                                  GpuStatsInfo::Stats::VULKAN_DEVICE_FEATURES_ENABLED,
276                                  VULKAN_FEATURES_MASK);
277     mGpuStats->insertTargetStats(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
278                                  GpuStatsInfo::Stats::VULKAN_INSTANCE_EXTENSION,
279                                  VULKAN_INSTANCE_EXTENSION_1);
280     mGpuStats->insertTargetStats(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
281                                  GpuStatsInfo::Stats::VULKAN_INSTANCE_EXTENSION,
282                                  VULKAN_INSTANCE_EXTENSION_2);
283     mGpuStats->insertTargetStats(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
284                                  GpuStatsInfo::Stats::VULKAN_DEVICE_EXTENSION,
285                                  VULKAN_DEVICE_EXTENSION_1);
286     mGpuStats->insertTargetStats(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
287                                  GpuStatsInfo::Stats::VULKAN_DEVICE_EXTENSION,
288                                  VULKAN_DEVICE_EXTENSION_2);
289     mGpuStats->addVulkanEngineName(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
290                              VULKAN_ENGINE_NAME_1);
291 
292     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr("cpuVulkanInUse = 1"));
293     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr("falsePrerotation = 1"));
294     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr("gles1InUse = 1"));
295     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr("createdGlesContext = 1"));
296     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr("createdVulkanDevice = 1"));
297     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr("createdVulkanSwapchain = 1"));
298     std::stringstream expectedResult;
299     expectedResult << "vulkanApiVersion = 0x" << std::hex << VULKAN_API_VERSION;
300     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr(expectedResult.str()));
301     expectedResult.str("");
302     expectedResult << "vulkanDeviceFeaturesEnabled = 0x" << std::hex << VULKAN_FEATURES_MASK;
303     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr(expectedResult.str()));
304     expectedResult.str("");
305     expectedResult << "vulkanInstanceExtensions: 0x" << std::hex << VULKAN_INSTANCE_EXTENSION_1
306                     << " 0x" << std::hex << VULKAN_INSTANCE_EXTENSION_2;
307     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr(expectedResult.str()));
308     expectedResult.str("");
309     expectedResult << "vulkanDeviceExtensions: 0x" << std::hex << VULKAN_DEVICE_EXTENSION_1
310                     << " 0x" << std::hex << VULKAN_DEVICE_EXTENSION_2;
311     expectedResult.str("");
312     expectedResult << "vulkanEngineNames: " << VULKAN_ENGINE_NAME_1 << ",";
313 
314     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr(expectedResult.str()));
315 }
316 
317 // Verify the vulkanEngineNames list behaves like a set and dedupes additions
TEST_F(GpuStatsTest,vulkanEngineNamesBehavesLikeSet)318 TEST_F(GpuStatsTest, vulkanEngineNamesBehavesLikeSet) {
319     mGpuStats->insertDriverStats(BUILTIN_DRIVER_PKG_NAME, BUILTIN_DRIVER_VER_NAME,
320                                  BUILTIN_DRIVER_VER_CODE, BUILTIN_DRIVER_BUILD_TIME, APP_PKG_NAME_1,
321                                  VULKAN_VERSION, GpuStatsInfo::Driver::GL, true,
322                                  DRIVER_LOADING_TIME_1);
323     for (int i = 0; i < 4; i++) {
324         mGpuStats->addVulkanEngineName(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
325                                        VULKAN_ENGINE_NAME_1);
326     }
327 
328     std::stringstream wrongResult, expectedResult;
329     wrongResult << "vulkanEngineNames: " << VULKAN_ENGINE_NAME_1 << ", " <<
330                    VULKAN_ENGINE_NAME_1;
331     expectedResult << "vulkanEngineNames: " << VULKAN_ENGINE_NAME_1;
332 
333     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), Not(HasSubstr(wrongResult.str())));
334     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr(expectedResult.str()));
335 }
336 
TEST_F(GpuStatsTest,vulkanEngineNamesCheckEmptyEngineNameAlone)337 TEST_F(GpuStatsTest, vulkanEngineNamesCheckEmptyEngineNameAlone) {
338     mGpuStats->insertDriverStats(BUILTIN_DRIVER_PKG_NAME, BUILTIN_DRIVER_VER_NAME,
339                                  BUILTIN_DRIVER_VER_CODE, BUILTIN_DRIVER_BUILD_TIME, APP_PKG_NAME_1,
340                                  VULKAN_VERSION, GpuStatsInfo::Driver::GL, true,
341                                  DRIVER_LOADING_TIME_1);
342 
343     mGpuStats->addVulkanEngineName(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
344                                    "");
345 
346     std::stringstream expectedResult;
347     expectedResult << "vulkanEngineNames: ,";
348 
349     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr(expectedResult.str()));
350 }
351 
TEST_F(GpuStatsTest,vulkanEngineNamesCheckEmptyEngineNameWithOthers)352 TEST_F(GpuStatsTest, vulkanEngineNamesCheckEmptyEngineNameWithOthers) {
353     mGpuStats->insertDriverStats(BUILTIN_DRIVER_PKG_NAME, BUILTIN_DRIVER_VER_NAME,
354                                  BUILTIN_DRIVER_VER_CODE, BUILTIN_DRIVER_BUILD_TIME, APP_PKG_NAME_1,
355                                  VULKAN_VERSION, GpuStatsInfo::Driver::GL, true,
356                                  DRIVER_LOADING_TIME_1);
357 
358     mGpuStats->addVulkanEngineName(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
359                                    VULKAN_ENGINE_NAME_1);
360     mGpuStats->addVulkanEngineName(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
361                                    "");
362     mGpuStats->addVulkanEngineName(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
363                                    VULKAN_ENGINE_NAME_2);
364 
365     std::stringstream expectedResult;
366     expectedResult << "vulkanEngineNames: " << VULKAN_ENGINE_NAME_1 << ", "
367                    << ", " <<  VULKAN_ENGINE_NAME_2;
368 
369     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr(expectedResult.str()));
370 }
371 
372 // Verify we always have the most recently used apps in mAppStats, even when we fill it.
TEST_F(GpuStatsTest,canInsertMoreThanMaxNumAppRecords)373 TEST_F(GpuStatsTest, canInsertMoreThanMaxNumAppRecords) {
374     constexpr int kNumExtraApps = 15;
375     static_assert(kNumExtraApps > GpuStats::APP_RECORD_HEADROOM);
376 
377     // Insert stats for GpuStats::MAX_NUM_APP_RECORDS so we fill it up.
378     for (int i = 0; i < GpuStats::MAX_NUM_APP_RECORDS + kNumExtraApps; ++i) {
379         std::stringstream nameStream;
380         nameStream << "testapp" << "_" << i;
381         std::string fullPkgName = nameStream.str();
382 
383         mGpuStats->insertDriverStats(BUILTIN_DRIVER_PKG_NAME, BUILTIN_DRIVER_VER_NAME,
384                                      BUILTIN_DRIVER_VER_CODE, BUILTIN_DRIVER_BUILD_TIME,
385                                      fullPkgName, VULKAN_VERSION, GpuStatsInfo::Driver::GL, true,
386                                      DRIVER_LOADING_TIME_1);
387         mGpuStats->insertTargetStats(fullPkgName, BUILTIN_DRIVER_VER_CODE,
388                                      GpuStatsInfo::Stats::CPU_VULKAN_IN_USE, 0);
389         mGpuStats->insertTargetStats(fullPkgName, BUILTIN_DRIVER_VER_CODE,
390                                      GpuStatsInfo::Stats::FALSE_PREROTATION, 0);
391         mGpuStats->insertTargetStats(fullPkgName, BUILTIN_DRIVER_VER_CODE,
392                                      GpuStatsInfo::Stats::GLES_1_IN_USE, 0);
393         mGpuStats->insertTargetStats(fullPkgName, BUILTIN_DRIVER_VER_CODE,
394                                      GpuStatsInfo::Stats::CREATED_GLES_CONTEXT, 0);
395         mGpuStats->insertTargetStats(fullPkgName, BUILTIN_DRIVER_VER_CODE,
396                                     GpuStatsInfo::Stats::CREATED_VULKAN_API_VERSION,
397                                     VULKAN_API_VERSION);
398         mGpuStats->insertTargetStats(fullPkgName, BUILTIN_DRIVER_VER_CODE,
399                                     GpuStatsInfo::Stats::CREATED_VULKAN_DEVICE, 0);
400         mGpuStats->insertTargetStats(fullPkgName, BUILTIN_DRIVER_VER_CODE,
401                                     GpuStatsInfo::Stats::CREATED_VULKAN_SWAPCHAIN, 0);
402         mGpuStats->insertTargetStats(fullPkgName, BUILTIN_DRIVER_VER_CODE,
403                                     GpuStatsInfo::Stats::VULKAN_DEVICE_FEATURES_ENABLED,
404                                     VULKAN_FEATURES_MASK);
405         mGpuStats->insertTargetStats(fullPkgName, BUILTIN_DRIVER_VER_CODE,
406                                     GpuStatsInfo::Stats::VULKAN_INSTANCE_EXTENSION,
407                                     VULKAN_INSTANCE_EXTENSION_1);
408         mGpuStats->insertTargetStats(fullPkgName, BUILTIN_DRIVER_VER_CODE,
409                                     GpuStatsInfo::Stats::VULKAN_INSTANCE_EXTENSION,
410                                     VULKAN_INSTANCE_EXTENSION_2);
411         mGpuStats->insertTargetStats(fullPkgName, BUILTIN_DRIVER_VER_CODE,
412                                     GpuStatsInfo::Stats::VULKAN_DEVICE_EXTENSION,
413                                     VULKAN_DEVICE_EXTENSION_1);
414         mGpuStats->insertTargetStats(fullPkgName, BUILTIN_DRIVER_VER_CODE,
415                                     GpuStatsInfo::Stats::VULKAN_DEVICE_EXTENSION,
416                                     VULKAN_DEVICE_EXTENSION_2);
417         mGpuStats->addVulkanEngineName(fullPkgName, BUILTIN_DRIVER_VER_CODE,
418                                  VULKAN_ENGINE_NAME_1);
419         mGpuStats->addVulkanEngineName(fullPkgName, BUILTIN_DRIVER_VER_CODE,
420                                  VULKAN_ENGINE_NAME_2);
421 
422         EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr(fullPkgName.c_str()));
423         EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr("cpuVulkanInUse = 1"));
424         EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr("falsePrerotation = 1"));
425         EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr("gles1InUse = 1"));
426         EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr("createdGlesContext = 1"));
427         EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr("createdVulkanDevice = 1"));
428         EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr("createdVulkanSwapchain = 1"));
429         std::stringstream expectedResult;
430         expectedResult << "vulkanApiVersion = 0x" << std::hex << VULKAN_API_VERSION;
431         EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr(expectedResult.str()));
432         expectedResult.str("");
433         expectedResult << "vulkanDeviceFeaturesEnabled = 0x" << std::hex << VULKAN_FEATURES_MASK;
434         EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr(expectedResult.str()));
435         expectedResult.str("");
436         expectedResult << "vulkanInstanceExtensions: 0x" << std::hex << VULKAN_INSTANCE_EXTENSION_1
437                         << " 0x" << std::hex << VULKAN_INSTANCE_EXTENSION_2;
438         EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr(expectedResult.str()));
439         expectedResult.str("");
440         expectedResult << "vulkanDeviceExtensions: 0x" << std::hex << VULKAN_DEVICE_EXTENSION_1
441                         << " 0x" << std::hex << VULKAN_DEVICE_EXTENSION_2;
442         expectedResult.str("");
443         expectedResult << "vulkanEngineNames: " << VULKAN_ENGINE_NAME_1 << ", "
444                        << VULKAN_ENGINE_NAME_2 << ",";
445         EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr(expectedResult.str()));
446     }
447 
448     // mAppStats purges GpuStats::APP_RECORD_HEADROOM apps removed everytime it's filled up.
449     int numPurges = kNumExtraApps / GpuStats::APP_RECORD_HEADROOM;
450     numPurges += (kNumExtraApps % GpuStats::APP_RECORD_HEADROOM) == 0 ? 0 : 1;
451 
452     // Verify the remaining apps are present.
453     for (int i = numPurges * GpuStats::APP_RECORD_HEADROOM;
454          i < GpuStats::MAX_NUM_APP_RECORDS + kNumExtraApps;
455          ++i) {
456         std::stringstream nameStream;
457         // Add a newline to search for the exact package name.
458         nameStream << "testapp" << "_" << i << "\n";
459         std::string fullPkgName = nameStream.str();
460 
461         EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr(fullPkgName.c_str()));
462     }
463 }
464 
TEST_F(GpuStatsTest,canDumpAllBeforeClearAll)465 TEST_F(GpuStatsTest, canDumpAllBeforeClearAll) {
466     mGpuStats->insertDriverStats(BUILTIN_DRIVER_PKG_NAME, BUILTIN_DRIVER_VER_NAME,
467                                  BUILTIN_DRIVER_VER_CODE, BUILTIN_DRIVER_BUILD_TIME, APP_PKG_NAME_1,
468                                  VULKAN_VERSION, GpuStatsInfo::Driver::GL, true,
469                                  DRIVER_LOADING_TIME_1);
470 
471     EXPECT_FALSE(inputCommand(InputCommand::DUMP_ALL_THEN_CLEAR).empty());
472     EXPECT_TRUE(inputCommand(InputCommand::DUMP_ALL).empty());
473 }
474 
TEST_F(GpuStatsTest,canDumpGlobalBeforeClearGlobal)475 TEST_F(GpuStatsTest, canDumpGlobalBeforeClearGlobal) {
476     mGpuStats->insertDriverStats(BUILTIN_DRIVER_PKG_NAME, BUILTIN_DRIVER_VER_NAME,
477                                  BUILTIN_DRIVER_VER_CODE, BUILTIN_DRIVER_BUILD_TIME, APP_PKG_NAME_1,
478                                  VULKAN_VERSION, GpuStatsInfo::Driver::GL, true,
479                                  DRIVER_LOADING_TIME_1);
480 
481     EXPECT_FALSE(inputCommand(InputCommand::DUMP_GLOBAL_THEN_CLEAR).empty());
482     EXPECT_TRUE(inputCommand(InputCommand::DUMP_GLOBAL).empty());
483     EXPECT_FALSE(inputCommand(InputCommand::DUMP_APP).empty());
484 }
485 
TEST_F(GpuStatsTest,canDumpAppBeforeClearApp)486 TEST_F(GpuStatsTest, canDumpAppBeforeClearApp) {
487     mGpuStats->insertDriverStats(BUILTIN_DRIVER_PKG_NAME, BUILTIN_DRIVER_VER_NAME,
488                                  BUILTIN_DRIVER_VER_CODE, BUILTIN_DRIVER_BUILD_TIME, APP_PKG_NAME_1,
489                                  VULKAN_VERSION, GpuStatsInfo::Driver::GL, true,
490                                  DRIVER_LOADING_TIME_1);
491 
492     EXPECT_FALSE(inputCommand(InputCommand::DUMP_APP_THEN_CLEAR).empty());
493     EXPECT_TRUE(inputCommand(InputCommand::DUMP_APP).empty());
494     EXPECT_FALSE(inputCommand(InputCommand::DUMP_GLOBAL).empty());
495 }
496 
TEST_F(GpuStatsTest,skipPullInvalidAtom)497 TEST_F(GpuStatsTest, skipPullInvalidAtom) {
498     TestableGpuStats testableGpuStats(mGpuStats.get());
499     mGpuStats->insertDriverStats(BUILTIN_DRIVER_PKG_NAME, BUILTIN_DRIVER_VER_NAME,
500                                  BUILTIN_DRIVER_VER_CODE, BUILTIN_DRIVER_BUILD_TIME, APP_PKG_NAME_1,
501                                  VULKAN_VERSION, GpuStatsInfo::Driver::GL, true,
502                                  DRIVER_LOADING_TIME_1);
503 
504     EXPECT_FALSE(inputCommand(InputCommand::DUMP_GLOBAL).empty());
505     EXPECT_FALSE(inputCommand(InputCommand::DUMP_APP).empty());
506 
507     EXPECT_TRUE(testableGpuStats.makePullAtomCallback(-1) == AStatsManager_PULL_SKIP);
508 
509     EXPECT_FALSE(inputCommand(InputCommand::DUMP_GLOBAL).empty());
510     EXPECT_FALSE(inputCommand(InputCommand::DUMP_APP).empty());
511 }
512 
TEST_F(GpuStatsTest,canPullGlobalAtom)513 TEST_F(GpuStatsTest, canPullGlobalAtom) {
514     TestableGpuStats testableGpuStats(mGpuStats.get());
515     mGpuStats->insertDriverStats(BUILTIN_DRIVER_PKG_NAME, BUILTIN_DRIVER_VER_NAME,
516                                  BUILTIN_DRIVER_VER_CODE, BUILTIN_DRIVER_BUILD_TIME, APP_PKG_NAME_1,
517                                  VULKAN_VERSION, GpuStatsInfo::Driver::GL, true,
518                                  DRIVER_LOADING_TIME_1);
519 
520     EXPECT_FALSE(inputCommand(InputCommand::DUMP_GLOBAL).empty());
521     EXPECT_FALSE(inputCommand(InputCommand::DUMP_APP).empty());
522 
523     EXPECT_TRUE(testableGpuStats.makePullAtomCallback(android::util::GPU_STATS_GLOBAL_INFO) ==
524                 AStatsManager_PULL_SUCCESS);
525 
526     EXPECT_TRUE(inputCommand(InputCommand::DUMP_GLOBAL).empty());
527     EXPECT_FALSE(inputCommand(InputCommand::DUMP_APP).empty());
528 }
529 
TEST_F(GpuStatsTest,canPullAppAtom)530 TEST_F(GpuStatsTest, canPullAppAtom) {
531     TestableGpuStats testableGpuStats(mGpuStats.get());
532     mGpuStats->insertDriverStats(BUILTIN_DRIVER_PKG_NAME, BUILTIN_DRIVER_VER_NAME,
533                                  BUILTIN_DRIVER_VER_CODE, BUILTIN_DRIVER_BUILD_TIME, APP_PKG_NAME_1,
534                                  VULKAN_VERSION, GpuStatsInfo::Driver::GL, true,
535                                  DRIVER_LOADING_TIME_1);
536 
537     EXPECT_FALSE(inputCommand(InputCommand::DUMP_GLOBAL).empty());
538     EXPECT_FALSE(inputCommand(InputCommand::DUMP_APP).empty());
539 
540     EXPECT_TRUE(testableGpuStats.makePullAtomCallback(android::util::GPU_STATS_APP_INFO) ==
541                 AStatsManager_PULL_SUCCESS);
542 
543     EXPECT_FALSE(inputCommand(InputCommand::DUMP_GLOBAL).empty());
544     EXPECT_TRUE(inputCommand(InputCommand::DUMP_APP).empty());
545 }
546 
547 } // namespace
548 } // namespace android
549