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/GpuService.h"
20
21 #include <android-base/stringprintf.h>
22 #include <android-base/properties.h>
23 #include <binder/IPCThreadState.h>
24 #include <binder/IResultReceiver.h>
25 #include <binder/Parcel.h>
26 #include <binder/PermissionCache.h>
27 #include <cutils/properties.h>
28 #include <gpumem/GpuMem.h>
29 #include <gpuwork/GpuWork.h>
30 #include <gpustats/GpuStats.h>
31 #include <private/android_filesystem_config.h>
32 #include <tracing/GpuMemTracer.h>
33 #include <utils/String8.h>
34 #include <utils/Trace.h>
35 #include <vkjson.h>
36 #include <vkprofiles.h>
37
38 #include <thread>
39 #include <memory>
40
41 namespace android {
42
43 using base::StringAppendF;
44
45 namespace {
46 status_t cmdHelp(int out);
47 status_t cmdVkjson(int out, int err);
48 status_t cmdVkprofiles(int out, int err);
49 void dumpGameDriverInfo(std::string* result);
50 } // namespace
51
52 const String16 sDump("android.permission.DUMP");
53 const String16 sAccessGpuServicePermission("android.permission.ACCESS_GPU_SERVICE");
54 const std::string sAngleGlesDriverSuffix = "angle";
55
56 const char* const GpuService::SERVICE_NAME = "gpu";
57
GpuService()58 GpuService::GpuService()
59 : mGpuMem(std::make_shared<GpuMem>()),
60 mGpuWork(std::make_shared<gpuwork::GpuWork>()),
61 mGpuStats(std::make_unique<GpuStats>()),
62 mGpuMemTracer(std::make_unique<GpuMemTracer>()) {
63
64 mGpuMemAsyncInitThread = std::make_unique<std::thread>([this] (){
65 mGpuMem->initialize();
66 mGpuMemTracer->initialize(mGpuMem);
67 });
68
69 mGpuWorkAsyncInitThread = std::make_unique<std::thread>([this]() {
70 mGpuWork->initialize();
71 });
72 };
73
~GpuService()74 GpuService::~GpuService() {
75 mGpuMem->stop();
76 mGpuWork->stop();
77
78 mGpuWorkAsyncInitThread->join();
79 mGpuMemAsyncInitThread->join();
80 }
81
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)82 void GpuService::setGpuStats(const std::string& driverPackageName,
83 const std::string& driverVersionName, uint64_t driverVersionCode,
84 int64_t driverBuildTime, const std::string& appPackageName,
85 const int32_t vulkanVersion, GpuStatsInfo::Driver driver,
86 bool isDriverLoaded, int64_t driverLoadingTime) {
87 mGpuStats->insertDriverStats(driverPackageName, driverVersionName, driverVersionCode,
88 driverBuildTime, appPackageName, vulkanVersion, driver,
89 isDriverLoaded, driverLoadingTime);
90 }
91
setTargetStats(const std::string & appPackageName,const uint64_t driverVersionCode,const GpuStatsInfo::Stats stats,const uint64_t value)92 void GpuService::setTargetStats(const std::string& appPackageName, const uint64_t driverVersionCode,
93 const GpuStatsInfo::Stats stats, const uint64_t value) {
94 mGpuStats->insertTargetStats(appPackageName, driverVersionCode, stats, value);
95 }
96
setTargetStatsArray(const std::string & appPackageName,const uint64_t driverVersionCode,const GpuStatsInfo::Stats stats,const uint64_t * values,const uint32_t valueCount)97 void GpuService::setTargetStatsArray(const std::string& appPackageName,
98 const uint64_t driverVersionCode, const GpuStatsInfo::Stats stats,
99 const uint64_t* values, const uint32_t valueCount) {
100 mGpuStats->insertTargetStatsArray(appPackageName, driverVersionCode, stats, values, valueCount);
101 }
102
addVulkanEngineName(const std::string & appPackageName,const uint64_t driverVersionCode,const char * engineName)103 void GpuService::addVulkanEngineName(const std::string& appPackageName,
104 const uint64_t driverVersionCode,
105 const char* engineName) {
106 mGpuStats->addVulkanEngineName(appPackageName, driverVersionCode, engineName);
107 }
108
toggleAngleAsSystemDriver(bool enabled)109 void GpuService::toggleAngleAsSystemDriver(bool enabled) {
110 IPCThreadState* ipc = IPCThreadState::self();
111 const int pid = ipc->getCallingPid();
112 const int uid = ipc->getCallingUid();
113
114 // only system_server with the ACCESS_GPU_SERVICE permission is allowed to set
115 // persist.graphics.egl
116 if (uid != AID_SYSTEM ||
117 !PermissionCache::checkPermission(sAccessGpuServicePermission, pid, uid)) {
118 ALOGE("Permission Denial: can't set persist.graphics.egl from setAngleAsSystemDriver() "
119 "pid=%d, uid=%d\n", pid, uid);
120 return;
121 }
122
123 std::lock_guard<std::mutex> lock(mLock);
124 if (enabled) {
125 android::base::SetProperty("persist.graphics.egl", sAngleGlesDriverSuffix);
126 } else {
127 android::base::SetProperty("persist.graphics.egl", "");
128 }
129 }
130
131
setUpdatableDriverPath(const std::string & driverPath)132 void GpuService::setUpdatableDriverPath(const std::string& driverPath) {
133 IPCThreadState* ipc = IPCThreadState::self();
134 const int pid = ipc->getCallingPid();
135 const int uid = ipc->getCallingUid();
136
137 // only system_server is allowed to set updatable driver path
138 if (uid != AID_SYSTEM) {
139 ALOGE("Permission Denial: can't set updatable driver path from pid=%d, uid=%d\n", pid, uid);
140 return;
141 }
142
143 std::lock_guard<std::mutex> lock(mLock);
144 mDeveloperDriverPath = driverPath;
145 }
146
getUpdatableDriverPath()147 std::string GpuService::getUpdatableDriverPath() {
148 std::lock_guard<std::mutex> lock(mLock);
149 return mDeveloperDriverPath;
150 }
151
shellCommand(int,int out,int err,std::vector<String16> & args)152 status_t GpuService::shellCommand(int /*in*/, int out, int err, std::vector<String16>& args) {
153 ATRACE_CALL();
154
155 ALOGV("shellCommand");
156 for (size_t i = 0, n = args.size(); i < n; i++)
157 ALOGV(" arg[%zu]: '%s'", i, String8(args[i]).c_str());
158
159 if (args.size() >= 1) {
160 if (args[0] == String16("vkjson")) return cmdVkjson(out, err);
161 if (args[0] == String16("vkprofiles")) return cmdVkprofiles(out, err);
162 if (args[0] == String16("help")) return cmdHelp(out);
163 }
164 // no command, or unrecognized command
165 cmdHelp(err);
166 return BAD_VALUE;
167 }
168
doDump(int fd,const Vector<String16> & args,bool)169 status_t GpuService::doDump(int fd, const Vector<String16>& args, bool /*asProto*/) {
170 std::string result;
171
172 IPCThreadState* ipc = IPCThreadState::self();
173 const int pid = ipc->getCallingPid();
174 const int uid = ipc->getCallingUid();
175
176 if ((uid != AID_SHELL) && !PermissionCache::checkPermission(sDump, pid, uid)) {
177 StringAppendF(&result, "Permission Denial: can't dump gpu from pid=%d, uid=%d\n", pid, uid);
178 } else {
179 bool dumpAll = true;
180 bool dumpDriverInfo = false;
181 bool dumpMem = false;
182 bool dumpStats = false;
183 bool dumpWork = false;
184 size_t numArgs = args.size();
185
186 if (numArgs) {
187 for (size_t index = 0; index < numArgs; ++index) {
188 if (args[index] == String16("--gpustats")) {
189 dumpStats = true;
190 } else if (args[index] == String16("--gpudriverinfo")) {
191 dumpDriverInfo = true;
192 } else if (args[index] == String16("--gpumem")) {
193 dumpMem = true;
194 } else if (args[index] == String16("--gpuwork")) {
195 dumpWork = true;
196 }
197 }
198 dumpAll = !(dumpDriverInfo || dumpMem || dumpStats || dumpWork);
199 }
200
201 if (dumpAll || dumpDriverInfo) {
202 dumpGameDriverInfo(&result);
203 result.append("\n");
204 }
205 if (dumpAll || dumpMem) {
206 mGpuMem->dump(args, &result);
207 result.append("\n");
208 }
209 if (dumpAll || dumpStats) {
210 mGpuStats->dump(args, &result);
211 result.append("\n");
212 }
213 if (dumpAll || dumpWork) {
214 mGpuWork->dump(args, &result);
215 result.append("\n");
216 }
217 }
218
219 write(fd, result.c_str(), result.size());
220 return NO_ERROR;
221 }
222
223 namespace {
224
cmdHelp(int out)225 status_t cmdHelp(int out) {
226 FILE* outs = fdopen(out, "w");
227 if (!outs) {
228 ALOGE("gpuservice: failed to create out stream: %s (%d)", strerror(errno), errno);
229 return BAD_VALUE;
230 }
231 fprintf(outs,
232 "GPU Service commands:\n"
233 " vkjson dump Vulkan properties as JSON\n"
234 " vkprofiles print support for select Vulkan profiles\n");
235 fclose(outs);
236 return NO_ERROR;
237 }
238
cmdVkjson(int out,int)239 status_t cmdVkjson(int out, int /*err*/) {
240 dprintf(out, "%s\n", VkJsonInstanceToJson(VkJsonGetInstance()).c_str());
241 return NO_ERROR;
242 }
243
cmdVkprofiles(int out,int)244 status_t cmdVkprofiles(int out, int /*err*/) {
245 dprintf(out, "%s\n", android::vkprofiles::vkProfiles().c_str());
246 return NO_ERROR;
247 }
248
dumpGameDriverInfo(std::string * result)249 void dumpGameDriverInfo(std::string* result) {
250 if (!result) return;
251
252 char stableGameDriver[PROPERTY_VALUE_MAX] = {};
253 property_get("ro.gfx.driver.0", stableGameDriver, "unsupported");
254 StringAppendF(result, "Stable Game Driver: %s\n", stableGameDriver);
255
256 char preReleaseGameDriver[PROPERTY_VALUE_MAX] = {};
257 property_get("ro.gfx.driver.1", preReleaseGameDriver, "unsupported");
258 StringAppendF(result, "Pre-release Game Driver: %s\n", preReleaseGameDriver);
259 }
260
261 } // anonymous namespace
262
263 } // namespace android
264