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
MnemeManagedPlacement.hpp
Go to the documentation of this file.
1#pragma once
2#include <cerrno>
3#include <cstdint>
4#include <cstring>
5#include <sys/mman.h>
6#include <unistd.h>
7
10
11namespace mneme {
12
13// The mapping the vendor runtime made for a managed allocation at an address
14// Mneme chose; empty when none was placed.
16 uintptr_t Start = 0;
17 uint64_t Size = 0;
18
19 bool contains(const void *Ptr, uint64_t Bytes) const {
20 auto P = reinterpret_cast<uintptr_t>(Ptr);
21 return Size && P >= Start && P + Bytes <= Start + Size;
22 }
23};
24
25class PlaceManagedMmap;
26class ReserveOnManagedMunmap;
27
28namespace detail {
29inline thread_local PlaceManagedMmap *ActivePlacement = nullptr;
30inline thread_local ReserveOnManagedMunmap *ActiveMunmapReserve = nullptr;
31
32inline uint64_t roundUpToPage(uint64_t Size) {
33 static const uint64_t PageSize = sysconf(_SC_PAGESIZE);
34 return util::roundUp(Size, PageSize);
35}
36
37// Replaces whatever is mapped at [Addr, Addr + Len) with reserved address
38// space, which Mneme owns.
39inline void reserveFixed(void *Addr, size_t Len) {
40 void *Got = mmap(Addr, Len, PROT_NONE, ReserveFlags | MAP_FIXED, -1, 0);
41 if (Got == MAP_FAILED)
42 LOG_FATAL("Cannot keep {} bytes at {} reserved: {}", Len, Addr,
43 std::strerror(errno));
44}
45} // namespace detail
46
47// While alive, the first anonymous mmap that this thread makes without an
48// address and that spans the same pages as an allocation of Size bytes is
49// placed in address space that Allocator reserved. Replay maps recorded memory
50// at its recorded address, so that address must be free in the replay process.
51// The runtime's own choice lies in the mmap region, which the replay process
52// fills with its libraries. The object must go out of scope before the caller
53// does anything else that may map memory, such as logging, so that only the
54// runtime's mapping is placed.
56 ChunkAllocator &Allocator;
57 // The runtime maps the allocation with its size, which some versions round
58 // to whole pages first. Its other mappings are bookkeeping.
59 uint64_t PageRoundedSize;
60 ManagedMapping Placed;
61
62 template <typename MmapFn>
63 friend void *placeOrForwardMmap(MmapFn Mmap, void *Addr, size_t Len, int Prot,
64 int Flags, int Fd, off_t Off);
65
66public:
67 PlaceManagedMmap(ChunkAllocator &Allocator, uint64_t Size)
68 : Allocator(Allocator), PageRoundedSize(detail::roundUpToPage(Size)) {
70 }
72
75
76 const ManagedMapping &placed() const { return Placed; }
77};
78
79// The runtime unmaps a managed allocation's mapping when the allocation is
80// freed. While alive, this object makes that munmap, recognised on this thread
81// by the address and length of Mapping, replace the mapping with reserved
82// address space instead of unmapping it. Otherwise the kernel could give the
83// addresses to another mapping before Mneme reuses them.
85 ManagedMapping Mapping;
86 bool WasUnmapped = false;
87
88 template <typename MunmapFn>
89 friend int reserveOrForwardMunmap(MunmapFn Munmap, void *Addr, size_t Len);
90
91public:
92 explicit ReserveOnManagedMunmap(const ManagedMapping &Mapping)
93 : Mapping(Mapping) {
95 }
97
100
101 bool wasUnmapped() const { return WasUnmapped; }
102};
103
104// The body of the mmap hook. Forwards to Mmap, placing the mapping when a
105// PlaceManagedMmap is active on this thread and the call qualifies. A mapping
106// that cannot be placed is made where the kernel chooses and leaves the
107// placement empty, so the caller reports it.
108template <typename MmapFn>
109void *placeOrForwardMmap(MmapFn Mmap, void *Addr, size_t Len, int Prot,
110 int Flags, int Fd, off_t Off) {
112 if (!P || Addr || !(Flags & MAP_ANONYMOUS) || (Flags & MAP_FIXED) ||
113 detail::roundUpToPage(Len) != P->PageRoundedSize)
114 return Mmap(Addr, Len, Prot, Flags, Fd, Off);
115
116 // Only one mapping is placed, and growing the allocator maps memory itself.
117 detail::ActivePlacement = nullptr;
118 uint64_t Size = P->Allocator.actualSize(Len);
119 void *Want = P->Allocator.allocate(Size);
120 if (!Want)
121 return Mmap(Addr, Len, Prot, Flags, Fd, Off);
122 // Mneme owns the reservation at Want, so the mapping may replace it.
123 void *Got = Mmap(Want, Len, Prot, Flags | MAP_FIXED, Fd, Off);
124 if (Got == MAP_FAILED) {
125 int Err = errno;
126 // A failed fixed mmap may already have unmapped the reservation.
128 P->Allocator.release(Want, Size);
129 errno = Err;
130 return MAP_FAILED;
131 }
132 P->Placed = {reinterpret_cast<uintptr_t>(Got), Len};
133 return Got;
134}
135
136// The body of the munmap hook. Forwards to Munmap, unless a
137// ReserveOnManagedMunmap is active on this thread and the call unmaps its
138// mapping.
139template <typename MunmapFn>
140int reserveOrForwardMunmap(MunmapFn Munmap, void *Addr, size_t Len) {
142 if (!R || reinterpret_cast<uintptr_t>(Addr) != R->Mapping.Start ||
143 Len != R->Mapping.Size)
144 return Munmap(Addr, Len);
145
147 detail::reserveFixed(Addr, Len);
148 R->WasUnmapped = true;
149 return 0;
150}
151
152} // namespace mneme
#define LOG_FATAL(...)
Definition MnemeLogger.hpp:113
void *(*)(void *, size_t, int, int, int, off_t) MmapFn
Definition MnemeRecordPreloadHIP.cpp:47
int(*)(void *, size_t) MunmapFn
Definition MnemeRecordPreloadHIP.cpp:48
void * mmap(void *Addr, size_t Len, int Prot, int Flags, int Fd, off_t Off) noexcept
Definition MnemeRecordPreloadHIP.cpp:60
Definition MnemePageManager.hpp:253
uint64_t actualSize(uint64_t Size) const
Definition MnemePageManager.hpp:315
void * allocate(uint64_t Size)
Definition MnemePageManager.hpp:324
void release(void *Addr, uint64_t Size)
Definition MnemePageManager.hpp:337
Definition MnemeManagedPlacement.hpp:55
PlaceManagedMmap(const PlaceManagedMmap &)=delete
PlaceManagedMmap(ChunkAllocator &Allocator, uint64_t Size)
Definition MnemeManagedPlacement.hpp:67
PlaceManagedMmap & operator=(const PlaceManagedMmap &)=delete
friend void * placeOrForwardMmap(MmapFn Mmap, void *Addr, size_t Len, int Prot, int Flags, int Fd, off_t Off)
Definition MnemeManagedPlacement.hpp:109
~PlaceManagedMmap()
Definition MnemeManagedPlacement.hpp:71
const ManagedMapping & placed() const
Definition MnemeManagedPlacement.hpp:76
Definition MnemeManagedPlacement.hpp:84
bool wasUnmapped() const
Definition MnemeManagedPlacement.hpp:101
~ReserveOnManagedMunmap()
Definition MnemeManagedPlacement.hpp:96
ReserveOnManagedMunmap & operator=(const ReserveOnManagedMunmap &)=delete
ReserveOnManagedMunmap(const ReserveOnManagedMunmap &)=delete
friend int reserveOrForwardMunmap(MunmapFn Munmap, void *Addr, size_t Len)
Definition MnemeManagedPlacement.hpp:140
ReserveOnManagedMunmap(const ManagedMapping &Mapping)
Definition MnemeManagedPlacement.hpp:92
int64_t u64 Size
Definition cupti_profile.cpp:179
Definition MnemeAnnotation.cpp:13
void reserveFixed(void *Addr, size_t Len)
Definition MnemeManagedPlacement.hpp:39
PlaceManagedMmap * ActivePlacement
Definition MnemeManagedPlacement.hpp:29
uint64_t roundUpToPage(uint64_t Size)
Definition MnemeManagedPlacement.hpp:32
ReserveOnManagedMunmap * ActiveMunmapReserve
Definition MnemeManagedPlacement.hpp:30
Ty roundUp(Ty Size, Ty Divider)
Definition MnemeUtils.hpp:16
Definition MnemeAnnotation.cpp:3
int reserveOrForwardMunmap(MunmapFn Munmap, void *Addr, size_t Len)
Definition MnemeManagedPlacement.hpp:140
constexpr int ReserveFlags
Definition MnemePageManager.hpp:199
@ Bytes
Definition MnemeConfig.hpp:24
void * placeOrForwardMmap(MmapFn Mmap, void *Addr, size_t Len, int Prot, int Flags, int Fd, off_t Off)
Definition MnemeManagedPlacement.hpp:109
@ Off
Definition MnemeConfig.hpp:23
Definition MnemeManagedPlacement.hpp:15
uint64_t Size
Definition MnemeManagedPlacement.hpp:17
bool contains(const void *Ptr, uint64_t Bytes) const
Definition MnemeManagedPlacement.hpp:19
uintptr_t Start
Definition MnemeManagedPlacement.hpp:16