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