blob: d033453dd748ef9b4b63c5f87aa0f71734b5b613 [file] [log] [blame]
Yiwei Zhang2d4c1882019-02-24 22:28:08 -08001/*
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 Zhang29f85932020-02-04 17:14:54 -080020#include "gpustats/GpuStats.h"
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080021
Yiwei Zhang29f85932020-02-04 17:14:54 -080022#include <android/util/ProtoOutputStream.h>
Yiwei Zhang174a2a02019-05-06 19:08:31 -070023#include <cutils/properties.h>
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080024#include <log/log.h>
Yiwei Zhangb59a1272020-02-04 14:58:01 -080025#include <stats_event.h>
26#include <statslog.h>
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080027#include <utils/Trace.h>
28
Yiwei Zhang174a2a02019-05-06 19:08:31 -070029#include <unordered_set>
30
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080031namespace android {
32
Yiwei Zhangb59a1272020-02-04 14:58:01 -080033GpuStats::~GpuStats() {
Alec Mouri822b10a2020-03-17 17:51:27 -070034 if (mStatsdRegistered) {
Tej Singh38a4b212020-03-13 19:04:51 -070035 AStatsManager_clearPullAtomCallback(android::util::GPU_STATS_GLOBAL_INFO);
36 AStatsManager_clearPullAtomCallback(android::util::GPU_STATS_APP_INFO);
Alec Mouri822b10a2020-03-17 17:51:27 -070037 }
Yiwei Zhangb59a1272020-02-04 14:58:01 -080038}
39
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -070040static void addLoadingCount(GpuStatsInfo::Driver driver, bool isDriverLoaded,
Yiwei Zhangf40fb102019-02-27 21:05:06 -080041 GpuStatsGlobalInfo* const outGlobalInfo) {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080042 switch (driver) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -070043 case GpuStatsInfo::Driver::GL:
44 case GpuStatsInfo::Driver::GL_UPDATED:
Yiwei Zhangf40fb102019-02-27 21:05:06 -080045 outGlobalInfo->glLoadingCount++;
46 if (!isDriverLoaded) outGlobalInfo->glLoadingFailureCount++;
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080047 break;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -070048 case GpuStatsInfo::Driver::VULKAN:
49 case GpuStatsInfo::Driver::VULKAN_UPDATED:
Yiwei Zhangf40fb102019-02-27 21:05:06 -080050 outGlobalInfo->vkLoadingCount++;
51 if (!isDriverLoaded) outGlobalInfo->vkLoadingFailureCount++;
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080052 break;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -070053 case GpuStatsInfo::Driver::ANGLE:
Yiwei Zhang8e7c4b62019-05-08 15:57:59 -070054 outGlobalInfo->angleLoadingCount++;
55 if (!isDriverLoaded) outGlobalInfo->angleLoadingFailureCount++;
56 break;
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080057 default:
Yiwei Zhang8e7c4b62019-05-08 15:57:59 -070058 break;
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080059 }
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080060}
61
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -070062static void addLoadingTime(GpuStatsInfo::Driver driver, int64_t driverLoadingTime,
Yiwei Zhangf40fb102019-02-27 21:05:06 -080063 GpuStatsAppInfo* const outAppInfo) {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080064 switch (driver) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -070065 case GpuStatsInfo::Driver::GL:
66 case GpuStatsInfo::Driver::GL_UPDATED:
Yiwei Zhang8e7c4b62019-05-08 15:57:59 -070067 if (outAppInfo->glDriverLoadingTime.size() < GpuStats::MAX_NUM_LOADING_TIMES) {
68 outAppInfo->glDriverLoadingTime.emplace_back(driverLoadingTime);
69 }
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080070 break;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -070071 case GpuStatsInfo::Driver::VULKAN:
72 case GpuStatsInfo::Driver::VULKAN_UPDATED:
Yiwei Zhang8e7c4b62019-05-08 15:57:59 -070073 if (outAppInfo->vkDriverLoadingTime.size() < GpuStats::MAX_NUM_LOADING_TIMES) {
74 outAppInfo->vkDriverLoadingTime.emplace_back(driverLoadingTime);
75 }
76 break;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -070077 case GpuStatsInfo::Driver::ANGLE:
Yiwei Zhang8e7c4b62019-05-08 15:57:59 -070078 if (outAppInfo->angleDriverLoadingTime.size() < GpuStats::MAX_NUM_LOADING_TIMES) {
79 outAppInfo->angleDriverLoadingTime.emplace_back(driverLoadingTime);
80 }
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080081 break;
82 default:
83 break;
84 }
85}
86
Tim Van Pattena9ad69b2021-11-24 19:29:38 -070087void 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 Zhangfdd7e782020-01-31 15:59:34 -0800119void 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 Zhang2d4c1882019-02-24 22:28:08 -0800124 ATRACE_CALL();
125
126 std::lock_guard<std::mutex> lock(mLock);
Alec Mouri822b10a2020-03-17 17:51:27 -0700127 registerStatsdCallbacksIfNeeded();
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800128 ALOGV("Received:\n"
129 "\tdriverPackageName[%s]\n"
130 "\tdriverVersionName[%s]\n"
131 "\tdriverVersionCode[%" PRIu64 "]\n"
132 "\tdriverBuildTime[%" PRId64 "]\n"
133 "\tappPackageName[%s]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700134 "\tvulkanVersion[%d]\n"
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800135 "\tdriver[%d]\n"
136 "\tisDriverLoaded[%d]\n"
137 "\tdriverLoadingTime[%" PRId64 "]",
138 driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700139 appPackageName.c_str(), vulkanVersion, static_cast<int32_t>(driver), isDriverLoaded,
140 driverLoadingTime);
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800141
142 if (!mGlobalStats.count(driverVersionCode)) {
Yiwei Zhangf40fb102019-02-27 21:05:06 -0800143 GpuStatsGlobalInfo globalInfo;
Yiwei Zhang8e7c4b62019-05-08 15:57:59 -0700144 addLoadingCount(driver, isDriverLoaded, &globalInfo);
Yiwei Zhangf40fb102019-02-27 21:05:06 -0800145 globalInfo.driverPackageName = driverPackageName;
146 globalInfo.driverVersionName = driverVersionName;
147 globalInfo.driverVersionCode = driverVersionCode;
148 globalInfo.driverBuildTime = driverBuildTime;
Yiwei Zhang794d2952019-05-06 17:43:59 -0700149 globalInfo.vulkanVersion = vulkanVersion;
Yiwei Zhangf40fb102019-02-27 21:05:06 -0800150 mGlobalStats.insert({driverVersionCode, globalInfo});
Yiwei Zhang8e7c4b62019-05-08 15:57:59 -0700151 } else {
152 addLoadingCount(driver, isDriverLoaded, &mGlobalStats[driverVersionCode]);
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800153 }
154
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800155 const std::string appStatsKey = appPackageName + std::to_string(driverVersionCode);
156 if (!mAppStats.count(appStatsKey)) {
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700157 if (mAppStats.size() >= MAX_NUM_APP_RECORDS) {
Tim Van Pattena9ad69b2021-11-24 19:29:38 -0700158 ALOGV("GpuStatsAppInfo has reached maximum size. Removing old stats to make room.");
159 purgeOldDriverStats();
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700160 }
161
Yiwei Zhangf40fb102019-02-27 21:05:06 -0800162 GpuStatsAppInfo appInfo;
163 addLoadingTime(driver, driverLoadingTime, &appInfo);
164 appInfo.appPackageName = appPackageName;
165 appInfo.driverVersionCode = driverVersionCode;
Tim Van Pattena9ad69b2021-11-24 19:29:38 -0700166 appInfo.angleInUse = driverPackageName == "angle";
167 appInfo.lastAccessTime = std::chrono::system_clock::now();
Yiwei Zhangf40fb102019-02-27 21:05:06 -0800168 mAppStats.insert({appStatsKey, appInfo});
Tim Van Pattena9ad69b2021-11-24 19:29:38 -0700169 } else {
170 mAppStats[appStatsKey].angleInUse = driverPackageName == "angle";
171 addLoadingTime(driver, driverLoadingTime, &mAppStats[appStatsKey]);
172 mAppStats[appStatsKey].lastAccessTime = std::chrono::system_clock::now();
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800173 }
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800174}
175
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700176void GpuStats::insertTargetStats(const std::string& appPackageName,
177 const uint64_t driverVersionCode, const GpuStatsInfo::Stats stats,
178 const uint64_t /*value*/) {
179 ATRACE_CALL();
180
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700181 const std::string appStatsKey = appPackageName + std::to_string(driverVersionCode);
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700182
183 std::lock_guard<std::mutex> lock(mLock);
Alec Mouri822b10a2020-03-17 17:51:27 -0700184 registerStatsdCallbacksIfNeeded();
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700185 if (!mAppStats.count(appStatsKey)) {
186 return;
187 }
188
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700189 switch (stats) {
190 case GpuStatsInfo::Stats::CPU_VULKAN_IN_USE:
191 mAppStats[appStatsKey].cpuVulkanInUse = true;
192 break;
Yiwei Zhang69395cd2019-07-03 16:55:39 -0700193 case GpuStatsInfo::Stats::FALSE_PREROTATION:
194 mAppStats[appStatsKey].falsePrerotation = true;
195 break;
Yiwei Zhang011538f2019-12-20 14:37:21 -0800196 case GpuStatsInfo::Stats::GLES_1_IN_USE:
197 mAppStats[appStatsKey].gles1InUse = true;
198 break;
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700199 default:
200 break;
201 }
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700202}
203
Yiwei Zhang174a2a02019-05-06 19:08:31 -0700204void GpuStats::interceptSystemDriverStatsLocked() {
205 // Append cpuVulkanVersion and glesVersion to system driver stats
206 if (!mGlobalStats.count(0) || mGlobalStats[0].glesVersion) {
207 return;
208 }
209
210 mGlobalStats[0].cpuVulkanVersion = property_get_int32("ro.cpuvulkan.version", 0);
211 mGlobalStats[0].glesVersion = property_get_int32("ro.opengles.version", 0);
212}
213
Alec Mouri822b10a2020-03-17 17:51:27 -0700214void GpuStats::registerStatsdCallbacksIfNeeded() {
215 if (!mStatsdRegistered) {
Tej Singh38a4b212020-03-13 19:04:51 -0700216 AStatsManager_setPullAtomCallback(android::util::GPU_STATS_GLOBAL_INFO, nullptr,
217 GpuStats::pullAtomCallback, this);
218 AStatsManager_setPullAtomCallback(android::util::GPU_STATS_APP_INFO, nullptr,
219 GpuStats::pullAtomCallback, this);
Alec Mouri822b10a2020-03-17 17:51:27 -0700220 mStatsdRegistered = true;
221 }
222}
223
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800224void GpuStats::dump(const Vector<String16>& args, std::string* result) {
225 ATRACE_CALL();
226
227 if (!result) {
228 ALOGE("Dump result shouldn't be nullptr.");
229 return;
230 }
231
232 std::lock_guard<std::mutex> lock(mLock);
233 bool dumpAll = true;
234
235 std::unordered_set<std::string> argsSet;
236 for (size_t i = 0; i < args.size(); i++) {
237 argsSet.insert(String8(args[i]).c_str());
238 }
239
240 const bool dumpGlobal = argsSet.count("--global") != 0;
241 if (dumpGlobal) {
242 dumpGlobalLocked(result);
243 dumpAll = false;
244 }
245
246 const bool dumpApp = argsSet.count("--app") != 0;
247 if (dumpApp) {
248 dumpAppLocked(result);
249 dumpAll = false;
250 }
251
Yiwei Zhangfdd7e782020-01-31 15:59:34 -0800252 if (dumpAll) {
253 dumpGlobalLocked(result);
254 dumpAppLocked(result);
255 }
256
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800257 if (argsSet.count("--clear")) {
258 bool clearAll = true;
259
260 if (dumpGlobal) {
261 mGlobalStats.clear();
262 clearAll = false;
263 }
264
265 if (dumpApp) {
266 mAppStats.clear();
267 clearAll = false;
268 }
269
270 if (clearAll) {
271 mGlobalStats.clear();
272 mAppStats.clear();
273 }
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800274 }
275}
276
277void GpuStats::dumpGlobalLocked(std::string* result) {
Yiwei Zhang174a2a02019-05-06 19:08:31 -0700278 interceptSystemDriverStatsLocked();
279
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800280 for (const auto& ele : mGlobalStats) {
Yiwei Zhangf40fb102019-02-27 21:05:06 -0800281 result->append(ele.second.toString());
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800282 result->append("\n");
283 }
284}
285
286void GpuStats::dumpAppLocked(std::string* result) {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800287 for (const auto& ele : mAppStats) {
Yiwei Zhangf40fb102019-02-27 21:05:06 -0800288 result->append(ele.second.toString());
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800289 result->append("\n");
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800290 }
291}
292
Yiwei Zhang29f85932020-02-04 17:14:54 -0800293static std::string protoOutputStreamToByteString(android::util::ProtoOutputStream& proto) {
294 if (!proto.size()) return "";
295
296 std::string byteString;
297 sp<android::util::ProtoReader> reader = proto.data();
298 while (reader->readBuffer() != nullptr) {
299 const size_t toRead = reader->currentToRead();
300 byteString.append((char*)reader->readBuffer(), toRead);
301 reader->move(toRead);
302 }
303
304 if (byteString.size() != proto.size()) return "";
305
306 return byteString;
307}
308
309static std::string int64VectorToProtoByteString(const std::vector<int64_t>& value) {
310 if (value.empty()) return "";
311
312 android::util::ProtoOutputStream proto;
313 for (const auto& ele : value) {
314 proto.write(android::util::FIELD_TYPE_INT64 | android::util::FIELD_COUNT_REPEATED |
315 1 /* field id */,
316 (long long)ele);
317 }
318
319 return protoOutputStreamToByteString(proto);
320}
321
322AStatsManager_PullAtomCallbackReturn GpuStats::pullAppInfoAtom(AStatsEventList* data) {
323 ATRACE_CALL();
324
325 std::lock_guard<std::mutex> lock(mLock);
326
327 if (data) {
328 for (const auto& ele : mAppStats) {
Salud Lemus30108fa2020-07-11 00:02:19 +0000329 std::string glDriverBytes = int64VectorToProtoByteString(
330 ele.second.glDriverLoadingTime);
331 std::string vkDriverBytes = int64VectorToProtoByteString(
332 ele.second.vkDriverLoadingTime);
333 std::string angleDriverBytes = int64VectorToProtoByteString(
334 ele.second.angleDriverLoadingTime);
Yiwei Zhang29f85932020-02-04 17:14:54 -0800335
Salud Lemus30108fa2020-07-11 00:02:19 +0000336 android::util::addAStatsEvent(
337 data,
338 android::util::GPU_STATS_APP_INFO,
339 ele.second.appPackageName.c_str(),
340 ele.second.driverVersionCode,
341 android::util::BytesField(glDriverBytes.c_str(),
342 glDriverBytes.length()),
343 android::util::BytesField(vkDriverBytes.c_str(),
344 vkDriverBytes.length()),
345 android::util::BytesField(angleDriverBytes.c_str(),
346 angleDriverBytes.length()),
347 ele.second.cpuVulkanInUse,
348 ele.second.falsePrerotation,
Tim Van Pattena9ad69b2021-11-24 19:29:38 -0700349 ele.second.gles1InUse,
350 ele.second.angleInUse);
Yiwei Zhang29f85932020-02-04 17:14:54 -0800351 }
352 }
353
354 mAppStats.clear();
355
356 return AStatsManager_PULL_SUCCESS;
357}
358
Yiwei Zhangb59a1272020-02-04 14:58:01 -0800359AStatsManager_PullAtomCallbackReturn GpuStats::pullGlobalInfoAtom(AStatsEventList* data) {
360 ATRACE_CALL();
361
362 std::lock_guard<std::mutex> lock(mLock);
363 // flush cpuVulkanVersion and glesVersion to builtin driver stats
364 interceptSystemDriverStatsLocked();
365
366 if (data) {
367 for (const auto& ele : mGlobalStats) {
Salud Lemus30108fa2020-07-11 00:02:19 +0000368 android::util::addAStatsEvent(
369 data,
370 android::util::GPU_STATS_GLOBAL_INFO,
371 ele.second.driverPackageName.c_str(),
372 ele.second.driverVersionName.c_str(),
373 ele.second.driverVersionCode,
374 ele.second.driverBuildTime,
375 ele.second.glLoadingCount,
376 ele.second.glLoadingFailureCount,
377 ele.second.vkLoadingCount,
378 ele.second.vkLoadingFailureCount,
379 ele.second.vulkanVersion,
380 ele.second.cpuVulkanVersion,
381 ele.second.glesVersion,
382 ele.second.angleLoadingCount,
383 ele.second.angleLoadingFailureCount);
Yiwei Zhangb59a1272020-02-04 14:58:01 -0800384 }
385 }
386
387 mGlobalStats.clear();
388
389 return AStatsManager_PULL_SUCCESS;
390}
391
392AStatsManager_PullAtomCallbackReturn GpuStats::pullAtomCallback(int32_t atomTag,
393 AStatsEventList* data,
394 void* cookie) {
395 ATRACE_CALL();
396
397 GpuStats* pGpuStats = reinterpret_cast<GpuStats*>(cookie);
398 if (atomTag == android::util::GPU_STATS_GLOBAL_INFO) {
399 return pGpuStats->pullGlobalInfoAtom(data);
Yiwei Zhang29f85932020-02-04 17:14:54 -0800400 } else if (atomTag == android::util::GPU_STATS_APP_INFO) {
401 return pGpuStats->pullAppInfoAtom(data);
Yiwei Zhangb59a1272020-02-04 14:58:01 -0800402 }
403
404 return AStatsManager_PULL_SKIP;
405}
406
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800407} // namespace android