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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
|
/**
* @file lltrace.h
* @brief Runtime statistics accumulation.
*
* $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$
*/
#ifndef LL_LLTRACE_H
#define LL_LLTRACE_H
#include "stdtypes.h"
#include "llpreprocessor.h"
#include "llmemory.h"
#include "llrefcount.h"
//#include "lltracethreadrecorder.h"
#include "llunit.h"
#include "llapr.h"
#include <list>
#define TOKEN_PASTE_ACTUAL(x, y) x##y
#define TOKEN_PASTE(x, y) TOKEN_PASTE_ACTUAL(x, y)
#define RECORD_BLOCK_TIME(block_timer) LLTrace::BlockTimer::Recorder TOKEN_PASTE(block_time_recorder, __COUNTER__)(block_timer);
namespace LLTrace
{
class Recording;
void init();
void cleanup();
LLThreadLocalPointer<class ThreadRecorder>& get_thread_recorder();
class LL_COMMON_API MasterThreadRecorder& getMasterThreadRecorder();
// one per thread per type
template<typename ACCUMULATOR>
class LL_COMMON_API AccumulatorBuffer : public LLRefCount
{
static const U32 DEFAULT_ACCUMULATOR_BUFFER_SIZE = 64;
private:
enum StaticAllocationMarker { STATIC_ALLOC };
AccumulatorBuffer(StaticAllocationMarker m)
: mStorageSize(64),
mNextStorageSlot(0),
mStorage(new ACCUMULATOR[DEFAULT_ACCUMULATOR_BUFFER_SIZE])
{}
public:
// copying an accumulator buffer does not copy the actual contents, but simply initializes the buffer size
// to be identical to the other buffer
AccumulatorBuffer(const AccumulatorBuffer& other = getDefaultBuffer())
: mStorageSize(other.mStorageSize),
mStorage(new ACCUMULATOR[other.mStorageSize]),
mNextStorageSlot(other.mNextStorageSlot)
{}
~AccumulatorBuffer()
{
if (sPrimaryStorage == mStorage)
{
//TODO pick another primary?
sPrimaryStorage = NULL;
}
}
LL_FORCE_INLINE ACCUMULATOR& operator[](size_t index)
{
return mStorage[index];
}
LL_FORCE_INLINE const ACCUMULATOR& operator[](size_t index) const
{
return mStorage[index];
}
void addSamples(const AccumulatorBuffer<ACCUMULATOR>& other)
{
llassert(mNextStorageSlot == other.mNextStorageSlot);
for (size_t i = 0; i < mNextStorageSlot; i++)
{
mStorage[i].addSamples(other.mStorage[i]);
}
}
void copyFrom(const AccumulatorBuffer<ACCUMULATOR>& other)
{
for (size_t i = 0; i < mNextStorageSlot; i++)
{
mStorage[i] = other.mStorage[i];
}
}
void reset()
{
for (size_t i = 0; i < mNextStorageSlot; i++)
{
mStorage[i].reset();
}
}
void makePrimary()
{
sPrimaryStorage = mStorage;
}
bool isPrimary() const
{
return sPrimaryStorage == mStorage;
}
LL_FORCE_INLINE static ACCUMULATOR* getPrimaryStorage()
{
return sPrimaryStorage.get();
}
// NOTE: this is not thread-safe. We assume that slots are reserved in the main thread before any child threads are spawned
size_t reserveSlot()
{
size_t next_slot = mNextStorageSlot++;
if (next_slot >= mStorageSize)
{
size_t new_size = mStorageSize + (mStorageSize >> 2);
delete [] mStorage;
mStorage = new ACCUMULATOR[new_size];
mStorageSize = new_size;
}
llassert(next_slot < mStorageSize);
return next_slot;
}
static AccumulatorBuffer<ACCUMULATOR>& getDefaultBuffer()
{
static AccumulatorBuffer sBuffer(STATIC_ALLOC);
return sBuffer;
}
private:
ACCUMULATOR* mStorage;
size_t mStorageSize;
size_t mNextStorageSlot;
static LLThreadLocalPointer<ACCUMULATOR> sPrimaryStorage;
};
template<typename ACCUMULATOR> LLThreadLocalPointer<ACCUMULATOR> AccumulatorBuffer<ACCUMULATOR>::sPrimaryStorage;
template<typename ACCUMULATOR>
class LL_COMMON_API TraceType
{
public:
TraceType(const char* name, const char* description = NULL)
: mName(name),
mDescription(description ? description : "")
{
mAccumulatorIndex = AccumulatorBuffer<ACCUMULATOR>::getDefaultBuffer().reserveSlot();
}
LL_FORCE_INLINE ACCUMULATOR& getPrimaryAccumulator()
{
return AccumulatorBuffer<ACCUMULATOR>::getPrimaryStorage()[mAccumulatorIndex];
}
ACCUMULATOR& getAccumulator(AccumulatorBuffer<ACCUMULATOR>* buffer) { return (*buffer)[mAccumulatorIndex]; }
const ACCUMULATOR& getAccumulator(const AccumulatorBuffer<ACCUMULATOR>* buffer) const { return (*buffer)[mAccumulatorIndex]; }
protected:
std::string mName;
std::string mDescription;
size_t mAccumulatorIndex;
};
template<typename T>
class LL_COMMON_API MeasurementAccumulator
{
public:
MeasurementAccumulator()
: mSum(0),
mMin(std::numeric_limits<T>::max()),
mMax(std::numeric_limits<T>::min()),
mMean(0),
mVarianceSum(0),
mNumSamples(0),
mLastValue(0)
{}
LL_FORCE_INLINE void sample(T value)
{
mNumSamples++;
mSum += value;
if (value < mMin)
{
mMin = value;
}
else if (value > mMax)
{
mMax = value;
}
F64 old_mean = mMean;
mMean += ((F64)value - old_mean) / (F64)mNumSamples;
mVarianceSum += ((F64)value - old_mean) * ((F64)value - mMean);
mLastValue = value;
}
void addSamples(const MeasurementAccumulator<T>& other)
{
mSum += other.mSum;
if (other.mMin < mMin)
{
mMin = other.mMin;
}
if (other.mMax > mMax)
{
mMax = other.mMax;
}
mNumSamples += other.mNumSamples;
F64 weight = (F64)mNumSamples / (F64)(mNumSamples + other.mNumSamples);
mMean = mMean * weight + other.mMean * (1.f - weight);
F64 n_1 = (F64)mNumSamples,
n_2 = (F64)other.mNumSamples;
F64 m_1 = mMean,
m_2 = other.mMean;
F64 sd_1 = getStandardDeviation(),
sd_2 = other.getStandardDeviation();
// combine variance (and hence standard deviation) of 2 different sized sample groups using
// the following formula: http://www.mrc-bsu.cam.ac.uk/cochrane/handbook/chapter_7/7_7_3_8_combining_groups.htm
if (n_1 == 0)
{
mVarianceSum = other.mVarianceSum;
}
else if (n_2 == 0)
{
// don't touch variance
// mVarianceSum = mVarianceSum;
}
else
{
mVarianceSum = (F32)mNumSamples
* ((((n_1 - 1.f) * sd_1 * sd_1)
+ ((n_2 - 1.f) * sd_2 * sd_2)
+ (((n_1 * n_2) / (n_1 + n_2))
* ((m_1 * m_1) + (m_2 * m_2) - (2.f * m_1 * m_2))))
/ (n_1 + n_2 - 1.f));
}
mLastValue = other.mLastValue;
}
void reset()
{
mNumSamples = 0;
mSum = 0;
mMin = 0;
mMax = 0;
}
T getSum() const { return mSum; }
T getMin() const { return mMin; }
T getMax() const { return mMax; }
T getLastValue() const { return mLastValue; }
F64 getMean() const { return mMean; }
F64 getStandardDeviation() const { return sqrtf(mVarianceSum / mNumSamples); }
U32 getSampleCount() const { return mNumSamples; }
private:
T mSum,
mMin,
mMax,
mLastValue;
F64 mMean,
mVarianceSum;
U32 mNumSamples;
};
template<typename T>
class LL_COMMON_API CountAccumulator
{
public:
CountAccumulator()
: mSum(0),
mNumSamples(0)
{}
LL_FORCE_INLINE void add(T value)
{
mNumSamples++;
mSum += value;
}
void addSamples(const CountAccumulator<T>& other)
{
mSum += other.mSum;
mNumSamples += other.mNumSamples;
}
void reset()
{
mNumSamples = 0;
mSum = 0;
}
T getSum() const { return mSum; }
private:
T mSum;
U32 mNumSamples;
};
template <typename T = F64, typename IS_UNIT = void>
class LL_COMMON_API Measurement
: public TraceType<MeasurementAccumulator<T> >,
public LLInstanceTracker<Measurement<T, IS_UNIT>, std::string>
{
public:
typedef T storage_t;
typedef T base_unit_t;
Measurement(const char* name, const char* description = NULL)
: TraceType(name),
LLInstanceTracker(name)
{}
void sample(T value)
{
getPrimaryAccumulator().sample(value);
}
};
template <typename T>
class LL_COMMON_API Measurement <T, typename T::is_unit_t>
: public Measurement<typename T::value_t>
{
public:
typedef typename T::storage_t storage_t;
typedef typename T::base_unit_t base_unit_t;
typedef Measurement<typename T::value_t> base_measurement_t;
Measurement(const char* name, const char* description = NULL)
: Measurement<typename T::value_t>(name)
{}
template<typename UNIT_T>
void sample(UNIT_T value)
{
base_measurement_t::sample(((T)value).value());
}
};
template <typename T = F64, typename IS_UNIT = void>
class LL_COMMON_API Count
: public TraceType<CountAccumulator<T> >,
public LLInstanceTracker<Count<T>, std::string>
{
public:
typedef T storage_t;
typedef T base_unit_t;
Count(const char* name, const char* description = NULL)
: TraceType(name),
LLInstanceTracker(name)
{}
void add(T value)
{
getPrimaryAccumulator().add(value);
}
};
template <typename T>
class LL_COMMON_API Count <T, typename T::is_unit_t>
: public Count<typename T::value_t>
{
public:
typedef typename T::storage_t storage_t;
typedef typename T::base_unit_t base_unit_t;
typedef Count<typename T::value_t> base_count_t;
Count(const char* name, const char* description = NULL)
: Count<typename T::value_t>(name)
{}
template<typename UNIT_T>
void add(UNIT_T value)
{
base_count_t::add(((T)value).value());
}
};
class LL_COMMON_API TimerAccumulator
{
public:
U32 mTotalTimeCounter,
mChildTimeCounter,
mCalls;
TimerAccumulator* mParent; // info for caller timer
TimerAccumulator* mLastCaller; // used to bootstrap tree construction
const class BlockTimer* mTimer; // points to block timer associated with this storage
U8 mActiveCount; // number of timers with this ID active on stack
bool mMoveUpTree; // needs to be moved up the tree of timers at the end of frame
std::vector<TimerAccumulator*> mChildren; // currently assumed child timers
void addSamples(const TimerAccumulator& other)
{
mTotalTimeCounter += other.mTotalTimeCounter;
mChildTimeCounter += other.mChildTimeCounter;
mCalls += other.mCalls;
}
void reset()
{
mTotalTimeCounter = 0;
mChildTimeCounter = 0;
mCalls = 0;
}
};
class LL_COMMON_API BlockTimer : public TraceType<TimerAccumulator>
{
public:
BlockTimer(const char* name)
: TraceType(name)
{}
struct Recorder
{
struct StackEntry
{
Recorder* mRecorder;
TimerAccumulator* mAccumulator;
U32 mChildTime;
};
LL_FORCE_INLINE Recorder(BlockTimer& block_timer)
: mLastRecorder(sCurRecorder)
{
mStartTime = getCPUClockCount32();
TimerAccumulator* accumulator = &block_timer.getPrimaryAccumulator(); // get per-thread accumulator
accumulator->mActiveCount++;
accumulator->mCalls++;
accumulator->mMoveUpTree |= (accumulator->mParent->mActiveCount == 0);
// push new timer on stack
sCurRecorder.mRecorder = this;
sCurRecorder.mAccumulator = accumulator;
sCurRecorder.mChildTime = 0;
}
LL_FORCE_INLINE ~Recorder()
{
U32 total_time = getCPUClockCount32() - mStartTime;
TimerAccumulator* accumulator = sCurRecorder.mAccumulator;
accumulator->mTotalTimeCounter += total_time;
accumulator->mChildTimeCounter += sCurRecorder.mChildTime;
accumulator->mActiveCount--;
accumulator->mLastCaller = mLastRecorder.mAccumulator;
mLastRecorder.mChildTime += total_time;
// pop stack
sCurRecorder = mLastRecorder;
}
StackEntry mLastRecorder;
U32 mStartTime;
};
private:
static U32 getCPUClockCount32()
{
U32 ret_val;
__asm
{
_emit 0x0f
_emit 0x31
shr eax,8
shl edx,24
or eax, edx
mov dword ptr [ret_val], eax
}
return ret_val;
}
// return full timer value, *not* shifted by 8 bits
static U64 getCPUClockCount64()
{
U64 ret_val;
__asm
{
_emit 0x0f
_emit 0x31
mov eax,eax
mov edx,edx
mov dword ptr [ret_val+4], edx
mov dword ptr [ret_val], eax
}
return ret_val;
}
static Recorder::StackEntry sCurRecorder;
};
}
#endif // LL_LLTRACE_H
|