blob: 9b4053b7fdad49ba4a72a5513eab685fa16fa759 [file] [log] [blame]
Yiwei Zhang99fc75a2020-04-30 10:46:39 -07001/*
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 Zhanga265b402020-06-25 22:02:29 -070027#include <unistd.h>
Yiwei Zhang99fc75a2020-04-30 10:46:39 -070028#include <utils/Trace.h>
29
30#include <unordered_map>
31#include <vector>
32
33namespace android {
34
35using base::StringAppendF;
36
37GpuMem::~GpuMem() {
38 bpf_detach_tracepoint(kGpuMemTraceGroup, kGpuMemTotalTracepoint);
39}
40
41void 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
Yiwei Zhanga265b402020-06-25 22:02:29 -070051 // TODO(http://b/159963505): Figure out a nicer way to wait until GPU driver loaded.
Yiwei Zhang99fc75a2020-04-30 10:46:39 -070052 // Attach the program to the tracepoint, and the tracepoint is automatically enabled here.
Yiwei Zhanga265b402020-06-25 22:02:29 -070053 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 Zhang99fc75a2020-04-30 10:46:39 -070062 }
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 Zhanga265b402020-06-25 22:02:29 -070071
72 mInitialized.store(true);
Yiwei Zhang99fc75a2020-04-30 10:46:39 -070073}
74
75void 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
80void GpuMem::dump(const Vector<String16>& /* args */, std::string* result) {
81 ATRACE_CALL();
82
Yiwei Zhanga265b402020-06-25 22:02:29 -070083 if (!mInitialized.load() || !mGpuMemTotalMap.isValid()) {
Yiwei Zhang99fc75a2020-04-30 10:46:39 -070084 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
132} // namespace android