blob: aaa8c185088dee9f5f293ef345d68c5d6a5f5f96 [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
Serdar Kocdemirb2901c92022-11-17 00:39:05 +000085void GpuService::setTargetStatsArray(const std::string& appPackageName,
86 const uint64_t driverVersionCode, const GpuStatsInfo::Stats stats,
87 const uint64_t* values, const uint32_t valueCount) {
88 mGpuStats->insertTargetStatsArray(appPackageName, driverVersionCode, stats, values, valueCount);
89}
90
Peiyong Lin0acbfdc2020-06-17 18:47:12 -070091void GpuService::setUpdatableDriverPath(const std::string& driverPath) {
Yiwei Zhangc7cdd052020-07-28 23:11:21 -070092 IPCThreadState* ipc = IPCThreadState::self();
93 const int pid = ipc->getCallingPid();
94 const int uid = ipc->getCallingUid();
95
96 // only system_server is allowed to set updatable driver path
97 if (uid != AID_SYSTEM) {
98 ALOGE("Permission Denial: can't set updatable driver path from pid=%d, uid=%d\n", pid, uid);
99 return;
100 }
101
102 std::lock_guard<std::mutex> lock(mLock);
103 mDeveloperDriverPath = driverPath;
Peiyong Lin0acbfdc2020-06-17 18:47:12 -0700104}
105
106std::string GpuService::getUpdatableDriverPath() {
Yiwei Zhangc7cdd052020-07-28 23:11:21 -0700107 std::lock_guard<std::mutex> lock(mLock);
108 return mDeveloperDriverPath;
Peiyong Lin0acbfdc2020-06-17 18:47:12 -0700109}
110
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800111status_t GpuService::shellCommand(int /*in*/, int out, int err, std::vector<String16>& args) {
112 ATRACE_CALL();
113
114 ALOGV("shellCommand");
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700115 for (size_t i = 0, n = args.size(); i < n; i++)
116 ALOGV(" arg[%zu]: '%s'", i, String8(args[i]).string());
117
Jesse Hall3e26b132016-12-20 14:31:28 -0800118 if (args.size() >= 1) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800119 if (args[0] == String16("vkjson")) return cmdVkjson(out, err);
120 if (args[0] == String16("help")) return cmdHelp(out);
Jesse Hall3e26b132016-12-20 14:31:28 -0800121 }
122 // no command, or unrecognized command
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800123 cmdHelp(err);
Jesse Hall3e26b132016-12-20 14:31:28 -0800124 return BAD_VALUE;
Jesse Hallfc038bd2016-03-26 22:20:22 -0700125}
126
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800127status_t GpuService::doDump(int fd, const Vector<String16>& args, bool /*asProto*/) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800128 std::string result;
129
130 IPCThreadState* ipc = IPCThreadState::self();
131 const int pid = ipc->getCallingPid();
132 const int uid = ipc->getCallingUid();
133
134 if ((uid != AID_SHELL) && !PermissionCache::checkPermission(sDump, pid, uid)) {
135 StringAppendF(&result, "Permission Denial: can't dump gpu from pid=%d, uid=%d\n", pid, uid);
136 } else {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800137 bool dumpAll = true;
Mikael Pessa3d146052019-07-09 09:25:38 -0700138 bool dumpDriverInfo = false;
Yiwei Zhang252e5f62020-02-19 15:44:17 -0800139 bool dumpMem = false;
Mikael Pessa3d146052019-07-09 09:25:38 -0700140 bool dumpStats = false;
Paul Thomsonf44c6b82021-12-01 13:53:58 +0000141 bool dumpWork = false;
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800142 size_t numArgs = args.size();
143
144 if (numArgs) {
Mikael Pessa3d146052019-07-09 09:25:38 -0700145 for (size_t index = 0; index < numArgs; ++index) {
146 if (args[index] == String16("--gpustats")) {
147 dumpStats = true;
148 } else if (args[index] == String16("--gpudriverinfo")) {
149 dumpDriverInfo = true;
Yiwei Zhang252e5f62020-02-19 15:44:17 -0800150 } else if (args[index] == String16("--gpumem")) {
151 dumpMem = true;
Paul Thomsonf44c6b82021-12-01 13:53:58 +0000152 } else if (args[index] == String16("--gpuwork")) {
153 dumpWork = true;
Mikael Pessa3d146052019-07-09 09:25:38 -0700154 }
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800155 }
Paul Thomsonf44c6b82021-12-01 13:53:58 +0000156 dumpAll = !(dumpDriverInfo || dumpMem || dumpStats || dumpWork);
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800157 }
158
Mikael Pessa3d146052019-07-09 09:25:38 -0700159 if (dumpAll || dumpDriverInfo) {
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800160 dumpGameDriverInfo(&result);
161 result.append("\n");
Mikael Pessa3d146052019-07-09 09:25:38 -0700162 }
Yiwei Zhang252e5f62020-02-19 15:44:17 -0800163 if (dumpAll || dumpMem) {
164 mGpuMem->dump(args, &result);
165 result.append("\n");
166 }
Mikael Pessa3d146052019-07-09 09:25:38 -0700167 if (dumpAll || dumpStats) {
Yiwei Zhang8cf7c7b2019-08-28 13:07:30 -0700168 mGpuStats->dump(args, &result);
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800169 result.append("\n");
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800170 }
Paul Thomsonf44c6b82021-12-01 13:53:58 +0000171 if (dumpAll || dumpWork) {
172 mGpuWork->dump(args, &result);
173 result.append("\n");
174 }
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800175 }
176
177 write(fd, result.c_str(), result.size());
178 return NO_ERROR;
179}
180
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700181namespace {
182
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800183status_t cmdHelp(int out) {
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700184 FILE* outs = fdopen(out, "w");
185 if (!outs) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800186 ALOGE("vkjson: failed to create out stream: %s (%d)", strerror(errno), errno);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700187 return BAD_VALUE;
188 }
189 fprintf(outs,
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800190 "GPU Service commands:\n"
191 " vkjson dump Vulkan properties as JSON\n");
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700192 fclose(outs);
193 return NO_ERROR;
194}
195
Jesse Hall3841bf42016-06-12 15:08:28 -0700196void vkjsonPrint(FILE* out) {
197 std::string json = VkJsonInstanceToJson(VkJsonGetInstance());
198 fwrite(json.data(), 1, json.size(), out);
199 fputc('\n', out);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700200}
201
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800202status_t cmdVkjson(int out, int /*err*/) {
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700203 FILE* outs = fdopen(out, "w");
204 if (!outs) {
Jesse Hall3841bf42016-06-12 15:08:28 -0700205 int errnum = errno;
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700206 ALOGE("vkjson: failed to create output stream: %s", strerror(errnum));
207 return -errnum;
208 }
Jesse Hall3841bf42016-06-12 15:08:28 -0700209 vkjsonPrint(outs);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700210 fclose(outs);
Jesse Hall3841bf42016-06-12 15:08:28 -0700211 return NO_ERROR;
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700212}
213
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800214void dumpGameDriverInfo(std::string* result) {
215 if (!result) return;
216
217 char stableGameDriver[PROPERTY_VALUE_MAX] = {};
218 property_get("ro.gfx.driver.0", stableGameDriver, "unsupported");
219 StringAppendF(result, "Stable Game Driver: %s\n", stableGameDriver);
220
221 char preReleaseGameDriver[PROPERTY_VALUE_MAX] = {};
222 property_get("ro.gfx.driver.1", preReleaseGameDriver, "unsupported");
223 StringAppendF(result, "Pre-release Game Driver: %s\n", preReleaseGameDriver);
224}
225
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700226} // anonymous namespace
227
Jesse Hallfc038bd2016-03-26 22:20:22 -0700228} // namespace android