blob: 58c6639916227bb482baa3300f3dfe69bfa2a94d [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 Zhangbaaef882020-02-02 17:45:30 -080020#include <gpustats/GpuStats.h>
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080021
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
Yiwei Zhangfdd7e782020-01-31 15:59:34 -080077void GpuStats::insertDriverStats(const std::string& driverPackageName,
78 const std::string& driverVersionName, uint64_t driverVersionCode,
79 int64_t driverBuildTime, const std::string& appPackageName,
80 const int32_t vulkanVersion, GpuStatsInfo::Driver driver,
81 bool isDriverLoaded, int64_t driverLoadingTime) {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080082 ATRACE_CALL();
83
84 std::lock_guard<std::mutex> lock(mLock);
85 ALOGV("Received:\n"
86 "\tdriverPackageName[%s]\n"
87 "\tdriverVersionName[%s]\n"
88 "\tdriverVersionCode[%" PRIu64 "]\n"
89 "\tdriverBuildTime[%" PRId64 "]\n"
90 "\tappPackageName[%s]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -070091 "\tvulkanVersion[%d]\n"
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080092 "\tdriver[%d]\n"
93 "\tisDriverLoaded[%d]\n"
94 "\tdriverLoadingTime[%" PRId64 "]",
95 driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -070096 appPackageName.c_str(), vulkanVersion, static_cast<int32_t>(driver), isDriverLoaded,
97 driverLoadingTime);
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080098
99 if (!mGlobalStats.count(driverVersionCode)) {
Yiwei Zhangf40fb102019-02-27 21:05:06 -0800100 GpuStatsGlobalInfo globalInfo;
Yiwei Zhang8e7c4b62019-05-08 15:57:59 -0700101 addLoadingCount(driver, isDriverLoaded, &globalInfo);
Yiwei Zhangf40fb102019-02-27 21:05:06 -0800102 globalInfo.driverPackageName = driverPackageName;
103 globalInfo.driverVersionName = driverVersionName;
104 globalInfo.driverVersionCode = driverVersionCode;
105 globalInfo.driverBuildTime = driverBuildTime;
Yiwei Zhang794d2952019-05-06 17:43:59 -0700106 globalInfo.vulkanVersion = vulkanVersion;
Yiwei Zhangf40fb102019-02-27 21:05:06 -0800107 mGlobalStats.insert({driverVersionCode, globalInfo});
Yiwei Zhang8e7c4b62019-05-08 15:57:59 -0700108 } else {
109 addLoadingCount(driver, isDriverLoaded, &mGlobalStats[driverVersionCode]);
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800110 }
111
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800112 const std::string appStatsKey = appPackageName + std::to_string(driverVersionCode);
113 if (!mAppStats.count(appStatsKey)) {
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700114 if (mAppStats.size() >= MAX_NUM_APP_RECORDS) {
115 ALOGV("GpuStatsAppInfo has reached maximum size. Ignore new stats.");
116 return;
117 }
118
Yiwei Zhangf40fb102019-02-27 21:05:06 -0800119 GpuStatsAppInfo appInfo;
120 addLoadingTime(driver, driverLoadingTime, &appInfo);
121 appInfo.appPackageName = appPackageName;
122 appInfo.driverVersionCode = driverVersionCode;
123 mAppStats.insert({appStatsKey, appInfo});
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800124 return;
125 }
126
127 addLoadingTime(driver, driverLoadingTime, &mAppStats[appStatsKey]);
128}
129
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700130void GpuStats::insertTargetStats(const std::string& appPackageName,
131 const uint64_t driverVersionCode, const GpuStatsInfo::Stats stats,
132 const uint64_t /*value*/) {
133 ATRACE_CALL();
134
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700135 const std::string appStatsKey = appPackageName + std::to_string(driverVersionCode);
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700136
137 std::lock_guard<std::mutex> lock(mLock);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700138 if (!mAppStats.count(appStatsKey)) {
139 return;
140 }
141
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700142 switch (stats) {
143 case GpuStatsInfo::Stats::CPU_VULKAN_IN_USE:
144 mAppStats[appStatsKey].cpuVulkanInUse = true;
145 break;
Yiwei Zhang69395cd2019-07-03 16:55:39 -0700146 case GpuStatsInfo::Stats::FALSE_PREROTATION:
147 mAppStats[appStatsKey].falsePrerotation = true;
148 break;
Yiwei Zhang011538f2019-12-20 14:37:21 -0800149 case GpuStatsInfo::Stats::GLES_1_IN_USE:
150 mAppStats[appStatsKey].gles1InUse = true;
151 break;
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700152 default:
153 break;
154 }
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700155}
156
Yiwei Zhang174a2a02019-05-06 19:08:31 -0700157void GpuStats::interceptSystemDriverStatsLocked() {
158 // Append cpuVulkanVersion and glesVersion to system driver stats
159 if (!mGlobalStats.count(0) || mGlobalStats[0].glesVersion) {
160 return;
161 }
162
163 mGlobalStats[0].cpuVulkanVersion = property_get_int32("ro.cpuvulkan.version", 0);
164 mGlobalStats[0].glesVersion = property_get_int32("ro.opengles.version", 0);
165}
166
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800167void GpuStats::dump(const Vector<String16>& args, std::string* result) {
168 ATRACE_CALL();
169
170 if (!result) {
171 ALOGE("Dump result shouldn't be nullptr.");
172 return;
173 }
174
175 std::lock_guard<std::mutex> lock(mLock);
176 bool dumpAll = true;
177
178 std::unordered_set<std::string> argsSet;
179 for (size_t i = 0; i < args.size(); i++) {
180 argsSet.insert(String8(args[i]).c_str());
181 }
182
183 const bool dumpGlobal = argsSet.count("--global") != 0;
184 if (dumpGlobal) {
185 dumpGlobalLocked(result);
186 dumpAll = false;
187 }
188
189 const bool dumpApp = argsSet.count("--app") != 0;
190 if (dumpApp) {
191 dumpAppLocked(result);
192 dumpAll = false;
193 }
194
Yiwei Zhangfdd7e782020-01-31 15:59:34 -0800195 if (dumpAll) {
196 dumpGlobalLocked(result);
197 dumpAppLocked(result);
198 }
199
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800200 if (argsSet.count("--clear")) {
201 bool clearAll = true;
202
203 if (dumpGlobal) {
204 mGlobalStats.clear();
205 clearAll = false;
206 }
207
208 if (dumpApp) {
209 mAppStats.clear();
210 clearAll = false;
211 }
212
213 if (clearAll) {
214 mGlobalStats.clear();
215 mAppStats.clear();
216 }
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800217 }
218}
219
220void GpuStats::dumpGlobalLocked(std::string* result) {
Yiwei Zhang174a2a02019-05-06 19:08:31 -0700221 interceptSystemDriverStatsLocked();
222
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800223 for (const auto& ele : mGlobalStats) {
Yiwei Zhangf40fb102019-02-27 21:05:06 -0800224 result->append(ele.second.toString());
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800225 result->append("\n");
226 }
227}
228
229void GpuStats::dumpAppLocked(std::string* result) {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800230 for (const auto& ele : mAppStats) {
Yiwei Zhangf40fb102019-02-27 21:05:06 -0800231 result->append(ele.second.toString());
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800232 result->append("\n");
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800233 }
234}
235
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800236} // namespace android