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 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 21 | #include "llvm/ProfileData/SampleProfWriter.h" |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringRef.h" |
| 23 | #include "llvm/ProfileData/ProfileCommon.h" |
| 24 | #include "llvm/ProfileData/SampleProf.h" |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ErrorOr.h" |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 26 | #include "llvm/Support/FileSystem.h" |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 27 | #include "llvm/Support/LEB128.h" |
Wei Mi | a0c0857 | 2018-06-11 22:40:43 +0000 | [diff] [blame] | 28 | #include "llvm/Support/MD5.h" |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
| 30 | #include <algorithm> |
| 31 | #include <cstdint> |
| 32 | #include <memory> |
Dehao Chen | 8d1c983 | 2017-05-11 23:43:44 +0000 | [diff] [blame] | 33 | #include <set> |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 34 | #include <system_error> |
| 35 | #include <utility> |
| 36 | #include <vector> |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 37 | |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 38 | using namespace llvm; |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 39 | using namespace sampleprof; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 40 | |
Dehao Chen | 8d1c983 | 2017-05-11 23:43:44 +0000 | [diff] [blame] | 41 | std::error_code |
| 42 | SampleProfileWriter::write(const StringMap<FunctionSamples> &ProfileMap) { |
| 43 | if (std::error_code EC = writeHeader(ProfileMap)) |
| 44 | return EC; |
| 45 | |
| 46 | // Sort the ProfileMap by total samples. |
| 47 | typedef std::pair<StringRef, const FunctionSamples *> NameFunctionSamples; |
| 48 | std::vector<NameFunctionSamples> V; |
| 49 | for (const auto &I : ProfileMap) |
| 50 | V.push_back(std::make_pair(I.getKey(), &I.second)); |
| 51 | |
| 52 | std::stable_sort( |
| 53 | V.begin(), V.end(), |
| 54 | [](const NameFunctionSamples &A, const NameFunctionSamples &B) { |
| 55 | if (A.second->getTotalSamples() == B.second->getTotalSamples()) |
| 56 | return A.first > B.first; |
| 57 | return A.second->getTotalSamples() > B.second->getTotalSamples(); |
| 58 | }); |
| 59 | |
| 60 | for (const auto &I : V) { |
| 61 | if (std::error_code EC = write(*I.second)) |
| 62 | return EC; |
| 63 | } |
| 64 | return sampleprof_error::success; |
| 65 | } |
| 66 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 67 | /// Write samples to a text file. |
Diego Novillo | 8e415a8 | 2015-11-13 20:24:28 +0000 | [diff] [blame] | 68 | /// |
| 69 | /// Note: it may be tempting to implement this in terms of |
Diego Novillo | ef548d2 | 2015-11-19 15:33:08 +0000 | [diff] [blame] | 70 | /// FunctionSamples::print(). Please don't. The dump functionality is intended |
Diego Novillo | 8e415a8 | 2015-11-13 20:24:28 +0000 | [diff] [blame] | 71 | /// for debugging and has no specified form. |
| 72 | /// |
| 73 | /// The format used here is more structured and deliberate because |
| 74 | /// it needs to be parsed by the SampleProfileReaderText class. |
Dehao Chen | 57d1dda | 2016-03-03 18:09:32 +0000 | [diff] [blame] | 75 | std::error_code SampleProfileWriterText::write(const FunctionSamples &S) { |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 76 | auto &OS = *OutputStream; |
Dehao Chen | 57d1dda | 2016-03-03 18:09:32 +0000 | [diff] [blame] | 77 | OS << S.getName() << ":" << S.getTotalSamples(); |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 78 | if (Indent == 0) |
| 79 | OS << ":" << S.getHeadSamples(); |
| 80 | OS << "\n"; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 81 | |
Diego Novillo | ef548d2 | 2015-11-19 15:33:08 +0000 | [diff] [blame] | 82 | SampleSorter<LineLocation, SampleRecord> SortedSamples(S.getBodySamples()); |
| 83 | for (const auto &I : SortedSamples.get()) { |
| 84 | LineLocation Loc = I->first; |
| 85 | const SampleRecord &Sample = I->second; |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 86 | OS.indent(Indent + 1); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 87 | if (Loc.Discriminator == 0) |
| 88 | OS << Loc.LineOffset << ": "; |
| 89 | else |
| 90 | OS << Loc.LineOffset << "." << Loc.Discriminator << ": "; |
| 91 | |
| 92 | OS << Sample.getSamples(); |
| 93 | |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 94 | for (const auto &J : Sample.getCallTargets()) |
| 95 | OS << " " << J.first() << ":" << J.second; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 96 | OS << "\n"; |
| 97 | } |
| 98 | |
Dehao Chen | 2c7ca9b | 2017-04-13 19:52:10 +0000 | [diff] [blame] | 99 | SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples( |
Diego Novillo | ef548d2 | 2015-11-19 15:33:08 +0000 | [diff] [blame] | 100 | S.getCallsiteSamples()); |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 101 | Indent += 1; |
Dehao Chen | 2c7ca9b | 2017-04-13 19:52:10 +0000 | [diff] [blame] | 102 | for (const auto &I : SortedCallsiteSamples.get()) |
| 103 | for (const auto &FS : I->second) { |
| 104 | LineLocation Loc = I->first; |
| 105 | const FunctionSamples &CalleeSamples = FS.second; |
| 106 | OS.indent(Indent); |
| 107 | if (Loc.Discriminator == 0) |
| 108 | OS << Loc.LineOffset << ": "; |
| 109 | else |
| 110 | OS << Loc.LineOffset << "." << Loc.Discriminator << ": "; |
| 111 | if (std::error_code EC = write(CalleeSamples)) |
| 112 | return EC; |
| 113 | } |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 114 | Indent -= 1; |
| 115 | |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 116 | return sampleprof_error::success; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 119 | std::error_code SampleProfileWriterBinary::writeNameIdx(StringRef FName) { |
| 120 | const auto &ret = NameTable.find(FName); |
| 121 | if (ret == NameTable.end()) |
| 122 | return sampleprof_error::truncated_name_table; |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 123 | encodeULEB128(ret->second, *OutputStream); |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 124 | return sampleprof_error::success; |
| 125 | } |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 126 | |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 127 | void SampleProfileWriterBinary::addName(StringRef FName) { |
Dehao Chen | 8d1c983 | 2017-05-11 23:43:44 +0000 | [diff] [blame] | 128 | NameTable.insert(std::make_pair(FName, 0)); |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void SampleProfileWriterBinary::addNames(const FunctionSamples &S) { |
| 132 | // Add all the names in indirect call targets. |
| 133 | for (const auto &I : S.getBodySamples()) { |
| 134 | const SampleRecord &Sample = I.second; |
| 135 | for (const auto &J : Sample.getCallTargets()) |
| 136 | addName(J.first()); |
| 137 | } |
| 138 | |
| 139 | // Recursively add all the names for inlined callsites. |
Dehao Chen | 2c7ca9b | 2017-04-13 19:52:10 +0000 | [diff] [blame] | 140 | for (const auto &J : S.getCallsiteSamples()) |
| 141 | for (const auto &FS : J.second) { |
| 142 | const FunctionSamples &CalleeSamples = FS.second; |
| 143 | addName(CalleeSamples.getName()); |
| 144 | addNames(CalleeSamples); |
| 145 | } |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Wei Mi | a0c0857 | 2018-06-11 22:40:43 +0000 | [diff] [blame] | 148 | void SampleProfileWriterBinary::stablizeNameTable(std::set<StringRef> &V) { |
| 149 | // Sort the names to make NameTable deterministic. |
| 150 | for (const auto &I : NameTable) |
| 151 | V.insert(I.first); |
| 152 | int i = 0; |
| 153 | for (const StringRef &N : V) |
| 154 | NameTable[N] = i++; |
| 155 | } |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 156 | |
Wei Mi | a0c0857 | 2018-06-11 22:40:43 +0000 | [diff] [blame] | 157 | std::error_code SampleProfileWriterRawBinary::writeNameTable() { |
| 158 | auto &OS = *OutputStream; |
| 159 | std::set<StringRef> V; |
| 160 | stablizeNameTable(V); |
| 161 | |
| 162 | // Write out the name table. |
| 163 | encodeULEB128(NameTable.size(), OS); |
| 164 | for (auto N : V) { |
| 165 | OS << N; |
| 166 | encodeULEB128(0, OS); |
| 167 | } |
| 168 | return sampleprof_error::success; |
| 169 | } |
| 170 | |
| 171 | std::error_code SampleProfileWriterCompactBinary::writeNameTable() { |
| 172 | auto &OS = *OutputStream; |
| 173 | std::set<StringRef> V; |
| 174 | stablizeNameTable(V); |
| 175 | |
| 176 | // Write out the name table. |
| 177 | encodeULEB128(NameTable.size(), OS); |
| 178 | for (auto N : V) { |
| 179 | encodeULEB128(MD5Hash(N), OS); |
| 180 | } |
| 181 | return sampleprof_error::success; |
| 182 | } |
| 183 | |
| 184 | std::error_code SampleProfileWriterRawBinary::writeMagicIdent() { |
| 185 | auto &OS = *OutputStream; |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 186 | // Write file magic identifier. |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 187 | encodeULEB128(SPMagic(), OS); |
| 188 | encodeULEB128(SPVersion(), OS); |
Wei Mi | a0c0857 | 2018-06-11 22:40:43 +0000 | [diff] [blame] | 189 | return sampleprof_error::success; |
| 190 | } |
| 191 | |
| 192 | std::error_code SampleProfileWriterCompactBinary::writeMagicIdent() { |
| 193 | auto &OS = *OutputStream; |
| 194 | // Write file magic identifier. |
| 195 | encodeULEB128(SPMagic(SPF_Compact_Binary), OS); |
| 196 | encodeULEB128(SPVersion(), OS); |
| 197 | return sampleprof_error::success; |
| 198 | } |
| 199 | |
| 200 | std::error_code SampleProfileWriterBinary::writeHeader( |
| 201 | const StringMap<FunctionSamples> &ProfileMap) { |
| 202 | writeMagicIdent(); |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 203 | |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 204 | computeSummary(ProfileMap); |
| 205 | if (auto EC = writeSummary()) |
| 206 | return EC; |
| 207 | |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 208 | // Generate the name table for all the functions referenced in the profile. |
| 209 | for (const auto &I : ProfileMap) { |
| 210 | addName(I.first()); |
| 211 | addNames(I.second); |
| 212 | } |
| 213 | |
Wei Mi | a0c0857 | 2018-06-11 22:40:43 +0000 | [diff] [blame] | 214 | writeNameTable(); |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 215 | return sampleprof_error::success; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 218 | std::error_code SampleProfileWriterBinary::writeSummary() { |
| 219 | auto &OS = *OutputStream; |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 220 | encodeULEB128(Summary->getTotalCount(), OS); |
| 221 | encodeULEB128(Summary->getMaxCount(), OS); |
Easwaran Raman | 6f4903d | 2016-03-28 23:14:29 +0000 | [diff] [blame] | 222 | encodeULEB128(Summary->getMaxFunctionCount(), OS); |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 223 | encodeULEB128(Summary->getNumCounts(), OS); |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 224 | encodeULEB128(Summary->getNumFunctions(), OS); |
| 225 | std::vector<ProfileSummaryEntry> &Entries = Summary->getDetailedSummary(); |
| 226 | encodeULEB128(Entries.size(), OS); |
| 227 | for (auto Entry : Entries) { |
| 228 | encodeULEB128(Entry.Cutoff, OS); |
| 229 | encodeULEB128(Entry.MinCount, OS); |
| 230 | encodeULEB128(Entry.NumCounts, OS); |
| 231 | } |
| 232 | return sampleprof_error::success; |
| 233 | } |
Dehao Chen | 57d1dda | 2016-03-03 18:09:32 +0000 | [diff] [blame] | 234 | std::error_code SampleProfileWriterBinary::writeBody(const FunctionSamples &S) { |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 235 | auto &OS = *OutputStream; |
| 236 | |
Dehao Chen | 57d1dda | 2016-03-03 18:09:32 +0000 | [diff] [blame] | 237 | if (std::error_code EC = writeNameIdx(S.getName())) |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 238 | return EC; |
| 239 | |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 240 | encodeULEB128(S.getTotalSamples(), OS); |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 241 | |
| 242 | // Emit all the body samples. |
Diego Novillo | b93483d | 2015-10-16 18:54:35 +0000 | [diff] [blame] | 243 | encodeULEB128(S.getBodySamples().size(), OS); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 244 | for (const auto &I : S.getBodySamples()) { |
| 245 | LineLocation Loc = I.first; |
| 246 | const SampleRecord &Sample = I.second; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 247 | encodeULEB128(Loc.LineOffset, OS); |
| 248 | encodeULEB128(Loc.Discriminator, OS); |
| 249 | encodeULEB128(Sample.getSamples(), OS); |
| 250 | encodeULEB128(Sample.getCallTargets().size(), OS); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 251 | for (const auto &J : Sample.getCallTargets()) { |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 252 | StringRef Callee = J.first(); |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 253 | uint64_t CalleeSamples = J.second; |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 254 | if (std::error_code EC = writeNameIdx(Callee)) |
| 255 | return EC; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 256 | encodeULEB128(CalleeSamples, OS); |
| 257 | } |
| 258 | } |
| 259 | |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 260 | // Recursively emit all the callsite samples. |
Dehao Chen | 2c27daf | 2017-08-03 00:09:18 +0000 | [diff] [blame] | 261 | uint64_t NumCallsites = 0; |
| 262 | for (const auto &J : S.getCallsiteSamples()) |
| 263 | NumCallsites += J.second.size(); |
| 264 | encodeULEB128(NumCallsites, OS); |
Dehao Chen | 2c7ca9b | 2017-04-13 19:52:10 +0000 | [diff] [blame] | 265 | for (const auto &J : S.getCallsiteSamples()) |
| 266 | for (const auto &FS : J.second) { |
| 267 | LineLocation Loc = J.first; |
| 268 | const FunctionSamples &CalleeSamples = FS.second; |
| 269 | encodeULEB128(Loc.LineOffset, OS); |
| 270 | encodeULEB128(Loc.Discriminator, OS); |
| 271 | if (std::error_code EC = writeBody(CalleeSamples)) |
| 272 | return EC; |
| 273 | } |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 274 | |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 275 | return sampleprof_error::success; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 276 | } |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 277 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 278 | /// Write samples of a top-level function to a binary file. |
Diego Novillo | b93483d | 2015-10-16 18:54:35 +0000 | [diff] [blame] | 279 | /// |
| 280 | /// \returns true if the samples were written successfully, false otherwise. |
Dehao Chen | 57d1dda | 2016-03-03 18:09:32 +0000 | [diff] [blame] | 281 | std::error_code SampleProfileWriterBinary::write(const FunctionSamples &S) { |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 282 | encodeULEB128(S.getHeadSamples(), *OutputStream); |
Dehao Chen | 57d1dda | 2016-03-03 18:09:32 +0000 | [diff] [blame] | 283 | return writeBody(S); |
Diego Novillo | b93483d | 2015-10-16 18:54:35 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 286 | /// Create a sample profile file writer based on the specified format. |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 287 | /// |
| 288 | /// \param Filename The file to create. |
| 289 | /// |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 290 | /// \param Format Encoding format for the profile file. |
| 291 | /// |
| 292 | /// \returns an error code indicating the status of the created writer. |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 293 | ErrorOr<std::unique_ptr<SampleProfileWriter>> |
| 294 | SampleProfileWriter::create(StringRef Filename, SampleProfileFormat Format) { |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 295 | std::error_code EC; |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 296 | std::unique_ptr<raw_ostream> OS; |
Wei Mi | d9be2c7 | 2018-06-12 05:53:49 +0000 | [diff] [blame] | 297 | if (Format == SPF_Binary || Format == SPF_Compact_Binary) |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 298 | OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::F_None)); |
| 299 | else |
| 300 | OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::F_Text)); |
| 301 | if (EC) |
| 302 | return EC; |
| 303 | |
| 304 | return create(OS, Format); |
| 305 | } |
| 306 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 307 | /// Create a sample profile stream writer based on the specified format. |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 308 | /// |
| 309 | /// \param OS The output stream to store the profile data to. |
| 310 | /// |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 311 | /// \param Format Encoding format for the profile file. |
| 312 | /// |
| 313 | /// \returns an error code indicating the status of the created writer. |
| 314 | ErrorOr<std::unique_ptr<SampleProfileWriter>> |
| 315 | SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS, |
| 316 | SampleProfileFormat Format) { |
| 317 | std::error_code EC; |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 318 | std::unique_ptr<SampleProfileWriter> Writer; |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 319 | |
Wei Mi | d9be2c7 | 2018-06-12 05:53:49 +0000 | [diff] [blame] | 320 | if (Format == SPF_Binary) |
Wei Mi | a0c0857 | 2018-06-11 22:40:43 +0000 | [diff] [blame] | 321 | Writer.reset(new SampleProfileWriterRawBinary(OS)); |
| 322 | else if (Format == SPF_Compact_Binary) |
| 323 | Writer.reset(new SampleProfileWriterCompactBinary(OS)); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 324 | else if (Format == SPF_Text) |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 325 | Writer.reset(new SampleProfileWriterText(OS)); |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 326 | else if (Format == SPF_GCC) |
| 327 | EC = sampleprof_error::unsupported_writing_format; |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 328 | else |
| 329 | EC = sampleprof_error::unrecognized_format; |
| 330 | |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 331 | if (EC) |
| 332 | return EC; |
| 333 | |
| 334 | return std::move(Writer); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 335 | } |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 336 | |
| 337 | void SampleProfileWriter::computeSummary( |
| 338 | const StringMap<FunctionSamples> &ProfileMap) { |
Easwaran Raman | e5a17e3 | 2016-05-19 21:07:12 +0000 | [diff] [blame] | 339 | SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs); |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 340 | for (const auto &I : ProfileMap) { |
| 341 | const FunctionSamples &Profile = I.second; |
Easwaran Raman | e5a17e3 | 2016-05-19 21:07:12 +0000 | [diff] [blame] | 342 | Builder.addRecord(Profile); |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 343 | } |
Benjamin Kramer | 38de59e | 2016-05-20 09:18:37 +0000 | [diff] [blame] | 344 | Summary = Builder.getSummary(); |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 345 | } |