1 /*
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
17 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
19 #include "GpuService.h"
20
21 #include <android-base/stringprintf.h>
22 #include <binder/IPCThreadState.h>
23 #include <binder/IResultReceiver.h>
24 #include <binder/Parcel.h>
25 #include <binder/PermissionCache.h>
26 #include <cutils/properties.h>
27 #include <gpumem/GpuMem.h>
28 #include <gpustats/GpuStats.h>
29 #include <private/android_filesystem_config.h>
30 #include <tracing/GpuMemTracer.h>
31 #include <utils/String8.h>
32 #include <utils/Trace.h>
33 #include <vkjson.h>
34
35 #include <thread>
36
37 namespace android {
38
39 using base::StringAppendF;
40
41 namespace {
42 status_t cmdHelp(int out);
43 status_t cmdVkjson(int out, int err);
44 void dumpGameDriverInfo(std::string* result);
45 } // namespace
46
47 const String16 sDump("android.permission.DUMP");
48
49 const char* const GpuService::SERVICE_NAME = "gpu";
50
GpuService()51 GpuService::GpuService()
52 : mGpuMem(std::make_shared<GpuMem>()),
53 mGpuStats(std::make_unique<GpuStats>()),
54 mGpuMemTracer(std::make_unique<GpuMemTracer>()) {
55 std::thread asyncInitThread([this]() {
56 mGpuMem->initialize();
57 mGpuMemTracer->initialize(mGpuMem);
58 });
59 asyncInitThread.detach();
60 };
61
setGpuStats(const std::string & driverPackageName,const std::string & driverVersionName,uint64_t driverVersionCode,int64_t driverBuildTime,const std::string & appPackageName,const int32_t vulkanVersion,GpuStatsInfo::Driver driver,bool isDriverLoaded,int64_t driverLoadingTime)62 void GpuService::setGpuStats(const std::string& driverPackageName,
63 const std::string& driverVersionName, uint64_t driverVersionCode,
64 int64_t driverBuildTime, const std::string& appPackageName,
65 const int32_t vulkanVersion, GpuStatsInfo::Driver driver,
66 bool isDriverLoaded, int64_t driverLoadingTime) {
67 mGpuStats->insertDriverStats(driverPackageName, driverVersionName, driverVersionCode,
68 driverBuildTime, appPackageName, vulkanVersion, driver,
69 isDriverLoaded, driverLoadingTime);
70 }
71
setTargetStats(const std::string & appPackageName,const uint64_t driverVersionCode,const GpuStatsInfo::Stats stats,const uint64_t value)72 void GpuService::setTargetStats(const std::string& appPackageName, const uint64_t driverVersionCode,
73 const GpuStatsInfo::Stats stats, const uint64_t value) {
74 mGpuStats->insertTargetStats(appPackageName, driverVersionCode, stats, value);
75 }
76
setUpdatableDriverPath(const std::string & driverPath)77 void GpuService::setUpdatableDriverPath(const std::string& driverPath) {
78 IPCThreadState* ipc = IPCThreadState::self();
79 const int pid = ipc->getCallingPid();
80 const int uid = ipc->getCallingUid();
81
82 // only system_server is allowed to set updatable driver path
83 if (uid != AID_SYSTEM) {
84 ALOGE("Permission Denial: can't set updatable driver path from pid=%d, uid=%d\n", pid, uid);
85 return;
86 }
87
88 std::lock_guard<std::mutex> lock(mLock);
89 mDeveloperDriverPath = driverPath;
90 }
91
getUpdatableDriverPath()92 std::string GpuService::getUpdatableDriverPath() {
93 std::lock_guard<std::mutex> lock(mLock);
94 return mDeveloperDriverPath;
95 }
96
shellCommand(int,int out,int err,std::vector<String16> & args)97 status_t GpuService::shellCommand(int /*in*/, int out, int err, std::vector<String16>& args) {
98 ATRACE_CALL();
99
100 ALOGV("shellCommand");
101 for (size_t i = 0, n = args.size(); i < n; i++)
102 ALOGV(" arg[%zu]: '%s'", i, String8(args[i]).string());
103
104 if (args.size() >= 1) {
105 if (args[0] == String16("vkjson")) return cmdVkjson(out, err);
106 if (args[0] == String16("help")) return cmdHelp(out);
107 }
108 // no command, or unrecognized command
109 cmdHelp(err);
110 return BAD_VALUE;
111 }
112
doDump(int fd,const Vector<String16> & args,bool)113 status_t GpuService::doDump(int fd, const Vector<String16>& args, bool /*asProto*/) {
114 std::string result;
115
116 IPCThreadState* ipc = IPCThreadState::self();
117 const int pid = ipc->getCallingPid();
118 const int uid = ipc->getCallingUid();
119
120 if ((uid != AID_SHELL) && !PermissionCache::checkPermission(sDump, pid, uid)) {
121 StringAppendF(&result, "Permission Denial: can't dump gpu from pid=%d, uid=%d\n", pid, uid);
122 } else {
123 bool dumpAll = true;
124 bool dumpDriverInfo = false;
125 bool dumpMem = false;
126 bool dumpStats = false;
127 size_t numArgs = args.size();
128
129 if (numArgs) {
130 for (size_t index = 0; index < numArgs; ++index) {
131 if (args[index] == String16("--gpustats")) {
132 dumpStats = true;
133 } else if (args[index] == String16("--gpudriverinfo")) {
134 dumpDriverInfo = true;
135 } else if (args[index] == String16("--gpumem")) {
136 dumpMem = true;
137 }
138 }
139 dumpAll = !(dumpDriverInfo || dumpMem || dumpStats);
140 }
141
142 if (dumpAll || dumpDriverInfo) {
143 dumpGameDriverInfo(&result);
144 result.append("\n");
145 }
146 if (dumpAll || dumpMem) {
147 mGpuMem->dump(args, &result);
148 result.append("\n");
149 }
150 if (dumpAll || dumpStats) {
151 mGpuStats->dump(args, &result);
152 result.append("\n");
153 }
154 }
155
156 write(fd, result.c_str(), result.size());
157 return NO_ERROR;
158 }
159
160 namespace {
161
cmdHelp(int out)162 status_t cmdHelp(int out) {
163 FILE* outs = fdopen(out, "w");
164 if (!outs) {
165 ALOGE("vkjson: failed to create out stream: %s (%d)", strerror(errno), errno);
166 return BAD_VALUE;
167 }
168 fprintf(outs,
169 "GPU Service commands:\n"
170 " vkjson dump Vulkan properties as JSON\n");
171 fclose(outs);
172 return NO_ERROR;
173 }
174
vkjsonPrint(FILE * out)175 void vkjsonPrint(FILE* out) {
176 std::string json = VkJsonInstanceToJson(VkJsonGetInstance());
177 fwrite(json.data(), 1, json.size(), out);
178 fputc('\n', out);
179 }
180
cmdVkjson(int out,int)181 status_t cmdVkjson(int out, int /*err*/) {
182 FILE* outs = fdopen(out, "w");
183 if (!outs) {
184 int errnum = errno;
185 ALOGE("vkjson: failed to create output stream: %s", strerror(errnum));
186 return -errnum;
187 }
188 vkjsonPrint(outs);
189 fclose(outs);
190 return NO_ERROR;
191 }
192
dumpGameDriverInfo(std::string * result)193 void dumpGameDriverInfo(std::string* result) {
194 if (!result) return;
195
196 char stableGameDriver[PROPERTY_VALUE_MAX] = {};
197 property_get("ro.gfx.driver.0", stableGameDriver, "unsupported");
198 StringAppendF(result, "Stable Game Driver: %s\n", stableGameDriver);
199
200 char preReleaseGameDriver[PROPERTY_VALUE_MAX] = {};
201 property_get("ro.gfx.driver.1", preReleaseGameDriver, "unsupported");
202 StringAppendF(result, "Pre-release Game Driver: %s\n", preReleaseGameDriver);
203 }
204
205 } // anonymous namespace
206
207 } // namespace android
208