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