blob: 423c89f7979ab9d2dc5e3580b8d5adfd519aedd7 [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 Zhang27ab3ac2019-07-02 18:10:55 -070030static void addLoadingCount(GpuStatsInfo::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) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -070033 case GpuStatsInfo::Driver::GL:
34 case GpuStatsInfo::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;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -070038 case GpuStatsInfo::Driver::VULKAN:
39 case GpuStatsInfo::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;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -070043 case GpuStatsInfo::Driver::ANGLE:
Yiwei Zhang8e7c4b62019-05-08 15:57:59 -070044 outGlobalInfo->angleLoadingCount++;
45 if (!isDriverLoaded) outGlobalInfo->angleLoadingFailureCount++;
46 break;
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080047 default:
Yiwei Zhang8e7c4b62019-05-08 15:57:59 -070048 break;
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080049 }
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080050}
51
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -070052static void addLoadingTime(GpuStatsInfo::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) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -070055 case GpuStatsInfo::Driver::GL:
56 case GpuStatsInfo::Driver::GL_UPDATED:
Yiwei Zhang8e7c4b62019-05-08 15:57:59 -070057 if (outAppInfo->glDriverLoadingTime.size() < GpuStats::MAX_NUM_LOADING_TIMES) {
58 outAppInfo->glDriverLoadingTime.emplace_back(driverLoadingTime);
59 }
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080060 break;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -070061 case GpuStatsInfo::Driver::VULKAN:
62 case GpuStatsInfo::Driver::VULKAN_UPDATED:
Yiwei Zhang8e7c4b62019-05-08 15:57:59 -070063 if (outAppInfo->vkDriverLoadingTime.size() < GpuStats::MAX_NUM_LOADING_TIMES) {
64 outAppInfo->vkDriverLoadingTime.emplace_back(driverLoadingTime);
65 }
66 break;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -070067 case GpuStatsInfo::Driver::ANGLE:
Yiwei Zhang8e7c4b62019-05-08 15:57:59 -070068 if (outAppInfo->angleDriverLoadingTime.size() < GpuStats::MAX_NUM_LOADING_TIMES) {
69 outAppInfo->angleDriverLoadingTime.emplace_back(driverLoadingTime);
70 }
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080071 break;
72 default:
73 break;
74 }
75}
76
77void GpuStats::insert(const std::string& driverPackageName, const std::string& driverVersionName,
78 uint64_t driverVersionCode, int64_t driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -070079 const std::string& appPackageName, const int32_t vulkanVersion,
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -070080 GpuStatsInfo::Driver driver, bool isDriverLoaded, int64_t driverLoadingTime) {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080081 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 Zhang794d2952019-05-06 17:43:59 -070090 "\tvulkanVersion[%d]\n"
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080091 "\tdriver[%d]\n"
92 "\tisDriverLoaded[%d]\n"
93 "\tdriverLoadingTime[%" PRId64 "]",
94 driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -070095 appPackageName.c_str(), vulkanVersion, static_cast<int32_t>(driver), isDriverLoaded,
96 driverLoadingTime);
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080097
98 if (!mGlobalStats.count(driverVersionCode)) {
Yiwei Zhangf40fb102019-02-27 21:05:06 -080099 GpuStatsGlobalInfo globalInfo;
Yiwei Zhang8e7c4b62019-05-08 15:57:59 -0700100 addLoadingCount(driver, isDriverLoaded, &globalInfo);
Yiwei Zhangf40fb102019-02-27 21:05:06 -0800101 globalInfo.driverPackageName = driverPackageName;
102 globalInfo.driverVersionName = driverVersionName;
103 globalInfo.driverVersionCode = driverVersionCode;
104 globalInfo.driverBuildTime = driverBuildTime;
Yiwei Zhang794d2952019-05-06 17:43:59 -0700105 globalInfo.vulkanVersion = vulkanVersion;
Yiwei Zhangf40fb102019-02-27 21:05:06 -0800106 mGlobalStats.insert({driverVersionCode, globalInfo});
Yiwei Zhang8e7c4b62019-05-08 15:57:59 -0700107 } else {
108 addLoadingCount(driver, isDriverLoaded, &mGlobalStats[driverVersionCode]);
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800109 }
110
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800111 const std::string appStatsKey = appPackageName + std::to_string(driverVersionCode);
112 if (!mAppStats.count(appStatsKey)) {
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700113 if (mAppStats.size() >= MAX_NUM_APP_RECORDS) {
114 ALOGV("GpuStatsAppInfo has reached maximum size. Ignore new stats.");
115 return;
116 }
117
Yiwei Zhangf40fb102019-02-27 21:05:06 -0800118 GpuStatsAppInfo appInfo;
119 addLoadingTime(driver, driverLoadingTime, &appInfo);
120 appInfo.appPackageName = appPackageName;
121 appInfo.driverVersionCode = driverVersionCode;
122 mAppStats.insert({appStatsKey, appInfo});
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800123 return;
124 }
125
126 addLoadingTime(driver, driverLoadingTime, &mAppStats[appStatsKey]);
127}
128
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700129void GpuStats::insertTargetStats(const std::string& appPackageName,
130 const uint64_t driverVersionCode, const GpuStatsInfo::Stats stats,
131 const uint64_t /*value*/) {
132 ATRACE_CALL();
133
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700134 const std::string appStatsKey = appPackageName + std::to_string(driverVersionCode);
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700135
136 std::lock_guard<std::mutex> lock(mLock);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700137 if (!mAppStats.count(appStatsKey)) {
138 return;
139 }
140
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700141 switch (stats) {
142 case GpuStatsInfo::Stats::CPU_VULKAN_IN_USE:
143 mAppStats[appStatsKey].cpuVulkanInUse = true;
144 break;
145 default:
146 break;
147 }
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700148}
149
Yiwei Zhang174a2a02019-05-06 19:08:31 -0700150void GpuStats::interceptSystemDriverStatsLocked() {
151 // Append cpuVulkanVersion and glesVersion to system driver stats
152 if (!mGlobalStats.count(0) || mGlobalStats[0].glesVersion) {
153 return;
154 }
155
156 mGlobalStats[0].cpuVulkanVersion = property_get_int32("ro.cpuvulkan.version", 0);
157 mGlobalStats[0].glesVersion = property_get_int32("ro.opengles.version", 0);
158}
159
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800160void GpuStats::dump(const Vector<String16>& args, std::string* result) {
161 ATRACE_CALL();
162
163 if (!result) {
164 ALOGE("Dump result shouldn't be nullptr.");
165 return;
166 }
167
168 std::lock_guard<std::mutex> lock(mLock);
169 bool dumpAll = true;
170
171 std::unordered_set<std::string> argsSet;
172 for (size_t i = 0; i < args.size(); i++) {
173 argsSet.insert(String8(args[i]).c_str());
174 }
175
176 const bool dumpGlobal = argsSet.count("--global") != 0;
177 if (dumpGlobal) {
178 dumpGlobalLocked(result);
179 dumpAll = false;
180 }
181
182 const bool dumpApp = argsSet.count("--app") != 0;
183 if (dumpApp) {
184 dumpAppLocked(result);
185 dumpAll = false;
186 }
187
188 if (argsSet.count("--clear")) {
189 bool clearAll = true;
190
191 if (dumpGlobal) {
192 mGlobalStats.clear();
193 clearAll = false;
194 }
195
196 if (dumpApp) {
197 mAppStats.clear();
198 clearAll = false;
199 }
200
201 if (clearAll) {
202 mGlobalStats.clear();
203 mAppStats.clear();
204 }
205
206 dumpAll = false;
207 }
208
209 if (dumpAll) {
210 dumpGlobalLocked(result);
211 dumpAppLocked(result);
212 }
213}
214
215void GpuStats::dumpGlobalLocked(std::string* result) {
Yiwei Zhang174a2a02019-05-06 19:08:31 -0700216 interceptSystemDriverStatsLocked();
217
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800218 for (const auto& ele : mGlobalStats) {
Yiwei Zhangf40fb102019-02-27 21:05:06 -0800219 result->append(ele.second.toString());
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800220 result->append("\n");
221 }
222}
223
224void GpuStats::dumpAppLocked(std::string* result) {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800225 for (const auto& ele : mAppStats) {
Yiwei Zhangf40fb102019-02-27 21:05:06 -0800226 result->append(ele.second.toString());
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800227 result->append("\n");
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800228 }
229}
230
Yiwei Zhangbaea97f2019-02-27 16:35:37 -0800231void GpuStats::pullGlobalStats(std::vector<GpuStatsGlobalInfo>* outStats) {
232 ATRACE_CALL();
233
234 std::lock_guard<std::mutex> lock(mLock);
235 outStats->clear();
236 outStats->reserve(mGlobalStats.size());
237
Yiwei Zhang174a2a02019-05-06 19:08:31 -0700238 interceptSystemDriverStatsLocked();
239
Yiwei Zhangbaea97f2019-02-27 16:35:37 -0800240 for (const auto& ele : mGlobalStats) {
241 outStats->emplace_back(ele.second);
242 }
243
244 mGlobalStats.clear();
245}
246
Yiwei Zhang178e0672019-03-04 14:17:12 -0800247void GpuStats::pullAppStats(std::vector<GpuStatsAppInfo>* outStats) {
248 ATRACE_CALL();
249
250 std::lock_guard<std::mutex> lock(mLock);
251 outStats->clear();
252 outStats->reserve(mAppStats.size());
253
254 for (const auto& ele : mAppStats) {
255 outStats->emplace_back(ele.second);
256 }
257
258 mAppStats.clear();
259}
260
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800261} // namespace android