Path: blob/main/contrib/llvm-project/llvm/lib/ProfileData/MemProfSummary.cpp
213766 views
//=-- MemProfSummary.cpp - MemProf summary support ---------------=//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file contains MemProf summary support.9//10//===----------------------------------------------------------------------===//1112#include "llvm/ProfileData/MemProfSummary.h"1314using namespace llvm;15using namespace llvm::memprof;1617void MemProfSummary::printSummaryYaml(raw_ostream &OS) const {18// For now emit as YAML comments, since they aren't read on input.19OS << "---\n";20OS << "# MemProfSummary:\n";21OS << "# Total contexts: " << NumContexts << "\n";22OS << "# Total cold contexts: " << NumColdContexts << "\n";23OS << "# Total hot contexts: " << NumHotContexts << "\n";24OS << "# Maximum cold context total size: " << MaxColdTotalSize << "\n";25OS << "# Maximum warm context total size: " << MaxWarmTotalSize << "\n";26OS << "# Maximum hot context total size: " << MaxHotTotalSize << "\n";27}2829void MemProfSummary::write(ProfOStream &OS) const {30// Write the current number of fields first, which helps enable backwards and31// forwards compatibility (see comment in header).32OS.write32(memprof::MemProfSummary::getNumSummaryFields());33auto StartPos = OS.tell();34(void)StartPos;35OS.write(NumContexts);36OS.write(NumColdContexts);37OS.write(NumHotContexts);38OS.write(MaxColdTotalSize);39OS.write(MaxWarmTotalSize);40OS.write(MaxHotTotalSize);41// Sanity check that the number of fields was kept in sync with actual fields.42assert((OS.tell() - StartPos) / 8 == MemProfSummary::getNumSummaryFields());43}4445std::unique_ptr<MemProfSummary>46MemProfSummary::deserialize(const unsigned char *&Ptr) {47auto NumSummaryFields =48support::endian::readNext<uint32_t, llvm::endianness::little>(Ptr);49// The initial version of the summary contains 6 fields. To support backwards50// compatibility with older profiles, if new summary fields are added (until a51// version bump) this code will need to check NumSummaryFields against the52// current value of MemProfSummary::getNumSummaryFields(). If NumSummaryFields53// is lower then default values will need to be filled in for the newer fields54// instead of trying to read them from the profile.55//56// For now, assert that the profile contains at least as many fields as57// expected by the code.58assert(NumSummaryFields >= MemProfSummary::getNumSummaryFields());5960auto MemProfSum = std::make_unique<MemProfSummary>(61support::endian::read<uint64_t, llvm::endianness::little>(Ptr),62support::endian::read<uint64_t, llvm::endianness::little>(Ptr + 8),63support::endian::read<uint64_t, llvm::endianness::little>(Ptr + 16),64support::endian::read<uint64_t, llvm::endianness::little>(Ptr + 24),65support::endian::read<uint64_t, llvm::endianness::little>(Ptr + 32),66support::endian::read<uint64_t, llvm::endianness::little>(Ptr + 40));6768// Enable forwards compatibility by skipping past any additional fields in the69// profile's summary.70Ptr += NumSummaryFields * sizeof(uint64_t);7172return MemProfSum;73}747576