blob: c36e4b90cf79001d6aa9dfe823d5cfd9ed526a25 [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>
Jesse Hall8b0d55e2016-03-31 19:29:36 -070030#include <utils/String8.h>
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080031#include <utils/Trace.h>
Jesse Hall8b0d55e2016-03-31 19:29:36 -070032#include <vkjson.h>
Jesse Hallfc038bd2016-03-26 22:20:22 -070033
Yiwei Zhanga265b402020-06-25 22:02:29 -070034#include <thread>
35
Jesse Hallfc038bd2016-03-26 22:20:22 -070036namespace android {
37
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080038using base::StringAppendF;
Jesse Hallfc038bd2016-03-26 22:20:22 -070039
Jesse Hall8b0d55e2016-03-31 19:29:36 -070040namespace {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080041status_t cmdHelp(int out);
42status_t cmdVkjson(int out, int err);
Yiwei Zhang2a61c152019-02-27 11:27:18 -080043void dumpGameDriverInfo(std::string* result);
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080044} // namespace
45
46const String16 sDump("android.permission.DUMP");
Jesse Hall8b0d55e2016-03-31 19:29:36 -070047
Yiwei Zhang423d0802018-11-16 10:56:12 -080048const char* const GpuService::SERVICE_NAME = "gpu";
Jesse Hallfc038bd2016-03-26 22:20:22 -070049
Yiwei Zhang252e5f62020-02-19 15:44:17 -080050GpuService::GpuService()
51 : mGpuMem(std::make_unique<GpuMem>()), mGpuStats(std::make_unique<GpuStats>()) {
Yiwei Zhanga265b402020-06-25 22:02:29 -070052 std::thread asyncInitThread([this]() { mGpuMem->initialize(); });
53 asyncInitThread.detach();
Yiwei Zhang252e5f62020-02-19 15:44:17 -080054};
Jesse Hallfc038bd2016-03-26 22:20:22 -070055
Greg Kaiser210bb7e2019-02-12 12:40:05 -080056void GpuService::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 11:51:55 -080057 const std::string& driverVersionName, uint64_t driverVersionCode,
Yiwei Zhang96c01712019-02-19 16:00:25 -080058 int64_t driverBuildTime, const std::string& appPackageName,
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -070059 const int32_t vulkanVersion, GpuStatsInfo::Driver driver,
Yiwei Zhang794d2952019-05-06 17:43:59 -070060 bool isDriverLoaded, int64_t driverLoadingTime) {
Yiwei Zhangfdd7e782020-01-31 15:59:34 -080061 mGpuStats->insertDriverStats(driverPackageName, driverVersionName, driverVersionCode,
62 driverBuildTime, appPackageName, vulkanVersion, driver,
63 isDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080064}
65
Yiwei Zhangbcba4112019-07-03 13:39:32 -070066void GpuService::setTargetStats(const std::string& appPackageName, const uint64_t driverVersionCode,
67 const GpuStatsInfo::Stats stats, const uint64_t value) {
68 mGpuStats->insertTargetStats(appPackageName, driverVersionCode, stats, value);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -070069}
70
Peiyong Lin0acbfdc2020-06-17 18:47:12 -070071void GpuService::setUpdatableDriverPath(const std::string& driverPath) {
72 developerDriverPath = driverPath;
73}
74
75std::string GpuService::getUpdatableDriverPath() {
76 return developerDriverPath;
77}
78
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080079status_t GpuService::shellCommand(int /*in*/, int out, int err, std::vector<String16>& args) {
80 ATRACE_CALL();
81
82 ALOGV("shellCommand");
Jesse Hall8b0d55e2016-03-31 19:29:36 -070083 for (size_t i = 0, n = args.size(); i < n; i++)
84 ALOGV(" arg[%zu]: '%s'", i, String8(args[i]).string());
85
Jesse Hall3e26b132016-12-20 14:31:28 -080086 if (args.size() >= 1) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080087 if (args[0] == String16("vkjson")) return cmdVkjson(out, err);
88 if (args[0] == String16("help")) return cmdHelp(out);
Jesse Hall3e26b132016-12-20 14:31:28 -080089 }
90 // no command, or unrecognized command
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080091 cmdHelp(err);
Jesse Hall3e26b132016-12-20 14:31:28 -080092 return BAD_VALUE;
Jesse Hallfc038bd2016-03-26 22:20:22 -070093}
94
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080095status_t GpuService::doDump(int fd, const Vector<String16>& args, bool /*asProto*/) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080096 std::string result;
97
98 IPCThreadState* ipc = IPCThreadState::self();
99 const int pid = ipc->getCallingPid();
100 const int uid = ipc->getCallingUid();
101
102 if ((uid != AID_SHELL) && !PermissionCache::checkPermission(sDump, pid, uid)) {
103 StringAppendF(&result, "Permission Denial: can't dump gpu from pid=%d, uid=%d\n", pid, uid);
104 } else {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800105 bool dumpAll = true;
Mikael Pessa3d146052019-07-09 09:25:38 -0700106 bool dumpDriverInfo = false;
Yiwei Zhang252e5f62020-02-19 15:44:17 -0800107 bool dumpMem = false;
Mikael Pessa3d146052019-07-09 09:25:38 -0700108 bool dumpStats = false;
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800109 size_t numArgs = args.size();
110
111 if (numArgs) {
Mikael Pessa3d146052019-07-09 09:25:38 -0700112 for (size_t index = 0; index < numArgs; ++index) {
113 if (args[index] == String16("--gpustats")) {
114 dumpStats = true;
115 } else if (args[index] == String16("--gpudriverinfo")) {
116 dumpDriverInfo = true;
Yiwei Zhang252e5f62020-02-19 15:44:17 -0800117 } else if (args[index] == String16("--gpumem")) {
118 dumpMem = true;
Mikael Pessa3d146052019-07-09 09:25:38 -0700119 }
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800120 }
Yiwei Zhangf881dd32020-06-07 17:00:21 -0700121 dumpAll = !(dumpDriverInfo || dumpMem || dumpStats);
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800122 }
123
Mikael Pessa3d146052019-07-09 09:25:38 -0700124 if (dumpAll || dumpDriverInfo) {
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800125 dumpGameDriverInfo(&result);
126 result.append("\n");
Mikael Pessa3d146052019-07-09 09:25:38 -0700127 }
Yiwei Zhang252e5f62020-02-19 15:44:17 -0800128 if (dumpAll || dumpMem) {
129 mGpuMem->dump(args, &result);
130 result.append("\n");
131 }
Mikael Pessa3d146052019-07-09 09:25:38 -0700132 if (dumpAll || dumpStats) {
Yiwei Zhang8cf7c7b2019-08-28 13:07:30 -0700133 mGpuStats->dump(args, &result);
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800134 result.append("\n");
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800135 }
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800136 }
137
138 write(fd, result.c_str(), result.size());
139 return NO_ERROR;
140}
141
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700142namespace {
143
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800144status_t cmdHelp(int out) {
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700145 FILE* outs = fdopen(out, "w");
146 if (!outs) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800147 ALOGE("vkjson: failed to create out stream: %s (%d)", strerror(errno), errno);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700148 return BAD_VALUE;
149 }
150 fprintf(outs,
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800151 "GPU Service commands:\n"
152 " vkjson dump Vulkan properties as JSON\n");
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700153 fclose(outs);
154 return NO_ERROR;
155}
156
Jesse Hall3841bf42016-06-12 15:08:28 -0700157void vkjsonPrint(FILE* out) {
158 std::string json = VkJsonInstanceToJson(VkJsonGetInstance());
159 fwrite(json.data(), 1, json.size(), out);
160 fputc('\n', out);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700161}
162
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800163status_t cmdVkjson(int out, int /*err*/) {
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700164 FILE* outs = fdopen(out, "w");
165 if (!outs) {
Jesse Hall3841bf42016-06-12 15:08:28 -0700166 int errnum = errno;
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700167 ALOGE("vkjson: failed to create output stream: %s", strerror(errnum));
168 return -errnum;
169 }
Jesse Hall3841bf42016-06-12 15:08:28 -0700170 vkjsonPrint(outs);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700171 fclose(outs);
Jesse Hall3841bf42016-06-12 15:08:28 -0700172 return NO_ERROR;
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700173}
174
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800175void dumpGameDriverInfo(std::string* result) {
176 if (!result) return;
177
178 char stableGameDriver[PROPERTY_VALUE_MAX] = {};
179 property_get("ro.gfx.driver.0", stableGameDriver, "unsupported");
180 StringAppendF(result, "Stable Game Driver: %s\n", stableGameDriver);
181
182 char preReleaseGameDriver[PROPERTY_VALUE_MAX] = {};
183 property_get("ro.gfx.driver.1", preReleaseGameDriver, "unsupported");
184 StringAppendF(result, "Pre-release Game Driver: %s\n", preReleaseGameDriver);
185}
186
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700187} // anonymous namespace
188
Jesse Hallfc038bd2016-03-26 22:20:22 -0700189} // namespace android