blob: 5174c8617134556322060ace2cfabf5d1e7b5415 [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
20#include "GpuStats.h"
21
Yiwei Zhang174a2a02019-05-06 19:08:31 -070022#include <cutils/properties.h>
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080023#include <log/log.h>
24#include <utils/Trace.h>
25
Yiwei Zhang174a2a02019-05-06 19:08:31 -070026#include <unordered_set>
27
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080028namespace android {
29
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080030static bool addLoadingCount(GraphicsEnv::Driver driver, bool isDriverLoaded,
Yiwei Zhangf40fb102019-02-27 21:05:06 -080031 GpuStatsGlobalInfo* const outGlobalInfo) {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080032 switch (driver) {
33 case GraphicsEnv::Driver::GL:
34 case GraphicsEnv::Driver::GL_UPDATED:
Yiwei Zhangf40fb102019-02-27 21:05:06 -080035 outGlobalInfo->glLoadingCount++;
36 if (!isDriverLoaded) outGlobalInfo->glLoadingFailureCount++;
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080037 break;
38 case GraphicsEnv::Driver::VULKAN:
39 case GraphicsEnv::Driver::VULKAN_UPDATED:
Yiwei Zhangf40fb102019-02-27 21:05:06 -080040 outGlobalInfo->vkLoadingCount++;
41 if (!isDriverLoaded) outGlobalInfo->vkLoadingFailureCount++;
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080042 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
52static void addLoadingTime(GraphicsEnv::Driver driver, int64_t driverLoadingTime,
Yiwei Zhangf40fb102019-02-27 21:05:06 -080053 GpuStatsAppInfo* const outAppInfo) {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080054 switch (driver) {
55 case GraphicsEnv::Driver::GL:
56 case GraphicsEnv::Driver::GL_UPDATED:
Yiwei Zhangf2d84132019-03-06 11:32:50 -080057 if (outAppInfo->glDriverLoadingTime.size() >= GpuStats::MAX_NUM_LOADING_TIMES) break;
Yiwei Zhangf40fb102019-02-27 21:05:06 -080058 outAppInfo->glDriverLoadingTime.emplace_back(driverLoadingTime);
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080059 break;
60 case GraphicsEnv::Driver::VULKAN:
61 case GraphicsEnv::Driver::VULKAN_UPDATED:
Yiwei Zhangf2d84132019-03-06 11:32:50 -080062 if (outAppInfo->vkDriverLoadingTime.size() >= GpuStats::MAX_NUM_LOADING_TIMES) break;
Yiwei Zhangf40fb102019-02-27 21:05:06 -080063 outAppInfo->vkDriverLoadingTime.emplace_back(driverLoadingTime);
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080064 break;
65 default:
66 break;
67 }
68}
69
70void GpuStats::insert(const std::string& driverPackageName, const std::string& driverVersionName,
71 uint64_t driverVersionCode, int64_t driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -070072 const std::string& appPackageName, const int32_t vulkanVersion,
73 GraphicsEnv::Driver driver, bool isDriverLoaded, int64_t driverLoadingTime) {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080074 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 Zhang794d2952019-05-06 17:43:59 -070083 "\tvulkanVersion[%d]\n"
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080084 "\tdriver[%d]\n"
85 "\tisDriverLoaded[%d]\n"
86 "\tdriverLoadingTime[%" PRId64 "]",
87 driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -070088 appPackageName.c_str(), vulkanVersion, static_cast<int32_t>(driver), isDriverLoaded,
89 driverLoadingTime);
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080090
91 if (!mGlobalStats.count(driverVersionCode)) {
Yiwei Zhangf40fb102019-02-27 21:05:06 -080092 GpuStatsGlobalInfo globalInfo;
93 if (!addLoadingCount(driver, isDriverLoaded, &globalInfo)) {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080094 return;
95 }
Yiwei Zhangf40fb102019-02-27 21:05:06 -080096 globalInfo.driverPackageName = driverPackageName;
97 globalInfo.driverVersionName = driverVersionName;
98 globalInfo.driverVersionCode = driverVersionCode;
99 globalInfo.driverBuildTime = driverBuildTime;
Yiwei Zhang794d2952019-05-06 17:43:59 -0700100 globalInfo.vulkanVersion = vulkanVersion;
Yiwei Zhangf40fb102019-02-27 21:05:06 -0800101 mGlobalStats.insert({driverVersionCode, globalInfo});
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800102 } else if (!addLoadingCount(driver, isDriverLoaded, &mGlobalStats[driverVersionCode])) {
103 return;
104 }
105
106 if (mAppStats.size() >= MAX_NUM_APP_RECORDS) {
Yiwei Zhangf40fb102019-02-27 21:05:06 -0800107 ALOGV("GpuStatsAppInfo has reached maximum size. Ignore new stats.");
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800108 return;
109 }
110
111 const std::string appStatsKey = appPackageName + std::to_string(driverVersionCode);
112 if (!mAppStats.count(appStatsKey)) {
Yiwei Zhangf40fb102019-02-27 21:05:06 -0800113 GpuStatsAppInfo appInfo;
114 addLoadingTime(driver, driverLoadingTime, &appInfo);
115 appInfo.appPackageName = appPackageName;
116 appInfo.driverVersionCode = driverVersionCode;
117 mAppStats.insert({appStatsKey, appInfo});
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800118 return;
119 }
120
121 addLoadingTime(driver, driverLoadingTime, &mAppStats[appStatsKey]);
122}
123
Yiwei Zhang174a2a02019-05-06 19:08:31 -0700124void 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 Zhang2d4c1882019-02-24 22:28:08 -0800134void 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
189void GpuStats::dumpGlobalLocked(std::string* result) {
Yiwei Zhang174a2a02019-05-06 19:08:31 -0700190 interceptSystemDriverStatsLocked();
191
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800192 for (const auto& ele : mGlobalStats) {
Yiwei Zhangf40fb102019-02-27 21:05:06 -0800193 result->append(ele.second.toString());
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800194 result->append("\n");
195 }
196}
197
198void GpuStats::dumpAppLocked(std::string* result) {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800199 for (const auto& ele : mAppStats) {
Yiwei Zhangf40fb102019-02-27 21:05:06 -0800200 result->append(ele.second.toString());
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800201 result->append("\n");
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800202 }
203}
204
Yiwei Zhangbaea97f2019-02-27 16:35:37 -0800205void 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 Zhang174a2a02019-05-06 19:08:31 -0700212 interceptSystemDriverStatsLocked();
213
Yiwei Zhangbaea97f2019-02-27 16:35:37 -0800214 for (const auto& ele : mGlobalStats) {
215 outStats->emplace_back(ele.second);
216 }
217
218 mGlobalStats.clear();
219}
220
Yiwei Zhang178e0672019-03-04 14:17:12 -0800221void 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 Zhang2d4c1882019-02-24 22:28:08 -0800235} // namespace android