summaryrefslogtreecommitdiff
path: root/services/gpuservice/gpumem/GpuMem.cpp
blob: 141fe021ee8c69ba37b72598e249a05495ddd726 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * Copyright 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#undef LOG_TAG
#define LOG_TAG "GpuMem"
#define ATRACE_TAG ATRACE_TAG_GRAPHICS

#include "gpumem/GpuMem.h"

#include <android-base/stringprintf.h>
#include <libbpf.h>
#include <bpf/WaitForProgsLoaded.h>
#include <log/log.h>
#include <unistd.h>
#include <utils/Timers.h>
#include <utils/Trace.h>

#include <unordered_map>
#include <vector>

namespace android {

using base::StringAppendF;

GpuMem::~GpuMem() {
    bpf_detach_tracepoint(kGpuMemTraceGroup, kGpuMemTotalTracepoint);
}

void GpuMem::initialize() {
    // Make sure bpf programs are loaded
    bpf::waitForProgsLoaded();

    errno = 0;
    int fd = bpf::retrieveProgram(kGpuMemTotalProgPath);
    if (fd < 0) {
        ALOGE("Failed to retrieve pinned program from %s [%d(%s)]", kGpuMemTotalProgPath, errno,
              strerror(errno));
        return;
    }

    // Attach the program to the tracepoint, and the tracepoint is automatically enabled here.
    errno = 0;
    int count = 0;
    while (bpf_attach_tracepoint(fd, kGpuMemTraceGroup, kGpuMemTotalTracepoint) < 0) {
        if (++count > kGpuWaitTimeout) {
            ALOGE("Failed to attach bpf program to %s/%s tracepoint [%d(%s)]", kGpuMemTraceGroup,
                  kGpuMemTotalTracepoint, errno, strerror(errno));
            return;
        }
        // Retry until GPU driver loaded or timeout.
        sleep(1);
    }

    // Use the read-only wrapper BpfMapRO to properly retrieve the read-only map.
    errno = 0;
    auto map = bpf::BpfMapRO<uint64_t, uint64_t>(kGpuMemTotalMapPath);
    if (!map.isValid()) {
        ALOGE("Failed to create bpf map from %s [%d(%s)]", kGpuMemTotalMapPath, errno,
              strerror(errno));
        return;
    }
    setGpuMemTotalMap(map);

    mInitialized.store(true);
}

void GpuMem::setGpuMemTotalMap(bpf::BpfMapRO<uint64_t, uint64_t>& map) {
    mGpuMemTotalMap = std::move(map);
}

// Dump the snapshots of global and per process memory usage on all gpus
void GpuMem::dump(const Vector<String16>& /* args */, std::string* result) {
    ATRACE_CALL();

    if (!mInitialized.load() || !mGpuMemTotalMap.isValid()) {
        result->append("Failed to initialize GPU memory eBPF\n");
        return;
    }

    auto res = mGpuMemTotalMap.getFirstKey();
    if (!res.ok()) {
        result->append("GPU memory total usage map is empty\n");
        return;
    }
    uint64_t key = res.value();
    // unordered_map<gpu_id, vector<pair<pid, size>>>
    std::unordered_map<uint32_t, std::vector<std::pair<uint32_t, uint64_t>>> dumpMap;
    while (true) {
        uint32_t gpu_id = key >> 32;
        uint32_t pid = key;

        res = mGpuMemTotalMap.readValue(key);
        if (!res.ok()) break;
        uint64_t size = res.value();

        dumpMap[gpu_id].emplace_back(pid, size);

        res = mGpuMemTotalMap.getNextKey(key);
        if (!res.ok()) break;
        key = res.value();
    }

    for (auto& gpu : dumpMap) {
        if (gpu.second.empty()) continue;
        StringAppendF(result, "Memory snapshot for GPU %u:\n", gpu.first);

        std::sort(gpu.second.begin(), gpu.second.end(),
                  [](auto& l, auto& r) { return l.first < r.first; });

        int i = 0;
        if (gpu.second[0].first != 0) {
            StringAppendF(result, "Global total: N/A\n");
        } else {
            StringAppendF(result, "Global total: %" PRIu64 "\n", gpu.second[0].second);
            i++;
        }
        for (; i < gpu.second.size(); i++) {
            StringAppendF(result, "Proc %u total: %" PRIu64 "\n", gpu.second[i].first,
                          gpu.second[i].second);
        }
    }
}

void GpuMem::traverseGpuMemTotals(const std::function<void(int64_t ts, uint32_t gpuId, uint32_t pid,
                                                           uint64_t size)>& callback) {
    auto res = mGpuMemTotalMap.getFirstKey();
    if (!res.ok()) return;
    uint64_t key = res.value();
    while (true) {
        uint32_t gpu_id = key >> 32;
        uint32_t pid = key;

        res = mGpuMemTotalMap.readValue(key);
        if (!res.ok()) break;
        uint64_t size = res.value();

        callback(systemTime(), gpu_id, pid, size);
        res = mGpuMemTotalMap.getNextKey(key);
        if (!res.ok()) break;
        key = res.value();
    }
}

} // namespace android