summaryrefslogtreecommitdiff
path: root/indra/llcommon/lltracerecording.h
blob: 13dffdc7015ac7a7952309737a73ebc0e0f62837 (plain)
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
/** 
 * @file lltracerecording.h
 * @brief Sampling object for collecting runtime statistics originating from lltrace.
 *
 * $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_LLTRACERECORDING_H
#define LL_LLTRACERECORDING_H

#include "stdtypes.h"
#include "llpreprocessor.h"

#include "llpointer.h"
#include "lltimer.h"
#include "lltraceaccumulators.h"

class LLStopWatchControlsMixinCommon
{
public:
	virtual ~LLStopWatchControlsMixinCommon() {}

	enum EPlayState
	{
		STOPPED,
		PAUSED,
		STARTED
	};

	void start();
	void stop();
	void pause();
	void resume();
	void restart();
	void reset();

	bool isStarted() const { return mPlayState == STARTED; }
	bool isPaused() const  { return mPlayState == PAUSED; }
	bool isStopped() const { return mPlayState == STOPPED; }
	EPlayState getPlayState() const { return mPlayState; }
	// force play state to specific value by calling appropriate handle* methods
	void setPlayState(EPlayState state);

protected:
	LLStopWatchControlsMixinCommon()
	:	mPlayState(STOPPED)
	{}

private:
	// trigger active behavior (without reset)
	virtual void handleStart() = 0;
	// stop active behavior
	virtual void handleStop() = 0;
	// clear accumulated state, can be called while started
	virtual void handleReset() = 0;

	EPlayState mPlayState;
};

template<typename DERIVED>
class LLStopWatchControlsMixin
:	public LLStopWatchControlsMixinCommon
{
public:

	typedef LLStopWatchControlsMixin<DERIVED> self_t;
	virtual void splitTo(DERIVED& other)
	{
		EPlayState play_state = getPlayState();
		stop();
		other.reset();

		handleSplitTo(other);

		other.setPlayState(play_state);
	}

	virtual void splitFrom(DERIVED& other)
	{
		static_cast<self_t&>(other).handleSplitTo(*static_cast<DERIVED*>(this));
	}
private:
	self_t& operator = (const self_t& other)
	{
		// don't do anything, derived class must implement logic
	}

	// atomically stop this object while starting the other
	// no data can be missed in between stop and start
	virtual void handleSplitTo(DERIVED& other) {};

};

namespace LLTrace
{
	template<typename T>
	class TraceType;

	template<typename T>
	class CountStatHandle;

	template<typename T>
	class SampleStatHandle;

	template<typename T>
	class EventStatHandle;

	class MemStatHandle;

	template<typename T>
	struct RelatedTypes
	{
		typedef F64 fractional_t;
		typedef T	sum_t;
	};

	template<typename T, typename UNIT_T>
	struct RelatedTypes<LLUnit<T, UNIT_T> >
	{
		typedef LLUnit<typename RelatedTypes<T>::fractional_t, UNIT_T> fractional_t;
		typedef LLUnit<typename RelatedTypes<T>::sum_t, UNIT_T> sum_t;
	};

	template<>
	struct RelatedTypes<bool>
	{
		typedef F64 fractional_t;
		typedef S32 sum_t;
	};

	class Recording 
	:	public LLStopWatchControlsMixin<Recording>
	{
	public:
		Recording(EPlayState state = LLStopWatchControlsMixinCommon::STOPPED);

		Recording(const Recording& other);
		~Recording();

		Recording& operator = (const Recording& other);

		// accumulate data from subsequent, non-overlapping recording
		void appendRecording(Recording& other);

		// grab latest recorded data
		void update();

		// ensure that buffers are exclusively owned by this recording
		void makeUnique() { mBuffers.makeUnique(); }

		// Timer accessors
		F64Seconds getSum(const TraceType<TimeBlockAccumulator>& stat);
		F64Seconds getSum(const TraceType<TimeBlockAccumulator::SelfTimeFacet>& stat);
		S32 getSum(const TraceType<TimeBlockAccumulator::CallCountFacet>& stat);

		F64Seconds getPerSec(const TraceType<TimeBlockAccumulator>& stat);
		F64Seconds getPerSec(const TraceType<TimeBlockAccumulator::SelfTimeFacet>& stat);
		F32 getPerSec(const TraceType<TimeBlockAccumulator::CallCountFacet>& stat);

		// Memory accessors
		bool hasValue(const TraceType<MemStatAccumulator>& stat);
		bool hasValue(const TraceType<MemStatAccumulator::ShadowMemFacet>& stat);

		F64Kilobytes getMin(const TraceType<MemStatAccumulator>& stat);
		F64Kilobytes getMean(const TraceType<MemStatAccumulator>& stat);
		F64Kilobytes getMax(const TraceType<MemStatAccumulator>& stat);
		F64Kilobytes getStandardDeviation(const TraceType<MemStatAccumulator>& stat);
		F64Kilobytes getLastValue(const TraceType<MemStatAccumulator>& stat);

		F64Kilobytes getMin(const TraceType<MemStatAccumulator::ShadowMemFacet>& stat);
		F64Kilobytes getMean(const TraceType<MemStatAccumulator::ShadowMemFacet>& stat);
		F64Kilobytes getMax(const TraceType<MemStatAccumulator::ShadowMemFacet>& stat);
		F64Kilobytes getStandardDeviation(const TraceType<MemStatAccumulator::ShadowMemFacet>& stat);
		F64Kilobytes getLastValue(const TraceType<MemStatAccumulator::ShadowMemFacet>& stat);

		F64Kilobytes getSum(const TraceType<MemStatAccumulator::AllocationFacet>& stat);
		F64Kilobytes getPerSec(const TraceType<MemStatAccumulator::AllocationFacet>& stat);
		S32 getSampleCount(const TraceType<MemStatAccumulator::AllocationFacet>& stat);

		F64Kilobytes getSum(const TraceType<MemStatAccumulator::DeallocationFacet>& stat);
		F64Kilobytes getPerSec(const TraceType<MemStatAccumulator::DeallocationFacet>& stat);
		S32 getSampleCount(const TraceType<MemStatAccumulator::DeallocationFacet>& stat);

		F64Kilobytes getSum(const TraceType<MemStatAccumulator::ShadowAllocationFacet>& stat);
		F64Kilobytes getPerSec(const TraceType<MemStatAccumulator::ShadowAllocationFacet>& stat);
		S32 getSampleCount(const TraceType<MemStatAccumulator::ShadowAllocationFacet>& stat);

		F64Kilobytes getSum(const TraceType<MemStatAccumulator::ShadowDeallocationFacet>& stat);
		F64Kilobytes getPerSec(const TraceType<MemStatAccumulator::ShadowDeallocationFacet>& stat);
		S32 getSampleCount(const TraceType<MemStatAccumulator::ShadowDeallocationFacet>& stat);

		// CountStatHandle accessors
		F64 getSum(const TraceType<CountAccumulator>& stat);
		template <typename T>
		typename RelatedTypes<T>::sum_t getSum(const CountStatHandle<T>& stat)
		{
			return (typename RelatedTypes<T>::sum_t)getSum(static_cast<const TraceType<CountAccumulator>&> (stat));
		}

		F64 getPerSec(const TraceType<CountAccumulator>& stat);
		template <typename T>
		typename RelatedTypes<T>::fractional_t getPerSec(const CountStatHandle<T>& stat)
		{
			return (typename RelatedTypes<T>::fractional_t)getPerSec(static_cast<const TraceType<CountAccumulator>&> (stat));
		}

		S32 getSampleCount(const TraceType<CountAccumulator>& stat);


		// SampleStatHandle accessors
		bool hasValue(const TraceType<SampleAccumulator>& stat);

		F64 getMin(const TraceType<SampleAccumulator>& stat);
		template <typename T>
		T getMin(const SampleStatHandle<T>& stat)
		{
			return (T)getMin(static_cast<const TraceType<SampleAccumulator>&> (stat));
		}

		F64 getMax(const TraceType<SampleAccumulator>& stat);
		template <typename T>
		T getMax(const SampleStatHandle<T>& stat)
		{
			return (T)getMax(static_cast<const TraceType<SampleAccumulator>&> (stat));
		}

		F64 getMean(const TraceType<SampleAccumulator>& stat);
		template <typename T>
		typename RelatedTypes<T>::fractional_t getMean(SampleStatHandle<T>& stat)
		{
			return (typename RelatedTypes<T>::fractional_t)getMean(static_cast<const TraceType<SampleAccumulator>&> (stat));
		}

		F64 getStandardDeviation(const TraceType<SampleAccumulator>& stat);
		template <typename T>
		typename RelatedTypes<T>::fractional_t getStandardDeviation(const SampleStatHandle<T>& stat)
		{
			return (typename RelatedTypes<T>::fractional_t)getStandardDeviation(static_cast<const TraceType<SampleAccumulator>&> (stat));
		}

		F64 getLastValue(const TraceType<SampleAccumulator>& stat);
		template <typename T>
		T getLastValue(const SampleStatHandle<T>& stat)
		{
			return (T)getLastValue(static_cast<const TraceType<SampleAccumulator>&> (stat));
		}

		S32 getSampleCount(const TraceType<SampleAccumulator>& stat);

		// EventStatHandle accessors
		bool hasValue(const TraceType<EventAccumulator>& stat);

		F64 getSum(const TraceType<EventAccumulator>& stat);
		template <typename T>
		typename RelatedTypes<T>::sum_t getSum(const EventStatHandle<T>& stat)
		{
			return (typename RelatedTypes<T>::sum_t)getSum(static_cast<const TraceType<EventAccumulator>&> (stat));
		}

		F64 getMin(const TraceType<EventAccumulator>& stat);
		template <typename T>
		T getMin(const EventStatHandle<T>& stat)
		{
			return (T)getMin(static_cast<const TraceType<EventAccumulator>&> (stat));
		}

		F64 getMax(const TraceType<EventAccumulator>& stat);
		template <typename T>
		T getMax(const EventStatHandle<T>& stat)
		{
			return (T)getMax(static_cast<const TraceType<EventAccumulator>&> (stat));
		}

		F64 getMean(const TraceType<EventAccumulator>& stat);
		template <typename T>
		typename RelatedTypes<T>::fractional_t getMean(EventStatHandle<T>& stat)
		{
			return (typename RelatedTypes<T>::fractional_t)getMean(static_cast<const TraceType<EventAccumulator>&> (stat));
		}

		F64 getStandardDeviation(const TraceType<EventAccumulator>& stat);
		template <typename T>
		typename RelatedTypes<T>::fractional_t getStandardDeviation(const EventStatHandle<T>& stat)
		{
			return (typename RelatedTypes<T>::fractional_t)getStandardDeviation(static_cast<const TraceType<EventAccumulator>&> (stat));
		}

		F64 getLastValue(const TraceType<EventAccumulator>& stat);
		template <typename T>
		T getLastValue(const EventStatHandle<T>& stat)
		{
			return (T)getLastValue(static_cast<const TraceType<EventAccumulator>&> (stat));
		}

		S32 getSampleCount(const TraceType<EventAccumulator>& stat);

		F64Seconds getDuration() const { return mElapsedSeconds; }

	protected:
		friend class ThreadRecorder;

		// implementation for LLStopWatchControlsMixin
		/*virtual*/ void handleStart();
		/*virtual*/ void handleStop();
		/*virtual*/ void handleReset();
		/*virtual*/ void handleSplitTo(Recording& other);

		// returns data for current thread
		class ThreadRecorder* getThreadRecorder(); 

		LLTimer											mSamplingTimer;
		F64Seconds					mElapsedSeconds;
		LLCopyOnWritePointer<AccumulatorBufferGroup>	mBuffers;
		bool											mInHandOff;

	};

	class LL_COMMON_API PeriodicRecording
	:	public LLStopWatchControlsMixin<PeriodicRecording>
	{
	public:
		PeriodicRecording(S32 num_periods, EPlayState state = STOPPED);

		void nextPeriod();
		S32 getNumRecordedPeriods() { return mNumPeriods; }

		F64Seconds getDuration() const;

		void appendPeriodicRecording(PeriodicRecording& other);
		void appendRecording(Recording& recording);
		Recording& getLastRecording();
		const Recording& getLastRecording() const;
		Recording& getCurRecording();
		const Recording& getCurRecording() const;
		Recording& getPrevRecording(S32 offset);
		const Recording& getPrevRecording(S32 offset) const;
		Recording snapshotCurRecording() const;

		template <typename T>
		S32 getSampleCount(const TraceType<T>& stat, S32 num_periods = S32_MAX)
        {
			S32 total_periods = mNumPeriods;
			num_periods = llmin(num_periods, isStarted() ? total_periods - 1 : total_periods);

            S32 num_samples = 0;
			for (S32 i = 1; i <= num_periods; i++)
			{
				Recording& recording = getPrevRecording(i);
				num_samples += recording.getSampleCount(stat);
			}
			return num_samples;
        }
        
		//
		// PERIODIC MIN
		//

		// catch all for stats that have a defined sum
		template <typename T>
		typename T::value_t getPeriodMin(const TraceType<T>& stat, S32 num_periods = S32_MAX)
		{
			S32 total_periods = mNumPeriods;
			num_periods = llmin(num_periods, isStarted() ? total_periods - 1 : total_periods);

			typename T::value_t min_val = std::numeric_limits<typename T::value_t>::max();
			for (S32 i = 1; i <= num_periods; i++)
			{
				Recording& recording = getPrevRecording(i);
				min_val = llmin(min_val, recording.getSum(stat));
			}
			return min_val;
		}

		template<typename T>
		T getPeriodMin(const CountStatHandle<T>& stat, S32 num_periods = S32_MAX)
		{
			return T(getPeriodMin(static_cast<const TraceType<CountAccumulator>&>(stat), num_periods));
		}

		F64 getPeriodMin(const TraceType<SampleAccumulator>& stat, S32 num_periods = S32_MAX);
		template<typename T>
		T getPeriodMin(const SampleStatHandle<T>& stat, S32 num_periods = S32_MAX)
		{
			return T(getPeriodMin(static_cast<const TraceType<SampleAccumulator>&>(stat), num_periods));
		}

		F64 getPeriodMin(const TraceType<EventAccumulator>& stat, S32 num_periods = S32_MAX);
		template<typename T>
		T getPeriodMin(const EventStatHandle<T>& stat, S32 num_periods = S32_MAX)
		{
			return T(getPeriodMin(static_cast<const TraceType<EventAccumulator>&>(stat), num_periods));
		}

		F64Kilobytes getPeriodMin(const TraceType<MemStatAccumulator>& stat, S32 num_periods = S32_MAX);
		F64Kilobytes getPeriodMin(const MemStatHandle& stat, S32 num_periods = S32_MAX);

		template <typename T>
		typename RelatedTypes<typename T::value_t>::fractional_t getPeriodMinPerSec(const TraceType<T>& stat, S32 num_periods = S32_MAX)
		{
			S32 total_periods = mNumPeriods;
			num_periods = llmin(num_periods, isStarted() ? total_periods - 1 : total_periods);

			typename RelatedTypes<typename T::value_t>::fractional_t min_val = std::numeric_limits<F64>::max();
			for (S32 i = 1; i <= num_periods; i++)
			{
				Recording& recording = getPrevRecording(i);
				min_val = llmin(min_val, recording.getPerSec(stat));
			}
			return (typename RelatedTypes<typename T::value_t>::fractional_t) min_val;
		}

		template<typename T>
		typename RelatedTypes<T>::fractional_t getPeriodMinPerSec(const CountStatHandle<T>& stat, S32 num_periods = S32_MAX)
		{
			return typename RelatedTypes<T>::fractional_t(getPeriodMinPerSec(static_cast<const TraceType<CountAccumulator>&>(stat), num_periods));
		}

		//
		// PERIODIC MAX
		//

		// catch all for stats that have a defined sum
		template <typename T>
		typename T::value_t getPeriodMax(const TraceType<T>& stat, S32 num_periods = S32_MAX)
		{
			S32 total_periods = mNumPeriods;
			num_periods = llmin(num_periods, isStarted() ? total_periods - 1 : total_periods);

			typename T::value_t max_val = std::numeric_limits<typename T::value_t>::min();
			for (S32 i = 1; i <= num_periods; i++)
			{
				Recording& recording = getPrevRecording(i);
				max_val = llmax(max_val, recording.getSum(stat));
			}
			return max_val;
		}

		template<typename T>
		T getPeriodMax(const CountStatHandle<T>& stat, S32 num_periods = S32_MAX)
		{
			return T(getPeriodMax(static_cast<const TraceType<CountAccumulator>&>(stat), num_periods));
		}

		F64 getPeriodMax(const TraceType<SampleAccumulator>& stat, S32 num_periods = S32_MAX);
		template<typename T>
		T getPeriodMax(const SampleStatHandle<T>& stat, S32 num_periods = S32_MAX)
		{
			return T(getPeriodMax(static_cast<const TraceType<SampleAccumulator>&>(stat), num_periods));
		}

		F64 getPeriodMax(const TraceType<EventAccumulator>& stat, S32 num_periods = S32_MAX);
		template<typename T>
		T getPeriodMax(const EventStatHandle<T>& stat, S32 num_periods = S32_MAX)
		{
			return T(getPeriodMax(static_cast<const TraceType<EventAccumulator>&>(stat), num_periods));
		}

		F64Kilobytes getPeriodMax(const TraceType<MemStatAccumulator>& stat, S32 num_periods = S32_MAX);
		F64Kilobytes getPeriodMax(const MemStatHandle& stat, S32 num_periods = S32_MAX);

		template <typename T>
		typename RelatedTypes<typename T::value_t>::fractional_t getPeriodMaxPerSec(const TraceType<T>& stat, S32 num_periods = S32_MAX)
		{
			S32 total_periods = mNumPeriods;
			num_periods = llmin(num_periods, isStarted() ? total_periods - 1 : total_periods);

			F64 max_val = std::numeric_limits<F64>::min();
			for (S32 i = 1; i <= num_periods; i++)
			{
				Recording& recording = getPrevRecording(i);
				max_val = llmax(max_val, recording.getPerSec(stat));
			}
			return (typename RelatedTypes<typename T::value_t>::fractional_t)max_val;
		}

		template<typename T>
		typename RelatedTypes<T>::fractional_t getPeriodMaxPerSec(const CountStatHandle<T>& stat, S32 num_periods = S32_MAX)
		{
			return typename RelatedTypes<T>::fractional_t(getPeriodMaxPerSec(static_cast<const TraceType<CountAccumulator>&>(stat), num_periods));
		}

		//
		// PERIODIC MEAN
		//

		// catch all for stats that have a defined sum
		template <typename T>
		typename RelatedTypes<typename T::value_t>::fractional_t getPeriodMean(const TraceType<T >& stat, S32 num_periods = S32_MAX)
		{
			S32 total_periods = mNumPeriods;
			num_periods = llmin(num_periods, isStarted() ? total_periods - 1 : total_periods);

			typename RelatedTypes<typename T::value_t>::fractional_t mean(0);

			for (S32 i = 1; i <= num_periods; i++)
			{
				Recording& recording = getPrevRecording(i);
				if (recording.getDuration() > (F32Seconds)0.f)
				{
					mean += recording.getSum(stat);
				}
			}
			return (num_periods
				? typename RelatedTypes<typename T::value_t>::fractional_t(mean / num_periods)
				: typename RelatedTypes<typename T::value_t>::fractional_t(NaN));
		}

		template<typename T>
		typename RelatedTypes<T>::fractional_t getPeriodMean(const CountStatHandle<T>& stat, S32 num_periods = S32_MAX)
		{
			return typename RelatedTypes<T>::fractional_t(getPeriodMean(static_cast<const TraceType<CountAccumulator>&>(stat), num_periods));
		}
		F64 getPeriodMean(const TraceType<SampleAccumulator>& stat, S32 num_periods = S32_MAX);
		template<typename T> 
		typename RelatedTypes<T>::fractional_t getPeriodMean(const SampleStatHandle<T>& stat, S32 num_periods = S32_MAX)
		{
			return typename RelatedTypes<T>::fractional_t(getPeriodMean(static_cast<const TraceType<SampleAccumulator>&>(stat), num_periods));
		}

		F64 getPeriodMean(const TraceType<EventAccumulator>& stat, S32 num_periods = S32_MAX);
		template<typename T>
		typename RelatedTypes<T>::fractional_t getPeriodMean(const EventStatHandle<T>& stat, S32 num_periods = S32_MAX)
		{
			return typename RelatedTypes<T>::fractional_t(getPeriodMean(static_cast<const TraceType<EventAccumulator>&>(stat), num_periods));
		}

		F64Kilobytes getPeriodMean(const TraceType<MemStatAccumulator>& stat, S32 num_periods = S32_MAX);
		F64Kilobytes getPeriodMean(const MemStatHandle& stat, S32 num_periods = S32_MAX);
		
		template <typename T>
		typename RelatedTypes<typename T::value_t>::fractional_t getPeriodMeanPerSec(const TraceType<T>& stat, S32 num_periods = S32_MAX)
		{
			S32 total_periods = mNumPeriods;
			num_periods = llmin(num_periods, isStarted() ? total_periods - 1 : total_periods);

			typename RelatedTypes<typename T::value_t>::fractional_t mean = 0;

			for (S32 i = 1; i <= num_periods; i++)
			{
				Recording& recording = getPrevRecording(i);
				if (recording.getDuration() > (F32Seconds)0.f)
				{
					mean += recording.getPerSec(stat);
				}
			}

			return (num_periods
				? typename RelatedTypes<typename T::value_t>::fractional_t(mean / num_periods)
				: typename RelatedTypes<typename T::value_t>::fractional_t(NaN));
		}

		template<typename T>
		typename RelatedTypes<T>::fractional_t getPeriodMeanPerSec(const CountStatHandle<T>& stat, S32 num_periods = S32_MAX)
		{
			return typename RelatedTypes<T>::fractional_t(getPeriodMeanPerSec(static_cast<const TraceType<CountAccumulator>&>(stat), num_periods));
		}

		//
		// PERIODIC STANDARD DEVIATION
		//

		F64 getPeriodStandardDeviation(const TraceType<SampleAccumulator>& stat, S32 num_periods = S32_MAX);

		template<typename T> 
		typename RelatedTypes<T>::fractional_t getPeriodStandardDeviation(const SampleStatHandle<T>& stat, S32 num_periods = S32_MAX)
		{
			return typename RelatedTypes<T>::fractional_t(getPeriodStandardDeviation(static_cast<const TraceType<SampleAccumulator>&>(stat), num_periods));
		}

		F64 getPeriodStandardDeviation(const TraceType<EventAccumulator>& stat, S32 num_periods = S32_MAX);
		template<typename T>
		typename RelatedTypes<T>::fractional_t getPeriodStandardDeviation(const EventStatHandle<T>& stat, S32 num_periods = S32_MAX)
		{
			return typename RelatedTypes<T>::fractional_t(getPeriodStandardDeviation(static_cast<const TraceType<EventAccumulator>&>(stat), num_periods));
		}

		F64Kilobytes getPeriodStandardDeviation(const TraceType<MemStatAccumulator>& stat, S32 num_periods = S32_MAX);
		F64Kilobytes getPeriodStandardDeviation(const MemStatHandle& stat, S32 num_periods = S32_MAX);

	private:
		// implementation for LLStopWatchControlsMixin
		/*virtual*/ void handleStart();
		/*virtual*/ void handleStop();
		/*virtual*/ void handleReset();
		/*virtual*/ void handleSplitTo(PeriodicRecording& other);

	private:
		std::vector<Recording>	mRecordingPeriods;
		const bool				mAutoResize;
		S32						mCurPeriod;
		S32						mNumPeriods;
	};

	PeriodicRecording& get_frame_recording();

	class ExtendableRecording
	:	public LLStopWatchControlsMixin<ExtendableRecording>
	{
	public:
		void extend();

		Recording& getAcceptedRecording() { return mAcceptedRecording; }
		const Recording& getAcceptedRecording() const {return mAcceptedRecording;}

		Recording& getPotentialRecording()				{ return mPotentialRecording; }
		const Recording& getPotentialRecording() const	{ return mPotentialRecording;}

	private:
		// implementation for LLStopWatchControlsMixin
		/*virtual*/ void handleStart();
		/*virtual*/ void handleStop();
		/*virtual*/ void handleReset();
		/*virtual*/ void handleSplitTo(ExtendableRecording& other);

	private:
		Recording mAcceptedRecording;
		Recording mPotentialRecording;
	};

	class ExtendablePeriodicRecording
	:	public LLStopWatchControlsMixin<ExtendablePeriodicRecording>
	{
	public:
		ExtendablePeriodicRecording();
		void extend();

		PeriodicRecording& getResults()				{ return mAcceptedRecording; }
		const PeriodicRecording& getResults() const	{return mAcceptedRecording;}
		
		void nextPeriod() { mPotentialRecording.nextPeriod(); }

	private:
		// implementation for LLStopWatchControlsMixin
		/*virtual*/ void handleStart();
		/*virtual*/ void handleStop();
		/*virtual*/ void handleReset();
		/*virtual*/ void handleSplitTo(ExtendablePeriodicRecording& other);

	private:
		PeriodicRecording mAcceptedRecording;
		PeriodicRecording mPotentialRecording;
	};
}

#endif // LL_LLTRACERECORDING_H