blob: 7b9782f4e8972debbae12326760ff70c3a61de5c [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>
Paul Thomsonf44c6b82021-12-01 13:53:58 +000028#include <gpuwork/GpuWork.h>
Yiwei Zhangbaaef882020-02-02 17:45:30 -080029#include <gpustats/GpuStats.h>
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080030#include <private/android_filesystem_config.h>
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -070031#include <tracing/GpuMemTracer.h>
Jesse Hall8b0d55e2016-03-31 19:29:36 -070032#include <utils/String8.h>
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080033#include <utils/Trace.h>
Jesse Hall8b0d55e2016-03-31 19:29:36 -070034#include <vkjson.h>
Jesse Hallfc038bd2016-03-26 22:20:22 -070035
Yiwei Zhanga265b402020-06-25 22:02:29 -070036#include <thread>
37
Jesse Hallfc038bd2016-03-26 22:20:22 -070038namespace android {
39
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080040using base::StringAppendF;
Jesse Hallfc038bd2016-03-26 22:20:22 -070041
Jesse Hall8b0d55e2016-03-31 19:29:36 -070042namespace {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080043status_t cmdHelp(int out);
44status_t cmdVkjson(int out, int err);
Yiwei Zhang2a61c152019-02-27 11:27:18 -080045void dumpGameDriverInfo(std::string* result);
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080046} // namespace
47
48const String16 sDump("android.permission.DUMP");
Jesse Hall8b0d55e2016-03-31 19:29:36 -070049
Yiwei Zhang423d0802018-11-16 10:56:12 -080050const char* const GpuService::SERVICE_NAME = "gpu";
Jesse Hallfc038bd2016-03-26 22:20:22 -070051
Yiwei Zhang252e5f62020-02-19 15:44:17 -080052GpuService::GpuService()
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -070053 : mGpuMem(std::make_shared<GpuMem>()),
Paul Thomsonf44c6b82021-12-01 13:53:58 +000054 mGpuWork(std::make_shared<gpuwork::GpuWork>()),
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -070055 mGpuStats(std::make_unique<GpuStats>()),
56 mGpuMemTracer(std::make_unique<GpuMemTracer>()) {
Paul Thomsonf44c6b82021-12-01 13:53:58 +000057
58 std::thread gpuMemAsyncInitThread([this]() {
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -070059 mGpuMem->initialize();
60 mGpuMemTracer->initialize(mGpuMem);
61 });
Paul Thomsonf44c6b82021-12-01 13:53:58 +000062 gpuMemAsyncInitThread.detach();
63
64 std::thread gpuWorkAsyncInitThread([this]() {
65 mGpuWork->initialize();
66 });
67 gpuWorkAsyncInitThread.detach();
Yiwei Zhang252e5f62020-02-19 15:44:17 -080068};
Jesse Hallfc038bd2016-03-26 22:20:22 -070069
Greg Kaiser210bb7e2019-02-12 12:40:05 -080070void GpuService::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 11:51:55 -080071 const std::string& driverVersionName, uint64_t driverVersionCode,
Yiwei Zhang96c01712019-02-19 16:00:25 -080072 int64_t driverBuildTime, const std::string& appPackageName,
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -070073 const int32_t vulkanVersion, GpuStatsInfo::Driver driver,
Yiwei Zhang794d2952019-05-06 17:43:59 -070074 bool isDriverLoaded, int64_t driverLoadingTime) {
Yiwei Zhangfdd7e782020-01-31 15:59:34 -080075 mGpuStats->insertDriverStats(driverPackageName, driverVersionName, driverVersionCode,
76 driverBuildTime, appPackageName, vulkanVersion, driver,
77 isDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080078}
79
Yiwei Zhangbcba4112019-07-03 13:39:32 -070080void GpuService::setTargetStats(const std::string& appPackageName, const uint64_t driverVersionCode,
81 const GpuStatsInfo::Stats stats, const uint64_t value) {
82 mGpuStats->insertTargetStats(appPackageName, driverVersionCode, stats, value);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -070083}
84
Peiyong Lin0acbfdc2020-06-17 18:47:12 -070085void GpuService::setUpdatableDriverPath(const std::string& driverPath) {
Yiwei Zhangc7cdd052020-07-28 23:11:21 -070086 IPCThreadState* ipc = IPCThreadState::self();
87 const int pid = ipc->getCallingPid();
88 const int uid = ipc->getCallingUid();
89
90 // only system_server is allowed to set updatable driver path
91 if (uid != AID_SYSTEM) {
92 ALOGE("Permission Denial: can't set updatable driver path from pid=%d, uid=%d\n", pid, uid);
93 return;
94 }
95
96 std::lock_guard<std::mutex> lock(mLock);
97 mDeveloperDriverPath = driverPath;
Peiyong Lin0acbfdc2020-06-17 18:47:12 -070098}
99
100std::string GpuService::getUpdatableDriverPath() {
Yiwei Zhangc7cdd052020-07-28 23:11:21 -0700101 std::lock_guard<std::mutex> lock(mLock);
102 return mDeveloperDriverPath;
Peiyong Lin0acbfdc2020-06-17 18:47:12 -0700103}
104
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800105status_t GpuService::shellCommand(int /*in*/, int out, int err, std::vector<String16>& args) {
106 ATRACE_CALL();
107
108 ALOGV("shellCommand");
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700109 for (size_t i = 0, n = args.size(); i < n; i++)
110 ALOGV(" arg[%zu]: '%s'", i, String8(args[i]).string());
111
Jesse Hall3e26b132016-12-20 14:31:28 -0800112 if (args.size() >= 1) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800113 if (args[0] == String16("vkjson")) return cmdVkjson(out, err);
114 if (args[0] == String16("help")) return cmdHelp(out);
Jesse Hall3e26b132016-12-20 14:31:28 -0800115 }
116 // no command, or unrecognized command
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800117 cmdHelp(err);
Jesse Hall3e26b132016-12-20 14:31:28 -0800118 return BAD_VALUE;
Jesse Hallfc038bd2016-03-26 22:20:22 -0700119}
120
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800121status_t GpuService::doDump(int fd, const Vector<String16>& args, bool /*asProto*/) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800122 std::string result;
123
124 IPCThreadState* ipc = IPCThreadState::self();
125 const int pid = ipc->getCallingPid();
126 const int uid = ipc->getCallingUid();
127
128 if ((uid != AID_SHELL) && !PermissionCache::checkPermission(sDump, pid, uid)) {
129 StringAppendF(&result, "Permission Denial: can't dump gpu from pid=%d, uid=%d\n", pid, uid);
130 } else {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800131 bool dumpAll = true;
Mikael Pessa3d146052019-07-09 09:25:38 -0700132 bool dumpDriverInfo = false;
Yiwei Zhang252e5f62020-02-19 15:44:17 -0800133 bool dumpMem = false;
Mikael Pessa3d146052019-07-09 09:25:38 -0700134 bool dumpStats = false;
Paul Thomsonf44c6b82021-12-01 13:53:58 +0000135 bool dumpWork = false;
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800136 size_t numArgs = args.size();
137
138 if (numArgs) {
Mikael Pessa3d146052019-07-09 09:25:38 -0700139 for (size_t index = 0; index < numArgs; ++index) {
140 if (args[index] == String16("--gpustats")) {
141 dumpStats = true;
142 } else if (args[index] == String16("--gpudriverinfo")) {
143 dumpDriverInfo = true;
Yiwei Zhang252e5f62020-02-19 15:44:17 -0800144 } else if (args[index] == String16("--gpumem")) {
145 dumpMem = true;
Paul Thomsonf44c6b82021-12-01 13:53:58 +0000146 } else if (args[index] == String16("--gpuwork")) {
147 dumpWork = true;
Mikael Pessa3d146052019-07-09 09:25:38 -0700148 }
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800149 }
Paul Thomsonf44c6b82021-12-01 13:53:58 +0000150 dumpAll = !(dumpDriverInfo || dumpMem || dumpStats || dumpWork);
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800151 }
152
Mikael Pessa3d146052019-07-09 09:25:38 -0700153 if (dumpAll || dumpDriverInfo) {
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800154 dumpGameDriverInfo(&result);
155 result.append("\n");
Mikael Pessa3d146052019-07-09 09:25:38 -0700156 }
Yiwei Zhang252e5f62020-02-19 15:44:17 -0800157 if (dumpAll || dumpMem) {
158 mGpuMem->dump(args, &result);
159 result.append("\n");
160 }
Mikael Pessa3d146052019-07-09 09:25:38 -0700161 if (dumpAll || dumpStats) {
Yiwei Zhang8cf7c7b2019-08-28 13:07:30 -0700162 mGpuStats->dump(args, &result);
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800163 result.append("\n");
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800164 }
Paul Thomsonf44c6b82021-12-01 13:53:58 +0000165 if (dumpAll || dumpWork) {
166 mGpuWork->dump(args, &result);
167 result.append("\n");
168 }
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800169 }
170
171 write(fd, result.c_str(), result.size());
172 return NO_ERROR;
173}
174
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700175namespace {
176
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800177status_t cmdHelp(int out) {
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700178 FILE* outs = fdopen(out, "w");
179 if (!outs) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800180 ALOGE("vkjson: failed to create out stream: %s (%d)", strerror(errno), errno);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700181 return BAD_VALUE;
182 }
183 fprintf(outs,
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800184 "GPU Service commands:\n"
185 " vkjson dump Vulkan properties as JSON\n");
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700186 fclose(outs);
187 return NO_ERROR;
188}
189
Jesse Hall3841bf42016-06-12 15:08:28 -0700190void vkjsonPrint(FILE* out) {
191 std::string json = VkJsonInstanceToJson(VkJsonGetInstance());
192 fwrite(json.data(), 1, json.size(), out);
193 fputc('\n', out);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700194}
195
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800196status_t cmdVkjson(int out, int /*err*/) {
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700197 FILE* outs = fdopen(out, "w");
198 if (!outs) {
Jesse Hall3841bf42016-06-12 15:08:28 -0700199 int errnum = errno;
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700200 ALOGE("vkjson: failed to create output stream: %s", strerror(errnum));
201 return -errnum;
202 }
Jesse Hall3841bf42016-06-12 15:08:28 -0700203 vkjsonPrint(outs);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700204 fclose(outs);
Jesse Hall3841bf42016-06-12 15:08:28 -0700205 return NO_ERROR;
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700206}
207
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800208void dumpGameDriverInfo(std::string* result) {
209 if (!result) return;
210
211 char stableGameDriver[PROPERTY_VALUE_MAX] = {};
212 property_get("ro.gfx.driver.0", stableGameDriver, "unsupported");
213 StringAppendF(result, "Stable Game Driver: %s\n", stableGameDriver);
214
215 char preReleaseGameDriver[PROPERTY_VALUE_MAX] = {};
216 property_get("ro.gfx.driver.1", preReleaseGameDriver, "unsupported");
217 StringAppendF(result, "Pre-release Game Driver: %s\n", preReleaseGameDriver);
218}
219
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700220} // anonymous namespace
221
Jesse Hallfc038bd2016-03-26 22:20:22 -0700222} // namespace android