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