• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cutils/properties.h>
21 #include <gmock/gmock.h>
22 #include <gpustats/GpuStats.h>
23 #include <gtest/gtest.h>
24 #include <stats_pull_atom_callback.h>
25 #include <statslog.h>
26 #include <utils/String16.h>
27 #include <utils/Vector.h>
28 
29 #include "TestableGpuStats.h"
30 
31 namespace android {
32 namespace {
33 
34 using testing::HasSubstr;
35 
36 // clang-format off
37 #define BUILTIN_DRIVER_PKG_NAME   "system"
38 #define BUILTIN_DRIVER_VER_NAME   "0"
39 #define BUILTIN_DRIVER_VER_CODE   0
40 #define BUILTIN_DRIVER_BUILD_TIME 123
41 #define UPDATED_DRIVER_PKG_NAME   "updated"
42 #define UPDATED_DRIVER_VER_NAME   "1"
43 #define UPDATED_DRIVER_VER_CODE   1
44 #define UPDATED_DRIVER_BUILD_TIME 234
45 #define VULKAN_VERSION            345
46 #define APP_PKG_NAME_1            "testapp1"
47 #define APP_PKG_NAME_2            "testapp2"
48 #define DRIVER_LOADING_TIME_1     678
49 #define DRIVER_LOADING_TIME_2     789
50 #define DRIVER_LOADING_TIME_3     891
51 
52 enum InputCommand : int32_t {
53     DUMP_ALL               = 0,
54     DUMP_GLOBAL            = 1,
55     DUMP_APP               = 2,
56     DUMP_ALL_THEN_CLEAR    = 3,
57     DUMP_GLOBAL_THEN_CLEAR = 4,
58     DUMP_APP_THEN_CLEAR    = 5,
59 };
60 // clang-format on
61 
62 class GpuStatsTest : public testing::Test {
63 public:
GpuStatsTest()64     GpuStatsTest() {
65         const ::testing::TestInfo* const test_info =
66                 ::testing::UnitTest::GetInstance()->current_test_info();
67         ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
68     }
69 
~GpuStatsTest()70     ~GpuStatsTest() {
71         const ::testing::TestInfo* const test_info =
72                 ::testing::UnitTest::GetInstance()->current_test_info();
73         ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
74     }
75 
76     std::string inputCommand(InputCommand cmd);
77 
SetUp()78     void SetUp() override {
79         mCpuVulkanVersion = property_get_int32("ro.cpuvulkan.version", 0);
80         mGlesVersion = property_get_int32("ro.opengles.version", 0);
81     }
82 
83     std::unique_ptr<GpuStats> mGpuStats = std::make_unique<GpuStats>();
84     int32_t mCpuVulkanVersion = 0;
85     int32_t mGlesVersion = 0;
86 };
87 
inputCommand(InputCommand cmd)88 std::string GpuStatsTest::inputCommand(InputCommand cmd) {
89     std::string result;
90     Vector<String16> args;
91 
92     switch (cmd) {
93         case InputCommand::DUMP_ALL:
94             break;
95         case InputCommand::DUMP_GLOBAL:
96             args.push_back(String16("--global"));
97             break;
98         case InputCommand::DUMP_APP:
99             args.push_back(String16("--app"));
100             break;
101         case InputCommand::DUMP_ALL_THEN_CLEAR:
102             args.push_back(String16("--clear"));
103             break;
104         case InputCommand::DUMP_GLOBAL_THEN_CLEAR:
105             args.push_back(String16("--global"));
106             args.push_back(String16("--clear"));
107             break;
108         case InputCommand::DUMP_APP_THEN_CLEAR:
109             args.push_back(String16("--app"));
110             args.push_back(String16("--clear"));
111             break;
112     }
113 
114     mGpuStats->dump(args, &result);
115     return result;
116 }
117 
TEST_F(GpuStatsTest,statsEmptyByDefault)118 TEST_F(GpuStatsTest, statsEmptyByDefault) {
119     ASSERT_TRUE(inputCommand(InputCommand::DUMP_ALL).empty());
120 }
121 
TEST_F(GpuStatsTest,canInsertBuiltinDriverStats)122 TEST_F(GpuStatsTest, canInsertBuiltinDriverStats) {
123     mGpuStats->insertDriverStats(BUILTIN_DRIVER_PKG_NAME, BUILTIN_DRIVER_VER_NAME,
124                                  BUILTIN_DRIVER_VER_CODE, BUILTIN_DRIVER_BUILD_TIME, APP_PKG_NAME_1,
125                                  VULKAN_VERSION, GpuStatsInfo::Driver::GL, true,
126                                  DRIVER_LOADING_TIME_1);
127 
128     std::string expectedResult = "driverPackageName = " + std::string(BUILTIN_DRIVER_PKG_NAME);
129     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr(expectedResult));
130     expectedResult = "driverVersionName = " + std::string(BUILTIN_DRIVER_VER_NAME);
131     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr(expectedResult));
132     expectedResult = "driverVersionCode = " + std::to_string(BUILTIN_DRIVER_VER_CODE);
133     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr(expectedResult));
134     expectedResult = "driverBuildTime = " + std::to_string(BUILTIN_DRIVER_BUILD_TIME);
135     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr(expectedResult));
136     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr("glLoadingCount = 1"));
137     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr("glLoadingFailureCount = 0"));
138     expectedResult = "appPackageName = " + std::string(APP_PKG_NAME_1);
139     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr(expectedResult));
140     expectedResult = "driverVersionCode = " + std::to_string(BUILTIN_DRIVER_VER_CODE);
141     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr(expectedResult));
142     expectedResult = "glDriverLoadingTime: " + std::to_string(DRIVER_LOADING_TIME_1);
143     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr(expectedResult));
144 }
145 
TEST_F(GpuStatsTest,canInsertUpdatedDriverStats)146 TEST_F(GpuStatsTest, canInsertUpdatedDriverStats) {
147     mGpuStats->insertDriverStats(UPDATED_DRIVER_PKG_NAME, UPDATED_DRIVER_VER_NAME,
148                                  UPDATED_DRIVER_VER_CODE, UPDATED_DRIVER_BUILD_TIME, APP_PKG_NAME_2,
149                                  VULKAN_VERSION, GpuStatsInfo::Driver::VULKAN_UPDATED, false,
150                                  DRIVER_LOADING_TIME_2);
151 
152     std::string expectedResult = "driverPackageName = " + std::string(UPDATED_DRIVER_PKG_NAME);
153     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr(expectedResult));
154     expectedResult = "driverVersionName = " + std::string(UPDATED_DRIVER_VER_NAME);
155     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr(expectedResult));
156     expectedResult = "driverVersionCode = " + std::to_string(UPDATED_DRIVER_VER_CODE);
157     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr(expectedResult));
158     expectedResult = "driverBuildTime = " + std::to_string(UPDATED_DRIVER_BUILD_TIME);
159     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr(expectedResult));
160     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr("vkLoadingCount = 1"));
161     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr("vkLoadingFailureCount = 1"));
162     expectedResult = "appPackageName = " + std::string(APP_PKG_NAME_2);
163     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr(expectedResult));
164     expectedResult = "driverVersionCode = " + std::to_string(UPDATED_DRIVER_VER_CODE);
165     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr(expectedResult));
166     expectedResult = "vkDriverLoadingTime: " + std::to_string(DRIVER_LOADING_TIME_2);
167     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr(expectedResult));
168 }
169 
TEST_F(GpuStatsTest,canInsertAngleDriverStats)170 TEST_F(GpuStatsTest, canInsertAngleDriverStats) {
171     mGpuStats->insertDriverStats(UPDATED_DRIVER_PKG_NAME, UPDATED_DRIVER_VER_NAME,
172                                  UPDATED_DRIVER_VER_CODE, UPDATED_DRIVER_BUILD_TIME, APP_PKG_NAME_2,
173                                  VULKAN_VERSION, GpuStatsInfo::Driver::ANGLE, true,
174                                  DRIVER_LOADING_TIME_3);
175 
176     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr("angleLoadingCount = 1"));
177     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr("angleLoadingFailureCount = 0"));
178     std::string expectedResult = "angleDriverLoadingTime: " + std::to_string(DRIVER_LOADING_TIME_3);
179     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr(expectedResult));
180 }
181 
TEST_F(GpuStatsTest,canDump3dApiVersion)182 TEST_F(GpuStatsTest, canDump3dApiVersion) {
183     mGpuStats->insertDriverStats(BUILTIN_DRIVER_PKG_NAME, BUILTIN_DRIVER_VER_NAME,
184                                  BUILTIN_DRIVER_VER_CODE, BUILTIN_DRIVER_BUILD_TIME, APP_PKG_NAME_1,
185                                  VULKAN_VERSION, GpuStatsInfo::Driver::GL, true,
186                                  DRIVER_LOADING_TIME_1);
187 
188     std::string expectedResult = "vulkanVersion = " + std::to_string(VULKAN_VERSION);
189     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr(expectedResult));
190     expectedResult = "cpuVulkanVersion = " + std::to_string(mCpuVulkanVersion);
191     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr(expectedResult));
192     expectedResult = "glesVersion = " + std::to_string(mGlesVersion);
193     EXPECT_THAT(inputCommand(InputCommand::DUMP_GLOBAL), HasSubstr(expectedResult));
194 }
195 
TEST_F(GpuStatsTest,canNotInsertTargetStatsBeforeProperSetup)196 TEST_F(GpuStatsTest, canNotInsertTargetStatsBeforeProperSetup) {
197     mGpuStats->insertTargetStats(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
198                                  GpuStatsInfo::Stats::CPU_VULKAN_IN_USE, 0);
199     mGpuStats->insertTargetStats(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
200                                  GpuStatsInfo::Stats::FALSE_PREROTATION, 0);
201     mGpuStats->insertTargetStats(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
202                                  GpuStatsInfo::Stats::GLES_1_IN_USE, 0);
203 
204     EXPECT_TRUE(inputCommand(InputCommand::DUMP_APP).empty());
205 }
206 
TEST_F(GpuStatsTest,canInsertTargetStatsAfterProperSetup)207 TEST_F(GpuStatsTest, canInsertTargetStatsAfterProperSetup) {
208     mGpuStats->insertDriverStats(BUILTIN_DRIVER_PKG_NAME, BUILTIN_DRIVER_VER_NAME,
209                                  BUILTIN_DRIVER_VER_CODE, BUILTIN_DRIVER_BUILD_TIME, APP_PKG_NAME_1,
210                                  VULKAN_VERSION, GpuStatsInfo::Driver::GL, true,
211                                  DRIVER_LOADING_TIME_1);
212     mGpuStats->insertTargetStats(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
213                                  GpuStatsInfo::Stats::CPU_VULKAN_IN_USE, 0);
214     mGpuStats->insertTargetStats(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
215                                  GpuStatsInfo::Stats::FALSE_PREROTATION, 0);
216     mGpuStats->insertTargetStats(APP_PKG_NAME_1, BUILTIN_DRIVER_VER_CODE,
217                                  GpuStatsInfo::Stats::GLES_1_IN_USE, 0);
218 
219     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr("cpuVulkanInUse = 1"));
220     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr("falsePrerotation = 1"));
221     EXPECT_THAT(inputCommand(InputCommand::DUMP_APP), HasSubstr("gles1InUse = 1"));
222 }
223 
TEST_F(GpuStatsTest,canDumpAllBeforeClearAll)224 TEST_F(GpuStatsTest, canDumpAllBeforeClearAll) {
225     mGpuStats->insertDriverStats(BUILTIN_DRIVER_PKG_NAME, BUILTIN_DRIVER_VER_NAME,
226                                  BUILTIN_DRIVER_VER_CODE, BUILTIN_DRIVER_BUILD_TIME, APP_PKG_NAME_1,
227                                  VULKAN_VERSION, GpuStatsInfo::Driver::GL, true,
228                                  DRIVER_LOADING_TIME_1);
229 
230     EXPECT_FALSE(inputCommand(InputCommand::DUMP_ALL_THEN_CLEAR).empty());
231     EXPECT_TRUE(inputCommand(InputCommand::DUMP_ALL).empty());
232 }
233 
TEST_F(GpuStatsTest,canDumpGlobalBeforeClearGlobal)234 TEST_F(GpuStatsTest, canDumpGlobalBeforeClearGlobal) {
235     mGpuStats->insertDriverStats(BUILTIN_DRIVER_PKG_NAME, BUILTIN_DRIVER_VER_NAME,
236                                  BUILTIN_DRIVER_VER_CODE, BUILTIN_DRIVER_BUILD_TIME, APP_PKG_NAME_1,
237                                  VULKAN_VERSION, GpuStatsInfo::Driver::GL, true,
238                                  DRIVER_LOADING_TIME_1);
239 
240     EXPECT_FALSE(inputCommand(InputCommand::DUMP_GLOBAL_THEN_CLEAR).empty());
241     EXPECT_TRUE(inputCommand(InputCommand::DUMP_GLOBAL).empty());
242     EXPECT_FALSE(inputCommand(InputCommand::DUMP_APP).empty());
243 }
244 
TEST_F(GpuStatsTest,canDumpAppBeforeClearApp)245 TEST_F(GpuStatsTest, canDumpAppBeforeClearApp) {
246     mGpuStats->insertDriverStats(BUILTIN_DRIVER_PKG_NAME, BUILTIN_DRIVER_VER_NAME,
247                                  BUILTIN_DRIVER_VER_CODE, BUILTIN_DRIVER_BUILD_TIME, APP_PKG_NAME_1,
248                                  VULKAN_VERSION, GpuStatsInfo::Driver::GL, true,
249                                  DRIVER_LOADING_TIME_1);
250 
251     EXPECT_FALSE(inputCommand(InputCommand::DUMP_APP_THEN_CLEAR).empty());
252     EXPECT_TRUE(inputCommand(InputCommand::DUMP_APP).empty());
253     EXPECT_FALSE(inputCommand(InputCommand::DUMP_GLOBAL).empty());
254 }
255 
TEST_F(GpuStatsTest,skipPullInvalidAtom)256 TEST_F(GpuStatsTest, skipPullInvalidAtom) {
257     TestableGpuStats testableGpuStats(mGpuStats.get());
258     mGpuStats->insertDriverStats(BUILTIN_DRIVER_PKG_NAME, BUILTIN_DRIVER_VER_NAME,
259                                  BUILTIN_DRIVER_VER_CODE, BUILTIN_DRIVER_BUILD_TIME, APP_PKG_NAME_1,
260                                  VULKAN_VERSION, GpuStatsInfo::Driver::GL, true,
261                                  DRIVER_LOADING_TIME_1);
262 
263     EXPECT_FALSE(inputCommand(InputCommand::DUMP_GLOBAL).empty());
264     EXPECT_FALSE(inputCommand(InputCommand::DUMP_APP).empty());
265 
266     EXPECT_TRUE(testableGpuStats.makePullAtomCallback(-1) == AStatsManager_PULL_SKIP);
267 
268     EXPECT_FALSE(inputCommand(InputCommand::DUMP_GLOBAL).empty());
269     EXPECT_FALSE(inputCommand(InputCommand::DUMP_APP).empty());
270 }
271 
TEST_F(GpuStatsTest,canPullGlobalAtom)272 TEST_F(GpuStatsTest, canPullGlobalAtom) {
273     TestableGpuStats testableGpuStats(mGpuStats.get());
274     mGpuStats->insertDriverStats(BUILTIN_DRIVER_PKG_NAME, BUILTIN_DRIVER_VER_NAME,
275                                  BUILTIN_DRIVER_VER_CODE, BUILTIN_DRIVER_BUILD_TIME, APP_PKG_NAME_1,
276                                  VULKAN_VERSION, GpuStatsInfo::Driver::GL, true,
277                                  DRIVER_LOADING_TIME_1);
278 
279     EXPECT_FALSE(inputCommand(InputCommand::DUMP_GLOBAL).empty());
280     EXPECT_FALSE(inputCommand(InputCommand::DUMP_APP).empty());
281 
282     EXPECT_TRUE(testableGpuStats.makePullAtomCallback(android::util::GPU_STATS_GLOBAL_INFO) ==
283                 AStatsManager_PULL_SUCCESS);
284 
285     EXPECT_TRUE(inputCommand(InputCommand::DUMP_GLOBAL).empty());
286     EXPECT_FALSE(inputCommand(InputCommand::DUMP_APP).empty());
287 }
288 
TEST_F(GpuStatsTest,canPullAppAtom)289 TEST_F(GpuStatsTest, canPullAppAtom) {
290     TestableGpuStats testableGpuStats(mGpuStats.get());
291     mGpuStats->insertDriverStats(BUILTIN_DRIVER_PKG_NAME, BUILTIN_DRIVER_VER_NAME,
292                                  BUILTIN_DRIVER_VER_CODE, BUILTIN_DRIVER_BUILD_TIME, APP_PKG_NAME_1,
293                                  VULKAN_VERSION, GpuStatsInfo::Driver::GL, true,
294                                  DRIVER_LOADING_TIME_1);
295 
296     EXPECT_FALSE(inputCommand(InputCommand::DUMP_GLOBAL).empty());
297     EXPECT_FALSE(inputCommand(InputCommand::DUMP_APP).empty());
298 
299     EXPECT_TRUE(testableGpuStats.makePullAtomCallback(android::util::GPU_STATS_APP_INFO) ==
300                 AStatsManager_PULL_SUCCESS);
301 
302     EXPECT_FALSE(inputCommand(InputCommand::DUMP_GLOBAL).empty());
303     EXPECT_TRUE(inputCommand(InputCommand::DUMP_APP).empty());
304 }
305 
306 } // namespace
307 } // namespace android
308