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