blob: c8253e05c2693af3616d8f64a5401f89481c0240 [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 Zhang2d4c1882019-02-24 22:28:08 -080056 mGpuStats->insert(driverPackageName, driverVersionName, driverVersionCode, driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -070057 appPackageName, vulkanVersion, driver, isDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080058}
59
Yiwei Zhangbaea97f2019-02-27 16:35:37 -080060status_t GpuService::getGpuStatsGlobalInfo(std::vector<GpuStatsGlobalInfo>* outStats) const {
Yiwei Zhangbaea97f2019-02-27 16:35:37 -080061 mGpuStats->pullGlobalStats(outStats);
Yiwei Zhangbaea97f2019-02-27 16:35:37 -080062 return OK;
63}
64
Yiwei Zhang178e0672019-03-04 14:17:12 -080065status_t GpuService::getGpuStatsAppInfo(std::vector<GpuStatsAppInfo>* outStats) const {
Yiwei Zhang178e0672019-03-04 14:17:12 -080066 mGpuStats->pullAppStats(outStats);
Yiwei Zhang178e0672019-03-04 14:17:12 -080067 return OK;
68}
69
Yiwei Zhangb1c1a372019-07-03 13:39:32 -070070void GpuService::setTargetStats(const std::string& appPackageName, const uint64_t driverVersionCode,
71 const GraphicsEnv::Stats stats, const uint64_t value) {
72 mGpuStats->insertTargetStats(appPackageName, driverVersionCode, stats, value);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -070073}
74
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080075status_t GpuService::shellCommand(int /*in*/, int out, int err, std::vector<String16>& args) {
76 ATRACE_CALL();
77
78 ALOGV("shellCommand");
Jesse Hall8b0d55e2016-03-31 19:29:36 -070079 for (size_t i = 0, n = args.size(); i < n; i++)
80 ALOGV(" arg[%zu]: '%s'", i, String8(args[i]).string());
81
Jesse Hall3e26b132016-12-20 14:31:28 -080082 if (args.size() >= 1) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080083 if (args[0] == String16("vkjson")) return cmdVkjson(out, err);
84 if (args[0] == String16("help")) return cmdHelp(out);
Jesse Hall3e26b132016-12-20 14:31:28 -080085 }
86 // no command, or unrecognized command
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080087 cmdHelp(err);
Jesse Hall3e26b132016-12-20 14:31:28 -080088 return BAD_VALUE;
Jesse Hallfc038bd2016-03-26 22:20:22 -070089}
90
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080091status_t GpuService::doDump(int fd, const Vector<String16>& args, bool /*asProto*/) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080092 std::string result;
93
94 IPCThreadState* ipc = IPCThreadState::self();
95 const int pid = ipc->getCallingPid();
96 const int uid = ipc->getCallingUid();
97
98 if ((uid != AID_SHELL) && !PermissionCache::checkPermission(sDump, pid, uid)) {
99 StringAppendF(&result, "Permission Denial: can't dump gpu from pid=%d, uid=%d\n", pid, uid);
100 } else {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800101 bool dumpAll = true;
102 size_t index = 0;
103 size_t numArgs = args.size();
104
105 if (numArgs) {
106 if ((index < numArgs) && (args[index] == String16("--gpustats"))) {
107 index++;
108 mGpuStats->dump(args, &result);
109 dumpAll = false;
110 }
111 }
112
113 if (dumpAll) {
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800114 dumpGameDriverInfo(&result);
115 result.append("\n");
116
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800117 mGpuStats->dump(Vector<String16>(), &result);
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800118 result.append("\n");
Yiwei Zhang2d4c1882019-02-24 22:28:08 -0800119 }
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800120 }
121
122 write(fd, result.c_str(), result.size());
123 return NO_ERROR;
124}
125
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700126namespace {
127
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800128status_t cmdHelp(int out) {
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700129 FILE* outs = fdopen(out, "w");
130 if (!outs) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800131 ALOGE("vkjson: failed to create out stream: %s (%d)", strerror(errno), errno);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700132 return BAD_VALUE;
133 }
134 fprintf(outs,
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800135 "GPU Service commands:\n"
136 " vkjson dump Vulkan properties as JSON\n");
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700137 fclose(outs);
138 return NO_ERROR;
139}
140
Jesse Hall3841bf42016-06-12 15:08:28 -0700141void vkjsonPrint(FILE* out) {
142 std::string json = VkJsonInstanceToJson(VkJsonGetInstance());
143 fwrite(json.data(), 1, json.size(), out);
144 fputc('\n', out);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700145}
146
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800147status_t cmdVkjson(int out, int /*err*/) {
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700148 FILE* outs = fdopen(out, "w");
149 if (!outs) {
Jesse Hall3841bf42016-06-12 15:08:28 -0700150 int errnum = errno;
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700151 ALOGE("vkjson: failed to create output stream: %s", strerror(errnum));
152 return -errnum;
153 }
Jesse Hall3841bf42016-06-12 15:08:28 -0700154 vkjsonPrint(outs);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700155 fclose(outs);
Jesse Hall3841bf42016-06-12 15:08:28 -0700156 return NO_ERROR;
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700157}
158
Yiwei Zhang2a61c152019-02-27 11:27:18 -0800159void dumpGameDriverInfo(std::string* result) {
160 if (!result) return;
161
162 char stableGameDriver[PROPERTY_VALUE_MAX] = {};
163 property_get("ro.gfx.driver.0", stableGameDriver, "unsupported");
164 StringAppendF(result, "Stable Game Driver: %s\n", stableGameDriver);
165
166 char preReleaseGameDriver[PROPERTY_VALUE_MAX] = {};
167 property_get("ro.gfx.driver.1", preReleaseGameDriver, "unsupported");
168 StringAppendF(result, "Pre-release Game Driver: %s\n", preReleaseGameDriver);
169}
170
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700171} // anonymous namespace
172
Jesse Hallfc038bd2016-03-26 22:20:22 -0700173} // namespace android