Mneme
Mneme a tool allowing recording the execution of a GPU (CUDA/HIP) kernel and replaying that kernel as an independent executable.
Loading...
Searching...
No Matches
MnemeVASpace.hpp
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <cstdint>
4#include <fstream>
5#include <optional>
6#include <string>
7#include <vector>
8
9namespace mneme {
10namespace util {
11
12struct VARange {
13 uintptr_t Start;
14 uintptr_t End;
15 uint64_t size() const { return End - Start; }
16};
17
18constexpr uint64_t LargePageSize = 2ULL << 20;
19
20// Large-page alignment when [Addr, Addr + Size) allows it, else PageSize.
21inline uint64_t mapAlignment(uintptr_t Addr, uint64_t Size,
22 uint64_t PageSize) {
23 if (Size >= LargePageSize && Addr % LargePageSize == 0)
24 return LargePageSize;
25 return PageSize;
26}
27
28// Calls F(Range, Line) for each entry of /proc/self/maps.
29template <typename Fn> void forEachMapping(Fn F) {
30 std::ifstream Maps("/proc/self/maps");
31 std::string Line;
32 while (std::getline(Maps, Line)) {
33 size_t Dash = Line.find('-');
34 if (Dash == std::string::npos)
35 continue;
36 uintptr_t S = std::stoull(Line.substr(0, Dash), nullptr, 16);
37 uintptr_t E = std::stoull(Line.substr(Dash + 1), nullptr, 16);
38 F(VARange{S, E}, Line);
39 }
40}
41
42// /proc/self/maps lines overlapping [Lo, Hi).
43inline std::string getMappingsIn(uintptr_t Lo, uintptr_t Hi) {
44 std::string Out;
45 forEachMapping([&](const VARange &R, const std::string &Line) {
46 if (R.Start < Hi && R.End > Lo)
47 Out += Line + "\n";
48 });
49 return Out;
50}
51
52// Unmapped user-space ranges of this process, from /proc/self/maps.
53inline std::vector<VARange> getFreeVARanges(uintptr_t Lo, uintptr_t Hi) {
54 std::vector<VARange> Mapped;
56 [&](const VARange &R, const std::string &) { Mapped.push_back(R); });
57 std::sort(
58 Mapped.begin(), Mapped.end(),
59 [](const VARange &A, const VARange &B) { return A.Start < B.Start; });
60
61 std::vector<VARange> Free;
62 uintptr_t Cur = Lo;
63 for (const auto &M : Mapped) {
64 if (M.Start >= Hi)
65 break;
66 if (M.Start > Cur)
67 Free.push_back({Cur, M.Start});
68 Cur = std::max(Cur, M.End);
69 }
70 if (Cur < Hi)
71 Free.push_back({Cur, Hi});
72 return Free;
73}
74
75// Lowest Align-aligned address >= Lo where Size bytes fit unmapped below Hi.
76inline std::optional<uintptr_t> firstFreeVAddr(uintptr_t Lo, uintptr_t Hi,
77 uint64_t Size, uint64_t Align) {
78 for (const auto &G : getFreeVARanges(Lo, Hi)) {
79 uintptr_t Addr = (G.Start + Align - 1) & ~(Align - 1);
80 if (Addr + Size <= G.End)
81 return Addr;
82 }
83 return std::nullopt;
84}
85
86// Candidate addresses for a Size-byte reservation, centered in the largest
87// free gaps first so neither the heap nor the mmap region grows into it.
88inline std::vector<uintptr_t> suggestVAddrs(uint64_t Size, uint64_t Alignment) {
89 constexpr uintptr_t Lo = 1ULL << 32;
90 constexpr uintptr_t Hi = 1ULL << 47;
91 constexpr uint64_t PreferredAlign = 1ULL << 30;
92
93 auto Free = getFreeVARanges(Lo, Hi);
94 std::sort(Free.begin(), Free.end(), [](const VARange &A, const VARange &B) {
95 return A.size() > B.size();
96 });
97
98 std::vector<uintptr_t> Addrs;
99 for (const auto &G : Free) {
100 if (G.size() < Size + 2 * Alignment)
101 continue;
102 uintptr_t Mid = G.Start + (G.size() - Size) / 2;
103 uint64_t Align =
104 G.size() >= Size + 2 * PreferredAlign ? PreferredAlign : Alignment;
105 uintptr_t Addr = Mid & ~(Align - 1);
106 if (Addr < G.Start)
107 Addr += Align;
108 if (Addr + Size <= G.End)
109 Addrs.push_back(Addr);
110 }
111 return Addrs;
112}
113
114} // namespace util
115} // namespace mneme
int64_t u64 Size
Definition cupti_profile.cpp:179
Definition MnemeLLVMUtils.hpp:34
void forEachMapping(Fn F)
Definition MnemeVASpace.hpp:29
std::optional< uintptr_t > firstFreeVAddr(uintptr_t Lo, uintptr_t Hi, uint64_t Size, uint64_t Align)
Definition MnemeVASpace.hpp:76
std::string getMappingsIn(uintptr_t Lo, uintptr_t Hi)
Definition MnemeVASpace.hpp:43
std::vector< VARange > getFreeVARanges(uintptr_t Lo, uintptr_t Hi)
Definition MnemeVASpace.hpp:53
constexpr uint64_t LargePageSize
Definition MnemeVASpace.hpp:18
std::vector< uintptr_t > suggestVAddrs(uint64_t Size, uint64_t Alignment)
Definition MnemeVASpace.hpp:88
uint64_t mapAlignment(uintptr_t Addr, uint64_t Size, uint64_t PageSize)
Definition MnemeVASpace.hpp:21
Definition MnemeAnnotation.cpp:3
@ Mapped
Definition DeviceTraits.hpp:71
Definition MnemeVASpace.hpp:12
uintptr_t Start
Definition MnemeVASpace.hpp:13
uint64_t size() const
Definition MnemeVASpace.hpp:15
uintptr_t End
Definition MnemeVASpace.hpp:14