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