OLD | NEW |
---|---|
(Empty) | |
1 //===- subzero/src/LinuxMallocProfiling.cpp - malloc/new tracing ---------===// | |
2 // | |
3 // The Subzero Code Generator | |
4 // | |
5 // This file is distributed under the University of Illinois Open Source | |
6 // License. See LICENSE.TXT for details. | |
7 // | |
8 //===----------------------------------------------------------------------===// | |
9 /// | |
10 /// \file | |
11 /// \brief malloc/new/...caller tracing. | |
12 /// | |
13 //===----------------------------------------------------------------------===// | |
14 | |
15 #include "LinuxMallocProfiling.h" | |
16 | |
17 #ifdef ALLOW_LINUX_MALLOC_PROFILE | |
Jim Stichnoth
2016/03/15 15:36:50
I don't like all the #ifdef here.
I think it woul
sehr
2016/03/17 16:47:14
Done.
| |
18 #include <dlfcn.h> | |
19 #include <malloc.h> | |
20 #include <iostream> | |
Jim Stichnoth
2016/03/15 15:36:50
LLVM sez iostream is forbidden.
http://llvm.org/d
sehr
2016/03/17 16:47:14
I plumbed through the Ostream that was already use
| |
21 #include <unordered_map> | |
22 | |
23 extern "C" void *__libc_malloc(size_t size); | |
24 | |
25 namespace { | |
26 // The Callers structure allocates memory, which would perturb the tracing. | |
27 // InAllocatorFunction is true when we are tracing a new/malloc/... | |
28 bool InAllocatorFunction = false; | |
29 | |
30 // Keep track of the number of times a particular call site address invoked | |
31 // an allocator. NOTE: this is not thread safe, so the user must invoke with | |
32 // --threads=0 to enable profilin. | |
John
2016/03/15 15:19:06
profiling
sehr
2016/03/17 16:47:14
Done.
| |
33 using MallocMap = std::unordered_map<void*, uint64_t>; | |
34 MallocMap *Callers; | |
35 | |
36 void *internalAllocator(size_t size, void *caller) { | |
37 if (Callers != nullptr && !InAllocatorFunction) { | |
38 InAllocatorFunction = true; | |
39 ++(*Callers)[caller]; | |
40 InAllocatorFunction = false; | |
41 } | |
42 return __libc_malloc(size); | |
43 } | |
44 } // end of anonymous namespace | |
45 | |
46 void *operator new (size_t size) { | |
John
2016/03/15 15:19:06
can you add a comment explaining why this works?
sehr
2016/03/17 16:47:14
Done.
| |
47 void *caller = __builtin_return_address(0); | |
48 return internalAllocator(size, caller); | |
49 } | |
50 | |
51 void *operator new[] (size_t size) { | |
52 void *caller = __builtin_return_address(0); | |
53 return internalAllocator(size, caller); | |
54 } | |
55 | |
56 extern "C" void *malloc(size_t size) { | |
57 void *caller = __builtin_return_address(0); | |
58 return internalAllocator(size, caller); | |
59 } | |
60 #endif // ALLOW_LINUX_MALLOC_PROFILE | |
61 | |
62 namespace Ice { | |
63 | |
64 void LinuxMallocProfiling::start() { | |
65 #ifdef ALLOW_LINUX_MALLOC_PROFILE | |
66 Callers = new MallocMap(); | |
67 #endif // ALLOW_LINUX_MALLOC_PROFILE | |
68 } | |
69 | |
70 void LinuxMallocProfiling::end() { | |
71 #ifdef ALLOW_LINUX_MALLOC_PROFILE | |
72 if (Callers == nullptr) { | |
73 return; | |
74 } | |
75 for (auto &C : *Callers) { | |
John
2016/03/15 15:19:06
does
const auto &
work?
sehr
2016/03/17 16:47:14
Yes. Done.
| |
76 Dl_info dli; | |
77 dladdr(C.first, &dli); | |
78 | |
79 std::cout << C.second << " "; | |
80 if (dli.dli_sname == NULL) { | |
81 std::cout << C.first; | |
82 } else { | |
83 std::cout << dli.dli_sname; | |
84 } | |
85 std::cout << "\n"; | |
86 } | |
87 delete Callers; | |
88 Callers = nullptr; | |
89 #endif // ALLOW_LINUX_MALLOC_PROFILE | |
90 } | |
91 | |
92 } // end of namespace Ice | |
OLD | NEW |