blob: 8accf9d450a03828a87781fece0854e7d4be605f [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 Zhangbb4eaf62019-02-23 17:53:27 -080027#include <private/android_filesystem_config.h>
Jesse Hall8b0d55e2016-03-31 19:29:36 -070028#include <utils/String8.h>
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080029#include <utils/Trace.h>
30
Jesse Hall8b0d55e2016-03-31 19:29:36 -070031#include <vkjson.h>
Jesse Hallfc038bd2016-03-26 22:20:22 -070032
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080033#include "gpustats/GpuStats.h"
34
Jesse Hallfc038bd2016-03-26 22:20:22 -070035namespace android {
36
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080037using base::StringAppendF;
Jesse Hallfc038bd2016-03-26 22:20:22 -070038
Jesse Hall8b0d55e2016-03-31 19:29:36 -070039namespace {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080040status_t cmdHelp(int out);
41status_t cmdVkjson(int out, int err);
Yiwei Zhang2a61c152019-02-27 11:27:18 -080042void dumpGameDriverInfo(std::string* result);
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080043} // namespace
44
45const String16 sDump("android.permission.DUMP");
Jesse Hall8b0d55e2016-03-31 19:29:36 -070046
Yiwei Zhang423d0802018-11-16 10:56:12 -080047const char* const GpuService::SERVICE_NAME = "gpu";
Jesse Hallfc038bd2016-03-26 22:20:22 -070048
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080049GpuService::GpuService() : mGpuStats(std::make_unique<GpuStats>()){};
Jesse Hallfc038bd2016-03-26 22:20:22 -070050
Greg Kaiser210bb7e2019-02-12 12:40:05 -080051void GpuService::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 11:51:55 -080052 const std::string& driverVersionName, uint64_t driverVersionCode,
Yiwei Zhang96c01712019-02-19 16:00:25 -080053 int64_t driverBuildTime, const std::string& appPackageName,
Yiwei Zhang794d2952019-05-06 17:43:59 -070054 const int32_t vulkanVersion, GraphicsEnv::Driver driver,
55 bool isDriverLoaded, int64_t driverLoadingTime) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080056 ATRACE_CALL();
57
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080058 mGpuStats->insert(driverPackageName, driverVersionName, driverVersionCode, driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -070059 appPackageName, vulkanVersion, driver, isDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080060}
61
Yiwei Zhangbaea97f2019-02-27 16:35:37 -080062status_t GpuService::getGpuStatsGlobalInfo(std::vector<GpuStatsGlobalInfo>* outStats) const {
63 ATRACE_CALL();
64
65 mGpuStats->pullGlobalStats(outStats);
66
67 return OK;
68}
69
Yiwei Zhang178e0672019-03-04 14:17:12 -080070status_t GpuService::getGpuStatsAppInfo(std::vector<GpuStatsAppInfo>* outStats) const {
71 ATRACE_CALL();
72
73 mGpuStats->pullAppStats(outStats);
74
75 return OK;
76}
77
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -070078void GpuService::setCpuVulkanInUse(const std::string& appPackageName,
79 const uint64_t driverVersionCode) {
80 ATRACE_CALL();
81
82 mGpuStats->setCpuVulkanInUse(appPackageName, driverVersionCode);
83}
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;
112 size_t index = 0;
113 size_t numArgs = args.size();
114
115 if (numArgs) {
116 if ((index < numArgs) && (args[index] == String16("--gpustats"))) {
117 index++;
118 mGpuStats->dump(args, &result);
119 dumpAll = false;
120 }
121 }
122
123 if (dumpAll) {
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800124 dumpGameDriverInfo(&result);
125 result.append("\n");
126
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800127 mGpuStats->dump(Vector<String16>(), &result);
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800128 result.append("\n");
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800129 }
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800130 }
131
132 write(fd, result.c_str(), result.size());
133 return NO_ERROR;
134}
135
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700136namespace {
137
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800138status_t cmdHelp(int out) {
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700139 FILE* outs = fdopen(out, "w");
140 if (!outs) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800141 ALOGE("vkjson: failed to create out stream: %s (%d)", strerror(errno), errno);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700142 return BAD_VALUE;
143 }
144 fprintf(outs,
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800145 "GPU Service commands:\n"
146 " vkjson dump Vulkan properties as JSON\n");
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700147 fclose(outs);
148 return NO_ERROR;
149}
150
Jesse Hall3841bf42016-06-12 15:08:28 -0700151void vkjsonPrint(FILE* out) {
152 std::string json = VkJsonInstanceToJson(VkJsonGetInstance());
153 fwrite(json.data(), 1, json.size(), out);
154 fputc('\n', out);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700155}
156
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800157status_t cmdVkjson(int out, int /*err*/) {
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700158 FILE* outs = fdopen(out, "w");
159 if (!outs) {
Jesse Hall3841bf42016-06-12 15:08:28 -0700160 int errnum = errno;
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700161 ALOGE("vkjson: failed to create output stream: %s", strerror(errnum));
162 return -errnum;
163 }
Jesse Hall3841bf42016-06-12 15:08:28 -0700164 vkjsonPrint(outs);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700165 fclose(outs);
Jesse Hall3841bf42016-06-12 15:08:28 -0700166 return NO_ERROR;
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700167}
168
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800169void dumpGameDriverInfo(std::string* result) {
170 if (!result) return;
171
172 char stableGameDriver[PROPERTY_VALUE_MAX] = {};
173 property_get("ro.gfx.driver.0", stableGameDriver, "unsupported");
174 StringAppendF(result, "Stable Game Driver: %s\n", stableGameDriver);
175
176 char preReleaseGameDriver[PROPERTY_VALUE_MAX] = {};
177 property_get("ro.gfx.driver.1", preReleaseGameDriver, "unsupported");
178 StringAppendF(result, "Pre-release Game Driver: %s\n", preReleaseGameDriver);
179}
180
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700181} // anonymous namespace
182
Jesse Hallfc038bd2016-03-26 22:20:22 -0700183} // namespace android