summaryrefslogtreecommitdiff
path: root/tier0/vprof.cpp
blob: 0ef667ccfcb0f4f5c81be42a3a46f5ea73c13bea (plain) (blame)
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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Real-Time Hierarchical Profiling
//
// $NoKeywords: $
//===========================================================================//

#include "pch_tier0.h"

#include "tier0/memalloc.h"
#include "tier0/valve_off.h"

#if defined(_WIN32) && !defined(_X360)
#define WIN_32_LEAN_AND_MEAN
#include <windows.h>
#endif

#include <assert.h>

#ifdef _WIN32
#pragma warning(disable:4073)
#pragma init_seg( lib )
#endif

#pragma warning(push, 1)
#pragma warning(disable:4786)
#pragma warning(disable:4530)
#include <map>
#include <vector>
#include <algorithm>
#pragma warning(pop)

#include "tier0/valve_on.h"
#include "tier0/vprof.h"
#include "tier0/l2cache.h"
#include "tier0/tslist.h"
#include "tier0/dynfunction.h"

#ifdef _X360

#include "xbox/xbox_console.h"

#else // NOT _X360:

#include "tier0/memdbgon.h"

#endif

// NOTE: Explicitly and intentionally using STL in here to not generate any
// cyclical dependencies between the low-level debug library and the higher
// level data structures (toml 01-27-03)
using namespace std;

#ifdef VPROF_ENABLED


#if defined(_X360) && !defined(_CERT)  // enable PIX CPU trace:
#include "tracerecording.h"
#pragma comment( lib, "tracerecording.lib" )
#pragma comment( lib, "xbdm.lib" )
#endif

//-----------------------------------------------------------------------------
bool g_VProfSignalSpike;

//-----------------------------------------------------------------------------

CVProfile g_VProfCurrentProfile;

int CVProfNode::s_iCurrentUniqueNodeID = 0;

CVProfNode::~CVProfNode()
{
#if !defined( _WIN32 ) && !defined( POSIX )
	delete m_pChild;
	delete m_pSibling;
#endif
}

CVProfNode *CVProfNode::GetSubNode( const tchar *pszName, int detailLevel, const tchar *pBudgetGroupName, int budgetFlags )
{
	// Try to find this sub node
	CVProfNode * child = m_pChild;
	while ( child ) 
	{
		if ( child->m_pszName == pszName ) 
		{
			return child;
		}
		child = child->m_pSibling;
	}

	// We didn't find it, so add it
	CVProfNode * node = new CVProfNode( pszName, detailLevel, this, pBudgetGroupName, budgetFlags );
	node->m_pSibling = m_pChild;
	m_pChild = node;
	return node;
}

CVProfNode *CVProfNode::GetSubNode( const tchar *pszName, int detailLevel, const tchar *pBudgetGroupName )
{
	return GetSubNode( pszName, detailLevel, pBudgetGroupName, BUDGETFLAG_OTHER );
}

//-------------------------------------

void CVProfNode::EnterScope()
{
	m_nCurFrameCalls++;
	if ( m_nRecursions++ == 0 ) 
	{
		m_Timer.Start();
#ifndef _X360
		if ( g_VProfCurrentProfile.UsePME() )
		{
			m_L2Cache.Start();
		}
#else // 360 code:
		if ( g_VProfCurrentProfile.UsePME() || ((m_iBitFlags & kRecordL2) != 0) ) 
		{
			m_PMCData.Start();
		}

		if ( (m_iBitFlags & kCPUTrace) != 0)
		{
			// this node is to be recorded. Which recording mode are we in?
			switch ( g_VProfCurrentProfile.GetCPUTraceMode() )
			{
			case CVProfile::kFirstHitNode:
			case CVProfile::kAllNodesInFrame_Recording:
			case CVProfile::kAllNodesInFrame_RecordingMultiFrame:
				// we are presently recording.
				if ( !XTraceStartRecording( g_VProfCurrentProfile.GetCPUTraceFilename() ) )
				{
					Msg( "XTraceStartRecording failed, error code %d\n", GetLastError() );
				}

			default:
				// no default.
				break;
			}

		}

#endif

#ifdef VPROF_VTUNE_GROUP
		g_VProfCurrentProfile.PushGroup( m_BudgetGroupID );
#endif
	}
}

//-------------------------------------

bool CVProfNode::ExitScope()
{
	if ( --m_nRecursions == 0 && m_nCurFrameCalls != 0 ) 
	{
		m_Timer.End();
		m_CurFrameTime += m_Timer.GetDuration();
#ifndef _X360
		if ( g_VProfCurrentProfile.UsePME() )
		{
			m_L2Cache.End();
			m_iCurL2CacheMiss += m_L2Cache.GetL2CacheMisses();
		}
#else // 360 code:
		if ( g_VProfCurrentProfile.UsePME() || ((m_iBitFlags & kRecordL2) != 0) ) 
		{
			m_PMCData.End();
			m_iCurL2CacheMiss   += m_PMCData.GetL2CacheMisses();
			m_iCurLoadHitStores += m_PMCData.GetLHS();
		}

		if ( (m_iBitFlags & kCPUTrace) != 0 )
		{
			// this node is enabled to be recorded. What mode are we in?
			switch ( g_VProfCurrentProfile.GetCPUTraceMode() )
			{
			case CVProfile::kFirstHitNode:
			{
				// one-off recording. stop now.
				if ( XTraceStopRecording() )
				{
					Msg( "CPU trace finished.\n" );
					if ( g_VProfCurrentProfile.TraceCompleteEvent() )
					{
						// signal VXConsole that trace is completed
						XBX_rTraceComplete();
					}
				}
				// don't trace again next frame, overwriting the file.
				g_VProfCurrentProfile.SetCPUTraceEnabled( CVProfile::kDisabled );
				break;
			}

			case CVProfile::kAllNodesInFrame_Recording:
			case CVProfile::kAllNodesInFrame_RecordingMultiFrame:
			{
				// one-off recording. stop now.
				if ( XTraceStopRecording() )
				{
					if ( g_VProfCurrentProfile.GetCPUTraceMode() == CVProfile::kAllNodesInFrame_RecordingMultiFrame )
					{
						Msg( "%.3f msec in %s\n", m_CurFrameTime.GetMillisecondsF(), g_VProfCurrentProfile.GetCPUTraceFilename() );
					}
					else
					{
						Msg( "CPU trace finished.\n" );
					}
				}
				
				// Spew time info for file to allow figuring it out later
				g_VProfCurrentProfile.LatchMultiFrame( m_CurFrameTime.GetLongCycles() );
				
#if 0 // This doesn't want to work on the xbox360-- MoveFile not available or file still being put down to disk?
				char suffix[ 32 ];
				_snprintf( suffix, sizeof( suffix ), "_%.3f_msecs", flMsecs );

				char fn[ 512 ];

				strncpy( fn, g_VProfCurrentProfile.GetCPUTraceFilename(), sizeof( fn ) );

				char *p = strrchr( fn, '.' );
				if ( *p )
				{	
					*p = 0;
				}
				strncat( fn, suffix, sizeof( fn ) );
				strncat( fn, ".pix2", sizeof( fn ) );
			
				BOOL bSuccess = MoveFile( g_VProfCurrentProfile.GetCPUTraceFilename(), fn );
				if ( !bSuccess )
				{
					DWORD eCode = GetLastError();
					Msg( "Error %d\n", eCode );
				}
#endif

				// we're still recording until the frame is done.
				// but, increment the index.
				g_VProfCurrentProfile.IncrementMultiTraceIndex();
				break;
			}

			}
			
			// g_VProfCurrentProfile.IsCPUTraceEnabled() && 


		}

#endif

#ifdef VPROF_VTUNE_GROUP
		g_VProfCurrentProfile.PopGroup();
#endif
	}
	return ( m_nRecursions == 0 );
}

//-------------------------------------

void CVProfNode::Pause()
{
	if ( m_nRecursions > 0 ) 
	{
		m_Timer.End();
		m_CurFrameTime += m_Timer.GetDuration();

#ifndef _X360
		if ( g_VProfCurrentProfile.UsePME() )
		{
			m_L2Cache.End();
			m_iCurL2CacheMiss += m_L2Cache.GetL2CacheMisses();
		}
#else // 360 code:
		if ( g_VProfCurrentProfile.UsePME() || ((m_iBitFlags & kRecordL2) != 0) ) 
		{
			m_PMCData.End();
			m_iCurL2CacheMiss   += m_PMCData.GetL2CacheMisses();
			m_iCurLoadHitStores += m_PMCData.GetLHS();
		}
#endif
	}
	if ( m_pChild ) 
	{
		m_pChild->Pause();
	}
	if ( m_pSibling ) 
	{
		m_pSibling->Pause();
	}
}

//-------------------------------------

void CVProfNode::Resume()
{
	if ( m_nRecursions > 0 ) 
	{
		m_Timer.Start();

#ifndef _X360
		if ( g_VProfCurrentProfile.UsePME() )
		{
			m_L2Cache.Start();
		}
#else
		if ( g_VProfCurrentProfile.UsePME() || ((m_iBitFlags & kRecordL2) != 0) ) 
		{
			m_PMCData.Start();
		}
#endif
	}
	if ( m_pChild ) 
	{
		m_pChild->Resume();
	}
	if ( m_pSibling ) 
	{
		m_pSibling->Resume();
	}
}

//-------------------------------------

void CVProfNode::Reset()
{
	m_nPrevFrameCalls = 0;
	m_PrevFrameTime.Init();

	m_nCurFrameCalls = 0;
	m_CurFrameTime.Init();
	
	m_nTotalCalls = 0;
	m_TotalTime.Init();
	
	m_PeakTime.Init();

	m_iPrevL2CacheMiss = 0;
	m_iCurL2CacheMiss = 0;
	m_iTotalL2CacheMiss = 0;

#ifdef _X360
	m_iPrevLoadHitStores = 0;
	m_iCurLoadHitStores = 0;
	m_iTotalLoadHitStores = 0;
#endif

	if ( m_pChild ) 
	{
		m_pChild->Reset();
	}
	if ( m_pSibling ) 
	{
		m_pSibling->Reset();
	}
}


//-------------------------------------

void CVProfNode::MarkFrame()
{
	m_nPrevFrameCalls = m_nCurFrameCalls;
	m_PrevFrameTime = m_CurFrameTime;
	m_iPrevL2CacheMiss = m_iCurL2CacheMiss;
#ifdef _X360
	m_iPrevLoadHitStores = m_iCurLoadHitStores;
#endif
	m_nTotalCalls += m_nCurFrameCalls;
	m_TotalTime += m_CurFrameTime;
	
	if ( m_PeakTime.IsLessThan( m_CurFrameTime ) )
	{
		m_PeakTime = m_CurFrameTime;
	}
	
	m_CurFrameTime.Init();
	m_nCurFrameCalls = 0;
	m_iTotalL2CacheMiss += m_iCurL2CacheMiss;
	m_iCurL2CacheMiss = 0;
#ifdef _X360
	m_iTotalLoadHitStores += m_iCurLoadHitStores;
	m_iCurLoadHitStores = 0;
#endif
	if ( m_pChild ) 
	{
		m_pChild->MarkFrame();
	}
	if ( m_pSibling ) 
	{
		m_pSibling->MarkFrame();
	}
}

//-------------------------------------

void CVProfNode::ResetPeak()
{
	m_PeakTime.Init();

	if ( m_pChild ) 
	{
		m_pChild->ResetPeak();
	}
	if ( m_pSibling ) 
	{
		m_pSibling->ResetPeak();
	}
}


void CVProfNode::SetCurFrameTime( unsigned long milliseconds )
{
	m_CurFrameTime.Init( (float)milliseconds );
}
#ifdef DBGFLAG_VALIDATE
//-----------------------------------------------------------------------------
// Purpose: Ensure that all of our internal structures are consistent, and
//			account for all memory that we've allocated.
// Input:	validator -		Our global validator object
//			pchName -		Our name (typically a member var in our container)
//-----------------------------------------------------------------------------
void CVProfNode::Validate( CValidator &validator, tchar *pchName )
{
	validator.Push( _T("CVProfNode"), this, pchName );

	m_L2Cache.Validate( validator, _T("m_L2Cache") );

	if ( m_pSibling )
		m_pSibling->Validate( validator, _T("m_pSibling") );
	if ( m_pChild )
		m_pChild->Validate( validator, _T("m_pChild") );

	validator.Pop( );
}
#endif // DBGFLAG_VALIDATE



//-----------------------------------------------------------------------------

struct TimeSums_t
{
	const tchar *pszProfileScope;
	int			calls;
	double 		time;
	double 		timeLessChildren;
	double		peak;
};

static bool TimeCompare( const TimeSums_t &lhs, const TimeSums_t &rhs )
{
	return ( lhs.time > rhs.time );
}

static bool TimeLessChildrenCompare( const TimeSums_t &lhs, const TimeSums_t &rhs )
{
	return ( lhs.timeLessChildren > rhs.timeLessChildren );
}

static bool PeakCompare( const TimeSums_t &lhs, const TimeSums_t &rhs )
{
	return ( lhs.peak > rhs.peak );
}

static bool AverageTimeCompare( const TimeSums_t &lhs, const TimeSums_t &rhs )
{
	double avgLhs = ( lhs.calls ) ? lhs.time / (double)lhs.calls : 0.0;
	double avgRhs = ( rhs.calls ) ? rhs.time / (double)rhs.calls : 0.0;
	return ( avgLhs > avgRhs );
}

static bool AverageTimeLessChildrenCompare( const TimeSums_t &lhs, const TimeSums_t &rhs )
{
	double avgLhs = ( lhs.calls ) ? lhs.timeLessChildren / (double)lhs.calls : 0.0;
	double avgRhs = ( rhs.calls ) ? rhs.timeLessChildren / (double)rhs.calls : 0.0;
	return ( avgLhs > avgRhs );

}
static bool PeakOverAverageCompare( const TimeSums_t &lhs, const TimeSums_t &rhs )
{
	double avgLhs = ( lhs.calls ) ? lhs.time / (double)lhs.calls : 0.0;
	double avgRhs = ( rhs.calls ) ? rhs.time / (double)rhs.calls : 0.0;
	
	double lhsPoA = ( avgLhs != 0 ) ? lhs.peak / avgLhs : 0.0;
	double rhsPoA = ( avgRhs != 0 ) ? rhs.peak / avgRhs : 0.0;
	
	return ( lhsPoA > rhsPoA );
}

map<CVProfNode *, double> 	g_TimesLessChildren;
int							g_TotalFrames;
map<const tchar *, size_t> g_TimeSumsMap;
vector<TimeSums_t> 			g_TimeSums;
CVProfNode *				g_pStartNode;
const tchar *				g_pszSumNode;

//-------------------------------------

void CVProfile::SumTimes( CVProfNode *pNode, int budgetGroupID )
{
	if ( !pNode )
		return; // this generally only happens on a failed FindNode()

	bool bSetStartNode;
	if ( !g_pStartNode && _tcscmp( pNode->GetName(), g_pszSumNode ) == 0 )
	{
		g_pStartNode = pNode;
		bSetStartNode = true;
	}
	else
		bSetStartNode = false;

	if ( GetRoot() != pNode )
	{
		if ( g_pStartNode && pNode->GetTotalCalls() > 0 && ( budgetGroupID == -1 || pNode->GetBudgetGroupID() == budgetGroupID ) )
		{
			double timeLessChildren = pNode->GetTotalTimeLessChildren();
			
			g_TimesLessChildren.insert( make_pair( pNode, timeLessChildren ) );
			
			map<const tchar *, size_t>::iterator iter;
			iter = g_TimeSumsMap.find( pNode->GetName() ); // intenionally using address of string rather than string compare (toml 01-27-03)
			if ( iter == g_TimeSumsMap.end() )
			{
				TimeSums_t timeSums = { pNode->GetName(), pNode->GetTotalCalls(), pNode->GetTotalTime(), timeLessChildren, pNode->GetPeakTime() };
				g_TimeSumsMap.insert( make_pair( pNode->GetName(), g_TimeSums.size() ) );
				g_TimeSums.push_back( timeSums );
			}
			else
			{
				TimeSums_t &timeSums = g_TimeSums[iter->second];
				timeSums.calls += pNode->GetTotalCalls();
				timeSums.time += pNode->GetTotalTime();
				timeSums.timeLessChildren += timeLessChildren;
				if ( pNode->GetPeakTime() > timeSums.peak )
					timeSums.peak = pNode->GetPeakTime();
			}
		}
			
		if( ( !g_pStartNode || pNode != g_pStartNode ) && pNode->GetSibling() )
		{
			SumTimes( pNode->GetSibling(), budgetGroupID );
		}
	}
	
	if( pNode->GetChild() )
	{
		SumTimes( pNode->GetChild(), budgetGroupID );
	}
		
	if ( bSetStartNode )
		g_pStartNode = NULL;
}

//-------------------------------------

CVProfNode *CVProfile::FindNode( CVProfNode *pStartNode, const tchar *pszNode )
{
	if ( _tcscmp( pStartNode->GetName(), pszNode ) != 0 )
	{
		CVProfNode *pFoundNode = NULL;
		if ( pStartNode->GetSibling() )
		{
			pFoundNode = FindNode( pStartNode->GetSibling(), pszNode );
		}

		if ( !pFoundNode && pStartNode->GetChild() )
		{
			pFoundNode = FindNode( pStartNode->GetChild(), pszNode );
		}

		return pFoundNode;
	}
	return pStartNode;
}

//-------------------------------------
#ifdef _X360

void CVProfile::PMCDisableAllNodes(CVProfNode *pStartNode)
{
	if (pStartNode == NULL)
	{
		pStartNode = GetRoot();
	}

	pStartNode->EnableL2andLHS(false);

	if ( pStartNode->GetSibling() )
	{
		PMCDisableAllNodes(pStartNode->GetSibling());
	}

	if ( pStartNode->GetChild() )
	{
		PMCDisableAllNodes(pStartNode->GetChild());
	}
}

// recursively set l2/lhs recording state for a node and all children AND SIBLINGS
static void PMCRecursiveL2Set(CVProfNode *pNode, bool enableState)
{
	if ( pNode )
	{
		pNode->EnableL2andLHS(enableState);
		if ( pNode->GetSibling() )
		{
			PMCRecursiveL2Set( pNode->GetSibling(), enableState );
		}
		if ( pNode->GetChild() )
		{
			PMCRecursiveL2Set( pNode->GetChild(), enableState );
		}
	}
}

bool CVProfile::PMCEnableL2Upon(const tchar *pszNodeName, bool bRecursive)
{
	// PMCDisableAllNodes();
	CVProfNode *pNode = FindNode( GetRoot(), pszNodeName );
	if (pNode)
	{
		pNode->EnableL2andLHS(true);
		if (bRecursive)
		{
			PMCRecursiveL2Set(pNode->GetChild(), true);
		}
		return true;
	}
	else
	{
		return false;
	}
}

bool CVProfile::PMCDisableL2Upon(const tchar *pszNodeName, bool bRecursive)
{
	// PMCDisableAllNodes();
	CVProfNode *pNode = FindNode( GetRoot(), pszNodeName );
	if ( pNode )
	{
		pNode->EnableL2andLHS( false );
		if ( bRecursive )
		{
			PMCRecursiveL2Set( pNode->GetChild(), false );
		}
		return true;
	}
	else
	{
		return false;
	}
}

static void DumpEnabledPMCNodesInner(CVProfNode* pNode)
{
	if (!pNode)
		return;

	if (pNode->IsL2andLHSEnabled())
	{
		Msg( _T("\t%s\n"), pNode->GetName() );
	}

	// depth first printing clearer
	if ( pNode->GetChild() )
	{
		DumpEnabledPMCNodesInner(pNode->GetChild());
	}

	if ( pNode->GetSibling() )
	{
		DumpEnabledPMCNodesInner(pNode->GetChild());
	}
}

void CVProfile::DumpEnabledPMCNodes( void )
{
	Msg( _T("Nodes enabled for PMC counters:\n") );
	CVProfNode *pNode = GetRoot();
	DumpEnabledPMCNodesInner( pNode );

	Msg( _T("(end)\n") );
}

CVProfNode *CVProfile::CPUTraceGetEnabledNode(CVProfNode *pStartNode) 
{
	if (!pStartNode)
	{
		pStartNode = GetRoot();
	}

	if ( (pStartNode->m_iBitFlags & CVProfNode::kCPUTrace) != 0 )
	{
		return pStartNode;
	}

	if (pStartNode->GetSibling())
	{
		CVProfNode *retval = CPUTraceGetEnabledNode(pStartNode->GetSibling());
		if (retval)
			return retval;
	}
	
	if (pStartNode->GetChild())
	{
		CVProfNode *retval = CPUTraceGetEnabledNode(pStartNode->GetChild());
		if (retval)
			return retval;
	}

	return NULL;
}

const char *CVProfile::SetCPUTraceFilename( const char *filename )
{
	strncpy( m_CPUTraceFilename, filename, sizeof( m_CPUTraceFilename ) );
	return GetCPUTraceFilename();	
}

/// Returns a pointer to an internal static, so you don't need to 
/// make temporary char buffers for this to write into. What of it?
/// You're not hanging on to that pointer. That would be foolish. 
const char *CVProfile::GetCPUTraceFilename()
{
	static char retBuf[256];

	switch ( m_iCPUTraceEnabled )
	{
	case kAllNodesInFrame_WaitingForMark:
	case kAllNodesInFrame_Recording:
		_snprintf( retBuf, sizeof( retBuf ), "e:\\%.128s%.4d.pix2", m_CPUTraceFilename, m_iSuccessiveTraceIndex );
		break;

	case kAllNodesInFrame_WaitingForMarkMultiFrame:
	case kAllNodesInFrame_RecordingMultiFrame:
		_snprintf( retBuf, sizeof( retBuf ), "e:\\%.128s_%.4d_%.4d.pix2", m_CPUTraceFilename, m_nFrameCount, m_iSuccessiveTraceIndex );
		break;

	default:
		_snprintf( retBuf, sizeof( retBuf ), "e:\\%.128s.pix2", m_CPUTraceFilename );
	}

	return retBuf;
}

bool CVProfile::TraceCompleteEvent( void )
{
	return m_bTraceCompleteEvent;
}

CVProfNode *CVProfile::CPUTraceEnableForNode(const tchar *pszNodeName)
{
	// disable whatever may be enabled already (we can only trace one node at a time)
	CPUTraceDisableAllNodes();

	CVProfNode *which = FindNode(GetRoot(), pszNodeName);
	if (which)
	{
		which->m_iBitFlags |= CVProfNode::kCPUTrace;
		return which;
	}
	else
		return NULL;
}

void CVProfile::CPUTraceDisableAllNodes(CVProfNode *pStartNode)
{
	if (!pStartNode)
	{
		pStartNode = GetRoot();
	}

	pStartNode->m_iBitFlags &= ~CVProfNode::kCPUTrace;

	if (pStartNode->GetSibling())
	{
		CPUTraceDisableAllNodes(pStartNode->GetSibling());
	}

	if (pStartNode->GetChild())
	{
		CPUTraceDisableAllNodes(pStartNode->GetChild());
	}

}

#endif

//-------------------------------------

void CVProfile::SumTimes( const tchar *pszStartNode, int budgetGroupID )
{
	if ( GetRoot()->GetChild() )
	{
		if ( pszStartNode == NULL )
			g_pStartNode = GetRoot();
		else
			g_pStartNode = NULL;

		g_pszSumNode = pszStartNode;
		SumTimes( GetRoot(), budgetGroupID );
		g_pStartNode = NULL;
	}

}

//-------------------------------------

// This array lets us generate the commonly used indent levels
// without looping. That then lets us print our vprof nodes
// in a single call, which is more efficient and works better
// with output streams like ETW where each call represents a
// 'line' of text. Indent levels beyond what is represented
// in this array are, regretfully, clamped, however the highest
// indent level seen in testing was 10.
static const char* s_indentText[] =
{
	"",											// 0
	"",											// 1
	"|  ",										// 2
	"|  |  ",									// 3
	"|  |  |  ",								// 4
	"|  |  |  |  ",								// 5
	"|  |  |  |  |  ",							// 6
	"|  |  |  |  |  |  ",						// 7
	"|  |  |  |  |  |  |  ",					// 8
	"|  |  |  |  |  |  |  |  ",					// 9
	"|  |  |  |  |  |  |  |  |  ",				// 10
	"|  |  |  |  |  |  |  |  |  |  ",			// 11
	"|  |  |  |  |  |  |  |  |  |  |  ",		// 12
	"|  |  |  |  |  |  |  |  |  |  |  |  ",		// 13
	"|  |  |  |  |  |  |  |  |  |  |  |  |  ",	// 14
};

void CVProfile::DumpNodes( CVProfNode *pNode, int indent, bool bAverageAndCountOnly )
{
	if ( !pNode )
		return; // this generally only happens on a failed FindNode()

	bool fIsRoot = ( pNode == GetRoot() );

	if ( fIsRoot || pNode == g_pStartNode )
	{
		if( bAverageAndCountOnly )
		{
			m_pOutputStream( _T(" Avg Time/Frame (ms)\n") );
			m_pOutputStream( _T("[ func+child   func ]     Count\n") );
			m_pOutputStream( _T("  ---------- ------      ------\n") );
		}
		else
		{
			m_pOutputStream( _T("       Sum (ms)         Avg Time/Frame (ms)     Avg Time/Call (ms)\n") );
			m_pOutputStream( _T("[ func+child   func ]  [ func+child   func ]  [ func+child   func ]  Count   Peak\n") );
			m_pOutputStream( _T("  ---------- ------      ---------- ------      ---------- ------   ------ ------\n") );
		}
	}

	if ( !fIsRoot )
	{
		map<CVProfNode *, double>::iterator iterTimeLessChildren = g_TimesLessChildren.find( pNode );
		
		indent = Max( indent, 0 );
		indent = Min( indent, (int)ARRAYSIZE( s_indentText ) - 1 );
		const char* indentText = s_indentText[ indent ];
		double dNodeTime = 0;
		if(iterTimeLessChildren != g_TimesLessChildren.end())
			dNodeTime = iterTimeLessChildren->second;

		if( bAverageAndCountOnly )
		{
			m_pOutputStream( _T("  %10.3f %6.2f      %6d  %s%s\n"), 
						 ( pNode->GetTotalCalls() > 0 ) ? pNode->GetTotalTime() / (double)NumFramesSampled() : 0, 
						 ( pNode->GetTotalCalls() > 0 ) ? dNodeTime / (double)NumFramesSampled() : 0, 
						 pNode->GetTotalCalls(), indentText, pNode->GetName() );
		}
		else
		{
			m_pOutputStream( _T("  %10.3f %6.2f      %10.3f %6.2f      %10.3f %6.2f   %6d %6.2f  %s%s\n"), 
						 pNode->GetTotalTime(), dNodeTime,
						 ( pNode->GetTotalCalls() > 0 ) ? pNode->GetTotalTime() / (double)NumFramesSampled() : 0, 
						 ( pNode->GetTotalCalls() > 0 ) ? dNodeTime / (double)NumFramesSampled() : 0, 
						 ( pNode->GetTotalCalls() > 0 ) ? pNode->GetTotalTime() / (double)pNode->GetTotalCalls() : 0, 
						 ( pNode->GetTotalCalls() > 0 ) ? dNodeTime / (double)pNode->GetTotalCalls() : 0, 
						 pNode->GetTotalCalls(), pNode->GetPeakTime(), indentText, pNode->GetName() );
		}
	}

	if( pNode->GetChild() )
	{
		DumpNodes( pNode->GetChild(), indent + 1, bAverageAndCountOnly );
	}
	
	if( !( fIsRoot || pNode == g_pStartNode ) && pNode->GetSibling() )
	{
		DumpNodes( pNode->GetSibling(), indent, bAverageAndCountOnly );
	}
}

//-------------------------------------

#if defined( _X360 )
static void CalcBudgetGroupTimes_Recursive( CVProfNode *pNode, unsigned int *groupTimes, int numGroups, float flScale )
{
	int			groupID;
	CVProfNode	*nodePtr;

	groupID = pNode->GetBudgetGroupID();
	if ( groupID >= numGroups )
	{
		return;
	}

	groupTimes[groupID] += flScale*pNode->GetPrevTimeLessChildren();	

	nodePtr = pNode->GetSibling();
	if ( nodePtr )
	{
		CalcBudgetGroupTimes_Recursive( nodePtr, groupTimes, numGroups, flScale );
	}

	nodePtr = pNode->GetChild();
	if ( nodePtr )
	{
		CalcBudgetGroupTimes_Recursive( nodePtr, groupTimes, numGroups, flScale );
	}
}

static void CalcBudgetGroupL2CacheMisses_Recursive( CVProfNode *pNode, unsigned int *groupTimes, int numGroups, float flScale )
{
	int			groupID;
	CVProfNode	*nodePtr;

	groupID = pNode->GetBudgetGroupID();
	if ( groupID >= numGroups )
	{
		return;
	}

	groupTimes[groupID] += flScale*pNode->GetPrevL2CacheMissLessChildren();	

	nodePtr = pNode->GetSibling();
	if ( nodePtr )
	{
		CalcBudgetGroupL2CacheMisses_Recursive( nodePtr, groupTimes, numGroups, flScale );
	}

	nodePtr = pNode->GetChild();
	if ( nodePtr )
	{
		CalcBudgetGroupL2CacheMisses_Recursive( nodePtr, groupTimes, numGroups, flScale );
	}
}

static void CalcBudgetGroupLHS_Recursive( CVProfNode *pNode, unsigned int *groupTimes, int numGroups, float flScale )
{
	int			groupID;
	CVProfNode	*nodePtr;

	groupID = pNode->GetBudgetGroupID();
	if ( groupID >= numGroups )
	{
		return;
	}

	groupTimes[groupID] += flScale*pNode->GetPrevLoadHitStoreLessChildren();	

	nodePtr = pNode->GetSibling();
	if ( nodePtr )
	{
		CalcBudgetGroupLHS_Recursive( nodePtr, groupTimes, numGroups, flScale );
	}

	nodePtr = pNode->GetChild();
	if ( nodePtr )
	{
		CalcBudgetGroupLHS_Recursive( nodePtr, groupTimes, numGroups, flScale );
	}
}


void CVProfile::VXConsoleReportMode( VXConsoleReportMode_t mode )
{
	m_ReportMode = mode;
}

void CVProfile::VXConsoleReportScale( VXConsoleReportMode_t mode, float flScale )
{
	m_pReportScale[mode] = flScale;
}


//-----------------------------------------------------------------------------
// Send the all the counter attributes once to VXConsole at profiling start
//-----------------------------------------------------------------------------
void CVProfile::VXProfileStart()
{
	const char		*names[XBX_MAX_PROFILE_COUNTERS];
	unsigned int	colors[XBX_MAX_PROFILE_COUNTERS];
	int				numGroups;
	int				counterGroup;
	const char		*pGroupName;
	int				i;
	int				r,g,b,a;

	// vprof system must be running
	if ( m_enabled <= 0 || !m_UpdateMode )
	{
		return;
	}

	if ( m_UpdateMode & VPROF_UPDATE_BUDGET )
	{
		// update budget profiling
		numGroups = g_VProfCurrentProfile.GetNumBudgetGroups();
		if ( numGroups > XBX_MAX_PROFILE_COUNTERS )
		{
			numGroups = XBX_MAX_PROFILE_COUNTERS;
		}
		for ( i=0; i<numGroups; i++ )
		{
			names[i] = g_VProfCurrentProfile.GetBudgetGroupName( i );
			g_VProfCurrentProfile.GetBudgetGroupColor( i, r, g, b, a );
			colors[i] = XMAKECOLOR( r, g, b );
		}

		// send all the profile attributes
		XBX_rSetProfileAttributes( "cpu", numGroups, names, colors );
	}

	if ( m_UpdateMode & (VPROF_UPDATE_TEXTURE_GLOBAL|VPROF_UPDATE_TEXTURE_PERFRAME) )
	{		
		// update texture profiling
		numGroups = 0;
		counterGroup = (m_UpdateMode & VPROF_UPDATE_TEXTURE_GLOBAL) ? COUNTER_GROUP_TEXTURE_GLOBAL : COUNTER_GROUP_TEXTURE_PER_FRAME;
		for ( i=0; i<g_VProfCurrentProfile.GetNumCounters(); i++ )
		{
			if ( g_VProfCurrentProfile.GetCounterGroup( i ) == counterGroup )
			{	
				// strip undesired prefix
				pGroupName = g_VProfCurrentProfile.GetCounterName( i );
				if ( !strnicmp( pGroupName, "texgroup_frame_", 15 ) )
				{
					pGroupName += 15;
				}
				else if ( !strnicmp( pGroupName, "texgroup_global_", 16 ) )
				{
					pGroupName += 16;
				}
				names[numGroups] = pGroupName;

				g_VProfCurrentProfile.GetBudgetGroupColor( numGroups, r, g, b, a );
				colors[numGroups] = XMAKECOLOR( r, g, b );

				numGroups++;
				if ( numGroups == XBX_MAX_PROFILE_COUNTERS )
				{
					break;
				}
			}
		}

		// send all the profile attributes
		XBX_rSetProfileAttributes( "texture", numGroups, names, colors );
	}
}

//-----------------------------------------------------------------------------
// Send the counters to VXConsole
//-----------------------------------------------------------------------------
void CVProfile::VXProfileUpdate()
{
	int				i;
	int				counterGroup;
	int				numGroups;
	unsigned int	groupData[XBX_MAX_PROFILE_COUNTERS];

	// vprof system must be running
	if ( m_enabled <= 0 || !m_UpdateMode )
	{
		return;
	}

	if ( m_UpdateMode & VPROF_UPDATE_BUDGET )
	{
		// send the cpu counters
		numGroups = g_VProfCurrentProfile.GetNumBudgetGroups();
		if ( numGroups > XBX_MAX_PROFILE_COUNTERS )
		{
			numGroups = XBX_MAX_PROFILE_COUNTERS;
		}
		memset( groupData, 0, numGroups * sizeof( unsigned int ) );

		CVProfNode *pNode = g_VProfCurrentProfile.GetRoot();
		if ( pNode && pNode->GetChild() )
		{
			switch ( m_ReportMode )
			{
			default:
			case VXCONSOLE_REPORT_TIME:
				CalcBudgetGroupTimes_Recursive( pNode->GetChild(), groupData, numGroups, m_pReportScale[VXCONSOLE_REPORT_TIME] );
				break;

			case VXCONSOLE_REPORT_L2CACHE_MISSES:
				CalcBudgetGroupL2CacheMisses_Recursive( pNode->GetChild(), groupData, numGroups, m_pReportScale[VXCONSOLE_REPORT_L2CACHE_MISSES] );
				break;

			case VXCONSOLE_REPORT_LOAD_HIT_STORE:
				CalcBudgetGroupLHS_Recursive( pNode->GetChild(), groupData, numGroups, m_pReportScale[VXCONSOLE_REPORT_LOAD_HIT_STORE] );
				break;
			}
		}

		XBX_rSetProfileData( "cpu", numGroups, groupData );
	}

	if ( m_UpdateMode & ( VPROF_UPDATE_TEXTURE_GLOBAL|VPROF_UPDATE_TEXTURE_PERFRAME ) )
	{
		// send the texture counters
		numGroups = 0;
		counterGroup = ( m_UpdateMode & VPROF_UPDATE_TEXTURE_GLOBAL ) ? COUNTER_GROUP_TEXTURE_GLOBAL : COUNTER_GROUP_TEXTURE_PER_FRAME;
		for ( i = 0; i < g_VProfCurrentProfile.GetNumCounters(); i++ )
		{
			if ( g_VProfCurrentProfile.GetCounterGroup( i ) == counterGroup )
			{
				// get the size in bytes
				groupData[numGroups++] = g_VProfCurrentProfile.GetCounterValue( i );
				if ( numGroups == XBX_MAX_PROFILE_COUNTERS )
				{
					break;
				}
			}
		}

		XBX_rSetProfileData( "texture", numGroups, groupData );
	}
}

void CVProfile::VXEnableUpdateMode( int event, bool bEnable )
{
	// enable or disable the updating of specified events
	if ( bEnable )
	{
		m_UpdateMode |= event;
	}
	else
	{
		m_UpdateMode &= ~event;
	}

	// force a resend of possibly affected attributes
	VXProfileStart();
}

#define MAX_VPROF_NODES_IN_LIST 4096
static void VXBuildNodeList_r( CVProfNode *pNode, xVProfNodeItem_t *pNodeList, int *pNumNodes )
{
	if ( !pNode )
	{
		return; 
	}
	if ( *pNumNodes >= MAX_VPROF_NODES_IN_LIST )
	{
		// list full
		return;
	}

	// add to list
	pNodeList[*pNumNodes].pName = (const char *)pNode->GetName();

	pNodeList[*pNumNodes].pBudgetGroupName = g_VProfCurrentProfile.GetBudgetGroupName( pNode->GetBudgetGroupID() );
	int r, g, b, a;
	g_VProfCurrentProfile.GetBudgetGroupColor( pNode->GetBudgetGroupID(), r, g, b, a );
	pNodeList[*pNumNodes].budgetGroupColor = XMAKECOLOR( r, g, b );

	pNodeList[*pNumNodes].totalCalls = pNode->GetTotalCalls();
	pNodeList[*pNumNodes].inclusiveTime = pNode->GetTotalTime();
	pNodeList[*pNumNodes].exclusiveTime = pNode->GetTotalTimeLessChildren();
	(*pNumNodes)++;

	CVProfNode	*nodePtr = pNode->GetSibling();
	if ( nodePtr )
	{
		VXBuildNodeList_r( nodePtr, pNodeList, pNumNodes );
	}

	nodePtr = pNode->GetChild();
	if ( nodePtr )
	{
		VXBuildNodeList_r( nodePtr, pNodeList, pNumNodes );
	}
}
void CVProfile::VXSendNodes( void )
{
	Pause();

	xVProfNodeItem_t *pNodeList = (xVProfNodeItem_t *)stackalloc( MAX_VPROF_NODES_IN_LIST * sizeof(xVProfNodeItem_t) );
	int numNodes = 0;
	VXBuildNodeList_r( GetRoot(), pNodeList, &numNodes );

	// send to vxconsole
	XBX_rVProfNodeList( numNodes, pNodeList );

	Resume();
}
#endif

//-------------------------------------
static void DumpSorted( CVProfile::StreamOut_t outputStream, const tchar *pszHeading, double totalTime, bool (*pfnSort)( const TimeSums_t &, const TimeSums_t & ), int maxLen = 999999 )
{
	unsigned i;
	vector<TimeSums_t> sortedSums;
	sortedSums = g_TimeSums;
	sort( sortedSums.begin(), sortedSums.end(), pfnSort );

	outputStream( _T("%s\n"), pszHeading);
    outputStream( _T("  Scope                                                      Calls Calls/Frame  Time+Child    Pct        Time    Pct   Avg/Frame    Avg/Call Avg-NoChild        Peak\n"));
    outputStream( _T("  ---------------------------------------------------- ----------- ----------- ----------- ------ ----------- ------ ----------- ----------- ----------- -----------\n"));
    for ( i = 0; i < sortedSums.size() && i < (unsigned)maxLen; i++ )
    {
		double avg = ( sortedSums[i].calls ) ? sortedSums[i].time / (double)sortedSums[i].calls : 0.0;
		double avgLessChildren = ( sortedSums[i].calls ) ? sortedSums[i].timeLessChildren / (double)sortedSums[i].calls : 0.0;
		
        outputStream( _T("  %52.52s%12d%12.3f%12.3f%7.2f%12.3f%7.2f%12.3f%12.3f%12.3f%12.3f\n"), 
             sortedSums[i].pszProfileScope,
             sortedSums[i].calls,
			 (float)sortedSums[i].calls / (float)g_TotalFrames,
			 sortedSums[i].time,
			 min( ( sortedSums[i].time / totalTime ) * 100.0, 100.0 ),
			 sortedSums[i].timeLessChildren,
			 min( ( sortedSums[i].timeLessChildren / totalTime ) * 100.0, 100.0 ),
			 sortedSums[i].time / (float)g_TotalFrames,
			 avg,
			 avgLessChildren,
			 sortedSums[i].peak );
	}
}

#if _X360
// Dump information on all nodes with PMC recording
static void DumpPMC( CVProfNode *pNode, bool &bPrintHeader, uint64 L2thresh = 1, uint64 LHSthresh = 1 )
{
	if (!pNode) return;

	uint64 l2 = pNode->GetL2CacheMisses();
	uint64 lhs = pNode->GetLoadHitStores();
	if ( l2  > L2thresh && 
		 lhs > LHSthresh )
	{
		// met threshold.
		if (bPrintHeader)
		{
			// print header
			Msg( _T("-- 360 PMC information --\n") );
			Msg( _T("Scope                                                  L2/call  L2/frame  LHS/call LHS/frame\n") );
			Msg( _T("---------------------------------------------------- --------- --------- --------- ---------\n") );

			bPrintHeader = false;
		}

		// print
		float calls = pNode->GetTotalCalls();
		float frames = g_TotalFrames;
		Msg( _T("%52.52s %9.2f %9.2f %9.2f %9.2f\n"), pNode->GetName(), l2/calls, l2/frames, lhs/calls, lhs/frames );
	}

	if ( pNode->GetSibling() )
	{
		DumpPMC( pNode->GetSibling(), bPrintHeader, L2thresh, LHSthresh );
	}

	if ( pNode->GetChild() )
	{
		DumpPMC( pNode->GetChild(), bPrintHeader, L2thresh, LHSthresh );
	}
}
#endif

//-------------------------------------

void CVProfile::SetOutputStream( StreamOut_t outputStream )
{
	if ( outputStream != NULL )
		m_pOutputStream = outputStream;
	else
		m_pOutputStream = Msg;
}

//-------------------------------------

void CVProfile::OutputReport( int type, const tchar *pszStartNode, int budgetGroupID )
{
	m_pOutputStream( _T("******** BEGIN VPROF REPORT ********\n"));
#ifdef _MSC_VER
#if (_MSC_VER < 1300)
	m_pOutputStream( _T("  (note: this report exceeds the output capacity of MSVC debug window. Use console window or console log.) \n"));
#endif
#endif

	g_TotalFrames = max( NumFramesSampled() - 1, 1 );
	
	if ( NumFramesSampled() == 0 || GetTotalTimeSampled() == 0)
		m_pOutputStream( _T("No samples\n") );
	else
	{
		if ( type & VPRT_SUMMARY )
		{
			m_pOutputStream( _T("-- Summary --\n") );
			m_pOutputStream( _T("%d frames sampled for %.2f seconds\n"), g_TotalFrames, GetTotalTimeSampled() / 1000.0 );
			m_pOutputStream( _T("Average %.2f fps, %.2f ms per frame\n"), 1000.0 / ( GetTotalTimeSampled() / g_TotalFrames ), GetTotalTimeSampled() / g_TotalFrames );
			m_pOutputStream( _T("Peak %.2f ms frame\n"), GetPeakFrameTime() );
			
			double timeAccountedFor = 100.0 - ( m_Root.GetTotalTimeLessChildren() / m_Root.GetTotalTime() );
			m_pOutputStream( _T("%.0f pct of time accounted for\n"), min( 100.0, timeAccountedFor ) );
			m_pOutputStream( _T("\n") );
		}

		if ( pszStartNode == NULL )
		{
			pszStartNode = GetRoot()->GetName();
		}

		SumTimes( pszStartNode, budgetGroupID );
		
		// Dump the hierarchy
		if ( type & VPRT_HIERARCHY )
		{
			m_pOutputStream( _T("-- Hierarchical Call Graph --\n"));
			if ( pszStartNode == NULL )
				g_pStartNode = NULL;
			else
				g_pStartNode = FindNode( GetRoot(), pszStartNode );

			DumpNodes( (!g_pStartNode) ? GetRoot() : g_pStartNode, 0, false );
			m_pOutputStream( _T("\n") );
		}
		
		if ( type & VPRT_HIERARCHY_TIME_PER_FRAME_AND_COUNT_ONLY )
		{
			m_pOutputStream( _T("-- Hierarchical Call Graph --\n"));
			if ( pszStartNode == NULL )
				g_pStartNode = NULL;
			else
				g_pStartNode = FindNode( GetRoot(), pszStartNode );

			DumpNodes( (!g_pStartNode) ? GetRoot() : g_pStartNode, 0, true );
			m_pOutputStream( _T("\n") );
		}

		int maxLen = ( type & VPRT_LIST_TOP_ITEMS_ONLY ) ? 25 : 999999;

		if ( type & VPRT_LIST_BY_TIME )
		{
			DumpSorted( m_pOutputStream, _T("-- Profile scopes sorted by time (including children) --"), GetTotalTimeSampled(), TimeCompare, maxLen );
			m_pOutputStream( _T("\n") );
		}
		if ( type & VPRT_LIST_BY_TIME_LESS_CHILDREN )
		{
			DumpSorted( m_pOutputStream, _T("-- Profile scopes sorted by time (without children) --"), GetTotalTimeSampled(), TimeLessChildrenCompare, maxLen );
			m_pOutputStream( _T("\n") );
		}
		if ( type & VPRT_LIST_BY_AVG_TIME )
		{
			DumpSorted( m_pOutputStream, _T("-- Profile scopes sorted by average time (including children) --"), GetTotalTimeSampled(), AverageTimeCompare, maxLen );
			m_pOutputStream( _T("\n") );
		}
		if ( type & VPRT_LIST_BY_AVG_TIME_LESS_CHILDREN )
		{
			DumpSorted( m_pOutputStream, _T("-- Profile scopes sorted by average time (without children) --"), GetTotalTimeSampled(), AverageTimeLessChildrenCompare, maxLen );
			m_pOutputStream( _T("\n") );
		}
		if ( type & VPRT_LIST_BY_PEAK_TIME )
		{
			DumpSorted( m_pOutputStream, _T("-- Profile scopes sorted by peak --"), GetTotalTimeSampled(), PeakCompare, maxLen);
			m_pOutputStream( _T("\n") );
		}
		if ( type & VPRT_LIST_BY_PEAK_OVER_AVERAGE )
		{
			DumpSorted( m_pOutputStream, _T("-- Profile scopes sorted by peak over average (including children) --"), GetTotalTimeSampled(), PeakOverAverageCompare, maxLen );
			m_pOutputStream( _T("\n") );
		}
		
		// TODO: Functions by time less children
		// TODO: Functions by time averages
		// TODO: Functions by peak
		// TODO: Peak deviation from average
		g_TimesLessChildren.clear();
		g_TimeSumsMap.clear();
		g_TimeSums.clear();

#ifdef _X360
		bool bPrintedHeader = true;
		DumpPMC( FindNode( GetRoot(), pszStartNode ), bPrintedHeader );
#endif

	}
	m_pOutputStream( _T("******** END VPROF REPORT ********\n"));

}

//=============================================================================

CVProfile::CVProfile() 
 :	m_Root( _T("Root"), 0, NULL, VPROF_BUDGETGROUP_OTHER_UNACCOUNTED, 0 ),
	m_pCurNode( &m_Root ), 
 	m_nFrames( 0 ),
 	m_enabled( 0 ),
 	m_pausedEnabledDepth( 0 ),
	m_fAtRoot( true ),
	m_pOutputStream( Msg )
{
#ifdef VPROF_VTUNE_GROUP
	m_GroupIDStackDepth = 1;
	m_GroupIDStack[0] = 0; // VPROF_BUDGETGROUP_OTHER_UNACCOUNTED
#endif

	m_TargetThreadId = ThreadGetCurrentId();
	
	// Go ahead and allocate 32 slots for budget group names
	MEM_ALLOC_CREDIT();
	m_pBudgetGroups = new CVProfile::CBudgetGroup[32];
	m_nBudgetGroupNames = 0;
	m_nBudgetGroupNamesAllocated = 32;

	// Add these here so that they will always be in the same order.
	// VPROF_BUDGETGROUP_OTHER_UNACCOUNTED has to be FIRST!!!!
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_OTHER_UNACCOUNTED,		BUDGETFLAG_OTHER | BUDGETFLAG_SERVER );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_WORLD_RENDERING,			BUDGETFLAG_CLIENT );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_DISPLACEMENT_RENDERING,	BUDGETFLAG_CLIENT );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_GAME,						BUDGETFLAG_OTHER | BUDGETFLAG_SERVER );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_PLAYER,					BUDGETFLAG_OTHER | BUDGETFLAG_SERVER );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_NPCS,						BUDGETFLAG_OTHER | BUDGETFLAG_SERVER );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_SERVER_ANIM,				BUDGETFLAG_OTHER | BUDGETFLAG_SERVER );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_CLIENT_ANIMATION,			BUDGETFLAG_CLIENT );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_PHYSICS,					BUDGETFLAG_OTHER | BUDGETFLAG_SERVER );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_STATICPROP_RENDERING,		BUDGETFLAG_CLIENT );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_MODEL_RENDERING,			BUDGETFLAG_CLIENT );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_MODEL_FAST_PATH_RENDERING,BUDGETFLAG_CLIENT );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_LIGHTCACHE,				BUDGETFLAG_CLIENT );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_BRUSHMODEL_RENDERING,		BUDGETFLAG_CLIENT );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_SHADOW_RENDERING,			BUDGETFLAG_CLIENT );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_DETAILPROP_RENDERING,		BUDGETFLAG_CLIENT );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_PARTICLE_RENDERING,		BUDGETFLAG_CLIENT );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_ROPES,					BUDGETFLAG_CLIENT );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_DLIGHT_RENDERING,			BUDGETFLAG_CLIENT );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_OTHER_NETWORKING,			BUDGETFLAG_OTHER | BUDGETFLAG_SERVER );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_OTHER_SOUND,				BUDGETFLAG_OTHER | BUDGETFLAG_SERVER );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_OTHER_VGUI,				BUDGETFLAG_OTHER | BUDGETFLAG_SERVER );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_OTHER_FILESYSTEM,			BUDGETFLAG_OTHER | BUDGETFLAG_SERVER );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_PREDICTION,				BUDGETFLAG_CLIENT );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_INTERPOLATION,			BUDGETFLAG_CLIENT );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_SWAP_BUFFERS,				BUDGETFLAG_CLIENT );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_OCCLUSION,				BUDGETFLAG_CLIENT );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_OVERLAYS,					BUDGETFLAG_CLIENT );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_TOOLS,					BUDGETFLAG_OTHER | BUDGETFLAG_CLIENT );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_TEXTURE_CACHE,			BUDGETFLAG_CLIENT );
	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_REPLAY,					BUDGETFLAG_SERVER );
//	BudgetGroupNameToBudgetGroupID( VPROF_BUDGETGROUP_DISP_HULLTRACES );

	m_bPMEInit = false;
	m_bPMEEnabled = false;

#ifdef _X360
	m_UpdateMode = 0;
	m_iCPUTraceEnabled = kDisabled;
	m_bTraceCompleteEvent = false;
	m_iSuccessiveTraceIndex = 0;
	m_ReportMode = VXCONSOLE_REPORT_TIME;
	m_pReportScale[VXCONSOLE_REPORT_TIME] = 1000.0f;
	m_pReportScale[VXCONSOLE_REPORT_L2CACHE_MISSES] = 1.0f;
	m_pReportScale[VXCONSOLE_REPORT_LOAD_HIT_STORE] = 0.1f;
	m_nFrameCount = 0;
	m_nFramesRemaining = 1;
	m_WorstCycles = 0;
	m_WorstTraceFilename[ 0 ] = 0;
#endif
}


CVProfile::~CVProfile()
{
	Term();
}


void CVProfile::FreeNodes_R( CVProfNode *pNode )
{
	CVProfNode *pNext;
	for ( CVProfNode *pChild = pNode->GetChild(); pChild; pChild = pNext )
	{
		pNext = pChild->GetSibling();
		FreeNodes_R( pChild );
	}
	
	if ( pNode == GetRoot() )
	{
		pNode->m_pChild = NULL;
	}
	else
	{
		delete pNode;
	}
}


void CVProfile::Term()
{
	int i;
	for( i = 0; i < m_nBudgetGroupNames; i++ )
	{
		delete [] m_pBudgetGroups[i].m_pName;
	}
	delete m_pBudgetGroups;
	m_nBudgetGroupNames = m_nBudgetGroupNamesAllocated = 0;
	m_pBudgetGroups = NULL;

	int n;
	for( n = 0; n < m_NumCounters; n++ )
	{
		delete [] m_CounterNames[n];
		m_CounterNames[n] = NULL;
	}
	m_NumCounters = 0;

	// Free the nodes.
	if ( GetRoot() )
	{
		FreeNodes_R( GetRoot() );
	}
}


#define COLORMIN 160
#define COLORMAX 255

static int g_ColorLookup[4] = 
{
	COLORMIN, 
	COLORMAX,
	COLORMIN+(COLORMAX-COLORMIN)/3,
	COLORMIN+((COLORMAX-COLORMIN)*2)/3, 
};

#define GET_BIT( val, bitnum ) ( ( val >> bitnum ) & 0x1 )

void CVProfile::GetBudgetGroupColor( int budgetGroupID, int &r, int &g, int &b, int &a )
{
	budgetGroupID = budgetGroupID % ( 1 << 6 );

	int index;
	index = GET_BIT( budgetGroupID, 0 ) | ( GET_BIT( budgetGroupID, 5 ) << 1 );
	r = g_ColorLookup[index];
	index = GET_BIT( budgetGroupID, 1 ) | ( GET_BIT( budgetGroupID, 4 ) << 1 );
	g = g_ColorLookup[index];
	index = GET_BIT( budgetGroupID, 2 ) | ( GET_BIT( budgetGroupID, 3 ) << 1 );
	b = g_ColorLookup[index];
	a = 255;
}

// return -1 if it doesn't exist.
int CVProfile::FindBudgetGroupName( const tchar *pBudgetGroupName )
{
	int i;
	for( i = 0; i < m_nBudgetGroupNames; i++ )
	{
		if( _tcsicmp( pBudgetGroupName, m_pBudgetGroups[i].m_pName ) == 0 )
		{
			return i;
		}
	}
	return -1;
}

int CVProfile::AddBudgetGroupName( const tchar *pBudgetGroupName, int budgetFlags )
{
	MEM_ALLOC_CREDIT();
	tchar *pNewString = new tchar[ _tcslen( pBudgetGroupName ) + 1 ];
	_tcscpy( pNewString, pBudgetGroupName );
	if( m_nBudgetGroupNames + 1 > m_nBudgetGroupNamesAllocated )
	{
		m_nBudgetGroupNamesAllocated *= 2;
		m_nBudgetGroupNamesAllocated = max( m_nBudgetGroupNames + 6, m_nBudgetGroupNamesAllocated );
		
		CBudgetGroup *pNew = new CBudgetGroup[ m_nBudgetGroupNamesAllocated ];
		for ( int i=0; i < m_nBudgetGroupNames; i++ )
			pNew[i] = m_pBudgetGroups[i];
		
		delete [] m_pBudgetGroups;
		m_pBudgetGroups = pNew;
	}

	m_pBudgetGroups[m_nBudgetGroupNames].m_pName = pNewString;
	m_pBudgetGroups[m_nBudgetGroupNames].m_BudgetFlags = budgetFlags;
	m_nBudgetGroupNames++;
	if( m_pNumBudgetGroupsChangedCallBack )
	{
		(*m_pNumBudgetGroupsChangedCallBack)();
	}

#if defined( _X360 )
	// re-start with all the known budgets
	VXProfileStart();
#endif
	return m_nBudgetGroupNames - 1;
}

int CVProfile::BudgetGroupNameToBudgetGroupID( const tchar *pBudgetGroupName, int budgetFlagsToORIn )
{
	int budgetGroupID = FindBudgetGroupName( pBudgetGroupName );
	if( budgetGroupID == -1 )
	{
		budgetGroupID = AddBudgetGroupName( pBudgetGroupName, budgetFlagsToORIn );
	}
	else
	{
		m_pBudgetGroups[budgetGroupID].m_BudgetFlags |= budgetFlagsToORIn;
	}

	return budgetGroupID;
}

int CVProfile::BudgetGroupNameToBudgetGroupID( const tchar *pBudgetGroupName )
{
	return BudgetGroupNameToBudgetGroupID( pBudgetGroupName, BUDGETFLAG_OTHER );
}

int CVProfile::GetNumBudgetGroups( void )
{
	return m_nBudgetGroupNames;
}

void CVProfile::RegisterNumBudgetGroupsChangedCallBack( void (*pCallBack)(void) )
{
	m_pNumBudgetGroupsChangedCallBack = pCallBack;
}

void CVProfile::HideBudgetGroup( int budgetGroupID, bool bHide )
{
	if( budgetGroupID != -1 )
	{
		if ( bHide )
			m_pBudgetGroups[budgetGroupID].m_BudgetFlags |= BUDGETFLAG_HIDDEN;
		else
			m_pBudgetGroups[budgetGroupID].m_BudgetFlags &= ~BUDGETFLAG_HIDDEN;
	}
}

int *CVProfile::FindOrCreateCounter( const tchar *pName, CounterGroup_t eCounterGroup )
{	
	Assert( m_NumCounters+1 < MAXCOUNTERS );
	if ( m_NumCounters + 1 >= MAXCOUNTERS || !InTargetThread() )
	{
		static int dummy;
		return &dummy;
	}
	int i;
	for( i = 0; i < m_NumCounters; i++ )
	{
		if( _tcsicmp( m_CounterNames[i], pName ) == 0 )
		{
			// found it!
			return &m_Counters[i];
		}
	}

	// NOTE: These get freed in ~CVProfile.
	MEM_ALLOC_CREDIT();
	tchar *pNewName = new tchar[_tcslen( pName ) + 1];
	_tcscpy( pNewName, pName );
	m_Counters[m_NumCounters] = 0;
	m_CounterGroups[m_NumCounters] = (char)eCounterGroup;
	m_CounterNames[m_NumCounters++] = pNewName;
	return &m_Counters[m_NumCounters-1];
}

void CVProfile::ResetCounters( CounterGroup_t eCounterGroup )
{
	int i;
	for( i = 0; i < m_NumCounters; i++ )
	{
		if ( m_CounterGroups[i] == eCounterGroup )
			m_Counters[i] = 0;
	}
}

int CVProfile::GetNumCounters() const
{
	return m_NumCounters;
}

const tchar *CVProfile::GetCounterName( int index ) const
{
	Assert( index >= 0 && index < m_NumCounters );
	return m_CounterNames[index];
}

int CVProfile::GetCounterValue( int index ) const
{
	Assert( index >= 0 && index < m_NumCounters );
	return m_Counters[index];
}

const tchar *CVProfile::GetCounterNameAndValue( int index, int &val ) const
{
	Assert( index >= 0 && index < m_NumCounters );
	val = m_Counters[index];
	return m_CounterNames[index];
}

CounterGroup_t CVProfile::GetCounterGroup( int index ) const
{
	Assert( index >= 0 && index < m_NumCounters );
	return (CounterGroup_t)m_CounterGroups[index];
}

#ifdef _X360
void CVProfile::LatchMultiFrame( int64 cycles )
{
	if ( cycles > m_WorstCycles )
	{
		strncpy( m_WorstTraceFilename, GetCPUTraceFilename(), sizeof( m_WorstTraceFilename ) );
		m_WorstCycles = cycles;
	}
}

void CVProfile::SpewWorstMultiFrame()
{
	CCycleCount cc( m_WorstCycles );
	m_pOutputStream( "%s == %.3f msec\n", m_WorstTraceFilename, cc.GetMillisecondsF() );
}
#endif

#ifdef DBGFLAG_VALIDATE

#ifdef _WIN64
#error the below is presumably broken on 64 bit
#endif // _WIN64

const int k_cSTLMapAllocOffset = 4;
#define GET_INTERNAL_MAP_ALLOC_PTR( pMap ) \
	( * ( (void **) ( ( ( byte * ) ( pMap ) ) + k_cSTLMapAllocOffset ) ) )
//-----------------------------------------------------------------------------
// Purpose: Ensure that all of our internal structures are consistent, and
//			account for all memory that we've allocated.
// Input:	validator -		Our global validator object
//			pchName -		Our name (typically a member var in our container)
//-----------------------------------------------------------------------------
void CVProfile::Validate( CValidator &validator, tchar *pchName )
{
	validator.Push( _T("CVProfile"), this, pchName );

	m_Root.Validate( validator, _T("m_Root") );

	for ( int iBudgetGroup=0; iBudgetGroup < m_nBudgetGroupNames; iBudgetGroup++ )
		validator.ClaimMemory( m_pBudgetGroups[iBudgetGroup].m_pName );

	validator.ClaimMemory( m_pBudgetGroups );

	// The std template map class allocates memory internally, but offer no way to get
	// access to their pointer.  Since this is for debug purposes only and the
	// std template classes don't change, just look at the well-known offset.  This
	// is arguably sick and wrong, kind of like marrying a squirrel.
	validator.ClaimMemory( GET_INTERNAL_MAP_ALLOC_PTR( &g_TimesLessChildren ) );
	validator.ClaimMemory( GET_INTERNAL_MAP_ALLOC_PTR( &g_TimeSumsMap ) );

	validator.Pop( );
}

#endif // DBGFLAG_VALIDATE

#endif // VPROF_ENABLED

#ifdef RAD_TELEMETRY_ENABLED

TelemetryData g_Telemetry;
static HTELEMETRY g_tmContext;
static TmU8 *g_pTmMemoryArena = NULL;
static bool g_TelemetryLoaded = false;

static unsigned int g_TelemetryFrameCount = 0;
static bool g_fTelemetryLevelChanged = false;

static const TmU32 TELEMETRY_ARENA_SIZE = 8 * 1024 * 1024; // How much memory we want Telemetry to use.

struct ThreadNameInfo_t
{
	TSLNodeBase_t base;
	ThreadId_t ThreadID;
	char szName[ 64 ];
};
static CTSSimpleList< ThreadNameInfo_t > g_ThreadNamesList;

static bool g_bThreadNameArrayChanged = false;
static int g_ThreadNameArrayCount = 0;
static ThreadNameInfo_t *g_ThreadNameArray[64];

void TelemetryThreadSetDebugName( ThreadId_t id, const char *pszName )
{
	ThreadNameInfo_t *pThreadNameInfo = new ThreadNameInfo_t;

	if( id == ( uint32 )-1 )
	{
		id = ThreadGetCurrentId();
	}

	pThreadNameInfo->ThreadID = id;
	strncpy( pThreadNameInfo->szName, pszName, ARRAYSIZE( pThreadNameInfo->szName ) );
	pThreadNameInfo->szName[ ARRAYSIZE( pThreadNameInfo->szName ) - 1 ] = 0;
	g_ThreadNamesList.Push( pThreadNameInfo );

	g_bThreadNameArrayChanged = true;
}

static void UpdateTelemetryThreadNames()
{
	if( g_bThreadNameArrayChanged )
	{
		// Go through and add any new thread names we got in our thread safe list to our thread names array.
		for( ThreadNameInfo_t *pThreadNameInfo = g_ThreadNamesList.Pop();
			  pThreadNameInfo;
			  pThreadNameInfo = g_ThreadNamesList.Pop() )
		{
			if( g_ThreadNameArrayCount < ARRAYSIZE( g_ThreadNameArray ) )
			{
				g_ThreadNameArray[ g_ThreadNameArrayCount ] = pThreadNameInfo;
				g_ThreadNameArrayCount++;
			}
			else
			{
				delete pThreadNameInfo;
			}
		}

		tmThreadName( g_tmContext, ThreadGetCurrentId(), "MainThrd" );

		for( int i = 0; i < g_ThreadNameArrayCount; i++ )
		{
			tmThreadName( g_tmContext, g_ThreadNameArray[i]->ThreadID, g_ThreadNameArray[i]->szName );
		}

		g_bThreadNameArrayChanged = false;
	}
}

static bool TelemetryInitialize()
{
	if( g_tmContext )
	{
		TmConnectionStatus status = tmGetConnectionStatus( g_tmContext );

		if( status == TMCS_CONNECTED || status == TMCS_CONNECTING )
			return true;
	}

	TmErrorCode retVal;

	if( !g_TelemetryLoaded )
	{
		// Pass in 0 if you want to use the release mode DLL or 1 if you want to
		// use the checked DLL.  The checked DLL is compiled with optimizations but
		// does extra run time checks and reporting.
		int nLoadTelemetry = tmLoadTelemetry( 0 );

		retVal = tmStartup();
		if ( retVal != TM_OK )
		{
			Warning( "TelemetryInit() failed: tmStartup() returned %d, tmLoadTelemetry() returned %d.\n", retVal, nLoadTelemetry );
			return false;
		}

		if( !g_pTmMemoryArena )
		{
			g_pTmMemoryArena = new TmU8[ TELEMETRY_ARENA_SIZE ];
		}

		retVal = tmInitializeContext( &g_tmContext, g_pTmMemoryArena, TELEMETRY_ARENA_SIZE );
		if ( retVal != TM_OK )
		{
			delete [] g_pTmMemoryArena;
			g_pTmMemoryArena = NULL;

			Warning( "TelemetryInit() failed: tmInitializeContext() returned %d.\n", retVal );
			return false;
		}

		g_TelemetryLoaded = true;
	}

	const char *pGameName = "tf2";

#if defined( IS_WINDOWS_PC )
	char baseExeFilename[512];
	if( GetModuleFileName ( GetModuleHandle( NULL ), baseExeFilename, sizeof( baseExeFilename ) ) )
	{
		char *pExt = strrchr( baseExeFilename, '.' );

		if( pExt )
			*pExt = 0;

		char *pSeparator = strrchr( baseExeFilename, '\\' );

		pGameName = pSeparator ? ( pSeparator + 1 ) : baseExeFilename;
	}

	// If you've got \\perforce\symbols on your _NT_SYMBOL_PATH, tmOpen() can take a massively long
	//	time in the symInitialize() routine. Since we don't really need that, kill it here.
	putenv( "_NT_SYMBOL_PATH=" );
#endif

	const char *pServerAddress = g_Telemetry.ServerAddress[0] ? g_Telemetry.ServerAddress : "localhost";
	TmConnectionType tmType = !V_tier0_stricmp( pServerAddress, "FILE" ) ? TMCT_FILE : TMCT_TCP;

	Msg( "TELEMETRY: Calling tmOpen( %s )...\n", pServerAddress );

	char szBuildInfo[ 2048 ];
	_snprintf( szBuildInfo, ARRAYSIZE( szBuildInfo ), "%s: %s", __DATE__ __TIME__, Plat_GetCommandLineA() );
	szBuildInfo[ ARRAYSIZE( szBuildInfo ) - 1 ] = 0;

	TmU32 TmOpenFlags = TMOF_DEFAULT | TMOF_MINIMAL_CONTEXT_SWITCHES;
	/* TmOpenFlags |= TMOF_DISABLE_CONTEXT_SWITCHES | TMOF_INIT_NETWORKING*/

	retVal = tmOpen( g_tmContext, pGameName, szBuildInfo, pServerAddress, tmType,
		TELEMETRY_DEFAULT_PORT, TmOpenFlags, 1000 );
	if ( retVal != TM_OK )
	{
		Warning( "TelemetryInitialize() failed: tmOpen returned %d.\n", retVal );
		return false;
	}

	Msg( "Telemetry initialized at level %u.\n", g_Telemetry.Level );

    // Make sure we set all the thread names.
	g_bThreadNameArrayChanged = true;
	UpdateTelemetryThreadNames();

	return true;
}

static void TelemetryShutdown( bool InDtor = false )
{
	if( g_tmContext )
	{
		// Msg can't be called here as tier0 may have already been shut down...
		if( !InDtor )
		{
			Msg( "Shutting down telemetry.\n" );
		}

		TmConnectionStatus status = tmGetConnectionStatus( g_tmContext );
		if( status == TMCS_CONNECTED || status == TMCS_CONNECTING )
			tmClose( g_tmContext );

		// Discontinue new usage of the context before shutting it down (multithreading).
		memset( g_Telemetry.tmContext, 0, sizeof( g_Telemetry.tmContext ) );
		HTELEMETRY hShutdown = g_tmContext;
		g_tmContext = NULL;

		tmShutdownContext( hShutdown ); 
		tmShutdown();
		g_TelemetryLoaded = false;
	}
}

// Helper class to initialize Telemetry.
class CTelemetryRegister
{
public:
	CTelemetryRegister() 	{}
	~CTelemetryRegister() 	{ TelemetryShutdown( true ); }
} g_TelemetryRegister;

PLATFORM_INTERFACE void TelemetrySetLevel( unsigned int Level )
{
	if( Level != g_Telemetry.Level )
	{
		DevMsg( "TelemetrySetLevel changed from 0x%x to 0x%x\n", g_Telemetry.Level, Level );

		g_Telemetry.Level = Level;
		g_TelemetryFrameCount = g_Telemetry.FrameCount;
		g_fTelemetryLevelChanged = true;
	}
}

static void TelemetryPlots()
{
	if( g_Telemetry.playbacktick )
	{
		tmPlotU32( TELEMETRY_LEVEL1, TMPT_INTEGER, 0, g_Telemetry.playbacktick, "game/PlaybackTick" );
		g_Telemetry.playbacktick = 0;
	}

	for( int i = 0; i < g_VProfCurrentProfile.GetNumCounters(); i++ )
	{
		if( g_VProfCurrentProfile.GetCounterGroup( i ) == COUNTER_GROUP_TELEMETRY )
		{
			int val;
			const char *name = g_VProfCurrentProfile.GetCounterNameAndValue( i, val );

			tmPlotI32( TELEMETRY_LEVEL1, TMPT_INTEGER, 0, val, name );
		}
	}

	g_VProfCurrentProfile.ResetCounters( COUNTER_GROUP_TELEMETRY );
}

PLATFORM_INTERFACE void TelemetryTick()
{
	static double s_d0 = Plat_FloatTime();
	static TmU64 s_t0 = tmFastTime();

	if( !g_Telemetry.Level && g_Telemetry.DemoTickStart && ( (uint32)g_Telemetry.playbacktick > g_Telemetry.DemoTickStart ) )
	{
		TelemetrySetLevel( 2 );
		g_Telemetry.DemoTickStart = 0;
	}
	if( g_Telemetry.Level && g_Telemetry.DemoTickEnd && ( (uint32)g_Telemetry.playbacktick > g_Telemetry.DemoTickEnd ) )
	{
		TelemetrySetLevel( 0 );
		g_Telemetry.DemoTickEnd = ( uint32 )-1;
	}

	// People can NIL out contexts in the TelemetryData structure to control
	//	the level and what sections to log. We always need to do ticks though,
	//	so use master context for this.
	if( g_tmContext )
	{
		// Update any new thread names.
		UpdateTelemetryThreadNames();

		if ( g_Telemetry.Level > 0 )
			TelemetryPlots();

		// Do a Telemetry Tick.
		tmTick( g_tmContext );

		// Update flRDTSCToMilliSeconds.
		TmU64 s_t1 = tmFastTime();
		double s_d1 = Plat_FloatTime();

		g_Telemetry.flRDTSCToMilliSeconds = 1000.0f / ( ( s_t1 - s_t0 ) / ( s_d1 - s_d0 ) );

		s_d0 = s_d1;
		s_t0 = s_t1;

		// Check if we're only supposed to run X amount of frames.
		if( g_TelemetryFrameCount && !tmIsPaused( g_tmContext ) )
		{
			g_TelemetryFrameCount--;
			if( !g_TelemetryFrameCount )
				TelemetrySetLevel( 0 );
		}
	}

	if( g_fTelemetryLevelChanged )
	{
		g_fTelemetryLevelChanged = false;
		memset( g_Telemetry.tmContext, 0, sizeof( g_Telemetry.tmContext ) );

		if( g_Telemetry.Level == 0 )
		{
			// Calling shutdown here invalidates all the telemetry context handles.
			// Background threads in the middle of Tm__Zone'd calls may crash...
			TelemetryShutdown();
		}
		else
		{
			if( !TelemetryInitialize() )
			{
				g_Telemetry.Level = 0;
			}
			else
			{
				tmPause( g_tmContext, 0 );

				uint32 Level = MIN( g_Telemetry.Level, ARRAYSIZE( g_Telemetry.tmContext ) );
				for( uint32 i = 0; i < Level; i++ )
				{
					g_Telemetry.tmContext[i] = g_tmContext;
				}
			}
		}

		//  	TM_SET_TIMELINE_SECTION_NAME( g_tmContext, "Level:0x%x", g_Telemetry.Level );

		// To disable various telemetry features, use the tmEnable() function as so:
		//	TM_ENABLE( g_tmContext, TMO_SUPPORT_PLOT, 0 );
	}
}

#endif // RAD_TELEMETRY_ENABLED