| 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> | 
|  | 26 | #include <private/android_filesystem_config.h> | 
| Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 27 | #include <utils/String8.h> | 
| Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 28 | #include <utils/Trace.h> | 
|  | 29 |  | 
| Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 30 | #include <vkjson.h> | 
| Jesse Hall | fc038bd | 2016-03-26 22:20:22 -0700 | [diff] [blame] | 31 |  | 
|  | 32 | namespace android { | 
|  | 33 |  | 
| Yiwei Zhang | bb4eaf6 | 2019-02-23 17:53:27 -0800 | [diff] [blame] | 34 | using base::StringAppendF; | 
| Jesse Hall | fc038bd | 2016-03-26 22:20:22 -0700 | [diff] [blame] | 35 |  | 
| Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 36 | namespace { | 
| Yiwei Zhang | bb4eaf6 | 2019-02-23 17:53:27 -0800 | [diff] [blame] | 37 | status_t cmdHelp(int out); | 
|  | 38 | status_t cmdVkjson(int out, int err); | 
|  | 39 | } // namespace | 
|  | 40 |  | 
|  | 41 | const String16 sDump("android.permission.DUMP"); | 
| Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 42 |  | 
| Yiwei Zhang | 423d080 | 2018-11-16 10:56:12 -0800 | [diff] [blame] | 43 | const char* const GpuService::SERVICE_NAME = "gpu"; | 
| Jesse Hall | fc038bd | 2016-03-26 22:20:22 -0700 | [diff] [blame] | 44 |  | 
| Peiyong Lin | 2da7465 | 2018-11-01 10:34:57 -0700 | [diff] [blame] | 45 | GpuService::GpuService() = default; | 
| Jesse Hall | fc038bd | 2016-03-26 22:20:22 -0700 | [diff] [blame] | 46 |  | 
| Greg Kaiser | 210bb7e | 2019-02-12 12:40:05 -0800 | [diff] [blame] | 47 | void GpuService::setGpuStats(const std::string& driverPackageName, | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 48 | const std::string& driverVersionName, uint64_t driverVersionCode, | 
| Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 49 | int64_t driverBuildTime, const std::string& appPackageName, | 
| Yiwei Zhang | 512a723 | 2019-02-15 16:04:41 -0800 | [diff] [blame] | 50 | GraphicsEnv::Driver driver, bool isDriverLoaded, | 
|  | 51 | int64_t driverLoadingTime) { | 
| Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 52 | ATRACE_CALL(); | 
|  | 53 |  | 
|  | 54 | std::lock_guard<std::mutex> lock(mStateLock); | 
|  | 55 | ALOGV("Received:\n" | 
|  | 56 | "\tdriverPackageName[%s]\n" | 
|  | 57 | "\tdriverVersionName[%s]\n" | 
| Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 58 | "\tdriverVersionCode[%" PRIu64 "]\n" | 
|  | 59 | "\tdriverBuildTime[%" PRId64 "]\n" | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 60 | "\tappPackageName[%s]\n" | 
|  | 61 | "\tdriver[%d]\n" | 
|  | 62 | "\tisDriverLoaded[%d]\n" | 
| Yiwei Zhang | 96c0171 | 2019-02-19 16:00:25 -0800 | [diff] [blame] | 63 | "\tdriverLoadingTime[%" PRId64 "]", | 
|  | 64 | driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime, | 
|  | 65 | appPackageName.c_str(), static_cast<int32_t>(driver), isDriverLoaded, driverLoadingTime); | 
| Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 66 | } | 
|  | 67 |  | 
|  | 68 | status_t GpuService::shellCommand(int /*in*/, int out, int err, std::vector<String16>& args) { | 
|  | 69 | ATRACE_CALL(); | 
|  | 70 |  | 
|  | 71 | ALOGV("shellCommand"); | 
| Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 72 | for (size_t i = 0, n = args.size(); i < n; i++) | 
|  | 73 | ALOGV("  arg[%zu]: '%s'", i, String8(args[i]).string()); | 
|  | 74 |  | 
| Jesse Hall | 3e26b13 | 2016-12-20 14:31:28 -0800 | [diff] [blame] | 75 | if (args.size() >= 1) { | 
| Yiwei Zhang | bb4eaf6 | 2019-02-23 17:53:27 -0800 | [diff] [blame] | 76 | if (args[0] == String16("vkjson")) return cmdVkjson(out, err); | 
|  | 77 | if (args[0] == String16("help")) return cmdHelp(out); | 
| Jesse Hall | 3e26b13 | 2016-12-20 14:31:28 -0800 | [diff] [blame] | 78 | } | 
|  | 79 | // no command, or unrecognized command | 
| Yiwei Zhang | bb4eaf6 | 2019-02-23 17:53:27 -0800 | [diff] [blame] | 80 | cmdHelp(err); | 
| Jesse Hall | 3e26b13 | 2016-12-20 14:31:28 -0800 | [diff] [blame] | 81 | return BAD_VALUE; | 
| Jesse Hall | fc038bd | 2016-03-26 22:20:22 -0700 | [diff] [blame] | 82 | } | 
|  | 83 |  | 
| Yiwei Zhang | bb4eaf6 | 2019-02-23 17:53:27 -0800 | [diff] [blame] | 84 | status_t GpuService::doDump(int fd, const Vector<String16>& /*args*/, bool /*asProto*/) { | 
|  | 85 | std::string result; | 
|  | 86 |  | 
|  | 87 | IPCThreadState* ipc = IPCThreadState::self(); | 
|  | 88 | const int pid = ipc->getCallingPid(); | 
|  | 89 | const int uid = ipc->getCallingUid(); | 
|  | 90 |  | 
|  | 91 | if ((uid != AID_SHELL) && !PermissionCache::checkPermission(sDump, pid, uid)) { | 
|  | 92 | StringAppendF(&result, "Permission Denial: can't dump gpu from pid=%d, uid=%d\n", pid, uid); | 
|  | 93 | } else { | 
|  | 94 | result.append("Hello world from dumpsys gpu.\n"); | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | write(fd, result.c_str(), result.size()); | 
|  | 98 | return NO_ERROR; | 
|  | 99 | } | 
|  | 100 |  | 
| Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 101 | namespace { | 
|  | 102 |  | 
| Yiwei Zhang | bb4eaf6 | 2019-02-23 17:53:27 -0800 | [diff] [blame] | 103 | status_t cmdHelp(int out) { | 
| Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 104 | FILE* outs = fdopen(out, "w"); | 
|  | 105 | if (!outs) { | 
| Yiwei Zhang | bb4eaf6 | 2019-02-23 17:53:27 -0800 | [diff] [blame] | 106 | ALOGE("vkjson: failed to create out stream: %s (%d)", strerror(errno), errno); | 
| Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 107 | return BAD_VALUE; | 
|  | 108 | } | 
|  | 109 | fprintf(outs, | 
| Yiwei Zhang | bb4eaf6 | 2019-02-23 17:53:27 -0800 | [diff] [blame] | 110 | "GPU Service commands:\n" | 
|  | 111 | "  vkjson   dump Vulkan properties as JSON\n"); | 
| Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 112 | fclose(outs); | 
|  | 113 | return NO_ERROR; | 
|  | 114 | } | 
|  | 115 |  | 
| Jesse Hall | 3841bf4 | 2016-06-12 15:08:28 -0700 | [diff] [blame] | 116 | void vkjsonPrint(FILE* out) { | 
|  | 117 | std::string json = VkJsonInstanceToJson(VkJsonGetInstance()); | 
|  | 118 | fwrite(json.data(), 1, json.size(), out); | 
|  | 119 | fputc('\n', out); | 
| Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 120 | } | 
|  | 121 |  | 
| Yiwei Zhang | bb4eaf6 | 2019-02-23 17:53:27 -0800 | [diff] [blame] | 122 | status_t cmdVkjson(int out, int /*err*/) { | 
| Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 123 | FILE* outs = fdopen(out, "w"); | 
|  | 124 | if (!outs) { | 
| Jesse Hall | 3841bf4 | 2016-06-12 15:08:28 -0700 | [diff] [blame] | 125 | int errnum = errno; | 
| Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 126 | ALOGE("vkjson: failed to create output stream: %s", strerror(errnum)); | 
|  | 127 | return -errnum; | 
|  | 128 | } | 
| Jesse Hall | 3841bf4 | 2016-06-12 15:08:28 -0700 | [diff] [blame] | 129 | vkjsonPrint(outs); | 
| Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 130 | fclose(outs); | 
| Jesse Hall | 3841bf4 | 2016-06-12 15:08:28 -0700 | [diff] [blame] | 131 | return NO_ERROR; | 
| Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 132 | } | 
|  | 133 |  | 
|  | 134 | } // anonymous namespace | 
|  | 135 |  | 
| Jesse Hall | fc038bd | 2016-03-26 22:20:22 -0700 | [diff] [blame] | 136 | } // namespace android |