blob: 44af4c4877ad61fa0ba0458c6dd5825190e22a17 [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
sergiuferentz5adee122023-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>
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>
Yiwei Zhangbaaef882020-02-02 17:45:30 -080028#include <gpustats/GpuStats.h>
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080029#include <private/android_filesystem_config.h>
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -070030#include <tracing/GpuMemTracer.h>
Jesse Hall8b0d55e2016-03-31 19:29:36 -070031#include <utils/String8.h>
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080032#include <utils/Trace.h>
Jesse Hall8b0d55e2016-03-31 19:29:36 -070033#include <vkjson.h>
Jesse Hallfc038bd2016-03-26 22:20:22 -070034
Yiwei Zhanga265b402020-06-25 22:02:29 -070035#include <thread>
sergiuferentz5adee122023-06-26 18:01:47 +000036#include <memory>
Yiwei Zhanga265b402020-06-25 22:02:29 -070037
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>()),
54 mGpuStats(std::make_unique<GpuStats>()),
55 mGpuMemTracer(std::make_unique<GpuMemTracer>()) {
sergiuferentz5adee122023-06-26 18:01:47 +000056 mGpuMemAsyncInitThread = std::make_unique<std::thread>([this] (){
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -070057 mGpuMem->initialize();
58 mGpuMemTracer->initialize(mGpuMem);
59 });
Yiwei Zhang252e5f62020-02-19 15:44:17 -080060};
Jesse Hallfc038bd2016-03-26 22:20:22 -070061
sergiuferentz5adee122023-06-26 18:01:47 +000062GpuService::~GpuService() {
63 mGpuMemAsyncInitThread->join();
64}
65
Greg Kaiser210bb7e2019-02-12 12:40:05 -080066void GpuService::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 11:51:55 -080067 const std::string& driverVersionName, uint64_t driverVersionCode,
Yiwei Zhang96c01712019-02-19 16:00:25 -080068 int64_t driverBuildTime, const std::string& appPackageName,
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -070069 const int32_t vulkanVersion, GpuStatsInfo::Driver driver,
Yiwei Zhang794d2952019-05-06 17:43:59 -070070 bool isDriverLoaded, int64_t driverLoadingTime) {
Yiwei Zhangfdd7e782020-01-31 15:59:34 -080071 mGpuStats->insertDriverStats(driverPackageName, driverVersionName, driverVersionCode,
72 driverBuildTime, appPackageName, vulkanVersion, driver,
73 isDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080074}
75
Yiwei Zhangbcba4112019-07-03 13:39:32 -070076void GpuService::setTargetStats(const std::string& appPackageName, const uint64_t driverVersionCode,
77 const GpuStatsInfo::Stats stats, const uint64_t value) {
78 mGpuStats->insertTargetStats(appPackageName, driverVersionCode, stats, value);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -070079}
80
Peiyong Lin0acbfdc2020-06-17 18:47:12 -070081void GpuService::setUpdatableDriverPath(const std::string& driverPath) {
Yiwei Zhangc7cdd052020-07-28 23:11:21 -070082 IPCThreadState* ipc = IPCThreadState::self();
83 const int pid = ipc->getCallingPid();
84 const int uid = ipc->getCallingUid();
85
86 // only system_server is allowed to set updatable driver path
87 if (uid != AID_SYSTEM) {
88 ALOGE("Permission Denial: can't set updatable driver path from pid=%d, uid=%d\n", pid, uid);
89 return;
90 }
91
92 std::lock_guard<std::mutex> lock(mLock);
93 mDeveloperDriverPath = driverPath;
Peiyong Lin0acbfdc2020-06-17 18:47:12 -070094}
95
96std::string GpuService::getUpdatableDriverPath() {
Yiwei Zhangc7cdd052020-07-28 23:11:21 -070097 std::lock_guard<std::mutex> lock(mLock);
98 return mDeveloperDriverPath;
Peiyong Lin0acbfdc2020-06-17 18:47:12 -070099}
100
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800101status_t GpuService::shellCommand(int /*in*/, int out, int err, std::vector<String16>& args) {
102 ATRACE_CALL();
103
104 ALOGV("shellCommand");
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700105 for (size_t i = 0, n = args.size(); i < n; i++)
106 ALOGV(" arg[%zu]: '%s'", i, String8(args[i]).string());
107
Jesse Hall3e26b132016-12-20 14:31:28 -0800108 if (args.size() >= 1) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800109 if (args[0] == String16("vkjson")) return cmdVkjson(out, err);
110 if (args[0] == String16("help")) return cmdHelp(out);
Jesse Hall3e26b132016-12-20 14:31:28 -0800111 }
112 // no command, or unrecognized command
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800113 cmdHelp(err);
Jesse Hall3e26b132016-12-20 14:31:28 -0800114 return BAD_VALUE;
Jesse Hallfc038bd2016-03-26 22:20:22 -0700115}
116
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800117status_t GpuService::doDump(int fd, const Vector<String16>& args, bool /*asProto*/) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800118 std::string result;
119
120 IPCThreadState* ipc = IPCThreadState::self();
121 const int pid = ipc->getCallingPid();
122 const int uid = ipc->getCallingUid();
123
124 if ((uid != AID_SHELL) && !PermissionCache::checkPermission(sDump, pid, uid)) {
125 StringAppendF(&result, "Permission Denial: can't dump gpu from pid=%d, uid=%d\n", pid, uid);
126 } else {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800127 bool dumpAll = true;
Mikael Pessa3d146052019-07-09 09:25:38 -0700128 bool dumpDriverInfo = false;
Yiwei Zhang252e5f62020-02-19 15:44:17 -0800129 bool dumpMem = false;
Mikael Pessa3d146052019-07-09 09:25:38 -0700130 bool dumpStats = false;
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800131 size_t numArgs = args.size();
132
133 if (numArgs) {
Mikael Pessa3d146052019-07-09 09:25:38 -0700134 for (size_t index = 0; index < numArgs; ++index) {
135 if (args[index] == String16("--gpustats")) {
136 dumpStats = true;
137 } else if (args[index] == String16("--gpudriverinfo")) {
138 dumpDriverInfo = true;
Yiwei Zhang252e5f62020-02-19 15:44:17 -0800139 } else if (args[index] == String16("--gpumem")) {
140 dumpMem = true;
Mikael Pessa3d146052019-07-09 09:25:38 -0700141 }
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800142 }
Yiwei Zhangf881dd32020-06-07 17:00:21 -0700143 dumpAll = !(dumpDriverInfo || dumpMem || dumpStats);
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800144 }
145
Mikael Pessa3d146052019-07-09 09:25:38 -0700146 if (dumpAll || dumpDriverInfo) {
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800147 dumpGameDriverInfo(&result);
148 result.append("\n");
Mikael Pessa3d146052019-07-09 09:25:38 -0700149 }
Yiwei Zhang252e5f62020-02-19 15:44:17 -0800150 if (dumpAll || dumpMem) {
151 mGpuMem->dump(args, &result);
152 result.append("\n");
153 }
Mikael Pessa3d146052019-07-09 09:25:38 -0700154 if (dumpAll || dumpStats) {
Yiwei Zhang8cf7c7b2019-08-28 13:07:30 -0700155 mGpuStats->dump(args, &result);
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800156 result.append("\n");
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800157 }
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800158 }
159
160 write(fd, result.c_str(), result.size());
161 return NO_ERROR;
162}
163
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700164namespace {
165
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800166status_t cmdHelp(int out) {
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700167 FILE* outs = fdopen(out, "w");
168 if (!outs) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800169 ALOGE("vkjson: failed to create out stream: %s (%d)", strerror(errno), errno);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700170 return BAD_VALUE;
171 }
172 fprintf(outs,
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800173 "GPU Service commands:\n"
174 " vkjson dump Vulkan properties as JSON\n");
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700175 fclose(outs);
176 return NO_ERROR;
177}
178
Jesse Hall3841bf42016-06-12 15:08:28 -0700179void vkjsonPrint(FILE* out) {
180 std::string json = VkJsonInstanceToJson(VkJsonGetInstance());
181 fwrite(json.data(), 1, json.size(), out);
182 fputc('\n', out);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700183}
184
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800185status_t cmdVkjson(int out, int /*err*/) {
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700186 FILE* outs = fdopen(out, "w");
187 if (!outs) {
Jesse Hall3841bf42016-06-12 15:08:28 -0700188 int errnum = errno;
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700189 ALOGE("vkjson: failed to create output stream: %s", strerror(errnum));
190 return -errnum;
191 }
Jesse Hall3841bf42016-06-12 15:08:28 -0700192 vkjsonPrint(outs);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700193 fclose(outs);
Jesse Hall3841bf42016-06-12 15:08:28 -0700194 return NO_ERROR;
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700195}
196
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800197void dumpGameDriverInfo(std::string* result) {
198 if (!result) return;
199
200 char stableGameDriver[PROPERTY_VALUE_MAX] = {};
201 property_get("ro.gfx.driver.0", stableGameDriver, "unsupported");
202 StringAppendF(result, "Stable Game Driver: %s\n", stableGameDriver);
203
204 char preReleaseGameDriver[PROPERTY_VALUE_MAX] = {};
205 property_get("ro.gfx.driver.1", preReleaseGameDriver, "unsupported");
206 StringAppendF(result, "Pre-release Game Driver: %s\n", preReleaseGameDriver);
207}
208
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700209} // anonymous namespace
210
Jesse Hallfc038bd2016-03-26 22:20:22 -0700211} // namespace android