LLVM  4.0.0
SampleProfWriter.h
Go to the documentation of this file.
1 //===- SampleProfWriter.h - Write LLVM sample profile data ----------------===//
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 definitions needed for writing sample profiles.
11 //
12 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_PROFILEDATA_SAMPLEPROFWRITER_H
14 #define LLVM_PROFILEDATA_SAMPLEPROFWRITER_H
15 
16 #include "llvm/ADT/MapVector.h"
17 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Support/ErrorOr.h"
23 
24 namespace llvm {
25 
26 namespace sampleprof {
27 
29 
30 /// \brief Sample-based profile writer. Base class.
32 public:
33  virtual ~SampleProfileWriter() {}
34 
35  /// Write sample profiles in \p S.
36  ///
37  /// \returns status code of the file update operation.
38  virtual std::error_code write(const FunctionSamples &S) = 0;
39 
40  /// Write all the sample profiles in the given map of samples.
41  ///
42  /// \returns status code of the file update operation.
43  std::error_code write(const StringMap<FunctionSamples> &ProfileMap) {
44  if (std::error_code EC = writeHeader(ProfileMap))
45  return EC;
46  for (const auto &I : ProfileMap) {
47  const FunctionSamples &Profile = I.second;
48  if (std::error_code EC = write(Profile))
49  return EC;
50  }
52  }
53 
55 
56  /// Profile writer factory.
57  ///
58  /// Create a new file writer based on the value of \p Format.
60  create(StringRef Filename, SampleProfileFormat Format);
61 
62  /// Create a new stream writer based on the value of \p Format.
63  /// For testing.
65  create(std::unique_ptr<raw_ostream> &OS, SampleProfileFormat Format);
66 
67 protected:
68  SampleProfileWriter(std::unique_ptr<raw_ostream> &OS)
69  : OutputStream(std::move(OS)) {}
70 
71  /// \brief Write a file header for the profile file.
72  virtual std::error_code
73  writeHeader(const StringMap<FunctionSamples> &ProfileMap) = 0;
74 
75  /// \brief Output stream where to emit the profile to.
76  std::unique_ptr<raw_ostream> OutputStream;
77 
78  /// \brief Profile summary.
79  std::unique_ptr<ProfileSummary> Summary;
80 
81  /// \brief Compute summary for this profile.
82  void computeSummary(const StringMap<FunctionSamples> &ProfileMap);
83 };
84 
85 /// \brief Sample-based profile writer (text format).
87 public:
88  std::error_code write(const FunctionSamples &S) override;
89 
90 protected:
91  SampleProfileWriterText(std::unique_ptr<raw_ostream> &OS)
92  : SampleProfileWriter(OS), Indent(0) {}
93 
94  std::error_code
95  writeHeader(const StringMap<FunctionSamples> &ProfileMap) override {
97  }
98 
99 private:
100  /// Indent level to use when writing.
101  ///
102  /// This is used when printing inlined callees.
103  unsigned Indent;
104 
106  SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
107  SampleProfileFormat Format);
108 };
109 
110 /// \brief Sample-based profile writer (binary format).
112 public:
113  std::error_code write(const FunctionSamples &S) override;
114 
115 protected:
116  SampleProfileWriterBinary(std::unique_ptr<raw_ostream> &OS)
117  : SampleProfileWriter(OS), NameTable() {}
118 
119  std::error_code
120  writeHeader(const StringMap<FunctionSamples> &ProfileMap) override;
121  std::error_code writeSummary();
122  std::error_code writeNameIdx(StringRef FName);
123  std::error_code writeBody(const FunctionSamples &S);
124 
125 private:
126  void addName(StringRef FName);
127  void addNames(const FunctionSamples &S);
128 
130 
132  SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
133  SampleProfileFormat Format);
134 };
135 
136 } // End namespace sampleprof
137 
138 } // End namespace llvm
139 
140 #endif // LLVM_PROFILEDATA_SAMPLEPROFWRITER_H
Represents either an error or a value T.
Definition: ErrorOr.h:68
Sample-based profile writer. Base class.
This class implements a map that also provides access to all stored values in a deterministic order...
Definition: MapVector.h:32
std::error_code writeHeader(const StringMap< FunctionSamples > &ProfileMap) override
Write a file header for the profile file.
std::unique_ptr< raw_ostream > OutputStream
Output stream where to emit the profile to.
SampleProfileWriterBinary(std::unique_ptr< raw_ostream > &OS)
std::error_code writeHeader(const StringMap< FunctionSamples > &ProfileMap) override
Write a file header for the profile file.
Representation of the samples collected for a function.
Definition: SampleProf.h:181
std::error_code writeNameIdx(StringRef FName)
std::unique_ptr< ProfileSummary > Summary
Profile summary.
Sample-based profile writer (binary format).
std::error_code write(const FunctionSamples &S) override
Write samples of a top-level function to a binary file.
virtual std::error_code write(const FunctionSamples &S)=0
Write sample profiles in S.
static ErrorOr< std::unique_ptr< SampleProfileWriter > > create(StringRef Filename, SampleProfileFormat Format)
Profile writer factory.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:223
std::error_code writeBody(const FunctionSamples &S)
SampleProfileWriterText(std::unique_ptr< raw_ostream > &OS)
#define I(x, y, z)
Definition: MD5.cpp:54
Provides ErrorOr<T> smart pointer.
virtual std::error_code writeHeader(const StringMap< FunctionSamples > &ProfileMap)=0
Write a file header for the profile file.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
SampleProfileWriter(std::unique_ptr< raw_ostream > &OS)
void computeSummary(const StringMap< FunctionSamples > &ProfileMap)
Compute summary for this profile.
std::error_code write(const FunctionSamples &S) override
Write samples to a text file.
Sample-based profile writer (text format).
std::error_code write(const StringMap< FunctionSamples > &ProfileMap)
Write all the sample profiles in the given map of samples.