1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
#include "trace.h"
#include "times.h"
#include "acconf.h"
namespace ledger {
bool trace_mode;
void trace(const std::string& cat, const std::string& str)
{
char buf[32];
std::cerr << boost::posix_time::to_simple_string(now) << " "
<< cat << ": " << str << std::endl;
}
void trace_push(const std::string& cat, const std::string& str,
timing_t& timer)
{
timer.start();
trace(cat, str);
}
void trace_pop(const std::string& cat, const std::string& str,
timing_t& timer)
{
timer.stop();
std::ostringstream out;
out << str << ": " << (double(timer.cumulative) / double(CLOCKS_PER_SEC)) << "s";
trace(cat, out.str());
}
live_objects_map live_objects;
object_count_map ctor_count;
object_count_map object_count;
object_count_map live_count;
bool tracing_active = false;
bool trace_ctor(void * ptr, const std::string& name)
{
if (! tracing_active)
return true;
DEBUG_PRINT("ledger.trace.debug", "trace_ctor " << ptr << " " << name);
std::string::size_type pos = name.find_first_of('(');
std::string cls_name(name, 0, pos);
live_objects.insert(live_objects_pair(ptr, cls_name));
object_count_map::iterator i = ctor_count.find(name);
if (i != ctor_count.end()) {
(*i).second++;
} else {
std::pair<object_count_map::iterator, bool> result
= ctor_count.insert(object_count_pair(name, 1));
if (! result.second) {
tracing_active = false;
return false;
}
}
object_count_map::iterator j = object_count.find(cls_name);
if (j != object_count.end()) {
(*j).second++;
} else {
std::pair<object_count_map::iterator, bool> result
= object_count.insert(object_count_pair(cls_name, 1));
if (! result.second) {
tracing_active = false;
return false;
}
}
object_count_map::iterator k = live_count.find(cls_name);
if (k != live_count.end()) {
(*k).second++;
} else {
std::pair<object_count_map::iterator, bool> result
= live_count.insert(object_count_pair(cls_name, 1));
if (! result.second) {
tracing_active = false;
return false;
}
}
return true;
}
bool trace_dtor(void * ptr, const std::string& name)
{
if (! tracing_active)
return true;
DEBUG_PRINT("ledger.trace.debug", "trace_dtor " << ptr << " " << name);
live_objects_map::iterator i = live_objects.find(ptr);
if (i == live_objects.end()) {
std::cerr << "Destruction of unknown object " << name << " " << ptr
<< std::endl;;
tracing_active = false;
return false;
}
std::string::size_type pos = name.find_first_of('(');
std::string cls_name(name, 0, pos);
int ptr_count = live_objects.count(ptr);
for (int x = 0; x < ptr_count; x++) {
if ((*i).second == cls_name) {
live_objects.erase(i);
break;
} else {
i++;
}
}
object_count_map::iterator k = live_count.find(name);
if (k == live_count.end()) {
std::cerr << "Destruction of unregistered class " << name
<< std::endl;;
tracing_active = false;
return false;
}
if (--(*k).second == 0)
live_count.erase(k);
return true;
}
void report_memory(std::ostream& out)
{
if (live_count.size() > 0)
out << "Live object counts:" << std::endl;
for (object_count_map::iterator i = live_count.begin();
i != live_count.end();
i++) {
out << " ";
out << std::right;
out.width(5);
out << (*i).second << " " << (*i).first << std::endl;
}
DEBUG_IF("ledger.trace.verbose") {
if (live_objects.size() > 0)
out << "Live objects:" << std::endl;
for (live_objects_map::iterator i = live_objects.begin();
i != live_objects.end();
i++) {
out << " ";
out << std::right;
out.width(5);
out << (*i).first << " " << (*i).second << std::endl;
}
}
if (object_count.size() > 0)
out << "Object counts:" << std::endl;
for (object_count_map::iterator i = object_count.begin();
i != object_count.end();
i++) {
out << " ";
out << std::right;
out.width(5);
out << (*i).second << " " << (*i).first << std::endl;
}
if (ctor_count.size() > 0)
out << "Constructor counts:" << std::endl;
for (object_count_map::iterator i = ctor_count.begin();
i != ctor_count.end();
i++) {
out << " ";
out << std::right;
out.width(5);
out << (*i).second << " " << (*i).first << std::endl;
}
}
} // namespace ledger
|