| Yiwei Zhang | 99fc75a | 2020-04-30 10:46:39 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright 2020 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 | #undef LOG_TAG | 
 | 18 | #define LOG_TAG "GpuMem" | 
 | 19 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS | 
 | 20 |  | 
 | 21 | #include "gpumem/GpuMem.h" | 
 | 22 |  | 
 | 23 | #include <android-base/stringprintf.h> | 
 | 24 | #include <libbpf.h> | 
 | 25 | #include <libbpf_android.h> | 
 | 26 | #include <log/log.h> | 
| Yiwei Zhang | a265b40 | 2020-06-25 22:02:29 -0700 | [diff] [blame] | 27 | #include <unistd.h> | 
| Yiwei Zhang | 5cd71a2 | 2020-08-13 17:39:51 -0700 | [diff] [blame] | 28 | #include <utils/Timers.h> | 
| Yiwei Zhang | 99fc75a | 2020-04-30 10:46:39 -0700 | [diff] [blame] | 29 | #include <utils/Trace.h> | 
 | 30 |  | 
 | 31 | #include <unordered_map> | 
 | 32 | #include <vector> | 
 | 33 |  | 
 | 34 | namespace android { | 
 | 35 |  | 
 | 36 | using base::StringAppendF; | 
 | 37 |  | 
 | 38 | GpuMem::~GpuMem() { | 
 | 39 |     bpf_detach_tracepoint(kGpuMemTraceGroup, kGpuMemTotalTracepoint); | 
 | 40 | } | 
 | 41 |  | 
 | 42 | void GpuMem::initialize() { | 
 | 43 |     // Make sure bpf programs are loaded | 
 | 44 |     bpf::waitForProgsLoaded(); | 
 | 45 |  | 
 | 46 |     int fd = bpf::bpfFdGet(kGpuMemTotalProgPath, BPF_F_RDONLY); | 
 | 47 |     if (fd < 0) { | 
 | 48 |         ALOGE("Failed to retrieve pinned program from %s", kGpuMemTotalProgPath); | 
 | 49 |         return; | 
 | 50 |     } | 
 | 51 |  | 
 | 52 |     // Attach the program to the tracepoint, and the tracepoint is automatically enabled here. | 
| Yiwei Zhang | a265b40 | 2020-06-25 22:02:29 -0700 | [diff] [blame] | 53 |     int count = 0; | 
 | 54 |     while (bpf_attach_tracepoint(fd, kGpuMemTraceGroup, kGpuMemTotalTracepoint) < 0) { | 
 | 55 |         if (++count > kGpuWaitTimeout) { | 
 | 56 |             ALOGE("Failed to attach bpf program to %s/%s tracepoint", kGpuMemTraceGroup, | 
 | 57 |                   kGpuMemTotalTracepoint); | 
 | 58 |             return; | 
 | 59 |         } | 
 | 60 |         // Retry until GPU driver loaded or timeout. | 
 | 61 |         sleep(1); | 
| Yiwei Zhang | 99fc75a | 2020-04-30 10:46:39 -0700 | [diff] [blame] | 62 |     } | 
 | 63 |  | 
 | 64 |     // Use the read-only wrapper BpfMapRO to properly retrieve the read-only map. | 
 | 65 |     auto map = bpf::BpfMapRO<uint64_t, uint64_t>(kGpuMemTotalMapPath); | 
 | 66 |     if (!map.isValid()) { | 
 | 67 |         ALOGE("Failed to create bpf map from %s", kGpuMemTotalMapPath); | 
 | 68 |         return; | 
 | 69 |     } | 
 | 70 |     setGpuMemTotalMap(map); | 
| Yiwei Zhang | a265b40 | 2020-06-25 22:02:29 -0700 | [diff] [blame] | 71 |  | 
 | 72 |     mInitialized.store(true); | 
| Yiwei Zhang | 99fc75a | 2020-04-30 10:46:39 -0700 | [diff] [blame] | 73 | } | 
 | 74 |  | 
 | 75 | void GpuMem::setGpuMemTotalMap(bpf::BpfMap<uint64_t, uint64_t>& map) { | 
 | 76 |     mGpuMemTotalMap = std::move(map); | 
 | 77 | } | 
 | 78 |  | 
 | 79 | // Dump the snapshots of global and per process memory usage on all gpus | 
 | 80 | void GpuMem::dump(const Vector<String16>& /* args */, std::string* result) { | 
 | 81 |     ATRACE_CALL(); | 
 | 82 |  | 
| Yiwei Zhang | a265b40 | 2020-06-25 22:02:29 -0700 | [diff] [blame] | 83 |     if (!mInitialized.load() || !mGpuMemTotalMap.isValid()) { | 
| Yiwei Zhang | 99fc75a | 2020-04-30 10:46:39 -0700 | [diff] [blame] | 84 |         result->append("Failed to initialize GPU memory eBPF\n"); | 
 | 85 |         return; | 
 | 86 |     } | 
 | 87 |  | 
 | 88 |     auto res = mGpuMemTotalMap.getFirstKey(); | 
 | 89 |     if (!res.ok()) { | 
 | 90 |         result->append("GPU memory total usage map is empty\n"); | 
 | 91 |         return; | 
 | 92 |     } | 
 | 93 |     uint64_t key = res.value(); | 
 | 94 |     // unordered_map<gpu_id, vector<pair<pid, size>>> | 
 | 95 |     std::unordered_map<uint32_t, std::vector<std::pair<uint32_t, uint64_t>>> dumpMap; | 
 | 96 |     while (true) { | 
 | 97 |         uint32_t gpu_id = key >> 32; | 
 | 98 |         uint32_t pid = key; | 
 | 99 |  | 
 | 100 |         res = mGpuMemTotalMap.readValue(key); | 
 | 101 |         if (!res.ok()) break; | 
 | 102 |         uint64_t size = res.value(); | 
 | 103 |  | 
 | 104 |         dumpMap[gpu_id].emplace_back(pid, size); | 
 | 105 |  | 
 | 106 |         res = mGpuMemTotalMap.getNextKey(key); | 
 | 107 |         if (!res.ok()) break; | 
 | 108 |         key = res.value(); | 
 | 109 |     } | 
 | 110 |  | 
 | 111 |     for (auto& gpu : dumpMap) { | 
 | 112 |         if (gpu.second.empty()) continue; | 
 | 113 |         StringAppendF(result, "Memory snapshot for GPU %u:\n", gpu.first); | 
 | 114 |  | 
 | 115 |         std::sort(gpu.second.begin(), gpu.second.end(), | 
 | 116 |                   [](auto& l, auto& r) { return l.first < r.first; }); | 
 | 117 |  | 
 | 118 |         int i = 0; | 
 | 119 |         if (gpu.second[0].first != 0) { | 
 | 120 |             StringAppendF(result, "Global total: N/A\n"); | 
 | 121 |         } else { | 
 | 122 |             StringAppendF(result, "Global total: %" PRIu64 "\n", gpu.second[0].second); | 
 | 123 |             i++; | 
 | 124 |         } | 
 | 125 |         for (; i < gpu.second.size(); i++) { | 
 | 126 |             StringAppendF(result, "Proc %u total: %" PRIu64 "\n", gpu.second[i].first, | 
 | 127 |                           gpu.second[i].second); | 
 | 128 |         } | 
 | 129 |     } | 
 | 130 | } | 
 | 131 |  | 
| Yiwei Zhang | 5cd71a2 | 2020-08-13 17:39:51 -0700 | [diff] [blame] | 132 | void GpuMem::traverseGpuMemTotals(const std::function<void(int64_t ts, uint32_t gpuId, uint32_t pid, | 
 | 133 |                                                            uint64_t size)>& callback) { | 
 | 134 |     auto res = mGpuMemTotalMap.getFirstKey(); | 
 | 135 |     if (!res.ok()) return; | 
 | 136 |     uint64_t key = res.value(); | 
 | 137 |     while (true) { | 
 | 138 |         uint32_t gpu_id = key >> 32; | 
 | 139 |         uint32_t pid = key; | 
 | 140 |  | 
 | 141 |         res = mGpuMemTotalMap.readValue(key); | 
 | 142 |         if (!res.ok()) break; | 
 | 143 |         uint64_t size = res.value(); | 
 | 144 |  | 
 | 145 |         callback(systemTime(), gpu_id, pid, size); | 
 | 146 |         res = mGpuMemTotalMap.getNextKey(key); | 
 | 147 |         if (!res.ok()) break; | 
 | 148 |         key = res.value(); | 
 | 149 |     } | 
 | 150 | } | 
 | 151 |  | 
| Yiwei Zhang | 99fc75a | 2020-04-30 10:46:39 -0700 | [diff] [blame] | 152 | } // namespace android |