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 | baaef88 | 2020-02-02 17:45:30 -0800 | [diff] [blame] | 27 | #include <gpustats/GpuStats.h> |
Yiwei Zhang | bb4eaf6 | 2019-02-23 17:53:27 -0800 | [diff] [blame] | 28 | #include <private/android_filesystem_config.h> |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 29 | #include <utils/String8.h> |
Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 30 | #include <utils/Trace.h> |
| 31 | |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 32 | #include <vkjson.h> |
Jesse Hall | fc038bd | 2016-03-26 22:20:22 -0700 | [diff] [blame] | 33 | |
| 34 | namespace android { |
| 35 | |
Yiwei Zhang | bb4eaf6 | 2019-02-23 17:53:27 -0800 | [diff] [blame] | 36 | using base::StringAppendF; |
Jesse Hall | fc038bd | 2016-03-26 22:20:22 -0700 | [diff] [blame] | 37 | |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 38 | namespace { |
Yiwei Zhang | bb4eaf6 | 2019-02-23 17:53:27 -0800 | [diff] [blame] | 39 | status_t cmdHelp(int out); |
| 40 | status_t cmdVkjson(int out, int err); |
Yiwei Zhang | 2a61c15 | 2019-02-27 11:27:18 -0800 | [diff] [blame] | 41 | void dumpGameDriverInfo(std::string* result); |
Yiwei Zhang | bb4eaf6 | 2019-02-23 17:53:27 -0800 | [diff] [blame] | 42 | } // namespace |
| 43 | |
| 44 | const String16 sDump("android.permission.DUMP"); |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 45 | |
Yiwei Zhang | 423d080 | 2018-11-16 10:56:12 -0800 | [diff] [blame] | 46 | const char* const GpuService::SERVICE_NAME = "gpu"; |
Jesse Hall | fc038bd | 2016-03-26 22:20:22 -0700 | [diff] [blame] | 47 | |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 48 | GpuService::GpuService() : mGpuStats(std::make_unique<GpuStats>()){}; |
Jesse Hall | fc038bd | 2016-03-26 22:20:22 -0700 | [diff] [blame] | 49 | |
Greg Kaiser | 210bb7e | 2019-02-12 12:40:05 -0800 | [diff] [blame] | 50 | void GpuService::setGpuStats(const std::string& driverPackageName, |
Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 51 | const std::string& driverVersionName, uint64_t driverVersionCode, |
Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 52 | int64_t driverBuildTime, const std::string& appPackageName, |
Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 53 | const int32_t vulkanVersion, GpuStatsInfo::Driver driver, |
Yiwei Zhang | 794d295 | 2019-05-06 17:43:59 -0700 | [diff] [blame] | 54 | bool isDriverLoaded, int64_t driverLoadingTime) { |
Yiwei Zhang | fdd7e78 | 2020-01-31 15:59:34 -0800 | [diff] [blame^] | 55 | mGpuStats->insertDriverStats(driverPackageName, driverVersionName, driverVersionCode, |
| 56 | driverBuildTime, appPackageName, vulkanVersion, driver, |
| 57 | isDriverLoaded, driverLoadingTime); |
Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Yiwei Zhang | baea97f | 2019-02-27 16:35:37 -0800 | [diff] [blame] | 60 | status_t GpuService::getGpuStatsGlobalInfo(std::vector<GpuStatsGlobalInfo>* outStats) const { |
Yiwei Zhang | baea97f | 2019-02-27 16:35:37 -0800 | [diff] [blame] | 61 | mGpuStats->pullGlobalStats(outStats); |
Yiwei Zhang | baea97f | 2019-02-27 16:35:37 -0800 | [diff] [blame] | 62 | return OK; |
| 63 | } |
| 64 | |
Yiwei Zhang | 178e067 | 2019-03-04 14:17:12 -0800 | [diff] [blame] | 65 | status_t GpuService::getGpuStatsAppInfo(std::vector<GpuStatsAppInfo>* outStats) const { |
Yiwei Zhang | 178e067 | 2019-03-04 14:17:12 -0800 | [diff] [blame] | 66 | mGpuStats->pullAppStats(outStats); |
Yiwei Zhang | 178e067 | 2019-03-04 14:17:12 -0800 | [diff] [blame] | 67 | return OK; |
| 68 | } |
| 69 | |
Yiwei Zhang | bcba411 | 2019-07-03 13:39:32 -0700 | [diff] [blame] | 70 | void GpuService::setTargetStats(const std::string& appPackageName, const uint64_t driverVersionCode, |
| 71 | const GpuStatsInfo::Stats stats, const uint64_t value) { |
| 72 | mGpuStats->insertTargetStats(appPackageName, driverVersionCode, stats, value); |
Yiwei Zhang | 8c5e3bd | 2019-05-09 14:34:19 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 75 | status_t GpuService::shellCommand(int /*in*/, int out, int err, std::vector<String16>& args) { |
| 76 | ATRACE_CALL(); |
| 77 | |
| 78 | ALOGV("shellCommand"); |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 79 | for (size_t i = 0, n = args.size(); i < n; i++) |
| 80 | ALOGV(" arg[%zu]: '%s'", i, String8(args[i]).string()); |
| 81 | |
Jesse Hall | 3e26b13 | 2016-12-20 14:31:28 -0800 | [diff] [blame] | 82 | if (args.size() >= 1) { |
Yiwei Zhang | bb4eaf6 | 2019-02-23 17:53:27 -0800 | [diff] [blame] | 83 | if (args[0] == String16("vkjson")) return cmdVkjson(out, err); |
| 84 | if (args[0] == String16("help")) return cmdHelp(out); |
Jesse Hall | 3e26b13 | 2016-12-20 14:31:28 -0800 | [diff] [blame] | 85 | } |
| 86 | // no command, or unrecognized command |
Yiwei Zhang | bb4eaf6 | 2019-02-23 17:53:27 -0800 | [diff] [blame] | 87 | cmdHelp(err); |
Jesse Hall | 3e26b13 | 2016-12-20 14:31:28 -0800 | [diff] [blame] | 88 | return BAD_VALUE; |
Jesse Hall | fc038bd | 2016-03-26 22:20:22 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 91 | status_t GpuService::doDump(int fd, const Vector<String16>& args, bool /*asProto*/) { |
Yiwei Zhang | bb4eaf6 | 2019-02-23 17:53:27 -0800 | [diff] [blame] | 92 | std::string result; |
| 93 | |
| 94 | IPCThreadState* ipc = IPCThreadState::self(); |
| 95 | const int pid = ipc->getCallingPid(); |
| 96 | const int uid = ipc->getCallingUid(); |
| 97 | |
| 98 | if ((uid != AID_SHELL) && !PermissionCache::checkPermission(sDump, pid, uid)) { |
| 99 | StringAppendF(&result, "Permission Denial: can't dump gpu from pid=%d, uid=%d\n", pid, uid); |
| 100 | } else { |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 101 | bool dumpAll = true; |
Mikael Pessa | 3d14605 | 2019-07-09 09:25:38 -0700 | [diff] [blame] | 102 | bool dumpDriverInfo = false; |
| 103 | bool dumpStats = false; |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 104 | size_t numArgs = args.size(); |
| 105 | |
| 106 | if (numArgs) { |
Mikael Pessa | 3d14605 | 2019-07-09 09:25:38 -0700 | [diff] [blame] | 107 | dumpAll = false; |
| 108 | for (size_t index = 0; index < numArgs; ++index) { |
| 109 | if (args[index] == String16("--gpustats")) { |
| 110 | dumpStats = true; |
| 111 | } else if (args[index] == String16("--gpudriverinfo")) { |
| 112 | dumpDriverInfo = true; |
Mikael Pessa | 3d14605 | 2019-07-09 09:25:38 -0700 | [diff] [blame] | 113 | } |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
Mikael Pessa | 3d14605 | 2019-07-09 09:25:38 -0700 | [diff] [blame] | 117 | if (dumpAll || dumpDriverInfo) { |
Yiwei Zhang | 2a61c15 | 2019-02-27 11:27:18 -0800 | [diff] [blame] | 118 | dumpGameDriverInfo(&result); |
| 119 | result.append("\n"); |
Mikael Pessa | 3d14605 | 2019-07-09 09:25:38 -0700 | [diff] [blame] | 120 | } |
| 121 | if (dumpAll || dumpStats) { |
Yiwei Zhang | 8cf7c7b | 2019-08-28 13:07:30 -0700 | [diff] [blame] | 122 | mGpuStats->dump(args, &result); |
Yiwei Zhang | 2a61c15 | 2019-02-27 11:27:18 -0800 | [diff] [blame] | 123 | result.append("\n"); |
Yiwei Zhang | 2d4c188 | 2019-02-24 22:28:08 -0800 | [diff] [blame] | 124 | } |
Yiwei Zhang | bb4eaf6 | 2019-02-23 17:53:27 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | write(fd, result.c_str(), result.size()); |
| 128 | return NO_ERROR; |
| 129 | } |
| 130 | |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 131 | namespace { |
| 132 | |
Yiwei Zhang | bb4eaf6 | 2019-02-23 17:53:27 -0800 | [diff] [blame] | 133 | status_t cmdHelp(int out) { |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 134 | FILE* outs = fdopen(out, "w"); |
| 135 | if (!outs) { |
Yiwei Zhang | bb4eaf6 | 2019-02-23 17:53:27 -0800 | [diff] [blame] | 136 | ALOGE("vkjson: failed to create out stream: %s (%d)", strerror(errno), errno); |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 137 | return BAD_VALUE; |
| 138 | } |
| 139 | fprintf(outs, |
Yiwei Zhang | bb4eaf6 | 2019-02-23 17:53:27 -0800 | [diff] [blame] | 140 | "GPU Service commands:\n" |
| 141 | " vkjson dump Vulkan properties as JSON\n"); |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 142 | fclose(outs); |
| 143 | return NO_ERROR; |
| 144 | } |
| 145 | |
Jesse Hall | 3841bf4 | 2016-06-12 15:08:28 -0700 | [diff] [blame] | 146 | void vkjsonPrint(FILE* out) { |
| 147 | std::string json = VkJsonInstanceToJson(VkJsonGetInstance()); |
| 148 | fwrite(json.data(), 1, json.size(), out); |
| 149 | fputc('\n', out); |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Yiwei Zhang | bb4eaf6 | 2019-02-23 17:53:27 -0800 | [diff] [blame] | 152 | status_t cmdVkjson(int out, int /*err*/) { |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 153 | FILE* outs = fdopen(out, "w"); |
| 154 | if (!outs) { |
Jesse Hall | 3841bf4 | 2016-06-12 15:08:28 -0700 | [diff] [blame] | 155 | int errnum = errno; |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 156 | ALOGE("vkjson: failed to create output stream: %s", strerror(errnum)); |
| 157 | return -errnum; |
| 158 | } |
Jesse Hall | 3841bf4 | 2016-06-12 15:08:28 -0700 | [diff] [blame] | 159 | vkjsonPrint(outs); |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 160 | fclose(outs); |
Jesse Hall | 3841bf4 | 2016-06-12 15:08:28 -0700 | [diff] [blame] | 161 | return NO_ERROR; |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Yiwei Zhang | 2a61c15 | 2019-02-27 11:27:18 -0800 | [diff] [blame] | 164 | void dumpGameDriverInfo(std::string* result) { |
| 165 | if (!result) return; |
| 166 | |
| 167 | char stableGameDriver[PROPERTY_VALUE_MAX] = {}; |
| 168 | property_get("ro.gfx.driver.0", stableGameDriver, "unsupported"); |
| 169 | StringAppendF(result, "Stable Game Driver: %s\n", stableGameDriver); |
| 170 | |
| 171 | char preReleaseGameDriver[PROPERTY_VALUE_MAX] = {}; |
| 172 | property_get("ro.gfx.driver.1", preReleaseGameDriver, "unsupported"); |
| 173 | StringAppendF(result, "Pre-release Game Driver: %s\n", preReleaseGameDriver); |
| 174 | } |
| 175 | |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 176 | } // anonymous namespace |
| 177 | |
Jesse Hall | fc038bd | 2016-03-26 22:20:22 -0700 | [diff] [blame] | 178 | } // namespace android |