Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 1 | //===- SampleProfWriter.cpp - 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 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. |
| 15 | // |
| 16 | // See lib/ProfileData/SampleProfReader.cpp for documentation on each of the |
| 17 | // supported formats. |
| 18 | // |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | #include "llvm/ProfileData/SampleProfWriter.h" |
| 22 | #include "llvm/Support/Debug.h" |
| 23 | #include "llvm/Support/ErrorOr.h" |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 24 | #include "llvm/Support/LEB128.h" |
| 25 | #include "llvm/Support/LineIterator.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 26 | #include "llvm/Support/MemoryBuffer.h" |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Regex.h" |
| 28 | |
| 29 | using namespace llvm::sampleprof; |
| 30 | using namespace llvm; |
| 31 | |
| 32 | /// \brief Write samples to a text file. |
Diego Novillo | 8e415a8 | 2015-11-13 20:24:28 +0000 | [diff] [blame] | 33 | /// |
| 34 | /// Note: it may be tempting to implement this in terms of |
Diego Novillo | ef548d2 | 2015-11-19 15:33:08 +0000 | [diff] [blame] | 35 | /// FunctionSamples::print(). Please don't. The dump functionality is intended |
Diego Novillo | 8e415a8 | 2015-11-13 20:24:28 +0000 | [diff] [blame] | 36 | /// for debugging and has no specified form. |
| 37 | /// |
| 38 | /// The format used here is more structured and deliberate because |
| 39 | /// it needs to be parsed by the SampleProfileReaderText class. |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 40 | std::error_code SampleProfileWriterText::write(StringRef FName, |
| 41 | const FunctionSamples &S) { |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 42 | auto &OS = *OutputStream; |
| 43 | |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 44 | OS << FName << ":" << S.getTotalSamples(); |
| 45 | if (Indent == 0) |
| 46 | OS << ":" << S.getHeadSamples(); |
| 47 | OS << "\n"; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 48 | |
Diego Novillo | ef548d2 | 2015-11-19 15:33:08 +0000 | [diff] [blame] | 49 | SampleSorter<LineLocation, SampleRecord> SortedSamples(S.getBodySamples()); |
| 50 | for (const auto &I : SortedSamples.get()) { |
| 51 | LineLocation Loc = I->first; |
| 52 | const SampleRecord &Sample = I->second; |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 53 | OS.indent(Indent + 1); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 54 | if (Loc.Discriminator == 0) |
| 55 | OS << Loc.LineOffset << ": "; |
| 56 | else |
| 57 | OS << Loc.LineOffset << "." << Loc.Discriminator << ": "; |
| 58 | |
| 59 | OS << Sample.getSamples(); |
| 60 | |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 61 | for (const auto &J : Sample.getCallTargets()) |
| 62 | OS << " " << J.first() << ":" << J.second; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 63 | OS << "\n"; |
| 64 | } |
| 65 | |
Diego Novillo | ef548d2 | 2015-11-19 15:33:08 +0000 | [diff] [blame] | 66 | SampleSorter<CallsiteLocation, FunctionSamples> SortedCallsiteSamples( |
| 67 | S.getCallsiteSamples()); |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 68 | Indent += 1; |
Diego Novillo | ef548d2 | 2015-11-19 15:33:08 +0000 | [diff] [blame] | 69 | for (const auto &I : SortedCallsiteSamples.get()) { |
| 70 | CallsiteLocation Loc = I->first; |
| 71 | const FunctionSamples &CalleeSamples = I->second; |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 72 | OS.indent(Indent); |
| 73 | if (Loc.Discriminator == 0) |
| 74 | OS << Loc.LineOffset << ": "; |
| 75 | else |
| 76 | OS << Loc.LineOffset << "." << Loc.Discriminator << ": "; |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 77 | if (std::error_code EC = write(Loc.CalleeName, CalleeSamples)) |
| 78 | return EC; |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 79 | } |
| 80 | Indent -= 1; |
| 81 | |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 82 | return sampleprof_error::success; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 85 | std::error_code SampleProfileWriterBinary::writeNameIdx(StringRef FName) { |
| 86 | const auto &ret = NameTable.find(FName); |
| 87 | if (ret == NameTable.end()) |
| 88 | return sampleprof_error::truncated_name_table; |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 89 | encodeULEB128(ret->second, *OutputStream); |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 90 | return sampleprof_error::success; |
| 91 | } |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 92 | |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 93 | void SampleProfileWriterBinary::addName(StringRef FName) { |
| 94 | auto NextIdx = NameTable.size(); |
| 95 | NameTable.insert(std::make_pair(FName, NextIdx)); |
| 96 | } |
| 97 | |
| 98 | void SampleProfileWriterBinary::addNames(const FunctionSamples &S) { |
| 99 | // Add all the names in indirect call targets. |
| 100 | for (const auto &I : S.getBodySamples()) { |
| 101 | const SampleRecord &Sample = I.second; |
| 102 | for (const auto &J : Sample.getCallTargets()) |
| 103 | addName(J.first()); |
| 104 | } |
| 105 | |
| 106 | // Recursively add all the names for inlined callsites. |
| 107 | for (const auto &J : S.getCallsiteSamples()) { |
| 108 | CallsiteLocation Loc = J.first; |
| 109 | const FunctionSamples &CalleeSamples = J.second; |
| 110 | addName(Loc.CalleeName); |
| 111 | addNames(CalleeSamples); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | std::error_code SampleProfileWriterBinary::writeHeader( |
| 116 | const StringMap<FunctionSamples> &ProfileMap) { |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 117 | auto &OS = *OutputStream; |
| 118 | |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 119 | // Write file magic identifier. |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 120 | encodeULEB128(SPMagic(), OS); |
| 121 | encodeULEB128(SPVersion(), OS); |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 122 | |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame^] | 123 | computeSummary(ProfileMap); |
| 124 | if (auto EC = writeSummary()) |
| 125 | return EC; |
| 126 | |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 127 | // Generate the name table for all the functions referenced in the profile. |
| 128 | for (const auto &I : ProfileMap) { |
| 129 | addName(I.first()); |
| 130 | addNames(I.second); |
| 131 | } |
| 132 | |
| 133 | // Write out the name table. |
| 134 | encodeULEB128(NameTable.size(), OS); |
| 135 | for (auto N : NameTable) { |
| 136 | OS << N.first; |
| 137 | encodeULEB128(0, OS); |
| 138 | } |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 139 | return sampleprof_error::success; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame^] | 142 | std::error_code SampleProfileWriterBinary::writeSummary() { |
| 143 | auto &OS = *OutputStream; |
| 144 | encodeULEB128(Summary->getTotalSamples(), OS); |
| 145 | encodeULEB128(Summary->getMaxSamplesPerLine(), OS); |
| 146 | encodeULEB128(Summary->getMaxHeadSamples(), OS); |
| 147 | encodeULEB128(Summary->getNumLinesWithSamples(), OS); |
| 148 | encodeULEB128(Summary->getNumFunctions(), OS); |
| 149 | std::vector<ProfileSummaryEntry> &Entries = Summary->getDetailedSummary(); |
| 150 | encodeULEB128(Entries.size(), OS); |
| 151 | for (auto Entry : Entries) { |
| 152 | encodeULEB128(Entry.Cutoff, OS); |
| 153 | encodeULEB128(Entry.MinCount, OS); |
| 154 | encodeULEB128(Entry.NumCounts, OS); |
| 155 | } |
| 156 | return sampleprof_error::success; |
| 157 | } |
Diego Novillo | b93483d | 2015-10-16 18:54:35 +0000 | [diff] [blame] | 158 | std::error_code SampleProfileWriterBinary::writeBody(StringRef FName, |
| 159 | const FunctionSamples &S) { |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 160 | auto &OS = *OutputStream; |
| 161 | |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 162 | if (std::error_code EC = writeNameIdx(FName)) |
| 163 | return EC; |
| 164 | |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 165 | encodeULEB128(S.getTotalSamples(), OS); |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 166 | |
| 167 | // Emit all the body samples. |
Diego Novillo | b93483d | 2015-10-16 18:54:35 +0000 | [diff] [blame] | 168 | encodeULEB128(S.getBodySamples().size(), OS); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 169 | for (const auto &I : S.getBodySamples()) { |
| 170 | LineLocation Loc = I.first; |
| 171 | const SampleRecord &Sample = I.second; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 172 | encodeULEB128(Loc.LineOffset, OS); |
| 173 | encodeULEB128(Loc.Discriminator, OS); |
| 174 | encodeULEB128(Sample.getSamples(), OS); |
| 175 | encodeULEB128(Sample.getCallTargets().size(), OS); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 176 | for (const auto &J : Sample.getCallTargets()) { |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 177 | StringRef Callee = J.first(); |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 178 | uint64_t CalleeSamples = J.second; |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 179 | if (std::error_code EC = writeNameIdx(Callee)) |
| 180 | return EC; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 181 | encodeULEB128(CalleeSamples, OS); |
| 182 | } |
| 183 | } |
| 184 | |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 185 | // Recursively emit all the callsite samples. |
| 186 | encodeULEB128(S.getCallsiteSamples().size(), OS); |
| 187 | for (const auto &J : S.getCallsiteSamples()) { |
| 188 | CallsiteLocation Loc = J.first; |
| 189 | const FunctionSamples &CalleeSamples = J.second; |
| 190 | encodeULEB128(Loc.LineOffset, OS); |
| 191 | encodeULEB128(Loc.Discriminator, OS); |
Diego Novillo | b93483d | 2015-10-16 18:54:35 +0000 | [diff] [blame] | 192 | if (std::error_code EC = writeBody(Loc.CalleeName, CalleeSamples)) |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 193 | return EC; |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 196 | return sampleprof_error::success; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 197 | } |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 198 | |
Diego Novillo | b93483d | 2015-10-16 18:54:35 +0000 | [diff] [blame] | 199 | /// \brief Write samples of a top-level function to a binary file. |
| 200 | /// |
| 201 | /// \returns true if the samples were written successfully, false otherwise. |
| 202 | std::error_code SampleProfileWriterBinary::write(StringRef FName, |
| 203 | const FunctionSamples &S) { |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 204 | encodeULEB128(S.getHeadSamples(), *OutputStream); |
Diego Novillo | b93483d | 2015-10-16 18:54:35 +0000 | [diff] [blame] | 205 | return writeBody(FName, S); |
| 206 | } |
| 207 | |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 208 | /// \brief Create a sample profile file writer based on the specified format. |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 209 | /// |
| 210 | /// \param Filename The file to create. |
| 211 | /// |
| 212 | /// \param Writer The writer to instantiate according to the specified format. |
| 213 | /// |
| 214 | /// \param Format Encoding format for the profile file. |
| 215 | /// |
| 216 | /// \returns an error code indicating the status of the created writer. |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 217 | ErrorOr<std::unique_ptr<SampleProfileWriter>> |
| 218 | SampleProfileWriter::create(StringRef Filename, SampleProfileFormat Format) { |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 219 | std::error_code EC; |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 220 | std::unique_ptr<raw_ostream> OS; |
| 221 | if (Format == SPF_Binary) |
| 222 | OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::F_None)); |
| 223 | else |
| 224 | OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::F_Text)); |
| 225 | if (EC) |
| 226 | return EC; |
| 227 | |
| 228 | return create(OS, Format); |
| 229 | } |
| 230 | |
| 231 | /// \brief Create a sample profile stream writer based on the specified format. |
| 232 | /// |
| 233 | /// \param OS The output stream to store the profile data to. |
| 234 | /// |
| 235 | /// \param Writer The writer to instantiate according to the specified format. |
| 236 | /// |
| 237 | /// \param Format Encoding format for the profile file. |
| 238 | /// |
| 239 | /// \returns an error code indicating the status of the created writer. |
| 240 | ErrorOr<std::unique_ptr<SampleProfileWriter>> |
| 241 | SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS, |
| 242 | SampleProfileFormat Format) { |
| 243 | std::error_code EC; |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 244 | std::unique_ptr<SampleProfileWriter> Writer; |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 245 | |
| 246 | if (Format == SPF_Binary) |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 247 | Writer.reset(new SampleProfileWriterBinary(OS)); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 248 | else if (Format == SPF_Text) |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 249 | Writer.reset(new SampleProfileWriterText(OS)); |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 250 | else if (Format == SPF_GCC) |
| 251 | EC = sampleprof_error::unsupported_writing_format; |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 252 | else |
| 253 | EC = sampleprof_error::unrecognized_format; |
| 254 | |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 255 | if (EC) |
| 256 | return EC; |
| 257 | |
| 258 | return std::move(Writer); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 259 | } |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame^] | 260 | |
| 261 | void SampleProfileWriter::computeSummary( |
| 262 | const StringMap<FunctionSamples> &ProfileMap) { |
| 263 | Summary.reset(new SampleProfileSummary(ProfileSummary::DefaultCutoffs)); |
| 264 | for (const auto &I : ProfileMap) { |
| 265 | const FunctionSamples &Profile = I.second; |
| 266 | Summary->addRecord(Profile); |
| 267 | } |
| 268 | Summary->computeDetailedSummary(); |
| 269 | } |