LLVM  4.0.0
SampleProf.h
Go to the documentation of this file.
1 //=-- SampleProf.h - Sampling profiling format support --------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains common definitions used in the reading and writing of
11 // sample profile data.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_PROFILEDATA_SAMPLEPROF_H_
16 #define LLVM_PROFILEDATA_SAMPLEPROF_H_
17 
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/ErrorOr.h"
23 
24 #include <map>
25 #include <system_error>
26 
27 namespace llvm {
28 
30 
31 enum class sampleprof_error {
32  success = 0,
33  bad_magic,
35  too_large,
36  truncated,
37  malformed,
43 };
44 
45 inline std::error_code make_error_code(sampleprof_error E) {
46  return std::error_code(static_cast<int>(E), sampleprof_category());
47 }
48 
50  sampleprof_error Result) {
51  // Prefer first error encountered as later errors may be secondary effects of
52  // the initial problem.
53  if (Accumulator == sampleprof_error::success &&
54  Result != sampleprof_error::success)
55  Accumulator = Result;
56  return Accumulator;
57 }
58 
59 } // end namespace llvm
60 
61 namespace std {
62 template <>
63 struct is_error_code_enum<llvm::sampleprof_error> : std::true_type {};
64 }
65 
66 namespace llvm {
67 
68 namespace sampleprof {
69 
70 static inline uint64_t SPMagic() {
71  return uint64_t('S') << (64 - 8) | uint64_t('P') << (64 - 16) |
72  uint64_t('R') << (64 - 24) | uint64_t('O') << (64 - 32) |
73  uint64_t('F') << (64 - 40) | uint64_t('4') << (64 - 48) |
74  uint64_t('2') << (64 - 56) | uint64_t(0xff);
75 }
76 
77 static inline uint64_t SPVersion() { return 103; }
78 
79 /// Represents the relative location of an instruction.
80 ///
81 /// Instruction locations are specified by the line offset from the
82 /// beginning of the function (marked by the line where the function
83 /// header is) and the discriminator value within that line.
84 ///
85 /// The discriminator value is useful to distinguish instructions
86 /// that are on the same line but belong to different basic blocks
87 /// (e.g., the two post-increment instructions in "if (p) x++; else y++;").
88 struct LineLocation {
90  void print(raw_ostream &OS) const;
91  void dump() const;
92  bool operator<(const LineLocation &O) const {
93  return LineOffset < O.LineOffset ||
95  }
96 
99 };
100 
102 
103 /// Representation of a single sample record.
104 ///
105 /// A sample record is represented by a positive integer value, which
106 /// indicates how frequently was the associated line location executed.
107 ///
108 /// Additionally, if the associated location contains a function call,
109 /// the record will hold a list of all the possible called targets. For
110 /// direct calls, this will be the exact function being invoked. For
111 /// indirect calls (function pointers, virtual table dispatch), this
112 /// will be a list of one or more functions.
114 public:
116 
117  SampleRecord() : NumSamples(0), CallTargets() {}
118 
119  /// Increment the number of samples for this record by \p S.
120  /// Optionally scale sample count \p S by \p Weight.
121  ///
122  /// Sample counts accumulate using saturating arithmetic, to avoid wrapping
123  /// around unsigned integers.
124  sampleprof_error addSamples(uint64_t S, uint64_t Weight = 1) {
125  bool Overflowed;
126  NumSamples = SaturatingMultiplyAdd(S, Weight, NumSamples, &Overflowed);
127  return Overflowed ? sampleprof_error::counter_overflow
129  }
130 
131  /// Add called function \p F with samples \p S.
132  /// Optionally scale sample count \p S by \p Weight.
133  ///
134  /// Sample counts accumulate using saturating arithmetic, to avoid wrapping
135  /// around unsigned integers.
137  uint64_t Weight = 1) {
138  uint64_t &TargetSamples = CallTargets[F];
139  bool Overflowed;
140  TargetSamples =
141  SaturatingMultiplyAdd(S, Weight, TargetSamples, &Overflowed);
142  return Overflowed ? sampleprof_error::counter_overflow
144  }
145 
146  /// Return true if this sample record contains function calls.
147  bool hasCalls() const { return CallTargets.size() > 0; }
148 
149  uint64_t getSamples() const { return NumSamples; }
150  const CallTargetMap &getCallTargets() const { return CallTargets; }
151 
152  /// Merge the samples in \p Other into this record.
153  /// Optionally scale sample counts by \p Weight.
154  sampleprof_error merge(const SampleRecord &Other, uint64_t Weight = 1) {
155  sampleprof_error Result = addSamples(Other.getSamples(), Weight);
156  for (const auto &I : Other.getCallTargets()) {
157  MergeResult(Result, addCalledTarget(I.first(), I.second, Weight));
158  }
159  return Result;
160  }
161 
162  void print(raw_ostream &OS, unsigned Indent) const;
163  void dump() const;
164 
165 private:
166  uint64_t NumSamples;
167  CallTargetMap CallTargets;
168 };
169 
170 raw_ostream &operator<<(raw_ostream &OS, const SampleRecord &Sample);
171 
172 typedef std::map<LineLocation, SampleRecord> BodySampleMap;
174 typedef std::map<LineLocation, FunctionSamples> CallsiteSampleMap;
175 
176 /// Representation of the samples collected for a function.
177 ///
178 /// This data structure contains all the collected samples for the body
179 /// of a function. Each sample corresponds to a LineLocation instance
180 /// within the body of the function.
182 public:
183  FunctionSamples() : Name(), TotalSamples(0), TotalHeadSamples(0) {}
184  void print(raw_ostream &OS = dbgs(), unsigned Indent = 0) const;
185  void dump() const;
186  sampleprof_error addTotalSamples(uint64_t Num, uint64_t Weight = 1) {
187  bool Overflowed;
188  TotalSamples =
189  SaturatingMultiplyAdd(Num, Weight, TotalSamples, &Overflowed);
190  return Overflowed ? sampleprof_error::counter_overflow
192  }
193  sampleprof_error addHeadSamples(uint64_t Num, uint64_t Weight = 1) {
194  bool Overflowed;
195  TotalHeadSamples =
196  SaturatingMultiplyAdd(Num, Weight, TotalHeadSamples, &Overflowed);
197  return Overflowed ? sampleprof_error::counter_overflow
199  }
201  uint64_t Num, uint64_t Weight = 1) {
202  return BodySamples[LineLocation(LineOffset, Discriminator)].addSamples(
203  Num, Weight);
204  }
206  uint32_t Discriminator,
207  const std::string &FName,
208  uint64_t Num, uint64_t Weight = 1) {
209  return BodySamples[LineLocation(LineOffset, Discriminator)].addCalledTarget(
210  FName, Num, Weight);
211  }
212 
213  /// Return the number of samples collected at the given location.
214  /// Each location is specified by \p LineOffset and \p Discriminator.
215  /// If the location is not found in profile, return error.
217  uint32_t Discriminator) const {
218  const auto &ret = BodySamples.find(LineLocation(LineOffset, Discriminator));
219  if (ret == BodySamples.end())
220  return std::error_code();
221  else
222  return ret->second.getSamples();
223  }
224 
225  /// Return the total number of call target samples collected at a given
226  /// location. Each location is specified by \p LineOffset and
227  /// \p Discriminator. If the location is not found in profile, return error.
229  uint32_t Discriminator) const {
230  const auto &ret = BodySamples.find(LineLocation(LineOffset, Discriminator));
231  if (ret == BodySamples.end())
232  return std::error_code();
233  uint64_t T = 0;
234  for (const auto &t_c : ret->second.getCallTargets()) {
235  T += t_c.second;
236  }
237  return T;
238  }
239 
240  /// Return the function samples at the given callsite location.
242  return CallsiteSamples[Loc];
243  }
244 
245  /// Return a pointer to function samples at the given callsite location.
247  auto iter = CallsiteSamples.find(Loc);
248  if (iter == CallsiteSamples.end()) {
249  return nullptr;
250  } else {
251  return &iter->second;
252  }
253  }
254 
255  bool empty() const { return TotalSamples == 0; }
256 
257  /// Return the total number of samples collected inside the function.
258  uint64_t getTotalSamples() const { return TotalSamples; }
259 
260  /// Return the total number of samples collected at the head of the
261  /// function.
262  uint64_t getHeadSamples() const { return TotalHeadSamples; }
263 
264  /// Return all the samples collected in the body of the function.
265  const BodySampleMap &getBodySamples() const { return BodySamples; }
266 
267  /// Return all the callsite samples collected in the body of the function.
269  return CallsiteSamples;
270  }
271 
272  /// Merge the samples in \p Other into this one.
273  /// Optionally scale samples by \p Weight.
274  sampleprof_error merge(const FunctionSamples &Other, uint64_t Weight = 1) {
276  Name = Other.getName();
277  MergeResult(Result, addTotalSamples(Other.getTotalSamples(), Weight));
278  MergeResult(Result, addHeadSamples(Other.getHeadSamples(), Weight));
279  for (const auto &I : Other.getBodySamples()) {
280  const LineLocation &Loc = I.first;
281  const SampleRecord &Rec = I.second;
282  MergeResult(Result, BodySamples[Loc].merge(Rec, Weight));
283  }
284  for (const auto &I : Other.getCallsiteSamples()) {
285  const LineLocation &Loc = I.first;
286  const FunctionSamples &Rec = I.second;
287  MergeResult(Result, functionSamplesAt(Loc).merge(Rec, Weight));
288  }
289  return Result;
290  }
291 
292  /// Set the name of the function.
293  void setName(StringRef FunctionName) { Name = FunctionName; }
294 
295  /// Return the function name.
296  const StringRef &getName() const { return Name; }
297 
298 private:
299  /// Mangled name of the function.
300  StringRef Name;
301 
302  /// Total number of samples collected inside this function.
303  ///
304  /// Samples are cumulative, they include all the samples collected
305  /// inside this function and all its inlined callees.
306  uint64_t TotalSamples;
307 
308  /// Total number of samples collected at the head of the function.
309  /// This is an approximation of the number of calls made to this function
310  /// at runtime.
311  uint64_t TotalHeadSamples;
312 
313  /// Map instruction locations to collected samples.
314  ///
315  /// Each entry in this map contains the number of samples
316  /// collected at the corresponding line offset. All line locations
317  /// are an offset from the start of the function.
318  BodySampleMap BodySamples;
319 
320  /// Map call sites to collected samples for the called function.
321  ///
322  /// Each entry in this map corresponds to all the samples
323  /// collected for the inlined function call at the given
324  /// location. For example, given:
325  ///
326  /// void foo() {
327  /// 1 bar();
328  /// ...
329  /// 8 baz();
330  /// }
331  ///
332  /// If the bar() and baz() calls were inlined inside foo(), this
333  /// map will contain two entries. One for all the samples collected
334  /// in the call to bar() at line offset 1, the other for all the samples
335  /// collected in the call to baz() at line offset 8.
336  CallsiteSampleMap CallsiteSamples;
337 };
338 
339 raw_ostream &operator<<(raw_ostream &OS, const FunctionSamples &FS);
340 
341 /// Sort a LocationT->SampleT map by LocationT.
342 ///
343 /// It produces a sorted list of <LocationT, SampleT> records by ascending
344 /// order of LocationT.
345 template <class LocationT, class SampleT> class SampleSorter {
346 public:
347  typedef std::pair<const LocationT, SampleT> SamplesWithLoc;
349 
350  SampleSorter(const std::map<LocationT, SampleT> &Samples) {
351  for (const auto &I : Samples)
352  V.push_back(&I);
353  std::stable_sort(V.begin(), V.end(),
354  [](const SamplesWithLoc *A, const SamplesWithLoc *B) {
355  return A->first < B->first;
356  });
357  }
358  const SamplesWithLocList &get() const { return V; }
359 
360 private:
362 };
363 
364 } // end namespace sampleprof
365 
366 } // end namespace llvm
367 
368 #endif // LLVM_PROFILEDATA_SAMPLEPROF_H_
MachineLoop * L
void push_back(const T &Elt)
Definition: SmallVector.h:211
Represents either an error or a value T.
Definition: ErrorOr.h:68
raw_ostream & operator<<(raw_ostream &OS, const LineLocation &Loc)
Definition: SampleProf.cpp:71
const BodySampleMap & getBodySamples() const
Return all the samples collected in the body of the function.
Definition: SampleProf.h:265
SmallVector< const SamplesWithLoc *, 20 > SamplesWithLocList
Definition: SampleProf.h:348
sampleprof_error merge(const SampleRecord &Other, uint64_t Weight=1)
Merge the samples in Other into this record.
Definition: SampleProf.h:154
LineLocation(uint32_t L, uint32_t D)
Definition: SampleProf.h:89
SampleSorter(const std::map< LocationT, SampleT > &Samples)
Definition: SampleProf.h:350
const FunctionSamples * findFunctionSamplesAt(const LineLocation &Loc) const
Return a pointer to function samples at the given callsite location.
Definition: SampleProf.h:246
const StringRef & getName() const
Return the function name.
Definition: SampleProf.h:296
const CallTargetMap & getCallTargets() const
Definition: SampleProf.h:150
Representation of the samples collected for a function.
Definition: SampleProf.h:181
static GCRegistry::Add< StatepointGC > D("statepoint-example","an example strategy for statepoint")
std::error_code make_error_code(BitcodeError E)
uint64_t getHeadSamples() const
Return the total number of samples collected at the head of the function.
Definition: SampleProf.h:262
ErrorOr< uint64_t > findCallSamplesAt(uint32_t LineOffset, uint32_t Discriminator) const
Return the total number of call target samples collected at a given location.
Definition: SampleProf.h:228
std::map< LineLocation, SampleRecord > BodySampleMap
Definition: SampleProf.h:172
#define F(x, y, z)
Definition: MD5.cpp:51
#define T
std::map< LineLocation, FunctionSamples > CallsiteSampleMap
Definition: SampleProf.h:173
sampleprof_error addCalledTarget(StringRef F, uint64_t S, uint64_t Weight=1)
Add called function F with samples S.
Definition: SampleProf.h:136
Representation of a single sample record.
Definition: SampleProf.h:113
void setName(StringRef FunctionName)
Set the name of the function.
Definition: SampleProf.h:293
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
bool operator<(const LineLocation &O) const
Definition: SampleProf.h:92
static uint64_t SPVersion()
Definition: SampleProf.h:77
Sort a LocationT->SampleT map by LocationT.
Definition: SampleProf.h:345
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
void print(raw_ostream &OS=dbgs(), unsigned Indent=0) const
Print the samples collected for a function on stream OS.
Definition: SampleProf.cpp:99
sampleprof_error
Definition: SampleProf.h:31
sampleprof_error addBodySamples(uint32_t LineOffset, uint32_t Discriminator, uint64_t Num, uint64_t Weight=1)
Definition: SampleProf.h:200
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:115
sampleprof_error addSamples(uint64_t S, uint64_t Weight=1)
Increment the number of samples for this record by S.
Definition: SampleProf.h:124
unsigned size() const
Definition: StringMap.h:114
static ManagedStatic< _object_error_category > error_category
uint64_t getTotalSamples() const
Return the total number of samples collected inside the function.
Definition: SampleProf.h:258
std::enable_if< std::is_unsigned< T >::value, T >::type SaturatingMultiplyAdd(T X, T Y, T A, bool *ResultOverflowed=nullptr)
Multiply two unsigned integers, X and Y, and add the unsigned integer, A to the product.
Definition: MathExtras.h:813
const CallsiteSampleMap & getCallsiteSamples() const
Return all the callsite samples collected in the body of the function.
Definition: SampleProf.h:268
sampleprof_error addCalledTargetSamples(uint32_t LineOffset, uint32_t Discriminator, const std::string &FName, uint64_t Num, uint64_t Weight=1)
Definition: SampleProf.h:205
sampleprof_error addTotalSamples(uint64_t Num, uint64_t Weight=1)
Definition: SampleProf.h:186
std::pair< const LocationT, SampleT > SamplesWithLoc
Definition: SampleProf.h:347
uint64_t getSamples() const
Definition: SampleProf.h:149
StringMap< uint64_t > CallTargetMap
Definition: SampleProf.h:115
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
const std::error_category & sampleprof_category()
Definition: SampleProf.cpp:61
void print(raw_ostream &OS) const
Definition: SampleProf.cpp:65
bool hasCalls() const
Return true if this sample record contains function calls.
Definition: SampleProf.h:147
static uint64_t SPMagic()
Definition: SampleProf.h:70
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:119
Represents the relative location of an instruction.
Definition: SampleProf.h:88
#define I(x, y, z)
Definition: MD5.cpp:54
sampleprof_error addHeadSamples(uint64_t Num, uint64_t Weight=1)
Definition: SampleProf.h:193
Provides ErrorOr<T> smart pointer.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
ErrorOr< uint64_t > findSamplesAt(uint32_t LineOffset, uint32_t Discriminator) const
Return the number of samples collected at the given location.
Definition: SampleProf.h:216
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
void print(raw_ostream &OS, unsigned Indent) const
Print the sample record to the stream OS indented by Indent.
Definition: SampleProf.cpp:80
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
sampleprof_error merge(const FunctionSamples &Other, uint64_t Weight=1)
Merge the samples in Other into this one.
Definition: SampleProf.h:274
sampleprof_error MergeResult(sampleprof_error &Accumulator, sampleprof_error Result)
Definition: SampleProf.h:49
FunctionSamples & functionSamplesAt(const LineLocation &Loc)
Return the function samples at the given callsite location.
Definition: SampleProf.h:241