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 #include <malloc.h> | |
Jim Stichnoth
2016/03/12 00:12:58
alphabetize these.
sehr
2016/03/15 00:36:45
Done.
| |
18 #include <dlfcn.h> | |
19 #include <iostream> | |
20 #include <map> | |
21 | |
22 extern "C" void *__libc_malloc(size_t size); | |
23 | |
24 namespace { | |
25 std::map<void*, uint64_t> *Callers; | |
Jim Stichnoth
2016/03/12 00:12:58
Use std::unordered_map<>, no?
Also, please use a
Jim Stichnoth
2016/03/12 00:36:15
Probably should add a note somewhere that this is
sehr
2016/03/15 00:36:45
Added here and a warning comes out if a build incl
sehr
2016/03/15 00:36:45
Done.
| |
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 void *InternalAllocator(size_t size, void *caller) { | |
Jim Stichnoth
2016/03/12 00:12:58
LLVM coding style would be:
void *internalAllocat
sehr
2016/03/15 00:36:45
Done.
| |
31 if (Callers != nullptr && !InAllocatorFunction) { | |
32 InAllocatorFunction = true; | |
33 auto SetI = Callers->find(caller); | |
Jim Stichnoth
2016/03/12 00:12:58
Can't you just simply:
++(*Callers)[caller];
?
sehr
2016/03/15 00:36:45
Done.
| |
34 if (SetI == Callers->end()) { | |
35 (*Callers)[caller] = 1; | |
36 } else { | |
37 (*Callers)[caller]++; | |
38 } | |
39 InAllocatorFunction = false; | |
40 } | |
41 return __libc_malloc(size); | |
42 } | |
43 } // end anonymous namespace | |
Jim Stichnoth
2016/03/12 00:12:58
end of anonymous namespace
sehr
2016/03/15 00:36:45
Done.
| |
44 | |
45 void *operator new (size_t size) { | |
46 void *caller = __builtin_return_address(0); | |
47 return InternalAllocator(size, caller); | |
48 } | |
49 | |
50 void *operator new[] (size_t size) { | |
51 void *caller = __builtin_return_address(0); | |
52 return InternalAllocator(size, caller); | |
53 } | |
54 | |
55 extern "C" void *malloc(size_t size) { | |
56 void *caller = __builtin_return_address(0); | |
57 return InternalAllocator(size, caller); | |
58 } | |
59 | |
60 namespace Ice { | |
61 | |
62 void LinuxMallocProfiling::Start() { | |
63 Callers = new std::map<void*, uint64_t>(); | |
64 } | |
65 | |
66 void LinuxMallocProfiling::End() { | |
67 for (auto C : *Callers) { | |
Karl
2016/03/11 23:25:11
Should you use?
for (auto &C : *Callers) {
sehr
2016/03/15 00:36:45
Done.
| |
68 Dl_info dli; | |
69 dladdr(C.first, &dli); | |
70 | |
71 std::cout << C.second << " "; | |
72 if (dli.dli_sname == NULL) { | |
73 std::cout << C.first; | |
74 } else { | |
75 std::cout << dli.dli_sname; | |
76 } | |
77 std::cout << std::endl; | |
Jim Stichnoth
2016/03/12 00:12:58
I believe LLVM coding style says use "\n" instead
sehr
2016/03/15 00:36:45
Done.
| |
78 } | |
79 delete Callers; | |
Jim Stichnoth
2016/03/12 00:12:58
Our code base favors unique_ptr<> over explicit de
sehr
2016/03/15 00:36:45
The Callers map needs to be accessible to non-memb
| |
80 Callers = nullptr; | |
81 } | |
82 | |
83 } // end namespace Ice | |
Jim Stichnoth
2016/03/12 00:12:58
end of namespace Ice
sehr
2016/03/15 00:36:45
Done.
| |
OLD | NEW |