Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | #undef LOG_TAG |
| 17 | #define LOG_TAG "GpuStats" |
| 18 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 19 | |
| 20 | #include "GpuStats.h" |
| 21 | |
Yiwei Zhang | 174a2a0 | 2019-05-06 19:08:31 -0700 | [diff] [blame] | 22 | #include <cutils/properties.h> |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 23 | #include <log/log.h> |
| 24 | #include <utils/Trace.h> |
| 25 | |
Yiwei Zhang | 174a2a0 | 2019-05-06 19:08:31 -0700 | [diff] [blame] | 26 | #include <unordered_set> |
| 27 | |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 28 | namespace android { |
| 29 | |
Yiwei Zhang | 8e7c4b6 | 2019-05-08 15:57:59 -0700 | [diff] [blame^] | 30 | static void addLoadingCount(GraphicsEnv::Driver driver, bool isDriverLoaded, |
Yiwei Zhang | f40fb10 | 2019-02-27 21:05:06 -0800 | [diff] [blame] | 31 | GpuStatsGlobalInfo* const outGlobalInfo) { |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 32 | switch (driver) { |
| 33 | case GraphicsEnv::Driver::GL: |
| 34 | case GraphicsEnv::Driver::GL_UPDATED: |
Yiwei Zhang | f40fb10 | 2019-02-27 21:05:06 -0800 | [diff] [blame] | 35 | outGlobalInfo->glLoadingCount++; |
| 36 | if (!isDriverLoaded) outGlobalInfo->glLoadingFailureCount++; |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 37 | break; |
| 38 | case GraphicsEnv::Driver::VULKAN: |
| 39 | case GraphicsEnv::Driver::VULKAN_UPDATED: |
Yiwei Zhang | f40fb10 | 2019-02-27 21:05:06 -0800 | [diff] [blame] | 40 | outGlobalInfo->vkLoadingCount++; |
| 41 | if (!isDriverLoaded) outGlobalInfo->vkLoadingFailureCount++; |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 42 | break; |
Yiwei Zhang | 8e7c4b6 | 2019-05-08 15:57:59 -0700 | [diff] [blame^] | 43 | case GraphicsEnv::Driver::ANGLE: |
| 44 | outGlobalInfo->angleLoadingCount++; |
| 45 | if (!isDriverLoaded) outGlobalInfo->angleLoadingFailureCount++; |
| 46 | break; |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 47 | default: |
Yiwei Zhang | 8e7c4b6 | 2019-05-08 15:57:59 -0700 | [diff] [blame^] | 48 | break; |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 49 | } |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static void addLoadingTime(GraphicsEnv::Driver driver, int64_t driverLoadingTime, |
Yiwei Zhang | f40fb10 | 2019-02-27 21:05:06 -0800 | [diff] [blame] | 53 | GpuStatsAppInfo* const outAppInfo) { |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 54 | switch (driver) { |
| 55 | case GraphicsEnv::Driver::GL: |
| 56 | case GraphicsEnv::Driver::GL_UPDATED: |
Yiwei Zhang | 8e7c4b6 | 2019-05-08 15:57:59 -0700 | [diff] [blame^] | 57 | if (outAppInfo->glDriverLoadingTime.size() < GpuStats::MAX_NUM_LOADING_TIMES) { |
| 58 | outAppInfo->glDriverLoadingTime.emplace_back(driverLoadingTime); |
| 59 | } |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 60 | break; |
| 61 | case GraphicsEnv::Driver::VULKAN: |
| 62 | case GraphicsEnv::Driver::VULKAN_UPDATED: |
Yiwei Zhang | 8e7c4b6 | 2019-05-08 15:57:59 -0700 | [diff] [blame^] | 63 | if (outAppInfo->vkDriverLoadingTime.size() < GpuStats::MAX_NUM_LOADING_TIMES) { |
| 64 | outAppInfo->vkDriverLoadingTime.emplace_back(driverLoadingTime); |
| 65 | } |
| 66 | break; |
| 67 | case GraphicsEnv::Driver::ANGLE: |
| 68 | if (outAppInfo->angleDriverLoadingTime.size() < GpuStats::MAX_NUM_LOADING_TIMES) { |
| 69 | outAppInfo->angleDriverLoadingTime.emplace_back(driverLoadingTime); |
| 70 | } |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 71 | break; |
| 72 | default: |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void GpuStats::insert(const std::string& driverPackageName, const std::string& driverVersionName, |
| 78 | uint64_t driverVersionCode, int64_t driverBuildTime, |
Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 79 | const std::string& appPackageName, const int32_t vulkanVersion, |
| 80 | GraphicsEnv::Driver driver, bool isDriverLoaded, int64_t driverLoadingTime) { |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 81 | ATRACE_CALL(); |
| 82 | |
| 83 | std::lock_guard<std::mutex> lock(mLock); |
| 84 | ALOGV("Received:\n" |
| 85 | "\tdriverPackageName[%s]\n" |
| 86 | "\tdriverVersionName[%s]\n" |
| 87 | "\tdriverVersionCode[%" PRIu64 "]\n" |
| 88 | "\tdriverBuildTime[%" PRId64 "]\n" |
| 89 | "\tappPackageName[%s]\n" |
Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 90 | "\tvulkanVersion[%d]\n" |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 91 | "\tdriver[%d]\n" |
| 92 | "\tisDriverLoaded[%d]\n" |
| 93 | "\tdriverLoadingTime[%" PRId64 "]", |
| 94 | driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime, |
Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 95 | appPackageName.c_str(), vulkanVersion, static_cast<int32_t>(driver), isDriverLoaded, |
| 96 | driverLoadingTime); |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 97 | |
| 98 | if (!mGlobalStats.count(driverVersionCode)) { |
Yiwei Zhang | f40fb10 | 2019-02-27 21:05:06 -0800 | [diff] [blame] | 99 | GpuStatsGlobalInfo globalInfo; |
Yiwei Zhang | 8e7c4b6 | 2019-05-08 15:57:59 -0700 | [diff] [blame^] | 100 | addLoadingCount(driver, isDriverLoaded, &globalInfo); |
Yiwei Zhang | f40fb10 | 2019-02-27 21:05:06 -0800 | [diff] [blame] | 101 | globalInfo.driverPackageName = driverPackageName; |
| 102 | globalInfo.driverVersionName = driverVersionName; |
| 103 | globalInfo.driverVersionCode = driverVersionCode; |
| 104 | globalInfo.driverBuildTime = driverBuildTime; |
Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 105 | globalInfo.vulkanVersion = vulkanVersion; |
Yiwei Zhang | f40fb10 | 2019-02-27 21:05:06 -0800 | [diff] [blame] | 106 | mGlobalStats.insert({driverVersionCode, globalInfo}); |
Yiwei Zhang | 8e7c4b6 | 2019-05-08 15:57:59 -0700 | [diff] [blame^] | 107 | } else { |
| 108 | addLoadingCount(driver, isDriverLoaded, &mGlobalStats[driverVersionCode]); |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | if (mAppStats.size() >= MAX_NUM_APP_RECORDS) { |
Yiwei Zhang | f40fb10 | 2019-02-27 21:05:06 -0800 | [diff] [blame] | 112 | ALOGV("GpuStatsAppInfo has reached maximum size. Ignore new stats."); |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 113 | return; |
| 114 | } |
| 115 | |
| 116 | const std::string appStatsKey = appPackageName + std::to_string(driverVersionCode); |
| 117 | if (!mAppStats.count(appStatsKey)) { |
Yiwei Zhang | f40fb10 | 2019-02-27 21:05:06 -0800 | [diff] [blame] | 118 | GpuStatsAppInfo appInfo; |
| 119 | addLoadingTime(driver, driverLoadingTime, &appInfo); |
| 120 | appInfo.appPackageName = appPackageName; |
| 121 | appInfo.driverVersionCode = driverVersionCode; |
| 122 | mAppStats.insert({appStatsKey, appInfo}); |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 123 | return; |
| 124 | } |
| 125 | |
| 126 | addLoadingTime(driver, driverLoadingTime, &mAppStats[appStatsKey]); |
| 127 | } |
| 128 | |
Yiwei Zhang | 174a2a0 | 2019-05-06 19:08:31 -0700 | [diff] [blame] | 129 | void GpuStats::interceptSystemDriverStatsLocked() { |
| 130 | // Append cpuVulkanVersion and glesVersion to system driver stats |
| 131 | if (!mGlobalStats.count(0) || mGlobalStats[0].glesVersion) { |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | mGlobalStats[0].cpuVulkanVersion = property_get_int32("ro.cpuvulkan.version", 0); |
| 136 | mGlobalStats[0].glesVersion = property_get_int32("ro.opengles.version", 0); |
| 137 | } |
| 138 | |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 139 | void GpuStats::dump(const Vector<String16>& args, std::string* result) { |
| 140 | ATRACE_CALL(); |
| 141 | |
| 142 | if (!result) { |
| 143 | ALOGE("Dump result shouldn't be nullptr."); |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | std::lock_guard<std::mutex> lock(mLock); |
| 148 | bool dumpAll = true; |
| 149 | |
| 150 | std::unordered_set<std::string> argsSet; |
| 151 | for (size_t i = 0; i < args.size(); i++) { |
| 152 | argsSet.insert(String8(args[i]).c_str()); |
| 153 | } |
| 154 | |
| 155 | const bool dumpGlobal = argsSet.count("--global") != 0; |
| 156 | if (dumpGlobal) { |
| 157 | dumpGlobalLocked(result); |
| 158 | dumpAll = false; |
| 159 | } |
| 160 | |
| 161 | const bool dumpApp = argsSet.count("--app") != 0; |
| 162 | if (dumpApp) { |
| 163 | dumpAppLocked(result); |
| 164 | dumpAll = false; |
| 165 | } |
| 166 | |
| 167 | if (argsSet.count("--clear")) { |
| 168 | bool clearAll = true; |
| 169 | |
| 170 | if (dumpGlobal) { |
| 171 | mGlobalStats.clear(); |
| 172 | clearAll = false; |
| 173 | } |
| 174 | |
| 175 | if (dumpApp) { |
| 176 | mAppStats.clear(); |
| 177 | clearAll = false; |
| 178 | } |
| 179 | |
| 180 | if (clearAll) { |
| 181 | mGlobalStats.clear(); |
| 182 | mAppStats.clear(); |
| 183 | } |
| 184 | |
| 185 | dumpAll = false; |
| 186 | } |
| 187 | |
| 188 | if (dumpAll) { |
| 189 | dumpGlobalLocked(result); |
| 190 | dumpAppLocked(result); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void GpuStats::dumpGlobalLocked(std::string* result) { |
Yiwei Zhang | 174a2a0 | 2019-05-06 19:08:31 -0700 | [diff] [blame] | 195 | interceptSystemDriverStatsLocked(); |
| 196 | |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 197 | for (const auto& ele : mGlobalStats) { |
Yiwei Zhang | f40fb10 | 2019-02-27 21:05:06 -0800 | [diff] [blame] | 198 | result->append(ele.second.toString()); |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 199 | result->append("\n"); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | void GpuStats::dumpAppLocked(std::string* result) { |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 204 | for (const auto& ele : mAppStats) { |
Yiwei Zhang | f40fb10 | 2019-02-27 21:05:06 -0800 | [diff] [blame] | 205 | result->append(ele.second.toString()); |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 206 | result->append("\n"); |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
Yiwei Zhang | baea97f | 2019-02-27 16:35:37 -0800 | [diff] [blame] | 210 | void GpuStats::pullGlobalStats(std::vector<GpuStatsGlobalInfo>* outStats) { |
| 211 | ATRACE_CALL(); |
| 212 | |
| 213 | std::lock_guard<std::mutex> lock(mLock); |
| 214 | outStats->clear(); |
| 215 | outStats->reserve(mGlobalStats.size()); |
| 216 | |
Yiwei Zhang | 174a2a0 | 2019-05-06 19:08:31 -0700 | [diff] [blame] | 217 | interceptSystemDriverStatsLocked(); |
| 218 | |
Yiwei Zhang | baea97f | 2019-02-27 16:35:37 -0800 | [diff] [blame] | 219 | for (const auto& ele : mGlobalStats) { |
| 220 | outStats->emplace_back(ele.second); |
| 221 | } |
| 222 | |
| 223 | mGlobalStats.clear(); |
| 224 | } |
| 225 | |
Yiwei Zhang | 178e067 | 2019-03-04 14:17:12 -0800 | [diff] [blame] | 226 | void GpuStats::pullAppStats(std::vector<GpuStatsAppInfo>* outStats) { |
| 227 | ATRACE_CALL(); |
| 228 | |
| 229 | std::lock_guard<std::mutex> lock(mLock); |
| 230 | outStats->clear(); |
| 231 | outStats->reserve(mAppStats.size()); |
| 232 | |
| 233 | for (const auto& ele : mAppStats) { |
| 234 | outStats->emplace_back(ele.second); |
| 235 | } |
| 236 | |
| 237 | mAppStats.clear(); |
| 238 | } |
| 239 | |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 240 | } // namespace android |