xref: /freebsd/contrib/llvm-project/compiler-rt/lib/memprof/memprof_rawprofile.cpp (revision 1fd87a682ad7442327078e1eeb63edc4258f9815)
14824e7fdSDimitry Andric #include <stdint.h>
24824e7fdSDimitry Andric #include <stdlib.h>
34824e7fdSDimitry Andric #include <string.h>
44824e7fdSDimitry Andric 
54824e7fdSDimitry Andric #include "memprof_rawprofile.h"
64824e7fdSDimitry Andric #include "profile/MemProfData.inc"
7349cc55cSDimitry Andric #include "sanitizer_common/sanitizer_allocator_internal.h"
84824e7fdSDimitry Andric #include "sanitizer_common/sanitizer_common.h"
9349cc55cSDimitry Andric #include "sanitizer_common/sanitizer_linux.h"
10349cc55cSDimitry Andric #include "sanitizer_common/sanitizer_procmaps.h"
11349cc55cSDimitry Andric #include "sanitizer_common/sanitizer_stackdepot.h"
12349cc55cSDimitry Andric #include "sanitizer_common/sanitizer_stackdepotbase.h"
13349cc55cSDimitry Andric #include "sanitizer_common/sanitizer_stacktrace.h"
14349cc55cSDimitry Andric #include "sanitizer_common/sanitizer_vector.h"
15349cc55cSDimitry Andric 
16349cc55cSDimitry Andric namespace __memprof {
17349cc55cSDimitry Andric using ::__sanitizer::Vector;
18*1fd87a68SDimitry Andric using ::llvm::memprof::MemInfoBlock;
194824e7fdSDimitry Andric using SegmentEntry = ::llvm::memprof::SegmentEntry;
204824e7fdSDimitry Andric using Header = ::llvm::memprof::Header;
21349cc55cSDimitry Andric 
22349cc55cSDimitry Andric namespace {
23349cc55cSDimitry Andric template <class T> char *WriteBytes(T Pod, char *&Buffer) {
24349cc55cSDimitry Andric   *(T *)Buffer = Pod;
25349cc55cSDimitry Andric   return Buffer + sizeof(T);
26349cc55cSDimitry Andric }
27349cc55cSDimitry Andric 
28349cc55cSDimitry Andric void RecordStackId(const uptr Key, UNUSED LockedMemInfoBlock *const &MIB,
29349cc55cSDimitry Andric                    void *Arg) {
30349cc55cSDimitry Andric   // No need to touch the MIB value here since we are only recording the key.
31349cc55cSDimitry Andric   auto *StackIds = reinterpret_cast<Vector<u64> *>(Arg);
32349cc55cSDimitry Andric   StackIds->PushBack(Key);
33349cc55cSDimitry Andric }
34349cc55cSDimitry Andric } // namespace
35349cc55cSDimitry Andric 
36349cc55cSDimitry Andric u64 SegmentSizeBytes(MemoryMappingLayoutBase &Layout) {
37349cc55cSDimitry Andric   u64 NumSegmentsToRecord = 0;
38349cc55cSDimitry Andric   MemoryMappedSegment segment;
39349cc55cSDimitry Andric   for (Layout.Reset(); Layout.Next(&segment);)
40349cc55cSDimitry Andric     if (segment.IsReadable() && segment.IsExecutable())
41349cc55cSDimitry Andric       NumSegmentsToRecord++;
42349cc55cSDimitry Andric 
43349cc55cSDimitry Andric   return sizeof(u64) // A header which stores the number of records.
44349cc55cSDimitry Andric          + sizeof(SegmentEntry) * NumSegmentsToRecord;
45349cc55cSDimitry Andric }
46349cc55cSDimitry Andric 
47349cc55cSDimitry Andric // The segment section uses the following format:
48349cc55cSDimitry Andric // ---------- Segment Info
49349cc55cSDimitry Andric // Num Entries
50349cc55cSDimitry Andric // ---------- Segment Entry
51349cc55cSDimitry Andric // Start
52349cc55cSDimitry Andric // End
53349cc55cSDimitry Andric // Offset
54349cc55cSDimitry Andric // BuildID 32B
55349cc55cSDimitry Andric // ----------
56349cc55cSDimitry Andric // ...
57349cc55cSDimitry Andric void SerializeSegmentsToBuffer(MemoryMappingLayoutBase &Layout,
58349cc55cSDimitry Andric                                const u64 ExpectedNumBytes, char *&Buffer) {
59349cc55cSDimitry Andric   char *Ptr = Buffer;
60349cc55cSDimitry Andric   // Reserve space for the final count.
61349cc55cSDimitry Andric   Ptr += sizeof(u64);
62349cc55cSDimitry Andric 
63349cc55cSDimitry Andric   u64 NumSegmentsRecorded = 0;
64349cc55cSDimitry Andric   MemoryMappedSegment segment;
65349cc55cSDimitry Andric 
66349cc55cSDimitry Andric   for (Layout.Reset(); Layout.Next(&segment);) {
67349cc55cSDimitry Andric     if (segment.IsReadable() && segment.IsExecutable()) {
68*1fd87a68SDimitry Andric       // TODO: Record segment.uuid when it is implemented for Linux-Elf.
69*1fd87a68SDimitry Andric       SegmentEntry Entry(segment.start, segment.end, segment.offset);
704824e7fdSDimitry Andric       memcpy(Ptr, &Entry, sizeof(SegmentEntry));
71349cc55cSDimitry Andric       Ptr += sizeof(SegmentEntry);
72349cc55cSDimitry Andric       NumSegmentsRecorded++;
73349cc55cSDimitry Andric     }
74349cc55cSDimitry Andric   }
75349cc55cSDimitry Andric 
76349cc55cSDimitry Andric   // Store the number of segments we recorded in the space we reserved.
77349cc55cSDimitry Andric   *((u64 *)Buffer) = NumSegmentsRecorded;
784824e7fdSDimitry Andric   CHECK(ExpectedNumBytes >= static_cast<u64>(Ptr - Buffer) &&
79349cc55cSDimitry Andric         "Expected num bytes != actual bytes written");
80349cc55cSDimitry Andric }
81349cc55cSDimitry Andric 
82349cc55cSDimitry Andric u64 StackSizeBytes(const Vector<u64> &StackIds) {
83349cc55cSDimitry Andric   u64 NumBytesToWrite = sizeof(u64);
84349cc55cSDimitry Andric 
85349cc55cSDimitry Andric   const u64 NumIds = StackIds.Size();
86349cc55cSDimitry Andric   for (unsigned k = 0; k < NumIds; ++k) {
87349cc55cSDimitry Andric     const u64 Id = StackIds[k];
88349cc55cSDimitry Andric     // One entry for the id and then one more for the number of stack pcs.
89349cc55cSDimitry Andric     NumBytesToWrite += 2 * sizeof(u64);
90349cc55cSDimitry Andric     const StackTrace St = StackDepotGet(Id);
91349cc55cSDimitry Andric 
92349cc55cSDimitry Andric     CHECK(St.trace != nullptr && St.size > 0 && "Empty stack trace");
93349cc55cSDimitry Andric     for (uptr i = 0; i < St.size && St.trace[i] != 0; i++) {
94349cc55cSDimitry Andric       NumBytesToWrite += sizeof(u64);
95349cc55cSDimitry Andric     }
96349cc55cSDimitry Andric   }
97349cc55cSDimitry Andric   return NumBytesToWrite;
98349cc55cSDimitry Andric }
99349cc55cSDimitry Andric 
100349cc55cSDimitry Andric // The stack info section uses the following format:
101349cc55cSDimitry Andric //
102349cc55cSDimitry Andric // ---------- Stack Info
103349cc55cSDimitry Andric // Num Entries
104349cc55cSDimitry Andric // ---------- Stack Entry
105349cc55cSDimitry Andric // Num Stacks
106349cc55cSDimitry Andric // PC1
107349cc55cSDimitry Andric // PC2
108349cc55cSDimitry Andric // ...
109349cc55cSDimitry Andric // ----------
110349cc55cSDimitry Andric void SerializeStackToBuffer(const Vector<u64> &StackIds,
111349cc55cSDimitry Andric                             const u64 ExpectedNumBytes, char *&Buffer) {
112349cc55cSDimitry Andric   const u64 NumIds = StackIds.Size();
113349cc55cSDimitry Andric   char *Ptr = Buffer;
114349cc55cSDimitry Andric   Ptr = WriteBytes(static_cast<u64>(NumIds), Ptr);
115349cc55cSDimitry Andric 
116349cc55cSDimitry Andric   for (unsigned k = 0; k < NumIds; ++k) {
117349cc55cSDimitry Andric     const u64 Id = StackIds[k];
118349cc55cSDimitry Andric     Ptr = WriteBytes(Id, Ptr);
119349cc55cSDimitry Andric     Ptr += sizeof(u64); // Bump it by u64, we will fill this in later.
120349cc55cSDimitry Andric     u64 Count = 0;
121349cc55cSDimitry Andric     const StackTrace St = StackDepotGet(Id);
122349cc55cSDimitry Andric     for (uptr i = 0; i < St.size && St.trace[i] != 0; i++) {
123349cc55cSDimitry Andric       // PCs in stack traces are actually the return addresses, that is,
124349cc55cSDimitry Andric       // addresses of the next instructions after the call.
125349cc55cSDimitry Andric       uptr pc = StackTrace::GetPreviousInstructionPc(St.trace[i]);
126349cc55cSDimitry Andric       Ptr = WriteBytes(static_cast<u64>(pc), Ptr);
127349cc55cSDimitry Andric       ++Count;
128349cc55cSDimitry Andric     }
129349cc55cSDimitry Andric     // Store the count in the space we reserved earlier.
130349cc55cSDimitry Andric     *(u64 *)(Ptr - (Count + 1) * sizeof(u64)) = Count;
131349cc55cSDimitry Andric   }
132349cc55cSDimitry Andric 
1334824e7fdSDimitry Andric   CHECK(ExpectedNumBytes >= static_cast<u64>(Ptr - Buffer) &&
134349cc55cSDimitry Andric         "Expected num bytes != actual bytes written");
135349cc55cSDimitry Andric }
136349cc55cSDimitry Andric 
137349cc55cSDimitry Andric // The MIB section has the following format:
138349cc55cSDimitry Andric // ---------- MIB Info
139349cc55cSDimitry Andric // Num Entries
140349cc55cSDimitry Andric // ---------- MIB Entry 0
141349cc55cSDimitry Andric // Alloc Count
142349cc55cSDimitry Andric // ...
143349cc55cSDimitry Andric // ---------- MIB Entry 1
144349cc55cSDimitry Andric // Alloc Count
145349cc55cSDimitry Andric // ...
146349cc55cSDimitry Andric // ----------
147349cc55cSDimitry Andric void SerializeMIBInfoToBuffer(MIBMapTy &MIBMap, const Vector<u64> &StackIds,
148349cc55cSDimitry Andric                               const u64 ExpectedNumBytes, char *&Buffer) {
149349cc55cSDimitry Andric   char *Ptr = Buffer;
150349cc55cSDimitry Andric   const u64 NumEntries = StackIds.Size();
151349cc55cSDimitry Andric   Ptr = WriteBytes(NumEntries, Ptr);
152349cc55cSDimitry Andric 
153349cc55cSDimitry Andric   for (u64 i = 0; i < NumEntries; i++) {
154349cc55cSDimitry Andric     const u64 Key = StackIds[i];
155349cc55cSDimitry Andric     MIBMapTy::Handle h(&MIBMap, Key, /*remove=*/true, /*create=*/false);
156349cc55cSDimitry Andric     CHECK(h.exists());
157349cc55cSDimitry Andric     Ptr = WriteBytes(Key, Ptr);
158349cc55cSDimitry Andric     Ptr = WriteBytes((*h)->mib, Ptr);
159349cc55cSDimitry Andric   }
160349cc55cSDimitry Andric 
1614824e7fdSDimitry Andric   CHECK(ExpectedNumBytes >= static_cast<u64>(Ptr - Buffer) &&
162349cc55cSDimitry Andric         "Expected num bytes != actual bytes written");
163349cc55cSDimitry Andric }
164349cc55cSDimitry Andric 
165349cc55cSDimitry Andric // Format
166349cc55cSDimitry Andric // ---------- Header
167349cc55cSDimitry Andric // Magic
168349cc55cSDimitry Andric // Version
169349cc55cSDimitry Andric // Total Size
170349cc55cSDimitry Andric // Segment Offset
171349cc55cSDimitry Andric // MIB Info Offset
172349cc55cSDimitry Andric // Stack Offset
173349cc55cSDimitry Andric // ---------- Segment Info
174349cc55cSDimitry Andric // Num Entries
175349cc55cSDimitry Andric // ---------- Segment Entry
176349cc55cSDimitry Andric // Start
177349cc55cSDimitry Andric // End
178349cc55cSDimitry Andric // Offset
179349cc55cSDimitry Andric // BuildID 32B
180349cc55cSDimitry Andric // ----------
181349cc55cSDimitry Andric // ...
1824824e7fdSDimitry Andric // ----------
1834824e7fdSDimitry Andric // Optional Padding Bytes
184349cc55cSDimitry Andric // ---------- MIB Info
185349cc55cSDimitry Andric // Num Entries
186349cc55cSDimitry Andric // ---------- MIB Entry
187349cc55cSDimitry Andric // Alloc Count
188349cc55cSDimitry Andric // ...
1894824e7fdSDimitry Andric // ----------
1904824e7fdSDimitry Andric // Optional Padding Bytes
191349cc55cSDimitry Andric // ---------- Stack Info
192349cc55cSDimitry Andric // Num Entries
193349cc55cSDimitry Andric // ---------- Stack Entry
194349cc55cSDimitry Andric // Num Stacks
195349cc55cSDimitry Andric // PC1
196349cc55cSDimitry Andric // PC2
197349cc55cSDimitry Andric // ...
198349cc55cSDimitry Andric // ----------
1994824e7fdSDimitry Andric // Optional Padding Bytes
200349cc55cSDimitry Andric // ...
201349cc55cSDimitry Andric u64 SerializeToRawProfile(MIBMapTy &MIBMap, MemoryMappingLayoutBase &Layout,
202349cc55cSDimitry Andric                           char *&Buffer) {
2034824e7fdSDimitry Andric   // Each section size is rounded up to 8b since the first entry in each section
2044824e7fdSDimitry Andric   // is a u64 which holds the number of entries in the section by convention.
2054824e7fdSDimitry Andric   const u64 NumSegmentBytes = RoundUpTo(SegmentSizeBytes(Layout), 8);
206349cc55cSDimitry Andric 
207349cc55cSDimitry Andric   Vector<u64> StackIds;
208349cc55cSDimitry Andric   MIBMap.ForEach(RecordStackId, reinterpret_cast<void *>(&StackIds));
209349cc55cSDimitry Andric   // The first 8b are for the total number of MIB records. Each MIB record is
210349cc55cSDimitry Andric   // preceded by a 8b stack id which is associated with stack frames in the next
211349cc55cSDimitry Andric   // section.
2124824e7fdSDimitry Andric   const u64 NumMIBInfoBytes = RoundUpTo(
2134824e7fdSDimitry Andric       sizeof(u64) + StackIds.Size() * (sizeof(u64) + sizeof(MemInfoBlock)), 8);
214349cc55cSDimitry Andric 
2154824e7fdSDimitry Andric   const u64 NumStackBytes = RoundUpTo(StackSizeBytes(StackIds), 8);
216349cc55cSDimitry Andric 
2174824e7fdSDimitry Andric   // Ensure that the profile is 8b aligned. We allow for some optional padding
2184824e7fdSDimitry Andric   // at the end so that any subsequent profile serialized to the same file does
2194824e7fdSDimitry Andric   // not incur unaligned accesses.
2204824e7fdSDimitry Andric   const u64 TotalSizeBytes = RoundUpTo(
2214824e7fdSDimitry Andric       sizeof(Header) + NumSegmentBytes + NumStackBytes + NumMIBInfoBytes, 8);
222349cc55cSDimitry Andric 
223349cc55cSDimitry Andric   // Allocate the memory for the entire buffer incl. info blocks.
224349cc55cSDimitry Andric   Buffer = (char *)InternalAlloc(TotalSizeBytes);
225349cc55cSDimitry Andric   char *Ptr = Buffer;
226349cc55cSDimitry Andric 
227349cc55cSDimitry Andric   Header header{MEMPROF_RAW_MAGIC_64,
228349cc55cSDimitry Andric                 MEMPROF_RAW_VERSION,
229349cc55cSDimitry Andric                 static_cast<u64>(TotalSizeBytes),
230349cc55cSDimitry Andric                 sizeof(Header),
231349cc55cSDimitry Andric                 sizeof(Header) + NumSegmentBytes,
232349cc55cSDimitry Andric                 sizeof(Header) + NumSegmentBytes + NumMIBInfoBytes};
233349cc55cSDimitry Andric   Ptr = WriteBytes(header, Ptr);
234349cc55cSDimitry Andric 
235349cc55cSDimitry Andric   SerializeSegmentsToBuffer(Layout, NumSegmentBytes, Ptr);
236349cc55cSDimitry Andric   Ptr += NumSegmentBytes;
237349cc55cSDimitry Andric 
238349cc55cSDimitry Andric   SerializeMIBInfoToBuffer(MIBMap, StackIds, NumMIBInfoBytes, Ptr);
239349cc55cSDimitry Andric   Ptr += NumMIBInfoBytes;
240349cc55cSDimitry Andric 
241349cc55cSDimitry Andric   SerializeStackToBuffer(StackIds, NumStackBytes, Ptr);
242349cc55cSDimitry Andric 
243349cc55cSDimitry Andric   return TotalSizeBytes;
244349cc55cSDimitry Andric }
245349cc55cSDimitry Andric 
246349cc55cSDimitry Andric } // namespace __memprof
247