1 //===- SampleProfWriter.cpp - Write LLVM sample profile data --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the class that writes LLVM sample profiles. It
11 // supports two file formats: text and binary. The textual representation
12 // is useful for debugging and testing purposes. The binary representation
13 // is more compact, resulting in smaller file sizes. However, they can
14 // both be used interchangeably.
16 // See lib/ProfileData/SampleProfReader.cpp for documentation on each of the
19 //===----------------------------------------------------------------------===//
21 #include "llvm/ProfileData/SampleProfWriter.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/ErrorOr.h"
24 #include "llvm/Support/LEB128.h"
25 #include "llvm/Support/LineIterator.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/Regex.h"
29 using namespace llvm::sampleprof;
32 /// \brief Write samples to a text file.
33 bool SampleProfileWriterText::write(StringRef FName, const FunctionSamples &S) {
34 OS << FName << ":" << S.getTotalSamples();
36 OS << ":" << S.getHeadSamples();
39 for (const auto &I : S.getBodySamples()) {
40 LineLocation Loc = I.first;
41 const SampleRecord &Sample = I.second;
42 OS.indent(Indent + 1);
43 if (Loc.Discriminator == 0)
44 OS << Loc.LineOffset << ": ";
46 OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
48 OS << Sample.getSamples();
50 for (const auto &J : Sample.getCallTargets())
51 OS << " " << J.first() << ":" << J.second;
56 for (const auto &I : S.getCallsiteSamples()) {
57 CallsiteLocation Loc = I.first;
58 const FunctionSamples &CalleeSamples = I.second;
60 if (Loc.Discriminator == 0)
61 OS << Loc.LineOffset << ": ";
63 OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
64 write(Loc.CalleeName, CalleeSamples);
71 SampleProfileWriterBinary::SampleProfileWriterBinary(StringRef F,
73 : SampleProfileWriter(F, EC, sys::fs::F_None) {
77 // Write the file header.
78 encodeULEB128(SPMagic(), OS);
79 encodeULEB128(SPVersion(), OS);
82 /// \brief Write samples to a binary file.
84 /// \returns true if the samples were written successfully, false otherwise.
85 bool SampleProfileWriterBinary::write(StringRef FName,
86 const FunctionSamples &S) {
89 encodeULEB128(S.getTotalSamples(), OS);
90 encodeULEB128(S.getHeadSamples(), OS);
91 encodeULEB128(S.getBodySamples().size(), OS);
93 // Emit all the body samples.
94 for (const auto &I : S.getBodySamples()) {
95 LineLocation Loc = I.first;
96 const SampleRecord &Sample = I.second;
97 encodeULEB128(Loc.LineOffset, OS);
98 encodeULEB128(Loc.Discriminator, OS);
99 encodeULEB128(Sample.getSamples(), OS);
100 encodeULEB128(Sample.getCallTargets().size(), OS);
101 for (const auto &J : Sample.getCallTargets()) {
102 std::string Callee = J.first();
103 unsigned CalleeSamples = J.second;
105 encodeULEB128(0, OS);
106 encodeULEB128(CalleeSamples, OS);
110 // Recursively emit all the callsite samples.
111 encodeULEB128(S.getCallsiteSamples().size(), OS);
112 for (const auto &J : S.getCallsiteSamples()) {
113 CallsiteLocation Loc = J.first;
114 const FunctionSamples &CalleeSamples = J.second;
115 encodeULEB128(Loc.LineOffset, OS);
116 encodeULEB128(Loc.Discriminator, OS);
117 write(Loc.CalleeName, CalleeSamples);
123 /// \brief Create a sample profile writer based on the specified format.
125 /// \param Filename The file to create.
127 /// \param Writer The writer to instantiate according to the specified format.
129 /// \param Format Encoding format for the profile file.
131 /// \returns an error code indicating the status of the created writer.
132 ErrorOr<std::unique_ptr<SampleProfileWriter>>
133 SampleProfileWriter::create(StringRef Filename, SampleProfileFormat Format) {
135 std::unique_ptr<SampleProfileWriter> Writer;
137 if (Format == SPF_Binary)
138 Writer.reset(new SampleProfileWriterBinary(Filename, EC));
139 else if (Format == SPF_Text)
140 Writer.reset(new SampleProfileWriterText(Filename, EC));
142 EC = sampleprof_error::unrecognized_format;
147 return std::move(Writer);