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 | |
Yiwei Zhang | 29f8593 | 2020-02-04 17:14:54 -0800 | [diff] [blame] | 20 | #include "gpustats/GpuStats.h" |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 21 | |
Yiwei Zhang | 29f8593 | 2020-02-04 17:14:54 -0800 | [diff] [blame] | 22 | #include <android/util/ProtoOutputStream.h> |
Yiwei Zhang | 174a2a0 | 2019-05-06 19:08:31 -0700 | [diff] [blame] | 23 | #include <cutils/properties.h> |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 24 | #include <log/log.h> |
Yiwei Zhang | b59a127 | 2020-02-04 14:58:01 -0800 | [diff] [blame] | 25 | #include <stats_event.h> |
| 26 | #include <statslog.h> |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 27 | #include <utils/Trace.h> |
| 28 | |
Yiwei Zhang | 174a2a0 | 2019-05-06 19:08:31 -0700 | [diff] [blame] | 29 | #include <unordered_set> |
| 30 | |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 31 | namespace android { |
| 32 | |
Yiwei Zhang | b59a127 | 2020-02-04 14:58:01 -0800 | [diff] [blame] | 33 | GpuStats::~GpuStats() { |
Alec Mouri | 822b10a | 2020-03-17 17:51:27 -0700 | [diff] [blame] | 34 | if (mStatsdRegistered) { |
Tej Singh | 38a4b21 | 2020-03-13 19:04:51 -0700 | [diff] [blame] | 35 | AStatsManager_clearPullAtomCallback(android::util::GPU_STATS_GLOBAL_INFO); |
| 36 | AStatsManager_clearPullAtomCallback(android::util::GPU_STATS_APP_INFO); |
Alec Mouri | 822b10a | 2020-03-17 17:51:27 -0700 | [diff] [blame] | 37 | } |
Yiwei Zhang | b59a127 | 2020-02-04 14:58:01 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 40 | static void addLoadingCount(GpuStatsInfo::Driver driver, bool isDriverLoaded, |
Yiwei Zhang | f40fb10 | 2019-02-27 21:05:06 -0800 | [diff] [blame] | 41 | GpuStatsGlobalInfo* const outGlobalInfo) { |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 42 | switch (driver) { |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 43 | case GpuStatsInfo::Driver::GL: |
| 44 | case GpuStatsInfo::Driver::GL_UPDATED: |
Yiwei Zhang | f40fb10 | 2019-02-27 21:05:06 -0800 | [diff] [blame] | 45 | outGlobalInfo->glLoadingCount++; |
| 46 | if (!isDriverLoaded) outGlobalInfo->glLoadingFailureCount++; |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 47 | break; |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 48 | case GpuStatsInfo::Driver::VULKAN: |
| 49 | case GpuStatsInfo::Driver::VULKAN_UPDATED: |
Yiwei Zhang | f40fb10 | 2019-02-27 21:05:06 -0800 | [diff] [blame] | 50 | outGlobalInfo->vkLoadingCount++; |
| 51 | if (!isDriverLoaded) outGlobalInfo->vkLoadingFailureCount++; |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 52 | break; |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 53 | case GpuStatsInfo::Driver::ANGLE: |
Yiwei Zhang | 8e7c4b6 | 2019-05-08 15:57:59 -0700 | [diff] [blame] | 54 | outGlobalInfo->angleLoadingCount++; |
| 55 | if (!isDriverLoaded) outGlobalInfo->angleLoadingFailureCount++; |
| 56 | break; |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 57 | default: |
Yiwei Zhang | 8e7c4b6 | 2019-05-08 15:57:59 -0700 | [diff] [blame] | 58 | break; |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 59 | } |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 62 | static void addLoadingTime(GpuStatsInfo::Driver driver, int64_t driverLoadingTime, |
Yiwei Zhang | f40fb10 | 2019-02-27 21:05:06 -0800 | [diff] [blame] | 63 | GpuStatsAppInfo* const outAppInfo) { |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 64 | switch (driver) { |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 65 | case GpuStatsInfo::Driver::GL: |
| 66 | case GpuStatsInfo::Driver::GL_UPDATED: |
Yiwei Zhang | 8e7c4b6 | 2019-05-08 15:57:59 -0700 | [diff] [blame] | 67 | if (outAppInfo->glDriverLoadingTime.size() < GpuStats::MAX_NUM_LOADING_TIMES) { |
| 68 | outAppInfo->glDriverLoadingTime.emplace_back(driverLoadingTime); |
| 69 | } |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 70 | break; |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 71 | case GpuStatsInfo::Driver::VULKAN: |
| 72 | case GpuStatsInfo::Driver::VULKAN_UPDATED: |
Yiwei Zhang | 8e7c4b6 | 2019-05-08 15:57:59 -0700 | [diff] [blame] | 73 | if (outAppInfo->vkDriverLoadingTime.size() < GpuStats::MAX_NUM_LOADING_TIMES) { |
| 74 | outAppInfo->vkDriverLoadingTime.emplace_back(driverLoadingTime); |
| 75 | } |
| 76 | break; |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 77 | case GpuStatsInfo::Driver::ANGLE: |
Yiwei Zhang | 8e7c4b6 | 2019-05-08 15:57:59 -0700 | [diff] [blame] | 78 | if (outAppInfo->angleDriverLoadingTime.size() < GpuStats::MAX_NUM_LOADING_TIMES) { |
| 79 | outAppInfo->angleDriverLoadingTime.emplace_back(driverLoadingTime); |
| 80 | } |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 81 | break; |
| 82 | default: |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | |
Tim Van Patten | a9ad69b | 2021-11-24 19:29:38 -0700 | [diff] [blame] | 87 | void GpuStats::purgeOldDriverStats() { |
| 88 | ALOG_ASSERT(mAppStats.size() == MAX_NUM_APP_RECORDS); |
| 89 | |
| 90 | struct GpuStatsApp { |
| 91 | // Key is <app package name>+<driver version code>. |
| 92 | const std::string *appStatsKey = nullptr; |
| 93 | const std::chrono::time_point<std::chrono::system_clock> *lastAccessTime = nullptr; |
| 94 | }; |
| 95 | std::vector<GpuStatsApp> gpuStatsApps(MAX_NUM_APP_RECORDS); |
| 96 | |
| 97 | // Create a list of pointers to package names and their last access times. |
| 98 | int index = 0; |
| 99 | for (const auto & [appStatsKey, gpuStatsAppInfo] : mAppStats) { |
| 100 | GpuStatsApp &gpuStatsApp = gpuStatsApps[index]; |
| 101 | gpuStatsApp.appStatsKey = &appStatsKey; |
| 102 | gpuStatsApp.lastAccessTime = &gpuStatsAppInfo.lastAccessTime; |
| 103 | ++index; |
| 104 | } |
| 105 | |
| 106 | // Sort the list with the oldest access times at the front. |
| 107 | std::sort(gpuStatsApps.begin(), gpuStatsApps.end(), [](GpuStatsApp a, GpuStatsApp b) -> bool { |
| 108 | return *a.lastAccessTime < *b.lastAccessTime; |
| 109 | }); |
| 110 | |
| 111 | // Remove the oldest packages from mAppStats to make room for new apps. |
| 112 | for (int i = 0; i < APP_RECORD_HEADROOM; ++i) { |
| 113 | mAppStats.erase(*gpuStatsApps[i].appStatsKey); |
| 114 | gpuStatsApps[i].appStatsKey = nullptr; |
| 115 | gpuStatsApps[i].lastAccessTime = nullptr; |
| 116 | } |
| 117 | } |
| 118 | |
Yiwei Zhang | fdd7e78 | 2020-01-31 15:59:34 -0800 | [diff] [blame] | 119 | void GpuStats::insertDriverStats(const std::string& driverPackageName, |
| 120 | const std::string& driverVersionName, uint64_t driverVersionCode, |
| 121 | int64_t driverBuildTime, const std::string& appPackageName, |
| 122 | const int32_t vulkanVersion, GpuStatsInfo::Driver driver, |
| 123 | bool isDriverLoaded, int64_t driverLoadingTime) { |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 124 | ATRACE_CALL(); |
| 125 | |
| 126 | std::lock_guard<std::mutex> lock(mLock); |
Alec Mouri | 822b10a | 2020-03-17 17:51:27 -0700 | [diff] [blame] | 127 | registerStatsdCallbacksIfNeeded(); |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 128 | ALOGV("Received:\n" |
| 129 | "\tdriverPackageName[%s]\n" |
| 130 | "\tdriverVersionName[%s]\n" |
| 131 | "\tdriverVersionCode[%" PRIu64 "]\n" |
| 132 | "\tdriverBuildTime[%" PRId64 "]\n" |
| 133 | "\tappPackageName[%s]\n" |
Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 134 | "\tvulkanVersion[%d]\n" |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 135 | "\tdriver[%d]\n" |
| 136 | "\tisDriverLoaded[%d]\n" |
| 137 | "\tdriverLoadingTime[%" PRId64 "]", |
| 138 | driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime, |
Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 139 | appPackageName.c_str(), vulkanVersion, static_cast<int32_t>(driver), isDriverLoaded, |
| 140 | driverLoadingTime); |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 141 | |
| 142 | if (!mGlobalStats.count(driverVersionCode)) { |
Yiwei Zhang | f40fb10 | 2019-02-27 21:05:06 -0800 | [diff] [blame] | 143 | GpuStatsGlobalInfo globalInfo; |
Yiwei Zhang | 8e7c4b6 | 2019-05-08 15:57:59 -0700 | [diff] [blame] | 144 | addLoadingCount(driver, isDriverLoaded, &globalInfo); |
Yiwei Zhang | f40fb10 | 2019-02-27 21:05:06 -0800 | [diff] [blame] | 145 | globalInfo.driverPackageName = driverPackageName; |
| 146 | globalInfo.driverVersionName = driverVersionName; |
| 147 | globalInfo.driverVersionCode = driverVersionCode; |
| 148 | globalInfo.driverBuildTime = driverBuildTime; |
Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 149 | globalInfo.vulkanVersion = vulkanVersion; |
Yiwei Zhang | f40fb10 | 2019-02-27 21:05:06 -0800 | [diff] [blame] | 150 | mGlobalStats.insert({driverVersionCode, globalInfo}); |
Yiwei Zhang | 8e7c4b6 | 2019-05-08 15:57:59 -0700 | [diff] [blame] | 151 | } else { |
| 152 | addLoadingCount(driver, isDriverLoaded, &mGlobalStats[driverVersionCode]); |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 155 | const std::string appStatsKey = appPackageName + std::to_string(driverVersionCode); |
| 156 | if (!mAppStats.count(appStatsKey)) { |
Yiwei Zhang | 8c5e3bd | 2019-05-09 14:34:19 -0700 | [diff] [blame] | 157 | if (mAppStats.size() >= MAX_NUM_APP_RECORDS) { |
Tim Van Patten | a9ad69b | 2021-11-24 19:29:38 -0700 | [diff] [blame] | 158 | ALOGV("GpuStatsAppInfo has reached maximum size. Removing old stats to make room."); |
| 159 | purgeOldDriverStats(); |
Yiwei Zhang | 8c5e3bd | 2019-05-09 14:34:19 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Yiwei Zhang | f40fb10 | 2019-02-27 21:05:06 -0800 | [diff] [blame] | 162 | GpuStatsAppInfo appInfo; |
| 163 | addLoadingTime(driver, driverLoadingTime, &appInfo); |
| 164 | appInfo.appPackageName = appPackageName; |
| 165 | appInfo.driverVersionCode = driverVersionCode; |
Solti | 54161bf | 2023-11-09 21:09:53 +0000 | [diff] [blame] | 166 | appInfo.angleInUse = |
| 167 | driver == GpuStatsInfo::Driver::ANGLE || driverPackageName == "angle"; |
Tim Van Patten | a9ad69b | 2021-11-24 19:29:38 -0700 | [diff] [blame] | 168 | appInfo.lastAccessTime = std::chrono::system_clock::now(); |
Yiwei Zhang | f40fb10 | 2019-02-27 21:05:06 -0800 | [diff] [blame] | 169 | mAppStats.insert({appStatsKey, appInfo}); |
Tim Van Patten | a9ad69b | 2021-11-24 19:29:38 -0700 | [diff] [blame] | 170 | } else { |
Solti | 54161bf | 2023-11-09 21:09:53 +0000 | [diff] [blame] | 171 | mAppStats[appStatsKey].angleInUse = |
| 172 | driver == GpuStatsInfo::Driver::ANGLE || driverPackageName == "angle"; |
Tim Van Patten | a9ad69b | 2021-11-24 19:29:38 -0700 | [diff] [blame] | 173 | addLoadingTime(driver, driverLoadingTime, &mAppStats[appStatsKey]); |
| 174 | mAppStats[appStatsKey].lastAccessTime = std::chrono::system_clock::now(); |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 175 | } |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Yiwei Zhang | bcba411 | 2019-07-03 13:39:32 -0700 | [diff] [blame] | 178 | void GpuStats::insertTargetStats(const std::string& appPackageName, |
| 179 | const uint64_t driverVersionCode, const GpuStatsInfo::Stats stats, |
Serdar Kocdemir | b2901c9 | 2022-11-17 00:39:05 +0000 | [diff] [blame] | 180 | const uint64_t value) { |
| 181 | return insertTargetStatsArray(appPackageName, driverVersionCode, stats, &value, 1); |
| 182 | } |
| 183 | |
| 184 | void GpuStats::insertTargetStatsArray(const std::string& appPackageName, |
| 185 | const uint64_t driverVersionCode, const GpuStatsInfo::Stats stats, |
| 186 | const uint64_t* values, const uint32_t valueCount) { |
Yiwei Zhang | bcba411 | 2019-07-03 13:39:32 -0700 | [diff] [blame] | 187 | ATRACE_CALL(); |
| 188 | |
Yiwei Zhang | 8c5e3bd | 2019-05-09 14:34:19 -0700 | [diff] [blame] | 189 | const std::string appStatsKey = appPackageName + std::to_string(driverVersionCode); |
Yiwei Zhang | bcba411 | 2019-07-03 13:39:32 -0700 | [diff] [blame] | 190 | |
| 191 | std::lock_guard<std::mutex> lock(mLock); |
Alec Mouri | 822b10a | 2020-03-17 17:51:27 -0700 | [diff] [blame] | 192 | registerStatsdCallbacksIfNeeded(); |
Serdar Kocdemir | b2901c9 | 2022-11-17 00:39:05 +0000 | [diff] [blame] | 193 | |
| 194 | const auto foundApp = mAppStats.find(appStatsKey); |
| 195 | if (foundApp == mAppStats.end()) { |
Yiwei Zhang | 8c5e3bd | 2019-05-09 14:34:19 -0700 | [diff] [blame] | 196 | return; |
| 197 | } |
| 198 | |
Serdar Kocdemir | b2901c9 | 2022-11-17 00:39:05 +0000 | [diff] [blame] | 199 | GpuStatsAppInfo& targetAppStats = foundApp->second; |
| 200 | |
| 201 | if (stats == GpuStatsInfo::Stats::VULKAN_INSTANCE_EXTENSION |
| 202 | || stats == GpuStatsInfo::Stats::VULKAN_DEVICE_EXTENSION) { |
| 203 | // Handle extension arrays separately as we need to store a unique set of them |
| 204 | // in the stats vector. Storing in std::set<> is not efficient for serialization tasks. |
| 205 | std::vector<int32_t>& targetVec = |
| 206 | (stats == GpuStatsInfo::Stats::VULKAN_INSTANCE_EXTENSION) ? |
| 207 | targetAppStats.vulkanInstanceExtensions : |
| 208 | targetAppStats.vulkanDeviceExtensions; |
| 209 | const bool addAll = (targetVec.size() == 0); |
| 210 | targetVec.reserve(valueCount); |
| 211 | |
| 212 | // Add new extensions into the set |
| 213 | for(uint32_t i = 0; |
| 214 | (i < valueCount) && (targetVec.size() < GpuStatsAppInfo::MAX_NUM_EXTENSIONS); |
| 215 | i++) { |
| 216 | const int32_t extVal = int32_t(values[i] & 0xFFFFFFFF); |
| 217 | if (addAll |
| 218 | || std::find(targetVec.cbegin(), targetVec.cend(), extVal) == targetVec.cend()) { |
| 219 | targetVec.push_back(extVal); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | else { |
| 224 | // Handle other type of stats info events |
| 225 | for(uint32_t i = 0; i < valueCount; i++) { |
| 226 | const uint64_t value = values[i]; |
| 227 | switch (stats) { |
| 228 | case GpuStatsInfo::Stats::CPU_VULKAN_IN_USE: |
| 229 | targetAppStats.cpuVulkanInUse = true; |
| 230 | break; |
| 231 | case GpuStatsInfo::Stats::FALSE_PREROTATION: |
| 232 | targetAppStats.falsePrerotation = true; |
| 233 | break; |
| 234 | case GpuStatsInfo::Stats::GLES_1_IN_USE: |
| 235 | targetAppStats.gles1InUse = true; |
| 236 | break; |
| 237 | case GpuStatsInfo::Stats::CREATED_GLES_CONTEXT: |
| 238 | targetAppStats.createdGlesContext = true; |
| 239 | break; |
| 240 | case GpuStatsInfo::Stats::CREATED_VULKAN_DEVICE: |
| 241 | targetAppStats.createdVulkanDevice = true; |
| 242 | break; |
| 243 | case GpuStatsInfo::Stats::CREATED_VULKAN_API_VERSION: |
| 244 | targetAppStats.vulkanApiVersion = uint32_t(value & 0xffffffff); |
| 245 | break; |
| 246 | case GpuStatsInfo::Stats::CREATED_VULKAN_SWAPCHAIN: |
| 247 | targetAppStats.createdVulkanSwapchain = true; |
| 248 | break; |
| 249 | case GpuStatsInfo::Stats::VULKAN_DEVICE_FEATURES_ENABLED: |
| 250 | // Merge all requested feature bits together for this app |
| 251 | targetAppStats.vulkanDeviceFeaturesEnabled |= value; |
| 252 | break; |
| 253 | default: |
| 254 | break; |
| 255 | } |
| 256 | } |
Yiwei Zhang | bcba411 | 2019-07-03 13:39:32 -0700 | [diff] [blame] | 257 | } |
Yiwei Zhang | 8c5e3bd | 2019-05-09 14:34:19 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Yiwei Zhang | 174a2a0 | 2019-05-06 19:08:31 -0700 | [diff] [blame] | 260 | void GpuStats::interceptSystemDriverStatsLocked() { |
| 261 | // Append cpuVulkanVersion and glesVersion to system driver stats |
| 262 | if (!mGlobalStats.count(0) || mGlobalStats[0].glesVersion) { |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | mGlobalStats[0].cpuVulkanVersion = property_get_int32("ro.cpuvulkan.version", 0); |
| 267 | mGlobalStats[0].glesVersion = property_get_int32("ro.opengles.version", 0); |
| 268 | } |
| 269 | |
Alec Mouri | 822b10a | 2020-03-17 17:51:27 -0700 | [diff] [blame] | 270 | void GpuStats::registerStatsdCallbacksIfNeeded() { |
| 271 | if (!mStatsdRegistered) { |
Tej Singh | 38a4b21 | 2020-03-13 19:04:51 -0700 | [diff] [blame] | 272 | AStatsManager_setPullAtomCallback(android::util::GPU_STATS_GLOBAL_INFO, nullptr, |
| 273 | GpuStats::pullAtomCallback, this); |
| 274 | AStatsManager_setPullAtomCallback(android::util::GPU_STATS_APP_INFO, nullptr, |
| 275 | GpuStats::pullAtomCallback, this); |
Alec Mouri | 822b10a | 2020-03-17 17:51:27 -0700 | [diff] [blame] | 276 | mStatsdRegistered = true; |
| 277 | } |
| 278 | } |
| 279 | |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 280 | void GpuStats::dump(const Vector<String16>& args, std::string* result) { |
| 281 | ATRACE_CALL(); |
| 282 | |
| 283 | if (!result) { |
| 284 | ALOGE("Dump result shouldn't be nullptr."); |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | std::lock_guard<std::mutex> lock(mLock); |
| 289 | bool dumpAll = true; |
| 290 | |
| 291 | std::unordered_set<std::string> argsSet; |
| 292 | for (size_t i = 0; i < args.size(); i++) { |
| 293 | argsSet.insert(String8(args[i]).c_str()); |
| 294 | } |
| 295 | |
| 296 | const bool dumpGlobal = argsSet.count("--global") != 0; |
| 297 | if (dumpGlobal) { |
| 298 | dumpGlobalLocked(result); |
| 299 | dumpAll = false; |
| 300 | } |
| 301 | |
| 302 | const bool dumpApp = argsSet.count("--app") != 0; |
| 303 | if (dumpApp) { |
| 304 | dumpAppLocked(result); |
| 305 | dumpAll = false; |
| 306 | } |
| 307 | |
Yiwei Zhang | fdd7e78 | 2020-01-31 15:59:34 -0800 | [diff] [blame] | 308 | if (dumpAll) { |
| 309 | dumpGlobalLocked(result); |
| 310 | dumpAppLocked(result); |
| 311 | } |
| 312 | |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 313 | if (argsSet.count("--clear")) { |
| 314 | bool clearAll = true; |
| 315 | |
| 316 | if (dumpGlobal) { |
| 317 | mGlobalStats.clear(); |
| 318 | clearAll = false; |
| 319 | } |
| 320 | |
| 321 | if (dumpApp) { |
| 322 | mAppStats.clear(); |
| 323 | clearAll = false; |
| 324 | } |
| 325 | |
| 326 | if (clearAll) { |
| 327 | mGlobalStats.clear(); |
| 328 | mAppStats.clear(); |
| 329 | } |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
| 333 | void GpuStats::dumpGlobalLocked(std::string* result) { |
Yiwei Zhang | 174a2a0 | 2019-05-06 19:08:31 -0700 | [diff] [blame] | 334 | interceptSystemDriverStatsLocked(); |
| 335 | |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 336 | for (const auto& ele : mGlobalStats) { |
Yiwei Zhang | f40fb10 | 2019-02-27 21:05:06 -0800 | [diff] [blame] | 337 | result->append(ele.second.toString()); |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 338 | result->append("\n"); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | void GpuStats::dumpAppLocked(std::string* result) { |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 343 | for (const auto& ele : mAppStats) { |
Yiwei Zhang | f40fb10 | 2019-02-27 21:05:06 -0800 | [diff] [blame] | 344 | result->append(ele.second.toString()); |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 345 | result->append("\n"); |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
Yiwei Zhang | 29f8593 | 2020-02-04 17:14:54 -0800 | [diff] [blame] | 349 | static std::string protoOutputStreamToByteString(android::util::ProtoOutputStream& proto) { |
| 350 | if (!proto.size()) return ""; |
| 351 | |
| 352 | std::string byteString; |
| 353 | sp<android::util::ProtoReader> reader = proto.data(); |
| 354 | while (reader->readBuffer() != nullptr) { |
| 355 | const size_t toRead = reader->currentToRead(); |
| 356 | byteString.append((char*)reader->readBuffer(), toRead); |
| 357 | reader->move(toRead); |
| 358 | } |
| 359 | |
| 360 | if (byteString.size() != proto.size()) return ""; |
| 361 | |
| 362 | return byteString; |
| 363 | } |
| 364 | |
| 365 | static std::string int64VectorToProtoByteString(const std::vector<int64_t>& value) { |
| 366 | if (value.empty()) return ""; |
| 367 | |
| 368 | android::util::ProtoOutputStream proto; |
| 369 | for (const auto& ele : value) { |
| 370 | proto.write(android::util::FIELD_TYPE_INT64 | android::util::FIELD_COUNT_REPEATED | |
| 371 | 1 /* field id */, |
| 372 | (long long)ele); |
| 373 | } |
| 374 | |
| 375 | return protoOutputStreamToByteString(proto); |
| 376 | } |
| 377 | |
| 378 | AStatsManager_PullAtomCallbackReturn GpuStats::pullAppInfoAtom(AStatsEventList* data) { |
| 379 | ATRACE_CALL(); |
| 380 | |
| 381 | std::lock_guard<std::mutex> lock(mLock); |
| 382 | |
| 383 | if (data) { |
| 384 | for (const auto& ele : mAppStats) { |
Salud Lemus | 30108fa | 2020-07-11 00:02:19 +0000 | [diff] [blame] | 385 | std::string glDriverBytes = int64VectorToProtoByteString( |
| 386 | ele.second.glDriverLoadingTime); |
| 387 | std::string vkDriverBytes = int64VectorToProtoByteString( |
| 388 | ele.second.vkDriverLoadingTime); |
| 389 | std::string angleDriverBytes = int64VectorToProtoByteString( |
| 390 | ele.second.angleDriverLoadingTime); |
Yiwei Zhang | 29f8593 | 2020-02-04 17:14:54 -0800 | [diff] [blame] | 391 | |
Salud Lemus | 30108fa | 2020-07-11 00:02:19 +0000 | [diff] [blame] | 392 | android::util::addAStatsEvent( |
| 393 | data, |
| 394 | android::util::GPU_STATS_APP_INFO, |
| 395 | ele.second.appPackageName.c_str(), |
| 396 | ele.second.driverVersionCode, |
| 397 | android::util::BytesField(glDriverBytes.c_str(), |
| 398 | glDriverBytes.length()), |
| 399 | android::util::BytesField(vkDriverBytes.c_str(), |
| 400 | vkDriverBytes.length()), |
| 401 | android::util::BytesField(angleDriverBytes.c_str(), |
| 402 | angleDriverBytes.length()), |
| 403 | ele.second.cpuVulkanInUse, |
| 404 | ele.second.falsePrerotation, |
Tim Van Patten | a9ad69b | 2021-11-24 19:29:38 -0700 | [diff] [blame] | 405 | ele.second.gles1InUse, |
Serdar Kocdemir | b2901c9 | 2022-11-17 00:39:05 +0000 | [diff] [blame] | 406 | ele.second.angleInUse, |
| 407 | ele.second.createdGlesContext, |
| 408 | ele.second.createdVulkanDevice, |
| 409 | ele.second.createdVulkanSwapchain, |
| 410 | ele.second.vulkanApiVersion, |
| 411 | ele.second.vulkanDeviceFeaturesEnabled, |
| 412 | ele.second.vulkanInstanceExtensions, |
| 413 | ele.second.vulkanDeviceExtensions); |
Yiwei Zhang | 29f8593 | 2020-02-04 17:14:54 -0800 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
| 417 | mAppStats.clear(); |
| 418 | |
| 419 | return AStatsManager_PULL_SUCCESS; |
| 420 | } |
| 421 | |
Yiwei Zhang | b59a127 | 2020-02-04 14:58:01 -0800 | [diff] [blame] | 422 | AStatsManager_PullAtomCallbackReturn GpuStats::pullGlobalInfoAtom(AStatsEventList* data) { |
| 423 | ATRACE_CALL(); |
| 424 | |
| 425 | std::lock_guard<std::mutex> lock(mLock); |
| 426 | // flush cpuVulkanVersion and glesVersion to builtin driver stats |
| 427 | interceptSystemDriverStatsLocked(); |
| 428 | |
| 429 | if (data) { |
| 430 | for (const auto& ele : mGlobalStats) { |
Salud Lemus | 30108fa | 2020-07-11 00:02:19 +0000 | [diff] [blame] | 431 | android::util::addAStatsEvent( |
| 432 | data, |
| 433 | android::util::GPU_STATS_GLOBAL_INFO, |
| 434 | ele.second.driverPackageName.c_str(), |
| 435 | ele.second.driverVersionName.c_str(), |
| 436 | ele.second.driverVersionCode, |
| 437 | ele.second.driverBuildTime, |
| 438 | ele.second.glLoadingCount, |
| 439 | ele.second.glLoadingFailureCount, |
| 440 | ele.second.vkLoadingCount, |
| 441 | ele.second.vkLoadingFailureCount, |
| 442 | ele.second.vulkanVersion, |
| 443 | ele.second.cpuVulkanVersion, |
| 444 | ele.second.glesVersion, |
| 445 | ele.second.angleLoadingCount, |
| 446 | ele.second.angleLoadingFailureCount); |
Yiwei Zhang | b59a127 | 2020-02-04 14:58:01 -0800 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
| 450 | mGlobalStats.clear(); |
| 451 | |
| 452 | return AStatsManager_PULL_SUCCESS; |
| 453 | } |
| 454 | |
| 455 | AStatsManager_PullAtomCallbackReturn GpuStats::pullAtomCallback(int32_t atomTag, |
| 456 | AStatsEventList* data, |
| 457 | void* cookie) { |
| 458 | ATRACE_CALL(); |
| 459 | |
| 460 | GpuStats* pGpuStats = reinterpret_cast<GpuStats*>(cookie); |
| 461 | if (atomTag == android::util::GPU_STATS_GLOBAL_INFO) { |
| 462 | return pGpuStats->pullGlobalInfoAtom(data); |
Yiwei Zhang | 29f8593 | 2020-02-04 17:14:54 -0800 | [diff] [blame] | 463 | } else if (atomTag == android::util::GPU_STATS_APP_INFO) { |
| 464 | return pGpuStats->pullAppInfoAtom(data); |
Yiwei Zhang | b59a127 | 2020-02-04 14:58:01 -0800 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | return AStatsManager_PULL_SKIP; |
| 468 | } |
| 469 | |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 470 | } // namespace android |