forked from ajclinto/memview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemoryState.h
389 lines (326 loc) · 11.4 KB
/
MemoryState.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
This file is part of memview, a real-time memory trace visualization
application.
Copyright (C) 2013 Andrew Clinton
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU 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., 59 Temple Place, Suite 330, Boston, MA
02111-1307, USA.
The GNU General Public License is contained in the file COPYING.
*/
#ifndef MemoryState_H
#define MemoryState_H
#include "Math.h"
#include "GLImage.h"
#include "IntervalMap.h"
#include "SparseArray.h"
#include "mv_ipc.h"
#include <memory>
// Storage for the entire memory state. This is specifically designed to
// operate without any locking or atomics for the single writer / many
// reader case.
class MemoryState {
public:
class State {
public:
static const int theTimeShift = 17;
private:
// Here, type is the combined metadata that excludes the time
static const uint32 theStateTypeMask = (1 << theTimeShift) - 1;
static const uint32 theStateTimeMask = ~theStateTypeMask;
// Sub-fields of type
static const int theSubDataBits = 3;
static const uint32 theSubDataMask = (1 << theSubDataBits) - 1;
static const int theSubTypeBits = 3;
static const uint32 theSubTypeMask = (1 << theSubTypeBits) - 1;
static const int theSubThreadBits = 10;
static const uint32 theSubThreadMask = (1 << theSubThreadBits) - 1;
static const uint32 theSubSelectedMask = 1 << (theTimeShift-1);
public:
void init(uint32 time, uint32 type)
{ uval = type | (time << theTimeShift); }
void setTime(uint32 time)
{ uval = (uval & theStateTypeMask) | (time << theTimeShift); }
void setFree() { uval |= (MV_TypeFree << MV_DataBits); }
void setSelected() { uval |= theSubSelectedMask; }
// Field accessors. Here type is the sub-type (without the thread
// id).
uint32 dtype() const { return uval & theSubDataMask; }
uint32 type() const { return (uval >> theSubDataBits) &
theSubTypeMask; }
uint32 thread() const { return (uval >> (theSubDataBits +
theSubTypeBits)) &
theSubThreadMask; }
uint32 selected() const { return uval & theSubSelectedMask; }
uint32 time() const { return uval >> theTimeShift; }
uint32 uval;
};
static const uint32 theStale = 1;
static const uint32 theFullLife = 1 << (32-State::theTimeShift);
static const uint32 theHalfLife = theFullLife >> 1;
private:
static const int theAllBits = 36;
static const int thePageBits = 12;
typedef SparseArray<State, 22, thePageBits> StateArray;
// Raw memory state
struct LinkItem {
LinkItem(uint64 bits, uint64 top, LinkItem *next)
: myState(bits)
, myTop(top)
, myNext(next) {}
~LinkItem() { delete myNext; }
StateArray myState;
uint64 myTop;
LinkItem *myNext;
};
inline void splitAddr(uint64 &addr, uint64 &top) const
{
top = addr & myTopMask;
addr &= myBottomMask;
}
public:
MemoryState(int ignorebits);
~MemoryState();
#if 1
class UpdateCache {
public:
UpdateCache(MemoryState &state)
: myState(state)
, myData(&state.myHead.myState)
, myTop(state.myHead.myTop)
{}
StateArray &getState(uint64 top)
{
if (__builtin_expect(myTop != top, false))
{
myTop = top;
myData = &myState.findOrCreateState(top);
}
return *myData;
}
private:
MemoryState &myState;
StateArray *myData;
uint64 myTop;
};
#else
// Implementation that assumes all memory addresses are within
// theAllMask, for performance testing
class UpdateCache {
public:
UpdateCache(MemoryState &state) : myState(state) {}
StateArray &getState(uint64)
{
return myState.myHead.myState;
}
private:
MemoryState &myState;
};
#endif
inline void updateAddress(uint64 addr, uint64 size, uint32 type,
UpdateCache &cache)
{
addr >>= myIgnoreBits;
size >>= myIgnoreBits;
uint64 top = 0;
splitAddr(addr, top);
StateArray &state = cache.getState(top);
state.setExists(addr);
uint64 last;
switch (size)
{
case 0:
case 1:
if (!(type & (MV_TypeFree << MV_DataBits)))
state[addr].init(myTime, type);
else
state[addr].setFree();
break;
case 2:
if (!(type & (MV_TypeFree << MV_DataBits)))
{
state[addr].init(myTime, type);
state[addr+1].init(myTime, type);
}
else
{
state[addr].setFree();
state[addr+1].setFree();
}
break;
default:
last = addr + size;
if (!(type & (MV_TypeFree << MV_DataBits)))
{
for (; addr < last; addr++)
state[addr].init(myTime, type);
}
else
{
for (; addr < last; addr++)
state[addr].setFree();
}
break;
}
}
void incrementTime(StackTraceMap *stacks = 0);
uint32 getTime() const { return myTime; }
int getIgnoreBits() const { return myIgnoreBits; }
uint64 getPageCount() const
{
uint64 pagecount = 0;
for (const LinkItem *it = &myHead; it; it = it->myNext)
{
pagecount += it->myState.getPageCount();
}
return pagecount;
}
// Print status information for a memory address
void appendAddressInfo(QString &message, uint64 addr,
const MMapMap &map);
// Abstract access to a single page
class DisplayPage : public StateArray::Page {
public:
DisplayPage()
: StateArray::Page()
, myTop(0) {}
DisplayPage(const StateArray::Page &src, uint64 top)
: StateArray::Page(src)
, myTop(top) {}
uint64 addr() const { return myTop | StateArray::Page::addr(); }
private:
uint64 myTop;
};
DisplayPage getPage(uint64 addr, uint64 &off) const
{
uint64 top;
splitAddr(addr, top);
StateArray *state = findState(top);
if (state)
return DisplayPage(state->getPage(addr, off), top);
off = 0;
return DisplayPage();
}
class DisplayIterator {
public:
DisplayIterator(LinkItem *head)
: myTop(head)
{
rewind();
}
DisplayIterator(const DisplayIterator &it)
: myTop(it.myTop)
{
rewind();
}
bool atEnd() const
{
return !myTop;
}
void advance()
{
myBottom->advance();
if (myBottom->atEnd())
{
myTop = myTop->myNext;
rewind();
}
}
DisplayPage page() const
{
return DisplayPage(myBottom->page(), myTop->myTop);
}
private:
void rewind()
{
if (myTop)
{
myBottom.reset(
new StateArray::Iterator(myTop->myState));
}
}
private:
LinkItem *myTop;
std::unique_ptr<StateArray::Iterator> myBottom;
};
DisplayIterator begin()
{
return DisplayIterator(&myHead);
}
// Build a mipmap from another memory state
void downsample(const MemoryState &state);
void downsamplePage(const DisplayPage &page, int shift, bool fast);
// Set a flag that is reset to false when downsample is complete
void setSamplingInProgress() { mySampling = true; }
bool isSamplingInProgress() const { return mySampling; }
private:
class StackInfoUpdater {
bool myFull;
public:
StackInfoUpdater(bool full) : myFull(full) {}
void operator()(StackInfo &val) const
{
State sval; sval.uval = val.myState;
uint32 state = sval.time();
if (state && ((state >= theHalfLife) ^ myFull))
{
sval.setTime(theStale);
val.myState = sval.uval;
}
}
};
LinkItem *findLink(uint64 top, LinkItem *&prev) const
{
LinkItem *it = const_cast<LinkItem *>(&myHead);
prev = 0;
while (it && it->myTop < top)
{
prev = it;
it = it->myNext;
}
return it;
}
StateArray *findState(uint64 top) const
{
LinkItem *prev;
LinkItem *it = findLink(top, prev);
return (it && it->myTop == top) ? &it->myState : 0;
}
StateArray &findOrCreateState(uint64 top)
{
LinkItem *prev;
LinkItem *it = findLink(top, prev);
if (it && it->myTop == top)
return it->myState;
// Double checked lock
QMutexLocker lock(&myWriteLock);
it = findLink(top, prev);
if (it && it->myTop == top)
return it->myState;
it = new LinkItem(myBottomBits, top, it);
if (prev)
prev->myNext = it;
return it->myState;
}
private:
QMutex myWriteLock;
uint32 myTime; // Rolling counter
bool mySampling;
// The number of low-order bits to ignore. This value determines the
// resolution and memory use for the profile.
int myIgnoreBits;
int myBottomBits;
uint64 myBottomMask;
uint64 myTopMask;
// Maps memory for mask 0 on creation
LinkItem myHead;
};
#endif