forked from KDE/heaptrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accumulatedtracedata.h
186 lines (154 loc) · 4.95 KB
/
accumulatedtracedata.h
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
185
186
/*
* Copyright 2015 Milian Wolff <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef ACCUMULATEDTRACEDATA_H
#define ACCUMULATEDTRACEDATA_H
#include <iosfwd>
#include <tuple>
#include <vector>
#include <fstream>
#include <unordered_map>
#include <map>
// sadly, C++ doesn't yet have opaque typedefs
template<typename Base>
struct Index
{
uint64_t index = 0;
explicit operator bool() const
{
return index;
}
bool operator<(Index o) const
{
return index < o.index;
}
bool operator!=(Index o) const
{
return index != o.index;
}
bool operator==(Index o) const
{
return index == o.index;
}
};
template<typename Base>
uint qHash(const Index<Base> index, uint seed = 0) noexcept
{
return qHash(index.index, seed);
}
struct StringIndex : public Index<StringIndex> {};
struct ModuleIndex : public StringIndex {};
struct FunctionIndex : public StringIndex {};
struct FileIndex : public StringIndex {};
struct IpIndex : public Index<IpIndex> {};
struct TraceIndex : public Index<TraceIndex> {};
struct InstructionPointer
{
uint64_t instructionPointer = 0;
ModuleIndex moduleIndex;
FunctionIndex functionIndex;
FileIndex fileIndex;
int line = 0;
bool compareWithoutAddress(const InstructionPointer &other) const
{
return std::make_tuple(moduleIndex, functionIndex, fileIndex, line)
< std::make_tuple(other.moduleIndex, other.functionIndex, other.fileIndex, other.line);
}
bool equalWithoutAddress(const InstructionPointer &other) const
{
return std::make_tuple(moduleIndex, functionIndex, fileIndex, line)
== std::make_tuple(other.moduleIndex, other.functionIndex, other.fileIndex, other.line);
}
};
struct TraceNode
{
IpIndex ipIndex;
TraceIndex parentIndex;
};
struct AllocationData
{
// number of allocations
uint64_t allocations = 0;
// bytes allocated in total
uint64_t allocated = 0;
// amount of bytes leaked
uint64_t leaked = 0;
// largest amount of bytes allocated
uint64_t peak = 0;
};
struct Allocation : public AllocationData
{
// backtrace entry point
TraceIndex traceIndex;
};
/**
* Merged allocation information by instruction pointer outside of alloc funcs
*/
struct MergedAllocation : public AllocationData
{
// individual backtraces
std::vector<Allocation> traces;
// location
IpIndex ipIndex;
};
/**
* Information for a single call to an allocation function
*/
struct AllocationInfo
{
TraceIndex traceIndex;
uint64_t size;
};
struct AccumulatedTraceData
{
AccumulatedTraceData();
virtual ~AccumulatedTraceData() = default;
virtual void handleTimeStamp(uint64_t oldStamp, uint64_t newStamp) = 0;
virtual void handleAllocation() = 0;
virtual void handleDebuggee(const char* command) = 0;
const std::string& stringify(const StringIndex stringId) const;
std::string prettyFunction(const std::string& function) const;
bool read(const std::string& inputFile);
bool read(std::istream& in);
bool shortenTemplates = false;
bool printHistogram = false;
bool fromAttached = false;
std::vector<Allocation> allocations;
std::map<uint64_t, uint64_t> sizeHistogram;
uint64_t totalAllocated = 0;
uint64_t totalAllocations = 0;
uint64_t peak = 0;
uint64_t leaked = 0;
uint64_t totalTime = 0;
// our indices are sequentially increasing thus a new allocation can only ever
// occur with an index larger than any other we encountered so far
// this can be used to our advantage in speeding up the findAllocation calls.
TraceIndex m_maxAllocationTraceIndex;
Allocation& findAllocation(const TraceIndex traceIndex);
InstructionPointer findIp(const IpIndex ipIndex) const;
TraceNode findTrace(const TraceIndex traceIndex) const;
bool isStopIndex(const StringIndex index) const;
// indices of functions that should stop the backtrace, e.g. main or static initialization
std::vector<StringIndex> stopIndices;
std::unordered_map<uint64_t, AllocationInfo> activeAllocations;
std::vector<InstructionPointer> instructionPointers;
std::vector<TraceNode> traces;
std::vector<std::string> strings;
std::vector<IpIndex> opNewIpIndices;
};
#endif // ACCUMULATEDTRACEDATA_H