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