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
|
/**
* @file lltracethreadrecorder.cpp
*
* $LicenseInfo:firstyear=2001&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2012, Linden Research, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*/
#include "linden_common.h"
#include "lltracethreadrecorder.h"
#include "llfasttimer.h"
namespace LLTrace
{
///////////////////////////////////////////////////////////////////////
// ThreadRecorder
///////////////////////////////////////////////////////////////////////
ThreadRecorder::ThreadRecorder()
{
//NB: the ordering of initialization in this function is very fragile due to a large number of implicit dependencies
set_thread_recorder(this);
TimeBlock& root_time_block = TimeBlock::getRootTimeBlock();
ThreadTimerStack* timer_stack = ThreadTimerStack::getInstance();
timer_stack->mTimeBlock = &root_time_block;
timer_stack->mActiveTimer = NULL;
mNumTimeBlockTreeNodes = AccumulatorBuffer<TimeBlockAccumulator>::getDefaultBuffer()->size();
mTimeBlockTreeNodes = new TimeBlockTreeNode[mNumTimeBlockTreeNodes];
mThreadRecording.start();
// initialize time block parent pointers
for (LLInstanceTracker<TimeBlock>::instance_iter it = LLInstanceTracker<TimeBlock>::beginInstances(), end_it = LLInstanceTracker<TimeBlock>::endInstances();
it != end_it;
++it)
{
TimeBlock& time_block = *it;
TimeBlockTreeNode& tree_node = mTimeBlockTreeNodes[it->getIndex()];
tree_node.mBlock = &time_block;
tree_node.mParent = &root_time_block;
it->getPrimaryAccumulator()->mParent = &root_time_block;
}
mRootTimer = new BlockTimer(root_time_block);
timer_stack->mActiveTimer = mRootTimer;
TimeBlock::getRootTimeBlock().getPrimaryAccumulator()->mActiveCount = 1;
}
ThreadRecorder::~ThreadRecorder()
{
delete mRootTimer;
while(mActiveRecordings.size())
{
mActiveRecordings.front()->mTargetRecording->stop();
}
set_thread_recorder(NULL);
delete[] mTimeBlockTreeNodes;
}
TimeBlockTreeNode* ThreadRecorder::getTimeBlockTreeNode(S32 index)
{
if (0 <= index && index < mNumTimeBlockTreeNodes)
{
return &mTimeBlockTreeNodes[index];
}
return NULL;
}
void ThreadRecorder::activate( Recording* recording )
{
ActiveRecording* active_recording = new ActiveRecording(recording);
if (!mActiveRecordings.empty())
{
mActiveRecordings.front()->mPartialRecording.handOffTo(active_recording->mPartialRecording);
}
mActiveRecordings.push_front(active_recording);
mActiveRecordings.front()->mPartialRecording.makePrimary();
}
ThreadRecorder::active_recording_list_t::iterator ThreadRecorder::update( Recording* recording )
{
active_recording_list_t::iterator it, end_it;
for (it = mActiveRecordings.begin(), end_it = mActiveRecordings.end();
it != end_it;
++it)
{
active_recording_list_t::iterator next_it = it;
++next_it;
// if we have another recording further down in the stack...
if (next_it != mActiveRecordings.end())
{
// ...push our gathered data down to it
(*next_it)->mPartialRecording.appendBuffers((*it)->mPartialRecording);
}
// copy accumulated measurements into result buffer and clear accumulator (mPartialRecording)
(*it)->moveBaselineToTarget();
if ((*it)->mTargetRecording == recording)
{
// found the recording, so return it
break;
}
}
if (it == end_it)
{
llwarns << "Recording not active on this thread" << llendl;
}
return it;
}
AccumulatorBuffer<CountAccumulator<F64> > gCountsFloat;
AccumulatorBuffer<MeasurementAccumulator<F64> > gMeasurementsFloat;
AccumulatorBuffer<CountAccumulator<S64> > gCounts;
AccumulatorBuffer<MeasurementAccumulator<S64> > gMeasurements;
AccumulatorBuffer<TimeBlockAccumulator> gStackTimers;
AccumulatorBuffer<MemStatAccumulator> gMemStats;
void ThreadRecorder::deactivate( Recording* recording )
{
active_recording_list_t::iterator it = update(recording);
if (it != mActiveRecordings.end())
{
// and if we've found the recording we wanted to update
active_recording_list_t::iterator next_it = it;
++next_it;
if (next_it != mActiveRecordings.end())
{
(*next_it)->mTargetRecording->makePrimary();
}
delete *it;
mActiveRecordings.erase(it);
}
}
ThreadRecorder::ActiveRecording::ActiveRecording( Recording* target )
: mTargetRecording(target)
{
}
void ThreadRecorder::ActiveRecording::moveBaselineToTarget()
{
mTargetRecording->appendBuffers(mPartialRecording);
mPartialRecording.resetBuffers();
}
///////////////////////////////////////////////////////////////////////
// SlaveThreadRecorder
///////////////////////////////////////////////////////////////////////
SlaveThreadRecorder::SlaveThreadRecorder()
{
getMasterThreadRecorder().addSlaveThread(this);
}
SlaveThreadRecorder::~SlaveThreadRecorder()
{
getMasterThreadRecorder().removeSlaveThread(this);
}
void SlaveThreadRecorder::pushToMaster()
{
mThreadRecording.stop();
{
LLMutexLock(getMasterThreadRecorder().getSlaveListMutex());
mSharedData.appendFrom(mThreadRecording);
}
mThreadRecording.start();
}
void SlaveThreadRecorder::SharedData::appendFrom( const Recording& source )
{
LLMutexLock lock(&mRecordingMutex);
mRecording.appendRecording(source);
}
void SlaveThreadRecorder::SharedData::appendTo( Recording& sink )
{
LLMutexLock lock(&mRecordingMutex);
sink.appendRecording(mRecording);
}
void SlaveThreadRecorder::SharedData::mergeFrom( const RecordingBuffers& source )
{
LLMutexLock lock(&mRecordingMutex);
mRecording.mergeBuffers(source);
}
void SlaveThreadRecorder::SharedData::mergeTo( RecordingBuffers& sink )
{
LLMutexLock lock(&mRecordingMutex);
sink.mergeBuffers(mRecording);
}
void SlaveThreadRecorder::SharedData::reset()
{
LLMutexLock lock(&mRecordingMutex);
mRecording.reset();
}
///////////////////////////////////////////////////////////////////////
// MasterThreadRecorder
///////////////////////////////////////////////////////////////////////
LLFastTimer::DeclareTimer FTM_PULL_TRACE_DATA_FROM_SLAVES("Pull slave trace data");
void MasterThreadRecorder::pullFromSlaveThreads()
{
LLFastTimer _(FTM_PULL_TRACE_DATA_FROM_SLAVES);
if (mActiveRecordings.empty()) return;
LLMutexLock lock(&mSlaveListMutex);
RecordingBuffers& target_recording_buffers = mActiveRecordings.front()->mPartialRecording;
for (slave_thread_recorder_list_t::iterator it = mSlaveThreadRecorders.begin(), end_it = mSlaveThreadRecorders.end();
it != end_it;
++it)
{
// ignore block timing info for now
(*it)->mSharedData.mergeTo(target_recording_buffers);
(*it)->mSharedData.reset();
}
}
void MasterThreadRecorder::addSlaveThread( class SlaveThreadRecorder* child )
{
LLMutexLock lock(&mSlaveListMutex);
mSlaveThreadRecorders.push_back(child);
}
void MasterThreadRecorder::removeSlaveThread( class SlaveThreadRecorder* child )
{
LLMutexLock lock(&mSlaveListMutex);
for (slave_thread_recorder_list_t::iterator it = mSlaveThreadRecorders.begin(), end_it = mSlaveThreadRecorders.end();
it != end_it;
++it)
{
if ((*it) == child)
{
mSlaveThreadRecorders.erase(it);
break;
}
}
}
void MasterThreadRecorder::pushToMaster()
{}
MasterThreadRecorder::MasterThreadRecorder()
{}
}
|