blob: 55a0c0a63976b9d8f850aea11a2cab404a36189a [file] [log] [blame]
Jesse Hallfc038bd2016-03-26 22:20:22 -07001/*
2 * Copyright 2016 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
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Jesse Hallfc038bd2016-03-26 22:20:22 -070019#include "GpuService.h"
20
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080021#include <android-base/stringprintf.h>
22#include <binder/IPCThreadState.h>
Jesse Hallc0b15772016-12-20 14:47:45 -080023#include <binder/IResultReceiver.h>
Jesse Hallfc038bd2016-03-26 22:20:22 -070024#include <binder/Parcel.h>
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080025#include <binder/PermissionCache.h>
Yiwei Zhang2a61c152019-02-27 11:27:18 -080026#include <cutils/properties.h>
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080027#include <private/android_filesystem_config.h>
Jesse Hall8b0d55e2016-03-31 19:29:36 -070028#include <utils/String8.h>
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080029#include <utils/Trace.h>
30
Mikael Pessa3d146052019-07-09 09:25:38 -070031#include <array>
32#include <fstream>
33#include <sstream>
34#include <sys/types.h>
Jesse Hall8b0d55e2016-03-31 19:29:36 -070035#include <vkjson.h>
Mikael Pessa3d146052019-07-09 09:25:38 -070036#include <unistd.h>
Jesse Hallfc038bd2016-03-26 22:20:22 -070037
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080038#include "gpustats/GpuStats.h"
39
Jesse Hallfc038bd2016-03-26 22:20:22 -070040namespace android {
41
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080042using base::StringAppendF;
Jesse Hallfc038bd2016-03-26 22:20:22 -070043
Jesse Hall8b0d55e2016-03-31 19:29:36 -070044namespace {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080045status_t cmdHelp(int out);
46status_t cmdVkjson(int out, int err);
Yiwei Zhang2a61c152019-02-27 11:27:18 -080047void dumpGameDriverInfo(std::string* result);
Mikael Pessa3d146052019-07-09 09:25:38 -070048void dumpMemoryInfo(std::string* result, const GpuMemoryMap& memories, uint32_t pid);
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080049} // namespace
50
51const String16 sDump("android.permission.DUMP");
Jesse Hall8b0d55e2016-03-31 19:29:36 -070052
Yiwei Zhang423d0802018-11-16 10:56:12 -080053const char* const GpuService::SERVICE_NAME = "gpu";
Jesse Hallfc038bd2016-03-26 22:20:22 -070054
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080055GpuService::GpuService() : mGpuStats(std::make_unique<GpuStats>()){};
Jesse Hallfc038bd2016-03-26 22:20:22 -070056
Greg Kaiser210bb7e2019-02-12 12:40:05 -080057void GpuService::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 11:51:55 -080058 const std::string& driverVersionName, uint64_t driverVersionCode,
Yiwei Zhang96c01712019-02-19 16:00:25 -080059 int64_t driverBuildTime, const std::string& appPackageName,
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -070060 const int32_t vulkanVersion, GpuStatsInfo::Driver driver,
Yiwei Zhang794d2952019-05-06 17:43:59 -070061 bool isDriverLoaded, int64_t driverLoadingTime) {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080062 mGpuStats->insert(driverPackageName, driverVersionName, driverVersionCode, driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -070063 appPackageName, vulkanVersion, driver, isDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080064}
65
Yiwei Zhangbaea97f2019-02-27 16:35:37 -080066status_t GpuService::getGpuStatsGlobalInfo(std::vector<GpuStatsGlobalInfo>* outStats) const {
Yiwei Zhangbaea97f2019-02-27 16:35:37 -080067 mGpuStats->pullGlobalStats(outStats);
Yiwei Zhangbaea97f2019-02-27 16:35:37 -080068 return OK;
69}
70
Yiwei Zhang178e0672019-03-04 14:17:12 -080071status_t GpuService::getGpuStatsAppInfo(std::vector<GpuStatsAppInfo>* outStats) const {
Yiwei Zhang178e0672019-03-04 14:17:12 -080072 mGpuStats->pullAppStats(outStats);
Yiwei Zhang178e0672019-03-04 14:17:12 -080073 return OK;
74}
75
Yiwei Zhangbcba4112019-07-03 13:39:32 -070076void GpuService::setTargetStats(const std::string& appPackageName, const uint64_t driverVersionCode,
77 const GpuStatsInfo::Stats stats, const uint64_t value) {
78 mGpuStats->insertTargetStats(appPackageName, driverVersionCode, stats, value);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -070079}
80
Mikael Pessa3d146052019-07-09 09:25:38 -070081bool isExpectedFormat(const char* str) {
82 // Should match in order:
83 // gpuaddr useraddr size id flags type usage sglen mapsize eglsrf eglimg
84 std::istringstream iss;
85 iss.str(str);
86
87 std::string word;
88 iss >> word;
89 if (word != "gpuaddr") { return false; }
90 iss >> word;
91 if (word != "useraddr") { return false; }
92 iss >> word;
93 if (word != "size") { return false; }
94 iss >> word;
95 if (word != "id") { return false; }
96 iss >> word;
97 if (word != "flags") { return false; }
98 iss >> word;
99 if (word != "type") { return false; }
100 iss >> word;
101 if (word != "usage") { return false; }
102 iss >> word;
103 if (word != "sglen") { return false; }
104 iss >> word;
105 if (word != "mapsize") { return false; }
106 iss >> word;
107 if (word != "eglsrf") { return false; }
108 iss >> word;
109 if (word != "eglimg") { return false; }
110 return true;
111}
112
113
114// Queries gpu memory via Qualcomm's /d/kgsl/proc/*/mem interface.
115status_t GpuService::getQCommGpuMemoryInfo(GpuMemoryMap* memories, std::string* result, int32_t dumpPid) const {
116 const std::string kDirectoryPath = "/d/kgsl/proc";
117 DIR* directory = opendir(kDirectoryPath.c_str());
118 if (!directory) { return PERMISSION_DENIED; }
119
120 // File Format:
121 // gpuaddr useraddr size id flags type usage sglen mapsize eglsrf eglimg
122 // 0000000000000000 0000000000000000 8359936 23 --w--pY-- gpumem VK/others( 38) 0 0 0 0
123 // 0000000000000000 0000000000000000 16293888 24 --wL--N-- ion surface 41 0 0 1
124
125 const bool dumpAll = dumpPid == 0;
126 static constexpr size_t kMaxLineLength = 1024;
127 static char line[kMaxLineLength];
128 while(dirent* subdir = readdir(directory)) {
129 // Skip "." and ".." in directory.
130 if (strcmp(subdir->d_name, ".") == 0 || strcmp(subdir->d_name, "..") == 0 ) { continue; }
131
132 std::string pid_str(subdir->d_name);
133 const uint32_t pid(stoi(pid_str));
134
135 if (!dumpAll && dumpPid != pid) {
136 continue;
137 }
138
139 std::string filepath(kDirectoryPath + "/" + pid_str + "/mem");
140 std::ifstream file(filepath);
141
142 // Check first line
143 file.getline(line, kMaxLineLength);
144 if (!isExpectedFormat(line)) {
145 continue;
146 }
147
148 if (result) {
149 StringAppendF(result, "%d:\n%s\n", pid, line);
150 }
151
152 while( file.getline(line, kMaxLineLength) ) {
153 if (result) {
154 StringAppendF(result, "%s\n", line);
155 }
156
157 std::istringstream iss;
158 iss.str(line);
159
160 // Skip gpuaddr, useraddr.
161 const char delimiter = ' ';
162 iss >> std::ws;
163 iss.ignore(kMaxLineLength, delimiter);
164 iss >> std::ws;
165 iss.ignore(kMaxLineLength, delimiter);
166
167 // Get size.
168 int64_t memsize;
169 iss >> memsize;
170
171 // Skip id, flags.
172 iss >> std::ws;
173 iss.ignore(kMaxLineLength, delimiter);
174 iss >> std::ws;
175 iss.ignore(kMaxLineLength, delimiter);
176
177 // Get type, usage.
178 std::string memtype;
179 std::string usage;
180 iss >> memtype >> usage;
181
182 // Adjust for the space in VK/others( #)
183 if (usage == "VK/others(") {
184 std::string vkTypeEnd;
185 iss >> vkTypeEnd;
186 usage.append(vkTypeEnd);
187 }
188
189 // Skip sglen.
190 iss >> std::ws;
191 iss.ignore(kMaxLineLength, delimiter);
192
193 // Get mapsize.
194 int64_t mapsize;
195 iss >> mapsize;
196
197 if (memsize == 0 && mapsize == 0) {
198 continue;
199 }
200
201 if (memtype == "gpumem") {
202 (*memories)[pid][usage].gpuMemory += memsize;
203 } else {
204 (*memories)[pid][usage].ionMemory += memsize;
205 }
206
207 if (mapsize > 0) {
208 (*memories)[pid][usage].mappedMemory += mapsize;
209 }
210 }
211
212 if (result) {
213 StringAppendF(result, "\n");
214 }
215 }
216
217 closedir(directory);
218
219 return OK;
220}
221
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800222status_t GpuService::shellCommand(int /*in*/, int out, int err, std::vector<String16>& args) {
223 ATRACE_CALL();
224
225 ALOGV("shellCommand");
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700226 for (size_t i = 0, n = args.size(); i < n; i++)
227 ALOGV(" arg[%zu]: '%s'", i, String8(args[i]).string());
228
Jesse Hall3e26b132016-12-20 14:31:28 -0800229 if (args.size() >= 1) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800230 if (args[0] == String16("vkjson")) return cmdVkjson(out, err);
231 if (args[0] == String16("help")) return cmdHelp(out);
Jesse Hall3e26b132016-12-20 14:31:28 -0800232 }
233 // no command, or unrecognized command
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800234 cmdHelp(err);
Jesse Hall3e26b132016-12-20 14:31:28 -0800235 return BAD_VALUE;
Jesse Hallfc038bd2016-03-26 22:20:22 -0700236}
237
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800238status_t GpuService::doDump(int fd, const Vector<String16>& args, bool /*asProto*/) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800239 std::string result;
240
241 IPCThreadState* ipc = IPCThreadState::self();
242 const int pid = ipc->getCallingPid();
243 const int uid = ipc->getCallingUid();
244
245 if ((uid != AID_SHELL) && !PermissionCache::checkPermission(sDump, pid, uid)) {
246 StringAppendF(&result, "Permission Denial: can't dump gpu from pid=%d, uid=%d\n", pid, uid);
247 } else {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800248 bool dumpAll = true;
Mikael Pessa3d146052019-07-09 09:25:38 -0700249 bool dumpDriverInfo = false;
250 bool dumpStats = false;
251 bool dumpMemory = false;
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800252 size_t numArgs = args.size();
Mikael Pessa3d146052019-07-09 09:25:38 -0700253 int32_t pid = 0;
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800254
255 if (numArgs) {
Mikael Pessa3d146052019-07-09 09:25:38 -0700256 dumpAll = false;
257 for (size_t index = 0; index < numArgs; ++index) {
258 if (args[index] == String16("--gpustats")) {
259 dumpStats = true;
260 } else if (args[index] == String16("--gpudriverinfo")) {
261 dumpDriverInfo = true;
262 } else if (args[index] == String16("--gpumem")) {
263 dumpMemory = true;
Peter Collingbourned09003d2019-08-20 10:31:18 -0700264 } else if (args[index].startsWith(String16("--gpumem="))) {
Mikael Pessa3d146052019-07-09 09:25:38 -0700265 dumpMemory = true;
266 pid = atoi(String8(&args[index][9]));
267 }
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800268 }
269 }
270
Mikael Pessa3d146052019-07-09 09:25:38 -0700271 if (dumpAll || dumpDriverInfo) {
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800272 dumpGameDriverInfo(&result);
273 result.append("\n");
Mikael Pessa3d146052019-07-09 09:25:38 -0700274 }
275 if (dumpAll || dumpStats) {
Yiwei Zhang8cf7c7b2019-08-28 13:07:30 -0700276 mGpuStats->dump(args, &result);
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800277 result.append("\n");
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800278 }
Mikael Pessa3d146052019-07-09 09:25:38 -0700279 if (dumpAll || dumpMemory) {
280 GpuMemoryMap memories;
281 // Currently only queries Qualcomm gpu memory. More will be added later.
282 if (getQCommGpuMemoryInfo(&memories, &result, pid) == OK) {
283 dumpMemoryInfo(&result, memories, pid);
284 result.append("\n");
285 }
286 }
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800287 }
288
289 write(fd, result.c_str(), result.size());
290 return NO_ERROR;
291}
292
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700293namespace {
294
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800295status_t cmdHelp(int out) {
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700296 FILE* outs = fdopen(out, "w");
297 if (!outs) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800298 ALOGE("vkjson: failed to create out stream: %s (%d)", strerror(errno), errno);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700299 return BAD_VALUE;
300 }
301 fprintf(outs,
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800302 "GPU Service commands:\n"
303 " vkjson dump Vulkan properties as JSON\n");
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700304 fclose(outs);
305 return NO_ERROR;
306}
307
Jesse Hall3841bf42016-06-12 15:08:28 -0700308void vkjsonPrint(FILE* out) {
309 std::string json = VkJsonInstanceToJson(VkJsonGetInstance());
310 fwrite(json.data(), 1, json.size(), out);
311 fputc('\n', out);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700312}
313
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800314status_t cmdVkjson(int out, int /*err*/) {
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700315 FILE* outs = fdopen(out, "w");
316 if (!outs) {
Jesse Hall3841bf42016-06-12 15:08:28 -0700317 int errnum = errno;
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700318 ALOGE("vkjson: failed to create output stream: %s", strerror(errnum));
319 return -errnum;
320 }
Jesse Hall3841bf42016-06-12 15:08:28 -0700321 vkjsonPrint(outs);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700322 fclose(outs);
Jesse Hall3841bf42016-06-12 15:08:28 -0700323 return NO_ERROR;
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700324}
325
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800326void dumpGameDriverInfo(std::string* result) {
327 if (!result) return;
328
329 char stableGameDriver[PROPERTY_VALUE_MAX] = {};
330 property_get("ro.gfx.driver.0", stableGameDriver, "unsupported");
331 StringAppendF(result, "Stable Game Driver: %s\n", stableGameDriver);
332
333 char preReleaseGameDriver[PROPERTY_VALUE_MAX] = {};
334 property_get("ro.gfx.driver.1", preReleaseGameDriver, "unsupported");
335 StringAppendF(result, "Pre-release Game Driver: %s\n", preReleaseGameDriver);
336}
337
Mikael Pessa3d146052019-07-09 09:25:38 -0700338// Read and print all memory info for each process from /d/kgsl/proc/<pid>/mem.
339void dumpMemoryInfo(std::string* result, const GpuMemoryMap& memories, uint32_t pid) {
340 if (!result) return;
341
342 // Write results.
343 StringAppendF(result, "GPU Memory Summary:\n");
344 for(auto& mem : memories) {
345 uint32_t process = mem.first;
346 if (pid != 0 && pid != process) {
347 continue;
348 }
349
350 StringAppendF(result, "%d:\n", process);
351 for(auto& memStruct : mem.second) {
352 StringAppendF(result, " %s", memStruct.first.c_str());
353
354 if(memStruct.second.gpuMemory > 0)
355 StringAppendF(result, ", GPU memory = %" PRId64, memStruct.second.gpuMemory);
356 if(memStruct.second.mappedMemory > 0)
357 StringAppendF(result, ", Mapped memory = %" PRId64, memStruct.second.mappedMemory);
358 if(memStruct.second.ionMemory > 0)
359 StringAppendF(result, ", Ion memory = %" PRId64, memStruct.second.ionMemory);
360
361 StringAppendF(result, "\n");
362 }
363 }
364}
365
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700366} // anonymous namespace
367
Jesse Hallfc038bd2016-03-26 22:20:22 -0700368} // namespace android