blob: 81b0a46e0c99d6c10d39ece9fe855a1a50b40e8e [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 Zhangbaaef882020-02-02 17:45:30 -080027#include <gpustats/GpuStats.h>
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080028#include <private/android_filesystem_config.h>
Jesse Hall8b0d55e2016-03-31 19:29:36 -070029#include <utils/String8.h>
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080030#include <utils/Trace.h>
31
Jesse Hall8b0d55e2016-03-31 19:29:36 -070032#include <vkjson.h>
Jesse Hallfc038bd2016-03-26 22:20:22 -070033
34namespace android {
35
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080036using base::StringAppendF;
Jesse Hallfc038bd2016-03-26 22:20:22 -070037
Jesse Hall8b0d55e2016-03-31 19:29:36 -070038namespace {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080039status_t cmdHelp(int out);
40status_t cmdVkjson(int out, int err);
Yiwei Zhang2a61c152019-02-27 11:27:18 -080041void dumpGameDriverInfo(std::string* result);
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080042} // namespace
43
44const String16 sDump("android.permission.DUMP");
Jesse Hall8b0d55e2016-03-31 19:29:36 -070045
Yiwei Zhang423d0802018-11-16 10:56:12 -080046const char* const GpuService::SERVICE_NAME = "gpu";
Jesse Hallfc038bd2016-03-26 22:20:22 -070047
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080048GpuService::GpuService() : mGpuStats(std::make_unique<GpuStats>()){};
Jesse Hallfc038bd2016-03-26 22:20:22 -070049
Greg Kaiser210bb7e2019-02-12 12:40:05 -080050void GpuService::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 11:51:55 -080051 const std::string& driverVersionName, uint64_t driverVersionCode,
Yiwei Zhang96c01712019-02-19 16:00:25 -080052 int64_t driverBuildTime, const std::string& appPackageName,
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -070053 const int32_t vulkanVersion, GpuStatsInfo::Driver driver,
Yiwei Zhang794d2952019-05-06 17:43:59 -070054 bool isDriverLoaded, int64_t driverLoadingTime) {
Yiwei Zhangfdd7e782020-01-31 15:59:34 -080055 mGpuStats->insertDriverStats(driverPackageName, driverVersionName, driverVersionCode,
56 driverBuildTime, appPackageName, vulkanVersion, driver,
57 isDriverLoaded, driverLoadingTime);
Yiwei Zhang178e0672019-03-04 14:17:12 -080058}
59
Yiwei Zhangb1c1a372019-07-03 13:39:32 -070060void GpuService::setTargetStats(const std::string& appPackageName, const uint64_t driverVersionCode,
Yiwei Zhangbcba4112019-07-03 13:39:32 -070061 const GpuStatsInfo::Stats stats, const uint64_t value) {
Yiwei Zhangb1c1a372019-07-03 13:39:32 -070062 mGpuStats->insertTargetStats(appPackageName, driverVersionCode, stats, value);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -070063}
64
Peiyong Lin6b388252020-06-17 18:47:12 -070065void GpuService::setUpdatableDriverPath(const std::string& driverPath) {
Xin Lie8b4e702020-08-29 01:34:09 -070066 IPCThreadState* ipc = IPCThreadState::self();
67 const int pid = ipc->getCallingPid();
68 const int uid = ipc->getCallingUid();
69
70 // only system_server is allowed to set updatable driver path
71 if (uid != AID_SYSTEM) {
72 ALOGE("Permission Denial: can't set updatable driver path from pid=%d, uid=%d\n", pid, uid);
73 return;
74 }
75
76 std::lock_guard<std::mutex> lock(mLock);
77 mDeveloperDriverPath = driverPath;
Peiyong Lin6b388252020-06-17 18:47:12 -070078}
79
80std::string GpuService::getUpdatableDriverPath() {
Xin Lie8b4e702020-08-29 01:34:09 -070081 std::lock_guard<std::mutex> lock(mLock);
82 return mDeveloperDriverPath;
Peiyong Lin6b388252020-06-17 18:47:12 -070083}
84
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080085status_t GpuService::shellCommand(int /*in*/, int out, int err, std::vector<String16>& args) {
86 ATRACE_CALL();
87
88 ALOGV("shellCommand");
Jesse Hall8b0d55e2016-03-31 19:29:36 -070089 for (size_t i = 0, n = args.size(); i < n; i++)
90 ALOGV(" arg[%zu]: '%s'", i, String8(args[i]).string());
91
Jesse Hall3e26b132016-12-20 14:31:28 -080092 if (args.size() >= 1) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080093 if (args[0] == String16("vkjson")) return cmdVkjson(out, err);
94 if (args[0] == String16("help")) return cmdHelp(out);
Jesse Hall3e26b132016-12-20 14:31:28 -080095 }
96 // no command, or unrecognized command
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080097 cmdHelp(err);
Jesse Hall3e26b132016-12-20 14:31:28 -080098 return BAD_VALUE;
Jesse Hallfc038bd2016-03-26 22:20:22 -070099}
100
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800101status_t GpuService::doDump(int fd, const Vector<String16>& args, bool /*asProto*/) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800102 std::string result;
103
104 IPCThreadState* ipc = IPCThreadState::self();
105 const int pid = ipc->getCallingPid();
106 const int uid = ipc->getCallingUid();
107
108 if ((uid != AID_SHELL) && !PermissionCache::checkPermission(sDump, pid, uid)) {
109 StringAppendF(&result, "Permission Denial: can't dump gpu from pid=%d, uid=%d\n", pid, uid);
110 } else {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800111 bool dumpAll = true;
Mikael Pessa3d146052019-07-09 09:25:38 -0700112 bool dumpDriverInfo = false;
113 bool dumpStats = false;
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800114 size_t numArgs = args.size();
115
116 if (numArgs) {
Mikael Pessa3d146052019-07-09 09:25:38 -0700117 for (size_t index = 0; index < numArgs; ++index) {
118 if (args[index] == String16("--gpustats")) {
119 dumpStats = true;
120 } else if (args[index] == String16("--gpudriverinfo")) {
121 dumpDriverInfo = true;
Mikael Pessa3d146052019-07-09 09:25:38 -0700122 }
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800123 }
Yiwei Zhang90153a12020-06-07 16:57:10 -0700124 dumpAll = !(dumpDriverInfo || dumpStats);
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800125 }
126
Mikael Pessa3d146052019-07-09 09:25:38 -0700127 if (dumpAll || dumpDriverInfo) {
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800128 dumpGameDriverInfo(&result);
129 result.append("\n");
Mikael Pessa3d146052019-07-09 09:25:38 -0700130 }
131 if (dumpAll || dumpStats) {
Yiwei Zhang8cf7c7b2019-08-28 13:07:30 -0700132 mGpuStats->dump(args, &result);
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800133 result.append("\n");
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800134 }
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800135 }
136
137 write(fd, result.c_str(), result.size());
138 return NO_ERROR;
139}
140
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700141namespace {
142
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800143status_t cmdHelp(int out) {
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700144 FILE* outs = fdopen(out, "w");
145 if (!outs) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800146 ALOGE("vkjson: failed to create out stream: %s (%d)", strerror(errno), errno);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700147 return BAD_VALUE;
148 }
149 fprintf(outs,
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800150 "GPU Service commands:\n"
151 " vkjson dump Vulkan properties as JSON\n");
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700152 fclose(outs);
153 return NO_ERROR;
154}
155
Jesse Hall3841bf42016-06-12 15:08:28 -0700156void vkjsonPrint(FILE* out) {
157 std::string json = VkJsonInstanceToJson(VkJsonGetInstance());
158 fwrite(json.data(), 1, json.size(), out);
159 fputc('\n', out);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700160}
161
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800162status_t cmdVkjson(int out, int /*err*/) {
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700163 FILE* outs = fdopen(out, "w");
164 if (!outs) {
Jesse Hall3841bf42016-06-12 15:08:28 -0700165 int errnum = errno;
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700166 ALOGE("vkjson: failed to create output stream: %s", strerror(errnum));
167 return -errnum;
168 }
Jesse Hall3841bf42016-06-12 15:08:28 -0700169 vkjsonPrint(outs);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700170 fclose(outs);
Jesse Hall3841bf42016-06-12 15:08:28 -0700171 return NO_ERROR;
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700172}
173
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800174void dumpGameDriverInfo(std::string* result) {
175 if (!result) return;
176
177 char stableGameDriver[PROPERTY_VALUE_MAX] = {};
178 property_get("ro.gfx.driver.0", stableGameDriver, "unsupported");
179 StringAppendF(result, "Stable Game Driver: %s\n", stableGameDriver);
180
181 char preReleaseGameDriver[PROPERTY_VALUE_MAX] = {};
182 property_get("ro.gfx.driver.1", preReleaseGameDriver, "unsupported");
183 StringAppendF(result, "Pre-release Game Driver: %s\n", preReleaseGameDriver);
184}
185
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700186} // anonymous namespace
187
Jesse Hallfc038bd2016-03-26 22:20:22 -0700188} // namespace android