| 1 | //=-- MemProfSummary.cpp - MemProf summary support ---------------=// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file contains MemProf summary support. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/ProfileData/MemProfSummary.h" |
| 14 | |
| 15 | using namespace llvm; |
| 16 | using namespace llvm::memprof; |
| 17 | |
| 18 | void MemProfSummary::printSummaryYaml(raw_ostream &OS) const { |
| 19 | // For now emit as YAML comments, since they aren't read on input. |
| 20 | OS << "---\n" ; |
| 21 | OS << "# MemProfSummary:\n" ; |
| 22 | OS << "# Total contexts: " << NumContexts << "\n" ; |
| 23 | OS << "# Total cold contexts: " << NumColdContexts << "\n" ; |
| 24 | OS << "# Total hot contexts: " << NumHotContexts << "\n" ; |
| 25 | OS << "# Maximum cold context total size: " << MaxColdTotalSize << "\n" ; |
| 26 | OS << "# Maximum warm context total size: " << MaxWarmTotalSize << "\n" ; |
| 27 | OS << "# Maximum hot context total size: " << MaxHotTotalSize << "\n" ; |
| 28 | if (HasDataAccessProfile) { |
| 29 | OS << "# Num hot symbols and string literals: " |
| 30 | << NumHotSymbolsAndStringLiterals << "\n" ; |
| 31 | OS << "# Num known cold symbols: " << NumKnownColdSymbols << "\n" ; |
| 32 | OS << "# Num known cold string literals: " << NumKnownColdStringLiterals |
| 33 | << "\n" ; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | void MemProfSummary::write(ProfOStream &OS) const { |
| 38 | // Write the current number of fields first, which helps enable backwards and |
| 39 | // forwards compatibility (see comment in header). |
| 40 | OS.write32(V: memprof::MemProfSummary::getNumSummaryFields()); |
| 41 | auto StartPos = OS.tell(); |
| 42 | (void)StartPos; |
| 43 | OS.write(V: NumContexts); |
| 44 | OS.write(V: NumColdContexts); |
| 45 | OS.write(V: NumHotContexts); |
| 46 | OS.write(V: MaxColdTotalSize); |
| 47 | OS.write(V: MaxWarmTotalSize); |
| 48 | OS.write(V: MaxHotTotalSize); |
| 49 | // Sanity check that the number of fields was kept in sync with actual fields. |
| 50 | assert((OS.tell() - StartPos) / 8 == MemProfSummary::getNumSummaryFields()); |
| 51 | } |
| 52 | |
| 53 | std::unique_ptr<MemProfSummary> |
| 54 | MemProfSummary::deserialize(const unsigned char *&Ptr) { |
| 55 | auto NumSummaryFields = |
| 56 | support::endian::readNext<uint32_t, llvm::endianness::little>(memory&: Ptr); |
| 57 | // The initial version of the summary contains 6 fields. To support backwards |
| 58 | // compatibility with older profiles, if new summary fields are added (until a |
| 59 | // version bump) this code will need to check NumSummaryFields against the |
| 60 | // current value of MemProfSummary::getNumSummaryFields(). If NumSummaryFields |
| 61 | // is lower then default values will need to be filled in for the newer fields |
| 62 | // instead of trying to read them from the profile. |
| 63 | // |
| 64 | // For now, assert that the profile contains at least as many fields as |
| 65 | // expected by the code. |
| 66 | assert(NumSummaryFields >= MemProfSummary::getNumSummaryFields()); |
| 67 | |
| 68 | auto MemProfSum = std::make_unique<MemProfSummary>( |
| 69 | args: support::endian::read<uint64_t, llvm::endianness::little>(P: Ptr), |
| 70 | args: support::endian::read<uint64_t, llvm::endianness::little>(P: Ptr + 8), |
| 71 | args: support::endian::read<uint64_t, llvm::endianness::little>(P: Ptr + 16), |
| 72 | args: support::endian::read<uint64_t, llvm::endianness::little>(P: Ptr + 24), |
| 73 | args: support::endian::read<uint64_t, llvm::endianness::little>(P: Ptr + 32), |
| 74 | args: support::endian::read<uint64_t, llvm::endianness::little>(P: Ptr + 40)); |
| 75 | |
| 76 | // Enable forwards compatibility by skipping past any additional fields in the |
| 77 | // profile's summary. |
| 78 | Ptr += NumSummaryFields * sizeof(uint64_t); |
| 79 | |
| 80 | return MemProfSum; |
| 81 | } |
| 82 | |
| 83 | // FIXME: Consider to serialize the data access summary fields, ideally |
| 84 | // batch this together with more substantial profile format change |
| 85 | // and bump version once. |
| 86 | void MemProfSummary::buildDataAccessSummary( |
| 87 | const DataAccessProfData &DataAccessProfile) { |
| 88 | HasDataAccessProfile = true; |
| 89 | NumHotSymbolsAndStringLiterals = DataAccessProfile.getRecords().size(); |
| 90 | NumKnownColdSymbols = DataAccessProfile.getKnownColdSymbols().size(); |
| 91 | NumKnownColdStringLiterals = DataAccessProfile.getKnownColdHashes().size(); |
| 92 | } |
| 93 | |