xref: /freebsd/contrib/llvm-project/llvm/lib/ProfileData/MemProfSummaryBuilder.cpp (revision 700637cbb5e582861067a11aaca4d053546871d2)
1*700637cbSDimitry Andric //=-- MemProfSummaryBuilder.cpp - MemProf summary building ---------------=//
2*700637cbSDimitry Andric //
3*700637cbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*700637cbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*700637cbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*700637cbSDimitry Andric //
7*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
8*700637cbSDimitry Andric //
9*700637cbSDimitry Andric // This file contains MemProf summary builder.
10*700637cbSDimitry Andric //
11*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
12*700637cbSDimitry Andric 
13*700637cbSDimitry Andric #include "llvm/ProfileData/MemProfSummaryBuilder.h"
14*700637cbSDimitry Andric #include "llvm/ProfileData/MemProfCommon.h"
15*700637cbSDimitry Andric 
16*700637cbSDimitry Andric using namespace llvm;
17*700637cbSDimitry Andric using namespace llvm::memprof;
18*700637cbSDimitry Andric 
getSummary()19*700637cbSDimitry Andric std::unique_ptr<MemProfSummary> MemProfSummaryBuilder::getSummary() {
20*700637cbSDimitry Andric   return std::make_unique<MemProfSummary>(NumContexts, NumColdContexts,
21*700637cbSDimitry Andric                                           NumHotContexts, MaxColdTotalSize,
22*700637cbSDimitry Andric                                           MaxWarmTotalSize, MaxHotTotalSize);
23*700637cbSDimitry Andric }
24*700637cbSDimitry Andric 
addRecord(uint64_t CSId,const PortableMemInfoBlock & Info)25*700637cbSDimitry Andric void MemProfSummaryBuilder::addRecord(uint64_t CSId,
26*700637cbSDimitry Andric                                       const PortableMemInfoBlock &Info) {
27*700637cbSDimitry Andric   auto I = Contexts.insert(CSId);
28*700637cbSDimitry Andric   if (!I.second)
29*700637cbSDimitry Andric     return;
30*700637cbSDimitry Andric   NumContexts++;
31*700637cbSDimitry Andric   auto AllocType = getAllocType(Info.getTotalLifetimeAccessDensity(),
32*700637cbSDimitry Andric                                 Info.getAllocCount(), Info.getTotalLifetime());
33*700637cbSDimitry Andric   auto TotalSize = Info.getTotalSize();
34*700637cbSDimitry Andric   switch (AllocType) {
35*700637cbSDimitry Andric   case AllocationType::Cold:
36*700637cbSDimitry Andric     NumColdContexts++;
37*700637cbSDimitry Andric     if (TotalSize > MaxColdTotalSize)
38*700637cbSDimitry Andric       MaxColdTotalSize = TotalSize;
39*700637cbSDimitry Andric     break;
40*700637cbSDimitry Andric   case AllocationType::NotCold:
41*700637cbSDimitry Andric     if (TotalSize > MaxWarmTotalSize)
42*700637cbSDimitry Andric       MaxWarmTotalSize = TotalSize;
43*700637cbSDimitry Andric     break;
44*700637cbSDimitry Andric   case AllocationType::Hot:
45*700637cbSDimitry Andric     NumHotContexts++;
46*700637cbSDimitry Andric     if (TotalSize > MaxHotTotalSize)
47*700637cbSDimitry Andric       MaxHotTotalSize = TotalSize;
48*700637cbSDimitry Andric     break;
49*700637cbSDimitry Andric   default:
50*700637cbSDimitry Andric     assert(false);
51*700637cbSDimitry Andric   }
52*700637cbSDimitry Andric }
53*700637cbSDimitry Andric 
addRecord(const IndexedMemProfRecord & Record)54*700637cbSDimitry Andric void MemProfSummaryBuilder::addRecord(const IndexedMemProfRecord &Record) {
55*700637cbSDimitry Andric   for (auto &Alloc : Record.AllocSites)
56*700637cbSDimitry Andric     addRecord(Alloc.CSId, Alloc.Info);
57*700637cbSDimitry Andric }
58*700637cbSDimitry Andric 
addRecord(const MemProfRecord & Record)59*700637cbSDimitry Andric void MemProfSummaryBuilder::addRecord(const MemProfRecord &Record) {
60*700637cbSDimitry Andric   for (auto &Alloc : Record.AllocSites)
61*700637cbSDimitry Andric     addRecord(computeFullStackId(Alloc.CallStack), Alloc.Info);
62*700637cbSDimitry Andric }
63