blob: 18c819f34c9e31eb8f45ad933e1fa14e9f1f2b62 [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 Zhang252e5f62020-02-19 15:44:17 -080027#include <gpumem/GpuMem.h>
Yiwei Zhangbaaef882020-02-02 17:45:30 -080028#include <gpustats/GpuStats.h>
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080029#include <private/android_filesystem_config.h>
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -070030#include <tracing/GpuMemTracer.h>
Jesse Hall8b0d55e2016-03-31 19:29:36 -070031#include <utils/String8.h>
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080032#include <utils/Trace.h>
Jesse Hall8b0d55e2016-03-31 19:29:36 -070033#include <vkjson.h>
Jesse Hallfc038bd2016-03-26 22:20:22 -070034
Yiwei Zhanga265b402020-06-25 22:02:29 -070035#include <thread>
36
Jesse Hallfc038bd2016-03-26 22:20:22 -070037namespace android {
38
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080039using base::StringAppendF;
Jesse Hallfc038bd2016-03-26 22:20:22 -070040
Jesse Hall8b0d55e2016-03-31 19:29:36 -070041namespace {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080042status_t cmdHelp(int out);
43status_t cmdVkjson(int out, int err);
Yiwei Zhang2a61c152019-02-27 11:27:18 -080044void dumpGameDriverInfo(std::string* result);
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080045} // namespace
46
47const String16 sDump("android.permission.DUMP");
Jesse Hall8b0d55e2016-03-31 19:29:36 -070048
Yiwei Zhang423d0802018-11-16 10:56:12 -080049const char* const GpuService::SERVICE_NAME = "gpu";
Jesse Hallfc038bd2016-03-26 22:20:22 -070050
Yiwei Zhang252e5f62020-02-19 15:44:17 -080051GpuService::GpuService()
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -070052 : mGpuMem(std::make_shared<GpuMem>()),
53 mGpuStats(std::make_unique<GpuStats>()),
54 mGpuMemTracer(std::make_unique<GpuMemTracer>()) {
55 std::thread asyncInitThread([this]() {
56 mGpuMem->initialize();
57 mGpuMemTracer->initialize(mGpuMem);
58 });
Yiwei Zhanga265b402020-06-25 22:02:29 -070059 asyncInitThread.detach();
Yiwei Zhang252e5f62020-02-19 15:44:17 -080060};
Jesse Hallfc038bd2016-03-26 22:20:22 -070061
Greg Kaiser210bb7e2019-02-12 12:40:05 -080062void GpuService::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 11:51:55 -080063 const std::string& driverVersionName, uint64_t driverVersionCode,
Yiwei Zhang96c01712019-02-19 16:00:25 -080064 int64_t driverBuildTime, const std::string& appPackageName,
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -070065 const int32_t vulkanVersion, GpuStatsInfo::Driver driver,
Yiwei Zhang794d2952019-05-06 17:43:59 -070066 bool isDriverLoaded, int64_t driverLoadingTime) {
Yiwei Zhangfdd7e782020-01-31 15:59:34 -080067 mGpuStats->insertDriverStats(driverPackageName, driverVersionName, driverVersionCode,
68 driverBuildTime, appPackageName, vulkanVersion, driver,
69 isDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080070}
71
Yiwei Zhangbcba4112019-07-03 13:39:32 -070072void GpuService::setTargetStats(const std::string& appPackageName, const uint64_t driverVersionCode,
73 const GpuStatsInfo::Stats stats, const uint64_t value) {
74 mGpuStats->insertTargetStats(appPackageName, driverVersionCode, stats, value);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -070075}
76
Peiyong Lin0acbfdc2020-06-17 18:47:12 -070077void GpuService::setUpdatableDriverPath(const std::string& driverPath) {
78 developerDriverPath = driverPath;
79}
80
81std::string GpuService::getUpdatableDriverPath() {
82 return developerDriverPath;
83}
84
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080085status_t GpuService::shellCommand(int /*in*/, int out, int err, std::vector<String16>& args) {
86 ATRACE_CALL();
87
88 ALOGV("shellCommand");
Jesse Hall8b0d55e2016-03-31 19:29:36 -070089 for (size_t i = 0, n = args.size(); i < n; i++)
90 ALOGV(" arg[%zu]: '%s'", i, String8(args[i]).string());
91
Jesse Hall3e26b132016-12-20 14:31:28 -080092 if (args.size() >= 1) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080093 if (args[0] == String16("vkjson")) return cmdVkjson(out, err);
94 if (args[0] == String16("help")) return cmdHelp(out);
Jesse Hall3e26b132016-12-20 14:31:28 -080095 }
96 // no command, or unrecognized command
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080097 cmdHelp(err);
Jesse Hall3e26b132016-12-20 14:31:28 -080098 return BAD_VALUE;
Jesse Hallfc038bd2016-03-26 22:20:22 -070099}
100
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800101status_t GpuService::doDump(int fd, const Vector<String16>& args, bool /*asProto*/) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800102 std::string result;
103
104 IPCThreadState* ipc = IPCThreadState::self();
105 const int pid = ipc->getCallingPid();
106 const int uid = ipc->getCallingUid();
107
108 if ((uid != AID_SHELL) && !PermissionCache::checkPermission(sDump, pid, uid)) {
109 StringAppendF(&result, "Permission Denial: can't dump gpu from pid=%d, uid=%d\n", pid, uid);
110 } else {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800111 bool dumpAll = true;
Mikael Pessa3d146052019-07-09 09:25:38 -0700112 bool dumpDriverInfo = false;
Yiwei Zhang252e5f62020-02-19 15:44:17 -0800113 bool dumpMem = false;
Mikael Pessa3d146052019-07-09 09:25:38 -0700114 bool dumpStats = false;
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800115 size_t numArgs = args.size();
116
117 if (numArgs) {
Mikael Pessa3d146052019-07-09 09:25:38 -0700118 for (size_t index = 0; index < numArgs; ++index) {
119 if (args[index] == String16("--gpustats")) {
120 dumpStats = true;
121 } else if (args[index] == String16("--gpudriverinfo")) {
122 dumpDriverInfo = true;
Yiwei Zhang252e5f62020-02-19 15:44:17 -0800123 } else if (args[index] == String16("--gpumem")) {
124 dumpMem = true;
Mikael Pessa3d146052019-07-09 09:25:38 -0700125 }
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800126 }
Yiwei Zhangf881dd32020-06-07 17:00:21 -0700127 dumpAll = !(dumpDriverInfo || dumpMem || dumpStats);
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800128 }
129
Mikael Pessa3d146052019-07-09 09:25:38 -0700130 if (dumpAll || dumpDriverInfo) {
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800131 dumpGameDriverInfo(&result);
132 result.append("\n");
Mikael Pessa3d146052019-07-09 09:25:38 -0700133 }
Yiwei Zhang252e5f62020-02-19 15:44:17 -0800134 if (dumpAll || dumpMem) {
135 mGpuMem->dump(args, &result);
136 result.append("\n");
137 }
Mikael Pessa3d146052019-07-09 09:25:38 -0700138 if (dumpAll || dumpStats) {
Yiwei Zhang8cf7c7b2019-08-28 13:07:30 -0700139 mGpuStats->dump(args, &result);
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800140 result.append("\n");
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800141 }
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800142 }
143
144 write(fd, result.c_str(), result.size());
145 return NO_ERROR;
146}
147
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700148namespace {
149
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800150status_t cmdHelp(int out) {
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700151 FILE* outs = fdopen(out, "w");
152 if (!outs) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800153 ALOGE("vkjson: failed to create out stream: %s (%d)", strerror(errno), errno);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700154 return BAD_VALUE;
155 }
156 fprintf(outs,
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800157 "GPU Service commands:\n"
158 " vkjson dump Vulkan properties as JSON\n");
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700159 fclose(outs);
160 return NO_ERROR;
161}
162
Jesse Hall3841bf42016-06-12 15:08:28 -0700163void vkjsonPrint(FILE* out) {
164 std::string json = VkJsonInstanceToJson(VkJsonGetInstance());
165 fwrite(json.data(), 1, json.size(), out);
166 fputc('\n', out);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700167}
168
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800169status_t cmdVkjson(int out, int /*err*/) {
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700170 FILE* outs = fdopen(out, "w");
171 if (!outs) {
Jesse Hall3841bf42016-06-12 15:08:28 -0700172 int errnum = errno;
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700173 ALOGE("vkjson: failed to create output stream: %s", strerror(errnum));
174 return -errnum;
175 }
Jesse Hall3841bf42016-06-12 15:08:28 -0700176 vkjsonPrint(outs);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700177 fclose(outs);
Jesse Hall3841bf42016-06-12 15:08:28 -0700178 return NO_ERROR;
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700179}
180
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800181void dumpGameDriverInfo(std::string* result) {
182 if (!result) return;
183
184 char stableGameDriver[PROPERTY_VALUE_MAX] = {};
185 property_get("ro.gfx.driver.0", stableGameDriver, "unsupported");
186 StringAppendF(result, "Stable Game Driver: %s\n", stableGameDriver);
187
188 char preReleaseGameDriver[PROPERTY_VALUE_MAX] = {};
189 property_get("ro.gfx.driver.1", preReleaseGameDriver, "unsupported");
190 StringAppendF(result, "Pre-release Game Driver: %s\n", preReleaseGameDriver);
191}
192
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700193} // anonymous namespace
194
Jesse Hallfc038bd2016-03-26 22:20:22 -0700195} // namespace android