ATLAS Offline Software
memory_hooks-common.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // common code for memory_hooks for tcmalloc and stdcmalloc which are used for the AthMemoryAuditor
6 // Rolf Seuster, Oct 2015
7 
8 #ifndef ATHENAAUDITORS_MEMORYHOOKSCOMMON_H
9 #define ATHENAAUDITORS_MEMORYHOOKSCOMMON_H
10 
11 #include <iostream>
12 #include <fstream>
13 #include <iomanip>
14 
15 // needed for implementing hooks into tcmalloc
16 #include "gperftools/malloc_hook.h"
17 
18 #include <cstdint>
19 #include <string.h>
20 #include <stdlib.h> // for getenv
21 
22 #include <boost/intrusive/splay_set.hpp>
23 
24 #define UNW_LOCAL_ONLY
25 
26 #if defined(__clang__)
27 # pragma clang diagnostic push
28 # if __has_warning("-Wextern-c-compat")
29 # pragma clang diagnostic ignored "-Wextern-c-compat"
30 # endif
31 #endif
32 #include "libunwind.h"
33 #if defined(__clang__)
34 # pragma clang diagnostic pop
35 #endif
36 
37 struct aiStruct
38 {
39  aiStruct() : index(0), stacktrace(0) {};
43 };
44 
45 static std::map<std::string, aiStruct> arrayAlgIndex;
46 static std::map<uint32_t, std::string> algorithms;
47 
48 static uint32_t curMaxIndex(0);
49 static uint32_t curIndex(0);
50 
51 static bool initialized(false);
52 static bool finished(false);
53 
54 static uintptr_t context(0);
55 static uintptr_t contextFirst(0);
56 static uint32_t current_stage(0);
57 static std::string defaultString = "N/A";
58 
59 static size_t stacktraceDepth(25);
60 
61 static bool collectStacktraces(true);
62 
63 //using namespace boost::intrusive;
64 
65 class myBlocks_tc : public boost::intrusive::bs_set_base_hook<boost::intrusive::optimize_size<false> >
66  {
67  public:
68  uintptr_t allocated;
69  uintptr_t allocsize;
72  std::vector<void*> allocatedFrom;
73 
74  public:
75  boost::intrusive::bs_set_member_hook<> member_hook_;
76 
78  : allocated(0)
79  , allocsize(0)
80  , algindex(0)
81  , curstage(0)
82  { };
83 
84  myBlocks_tc(uintptr_t mem, uintptr_t s, uint32_t algi, uint32_t cstage, size_t stDepth)
85  : allocated(mem)
86  , allocsize(s)
87  , algindex(algi)
88  , curstage(cstage)
89  , allocatedFrom(stDepth,nullptr) { };
90 
91  friend bool operator< ( const myBlocks_tc &a, const myBlocks_tc &b )
92  {
93  return a.allocated < b.allocated;
94  };
95 
96  friend bool operator> ( const myBlocks_tc &a, const myBlocks_tc &b )
97  {
98  return a.allocated > b.allocated;
99  };
100 
101  friend bool operator== ( const myBlocks_tc &a, const myBlocks_tc &b)
102  {
103  return a.allocated == b.allocated;
104  };
105 };
106 
108 
109 using allocSet_tc = boost::intrusive::splay_set< myBlocks_tc, boost::intrusive::base_hook<boost::intrusive::bs_set_base_hook<> > > ;
110 using allocSet_deleter = std::default_delete<myBlocks_tc>;
111 
112 static allocSet_tc::iterator allocset_last;
113 static allocSet_tc allocset_tc;
114 static allocSet_tc free_set_tc;
115 
116 #endif
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
aiStruct::index
uint16_t index
Definition: memory_hooks-common.h:40
bg_tc
myBlocks_tc * bg_tc
Definition: memory_hooks-common.h:107
aiStruct::aiStruct
aiStruct(uint16_t i, uint16_t s)
Definition: memory_hooks-common.h:40
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
index
Definition: index.py:1
aiStruct::stacktrace
uint16_t stacktrace
Definition: memory_hooks-common.h:42
myBlocks_tc::allocsize
uintptr_t allocsize
Definition: memory_hooks-common.h:69
myBlocks_tc::member_hook_
boost::intrusive::bs_set_member_hook member_hook_
Definition: memory_hooks-common.h:75
myBlocks_tc::allocated
uintptr_t allocated
Definition: memory_hooks-common.h:68
myBlocks_tc::operator==
friend bool operator==(const myBlocks_tc &a, const myBlocks_tc &b)
Definition: memory_hooks-common.h:101
allocSet_deleter
std::default_delete< myBlocks_tc > allocSet_deleter
Definition: memory_hooks-common.h:110
myBlocks_tc
Definition: memory_hooks-common.h:66
xAOD::uint16_t
setWord1 uint16_t
Definition: eFexEMRoI_v1.cxx:88
lumiFormat.i
int i
Definition: lumiFormat.py:92
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
myBlocks_tc::allocatedFrom
std::vector< void * > allocatedFrom
Definition: memory_hooks-common.h:72
myBlocks_tc::myBlocks_tc
myBlocks_tc()
Definition: memory_hooks-common.h:77
myBlocks_tc::operator<
friend bool operator<(const myBlocks_tc &a, const myBlocks_tc &b)
Definition: memory_hooks-common.h:91
allocSet_tc
boost::intrusive::splay_set< myBlocks_tc, boost::intrusive::base_hook< boost::intrusive::bs_set_base_hook<> > > allocSet_tc
Definition: memory_hooks-common.h:109
a
TList * a
Definition: liststreamerinfos.cxx:10
myBlocks_tc::operator>
friend bool operator>(const myBlocks_tc &a, const myBlocks_tc &b)
Definition: memory_hooks-common.h:96
aiStruct
Definition: memory_hooks-common.h:38
myBlocks_tc::algindex
uint32_t algindex
Definition: memory_hooks-common.h:70
myBlocks_tc::curstage
uint32_t curstage
Definition: memory_hooks-common.h:71
myBlocks_tc::myBlocks_tc
myBlocks_tc(uintptr_t mem, uintptr_t s, uint32_t algi, uint32_t cstage, size_t stDepth)
Definition: memory_hooks-common.h:84
aiStruct::aiStruct
aiStruct()
Definition: memory_hooks-common.h:39