Path: blob/main/contrib/llvm-project/llvm/lib/ProfileData/MemProfCommon.cpp
213765 views
//=-- MemProfCommon.cpp - MemProf common utilities ---------------=//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 common utilities.9//10//===----------------------------------------------------------------------===//1112#include "llvm/ProfileData/MemProfCommon.h"13#include "llvm/ProfileData/MemProf.h"14#include "llvm/Support/BLAKE3.h"15#include "llvm/Support/CommandLine.h"16#include "llvm/Support/Compiler.h"17#include "llvm/Support/HashBuilder.h"1819using namespace llvm;20using namespace llvm::memprof;2122// Upper bound on lifetime access density (accesses per byte per lifetime sec)23// for marking an allocation cold.24LLVM_ABI cl::opt<float> MemProfLifetimeAccessDensityColdThreshold(25"memprof-lifetime-access-density-cold-threshold", cl::init(0.05),26cl::Hidden,27cl::desc("The threshold the lifetime access density (accesses per byte per "28"lifetime sec) must be under to consider an allocation cold"));2930// Lower bound on lifetime to mark an allocation cold (in addition to accesses31// per byte per sec above). This is to avoid pessimizing short lived objects.32LLVM_ABI cl::opt<unsigned> MemProfAveLifetimeColdThreshold(33"memprof-ave-lifetime-cold-threshold", cl::init(200), cl::Hidden,34cl::desc("The average lifetime (s) for an allocation to be considered "35"cold"));3637// Lower bound on average lifetime accesses density (total life time access38// density / alloc count) for marking an allocation hot.39LLVM_ABI cl::opt<unsigned> MemProfMinAveLifetimeAccessDensityHotThreshold(40"memprof-min-ave-lifetime-access-density-hot-threshold", cl::init(1000),41cl::Hidden,42cl::desc("The minimum TotalLifetimeAccessDensity / AllocCount for an "43"allocation to be considered hot"));4445LLVM_ABI cl::opt<bool>46MemProfUseHotHints("memprof-use-hot-hints", cl::init(false), cl::Hidden,47cl::desc("Enable use of hot hints (only supported for "48"unambigously hot allocations)"));4950AllocationType llvm::memprof::getAllocType(uint64_t TotalLifetimeAccessDensity,51uint64_t AllocCount,52uint64_t TotalLifetime) {53// The access densities are multiplied by 100 to hold 2 decimal places of54// precision, so need to divide by 100.55if (((float)TotalLifetimeAccessDensity) / AllocCount / 100 <56MemProfLifetimeAccessDensityColdThreshold57// Lifetime is expected to be in ms, so convert the threshold to ms.58&& ((float)TotalLifetime) / AllocCount >=59MemProfAveLifetimeColdThreshold * 1000)60return AllocationType::Cold;6162// The access densities are multiplied by 100 to hold 2 decimal places of63// precision, so need to divide by 100.64if (MemProfUseHotHints &&65((float)TotalLifetimeAccessDensity) / AllocCount / 100 >66MemProfMinAveLifetimeAccessDensityHotThreshold)67return AllocationType::Hot;6869return AllocationType::NotCold;70}7172uint64_t llvm::memprof::computeFullStackId(ArrayRef<Frame> CallStack) {73llvm::HashBuilder<llvm::TruncatedBLAKE3<8>, llvm::endianness::little>74HashBuilder;75for (auto &F : CallStack)76HashBuilder.add(F.Function, F.LineOffset, F.Column);77llvm::BLAKE3Result<8> Hash = HashBuilder.final();78uint64_t Id;79std::memcpy(&Id, Hash.data(), sizeof(Hash));80return Id;81}828384