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