blob: 5e7b2e8df84ea603623d7cd78142760a930dadbd [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>
Yuxin Hub69e9882023-04-13 03:51:41 +000022#include <android-base/properties.h>
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080023#include <binder/IPCThreadState.h>
Jesse Hallc0b15772016-12-20 14:47:45 -080024#include <binder/IResultReceiver.h>
Jesse Hallfc038bd2016-03-26 22:20:22 -070025#include <binder/Parcel.h>
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080026#include <binder/PermissionCache.h>
Yiwei Zhang2a61c152019-02-27 11:27:18 -080027#include <cutils/properties.h>
Yiwei Zhang252e5f62020-02-19 15:44:17 -080028#include <gpumem/GpuMem.h>
Paul Thomsonf44c6b82021-12-01 13:53:58 +000029#include <gpuwork/GpuWork.h>
Yiwei Zhangbaaef882020-02-02 17:45:30 -080030#include <gpustats/GpuStats.h>
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080031#include <private/android_filesystem_config.h>
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -070032#include <tracing/GpuMemTracer.h>
Jesse Hall8b0d55e2016-03-31 19:29:36 -070033#include <utils/String8.h>
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080034#include <utils/Trace.h>
Jesse Hall8b0d55e2016-03-31 19:29:36 -070035#include <vkjson.h>
Jesse Hallfc038bd2016-03-26 22:20:22 -070036
Yiwei Zhanga265b402020-06-25 22:02:29 -070037#include <thread>
38
Jesse Hallfc038bd2016-03-26 22:20:22 -070039namespace android {
40
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080041using base::StringAppendF;
Jesse Hallfc038bd2016-03-26 22:20:22 -070042
Jesse Hall8b0d55e2016-03-31 19:29:36 -070043namespace {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080044status_t cmdHelp(int out);
45status_t cmdVkjson(int out, int err);
Yiwei Zhang2a61c152019-02-27 11:27:18 -080046void dumpGameDriverInfo(std::string* result);
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080047} // namespace
48
49const String16 sDump("android.permission.DUMP");
Yuxin Hub69e9882023-04-13 03:51:41 +000050const String16 sAccessGpuServicePermission("android.permission.ACCESS_GPU_SERVICE");
51const std::string sAngleGlesDriverSuffix = "angle";
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 Zhang252e5f62020-02-19 15:44:17 -080055GpuService::GpuService()
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -070056 : mGpuMem(std::make_shared<GpuMem>()),
Paul Thomsonf44c6b82021-12-01 13:53:58 +000057 mGpuWork(std::make_shared<gpuwork::GpuWork>()),
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -070058 mGpuStats(std::make_unique<GpuStats>()),
59 mGpuMemTracer(std::make_unique<GpuMemTracer>()) {
Paul Thomsonf44c6b82021-12-01 13:53:58 +000060
61 std::thread gpuMemAsyncInitThread([this]() {
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -070062 mGpuMem->initialize();
63 mGpuMemTracer->initialize(mGpuMem);
64 });
Paul Thomsonf44c6b82021-12-01 13:53:58 +000065 gpuMemAsyncInitThread.detach();
66
67 std::thread gpuWorkAsyncInitThread([this]() {
68 mGpuWork->initialize();
69 });
70 gpuWorkAsyncInitThread.detach();
Yiwei Zhang252e5f62020-02-19 15:44:17 -080071};
Jesse Hallfc038bd2016-03-26 22:20:22 -070072
Greg Kaiser210bb7e2019-02-12 12:40:05 -080073void GpuService::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 11:51:55 -080074 const std::string& driverVersionName, uint64_t driverVersionCode,
Yiwei Zhang96c01712019-02-19 16:00:25 -080075 int64_t driverBuildTime, const std::string& appPackageName,
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -070076 const int32_t vulkanVersion, GpuStatsInfo::Driver driver,
Yiwei Zhang794d2952019-05-06 17:43:59 -070077 bool isDriverLoaded, int64_t driverLoadingTime) {
Yiwei Zhangfdd7e782020-01-31 15:59:34 -080078 mGpuStats->insertDriverStats(driverPackageName, driverVersionName, driverVersionCode,
79 driverBuildTime, appPackageName, vulkanVersion, driver,
80 isDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080081}
82
Yiwei Zhangbcba4112019-07-03 13:39:32 -070083void GpuService::setTargetStats(const std::string& appPackageName, const uint64_t driverVersionCode,
84 const GpuStatsInfo::Stats stats, const uint64_t value) {
85 mGpuStats->insertTargetStats(appPackageName, driverVersionCode, stats, value);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -070086}
87
Serdar Kocdemirb2901c92022-11-17 00:39:05 +000088void GpuService::setTargetStatsArray(const std::string& appPackageName,
89 const uint64_t driverVersionCode, const GpuStatsInfo::Stats stats,
90 const uint64_t* values, const uint32_t valueCount) {
91 mGpuStats->insertTargetStatsArray(appPackageName, driverVersionCode, stats, values, valueCount);
92}
93
Yuxin Hub69e9882023-04-13 03:51:41 +000094void GpuService::toggleAngleAsSystemDriver(bool enabled) {
95 IPCThreadState* ipc = IPCThreadState::self();
96 const int pid = ipc->getCallingPid();
97 const int uid = ipc->getCallingUid();
98
99 // only system_server with the ACCESS_GPU_SERVICE permission is allowed to set
100 // persist.graphics.egl
101 if (uid != AID_SYSTEM ||
102 !PermissionCache::checkPermission(sAccessGpuServicePermission, pid, uid)) {
103 ALOGE("Permission Denial: can't set persist.graphics.egl from setAngleAsSystemDriver() "
104 "pid=%d, uid=%d\n", pid, uid);
105 return;
106 }
107
108 std::lock_guard<std::mutex> lock(mLock);
109 if (enabled) {
110 android::base::SetProperty("persist.graphics.egl", sAngleGlesDriverSuffix);
111 } else {
112 android::base::SetProperty("persist.graphics.egl", "");
113 }
114}
115
116
Peiyong Lin0acbfdc2020-06-17 18:47:12 -0700117void GpuService::setUpdatableDriverPath(const std::string& driverPath) {
Yiwei Zhangc7cdd052020-07-28 23:11:21 -0700118 IPCThreadState* ipc = IPCThreadState::self();
119 const int pid = ipc->getCallingPid();
120 const int uid = ipc->getCallingUid();
121
122 // only system_server is allowed to set updatable driver path
123 if (uid != AID_SYSTEM) {
124 ALOGE("Permission Denial: can't set updatable driver path from pid=%d, uid=%d\n", pid, uid);
125 return;
126 }
127
128 std::lock_guard<std::mutex> lock(mLock);
129 mDeveloperDriverPath = driverPath;
Peiyong Lin0acbfdc2020-06-17 18:47:12 -0700130}
131
132std::string GpuService::getUpdatableDriverPath() {
Yiwei Zhangc7cdd052020-07-28 23:11:21 -0700133 std::lock_guard<std::mutex> lock(mLock);
134 return mDeveloperDriverPath;
Peiyong Lin0acbfdc2020-06-17 18:47:12 -0700135}
136
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800137status_t GpuService::shellCommand(int /*in*/, int out, int err, std::vector<String16>& args) {
138 ATRACE_CALL();
139
140 ALOGV("shellCommand");
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700141 for (size_t i = 0, n = args.size(); i < n; i++)
142 ALOGV(" arg[%zu]: '%s'", i, String8(args[i]).string());
143
Jesse Hall3e26b132016-12-20 14:31:28 -0800144 if (args.size() >= 1) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800145 if (args[0] == String16("vkjson")) return cmdVkjson(out, err);
146 if (args[0] == String16("help")) return cmdHelp(out);
Jesse Hall3e26b132016-12-20 14:31:28 -0800147 }
148 // no command, or unrecognized command
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800149 cmdHelp(err);
Jesse Hall3e26b132016-12-20 14:31:28 -0800150 return BAD_VALUE;
Jesse Hallfc038bd2016-03-26 22:20:22 -0700151}
152
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800153status_t GpuService::doDump(int fd, const Vector<String16>& args, bool /*asProto*/) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800154 std::string result;
155
156 IPCThreadState* ipc = IPCThreadState::self();
157 const int pid = ipc->getCallingPid();
158 const int uid = ipc->getCallingUid();
159
160 if ((uid != AID_SHELL) && !PermissionCache::checkPermission(sDump, pid, uid)) {
161 StringAppendF(&result, "Permission Denial: can't dump gpu from pid=%d, uid=%d\n", pid, uid);
162 } else {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800163 bool dumpAll = true;
Mikael Pessa3d146052019-07-09 09:25:38 -0700164 bool dumpDriverInfo = false;
Yiwei Zhang252e5f62020-02-19 15:44:17 -0800165 bool dumpMem = false;
Mikael Pessa3d146052019-07-09 09:25:38 -0700166 bool dumpStats = false;
Paul Thomsonf44c6b82021-12-01 13:53:58 +0000167 bool dumpWork = false;
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800168 size_t numArgs = args.size();
169
170 if (numArgs) {
Mikael Pessa3d146052019-07-09 09:25:38 -0700171 for (size_t index = 0; index < numArgs; ++index) {
172 if (args[index] == String16("--gpustats")) {
173 dumpStats = true;
174 } else if (args[index] == String16("--gpudriverinfo")) {
175 dumpDriverInfo = true;
Yiwei Zhang252e5f62020-02-19 15:44:17 -0800176 } else if (args[index] == String16("--gpumem")) {
177 dumpMem = true;
Paul Thomsonf44c6b82021-12-01 13:53:58 +0000178 } else if (args[index] == String16("--gpuwork")) {
179 dumpWork = true;
Mikael Pessa3d146052019-07-09 09:25:38 -0700180 }
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800181 }
Paul Thomsonf44c6b82021-12-01 13:53:58 +0000182 dumpAll = !(dumpDriverInfo || dumpMem || dumpStats || dumpWork);
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800183 }
184
Mikael Pessa3d146052019-07-09 09:25:38 -0700185 if (dumpAll || dumpDriverInfo) {
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800186 dumpGameDriverInfo(&result);
187 result.append("\n");
Mikael Pessa3d146052019-07-09 09:25:38 -0700188 }
Yiwei Zhang252e5f62020-02-19 15:44:17 -0800189 if (dumpAll || dumpMem) {
190 mGpuMem->dump(args, &result);
191 result.append("\n");
192 }
Mikael Pessa3d146052019-07-09 09:25:38 -0700193 if (dumpAll || dumpStats) {
Yiwei Zhang8cf7c7b2019-08-28 13:07:30 -0700194 mGpuStats->dump(args, &result);
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800195 result.append("\n");
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800196 }
Paul Thomsonf44c6b82021-12-01 13:53:58 +0000197 if (dumpAll || dumpWork) {
198 mGpuWork->dump(args, &result);
199 result.append("\n");
200 }
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800201 }
202
203 write(fd, result.c_str(), result.size());
204 return NO_ERROR;
205}
206
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700207namespace {
208
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800209status_t cmdHelp(int out) {
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700210 FILE* outs = fdopen(out, "w");
211 if (!outs) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800212 ALOGE("vkjson: failed to create out stream: %s (%d)", strerror(errno), errno);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700213 return BAD_VALUE;
214 }
215 fprintf(outs,
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800216 "GPU Service commands:\n"
217 " vkjson dump Vulkan properties as JSON\n");
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700218 fclose(outs);
219 return NO_ERROR;
220}
221
Jesse Hall3841bf42016-06-12 15:08:28 -0700222void vkjsonPrint(FILE* out) {
223 std::string json = VkJsonInstanceToJson(VkJsonGetInstance());
224 fwrite(json.data(), 1, json.size(), out);
225 fputc('\n', out);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700226}
227
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800228status_t cmdVkjson(int out, int /*err*/) {
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700229 FILE* outs = fdopen(out, "w");
230 if (!outs) {
Jesse Hall3841bf42016-06-12 15:08:28 -0700231 int errnum = errno;
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700232 ALOGE("vkjson: failed to create output stream: %s", strerror(errnum));
233 return -errnum;
234 }
Jesse Hall3841bf42016-06-12 15:08:28 -0700235 vkjsonPrint(outs);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700236 fclose(outs);
Jesse Hall3841bf42016-06-12 15:08:28 -0700237 return NO_ERROR;
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700238}
239
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800240void dumpGameDriverInfo(std::string* result) {
241 if (!result) return;
242
243 char stableGameDriver[PROPERTY_VALUE_MAX] = {};
244 property_get("ro.gfx.driver.0", stableGameDriver, "unsupported");
245 StringAppendF(result, "Stable Game Driver: %s\n", stableGameDriver);
246
247 char preReleaseGameDriver[PROPERTY_VALUE_MAX] = {};
248 property_get("ro.gfx.driver.1", preReleaseGameDriver, "unsupported");
249 StringAppendF(result, "Pre-release Game Driver: %s\n", preReleaseGameDriver);
250}
251
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700252} // anonymous namespace
253
Jesse Hallfc038bd2016-03-26 22:20:22 -0700254} // namespace android