ATLAS Offline Software
memory_hooks-tcmalloc.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // memory_hooks for tcmalloc which are used for the AthMemoryAuditor
6 // Rolf Seuster, Oct 2015
7 
8 #ifndef ATHENAAUDITORS_MEMORYHOOKSTCMALLOC_H
9 #define ATHENAAUDITORS_MEMORYHOOKSTCMALLOC_H
10 
11 // This auditor is not thread-safe without signiicant work.
12 // Disable checking for now.
13 // We'll also report an ERROR is this is used in an MT job.
16 
17 #include <iostream>
18 #include <fstream>
19 #include <iomanip>
20 
21 #include <cstdint>
22 #include <string.h>
23 #include <stdlib.h> // for getenv
24 
25 // hooks and utilities from tcmalloc
26 #include <gperftools/malloc_extension.h>
27 #include <gperftools/malloc_hook.h>
28 #include <gperftools/stacktrace.h>
29 #include "gperftools/tcmalloc.h"
30 
31 // various counters needed
32 static long long counter_tc_m(0);
33 static long long counter_tc_f(0);
34 static long long counter_tc_mtf(0);
35 static long long counter_tc_mm(0);
36 static long long counter_tc_mum(0);
37 
41 
42 // define empty stubs, these will be interposed by the real tcmalloc
43 // if they are actually called the job is badly misconfigured - should never happen
44 int MallocHook_RemoveNewHook(MallocHook_NewHook)
45 { std::cerr << "TCMALLOC HOOKS: RemoveNewHook - Should not be here\n"; abort(); return 1; }
46 int MallocHook_RemoveDeleteHook(MallocHook_DeleteHook)
47 { std::cerr << "TCMALLOC HOOKS: RemoveDeleteHook - Should not be here\n"; abort(); return 1; }
48 int MallocHook_AddNewHook(MallocHook_NewHook)
49 { std::cerr << "TCMALLOC HOOKS: AddNewHook - Should not be here\n"; abort(); return 1; }
50 int MallocHook_AddDeleteHook(MallocHook_DeleteHook)
51 { std::cerr << "TCMALLOC HOOKS: AddDeleteHook - Should not be here\n"; abort(); return 1; }
52 // both are not really implemented yet, just counting - FIXME to be done
53 int MallocHook_AddMmapHook(MallocHook_MmapHook)
54 { std::cerr << "TCMALLOC HOOKS: AddMmapHook - Should not be here\n"; abort(); return 1; }
55 int MallocHook_AddMunmapHook(MallocHook_MunmapHook)
56 { std::cerr << "TCMALLOC HOOKS: AddMunmapHook - Should not be here\n"; abort(); return 1; }
57 // interpose this as well - we use this to discover if we have tcmalloc preloaded
58 MallocExtension* MallocExtension::instance() { AthMemoryAuditor::m_usetcmalloc=false; return nullptr; }
59 
60 void athena_NewHook(const void* ptr, size_t size)
61 {
62  counter_tc_m++;
63 
65  myBlocks_tc *b = new myBlocks_tc( (uintptr_t)ptr, size, curIndex, current_stage, stacktraceDepth );
66 
67  if(collectStacktraces)
68  {
69  b->allocatedFrom.resize(stacktraceDepth);
70  unw_backtrace (b->allocatedFrom.data(), b->allocatedFrom.capacity());
71  }
72  std::pair<allocSet_tc::iterator, bool> i = allocset_tc.insert( *b );
73  if(!i.second)
74  {
75  std::cerr << "ALREADY EXISTING " << std::hex << ptr << std::dec << "\n";
76  }
77  allocset_last=i.first;
79 }
80 
81 void athena_DeleteHook(const void* ptr)
82 {
83  counter_tc_f++;
84  if(ptr==nullptr)
85  {
86  counter_tc_mtf++;
87  return;
88  }
89 
91  if ( allocset_last == allocset_tc.end() || allocset_last->allocated != (uintptr_t)ptr )
92  {
93  bg_tc->allocated=(uintptr_t)ptr;
94  allocSet_tc::iterator i = allocset_tc.find( *bg_tc );
95  if ( i != allocset_tc.end() )
96  {
97  allocset_tc.erase_and_dispose(i, allocSet_deleter());
98  }
99  }
100  else
101  {
102  counter_fna++;
103  // std::cerr << "TCMALLOC delete-last\n";
104  allocset_tc.erase(allocset_last);
105  delete &(*allocset_last);
106  allocset_last = allocset_tc.end();
107  }
109 }
110 
111 /* not fully implemented yet, just counting */
112 void athena_MmapHook(const void* /* result */,
113  const void* /* start */,
114  size_t /* size */,
115  int /* protection */,
116  int /* flags */,
117  int /* fd */,
118  off_t /* offset */)
119 {
120  counter_tc_mm++;
121  // std::cerr << "TCMALLOC mmap" << std::hex << result << " " << size << std::dec << "\n";
122 }
123 
124 void athena_MunmapHook(const void* /* ptr */, size_t /* size */)
125 {
126  counter_tc_mum++;
127  // std::cerr << "TCMALLOC munmap " << std::hex << ptr << " " << size << std::dec << "\n";
128 }
129 
130 void uninstall()
131 {
134 }
135 
137 {
138  bg_tc = new myBlocks_tc();
139  allocset_last=allocset_tc.end();
140 
141  if(MallocHook::AddNewHook(&athena_NewHook))
142  std::cerr << "TCMALLOC NEW HOOK OK\n";
143  if(MallocHook::AddDeleteHook(&athena_DeleteHook))
144  std::cerr << "TCMALLOC DELETE HOOK OK\n";
145  if(MallocHook::AddMmapHook(&athena_MmapHook))
146  std::cerr << "TCMALLOC MMAP HOOK OK\n";
147  if(MallocHook::AddMunmapHook(&athena_MunmapHook))
148  std::cerr << "TCMALLOC MUNMAP HOOK OK\n";
149 
150  initialized=true;
151 }
152 #endif
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
bg_tc
myBlocks_tc * bg_tc
Definition: memory_hooks-common.h:107
MallocHook_RemoveNewHook
int MallocHook_RemoveNewHook(MallocHook_NewHook)
Definition: memory_hooks-tcmalloc.h:44
athena_DeleteHook
void athena_DeleteHook(const void *ptr)
Definition: memory_hooks-tcmalloc.h:81
my_init_tcmalloc
void my_init_tcmalloc()
Definition: memory_hooks-tcmalloc.h:136
myBlocks_tc::allocated
uintptr_t allocated
Definition: memory_hooks-common.h:68
ATLAS_NO_CHECK_FILE_THREAD_SAFETY
ATLAS_NO_CHECK_FILE_THREAD_SAFETY
Definition: memory_hooks-tcmalloc.h:15
instance
std::map< std::string, double > instance
Definition: Run_To_Get_Tags.h:8
MallocHook_RemoveDeleteHook
int MallocHook_RemoveDeleteHook(MallocHook_DeleteHook)
Definition: memory_hooks-tcmalloc.h:46
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
allocSet_deleter
std::default_delete< myBlocks_tc > allocSet_deleter
Definition: memory_hooks-common.h:110
AthMemoryAuditor::m_usetcmalloc
static bool m_usetcmalloc
Definition: AthMemoryAuditor.h:46
myBlocks_tc
Definition: memory_hooks-common.h:66
athena_MmapHook
void athena_MmapHook(const void *, const void *, size_t, int, int, int, off_t)
Definition: memory_hooks-tcmalloc.h:112
lumiFormat.i
int i
Definition: lumiFormat.py:92
MallocHook_AddMmapHook
int MallocHook_AddMmapHook(MallocHook_MmapHook)
Definition: memory_hooks-tcmalloc.h:53
athena_NewHook
void athena_NewHook(const void *ptr, size_t size)
Definition: memory_hooks-tcmalloc.h:60
MallocHook_AddNewHook
int MallocHook_AddNewHook(MallocHook_NewHook)
Definition: memory_hooks-tcmalloc.h:48
MallocHook_AddDeleteHook
int MallocHook_AddDeleteHook(MallocHook_DeleteHook)
Definition: memory_hooks-tcmalloc.h:50
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
athena_MunmapHook
void athena_MunmapHook(const void *, size_t)
Definition: memory_hooks-tcmalloc.h:124
MallocHook_AddMunmapHook
int MallocHook_AddMunmapHook(MallocHook_MunmapHook)
Definition: memory_hooks-tcmalloc.h:55
checker_macros.h
Define macros for attributes used to control the static checker.
uninstall
void uninstall()
Definition: memory_hooks-tcmalloc.h:130