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 | |
Wei Mi | be90732 | 2019-08-23 19:05:30 +0000 | [diff] [blame] | 42 | std::error_code SampleProfileWriter::writeFuncProfiles( |
| 43 | const StringMap<FunctionSamples> &ProfileMap) { |
Dehao Chen | 8d1c983 | 2017-05-11 23:43:44 +0000 | [diff] [blame] | 44 | // Sort the ProfileMap by total samples. |
| 45 | typedef std::pair<StringRef, const FunctionSamples *> NameFunctionSamples; |
| 46 | std::vector<NameFunctionSamples> V; |
| 47 | for (const auto &I : ProfileMap) |
| 48 | V.push_back(std::make_pair(I.getKey(), &I.second)); |
| 49 | |
Fangrui Song | efd94c5 | 2019-04-23 14:51:27 +0000 | [diff] [blame] | 50 | llvm::stable_sort( |
| 51 | V, [](const NameFunctionSamples &A, const NameFunctionSamples &B) { |
Dehao Chen | 8d1c983 | 2017-05-11 23:43:44 +0000 | [diff] [blame] | 52 | if (A.second->getTotalSamples() == B.second->getTotalSamples()) |
| 53 | return A.first > B.first; |
| 54 | return A.second->getTotalSamples() > B.second->getTotalSamples(); |
| 55 | }); |
| 56 | |
| 57 | for (const auto &I : V) { |
Wei Mi | be90732 | 2019-08-23 19:05:30 +0000 | [diff] [blame] | 58 | if (std::error_code EC = writeSample(*I.second)) |
Dehao Chen | 8d1c983 | 2017-05-11 23:43:44 +0000 | [diff] [blame] | 59 | return EC; |
| 60 | } |
| 61 | return sampleprof_error::success; |
| 62 | } |
| 63 | |
Wei Mi | be90732 | 2019-08-23 19:05:30 +0000 | [diff] [blame] | 64 | std::error_code |
| 65 | SampleProfileWriter::write(const StringMap<FunctionSamples> &ProfileMap) { |
| 66 | if (std::error_code EC = writeHeader(ProfileMap)) |
| 67 | return EC; |
| 68 | |
| 69 | if (std::error_code EC = writeFuncProfiles(ProfileMap)) |
| 70 | return EC; |
| 71 | |
| 72 | return sampleprof_error::success; |
| 73 | } |
| 74 | |
| 75 | /// Return the current position and prepare to use it as the start |
| 76 | /// position of a section. |
| 77 | uint64_t SampleProfileWriterExtBinaryBase::markSectionStart() { |
| 78 | return OutputStream->tell(); |
| 79 | } |
| 80 | |
| 81 | /// Add a new section into section header table. Return the position |
| 82 | /// of SectionEnd. |
| 83 | uint64_t |
| 84 | SampleProfileWriterExtBinaryBase::addNewSection(SecType Sec, |
| 85 | uint64_t SectionStart) { |
| 86 | uint64_t SectionEnd = OutputStream->tell(); |
| 87 | SecHdrTable.push_back( |
| 88 | {Sec, 0, SectionStart - FileStart, SectionEnd - SectionStart}); |
| 89 | return SectionEnd; |
| 90 | } |
| 91 | |
| 92 | std::error_code SampleProfileWriterExtBinaryBase::write( |
| 93 | const StringMap<FunctionSamples> &ProfileMap) { |
| 94 | if (std::error_code EC = writeHeader(ProfileMap)) |
| 95 | return EC; |
| 96 | |
| 97 | if (std::error_code EC = writeSections(ProfileMap)) |
| 98 | return EC; |
| 99 | |
| 100 | if (std::error_code EC = writeSecHdrTable()) |
| 101 | return EC; |
| 102 | |
| 103 | return sampleprof_error::success; |
| 104 | } |
| 105 | |
| 106 | std::error_code SampleProfileWriterExtBinary::writeSections( |
| 107 | const StringMap<FunctionSamples> &ProfileMap) { |
| 108 | uint64_t SectionStart = markSectionStart(); |
| 109 | computeSummary(ProfileMap); |
| 110 | if (auto EC = writeSummary()) |
| 111 | return EC; |
| 112 | SectionStart = addNewSection(SecProfSummary, SectionStart); |
| 113 | |
| 114 | // Generate the name table for all the functions referenced in the profile. |
| 115 | for (const auto &I : ProfileMap) { |
| 116 | addName(I.first()); |
| 117 | addNames(I.second); |
| 118 | } |
| 119 | writeNameTable(); |
| 120 | SectionStart = addNewSection(SecNameTable, SectionStart); |
| 121 | |
| 122 | if (std::error_code EC = writeFuncProfiles(ProfileMap)) |
| 123 | return EC; |
| 124 | addNewSection(SecLBRProfile, SectionStart); |
| 125 | |
| 126 | return sampleprof_error::success; |
| 127 | } |
| 128 | |
Wei Mi | 6a14325 | 2018-09-14 20:52:59 +0000 | [diff] [blame] | 129 | std::error_code SampleProfileWriterCompactBinary::write( |
| 130 | const StringMap<FunctionSamples> &ProfileMap) { |
| 131 | if (std::error_code EC = SampleProfileWriter::write(ProfileMap)) |
| 132 | return EC; |
| 133 | if (std::error_code EC = writeFuncOffsetTable()) |
| 134 | return EC; |
| 135 | return sampleprof_error::success; |
| 136 | } |
| 137 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 138 | /// Write samples to a text file. |
Diego Novillo | 8e415a8 | 2015-11-13 20:24:28 +0000 | [diff] [blame] | 139 | /// |
| 140 | /// Note: it may be tempting to implement this in terms of |
Diego Novillo | ef548d2 | 2015-11-19 15:33:08 +0000 | [diff] [blame] | 141 | /// FunctionSamples::print(). Please don't. The dump functionality is intended |
Diego Novillo | 8e415a8 | 2015-11-13 20:24:28 +0000 | [diff] [blame] | 142 | /// for debugging and has no specified form. |
| 143 | /// |
| 144 | /// The format used here is more structured and deliberate because |
| 145 | /// it needs to be parsed by the SampleProfileReaderText class. |
Wei Mi | be90732 | 2019-08-23 19:05:30 +0000 | [diff] [blame] | 146 | std::error_code SampleProfileWriterText::writeSample(const FunctionSamples &S) { |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 147 | auto &OS = *OutputStream; |
Dehao Chen | 57d1dda | 2016-03-03 18:09:32 +0000 | [diff] [blame] | 148 | OS << S.getName() << ":" << S.getTotalSamples(); |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 149 | if (Indent == 0) |
| 150 | OS << ":" << S.getHeadSamples(); |
| 151 | OS << "\n"; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 152 | |
Diego Novillo | ef548d2 | 2015-11-19 15:33:08 +0000 | [diff] [blame] | 153 | SampleSorter<LineLocation, SampleRecord> SortedSamples(S.getBodySamples()); |
| 154 | for (const auto &I : SortedSamples.get()) { |
| 155 | LineLocation Loc = I->first; |
| 156 | const SampleRecord &Sample = I->second; |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 157 | OS.indent(Indent + 1); |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 158 | if (Loc.Discriminator == 0) |
| 159 | OS << Loc.LineOffset << ": "; |
| 160 | else |
| 161 | OS << Loc.LineOffset << "." << Loc.Discriminator << ": "; |
| 162 | |
| 163 | OS << Sample.getSamples(); |
| 164 | |
Wenlei He | 5adace3 | 2019-08-20 20:52:00 +0000 | [diff] [blame] | 165 | for (const auto &J : Sample.getSortedCallTargets()) |
| 166 | OS << " " << J.first << ":" << J.second; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 167 | OS << "\n"; |
| 168 | } |
| 169 | |
Dehao Chen | 2c7ca9b | 2017-04-13 19:52:10 +0000 | [diff] [blame] | 170 | SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples( |
Diego Novillo | ef548d2 | 2015-11-19 15:33:08 +0000 | [diff] [blame] | 171 | S.getCallsiteSamples()); |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 172 | Indent += 1; |
Dehao Chen | 2c7ca9b | 2017-04-13 19:52:10 +0000 | [diff] [blame] | 173 | for (const auto &I : SortedCallsiteSamples.get()) |
| 174 | for (const auto &FS : I->second) { |
| 175 | LineLocation Loc = I->first; |
| 176 | const FunctionSamples &CalleeSamples = FS.second; |
| 177 | OS.indent(Indent); |
| 178 | if (Loc.Discriminator == 0) |
| 179 | OS << Loc.LineOffset << ": "; |
| 180 | else |
| 181 | OS << Loc.LineOffset << "." << Loc.Discriminator << ": "; |
Wei Mi | be90732 | 2019-08-23 19:05:30 +0000 | [diff] [blame] | 182 | if (std::error_code EC = writeSample(CalleeSamples)) |
Dehao Chen | 2c7ca9b | 2017-04-13 19:52:10 +0000 | [diff] [blame] | 183 | return EC; |
| 184 | } |
Diego Novillo | aae1ed8 | 2015-10-08 19:40:37 +0000 | [diff] [blame] | 185 | Indent -= 1; |
| 186 | |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 187 | return sampleprof_error::success; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 190 | std::error_code SampleProfileWriterBinary::writeNameIdx(StringRef FName) { |
| 191 | const auto &ret = NameTable.find(FName); |
| 192 | if (ret == NameTable.end()) |
| 193 | return sampleprof_error::truncated_name_table; |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 194 | encodeULEB128(ret->second, *OutputStream); |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 195 | return sampleprof_error::success; |
| 196 | } |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 197 | |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 198 | void SampleProfileWriterBinary::addName(StringRef FName) { |
Dehao Chen | 8d1c983 | 2017-05-11 23:43:44 +0000 | [diff] [blame] | 199 | NameTable.insert(std::make_pair(FName, 0)); |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void SampleProfileWriterBinary::addNames(const FunctionSamples &S) { |
| 203 | // Add all the names in indirect call targets. |
| 204 | for (const auto &I : S.getBodySamples()) { |
| 205 | const SampleRecord &Sample = I.second; |
| 206 | for (const auto &J : Sample.getCallTargets()) |
| 207 | addName(J.first()); |
| 208 | } |
| 209 | |
| 210 | // Recursively add all the names for inlined callsites. |
Dehao Chen | 2c7ca9b | 2017-04-13 19:52:10 +0000 | [diff] [blame] | 211 | for (const auto &J : S.getCallsiteSamples()) |
| 212 | for (const auto &FS : J.second) { |
| 213 | const FunctionSamples &CalleeSamples = FS.second; |
| 214 | addName(CalleeSamples.getName()); |
| 215 | addNames(CalleeSamples); |
| 216 | } |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Wei Mi | a0c0857 | 2018-06-11 22:40:43 +0000 | [diff] [blame] | 219 | void SampleProfileWriterBinary::stablizeNameTable(std::set<StringRef> &V) { |
| 220 | // Sort the names to make NameTable deterministic. |
| 221 | for (const auto &I : NameTable) |
| 222 | V.insert(I.first); |
| 223 | int i = 0; |
| 224 | for (const StringRef &N : V) |
| 225 | NameTable[N] = i++; |
| 226 | } |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 227 | |
Wei Mi | be90732 | 2019-08-23 19:05:30 +0000 | [diff] [blame] | 228 | std::error_code SampleProfileWriterBinary::writeNameTable() { |
Wei Mi | a0c0857 | 2018-06-11 22:40:43 +0000 | [diff] [blame] | 229 | auto &OS = *OutputStream; |
| 230 | std::set<StringRef> V; |
| 231 | stablizeNameTable(V); |
| 232 | |
| 233 | // Write out the name table. |
| 234 | encodeULEB128(NameTable.size(), OS); |
| 235 | for (auto N : V) { |
| 236 | OS << N; |
| 237 | encodeULEB128(0, OS); |
| 238 | } |
| 239 | return sampleprof_error::success; |
| 240 | } |
| 241 | |
Wei Mi | 6a14325 | 2018-09-14 20:52:59 +0000 | [diff] [blame] | 242 | std::error_code SampleProfileWriterCompactBinary::writeFuncOffsetTable() { |
| 243 | auto &OS = *OutputStream; |
| 244 | |
| 245 | // Fill the slot remembered by TableOffset with the offset of FuncOffsetTable. |
| 246 | auto &OFS = static_cast<raw_fd_ostream &>(OS); |
| 247 | uint64_t FuncOffsetTableStart = OS.tell(); |
| 248 | if (OFS.seek(TableOffset) == (uint64_t)-1) |
| 249 | return sampleprof_error::ostream_seek_unsupported; |
| 250 | support::endian::Writer Writer(*OutputStream, support::little); |
| 251 | Writer.write(FuncOffsetTableStart); |
| 252 | if (OFS.seek(FuncOffsetTableStart) == (uint64_t)-1) |
| 253 | return sampleprof_error::ostream_seek_unsupported; |
| 254 | |
| 255 | // Write out the table size. |
| 256 | encodeULEB128(FuncOffsetTable.size(), OS); |
| 257 | |
| 258 | // Write out FuncOffsetTable. |
| 259 | for (auto entry : FuncOffsetTable) { |
| 260 | writeNameIdx(entry.first); |
| 261 | encodeULEB128(entry.second, OS); |
| 262 | } |
| 263 | return sampleprof_error::success; |
| 264 | } |
| 265 | |
Wei Mi | a0c0857 | 2018-06-11 22:40:43 +0000 | [diff] [blame] | 266 | std::error_code SampleProfileWriterCompactBinary::writeNameTable() { |
| 267 | auto &OS = *OutputStream; |
| 268 | std::set<StringRef> V; |
| 269 | stablizeNameTable(V); |
| 270 | |
| 271 | // Write out the name table. |
| 272 | encodeULEB128(NameTable.size(), OS); |
| 273 | for (auto N : V) { |
| 274 | encodeULEB128(MD5Hash(N), OS); |
| 275 | } |
| 276 | return sampleprof_error::success; |
| 277 | } |
| 278 | |
Wei Mi | be90732 | 2019-08-23 19:05:30 +0000 | [diff] [blame] | 279 | std::error_code |
| 280 | SampleProfileWriterBinary::writeMagicIdent(SampleProfileFormat Format) { |
Wei Mi | a0c0857 | 2018-06-11 22:40:43 +0000 | [diff] [blame] | 281 | auto &OS = *OutputStream; |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 282 | // Write file magic identifier. |
Wei Mi | be90732 | 2019-08-23 19:05:30 +0000 | [diff] [blame] | 283 | encodeULEB128(SPMagic(Format), OS); |
Wei Mi | a0c0857 | 2018-06-11 22:40:43 +0000 | [diff] [blame] | 284 | encodeULEB128(SPVersion(), OS); |
| 285 | return sampleprof_error::success; |
| 286 | } |
| 287 | |
| 288 | std::error_code SampleProfileWriterBinary::writeHeader( |
| 289 | const StringMap<FunctionSamples> &ProfileMap) { |
Wei Mi | be90732 | 2019-08-23 19:05:30 +0000 | [diff] [blame] | 290 | writeMagicIdent(Format); |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 291 | |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 292 | computeSummary(ProfileMap); |
| 293 | if (auto EC = writeSummary()) |
| 294 | return EC; |
| 295 | |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 296 | // Generate the name table for all the functions referenced in the profile. |
| 297 | for (const auto &I : ProfileMap) { |
| 298 | addName(I.first()); |
| 299 | addNames(I.second); |
| 300 | } |
| 301 | |
Wei Mi | a0c0857 | 2018-06-11 22:40:43 +0000 | [diff] [blame] | 302 | writeNameTable(); |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 303 | return sampleprof_error::success; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Wei Mi | be90732 | 2019-08-23 19:05:30 +0000 | [diff] [blame] | 306 | void SampleProfileWriterExtBinaryBase::allocSecHdrTable() { |
| 307 | support::endian::Writer Writer(*OutputStream, support::little); |
| 308 | |
| 309 | Writer.write(static_cast<uint64_t>(SectionLayout.size())); |
| 310 | SecHdrTableOffset = OutputStream->tell(); |
| 311 | for (uint32_t i = 0; i < SectionLayout.size(); i++) { |
| 312 | Writer.write(static_cast<uint64_t>(-1)); |
| 313 | Writer.write(static_cast<uint64_t>(-1)); |
| 314 | Writer.write(static_cast<uint64_t>(-1)); |
| 315 | Writer.write(static_cast<uint64_t>(-1)); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | std::error_code SampleProfileWriterExtBinaryBase::writeSecHdrTable() { |
| 320 | auto &OFS = static_cast<raw_fd_ostream &>(*OutputStream); |
| 321 | uint64_t Saved = OutputStream->tell(); |
| 322 | |
| 323 | // Set OutputStream to the location saved in SecHdrTableOffset. |
| 324 | if (OFS.seek(SecHdrTableOffset) == (uint64_t)-1) |
| 325 | return sampleprof_error::ostream_seek_unsupported; |
| 326 | support::endian::Writer Writer(*OutputStream, support::little); |
| 327 | |
| 328 | DenseMap<uint32_t, uint32_t> IndexMap; |
| 329 | for (uint32_t i = 0; i < SecHdrTable.size(); i++) { |
| 330 | IndexMap.insert({static_cast<uint32_t>(SecHdrTable[i].Type), i}); |
| 331 | } |
| 332 | |
| 333 | // Write the sections in the order specified in SectionLayout. |
| 334 | // That is the sections order Reader will see. Note that the |
| 335 | // sections order in which Reader expects to read may be different |
| 336 | // from the order in which Writer is able to write, so we need |
| 337 | // to adjust the order in SecHdrTable to be consistent with |
| 338 | // SectionLayout when we write SecHdrTable to the memory. |
| 339 | for (uint32_t i = 0; i < SectionLayout.size(); i++) { |
| 340 | uint32_t idx = IndexMap[static_cast<uint32_t>(SectionLayout[i])]; |
| 341 | Writer.write(static_cast<uint64_t>(SecHdrTable[idx].Type)); |
| 342 | Writer.write(static_cast<uint64_t>(SecHdrTable[idx].Flag)); |
| 343 | Writer.write(static_cast<uint64_t>(SecHdrTable[idx].Offset)); |
| 344 | Writer.write(static_cast<uint64_t>(SecHdrTable[idx].Size)); |
| 345 | } |
| 346 | |
| 347 | // Reset OutputStream. |
| 348 | if (OFS.seek(Saved) == (uint64_t)-1) |
| 349 | return sampleprof_error::ostream_seek_unsupported; |
| 350 | |
| 351 | return sampleprof_error::success; |
| 352 | } |
| 353 | |
| 354 | std::error_code SampleProfileWriterExtBinaryBase::writeHeader( |
| 355 | const StringMap<FunctionSamples> &ProfileMap) { |
| 356 | auto &OS = *OutputStream; |
| 357 | FileStart = OS.tell(); |
| 358 | writeMagicIdent(Format); |
| 359 | |
| 360 | initSectionLayout(); |
| 361 | allocSecHdrTable(); |
| 362 | return sampleprof_error::success; |
| 363 | } |
| 364 | |
Wei Mi | 6a14325 | 2018-09-14 20:52:59 +0000 | [diff] [blame] | 365 | std::error_code SampleProfileWriterCompactBinary::writeHeader( |
| 366 | const StringMap<FunctionSamples> &ProfileMap) { |
| 367 | support::endian::Writer Writer(*OutputStream, support::little); |
| 368 | if (auto EC = SampleProfileWriterBinary::writeHeader(ProfileMap)) |
| 369 | return EC; |
| 370 | |
| 371 | // Reserve a slot for the offset of function offset table. The slot will |
| 372 | // be populated with the offset of FuncOffsetTable later. |
| 373 | TableOffset = OutputStream->tell(); |
| 374 | Writer.write(static_cast<uint64_t>(-2)); |
| 375 | return sampleprof_error::success; |
| 376 | } |
| 377 | |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 378 | std::error_code SampleProfileWriterBinary::writeSummary() { |
| 379 | auto &OS = *OutputStream; |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 380 | encodeULEB128(Summary->getTotalCount(), OS); |
| 381 | encodeULEB128(Summary->getMaxCount(), OS); |
Easwaran Raman | 6f4903d | 2016-03-28 23:14:29 +0000 | [diff] [blame] | 382 | encodeULEB128(Summary->getMaxFunctionCount(), OS); |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 383 | encodeULEB128(Summary->getNumCounts(), OS); |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 384 | encodeULEB128(Summary->getNumFunctions(), OS); |
| 385 | std::vector<ProfileSummaryEntry> &Entries = Summary->getDetailedSummary(); |
| 386 | encodeULEB128(Entries.size(), OS); |
| 387 | for (auto Entry : Entries) { |
| 388 | encodeULEB128(Entry.Cutoff, OS); |
| 389 | encodeULEB128(Entry.MinCount, OS); |
| 390 | encodeULEB128(Entry.NumCounts, OS); |
| 391 | } |
| 392 | return sampleprof_error::success; |
| 393 | } |
Dehao Chen | 57d1dda | 2016-03-03 18:09:32 +0000 | [diff] [blame] | 394 | std::error_code SampleProfileWriterBinary::writeBody(const FunctionSamples &S) { |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 395 | auto &OS = *OutputStream; |
| 396 | |
Dehao Chen | 57d1dda | 2016-03-03 18:09:32 +0000 | [diff] [blame] | 397 | if (std::error_code EC = writeNameIdx(S.getName())) |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 398 | return EC; |
| 399 | |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 400 | encodeULEB128(S.getTotalSamples(), OS); |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 401 | |
| 402 | // Emit all the body samples. |
Diego Novillo | b93483d | 2015-10-16 18:54:35 +0000 | [diff] [blame] | 403 | encodeULEB128(S.getBodySamples().size(), OS); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 404 | for (const auto &I : S.getBodySamples()) { |
| 405 | LineLocation Loc = I.first; |
| 406 | const SampleRecord &Sample = I.second; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 407 | encodeULEB128(Loc.LineOffset, OS); |
| 408 | encodeULEB128(Loc.Discriminator, OS); |
| 409 | encodeULEB128(Sample.getSamples(), OS); |
| 410 | encodeULEB128(Sample.getCallTargets().size(), OS); |
Wenlei He | 5adace3 | 2019-08-20 20:52:00 +0000 | [diff] [blame] | 411 | for (const auto &J : Sample.getSortedCallTargets()) { |
| 412 | StringRef Callee = J.first; |
Diego Novillo | 38be333 | 2015-10-15 16:36:21 +0000 | [diff] [blame] | 413 | uint64_t CalleeSamples = J.second; |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 414 | if (std::error_code EC = writeNameIdx(Callee)) |
| 415 | return EC; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 416 | encodeULEB128(CalleeSamples, OS); |
| 417 | } |
| 418 | } |
| 419 | |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 420 | // Recursively emit all the callsite samples. |
Dehao Chen | 2c27daf | 2017-08-03 00:09:18 +0000 | [diff] [blame] | 421 | uint64_t NumCallsites = 0; |
| 422 | for (const auto &J : S.getCallsiteSamples()) |
| 423 | NumCallsites += J.second.size(); |
| 424 | encodeULEB128(NumCallsites, OS); |
Dehao Chen | 2c7ca9b | 2017-04-13 19:52:10 +0000 | [diff] [blame] | 425 | for (const auto &J : S.getCallsiteSamples()) |
| 426 | for (const auto &FS : J.second) { |
| 427 | LineLocation Loc = J.first; |
| 428 | const FunctionSamples &CalleeSamples = FS.second; |
| 429 | encodeULEB128(Loc.LineOffset, OS); |
| 430 | encodeULEB128(Loc.Discriminator, OS); |
| 431 | if (std::error_code EC = writeBody(CalleeSamples)) |
| 432 | return EC; |
| 433 | } |
Diego Novillo | a7f1e8e | 2015-10-09 17:54:24 +0000 | [diff] [blame] | 434 | |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 435 | return sampleprof_error::success; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 436 | } |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 437 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 438 | /// Write samples of a top-level function to a binary file. |
Diego Novillo | b93483d | 2015-10-16 18:54:35 +0000 | [diff] [blame] | 439 | /// |
| 440 | /// \returns true if the samples were written successfully, false otherwise. |
Wei Mi | be90732 | 2019-08-23 19:05:30 +0000 | [diff] [blame] | 441 | std::error_code |
| 442 | SampleProfileWriterBinary::writeSample(const FunctionSamples &S) { |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 443 | encodeULEB128(S.getHeadSamples(), *OutputStream); |
Dehao Chen | 57d1dda | 2016-03-03 18:09:32 +0000 | [diff] [blame] | 444 | return writeBody(S); |
Diego Novillo | b93483d | 2015-10-16 18:54:35 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Wei Mi | 6a14325 | 2018-09-14 20:52:59 +0000 | [diff] [blame] | 447 | std::error_code |
Wei Mi | be90732 | 2019-08-23 19:05:30 +0000 | [diff] [blame] | 448 | SampleProfileWriterCompactBinary::writeSample(const FunctionSamples &S) { |
Wei Mi | 6a14325 | 2018-09-14 20:52:59 +0000 | [diff] [blame] | 449 | uint64_t Offset = OutputStream->tell(); |
| 450 | StringRef Name = S.getName(); |
| 451 | FuncOffsetTable[Name] = Offset; |
| 452 | encodeULEB128(S.getHeadSamples(), *OutputStream); |
| 453 | return writeBody(S); |
| 454 | } |
| 455 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 456 | /// Create a sample profile file writer based on the specified format. |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 457 | /// |
| 458 | /// \param Filename The file to create. |
| 459 | /// |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 460 | /// \param Format Encoding format for the profile file. |
| 461 | /// |
| 462 | /// \returns an error code indicating the status of the created writer. |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 463 | ErrorOr<std::unique_ptr<SampleProfileWriter>> |
| 464 | SampleProfileWriter::create(StringRef Filename, SampleProfileFormat Format) { |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 465 | std::error_code EC; |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 466 | std::unique_ptr<raw_ostream> OS; |
Wei Mi | be90732 | 2019-08-23 19:05:30 +0000 | [diff] [blame] | 467 | if (Format == SPF_Binary || Format == SPF_Ext_Binary || |
| 468 | Format == SPF_Compact_Binary) |
Fangrui Song | d9b948b | 2019-08-05 05:43:48 +0000 | [diff] [blame] | 469 | OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::OF_None)); |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 470 | else |
Fangrui Song | d9b948b | 2019-08-05 05:43:48 +0000 | [diff] [blame] | 471 | OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::OF_Text)); |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 472 | if (EC) |
| 473 | return EC; |
| 474 | |
| 475 | return create(OS, Format); |
| 476 | } |
| 477 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 478 | /// Create a sample profile stream writer based on the specified format. |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 479 | /// |
| 480 | /// \param OS The output stream to store the profile data to. |
| 481 | /// |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 482 | /// \param Format Encoding format for the profile file. |
| 483 | /// |
| 484 | /// \returns an error code indicating the status of the created writer. |
| 485 | ErrorOr<std::unique_ptr<SampleProfileWriter>> |
| 486 | SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS, |
| 487 | SampleProfileFormat Format) { |
| 488 | std::error_code EC; |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 489 | std::unique_ptr<SampleProfileWriter> Writer; |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 490 | |
Wei Mi | d9be2c7 | 2018-06-12 05:53:49 +0000 | [diff] [blame] | 491 | if (Format == SPF_Binary) |
Wei Mi | a0c0857 | 2018-06-11 22:40:43 +0000 | [diff] [blame] | 492 | Writer.reset(new SampleProfileWriterRawBinary(OS)); |
Wei Mi | be90732 | 2019-08-23 19:05:30 +0000 | [diff] [blame] | 493 | else if (Format == SPF_Ext_Binary) |
| 494 | Writer.reset(new SampleProfileWriterExtBinary(OS)); |
Wei Mi | a0c0857 | 2018-06-11 22:40:43 +0000 | [diff] [blame] | 495 | else if (Format == SPF_Compact_Binary) |
| 496 | Writer.reset(new SampleProfileWriterCompactBinary(OS)); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 497 | else if (Format == SPF_Text) |
Nathan Slingerland | 51abea7 | 2015-12-10 17:21:42 +0000 | [diff] [blame] | 498 | Writer.reset(new SampleProfileWriterText(OS)); |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 499 | else if (Format == SPF_GCC) |
| 500 | EC = sampleprof_error::unsupported_writing_format; |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 501 | else |
| 502 | EC = sampleprof_error::unrecognized_format; |
| 503 | |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 504 | if (EC) |
| 505 | return EC; |
| 506 | |
Wei Mi | be90732 | 2019-08-23 19:05:30 +0000 | [diff] [blame] | 507 | Writer->Format = Format; |
Diego Novillo | fcd5560 | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 508 | return std::move(Writer); |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 509 | } |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 510 | |
| 511 | void SampleProfileWriter::computeSummary( |
| 512 | const StringMap<FunctionSamples> &ProfileMap) { |
Easwaran Raman | e5a17e3 | 2016-05-19 21:07:12 +0000 | [diff] [blame] | 513 | SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs); |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 514 | for (const auto &I : ProfileMap) { |
| 515 | const FunctionSamples &Profile = I.second; |
Easwaran Raman | e5a17e3 | 2016-05-19 21:07:12 +0000 | [diff] [blame] | 516 | Builder.addRecord(Profile); |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 517 | } |
Benjamin Kramer | 38de59e | 2016-05-20 09:18:37 +0000 | [diff] [blame] | 518 | Summary = Builder.getSummary(); |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 519 | } |