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