1 #ifndef MEMPROF_DATA_INC
2 #define MEMPROF_DATA_INC
3 /*===-- MemProfData.inc - MemProf profiling runtime structures -*- C++ -*-=== *\
5 |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6 |* See https://llvm.org/LICENSE.txt for license information.
7 |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
9 \*===----------------------------------------------------------------------===*/
11 * This is the main file that defines all the data structure, signature,
12 * constant literals that are shared across profiling runtime library,
13 * and host tools (reader/writer).
15 * This file has two identical copies. The primary copy lives in LLVM and
16 * the other one sits in compiler-rt/include/profile directory. To make changes
17 * in this file, first modify the primary copy and copy it over to compiler-rt.
18 * Testing of any change in this file can start only after the two copies are
21 \*===----------------------------------------------------------------------===*/
25 #define PACKED(...) __pragma(pack(push,1)) __VA_ARGS__ __pragma(pack(pop))
27 #define PACKED(...) __VA_ARGS__ __attribute__((__packed__))
30 // A 64-bit magic number to uniquely identify the raw binary memprof profile file.
31 #define MEMPROF_RAW_MAGIC_64 \
32 ((uint64_t)255 << 56 | (uint64_t)'m' << 48 | (uint64_t)'p' << 40 | (uint64_t)'r' << 32 | \
33 (uint64_t)'o' << 24 | (uint64_t)'f' << 16 | (uint64_t)'r' << 8 | (uint64_t)129)
35 // The version number of the raw binary format.
36 #define MEMPROF_RAW_VERSION 4ULL
38 // Currently supported versions.
39 #define MEMPROF_RAW_SUPPORTED_VERSIONS \
42 #define MEMPROF_V3_MIB_SIZE 132ULL;
44 #define MEMPROF_BUILDID_MAX_SIZE 32ULL
48 // A struct describing the header used for the raw binary memprof profile format.
49 PACKED(struct Header {
53 uint64_t SegmentOffset;
58 // A struct describing the information necessary to describe a /proc/maps
59 // segment entry for a particular binary/library identified by its build id.
60 PACKED(struct SegmentEntry {
65 uint8_t BuildId[MEMPROF_BUILDID_MAX_SIZE] = {0};
67 // This constructor is only used in tests so don't set the BuildId.
68 SegmentEntry(uint64_t S, uint64_t E, uint64_t O)
69 : Start(S), End(E), Offset(O), BuildIdSize(0) {}
71 SegmentEntry(const SegmentEntry& S) {
75 BuildIdSize = S.BuildIdSize;
76 memcpy(BuildId, S.BuildId, S.BuildIdSize);
79 SegmentEntry& operator=(const SegmentEntry& S) {
83 BuildIdSize = S.BuildIdSize;
84 memcpy(BuildId, S.BuildId, S.BuildIdSize);
88 bool operator==(const SegmentEntry& S) const {
89 return Start == S.Start && End == S.End && Offset == S.Offset &&
90 BuildIdSize == S.BuildIdSize &&
91 memcmp(BuildId, S.BuildId, S.BuildIdSize) == 0;
95 // Packed struct definition for MSVC. We can't use the PACKED macro defined in
96 // MemProfData.inc since it would mean we are embedding a directive (the
97 // #include for MIBEntryDef) into the macros which is undefined behaviour.
99 __pragma(pack(push,1))
102 // A struct representing the heap allocation characteristics of a particular
103 // runtime context. This struct is shared between the compiler-rt runtime and
104 // the raw profile reader. The indexed format uses a separate, self-describing
105 // backwards compatible format.
108 #define MIBEntryDef(NameTag, Name, Type) Type Name;
109 #include "MIBEntryDef.inc"
112 bool operator==(const MemInfoBlock& Other) const {
114 #define MIBEntryDef(NameTag, Name, Type) \
115 IsEqual = (IsEqual && Name == Other.Name);
116 #include "MIBEntryDef.inc"
122 #define MIBEntryDef(NameTag, Name, Type) Name = Type();
123 #include "MIBEntryDef.inc"
127 MemInfoBlock(uint32_t Size, uint64_t AccessCount, uint32_t AllocTs,
128 uint32_t DeallocTs, uint32_t AllocCpu, uint32_t DeallocCpu,
129 uintptr_t Histogram, uint32_t HistogramSize)
132 TotalAccessCount = AccessCount;
133 MinAccessCount = AccessCount;
134 MaxAccessCount = AccessCount;
138 AllocTimestamp = AllocTs;
139 DeallocTimestamp = DeallocTs;
140 TotalLifetime = DeallocTimestamp - AllocTimestamp;
141 MinLifetime = TotalLifetime;
142 MaxLifetime = TotalLifetime;
143 // Access density is accesses per byte. Multiply by 100 to include the
145 TotalAccessDensity = AccessCount * 100 / Size;
146 MinAccessDensity = TotalAccessDensity;
147 MaxAccessDensity = TotalAccessDensity;
148 // Lifetime access density is the access density per second of lifetime.
149 // Multiply by 1000 to convert denominator lifetime to seconds (using a
150 // minimum lifetime of 1ms to avoid divide by 0. Do the multiplication first
151 // to reduce truncations to 0.
152 TotalLifetimeAccessDensity =
153 TotalAccessDensity * 1000 / (TotalLifetime ? TotalLifetime : 1);
154 MinLifetimeAccessDensity = TotalLifetimeAccessDensity;
155 MaxLifetimeAccessDensity = TotalLifetimeAccessDensity;
156 AllocCpuId = AllocCpu;
157 DeallocCpuId = DeallocCpu;
158 NumMigratedCpu = AllocCpuId != DeallocCpuId;
159 AccessHistogramSize = HistogramSize;
160 AccessHistogram = Histogram;
163 void Merge(const MemInfoBlock &newMIB) {
164 AllocCount += newMIB.AllocCount;
166 TotalAccessCount += newMIB.TotalAccessCount;
167 MinAccessCount = newMIB.MinAccessCount < MinAccessCount ? newMIB.MinAccessCount : MinAccessCount;
168 MaxAccessCount = newMIB.MaxAccessCount > MaxAccessCount ? newMIB.MaxAccessCount : MaxAccessCount;
170 TotalSize += newMIB.TotalSize;
171 MinSize = newMIB.MinSize < MinSize ? newMIB.MinSize : MinSize;
172 MaxSize = newMIB.MaxSize > MaxSize ? newMIB.MaxSize : MaxSize;
174 TotalLifetime += newMIB.TotalLifetime;
175 MinLifetime = newMIB.MinLifetime < MinLifetime ? newMIB.MinLifetime : MinLifetime;
176 MaxLifetime = newMIB.MaxLifetime > MaxLifetime ? newMIB.MaxLifetime : MaxLifetime;
178 TotalAccessDensity += newMIB.TotalAccessDensity;
179 MinAccessDensity = newMIB.MinAccessDensity < MinAccessDensity
180 ? newMIB.MinAccessDensity
182 MaxAccessDensity = newMIB.MaxAccessDensity > MaxAccessDensity
183 ? newMIB.MaxAccessDensity
186 TotalLifetimeAccessDensity += newMIB.TotalLifetimeAccessDensity;
187 MinLifetimeAccessDensity =
188 newMIB.MinLifetimeAccessDensity < MinLifetimeAccessDensity
189 ? newMIB.MinLifetimeAccessDensity
190 : MinLifetimeAccessDensity;
191 MaxLifetimeAccessDensity =
192 newMIB.MaxLifetimeAccessDensity > MaxLifetimeAccessDensity
193 ? newMIB.MaxLifetimeAccessDensity
194 : MaxLifetimeAccessDensity;
196 // We know newMIB was deallocated later, so just need to check if it was
197 // allocated before last one deallocated.
198 NumLifetimeOverlaps += newMIB.AllocTimestamp < DeallocTimestamp;
199 AllocTimestamp = newMIB.AllocTimestamp;
200 DeallocTimestamp = newMIB.DeallocTimestamp;
202 NumSameAllocCpu += AllocCpuId == newMIB.AllocCpuId;
203 NumSameDeallocCpu += DeallocCpuId == newMIB.DeallocCpuId;
204 AllocCpuId = newMIB.AllocCpuId;
205 DeallocCpuId = newMIB.DeallocCpuId;
207 // For merging histograms, we always keep the longer histogram, and add
208 // values of shorter histogram to larger one.
209 uintptr_t ShorterHistogram;
210 uint32_t ShorterHistogramSize;
211 if (newMIB.AccessHistogramSize > AccessHistogramSize) {
212 ShorterHistogram = AccessHistogram;
213 ShorterHistogramSize = AccessHistogramSize;
214 // Swap histogram of current to larger histogram
215 AccessHistogram = newMIB.AccessHistogram;
216 AccessHistogramSize = newMIB.AccessHistogramSize;
218 ShorterHistogram = newMIB.AccessHistogram;
219 ShorterHistogramSize = newMIB.AccessHistogramSize;
221 for (size_t i = 0; i < ShorterHistogramSize; ++i) {
222 ((uint64_t *)AccessHistogram)[i] += ((uint64_t *)ShorterHistogram)[i];
227 } __pragma(pack(pop));
229 } __attribute__((__packed__));
232 } // namespace memprof