blob: a481c628aaac22075ef5c0e2b22a94dec0bc6910 [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
sergiuferentz3c00cbc2023-06-26 18:01:47 +000019#include "gpuservice/GpuService.h"
Jesse Hallfc038bd2016-03-26 22:20:22 -070020
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>
Trevor David Blackf2b7c862024-02-15 23:12:22 +000036#include <vkprofiles.h>
Jesse Hallfc038bd2016-03-26 22:20:22 -070037
Yiwei Zhanga265b402020-06-25 22:02:29 -070038#include <thread>
sergiuferentz3c00cbc2023-06-26 18:01:47 +000039#include <memory>
Yiwei Zhanga265b402020-06-25 22:02:29 -070040
Jesse Hallfc038bd2016-03-26 22:20:22 -070041namespace android {
42
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080043using base::StringAppendF;
Jesse Hallfc038bd2016-03-26 22:20:22 -070044
Jesse Hall8b0d55e2016-03-31 19:29:36 -070045namespace {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080046status_t cmdHelp(int out);
47status_t cmdVkjson(int out, int err);
Trevor David Blackf2b7c862024-02-15 23:12:22 +000048status_t cmdVkprofiles(int out, int err);
Yiwei Zhang2a61c152019-02-27 11:27:18 -080049void dumpGameDriverInfo(std::string* result);
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080050} // namespace
51
52const String16 sDump("android.permission.DUMP");
Yuxin Hub69e9882023-04-13 03:51:41 +000053const String16 sAccessGpuServicePermission("android.permission.ACCESS_GPU_SERVICE");
54const std::string sAngleGlesDriverSuffix = "angle";
Jesse Hall8b0d55e2016-03-31 19:29:36 -070055
Yiwei Zhang423d0802018-11-16 10:56:12 -080056const char* const GpuService::SERVICE_NAME = "gpu";
Jesse Hallfc038bd2016-03-26 22:20:22 -070057
Yiwei Zhang252e5f62020-02-19 15:44:17 -080058GpuService::GpuService()
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -070059 : mGpuMem(std::make_shared<GpuMem>()),
Paul Thomsonf44c6b82021-12-01 13:53:58 +000060 mGpuWork(std::make_shared<gpuwork::GpuWork>()),
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -070061 mGpuStats(std::make_unique<GpuStats>()),
62 mGpuMemTracer(std::make_unique<GpuMemTracer>()) {
Paul Thomsonf44c6b82021-12-01 13:53:58 +000063
sergiuferentz3c00cbc2023-06-26 18:01:47 +000064 mGpuMemAsyncInitThread = std::make_unique<std::thread>([this] (){
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -070065 mGpuMem->initialize();
66 mGpuMemTracer->initialize(mGpuMem);
67 });
Paul Thomsonf44c6b82021-12-01 13:53:58 +000068
sergiuferentz3c00cbc2023-06-26 18:01:47 +000069 mGpuWorkAsyncInitThread = std::make_unique<std::thread>([this]() {
Paul Thomsonf44c6b82021-12-01 13:53:58 +000070 mGpuWork->initialize();
71 });
Yiwei Zhang252e5f62020-02-19 15:44:17 -080072};
Jesse Hallfc038bd2016-03-26 22:20:22 -070073
sergiuferentz3c00cbc2023-06-26 18:01:47 +000074GpuService::~GpuService() {
Steven Moreland52361b42024-04-26 21:53:06 +000075 mGpuMem->stop();
76 mGpuWork->stop();
77
sergiuferentz3c00cbc2023-06-26 18:01:47 +000078 mGpuWorkAsyncInitThread->join();
79 mGpuMemAsyncInitThread->join();
80}
81
Greg Kaiser210bb7e2019-02-12 12:40:05 -080082void GpuService::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 11:51:55 -080083 const std::string& driverVersionName, uint64_t driverVersionCode,
Yiwei Zhang96c01712019-02-19 16:00:25 -080084 int64_t driverBuildTime, const std::string& appPackageName,
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -070085 const int32_t vulkanVersion, GpuStatsInfo::Driver driver,
Yiwei Zhang794d2952019-05-06 17:43:59 -070086 bool isDriverLoaded, int64_t driverLoadingTime) {
Yiwei Zhangfdd7e782020-01-31 15:59:34 -080087 mGpuStats->insertDriverStats(driverPackageName, driverVersionName, driverVersionCode,
88 driverBuildTime, appPackageName, vulkanVersion, driver,
89 isDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080090}
91
Yiwei Zhangbcba4112019-07-03 13:39:32 -070092void GpuService::setTargetStats(const std::string& appPackageName, const uint64_t driverVersionCode,
93 const GpuStatsInfo::Stats stats, const uint64_t value) {
94 mGpuStats->insertTargetStats(appPackageName, driverVersionCode, stats, value);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -070095}
96
Serdar Kocdemirb2901c92022-11-17 00:39:05 +000097void GpuService::setTargetStatsArray(const std::string& appPackageName,
98 const uint64_t driverVersionCode, const GpuStatsInfo::Stats stats,
99 const uint64_t* values, const uint32_t valueCount) {
100 mGpuStats->insertTargetStatsArray(appPackageName, driverVersionCode, stats, values, valueCount);
101}
102
Yuxin Hub69e9882023-04-13 03:51:41 +0000103void GpuService::toggleAngleAsSystemDriver(bool enabled) {
104 IPCThreadState* ipc = IPCThreadState::self();
105 const int pid = ipc->getCallingPid();
106 const int uid = ipc->getCallingUid();
107
108 // only system_server with the ACCESS_GPU_SERVICE permission is allowed to set
109 // persist.graphics.egl
110 if (uid != AID_SYSTEM ||
111 !PermissionCache::checkPermission(sAccessGpuServicePermission, pid, uid)) {
112 ALOGE("Permission Denial: can't set persist.graphics.egl from setAngleAsSystemDriver() "
113 "pid=%d, uid=%d\n", pid, uid);
114 return;
115 }
116
117 std::lock_guard<std::mutex> lock(mLock);
118 if (enabled) {
119 android::base::SetProperty("persist.graphics.egl", sAngleGlesDriverSuffix);
120 } else {
121 android::base::SetProperty("persist.graphics.egl", "");
122 }
123}
124
125
Peiyong Lin0acbfdc2020-06-17 18:47:12 -0700126void GpuService::setUpdatableDriverPath(const std::string& driverPath) {
Yiwei Zhangc7cdd052020-07-28 23:11:21 -0700127 IPCThreadState* ipc = IPCThreadState::self();
128 const int pid = ipc->getCallingPid();
129 const int uid = ipc->getCallingUid();
130
131 // only system_server is allowed to set updatable driver path
132 if (uid != AID_SYSTEM) {
133 ALOGE("Permission Denial: can't set updatable driver path from pid=%d, uid=%d\n", pid, uid);
134 return;
135 }
136
137 std::lock_guard<std::mutex> lock(mLock);
138 mDeveloperDriverPath = driverPath;
Peiyong Lin0acbfdc2020-06-17 18:47:12 -0700139}
140
141std::string GpuService::getUpdatableDriverPath() {
Yiwei Zhangc7cdd052020-07-28 23:11:21 -0700142 std::lock_guard<std::mutex> lock(mLock);
143 return mDeveloperDriverPath;
Peiyong Lin0acbfdc2020-06-17 18:47:12 -0700144}
145
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800146status_t GpuService::shellCommand(int /*in*/, int out, int err, std::vector<String16>& args) {
147 ATRACE_CALL();
148
149 ALOGV("shellCommand");
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700150 for (size_t i = 0, n = args.size(); i < n; i++)
Tomasz Wasilczykb19fe0c2023-08-11 00:06:51 +0000151 ALOGV(" arg[%zu]: '%s'", i, String8(args[i]).c_str());
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700152
Jesse Hall3e26b132016-12-20 14:31:28 -0800153 if (args.size() >= 1) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800154 if (args[0] == String16("vkjson")) return cmdVkjson(out, err);
Trevor David Blackf2b7c862024-02-15 23:12:22 +0000155 if (args[0] == String16("vkprofiles")) return cmdVkprofiles(out, err);
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800156 if (args[0] == String16("help")) return cmdHelp(out);
Jesse Hall3e26b132016-12-20 14:31:28 -0800157 }
158 // no command, or unrecognized command
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800159 cmdHelp(err);
Jesse Hall3e26b132016-12-20 14:31:28 -0800160 return BAD_VALUE;
Jesse Hallfc038bd2016-03-26 22:20:22 -0700161}
162
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800163status_t GpuService::doDump(int fd, const Vector<String16>& args, bool /*asProto*/) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800164 std::string result;
165
166 IPCThreadState* ipc = IPCThreadState::self();
167 const int pid = ipc->getCallingPid();
168 const int uid = ipc->getCallingUid();
169
170 if ((uid != AID_SHELL) && !PermissionCache::checkPermission(sDump, pid, uid)) {
171 StringAppendF(&result, "Permission Denial: can't dump gpu from pid=%d, uid=%d\n", pid, uid);
172 } else {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800173 bool dumpAll = true;
Mikael Pessa3d146052019-07-09 09:25:38 -0700174 bool dumpDriverInfo = false;
Yiwei Zhang252e5f62020-02-19 15:44:17 -0800175 bool dumpMem = false;
Mikael Pessa3d146052019-07-09 09:25:38 -0700176 bool dumpStats = false;
Paul Thomsonf44c6b82021-12-01 13:53:58 +0000177 bool dumpWork = false;
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800178 size_t numArgs = args.size();
179
180 if (numArgs) {
Mikael Pessa3d146052019-07-09 09:25:38 -0700181 for (size_t index = 0; index < numArgs; ++index) {
182 if (args[index] == String16("--gpustats")) {
183 dumpStats = true;
184 } else if (args[index] == String16("--gpudriverinfo")) {
185 dumpDriverInfo = true;
Yiwei Zhang252e5f62020-02-19 15:44:17 -0800186 } else if (args[index] == String16("--gpumem")) {
187 dumpMem = true;
Paul Thomsonf44c6b82021-12-01 13:53:58 +0000188 } else if (args[index] == String16("--gpuwork")) {
189 dumpWork = true;
Mikael Pessa3d146052019-07-09 09:25:38 -0700190 }
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800191 }
Paul Thomsonf44c6b82021-12-01 13:53:58 +0000192 dumpAll = !(dumpDriverInfo || dumpMem || dumpStats || dumpWork);
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800193 }
194
Mikael Pessa3d146052019-07-09 09:25:38 -0700195 if (dumpAll || dumpDriverInfo) {
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800196 dumpGameDriverInfo(&result);
197 result.append("\n");
Mikael Pessa3d146052019-07-09 09:25:38 -0700198 }
Yiwei Zhang252e5f62020-02-19 15:44:17 -0800199 if (dumpAll || dumpMem) {
200 mGpuMem->dump(args, &result);
201 result.append("\n");
202 }
Mikael Pessa3d146052019-07-09 09:25:38 -0700203 if (dumpAll || dumpStats) {
Yiwei Zhang8cf7c7b2019-08-28 13:07:30 -0700204 mGpuStats->dump(args, &result);
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800205 result.append("\n");
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800206 }
Paul Thomsonf44c6b82021-12-01 13:53:58 +0000207 if (dumpAll || dumpWork) {
208 mGpuWork->dump(args, &result);
209 result.append("\n");
210 }
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800211 }
212
213 write(fd, result.c_str(), result.size());
214 return NO_ERROR;
215}
216
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700217namespace {
218
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800219status_t cmdHelp(int out) {
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700220 FILE* outs = fdopen(out, "w");
221 if (!outs) {
Trevor David Blackf2b7c862024-02-15 23:12:22 +0000222 ALOGE("gpuservice: failed to create out stream: %s (%d)", strerror(errno), errno);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700223 return BAD_VALUE;
224 }
225 fprintf(outs,
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800226 "GPU Service commands:\n"
Trevor David Blackf2b7c862024-02-15 23:12:22 +0000227 " vkjson dump Vulkan properties as JSON\n"
228 " vkprofiles print support for select Vulkan profiles\n");
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700229 fclose(outs);
230 return NO_ERROR;
231}
232
Trevor David Blackf2b7c862024-02-15 23:12:22 +0000233status_t cmdVkjson(int out, int /*err*/) {
234 dprintf(out, "%s\n", VkJsonInstanceToJson(VkJsonGetInstance()).c_str());
235 return NO_ERROR;
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700236}
237
Trevor David Blackf2b7c862024-02-15 23:12:22 +0000238status_t cmdVkprofiles(int out, int /*err*/) {
239 dprintf(out, "%s\n", android::vkprofiles::vkProfiles().c_str());
Jesse Hall3841bf42016-06-12 15:08:28 -0700240 return NO_ERROR;
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700241}
242
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800243void dumpGameDriverInfo(std::string* result) {
244 if (!result) return;
245
246 char stableGameDriver[PROPERTY_VALUE_MAX] = {};
247 property_get("ro.gfx.driver.0", stableGameDriver, "unsupported");
248 StringAppendF(result, "Stable Game Driver: %s\n", stableGameDriver);
249
250 char preReleaseGameDriver[PROPERTY_VALUE_MAX] = {};
251 property_get("ro.gfx.driver.1", preReleaseGameDriver, "unsupported");
252 StringAppendF(result, "Pre-release Game Driver: %s\n", preReleaseGameDriver);
253}
254
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700255} // anonymous namespace
256
Jesse Hallfc038bd2016-03-26 22:20:22 -0700257} // namespace android