Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 1 | //=-- SampleProf.cpp - Sample profiling format support --------------------===// |
| 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 contains common definitions used in the reading and writing of |
| 10 | // sample profile data. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/ProfileData/SampleProf.h" |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 15 | #include "llvm/Config/llvm-config.h" |
| 16 | #include "llvm/IR/DebugInfoMetadata.h" |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Compiler.h" |
Wei Mi | 798e59b | 2019-08-31 02:27:26 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Compression.h" |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
Wei Mi | 798e59b | 2019-08-31 02:27:26 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Error.h" |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ErrorHandling.h" |
Wei Mi | 798e59b | 2019-08-31 02:27:26 +0000 | [diff] [blame] | 22 | #include "llvm/Support/LEB128.h" |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ManagedStatic.h" |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | #include <string> |
| 26 | #include <system_error> |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace llvm; |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 29 | using namespace sampleprof; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 30 | |
Wei Mi | 94d44c9 | 2018-09-06 22:03:37 +0000 | [diff] [blame] | 31 | namespace llvm { |
| 32 | namespace sampleprof { |
| 33 | SampleProfileFormat FunctionSamples::Format; |
Wei Mi | 94d44c9 | 2018-09-06 22:03:37 +0000 | [diff] [blame] | 34 | } // namespace sampleprof |
| 35 | } // namespace llvm |
| 36 | |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 37 | namespace { |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 38 | |
Peter Collingbourne | 4718f8b | 2016-05-24 20:13:46 +0000 | [diff] [blame] | 39 | // FIXME: This class is only here to support the transition to llvm::Error. It |
| 40 | // will be removed once this transition is complete. Clients should prefer to |
| 41 | // deal with the Error value directly, rather than converting to error_code. |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 42 | class SampleProfErrorCategoryType : public std::error_category { |
Reid Kleckner | 990504e | 2016-10-19 23:52:38 +0000 | [diff] [blame] | 43 | const char *name() const noexcept override { return "llvm.sampleprof"; } |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 44 | |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 45 | std::string message(int IE) const override { |
| 46 | sampleprof_error E = static_cast<sampleprof_error>(IE); |
| 47 | switch (E) { |
| 48 | case sampleprof_error::success: |
| 49 | return "Success"; |
| 50 | case sampleprof_error::bad_magic: |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 51 | return "Invalid sample profile data (bad magic)"; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 52 | case sampleprof_error::unsupported_version: |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 53 | return "Unsupported sample profile format version"; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 54 | case sampleprof_error::too_large: |
| 55 | return "Too much profile data"; |
| 56 | case sampleprof_error::truncated: |
| 57 | return "Truncated profile data"; |
| 58 | case sampleprof_error::malformed: |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 59 | return "Malformed sample profile data"; |
Diego Novillo | d5336ae | 2014-11-01 00:56:55 +0000 | [diff] [blame] | 60 | case sampleprof_error::unrecognized_format: |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 61 | return "Unrecognized sample profile encoding format"; |
Diego Novillo | 760c5a8 | 2015-10-13 22:48:46 +0000 | [diff] [blame] | 62 | case sampleprof_error::unsupported_writing_format: |
| 63 | return "Profile encoding format unsupported for writing operations"; |
| 64 | case sampleprof_error::truncated_name_table: |
| 65 | return "Truncated function name table"; |
Diego Novillo | 3376a78 | 2015-09-17 00:17:24 +0000 | [diff] [blame] | 66 | case sampleprof_error::not_implemented: |
| 67 | return "Unimplemented feature"; |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 68 | case sampleprof_error::counter_overflow: |
| 69 | return "Counter overflow"; |
Wei Mi | 6a14325 | 2018-09-14 20:52:59 +0000 | [diff] [blame] | 70 | case sampleprof_error::ostream_seek_unsupported: |
| 71 | return "Ostream does not support seek"; |
Wei Mi | 798e59b | 2019-08-31 02:27:26 +0000 | [diff] [blame] | 72 | case sampleprof_error::compress_failed: |
| 73 | return "Compress failure"; |
| 74 | case sampleprof_error::uncompress_failed: |
| 75 | return "Uncompress failure"; |
| 76 | case sampleprof_error::zlib_unavailable: |
| 77 | return "Zlib is unavailable"; |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 78 | } |
| 79 | llvm_unreachable("A value of sampleprof_error has no message."); |
| 80 | } |
| 81 | }; |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 82 | |
| 83 | } // end anonymous namespace |
Diego Novillo | c572e92 | 2014-10-30 18:00:06 +0000 | [diff] [blame] | 84 | |
| 85 | static ManagedStatic<SampleProfErrorCategoryType> ErrorCategory; |
| 86 | |
| 87 | const std::error_category &llvm::sampleprof_category() { |
| 88 | return *ErrorCategory; |
| 89 | } |
Diego Novillo | 4b6bdb5 | 2015-11-12 17:58:14 +0000 | [diff] [blame] | 90 | |
Diego Novillo | ba920be | 2015-11-17 19:04:46 +0000 | [diff] [blame] | 91 | void LineLocation::print(raw_ostream &OS) const { |
| 92 | OS << LineOffset; |
| 93 | if (Discriminator > 0) |
| 94 | OS << "." << Discriminator; |
| 95 | } |
| 96 | |
| 97 | raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS, |
| 98 | const LineLocation &Loc) { |
| 99 | Loc.print(OS); |
| 100 | return OS; |
| 101 | } |
| 102 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 103 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Yaron Keren | eb2a254 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 104 | LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); } |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 105 | #endif |
Diego Novillo | ba920be | 2015-11-17 19:04:46 +0000 | [diff] [blame] | 106 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 107 | /// Print the sample record to the stream \p OS indented by \p Indent. |
Diego Novillo | 8e415a8 | 2015-11-13 20:24:28 +0000 | [diff] [blame] | 108 | void SampleRecord::print(raw_ostream &OS, unsigned Indent) const { |
| 109 | OS << NumSamples; |
| 110 | if (hasCalls()) { |
| 111 | OS << ", calls:"; |
Wenlei He | 5adace3 | 2019-08-20 20:52:00 +0000 | [diff] [blame] | 112 | for (const auto &I : getSortedCallTargets()) |
| 113 | OS << " " << I.first << ":" << I.second; |
Diego Novillo | 8e415a8 | 2015-11-13 20:24:28 +0000 | [diff] [blame] | 114 | } |
| 115 | OS << "\n"; |
| 116 | } |
| 117 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 118 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Yaron Keren | eb2a254 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 119 | LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); } |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 120 | #endif |
Diego Novillo | ba920be | 2015-11-17 19:04:46 +0000 | [diff] [blame] | 121 | |
| 122 | raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS, |
| 123 | const SampleRecord &Sample) { |
| 124 | Sample.print(OS, 0); |
| 125 | return OS; |
| 126 | } |
| 127 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 128 | /// Print the samples collected for a function on stream \p OS. |
Diego Novillo | 4b6bdb5 | 2015-11-12 17:58:14 +0000 | [diff] [blame] | 129 | void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const { |
| 130 | OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size() |
| 131 | << " sampled lines\n"; |
Diego Novillo | 8e415a8 | 2015-11-13 20:24:28 +0000 | [diff] [blame] | 132 | |
Diego Novillo | 379cc5e | 2015-11-19 22:18:30 +0000 | [diff] [blame] | 133 | OS.indent(Indent); |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 134 | if (!BodySamples.empty()) { |
Diego Novillo | 379cc5e | 2015-11-19 22:18:30 +0000 | [diff] [blame] | 135 | OS << "Samples collected in the function's body {\n"; |
| 136 | SampleSorter<LineLocation, SampleRecord> SortedBodySamples(BodySamples); |
| 137 | for (const auto &SI : SortedBodySamples.get()) { |
| 138 | OS.indent(Indent + 2); |
| 139 | OS << SI->first << ": " << SI->second; |
| 140 | } |
Diego Novillo | 4b6bdb5 | 2015-11-12 17:58:14 +0000 | [diff] [blame] | 141 | OS.indent(Indent); |
Diego Novillo | 379cc5e | 2015-11-19 22:18:30 +0000 | [diff] [blame] | 142 | OS << "}\n"; |
| 143 | } else { |
| 144 | OS << "No samples collected in the function's body\n"; |
Diego Novillo | 4b6bdb5 | 2015-11-12 17:58:14 +0000 | [diff] [blame] | 145 | } |
Diego Novillo | 8e415a8 | 2015-11-13 20:24:28 +0000 | [diff] [blame] | 146 | |
Diego Novillo | 379cc5e | 2015-11-19 22:18:30 +0000 | [diff] [blame] | 147 | OS.indent(Indent); |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 148 | if (!CallsiteSamples.empty()) { |
Diego Novillo | 379cc5e | 2015-11-19 22:18:30 +0000 | [diff] [blame] | 149 | OS << "Samples collected in inlined callsites {\n"; |
Dehao Chen | 2c7ca9b | 2017-04-13 19:52:10 +0000 | [diff] [blame] | 150 | SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples( |
Diego Novillo | 379cc5e | 2015-11-19 22:18:30 +0000 | [diff] [blame] | 151 | CallsiteSamples); |
| 152 | for (const auto &CS : SortedCallsiteSamples.get()) { |
Dehao Chen | 2c7ca9b | 2017-04-13 19:52:10 +0000 | [diff] [blame] | 153 | for (const auto &FS : CS->second) { |
| 154 | OS.indent(Indent + 2); |
| 155 | OS << CS->first << ": inlined callee: " << FS.second.getName() << ": "; |
| 156 | FS.second.print(OS, Indent + 4); |
| 157 | } |
Diego Novillo | 379cc5e | 2015-11-19 22:18:30 +0000 | [diff] [blame] | 158 | } |
| 159 | OS << "}\n"; |
| 160 | } else { |
| 161 | OS << "No inlined callsites in this function\n"; |
Diego Novillo | 4b6bdb5 | 2015-11-12 17:58:14 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
Diego Novillo | ba920be | 2015-11-17 19:04:46 +0000 | [diff] [blame] | 164 | |
| 165 | raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS, |
| 166 | const FunctionSamples &FS) { |
| 167 | FS.print(OS); |
| 168 | return OS; |
| 169 | } |
| 170 | |
Mircea Trofin | 5695097 | 2018-02-22 06:42:57 +0000 | [diff] [blame] | 171 | unsigned FunctionSamples::getOffset(const DILocation *DIL) { |
| 172 | return (DIL->getLine() - DIL->getScope()->getSubprogram()->getLine()) & |
| 173 | 0xffff; |
| 174 | } |
| 175 | |
| 176 | const FunctionSamples * |
| 177 | FunctionSamples::findFunctionSamples(const DILocation *DIL) const { |
| 178 | assert(DIL); |
| 179 | SmallVector<std::pair<LineLocation, StringRef>, 10> S; |
| 180 | |
| 181 | const DILocation *PrevDIL = DIL; |
| 182 | for (DIL = DIL->getInlinedAt(); DIL; DIL = DIL->getInlinedAt()) { |
| 183 | S.push_back(std::make_pair( |
| 184 | LineLocation(getOffset(DIL), DIL->getBaseDiscriminator()), |
| 185 | PrevDIL->getScope()->getSubprogram()->getLinkageName())); |
| 186 | PrevDIL = DIL; |
| 187 | } |
| 188 | if (S.size() == 0) |
| 189 | return this; |
| 190 | const FunctionSamples *FS = this; |
| 191 | for (int i = S.size() - 1; i >= 0 && FS != nullptr; i--) { |
| 192 | FS = FS->findFunctionSamplesAt(S[i].first, S[i].second); |
| 193 | } |
| 194 | return FS; |
| 195 | } |
| 196 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 197 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Eugene Zelenko | e78d131 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 198 | LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); } |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 199 | #endif |
Wei Mi | 798e59b | 2019-08-31 02:27:26 +0000 | [diff] [blame] | 200 | |
| 201 | std::error_code ProfileSymbolList::read(uint64_t CompressSize, |
| 202 | uint64_t UncompressSize, |
| 203 | const uint8_t *Data) { |
| 204 | const char *ListStart = reinterpret_cast<const char *>(Data); |
| 205 | // CompressSize being non-zero means the profile is compressed and |
| 206 | // needs to be uncompressed first. |
| 207 | if (CompressSize) { |
| 208 | if (!llvm::zlib::isAvailable()) |
| 209 | return sampleprof_error::zlib_unavailable; |
| 210 | |
| 211 | StringRef CompressedStrings(reinterpret_cast<const char *>(Data), |
| 212 | CompressSize); |
| 213 | char *Buffer = Allocator.Allocate<char>(UncompressSize); |
Wei Mi | 198009a | 2019-08-31 03:17:49 +0000 | [diff] [blame] | 214 | size_t UCSize = UncompressSize; |
| 215 | llvm::Error E = zlib::uncompress(CompressedStrings, Buffer, UCSize); |
Wei Mi | 798e59b | 2019-08-31 02:27:26 +0000 | [diff] [blame] | 216 | if (E) |
| 217 | return sampleprof_error::uncompress_failed; |
| 218 | ListStart = Buffer; |
| 219 | } |
| 220 | |
| 221 | uint64_t Size = 0; |
| 222 | while (Size < UncompressSize) { |
| 223 | StringRef Str(ListStart + Size); |
| 224 | add(Str); |
| 225 | Size += Str.size() + 1; |
| 226 | } |
| 227 | return sampleprof_error::success; |
| 228 | } |
| 229 | |
| 230 | std::error_code ProfileSymbolList::write(raw_ostream &OS) { |
| 231 | // Sort the symbols before doing compression. It will make the |
| 232 | // compression much more effective. |
| 233 | std::vector<StringRef> SortedList; |
| 234 | SortedList.insert(SortedList.begin(), Syms.begin(), Syms.end()); |
| 235 | llvm::sort(SortedList); |
| 236 | |
| 237 | std::string UncompressedStrings; |
| 238 | for (auto &Sym : SortedList) { |
| 239 | UncompressedStrings.append(Sym.str()); |
| 240 | UncompressedStrings.append(1, '\0'); |
| 241 | } |
| 242 | |
| 243 | if (ToCompress) { |
| 244 | if (!llvm::zlib::isAvailable()) |
| 245 | return sampleprof_error::zlib_unavailable; |
| 246 | SmallString<128> CompressedStrings; |
| 247 | llvm::Error E = zlib::compress(UncompressedStrings, CompressedStrings, |
| 248 | zlib::BestSizeCompression); |
| 249 | if (E) |
| 250 | return sampleprof_error::compress_failed; |
| 251 | encodeULEB128(UncompressedStrings.size(), OS); |
| 252 | encodeULEB128(CompressedStrings.size(), OS); |
| 253 | OS << CompressedStrings.str(); |
| 254 | } else { |
| 255 | encodeULEB128(UncompressedStrings.size(), OS); |
| 256 | // If profile symbol list is not compressed, we will still save |
| 257 | // a compressed size value, but the value of the size is 0. |
| 258 | encodeULEB128(0, OS); |
| 259 | OS << UncompressedStrings; |
| 260 | } |
| 261 | return sampleprof_error::success; |
| 262 | } |
| 263 | |
| 264 | void ProfileSymbolList::dump(raw_ostream &OS) const { |
| 265 | OS << "======== Dump profile symbol list ========\n"; |
| 266 | std::vector<StringRef> SortedList; |
| 267 | SortedList.insert(SortedList.begin(), Syms.begin(), Syms.end()); |
| 268 | llvm::sort(SortedList); |
| 269 | |
| 270 | for (auto &Sym : SortedList) |
| 271 | OS << Sym << "\n"; |
| 272 | } |