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