blob: 1cb78db12dc4fa7cb2920969d9deb2c9065d2cec [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 Zhangf40fb102019-02-27 21:05:06 -080022#include <unordered_set>
23
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080024#include <log/log.h>
25#include <utils/Trace.h>
26
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080027namespace android {
28
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080029static bool addLoadingCount(GraphicsEnv::Driver driver, bool isDriverLoaded,
Yiwei Zhangf40fb102019-02-27 21:05:06 -080030 GpuStatsGlobalInfo* const outGlobalInfo) {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080031 switch (driver) {
32 case GraphicsEnv::Driver::GL:
33 case GraphicsEnv::Driver::GL_UPDATED:
Yiwei Zhangf40fb102019-02-27 21:05:06 -080034 outGlobalInfo->glLoadingCount++;
35 if (!isDriverLoaded) outGlobalInfo->glLoadingFailureCount++;
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080036 break;
37 case GraphicsEnv::Driver::VULKAN:
38 case GraphicsEnv::Driver::VULKAN_UPDATED:
Yiwei Zhangf40fb102019-02-27 21:05:06 -080039 outGlobalInfo->vkLoadingCount++;
40 if (!isDriverLoaded) outGlobalInfo->vkLoadingFailureCount++;
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080041 break;
42 default:
43 // Currently we don't support GraphicsEnv::Driver::ANGLE because the
44 // basic driver package info only belongs to system or updated driver.
45 return false;
46 }
47
48 return true;
49}
50
51static void addLoadingTime(GraphicsEnv::Driver driver, int64_t driverLoadingTime,
Yiwei Zhangf40fb102019-02-27 21:05:06 -080052 GpuStatsAppInfo* const outAppInfo) {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080053 switch (driver) {
54 case GraphicsEnv::Driver::GL:
55 case GraphicsEnv::Driver::GL_UPDATED:
Yiwei Zhangf2d84132019-03-06 11:32:50 -080056 if (outAppInfo->glDriverLoadingTime.size() >= GpuStats::MAX_NUM_LOADING_TIMES) break;
Yiwei Zhangf40fb102019-02-27 21:05:06 -080057 outAppInfo->glDriverLoadingTime.emplace_back(driverLoadingTime);
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080058 break;
59 case GraphicsEnv::Driver::VULKAN:
60 case GraphicsEnv::Driver::VULKAN_UPDATED:
Yiwei Zhangf2d84132019-03-06 11:32:50 -080061 if (outAppInfo->vkDriverLoadingTime.size() >= GpuStats::MAX_NUM_LOADING_TIMES) break;
Yiwei Zhangf40fb102019-02-27 21:05:06 -080062 outAppInfo->vkDriverLoadingTime.emplace_back(driverLoadingTime);
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080063 break;
64 default:
65 break;
66 }
67}
68
69void GpuStats::insert(const std::string& driverPackageName, const std::string& driverVersionName,
70 uint64_t driverVersionCode, int64_t driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -070071 const std::string& appPackageName, const int32_t vulkanVersion,
72 GraphicsEnv::Driver driver, bool isDriverLoaded, int64_t driverLoadingTime) {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080073 ATRACE_CALL();
74
75 std::lock_guard<std::mutex> lock(mLock);
76 ALOGV("Received:\n"
77 "\tdriverPackageName[%s]\n"
78 "\tdriverVersionName[%s]\n"
79 "\tdriverVersionCode[%" PRIu64 "]\n"
80 "\tdriverBuildTime[%" PRId64 "]\n"
81 "\tappPackageName[%s]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -070082 "\tvulkanVersion[%d]\n"
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080083 "\tdriver[%d]\n"
84 "\tisDriverLoaded[%d]\n"
85 "\tdriverLoadingTime[%" PRId64 "]",
86 driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -070087 appPackageName.c_str(), vulkanVersion, static_cast<int32_t>(driver), isDriverLoaded,
88 driverLoadingTime);
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080089
90 if (!mGlobalStats.count(driverVersionCode)) {
Yiwei Zhangf40fb102019-02-27 21:05:06 -080091 GpuStatsGlobalInfo globalInfo;
92 if (!addLoadingCount(driver, isDriverLoaded, &globalInfo)) {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080093 return;
94 }
Yiwei Zhangf40fb102019-02-27 21:05:06 -080095 globalInfo.driverPackageName = driverPackageName;
96 globalInfo.driverVersionName = driverVersionName;
97 globalInfo.driverVersionCode = driverVersionCode;
98 globalInfo.driverBuildTime = driverBuildTime;
Yiwei Zhang794d2952019-05-06 17:43:59 -070099 globalInfo.vulkanVersion = vulkanVersion;
Yiwei Zhangf40fb102019-02-27 21:05:06 -0800100 mGlobalStats.insert({driverVersionCode, globalInfo});
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800101 } else if (!addLoadingCount(driver, isDriverLoaded, &mGlobalStats[driverVersionCode])) {
102 return;
103 }
104
105 if (mAppStats.size() >= MAX_NUM_APP_RECORDS) {
Yiwei Zhangf40fb102019-02-27 21:05:06 -0800106 ALOGV("GpuStatsAppInfo has reached maximum size. Ignore new stats.");
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800107 return;
108 }
109
110 const std::string appStatsKey = appPackageName + std::to_string(driverVersionCode);
111 if (!mAppStats.count(appStatsKey)) {
Yiwei Zhangf40fb102019-02-27 21:05:06 -0800112 GpuStatsAppInfo appInfo;
113 addLoadingTime(driver, driverLoadingTime, &appInfo);
114 appInfo.appPackageName = appPackageName;
115 appInfo.driverVersionCode = driverVersionCode;
116 mAppStats.insert({appStatsKey, appInfo});
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800117 return;
118 }
119
120 addLoadingTime(driver, driverLoadingTime, &mAppStats[appStatsKey]);
121}
122
123void GpuStats::dump(const Vector<String16>& args, std::string* result) {
124 ATRACE_CALL();
125
126 if (!result) {
127 ALOGE("Dump result shouldn't be nullptr.");
128 return;
129 }
130
131 std::lock_guard<std::mutex> lock(mLock);
132 bool dumpAll = true;
133
134 std::unordered_set<std::string> argsSet;
135 for (size_t i = 0; i < args.size(); i++) {
136 argsSet.insert(String8(args[i]).c_str());
137 }
138
139 const bool dumpGlobal = argsSet.count("--global") != 0;
140 if (dumpGlobal) {
141 dumpGlobalLocked(result);
142 dumpAll = false;
143 }
144
145 const bool dumpApp = argsSet.count("--app") != 0;
146 if (dumpApp) {
147 dumpAppLocked(result);
148 dumpAll = false;
149 }
150
151 if (argsSet.count("--clear")) {
152 bool clearAll = true;
153
154 if (dumpGlobal) {
155 mGlobalStats.clear();
156 clearAll = false;
157 }
158
159 if (dumpApp) {
160 mAppStats.clear();
161 clearAll = false;
162 }
163
164 if (clearAll) {
165 mGlobalStats.clear();
166 mAppStats.clear();
167 }
168
169 dumpAll = false;
170 }
171
172 if (dumpAll) {
173 dumpGlobalLocked(result);
174 dumpAppLocked(result);
175 }
176}
177
178void GpuStats::dumpGlobalLocked(std::string* result) {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800179 for (const auto& ele : mGlobalStats) {
Yiwei Zhangf40fb102019-02-27 21:05:06 -0800180 result->append(ele.second.toString());
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800181 result->append("\n");
182 }
183}
184
185void GpuStats::dumpAppLocked(std::string* result) {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800186 for (const auto& ele : mAppStats) {
Yiwei Zhangf40fb102019-02-27 21:05:06 -0800187 result->append(ele.second.toString());
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800188 result->append("\n");
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800189 }
190}
191
Yiwei Zhangbaea97f2019-02-27 16:35:37 -0800192void GpuStats::pullGlobalStats(std::vector<GpuStatsGlobalInfo>* outStats) {
193 ATRACE_CALL();
194
195 std::lock_guard<std::mutex> lock(mLock);
196 outStats->clear();
197 outStats->reserve(mGlobalStats.size());
198
199 for (const auto& ele : mGlobalStats) {
200 outStats->emplace_back(ele.second);
201 }
202
203 mGlobalStats.clear();
204}
205
Yiwei Zhang178e0672019-03-04 14:17:12 -0800206void GpuStats::pullAppStats(std::vector<GpuStatsAppInfo>* outStats) {
207 ATRACE_CALL();
208
209 std::lock_guard<std::mutex> lock(mLock);
210 outStats->clear();
211 outStats->reserve(mAppStats.size());
212
213 for (const auto& ele : mAppStats) {
214 outStats->emplace_back(ele.second);
215 }
216
217 mAppStats.clear();
218}
219
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800220} // namespace android