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