summaryrefslogtreecommitdiff
path: root/indra/llcommon/lltrace.h
blob: 355617a89829743ecae7fbd699b0c7b0602aa8b6 (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
/** 
 * @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 "lltraceaccumulators.h"
#include "llthreadlocalstorage.h"
#include "lltimer.h"

#include <list>

namespace LLTrace
{
class Recording;

template<typename T>
T storage_value(T val) { return val; }

template<typename UNIT_TYPE, typename STORAGE_TYPE> 
STORAGE_TYPE storage_value(LLUnit<STORAGE_TYPE, UNIT_TYPE> val) { return val.value(); }

template<typename UNIT_TYPE, typename STORAGE_TYPE> 
STORAGE_TYPE storage_value(LLUnitImplicit<STORAGE_TYPE, UNIT_TYPE> val) { return val.value(); }

class TraceBase
{
public:
	TraceBase(const char* name, const char* description);
	virtual ~TraceBase() {};
	virtual const char* getUnitLabel() const;

	const std::string& getName() const { return mName; }
	const std::string& getDescription() const { return mDescription; }

protected:
	std::string	mName;
	std::string	mDescription;
};

template<typename ACCUMULATOR>
class TraceType 
:	public TraceBase,
	public LLInstanceTracker<TraceType<ACCUMULATOR>, std::string>
{
public:
	TraceType(const char* name, const char* description = NULL)
	:	LLInstanceTracker<TraceType<ACCUMULATOR>, std::string>(name),
		TraceBase(name, description),
		mAccumulatorIndex(AccumulatorBuffer<ACCUMULATOR>::getDefaultBuffer()->reserveSlot())
	{}

	LL_FORCE_INLINE ACCUMULATOR& getCurrentAccumulator() const
	{
		ACCUMULATOR* accumulator_storage = LLThreadLocalSingletonPointer<ACCUMULATOR>::getInstance();
		return accumulator_storage ? accumulator_storage[mAccumulatorIndex] : (*AccumulatorBuffer<ACCUMULATOR>::getDefaultBuffer())[mAccumulatorIndex];
	}

	size_t getIndex() const { return mAccumulatorIndex; }
	static size_t getNumIndices() { return AccumulatorBuffer<ACCUMULATOR>::getNumIndices(); }

protected:
	const size_t		mAccumulatorIndex;
};


template<>
class TraceType<TimeBlockAccumulator::CallCountFacet>
:	public TraceType<TimeBlockAccumulator>
{
public:

	TraceType(const char* name, const char* description = "")
	:	TraceType<TimeBlockAccumulator>(name, description)
	{}
};

template<>
class TraceType<TimeBlockAccumulator::SelfTimeFacet>
	:	public TraceType<TimeBlockAccumulator>
{
public:

	TraceType(const char* name, const char* description = "")
		:	TraceType<TimeBlockAccumulator>(name, description)
	{}
};

template <typename T = F64>
class EventStatHandle
:	public TraceType<EventAccumulator>
{
public:
	typedef F64 storage_t;
	typedef TraceType<EventAccumulator> trace_t;
	typedef EventStatHandle<T> self_t;

	EventStatHandle(const char* name, const char* description = NULL)
	:	trace_t(name, description)
	{}

	/*virtual*/ const char* getUnitLabel() const { return LLGetUnitLabel<T>::getUnitLabel(); }

};

template<typename T, typename VALUE_T>
void record(EventStatHandle<T>& measurement, VALUE_T value)
{
	T converted_value(value);
	measurement.getCurrentAccumulator().record(storage_value(converted_value));
}

template <typename T = F64>
class SampleStatHandle
:	public TraceType<SampleAccumulator>
{
public:
	typedef F64 storage_t;
	typedef TraceType<SampleAccumulator> trace_t;
	typedef SampleStatHandle<T> self_t;

	SampleStatHandle(const char* name, const char* description = NULL)
	:	trace_t(name, description)
	{}

	/*virtual*/ const char* getUnitLabel() const { return LLGetUnitLabel<T>::getUnitLabel(); }
};

template<typename T, typename VALUE_T>
void sample(SampleStatHandle<T>& measurement, VALUE_T value)
{
	T converted_value(value);
	measurement.getCurrentAccumulator().sample(storage_value(converted_value));
}

template <typename T = F64>
class CountStatHandle
:	public TraceType<CountAccumulator>
{
public:
	typedef F64 storage_t;
	typedef TraceType<CountAccumulator> trace_t;
	typedef CountStatHandle<T> self_t;

	CountStatHandle(const char* name, const char* description = NULL) 
	:	trace_t(name, description)
	{}

	/*virtual*/ const char* getUnitLabel() const { return LLGetUnitLabel<T>::getUnitLabel(); }
};

template<typename T, typename VALUE_T>
void add(CountStatHandle<T>& count, VALUE_T value)
{
	T converted_value(value);
	count.getCurrentAccumulator().add(storage_value(converted_value));
}

template<>
class TraceType<MemStatAccumulator::AllocationFacet>
:	public TraceType<MemStatAccumulator>
{
public:

	TraceType(const char* name, const char* description = "")
	:	TraceType<MemStatAccumulator>(name, description)
	{}
};

template<>
class TraceType<MemStatAccumulator::DeallocationFacet>
:	public TraceType<MemStatAccumulator>
{
public:

	TraceType(const char* name, const char* description = "")
	:	TraceType<MemStatAccumulator>(name, description)
	{}
};

template<>
class TraceType<MemStatAccumulator::ShadowAllocationFacet>
	:	public TraceType<MemStatAccumulator>
{
public:

	TraceType(const char* name, const char* description = "")
		:	TraceType<MemStatAccumulator>(name, description)
	{}
};

template<>
class TraceType<MemStatAccumulator::ShadowDeallocationFacet>
	:	public TraceType<MemStatAccumulator>
{
public:

	TraceType(const char* name, const char* description = "")
		:	TraceType<MemStatAccumulator>(name, description)
	{}
};

template<>
class TraceType<MemStatAccumulator::ShadowMemFacet>
	:	public TraceType<MemStatAccumulator>
{
public:

	TraceType(const char* name, const char* description = "")
		:	TraceType<MemStatAccumulator>(name, description)
	{}
};

class MemStatHandle : public TraceType<MemStatAccumulator>
{
public:
	typedef TraceType<MemStatAccumulator> trace_t;
	MemStatHandle(const char* name)
	:	trace_t(name)
	{}

	void setName(const char* name)
	{
		mName = name;
		setKey(name);
	}

	/*virtual*/ const char* getUnitLabel() const { return "KB"; }

	TraceType<MemStatAccumulator::AllocationFacet>& allocations() 
	{ 
		return static_cast<TraceType<MemStatAccumulator::AllocationFacet>&>(*(TraceType<MemStatAccumulator>*)this);
	}

	TraceType<MemStatAccumulator::DeallocationFacet>& deallocations() 
	{ 
		return static_cast<TraceType<MemStatAccumulator::DeallocationFacet>&>(*(TraceType<MemStatAccumulator>*)this);
	}

	TraceType<MemStatAccumulator::ShadowAllocationFacet>& shadowAllocations() 
	{ 
		return static_cast<TraceType<MemStatAccumulator::ShadowAllocationFacet>&>(*(TraceType<MemStatAccumulator>*)this);
	}

	TraceType<MemStatAccumulator::ShadowDeallocationFacet>& shadowDeallocations() 
	{ 
		return static_cast<TraceType<MemStatAccumulator::ShadowDeallocationFacet>&>(*(TraceType<MemStatAccumulator>*)this);
	}

	TraceType<MemStatAccumulator::ShadowMemFacet>& shadowMem() 
	{ 
		return static_cast<TraceType<MemStatAccumulator::ShadowMemFacet>&>(*(TraceType<MemStatAccumulator>*)this);
	}
};

inline void claim_footprint(MemStatHandle& measurement, S32 size)
{
	MemStatAccumulator& accumulator = measurement.getCurrentAccumulator();
	accumulator.mSize.sample(accumulator.mSize.hasValue() ? accumulator.mSize.getLastValue() + (F64)size : (F64)size);
	accumulator.mAllocated.add(1);
}

inline void disclaim_footprint(MemStatHandle& measurement, S32 size)
{
	MemStatAccumulator& accumulator = measurement.getCurrentAccumulator();
	accumulator.mSize.sample(accumulator.mSize.hasValue() ? accumulator.mSize.getLastValue() - (F64)size : -(F64)size);
	accumulator.mDeallocated.add(1);
}

inline void claim_shadow(MemStatHandle& measurement, S32 size)
{
	MemStatAccumulator& accumulator = measurement.getCurrentAccumulator();
	accumulator.mShadowSize.sample(accumulator.mSize.hasValue() ? accumulator.mSize.getLastValue() + (F64)size : (F64)size);
	accumulator.mShadowAllocated.add(1);
}

inline void disclaim_shadow(MemStatHandle& measurement, S32 size)
{
	MemStatAccumulator& accumulator = measurement.getCurrentAccumulator();
	accumulator.mShadowSize.sample(accumulator.mSize.hasValue() ? accumulator.mSize.getLastValue() - (F64)size : -(F64)size);
	accumulator.mShadowDeallocated.add(1);
}

// measures effective memory footprint of specified type
// specialize to cover different types

template<typename T, typename IS_MEM_TRACKABLE = void>
struct MeasureMem
{
	static size_t measureFootprint(const T& value)
	{
		return sizeof(T);
	}

	static size_t measureFootprint()
	{
		return sizeof(T);
	}

	static size_t measureShadow(const T& value)
	{
		return 0;
	}

	static size_t measureShadow()
	{
		return 0;
	}
};

template<typename T>
struct MeasureMem<T, typename T::mem_trackable_tag_t>
{
	static size_t measureFootprint(const T& value)
	{
		return sizeof(T) + value.getMemFootprint();
	}

	static size_t measureFootprint()
	{
		return sizeof(T);
	}

	static size_t measureShadow(const T& value)
	{
		return value.getMemShadow();
	}

	static size_t measureShadow()
	{
		return MeasureMem<T>::measureShadow();
	}
};


template<typename T, typename IS_MEM_TRACKABLE>
struct MeasureMem<T*, IS_MEM_TRACKABLE>
{
	static size_t measureFootprint(const T* value)
	{
		if (!value)
		{
			return 0;
		}
		return MeasureMem<T>::measureFootprint(*value);
	}

	static size_t measureFootprint()
	{
		return MeasureMem<T>::measureFootprint();
	}

	static size_t measureShadow(const T* value)
	{
		return MeasureMem<T>::measureShadow(*value);
	}

	static size_t measureShadow()
	{
		return MeasureMem<T>::measureShadow();
	}
};

template<typename T, typename IS_MEM_TRACKABLE>
struct MeasureMem<std::basic_string<T>, IS_MEM_TRACKABLE>
{
	static size_t measureFootprint(const std::basic_string<T>& value)
	{
		return value.capacity() * sizeof(T);
	}

	static size_t measureFootprint()
	{
		return sizeof(std::basic_string<T>);
	}

	static size_t measureShadow(const std::basic_string<T>& value)
	{
		return 0;
	}

	static size_t measureShadow()
	{
		return 0;
	}
};

template<typename DERIVED, size_t ALIGNMENT = LL_DEFAULT_HEAP_ALIGN>
class MemTrackable
{
public:
	typedef void mem_trackable_tag_t;

	enum EMemType
	{
		MEM_FOOTPRINT, 
		MEM_SHADOW
	};

	MemTrackable()
	:	mMemFootprint(0),
		mMemShadow(0)
	{
		static bool name_initialized = false;
		if (!name_initialized)
		{
			name_initialized = true;
			sMemStat.setName(typeid(DERIVED).name());
		}
	}

	virtual ~MemTrackable()
	{
		disclaimMem(mMemFootprint, MEM_FOOTPRINT);
		disclaimMem(mMemShadow, MEM_SHADOW);
	}

	static MemStatHandle& getMemStatHandle()
	{
		return sMemStat;
	}

	S32 getMemFootprint() const	{ return mMemFootprint; }
	S32 getMemShadow() const	{ return mMemShadow; }

	void* operator new(size_t size) 
	{
		claim_footprint(sMemStat, size);
		return ll_aligned_malloc(ALIGNMENT, size);
	}

	void operator delete(void* ptr, size_t size)
	{
		disclaim_footprint(sMemStat, size);
		ll_aligned_free(ALIGNMENT, ptr);
	}

	void *operator new [](size_t size)
	{
		claim_footprint(sMemStat, size);
		return ll_aligned_malloc(ALIGNMENT, size);
	}

	void operator delete[](void* ptr, size_t size)
	{
		disclaim_footprint(sMemStat, size);
		ll_aligned_free(ALIGNMENT, ptr);
	}

	// claim memory associated with other objects/data as our own, adding to our calculated footprint
	template<typename CLAIM_T>
	CLAIM_T& claimMem(CLAIM_T& value, EMemType mem_type = MEM_FOOTPRINT)
	{
		trackAlloc(MeasureMem<CLAIM_T>::measureFootprint(value), mem_type);
		trackAlloc(MeasureMem<CLAIM_T>::measureShadow(value), MEM_SHADOW);
		return value;
	}

	template<typename CLAIM_T>
	const CLAIM_T& claimMem(const CLAIM_T& value, EMemType mem_type = MEM_FOOTPRINT)
	{
		trackAlloc(MeasureMem<CLAIM_T>::measureFootprint(value), mem_type);
		trackAlloc(MeasureMem<CLAIM_T>::measureShadow(value), MEM_SHADOW);
		return value;
	}

	size_t& claimMem(size_t& size, EMemType mem_type = MEM_FOOTPRINT)
	{
		trackAlloc(size, mem_type);
		return size;
	}

	S32& claimMem(S32& size, EMemType mem_type = MEM_FOOTPRINT)
	{
		trackAlloc(size, mem_type);
		return size;
	}

	// remove memory we had claimed from our calculated footprint
	template<typename CLAIM_T>
	CLAIM_T& disclaimMem(CLAIM_T& value, EMemType mem_type = MEM_FOOTPRINT)
	{
		trackDealloc(MeasureMem<CLAIM_T>::measureFootprint(value), mem_type);
		trackDealloc(MeasureMem<CLAIM_T>::measureShadow(value), MEM_SHADOW);
		return value;
	}

	template<typename CLAIM_T>
	const CLAIM_T& disclaimMem(const CLAIM_T& value, EMemType mem_type = MEM_FOOTPRINT)
	{
		trackDealloc(MeasureMem<CLAIM_T>::measureFootprint(value), mem_type);
		trackDealloc(MeasureMem<CLAIM_T>::measureShadow(value), MEM_SHADOW);
		return value;
	}

	size_t& disclaimMem(size_t& size, EMemType mem_type = MEM_FOOTPRINT)
	{
		trackDealloc(size, mem_type);
		return size;
	}

	S32& disclaimMem(S32& size, EMemType mem_type = MEM_FOOTPRINT)
	{
		trackDealloc(size, mem_type);
		return size;
	}

private:

	void trackAlloc(S32 size, EMemType mem_type)
	{
		if (mem_type == MEM_FOOTPRINT)
		{
			claim_footprint(sMemStat, size);
			mMemFootprint += size;
		}
		else
		{
			claim_shadow(sMemStat, size);
			mMemShadow += size;
		}
	}

	void trackDealloc(S32 size, EMemType mem_type)
	{
		if (mem_type == MEM_FOOTPRINT)
		{
			disclaim_footprint(sMemStat, size);
			mMemFootprint -= size;
		}
		else
		{
			disclaim_shadow(sMemStat, size);
			mMemShadow -= size;
		}
	}

private:
	// use signed values so that we can temporarily go negative
	// and reconcile in destructor
	// NB: this assumes that no single class is responsible for > 2GB of allocations
	S32 mMemFootprint,
		mMemShadow;
	
	static	MemStatHandle	sMemStat;
};

template<typename DERIVED, size_t ALIGNMENT>
MemStatHandle MemTrackable<DERIVED, ALIGNMENT>::sMemStat("");

}
#endif // LL_LLTRACE_H