blob: bf85e88cbd8d175982d86ca6e452a151eb429bae [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>
26#include <private/android_filesystem_config.h>
Jesse Hall8b0d55e2016-03-31 19:29:36 -070027#include <utils/String8.h>
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080028#include <utils/Trace.h>
29
Jesse Hall8b0d55e2016-03-31 19:29:36 -070030#include <vkjson.h>
Jesse Hallfc038bd2016-03-26 22:20:22 -070031
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080032#include "gpustats/GpuStats.h"
33
Jesse Hallfc038bd2016-03-26 22:20:22 -070034namespace 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);
41} // namespace
42
43const String16 sDump("android.permission.DUMP");
Jesse Hall8b0d55e2016-03-31 19:29:36 -070044
Yiwei Zhang423d0802018-11-16 10:56:12 -080045const char* const GpuService::SERVICE_NAME = "gpu";
Jesse Hallfc038bd2016-03-26 22:20:22 -070046
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080047GpuService::GpuService() : mGpuStats(std::make_unique<GpuStats>()){};
Jesse Hallfc038bd2016-03-26 22:20:22 -070048
Greg Kaiser210bb7e2019-02-12 12:40:05 -080049void GpuService::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 11:51:55 -080050 const std::string& driverVersionName, uint64_t driverVersionCode,
Yiwei Zhang96c01712019-02-19 16:00:25 -080051 int64_t driverBuildTime, const std::string& appPackageName,
Yiwei Zhang512a7232019-02-15 16:04:41 -080052 GraphicsEnv::Driver driver, bool isDriverLoaded,
53 int64_t driverLoadingTime) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080054 ATRACE_CALL();
55
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080056 mGpuStats->insert(driverPackageName, driverVersionName, driverVersionCode, driverBuildTime,
57 appPackageName, 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 {
61 ATRACE_CALL();
62
63 mGpuStats->pullGlobalStats(outStats);
64
65 return OK;
66}
67
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080068status_t GpuService::shellCommand(int /*in*/, int out, int err, std::vector<String16>& args) {
69 ATRACE_CALL();
70
71 ALOGV("shellCommand");
Jesse Hall8b0d55e2016-03-31 19:29:36 -070072 for (size_t i = 0, n = args.size(); i < n; i++)
73 ALOGV(" arg[%zu]: '%s'", i, String8(args[i]).string());
74
Jesse Hall3e26b132016-12-20 14:31:28 -080075 if (args.size() >= 1) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080076 if (args[0] == String16("vkjson")) return cmdVkjson(out, err);
77 if (args[0] == String16("help")) return cmdHelp(out);
Jesse Hall3e26b132016-12-20 14:31:28 -080078 }
79 // no command, or unrecognized command
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080080 cmdHelp(err);
Jesse Hall3e26b132016-12-20 14:31:28 -080081 return BAD_VALUE;
Jesse Hallfc038bd2016-03-26 22:20:22 -070082}
83
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080084status_t GpuService::doDump(int fd, const Vector<String16>& args, bool /*asProto*/) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -080085 std::string result;
86
87 IPCThreadState* ipc = IPCThreadState::self();
88 const int pid = ipc->getCallingPid();
89 const int uid = ipc->getCallingUid();
90
91 if ((uid != AID_SHELL) && !PermissionCache::checkPermission(sDump, pid, uid)) {
92 StringAppendF(&result, "Permission Denial: can't dump gpu from pid=%d, uid=%d\n", pid, uid);
93 } else {
Yiwei Zhang2d4c1882019-02-24 22:28:08 -080094 bool dumpAll = true;
95 size_t index = 0;
96 size_t numArgs = args.size();
97
98 if (numArgs) {
99 if ((index < numArgs) && (args[index] == String16("--gpustats"))) {
100 index++;
101 mGpuStats->dump(args, &result);
102 dumpAll = false;
103 }
104 }
105
106 if (dumpAll) {
107 mGpuStats->dump(Vector<String16>(), &result);
108 }
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800109 }
110
111 write(fd, result.c_str(), result.size());
112 return NO_ERROR;
113}
114
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700115namespace {
116
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800117status_t cmdHelp(int out) {
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700118 FILE* outs = fdopen(out, "w");
119 if (!outs) {
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800120 ALOGE("vkjson: failed to create out stream: %s (%d)", strerror(errno), errno);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700121 return BAD_VALUE;
122 }
123 fprintf(outs,
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800124 "GPU Service commands:\n"
125 " vkjson dump Vulkan properties as JSON\n");
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700126 fclose(outs);
127 return NO_ERROR;
128}
129
Jesse Hall3841bf42016-06-12 15:08:28 -0700130void vkjsonPrint(FILE* out) {
131 std::string json = VkJsonInstanceToJson(VkJsonGetInstance());
132 fwrite(json.data(), 1, json.size(), out);
133 fputc('\n', out);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700134}
135
Yiwei Zhangbb4eaf62019-02-23 17:53:27 -0800136status_t cmdVkjson(int out, int /*err*/) {
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700137 FILE* outs = fdopen(out, "w");
138 if (!outs) {
Jesse Hall3841bf42016-06-12 15:08:28 -0700139 int errnum = errno;
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700140 ALOGE("vkjson: failed to create output stream: %s", strerror(errnum));
141 return -errnum;
142 }
Jesse Hall3841bf42016-06-12 15:08:28 -0700143 vkjsonPrint(outs);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700144 fclose(outs);
Jesse Hall3841bf42016-06-12 15:08:28 -0700145 return NO_ERROR;
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700146}
147
148} // anonymous namespace
149
Jesse Hallfc038bd2016-03-26 22:20:22 -0700150} // namespace android