|
| 1 | +//=-- MemProfCommon.cpp - MemProf common utilities ---------------=// |
| 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 common utilities. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "llvm/ProfileData/MemProfCommon.h" |
| 14 | +#include "llvm/ProfileData/MemProf.h" |
| 15 | +#include "llvm/Support/BLAKE3.h" |
| 16 | +#include "llvm/Support/CommandLine.h" |
| 17 | +#include "llvm/Support/HashBuilder.h" |
| 18 | + |
| 19 | +using namespace llvm; |
| 20 | +using namespace llvm::memprof; |
| 21 | + |
| 22 | +// Upper bound on lifetime access density (accesses per byte per lifetime sec) |
| 23 | +// for marking an allocation cold. |
| 24 | +cl::opt<float> MemProfLifetimeAccessDensityColdThreshold( |
| 25 | + "memprof-lifetime-access-density-cold-threshold", cl::init(0.05), |
| 26 | + cl::Hidden, |
| 27 | + cl::desc("The threshold the lifetime access density (accesses per byte per " |
| 28 | + "lifetime sec) must be under to consider an allocation cold")); |
| 29 | + |
| 30 | +// Lower bound on lifetime to mark an allocation cold (in addition to accesses |
| 31 | +// per byte per sec above). This is to avoid pessimizing short lived objects. |
| 32 | +cl::opt<unsigned> MemProfAveLifetimeColdThreshold( |
| 33 | + "memprof-ave-lifetime-cold-threshold", cl::init(200), cl::Hidden, |
| 34 | + cl::desc("The average lifetime (s) for an allocation to be considered " |
| 35 | + "cold")); |
| 36 | + |
| 37 | +// Lower bound on average lifetime accesses density (total life time access |
| 38 | +// density / alloc count) for marking an allocation hot. |
| 39 | +cl::opt<unsigned> MemProfMinAveLifetimeAccessDensityHotThreshold( |
| 40 | + "memprof-min-ave-lifetime-access-density-hot-threshold", cl::init(1000), |
| 41 | + cl::Hidden, |
| 42 | + cl::desc("The minimum TotalLifetimeAccessDensity / AllocCount for an " |
| 43 | + "allocation to be considered hot")); |
| 44 | + |
| 45 | +cl::opt<bool> |
| 46 | + MemProfUseHotHints("memprof-use-hot-hints", cl::init(false), cl::Hidden, |
| 47 | + cl::desc("Enable use of hot hints (only supported for " |
| 48 | + "unambigously hot allocations)")); |
| 49 | + |
| 50 | +AllocationType llvm::memprof::getAllocType(uint64_t TotalLifetimeAccessDensity, |
| 51 | + uint64_t AllocCount, |
| 52 | + uint64_t TotalLifetime) { |
| 53 | + // The access densities are multiplied by 100 to hold 2 decimal places of |
| 54 | + // precision, so need to divide by 100. |
| 55 | + if (((float)TotalLifetimeAccessDensity) / AllocCount / 100 < |
| 56 | + MemProfLifetimeAccessDensityColdThreshold |
| 57 | + // Lifetime is expected to be in ms, so convert the threshold to ms. |
| 58 | + && ((float)TotalLifetime) / AllocCount >= |
| 59 | + MemProfAveLifetimeColdThreshold * 1000) |
| 60 | + return AllocationType::Cold; |
| 61 | + |
| 62 | + // The access densities are multiplied by 100 to hold 2 decimal places of |
| 63 | + // precision, so need to divide by 100. |
| 64 | + if (MemProfUseHotHints && |
| 65 | + ((float)TotalLifetimeAccessDensity) / AllocCount / 100 > |
| 66 | + MemProfMinAveLifetimeAccessDensityHotThreshold) |
| 67 | + return AllocationType::Hot; |
| 68 | + |
| 69 | + return AllocationType::NotCold; |
| 70 | +} |
| 71 | + |
| 72 | +uint64_t llvm::memprof::computeFullStackId(ArrayRef<Frame> CallStack) { |
| 73 | + llvm::HashBuilder<llvm::TruncatedBLAKE3<8>, llvm::endianness::little> |
| 74 | + HashBuilder; |
| 75 | + for (auto &F : CallStack) |
| 76 | + HashBuilder.add(F.Function, F.LineOffset, F.Column); |
| 77 | + llvm::BLAKE3Result<8> Hash = HashBuilder.final(); |
| 78 | + uint64_t Id; |
| 79 | + std::memcpy(&Id, Hash.data(), sizeof(Hash)); |
| 80 | + return Id; |
| 81 | +} |
0 commit comments