-
Notifications
You must be signed in to change notification settings - Fork 14
/
MemoryState.C
228 lines (188 loc) · 5.77 KB
/
MemoryState.C
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
/*
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.
*/
#include <QThreadPool>
#include "MemoryState.h"
#include "StopWatch.h"
#include "Color.h"
#include "GLImage.h"
#include <assert.h>
#include <sys/mman.h>
#include <stdio.h>
#include <unistd.h>
#include <vector>
MemoryState::MemoryState(int ignorebits)
: myTime(2)
, mySampling(false)
, myIgnoreBits(ignorebits)
, myBottomBits(SYSmax(theAllBits-ignorebits, thePageBits))
, myHead(myBottomBits, 0, 0)
{
myBottomMask = (1ull << myBottomBits)-1;
myTopMask = ~myBottomMask;
}
MemoryState::~MemoryState()
{
}
void
MemoryState::incrementTime(StackTraceMap *stacks)
{
QMutexLocker lock(&myWriteLock);
myTime++;
bool half = myTime == theHalfLife;
bool full = myTime == theFullLife;
if (half || full)
{
// The time wrapped
for (DisplayIterator it(begin()); !it.atEnd(); it.advance())
{
DisplayPage page(it.page());
for (uint64 i = 0; i < page.size(); i++)
{
uint32 state = page.state(i).time();
if (state && ((state >= theHalfLife) ^ full))
page.state(i).setTime(theStale);
}
}
if (stacks)
{
StackTraceMapWriter writer(*stacks);
uint64 start, end;
writer.getTotalInterval(start, end);
writer.apply(start, end, StackInfoUpdater(full));
}
if (full)
myTime = 2;
}
}
void
MemoryState::appendAddressInfo(
QString &message, uint64 addr,
const MMapMap &map)
{
if (!addr)
return;
QString tmp;
uint64 paddr = addr << myIgnoreBits;
MMapMapReader reader(map);
auto it = reader.find(paddr);
MMapInfo mmapinfo{"Address", 0, false};
if (it != reader.end())
mmapinfo = it.value();
tmp.sprintf("\t%s: 0x%.12llx", mmapinfo.myStr.c_str(), paddr);
message.append(tmp);
uint64 off;
auto page = getPage(addr, off);
if (!page.exists())
return;
State entry = page.state(off);
if (!entry.uval)
return;
const char *typestr = 0;
int type = entry.type();
switch (type & ~MV_TypeFree)
{
case MV_TypeRead: typestr = "Read"; break;
case MV_TypeWrite: typestr = "Written"; break;
case MV_TypeInstr: typestr = "Instruction"; break;
case MV_TypeAlloc: typestr = "Allocated"; break;
}
if (typestr)
{
if (!mmapinfo.myMapped)
typestr = "Unmapped";
else if (type & MV_TypeFree)
typestr = "Deallocated";
tmp.sprintf("\t(Thread %d %s)", entry.thread(), typestr);
message.append(tmp);
}
}
class Downsample : public QRunnable {
public:
Downsample(MemoryState &dst, int shift, bool fast)
: myDst(dst)
, myShift(shift)
, myFast(fast)
{
}
void push(const MemoryState::DisplayPage src)
{
mySrc.push_back(src);
}
size_t size() const { return mySrc.size(); }
virtual void run()
{
for (auto it = mySrc.begin(); it != mySrc.end(); ++it)
myDst.downsamplePage(*it, myShift, myFast);
}
private:
MemoryState &myDst;
std::vector<MemoryState::DisplayPage> mySrc;
int myShift;
bool myFast;
};
void
MemoryState::downsample(const MemoryState &state)
{
const int shift = myIgnoreBits - state.myIgnoreBits;
// Copy time first for the display to work correctly
myTime = state.myTime;
Downsample *task = 0;
uint64 bunch_size = 16;
for (DisplayIterator it(const_cast<MemoryState &>(state).begin());
!it.atEnd(); it.advance())
{
// Split up source pages into tasks. This isn't strictly
// thread-safe when 1 << shift is greater than bunch_size, but the
// errors aren't usually visible.
if (!task)
task = new Downsample(*this, shift, false);
task->push(it.page());
if (task->size() >= bunch_size)
{
QThreadPool::globalInstance()->start(task);
task = 0;
}
}
if (task)
QThreadPool::globalInstance()->start(task);
QThreadPool::globalInstance()->waitForDone();
mySampling = false;
}
void
MemoryState::downsamplePage(const DisplayPage &page, int shift, bool fast)
{
const uint64 scale = 1ull << shift;
const uint64 stride = fast ? 1 : scale;
uint64 myaddr = page.addr() >> shift;
uint64 mytop;
splitAddr(myaddr, mytop);
StateArray &state = findOrCreateState(mytop);
state.setExists(myaddr);
for (uint64 i = 0; i < page.size(); i += scale)
{
uint &mystate = state[myaddr].uval;
const State *arr = page.stateArray();
uint64 n = SYSmin(i + stride, page.size());
for (uint64 j = i; j < n; j++)
{
mystate = SYSmax(mystate, arr[j].uval);
}
myaddr++;
}
}