blob: 7fb06a2e30f12a560c98e09a37b96004a9c2e13b [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
sergiuferentz9e094712023-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>
Jesse Hallfc038bd2016-03-26 22:20:22 -070036
Yiwei Zhanga265b402020-06-25 22:02:29 -070037#include <thread>
sergiuferentz9e094712023-06-26 18:01:47 +000038#include <memory>
Yiwei Zhanga265b402020-06-25 22:02:29 -070039
Jesse Hallfc038bd2016-03-26 22:20:22 -070040namespace android {
41
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080042using base::StringAppendF;
Jesse Hallfc038bd2016-03-26 22:20:22 -070043
Jesse Hall8b0d55e2016-03-31 19:29:36 -070044namespace {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080045status_t cmdHelp(int out);
46status_t cmdVkjson(int out, int err);
Yiwei Zhang2a61c152019-02-27 11:27:18 -080047void dumpGameDriverInfo(std::string* result);
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080048} // namespace
49
50const String16 sDump("android.permission.DUMP");
Yuxin Hub69e9882023-04-13 03:51:41 +000051const String16 sAccessGpuServicePermission("android.permission.ACCESS_GPU_SERVICE");
52const std::string sAngleGlesDriverSuffix = "angle";
Jesse Hall8b0d55e2016-03-31 19:29:36 -070053
Yiwei Zhang423d0802018-11-16 10:56:12 -080054const char* const GpuService::SERVICE_NAME = "gpu";
Jesse Hallfc038bd2016-03-26 22:20:22 -070055
Yiwei Zhang252e5f62020-02-19 15:44:17 -080056GpuService::GpuService()
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -070057 : mGpuMem(std::make_shared<GpuMem>()),
Paul Thomsonf44c6b82021-12-01 13:53:58 +000058 mGpuWork(std::make_shared<gpuwork::GpuWork>()),
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -070059 mGpuStats(std::make_unique<GpuStats>()),
60 mGpuMemTracer(std::make_unique<GpuMemTracer>()) {
Paul Thomsonf44c6b82021-12-01 13:53:58 +000061
sergiuferentz9e094712023-06-26 18:01:47 +000062 mGpuMemAsyncInitThread = std::make_unique<std::thread>([this] (){
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -070063 mGpuMem->initialize();
64 mGpuMemTracer->initialize(mGpuMem);
65 });
Paul Thomsonf44c6b82021-12-01 13:53:58 +000066
sergiuferentz9e094712023-06-26 18:01:47 +000067 mGpuWorkAsyncInitThread = std::make_unique<std::thread>([this]() {
Paul Thomsonf44c6b82021-12-01 13:53:58 +000068 mGpuWork->initialize();
69 });
Peiyong Linbcf03f12023-11-06 19:28:00 +000070 property_set("persist.graphics.egl", "");
Yiwei Zhang252e5f62020-02-19 15:44:17 -080071};
Jesse Hallfc038bd2016-03-26 22:20:22 -070072
sergiuferentz9e094712023-06-26 18:01:47 +000073GpuService::~GpuService() {
74 mGpuWorkAsyncInitThread->join();
75 mGpuMemAsyncInitThread->join();
76}
77
Greg Kaiser210bb7e2019-02-12 12:40:05 -080078void GpuService::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 11:51:55 -080079 const std::string& driverVersionName, uint64_t driverVersionCode,
Yiwei Zhang96c01712019-02-19 16:00:25 -080080 int64_t driverBuildTime, const std::string& appPackageName,
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -070081 const int32_t vulkanVersion, GpuStatsInfo::Driver driver,
Yiwei Zhang794d2952019-05-06 17:43:59 -070082 bool isDriverLoaded, int64_t driverLoadingTime) {
Yiwei Zhangfdd7e782020-01-31 15:59:34 -080083 mGpuStats->insertDriverStats(driverPackageName, driverVersionName, driverVersionCode,
84 driverBuildTime, appPackageName, vulkanVersion, driver,
85 isDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080086}
87
Yiwei Zhangbcba4112019-07-03 13:39:32 -070088void GpuService::setTargetStats(const std::string& appPackageName, const uint64_t driverVersionCode,
89 const GpuStatsInfo::Stats stats, const uint64_t value) {
90 mGpuStats->insertTargetStats(appPackageName, driverVersionCode, stats, value);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -070091}
92
Serdar Kocdemirb2901c92022-11-17 00:39:05 +000093void GpuService::setTargetStatsArray(const std::string& appPackageName,
94 const uint64_t driverVersionCode, const GpuStatsInfo::Stats stats,
95 const uint64_t* values, const uint32_t valueCount) {
96 mGpuStats->insertTargetStatsArray(appPackageName, driverVersionCode, stats, values, valueCount);
97}
98
Yuxin Hub69e9882023-04-13 03:51:41 +000099void GpuService::toggleAngleAsSystemDriver(bool enabled) {
100 IPCThreadState* ipc = IPCThreadState::self();
101 const int pid = ipc->getCallingPid();
102 const int uid = ipc->getCallingUid();
103
104 // only system_server with the ACCESS_GPU_SERVICE permission is allowed to set
105 // persist.graphics.egl
106 if (uid != AID_SYSTEM ||
107 !PermissionCache::checkPermission(sAccessGpuServicePermission, pid, uid)) {
108 ALOGE("Permission Denial: can't set persist.graphics.egl from setAngleAsSystemDriver() "
109 "pid=%d, uid=%d\n", pid, uid);
110 return;
111 }
112
113 std::lock_guard<std::mutex> lock(mLock);
114 if (enabled) {
115 android::base::SetProperty("persist.graphics.egl", sAngleGlesDriverSuffix);
116 } else {
117 android::base::SetProperty("persist.graphics.egl", "");
118 }
119}
120
121
Peiyong Lin0acbfdc2020-06-17 18:47:12 -0700122void GpuService::setUpdatableDriverPath(const std::string& driverPath) {
Yiwei Zhangc7cdd052020-07-28 23:11:21 -0700123 IPCThreadState* ipc = IPCThreadState::self();
124 const int pid = ipc->getCallingPid();
125 const int uid = ipc->getCallingUid();
126
127 // only system_server is allowed to set updatable driver path
128 if (uid != AID_SYSTEM) {
129 ALOGE("Permission Denial: can't set updatable driver path from pid=%d, uid=%d\n", pid, uid);
130 return;
131 }
132
133 std::lock_guard<std::mutex> lock(mLock);
134 mDeveloperDriverPath = driverPath;
Peiyong Lin0acbfdc2020-06-17 18:47:12 -0700135}
136
137std::string GpuService::getUpdatableDriverPath() {
Yiwei Zhangc7cdd052020-07-28 23:11:21 -0700138 std::lock_guard<std::mutex> lock(mLock);
139 return mDeveloperDriverPath;
Peiyong Lin0acbfdc2020-06-17 18:47:12 -0700140}
141
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800142status_t GpuService::shellCommand(int /*in*/, int out, int err, std::vector<String16>& args) {
143 ATRACE_CALL();
144
145 ALOGV("shellCommand");
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700146 for (size_t i = 0, n = args.size(); i < n; i++)
Tomasz Wasilczykf2add402023-08-11 00:06:51 +0000147 ALOGV(" arg[%zu]: '%s'", i, String8(args[i]).c_str());
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700148
Jesse Hall3e26b132016-12-20 14:31:28 -0800149 if (args.size() >= 1) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800150 if (args[0] == String16("vkjson")) return cmdVkjson(out, err);
151 if (args[0] == String16("help")) return cmdHelp(out);
Jesse Hall3e26b132016-12-20 14:31:28 -0800152 }
153 // no command, or unrecognized command
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800154 cmdHelp(err);
Jesse Hall3e26b132016-12-20 14:31:28 -0800155 return BAD_VALUE;
Jesse Hallfc038bd2016-03-26 22:20:22 -0700156}
157
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800158status_t GpuService::doDump(int fd, const Vector<String16>& args, bool /*asProto*/) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800159 std::string result;
160
161 IPCThreadState* ipc = IPCThreadState::self();
162 const int pid = ipc->getCallingPid();
163 const int uid = ipc->getCallingUid();
164
165 if ((uid != AID_SHELL) && !PermissionCache::checkPermission(sDump, pid, uid)) {
166 StringAppendF(&result, "Permission Denial: can't dump gpu from pid=%d, uid=%d\n", pid, uid);
167 } else {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800168 bool dumpAll = true;
Mikael Pessa3d146052019-07-09 09:25:38 -0700169 bool dumpDriverInfo = false;
Yiwei Zhang252e5f62020-02-19 15:44:17 -0800170 bool dumpMem = false;
Mikael Pessa3d146052019-07-09 09:25:38 -0700171 bool dumpStats = false;
Paul Thomsonf44c6b82021-12-01 13:53:58 +0000172 bool dumpWork = false;
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800173 size_t numArgs = args.size();
174
175 if (numArgs) {
Mikael Pessa3d146052019-07-09 09:25:38 -0700176 for (size_t index = 0; index < numArgs; ++index) {
177 if (args[index] == String16("--gpustats")) {
178 dumpStats = true;
179 } else if (args[index] == String16("--gpudriverinfo")) {
180 dumpDriverInfo = true;
Yiwei Zhang252e5f62020-02-19 15:44:17 -0800181 } else if (args[index] == String16("--gpumem")) {
182 dumpMem = true;
Paul Thomsonf44c6b82021-12-01 13:53:58 +0000183 } else if (args[index] == String16("--gpuwork")) {
184 dumpWork = true;
Mikael Pessa3d146052019-07-09 09:25:38 -0700185 }
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800186 }
Paul Thomsonf44c6b82021-12-01 13:53:58 +0000187 dumpAll = !(dumpDriverInfo || dumpMem || dumpStats || dumpWork);
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800188 }
189
Mikael Pessa3d146052019-07-09 09:25:38 -0700190 if (dumpAll || dumpDriverInfo) {
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800191 dumpGameDriverInfo(&result);
192 result.append("\n");
Mikael Pessa3d146052019-07-09 09:25:38 -0700193 }
Yiwei Zhang252e5f62020-02-19 15:44:17 -0800194 if (dumpAll || dumpMem) {
195 mGpuMem->dump(args, &result);
196 result.append("\n");
197 }
Mikael Pessa3d146052019-07-09 09:25:38 -0700198 if (dumpAll || dumpStats) {
Yiwei Zhang8cf7c7b2019-08-28 13:07:30 -0700199 mGpuStats->dump(args, &result);
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800200 result.append("\n");
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800201 }
Paul Thomsonf44c6b82021-12-01 13:53:58 +0000202 if (dumpAll || dumpWork) {
203 mGpuWork->dump(args, &result);
204 result.append("\n");
205 }
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800206 }
207
208 write(fd, result.c_str(), result.size());
209 return NO_ERROR;
210}
211
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700212namespace {
213
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800214status_t cmdHelp(int out) {
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700215 FILE* outs = fdopen(out, "w");
216 if (!outs) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800217 ALOGE("vkjson: failed to create out stream: %s (%d)", strerror(errno), errno);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700218 return BAD_VALUE;
219 }
220 fprintf(outs,
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800221 "GPU Service commands:\n"
222 " vkjson dump Vulkan properties as JSON\n");
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700223 fclose(outs);
224 return NO_ERROR;
225}
226
Jesse Hall3841bf42016-06-12 15:08:28 -0700227void vkjsonPrint(FILE* out) {
228 std::string json = VkJsonInstanceToJson(VkJsonGetInstance());
229 fwrite(json.data(), 1, json.size(), out);
230 fputc('\n', out);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700231}
232
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800233status_t cmdVkjson(int out, int /*err*/) {
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700234 FILE* outs = fdopen(out, "w");
235 if (!outs) {
Jesse Hall3841bf42016-06-12 15:08:28 -0700236 int errnum = errno;
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700237 ALOGE("vkjson: failed to create output stream: %s", strerror(errnum));
238 return -errnum;
239 }
Jesse Hall3841bf42016-06-12 15:08:28 -0700240 vkjsonPrint(outs);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700241 fclose(outs);
Jesse Hall3841bf42016-06-12 15:08:28 -0700242 return NO_ERROR;
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700243}
244
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800245void dumpGameDriverInfo(std::string* result) {
246 if (!result) return;
247
248 char stableGameDriver[PROPERTY_VALUE_MAX] = {};
249 property_get("ro.gfx.driver.0", stableGameDriver, "unsupported");
250 StringAppendF(result, "Stable Game Driver: %s\n", stableGameDriver);
251
252 char preReleaseGameDriver[PROPERTY_VALUE_MAX] = {};
253 property_get("ro.gfx.driver.1", preReleaseGameDriver, "unsupported");
254 StringAppendF(result, "Pre-release Game Driver: %s\n", preReleaseGameDriver);
255}
256
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700257} // anonymous namespace
258
Jesse Hallfc038bd2016-03-26 22:20:22 -0700259} // namespace android