summaryrefslogtreecommitdiff
path: root/Runtime/Profiler/ProfilerImpl.cpp
blob: 2abafa5fad5f0667a275e012131dafcce1344482 (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
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
#include "UnityPrefix.h"
#include "Profiler.h"
#include "Runtime/BaseClasses/BaseObject.h"

#include <string.h>
#if ENABLE_PROFILER

#include "ProfilerImpl.h"
#include "GPUProfiler.h"
#include "ProfilerHistory.h"
#include "ProfilerFrameData.h"
#include "CollectProfilerStats.h"
#include "ProfilerConnection.h"
#include "Runtime/Misc/BuildSettings.h"
#include "Runtime/Utilities/File.h"
#include "Runtime/Serialize/SwapEndianBytes.h"
#include "Runtime/GfxDevice/GfxDevice.h"
#include "Runtime/Threads/JobScheduler.h"
#include "IntelGPAProfiler.h"
#include "Runtime/Scripting/ScriptingManager.h"

#if UNITY_PS3
#include "External/libsntuner.h"
#define PS3_TUNER_SAMPLE_BEGIN(s) snPushMarker((s)->name)
#define PS3_TUNER_SAMPLE_END() snPopMarker()
#else
#define PS3_TUNER_SAMPLE_BEGIN(s)
#define PS3_TUNER_SAMPLE_END()
#endif


#define DEBUG_AUTO_PROFILER_LOG 0
#if DEBUG_AUTO_PROFILER_LOG && !UNITY_BUILD_COPY_PROTECTED
#error "Must disable DEBUG_AUTO_PROFILER_LOG in deployed build"
#endif

#if UNITY_EDITOR
#include "Editor/Src/EditorHelper.h"
#endif


#if ENABLE_MONO
#include "Runtime/Scripting/CommonScriptingClasses.h"
const int kMonoProfilerDefaultFlags = MONO_PROFILE_GC | MONO_PROFILE_ALLOCATIONS | MONO_PROFILE_EXCEPTIONS;
#endif // #if ENABLE_MONO

const int kProfilerDataStreamVersion = 0x20122123;

void profiler_begin_frame()
{
	if (UnityProfiler::GetPtr())
		UnityProfiler::Get().BeginFrame();
}

void profiler_end_frame()
{
	if (UnityProfiler::GetPtr())
		UnityProfiler::Get().EndFrame();
}

void profiler_start_mode(ProfilerMode flags)
{
	if (UnityProfiler::GetPtr())
		UnityProfiler::Get().StartProfilingMode(flags);
}

void profiler_end_mode(ProfilerMode flags)
{
	if (UnityProfiler::GetPtr())
		UnityProfiler::Get().EndProfilingMode(flags);
}

void profiler_begin_thread_safe(ProfilerInformation* info, const Object* obj)
{
	PS3_TUNER_SAMPLE_BEGIN(info);
	
	UnityProfilerPerThread* prof = UnityProfilerPerThread::ms_InstanceTLS;
	if (prof && prof->m_ProfilerAllowSampling)
	{
		SET_ALLOC_OWNER(NULL);
		prof->BeginSample(info, obj);
	}
}

void profiler_end_thread_safe()
{
	PS3_TUNER_SAMPLE_END();
	
	UnityProfilerPerThread* prof = UnityProfilerPerThread::ms_InstanceTLS;
	if (prof && prof->m_ProfilerAllowSampling)
		prof->EndSample(START_TIME);
}


void profiler_begin(ProfilerInformation* info, const Object* obj)
{
	INTEL_GPA_SAMPLE_BEGIN(info);
	PS3_TUNER_SAMPLE_BEGIN(info);

	UnityProfilerPerThread* prof = UnityProfilerPerThread::ms_InstanceTLS;
	if (prof && prof->m_ProfilerAllowSampling)
	{
		SET_ALLOC_OWNER(NULL);
		prof->BeginSample(info, obj);
	}
}

void profiler_end()
{
	INTEL_GPA_SAMPLE_END();
	PS3_TUNER_SAMPLE_END();

	UnityProfilerPerThread* prof = UnityProfilerPerThread::ms_InstanceTLS;
	if (prof && prof->m_ProfilerAllowSampling)
		prof->EndSample(START_TIME);
}

GpuSection g_CurrentGPUSection = kGPUSectionOther;

void gpu_time_sample()
{
	GPUProfiler::GPUTimeSample();
}

void profiler_initialize_thread (const char* name, bool separateBeginEnd)
{
#if UNITY_EDITOR
	if(IsDeveloperBuild())
		UnityProfilerPerThread::Initialize(name, true);
#endif
}

void profiler_cleanup_thread ()
{
	UnityProfilerPerThread::Cleanup();
}

void profiler_set_active_seperate_thread (bool enabled)
{
	UnityProfiler* prof = UnityProfiler::GetPtr();
	if (prof)
		prof->SetActiveSeparateThread(enabled);
}

void profiler_begin_frame_seperate_thread (ProfilerMode mode)
{
	UnityProfiler* prof = UnityProfiler::GetPtr();
	if (prof)
		prof->BeginFrameSeparateThread(mode);
	
}

void profiler_disable_sampling_seperate_thread ()
{
	UnityProfiler* prof = UnityProfiler::GetPtr();
	if (prof)
		prof->DisableSamplingSeparateThread();
	
}

void profiler_end_frame_seperate_thread (int frameIDAndValid)
{
	UnityProfiler* prof = UnityProfiler::GetPtr();
	if (prof)
		prof->EndFrameSeparateThread(frameIDAndValid);
}
 

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



ProfilerInformation::ProfilerInformation (const char* const functionName, ProfilerGroup grp, bool warn)
: name(functionName)
, group(grp)
, flags(kDefault)
, isWarning(warn)
{
	INTEL_GPA_INFORMATION_INITIALIZE();
}



// -------------------------------------------------------------------------
// Per-thread profiler class

const int kDeepProfilingMaxSamples = 1024 * 1024 * 4; // 80 MB on 32bit
const int kNormalProfilingMaxSamples = 1024 * 512;    // 10 MB on 32bit


UNITY_TLS_VALUE(UnityProfilerPerThread*) UnityProfilerPerThread::ms_InstanceTLS;

void UnityProfilerPerThread::Initialize(const char* threadName, bool separateBeginEnd)
{
	SET_ALLOC_OWNER(UnityProfiler::GetPtr());
	Assert(ms_InstanceTLS == NULL);
	ms_InstanceTLS = UNITY_NEW(UnityProfilerPerThread, kMemProfiler)(threadName, separateBeginEnd);
	
	UnityProfiler::ms_Instance->AddPerThreadProfiler(ms_InstanceTLS);
}

void UnityProfilerPerThread::Cleanup()
{
	if(ms_InstanceTLS == NULL)
		return;
	
	UnityProfiler::ms_Instance->RemovePerThreadProfiler(ms_InstanceTLS);
	
	UnityProfilerPerThread* instance = ms_InstanceTLS;
	UNITY_DELETE(instance, kMemProfiler);
	ms_InstanceTLS = NULL;
}

UnityProfilerPerThread::UnityProfilerPerThread(const char* threadName, bool separateBeginEnd)
: m_ProfilerAllowSampling(false)
, m_ActiveGlobalAllocator(1024 * 128, kMemProfiler)
, m_OutOfSampleMemory(false)
, m_ErrorDurringFrame(false)
, m_ActiveSamples(kMemProfiler)
, m_SampleStack(kMemProfiler)
, m_SampleTimeBeginStack(kMemProfiler)
, m_GPUTimeSamples(kMemProfiler)
, m_InstanceIDSamples(kMemProfiler)
, m_AllocatedGCMemorySamples(kMemProfiler)
, m_WarningSamples(kMemProfiler)
, m_ProfilersListNode(this)
, m_GCCollectTime(0)
, m_ThreadName(threadName)
, m_ThreadIndex(0)
, m_SeparateBeginEnd(separateBeginEnd)
{
	#if ENABLE_THREAD_CHECK_IN_ALLOCS
	Thread::ThreadID tid = Thread::GetCurrentThreadID();
	m_ActiveGlobalAllocator.SetThreadIDs(tid, tid);
	#endif
}

UnityProfilerPerThread::~UnityProfilerPerThread()
{
	m_ActiveGlobalAllocator.purge (true);
	m_ActiveMethodCache.clear();
	m_DynamicMethodCache.clear();
}

void UnityProfilerPerThread::BeginFrame(ProfilerMode mode)
{
	if (mode & kProfilerDeepScripts)
		m_ActiveSamples.resize_uninitialized(kDeepProfilingMaxSamples);
	else
		m_ActiveSamples.resize_uninitialized(kNormalProfilingMaxSamples);
	
	m_ActiveSamples[0] = ProfilerSample();
	m_ActiveSamples[0].startTimeUS = GetProfileTime(START_TIME)/1000;
	m_NextSampleIndex = 1;
	
	m_AllocatedGCMemorySamples.resize_uninitialized(0);
	m_GPUTimeSamples.resize_uninitialized(0);
	m_InstanceIDSamples.resize_uninitialized(0);
	m_WarningSamples.resize_uninitialized(0);

	m_SampleStack.resize_uninitialized(0);
	m_SampleStack.push_back(0);
	
	m_SampleTimeBeginStack.push_back(START_TIME);
	
	m_GCCollectTime = 0;
}


bool UnityProfilerPerThread::EndFrame()
{	
	Assert (!GetIsActive());
	
	ProfilerSample* rootSample = GetRoot();
	if (m_SampleStack.size()>1)
	{
		if (!m_ErrorDurringFrame)
			ErrorString("Too many Profiler.BeginSample (BeginSample and EndSample count must match)");
		m_ErrorDurringFrame = true;
	}
	
	bool ok = (rootSample->nbChildren != 0 && !m_OutOfSampleMemory && !m_ErrorDurringFrame);
	return ok;
}


void UnityProfilerPerThread::ClearFrame () 
{ 
	m_NextSampleIndex = 0;
	
	m_SampleStack.resize_uninitialized(0);
	m_SampleTimeBeginStack.resize_uninitialized(0);	
	m_GPUTimeSamples.resize_uninitialized(0);
	m_InstanceIDSamples.resize_uninitialized(0);
	m_AllocatedGCMemorySamples.resize_uninitialized(0);
	m_WarningSamples.resize_uninitialized(0);

	m_ProfilerAllowSampling = false;
	m_OutOfSampleMemory = false;
	m_ErrorDurringFrame = false;
}


void UnityProfilerPerThread::SetIsActive (bool enabled)
{
	if (!enabled && m_ProfilerAllowSampling)
	{
		if (m_GCCollectTime != 0)
			InjectGCCollectSample();
	}
	m_ProfilerAllowSampling = enabled && !m_SampleStack.empty();
}

void UnityProfilerPerThread::AddMiscSamplesAfterFrame(ProfileTimeFormat frameDuration, bool addOverhead)
{
	// Insert GC.Collect sample if we had one
	if (m_GCCollectTime != 0)
		InjectGCCollectSample();

	ProfilerSample* rootSample = GetRoot();
	if (rootSample)
		rootSample->timeUS = frameDuration / 1000;

	if (addOverhead)
		CreateOverheadSample();
}


ProfilerSample* UnityProfilerPerThread::BeginSample(ProfilerInformation* info, const Object* obj)
{
	DebugAssert(this);
	
	DebugAssert(m_ProfilerAllowSampling);
	DebugAssert(!m_SampleStack.empty() == m_ProfilerAllowSampling);
	
	// Insert GC.Collect sample if we had one
	if (m_GCCollectTime != 0)
		InjectGCCollectSample();
	
	// Add child to children
	m_ActiveSamples[m_SampleStack.back()].nbChildren++;
	
	// Add new sample and insert into stack
	m_SampleStack.push_back(m_NextSampleIndex);
	ProfilerSample* sample = &m_ActiveSamples[m_NextSampleIndex];
	m_NextSampleIndex++;
	if(m_NextSampleIndex > ((int)m_ActiveSamples.size()-1))
	{
		m_NextSampleIndex = m_ActiveSamples.size()-1;
		if(!m_OutOfSampleMemory)
		{
			m_OutOfSampleMemory = true;
			ErrorString("The profiler has run out of samples for this frame. This frame will be skipped.");	
		}		
	}
	
	// Initialize sample object 
	sample->information = info;
	
	if(info->isWarning)
		m_WarningSamples.push_back(GetActiveSampleIndex ());

	sample->nbChildren = 0;
	sample->startTimeUS = 0;
	
	if(obj != NULL)
	{
		ProfilerData::InstanceID instanceSample = {GetActiveSampleIndex(), obj->GetInstanceID()};
		m_InstanceIDSamples.push_back(instanceSample);
	}
	
	// Get current processor time and store it in the sample
	m_SampleTimeBeginStack.push_back(START_TIME);
	return sample;
}


void UnityProfilerPerThread::EndSample(ABSOLUTE_TIME time)
{
	DebugAssert(this);
	
	DebugAssert(m_ProfilerAllowSampling);	
	if(m_SampleStack.size() <= 1)
	{
		if(!m_ErrorDurringFrame)
			ErrorString("Non matching Profiler.EndSample (BeginSample and EndSample count must match)");
		m_ErrorDurringFrame = true;
		return;
	}
	DebugAssert(m_SampleStack.empty() != m_ProfilerAllowSampling);
	
	ProfilerSample* sample = &m_ActiveSamples[m_SampleStack.back()];
	// Set duration and start time
	sample->startTimeUS = GetProfileTime(m_SampleTimeBeginStack.back())/1000;	
	sample->timeUS = GetProfileTime(SUBTRACTED_TIME(time, m_SampleTimeBeginStack.back()))/1000;
	
	// Insert GC.Collect sample if we had one
	if (m_GCCollectTime != 0)
		InjectGCCollectSample();
	
	m_SampleStack.pop_back();
	m_SampleTimeBeginStack.pop_back();	
}


void UnityProfilerPerThread::BeginSampleDynamic(const std::string& name, const Object* obj)
{
	DebugAssert(this);
	if (!m_ProfilerAllowSampling)
		return;
	
	DynamicMethodCache::iterator found = m_DynamicMethodCache.find(name);
	if (found != m_DynamicMethodCache.end())
	{
		BeginSample(&found->second, obj);
	}
	else
	{
		found = m_DynamicMethodCache.insert(make_pair(name, ProfilerInformation(NULL, kProfilerScripts))).first;
		found->second.name = found->first.c_str();
		found->second.group = kProfilerScripts;
		BeginSample(&found->second, obj);
	}
}


void UnityProfilerPerThread::SaveToFrameData (ProfilerFrameData& dst) const
{
	ProfilerFrameData::ThreadData& tdata = dst.m_ThreadData[m_ThreadIndex];
	tdata.m_ThreadName = m_ThreadName;
	tdata.m_AllSamples.assign(m_ActiveSamples.begin(), &m_ActiveSamples[m_NextSampleIndex]);
	tdata.m_GPUTimeSamples.assign(m_GPUTimeSamples.begin(), m_GPUTimeSamples.end());
	tdata.m_InstanceIDSamples.assign(m_InstanceIDSamples.begin(), m_InstanceIDSamples.end());
	tdata.m_AllocatedGCMemorySamples.assign(m_AllocatedGCMemorySamples.begin(), m_AllocatedGCMemorySamples.end());
	tdata.m_WarningSamples.assign(m_WarningSamples.begin(), m_WarningSamples.end());
}



// -------------------------------------------------------------------------
// Global Profiler class



UnityProfiler* UnityProfiler::ms_Instance = NULL;

PROFILER_INFORMATION(gGCCollect, "GC.Collect", kProfilerGC)
PROFILER_INFORMATION(gOverheadProfile, "Overhead", kProfilerOverhead)


#if SUPPORT_THREADS
#define IS_MAIN_THREAD(p) Thread::EqualsCurrentThreadID(p->m_MainThreadID)
#else
#define IS_MAIN_THREAD(p) (true)
#endif


void UnityProfiler::Initialize ()
{
	Assert(ms_Instance == NULL);
	ms_Instance = UNITY_NEW_AS_ROOT(UnityProfiler,kMemProfiler, "Profiler", "");
	UnityProfilerPerThread::Initialize("Main Thread");
}

void UnityProfiler::CleanupGfx ()
{
	Assert(ms_Instance != NULL);
	for (int i = 0; i < kFrameCount; i++)
	{
		if (ms_Instance->m_PreviousFrames[i] != NULL)
			GPUProfiler::ClearTimerQueries(ms_Instance->m_PreviousFrames[i]->m_ThreadData[0].m_GPUTimeSamples);
	}
	ProfilerFrameData::FreeAllTimerQueries();
}

void UnityProfiler::Cleanup ()
{
	UnityProfilerPerThread::Cleanup();
	
	Assert(ms_Instance != NULL);
	UNITY_DELETE(ms_Instance, kMemProfiler);
	ms_Instance = NULL;
}



UnityProfiler::UnityProfiler() 
: m_ProfilerEnabledThisFrame(false)
, m_ProfilerEnabledLastFrame(false)
, m_ProfilerAllowSamplingGlobal(false)
, m_EnabledCount(0)
, m_FramesLogged(0)
, m_TextFile(NULL)
, m_DataFile(NULL)
, m_BinaryLogEnabled(false)
, m_ProfilerCount(0)
, m_FrameIDCounter(1)
{
#if SUPPORT_THREADS
	m_MainThreadID = Thread::GetCurrentThreadID();
#endif

	m_PendingProfilerMode = kProfilerGame;
	m_ProfilerMode = m_PendingProfilerMode;
	ABSOLUTE_TIME_INIT(m_LastEnabledTime);

	memset(m_PreviousFrames, 0, sizeof(m_PreviousFrames));

	#if DEBUG_AUTO_PROFILER_LOG
#if UNITY_OSX
	SetEnabled(true);
	string result = getenv ("HOME");
	SetLogPath(AppendPathName( result, "Library/Logs/Unity/Profiler.log"));
#elif UNITY_WIN
	SetEnabled(true);
	const char* tempPath = ::getenv("TEMP");
	if (!tempPath)
		tempPath = "C:";
	SetLogPath(AppendPathName (tempPath, "UnityProfiler.log"));
#endif
	#endif // #if DEBUG_AUTO_PROFILER_LOG
}


UnityProfiler::~UnityProfiler()
{
	SetLogPath("");
	UNITY_DELETE(m_TextFile, kMemProfiler);
	UNITY_DELETE(m_DataFile, kMemProfiler);
}

void UnityProfiler::AddPerThreadProfiler (UnityProfilerPerThread* prof)
{
	Mutex::AutoLock lock(m_ProfilersMutex);
	m_Profilers.push_back(prof->GetProfilersListNode());
	++m_ProfilerCount;
}

void UnityProfiler::RemovePerThreadProfiler (UnityProfilerPerThread* prof)
{
	if (!this)
		return;
	Mutex::AutoLock lock(m_ProfilersMutex);
	prof->GetProfilersListNode().RemoveFromList();
	--m_ProfilerCount;
}


void UnityProfiler::CheckPro()
{
	if (GetEnabled())
	{
		BuildSettings* buildSettings = GetBuildSettingsPtr();
#if UNITY_EDITOR
		if (buildSettings && !buildSettings->hasPROVersion)
#else
		if (buildSettings && !buildSettings->hasAdvancedVersion)
#endif
		{
			ErrorString("Profiler is only supported in Unity Pro.");
			SetEnabled(false);
		}
	}
}

void UnityProfiler::SetEnabled (bool val) 
{
	BuildSettings* buildSettings = GetBuildSettingsPtr();
#if UNITY_EDITOR
	if (buildSettings && !buildSettings->hasPROVersion)
		return;
#else
	if (buildSettings && !buildSettings->hasAdvancedVersion)
		return;
#endif
	
	if(val)
		m_PendingProfilerMode |= kProfilerEnabled;
	else
		m_PendingProfilerMode &= ~kProfilerEnabled;
}

void UnityProfiler::RecordPreviousFrame(ProfilerMode mode)
{
	UnityProfiler* profiler = UnityProfiler::GetPtr();
	if(!profiler)
		return;

	if(profiler->m_ProfilerEnabledLastFrame)
	{
		GPUProfiler::EndFrame();
		profiler->EndProfilingMode(mode);
		profiler->EndFrame();
		profiler->m_ProfilerEnabledLastFrame = false;
	}
}

bool UnityProfiler::StartNewFrame(ProfilerMode mode)
{
	UnityProfiler* profiler = UnityProfiler::GetPtr();
	if(!profiler)
		return false;

	UnityProfiler::Get().UpdateEnabled();

	if (UnityProfiler::Get().GetEnabled())
	{
		profiler->BeginFrame();
		profiler->StartProfilingMode(mode);
		GPUProfiler::BeginFrame();
		profiler->m_ProfilerEnabledLastFrame = true;
	}

	return profiler->m_ProfilerEnabledLastFrame;
}

void UnityProfiler::BeginFrame () 
{ 
	DebugAssert(IS_MAIN_THREAD(this));
	
	CheckPro();
	
	GfxDevice& device = GetGfxDevice();
	device.ProfileControl(GfxDevice::kGfxProfDisableSampling, 0);
	
	m_ProfilerMode = m_PendingProfilerMode;
	m_ProfilerEnabledThisFrame = m_ProfilerMode & kProfilerEnabled;
	
	{
		Mutex::AutoLock lock(m_ProfilersMutex);
		for(ProfilersList::iterator it = m_Profilers.begin(); it != m_Profilers.end(); ++it)
		{
			UnityProfilerPerThread& prof = **it;
			if (!prof.IsSeparateBeginEnd())
				prof.m_ProfilerAllowSampling = false;
		}
	}
	m_ProfilerAllowSamplingGlobal = false;
	
	if(!m_ProfilerEnabledThisFrame)
		return;
	
	device.ProfileControl(GfxDevice::kGfxProfBeginFrame, m_ProfilerMode);
	{
		Mutex::AutoLock lock(m_ProfilersMutex);
		for(ProfilersList::iterator it = m_Profilers.begin(); it != m_Profilers.end(); ++it)
		{
			UnityProfilerPerThread& prof = **it;
			if (!prof.IsSeparateBeginEnd())
				prof.BeginFrame(m_ProfilerMode);
		}
	}

	
	m_TotalProfilerFrameDuration = START_TIME;
	// accumulates all time from startFrame(N) to startFrame(N+1)
	m_ProfilerEnabledDuration = 0;
}

	
void UnityProfiler::BeginFrameSeparateThread(ProfilerMode mode)
{
	Mutex::AutoLock lock(m_ProfilersMutex);
	UnityProfilerPerThread* profTLS = UnityProfilerPerThread::ms_InstanceTLS;
	if (!profTLS)
		return;
	Assert(profTLS->IsSeparateBeginEnd());

	profTLS->BeginFrame(mode);
}

void UnityProfiler::DisableSamplingSeparateThread()
{
	Mutex::AutoLock lock(m_ProfilersMutex);	
	UnityProfilerPerThread* profTLS = UnityProfilerPerThread::ms_InstanceTLS;
	if (!profTLS)
		return;
	Assert(profTLS->IsSeparateBeginEnd());
	
	profTLS->m_ProfilerAllowSampling = false;
}

void UnityProfiler::SetActiveSeparateThread(bool enabled)
{
	Mutex::AutoLock lock(m_ProfilersMutex);	
	UnityProfilerPerThread* profTLS = UnityProfilerPerThread::ms_InstanceTLS;
	if (!profTLS)
		return;
	Assert(profTLS->IsSeparateBeginEnd());
	
	profTLS->SetIsActive(enabled);
}


void UnityProfiler::EndFrame () 
{
	Assert (m_EnabledCount == 0);

	if (!m_ProfilerEnabledThisFrame)
		return;

	bool profileFrameValid = true;
	int threadIdx = 0;
	{
		Mutex::AutoLock lock(m_ProfilersMutex);
		for(ProfilersList::iterator it = m_Profilers.begin(); it != m_Profilers.end(); ++it)
		{
			UnityProfilerPerThread& prof = **it;
			prof.SetThreadIndex(threadIdx);
			if (!prof.IsSeparateBeginEnd())
			{
				bool threadValid = prof.EndFrame();
				if (threadIdx == 0 && !threadValid)
					profileFrameValid = false;
			}
			++threadIdx;
		}
	}

	int curFrameID = 0;
	if (profileFrameValid)
	{
		StartProfilingMode(kProfilerGame);

		threadIdx = 0;
		{
			Mutex::AutoLock lock(m_ProfilersMutex);
			for(ProfilersList::iterator it = m_Profilers.begin(); it != m_Profilers.end(); ++it)
			{
				UnityProfilerPerThread& prof = **it;
				if (!prof.IsSeparateBeginEnd())
					prof.AddMiscSamplesAfterFrame(m_ProfilerEnabledDuration, threadIdx==0);
				++threadIdx;
			}
		}

		EndProfilingMode(kProfilerGame);
		

		// Collect GPU timing for recent frames (non blocking)
		for (int i = kFrameCount - 2; i >= 0; i--)
		{
			if (m_PreviousFrames[i] != NULL)
				GPUProfiler::CollectGPUTime(m_PreviousFrames[i]->m_ThreadData[0].m_GPUTimeSamples, false);
		}

		// Compute GPU timing for oldest frames (blocking)
		ProfilerFrameData* oldestFrame = m_PreviousFrames[kFrameCount-1];
		if(oldestFrame)
		{
			oldestFrame->m_TotalGPUTimeInMicroSec = GPUProfiler::ComputeGPUTime(oldestFrame->m_ThreadData[0].m_GPUTimeSamples);

			Mutex::AutoLock prevFramesLock(m_PrevFramesMutex);

			LogFrame(oldestFrame);
#if UNITY_EDITOR
			// profilerhistory takes ownership and pushes pointer on a vector;
			ProfilerHistory::Get().AddFrameDataAndTransferOwnership (oldestFrame, ProfilerConnection::GetEditorGuid());
			// allocate new frame
			oldestFrame = UNITY_NEW(ProfilerFrameData, kMemProfiler) (m_ProfilerCount, ++m_FrameIDCounter);
#elif ENABLE_PLAYERCONNECTION
			ProfilerConnection::Get().SendFrameDataToEditor (*oldestFrame); // reuse allocated memory
			// GPUProfiler::ExtractGPUTime(m_PreviousGPUTimeSamples); // TODO use this sceme instead to reduce memory
			// ProfilerConnection::Get().TransferPartialData(...)
			// m_PreviousGPUTimeSamples.swap(m_GPUTimeSamples);
#endif
		}

		ProfilerFrameData* curFrame = NULL;
		{
			Mutex::AutoLock prevFramesLock(m_PrevFramesMutex);
			for (int i = 1; i < kFrameCount; i++)
				m_PreviousFrames[i] = m_PreviousFrames[i - 1];

			m_PreviousFrames[0] = oldestFrame;

			if (m_PreviousFrames[0] == NULL)
			{
				m_PreviousFrames[0] = UNITY_NEW(ProfilerFrameData,kMemProfiler) (m_ProfilerCount, ++m_FrameIDCounter);
			}
			
			curFrame = m_PreviousFrames[0];
		}

		curFrameID = curFrame->m_FrameID;
		{
			Mutex::AutoLock lock(m_ProfilersMutex);
			for(ProfilersList::iterator it = m_Profilers.begin(); it != m_Profilers.end(); ++it)
			{
				UnityProfilerPerThread& prof = **it;
				if (!prof.IsSeparateBeginEnd())
					prof.SaveToFrameData(*curFrame);
			}
		}

		CollectProfilerStats(curFrame->allStats);
	}

	const unsigned frameIDAndValidFlag = curFrameID | (profileFrameValid ? 0x80000000 : 0);
	GetGfxDevice().ProfileControl(GfxDevice::kGfxProfEndFrame, frameIDAndValidFlag);
	#if ENABLE_JOB_SCHEDULER
	GetJobScheduler().EndProfilerFrame (frameIDAndValidFlag);
	#endif

	ABSOLUTE_TIME frameStartTime = m_TotalProfilerFrameDuration;
	m_TotalProfilerFrameDuration = SUBTRACTED_TIME(START_TIME, frameStartTime);
	if (profileFrameValid)
	{
			m_PreviousFrames[0]->m_StartTimeUS = GetProfileTime(frameStartTime) / 1000;
			m_PreviousFrames[0]->m_TotalCPUTimeInMicroSec = GetProfileTime(m_TotalProfilerFrameDuration) / 1000;
	}

	{
		Mutex::AutoLock lock(m_ProfilersMutex);
		for(ProfilersList::iterator it = m_Profilers.begin(); it != m_Profilers.end(); ++it)
		{
			UnityProfilerPerThread& prof = **it;
			if (!prof.IsSeparateBeginEnd())
				prof.ClearFrame();
		}
	}
	m_ProfilerEnabledThisFrame = false;
	m_ProfilerAllowSamplingGlobal = false;
}



void UnityProfiler::EndFrameSeparateThread(unsigned frameIDAndValid)
{
	Mutex::AutoLock lock(m_ProfilersMutex);
	UnityProfilerPerThread* profTLS = UnityProfilerPerThread::ms_InstanceTLS;
	if (!profTLS)
		return;
	Assert(profTLS->IsSeparateBeginEnd());

	profTLS->EndFrame();

	const bool frameValid = (frameIDAndValid & 0x80000000);
	if (frameValid)
	{
		Mutex::AutoLock prevFramesLock(m_PrevFramesMutex);
		int frameID = frameIDAndValid & 0x7fffffff;
		for (int i = 0; i < kFrameCount; ++i)
		{
			ProfilerFrameData* frame = m_PreviousFrames[i];
			if (!frame)
				continue;
			if (frame->m_FrameID != frameID)
				continue;
			profTLS->SaveToFrameData(*frame);
		}
	}

	profTLS->ClearFrame();
}


void UnityProfiler::ClearPendingFrames()
{
	Mutex::AutoLock prevFramesLock(m_PrevFramesMutex);
	for (int i = 0; i < kFrameCount; i++)
	{
		UNITY_DELETE(m_PreviousFrames[i],kMemProfiler);
		m_PreviousFrames[i] = NULL;
	}
}


const ProfilerSample* UnityProfilerPerThread::GetActiveSample(int parentLevel) const
{
	const int indexInStack = m_SampleStack.size()-1 - parentLevel;
	if (indexInStack < 0 || indexInStack >= m_SampleStack.size())
		return NULL;
	const int sampleIndex = m_SampleStack[indexInStack];
	const ProfilerSample* sample = &m_ActiveSamples[sampleIndex];
	return sample;
}


void UnityProfilerPerThread::InjectGCCollectSample() 
{
	ProfileTimeFormat collectTime = m_GCCollectTime;
	m_GCCollectTime = 0;

	BeginSample(&gGCCollect, NULL);
	ProfilerSample* gcSample = GetActiveSample();
	EndSample(START_TIME);
	gcSample->timeUS = collectTime/1000;
}


void UnityProfilerPerThread::CreateOverheadSample()
	{
	BeginSample(&gOverheadProfile, NULL);
	ProfilerSample* overheadSample = GetActiveSample();
	EndSample(START_TIME);

	// Calculate overhead time be taking the root time and subtracting all children.
	ProfilerSample* root = GetRoot();
	ProfileTimeFormat overheadTime = root->timeUS * 1000;
	const ProfilerSample* sample = root + 1;
	for (int i=0;i<root->nbChildren;i++)
	{
		overheadTime -= sample->timeUS*1000;
		sample = SkipSampleRecurse(sample);
	}
	overheadSample->timeUS += overheadTime/1000;
}


const ProfilerSample* SkipSampleRecurse (const ProfilerSample* sample)
{
	const ProfilerSample* child = sample + 1;
	for (int i=0;i<sample->nbChildren;i++)
		child = SkipSampleRecurse(child);

	return child;
}


static void UpdateWithSmallestTime (ABSOLUTE_TIME& val, ABSOLUTE_TIME newval, int iteration)
{
	if (iteration == 0 || IsSmallerAbsoluteTime (newval, val))
		val = newval;
}


void UnityProfiler::StartProfilingMode (ProfilerMode mode)
{
	if(m_ProfilerMode & mode)
	{
		SetIsActive(true);
	}
}

void UnityProfiler::EndProfilingMode (ProfilerMode mode)
{
	if(m_ProfilerMode & mode)
		SetIsActive(false);
}


void UnityProfiler::SetIsActive (bool enabled)
{
	if (enabled)
	{
		Mutex::AutoLock lock(m_ProfilersMutex);
		for(ProfilersList::iterator it = m_Profilers.begin(); it != m_Profilers.end(); ++it)
		{
			UnityProfilerPerThread& prof = **it;
			if (!prof.IsSeparateBeginEnd())
				prof.ClearGCCollectTime();
		}
	}
		
	if(!m_ProfilerEnabledThisFrame)
		return;
	// m_EnabledCount can go below 0 (double disable), but will not start before enableCount is 1
	m_EnabledCount += (enabled?1:-1);
	if ( enabled && (m_EnabledCount != 1))
		return;

	if (!enabled && (m_EnabledCount != 0))
		return;
	
	
	if (!enabled && m_ProfilerAllowSamplingGlobal)
	{
		m_ProfilerEnabledDuration += GetProfileTime(SUBTRACTED_TIME(START_TIME, m_LastEnabledTime));
		ABSOLUTE_TIME_INIT(m_LastEnabledTime);
	}
	
	m_ProfilerAllowSamplingGlobal = enabled;
	{
		Mutex::AutoLock lock(m_ProfilersMutex);
		for(ProfilersList::iterator it = m_Profilers.begin(); it != m_Profilers.end(); ++it)
		{
			UnityProfilerPerThread& prof = **it;
			if (!prof.IsSeparateBeginEnd())
				prof.SetIsActive (enabled);
		}
	}
	GetGfxDevice().ProfileControl(GfxDevice::kGfxProfSetActive, enabled ? 1 : 0);
	
	if (enabled && m_ProfilerAllowSamplingGlobal)
	{
		Assert(GetProfileTime (m_LastEnabledTime) == 0);
		m_LastEnabledTime = START_TIME;
	}
}


void UnityProfiler::GetDebugStats (DebugStats& debugStats)
{
	Mutex::AutoLock lock(m_ProfilersMutex);	
	debugStats.m_ProfilerMemoryUsage = 0;
	debugStats.m_AllocatedProfileSamples = 0;

	debugStats.m_ProfilerMemoryUsageOthers = 0;
	for(ProfilersList::iterator it = m_Profilers.begin(); it != m_Profilers.end(); ++it)
	{
		debugStats.m_ProfilerMemoryUsageOthers += (*it)->GetAllocatedBytes();
	}
}

void UnityProfiler::CleanupMonoMethodCaches()
{
	Mutex::AutoLock lock(m_ProfilersMutex);	
	for(ProfilersList::iterator it = m_Profilers.begin(); it != m_Profilers.end(); ++it)
	{
		(*it)->CleanupMonoMethodCache();
	}
}


#if ENABLE_MONO || UNITY_WINRT

ProfilerSample* mono_profiler_begin(ScriptingMethodPtr method, ScriptingClassPtr profileKlass, ScriptingObjectPtr instance)
{
	UnityProfilerPerThread* profTLS = UnityProfilerPerThread::ms_InstanceTLS;
	if (!profTLS || !profTLS->m_ProfilerAllowSampling)
		return NULL;
	UnityProfiler* profiler = UnityProfiler::ms_Instance;
	
	if (!IS_MAIN_THREAD(profiler))
		return NULL;
	
	// If deep profiling, we return the current sample, which is the one we have to get back to when this call exits
	if ((profiler->m_ProfilerMode & kProfilerDeepScripts) != 0)
		return profTLS->GetActiveSample();
	
	DebugAssert(profTLS->m_SampleStack.empty() != profTLS->m_ProfilerAllowSampling);

	// Extract Object ptr for profiler
	Object* objPtr = NULL;

#if ENABLE_MONO
	if (instance)
	{
		MonoClass* instanceClass = mono_object_get_class(instance);
		if (mono_class_is_subclass_of(instanceClass, MONO_COMMON.unityEngineObject, false))
			objPtr = ScriptingObjectOfType<Object>(instance).GetPtr();
	}
#endif
	
	// Do we have this method's info in the cache?
	ProfilerInformation* information;
	UnityProfilerPerThread::MethodInfoCache::iterator it = profTLS->m_ActiveMethodCache.find(method);
	if (it != profTLS->m_ActiveMethodCache.end())
		information = it->second;
	else
		information = profTLS->CreateProfilerInformationForMethod(instance, method, scripting_method_get_name(method), profileKlass, ProfilerInformation::kScriptMonoRuntimeInvoke);
	
	// Begin Sample
	return profTLS->BeginSample(information, objPtr);
}

void mono_profiler_end(ProfilerSample* beginsample)
{
	UnityProfilerPerThread* profTLS = UnityProfilerPerThread::ms_InstanceTLS;
	if (!profTLS || !profTLS->m_ProfilerAllowSampling)
		return;
	UnityProfiler* profiler = UnityProfiler::ms_Instance;
	
	if (!IS_MAIN_THREAD(profiler))
		return;

	// roll back the stack if there has been an exception, and some end samples have been skipped
	while(profTLS->GetActiveSample() != beginsample)
		profTLS->EndSample(START_TIME);		

	// if not deep profiling, end the current sample
	if ((profiler->m_ProfilerMode & kProfilerDeepScripts) == 0)
		profTLS->EndSample(START_TIME);
}

#endif // #if ENABLE_MONO || UNITY_WINRT


ProfilerInformation* UnityProfilerPerThread::CreateProfilerInformationForMethod(ScriptingObjectPtr object, ScriptingMethodPtr method, const char* methodName, ScriptingTypePtr profileKlass, int flags)
{
#if !ENABLE_MONO && !UNITY_WINRT
	return 0;
#else // ENABLE_MONO || UNITY_WINRT

	ProfilerInformation* information = static_cast<ProfilerInformation*> (m_ActiveGlobalAllocator.allocate(sizeof(ProfilerInformation)));
	information->group = kProfilerScripts;
	information->flags = flags;
	information->isWarning = false;

#if ENABLE_MONO
	const char* klassName = mono_class_get_name(mono_method_get_class(method->monoMethod));
#else	// UNITY_WINRT
	const char* klassName = "";
	if (object != SCRIPTING_NULL)
	{
		ScriptingTypePtr klass = scripting_object_get_class(object, GetScriptingTypeRegistry());
		if (klass != NULL)
			klassName = scripting_class_get_name(klass);
	}
#endif

	if (profileKlass == NULL)
	{
		// Optimized snprintf(buffer, kNameBufferSize, "%s.%s()", klassName, methodName); to minimize profiler overhead
		int size = 4;
		for(int i=0;i<klassName[i] != 0;i++) { size++; }
		for(int i=0;i<methodName[i] != 0;i++) { size++; }
		
		char* allocatedBuffer = static_cast<char*> (m_ActiveGlobalAllocator.allocate(size));
		information->name = allocatedBuffer;
		char* c = allocatedBuffer;
		for(int i=0;i<klassName[i] != 0;i++)
		{
			*c = klassName[i]; c++;
		}
		
		*c = '.'; c++;
		for(int i=0;i<methodName[i] != 0;i++)
		{
			*c = methodName[i]; c++;
		}
		
		c[0] = '(';
		c[1] = ')';
		c[2] = 0;

		// Put into method cache
		m_ActiveMethodCache.insert (std::make_pair(method, information));
		
		return information;
	}
	else
	{
		enum { kNameBufferSize = 256 };
		char buffer[kNameBufferSize];
		char coroutineMethodName[kNameBufferSize] = { 0 };
		
		// The generated class for a coroutine is called "<Start>Iterator_1"
		// We want to extract "Start" and use that as the method name.
		const char* coroutineMethodNameEnd = NULL;
		if (klassName[0] == '<')
			coroutineMethodNameEnd = strchr(klassName, '>');
		
		if (coroutineMethodNameEnd != NULL)
			strncpy(coroutineMethodName, klassName + 1, std::min((int)(coroutineMethodNameEnd - (klassName + 1)), (int)kNameBufferSize));
		else
			strncpy(coroutineMethodName, klassName, kNameBufferSize);
			
		snprintf(buffer, kNameBufferSize, "%s.%s() [Coroutine: %s]", scripting_class_get_name(profileKlass), coroutineMethodName, methodName);
		
		int size = strlen(buffer)+1;
		char* copyBuffer = static_cast<char*> (m_ActiveGlobalAllocator.allocate(size));
		memcpy(copyBuffer, buffer, size);
		information->name = copyBuffer;

		// Put into method cache
		m_ActiveMethodCache.insert (std::make_pair(method, information));
		
		return information;
	}
#endif // ENABLE_MONO || UNITY_WINRT
}	



ProfilerInformation* UnityProfilerPerThread::GetProfilerInformation (const std::string& name, UInt16 group, UInt16 flags, bool isWarning)
{
	DynamicMethodCache::iterator found = m_DynamicMethodCache.find(name);
	if (found != m_DynamicMethodCache.end())
		return &found->second;
	else
	{
		// Put into method cache
		DynamicMethodCache::iterator found = m_DynamicMethodCache.insert (std::make_pair(name, ProfilerInformation(NULL, (ProfilerGroup) group))).first;
		found->second.name = found->first.c_str();
		found->second.flags = flags;
		found->second.isWarning = isWarning;
		return &found->second;
	}
}
	

void UnityProfilerPerThread::EnterMonoMethod(MonoMethod *method)
{
#if ENABLE_MONO
	if (!m_ProfilerAllowSampling)
		return;
	const char* methodName = mono_method_get_name(method);
	if (strncmp (methodName, "runtime_invoke", 14) == 0)
		return;

	// Do we have this method's info in the cache?
	ScriptingMethodPtr scriptingMethod = GetScriptingMethodRegistry().GetMethod(method);
	MethodInfoCache::iterator it = m_ActiveMethodCache.find(scriptingMethod);
	if (it != m_ActiveMethodCache.end())
	{
		BeginSample (it->second, NULL);
		return;
	}

	// Method info not in the cache; create and cache it
	ProfilerInformation* information = CreateProfilerInformationForMethod(NULL, scriptingMethod, methodName, NULL, ProfilerInformation::kScriptEnterLeave);
	
	// Begin Sample
	BeginSample(information, NULL);
	#endif // #if ENABLE_MONO
} 


void UnityProfilerPerThread::LeaveMonoMethod(MonoMethod *method)
{
#if ENABLE_MONO
	if (!m_ProfilerAllowSampling)
		return;
	const char* methodName = mono_method_get_name(method);
	if (strncmp (methodName, "runtime_invoke", 14) == 0)
		return;
	
	ABSOLUTE_TIME time = START_TIME;
	EndSample(time);
	#endif // #if ENABLE_MONO
}


void UnityProfilerPerThread::SampleGCAllocation (MonoObject *obj, MonoClass *klass)
{
#if ENABLE_MONO
	if (!m_ProfilerAllowSampling)
		return;
	
#if 0
	// We can extract the name of the class being allocated here.
	string info = Format("\n\t\t%s size: %d", mono_class_get_name(klass), size);
#endif	
	
	int size = mono_object_get_size(obj);
	ProfilerData::AllocatedGCMemory allocSample = {GetActiveSampleIndex(), size};
	m_AllocatedGCMemorySamples.push_back(allocSample);
	#endif // #if ENABLE_MONO
}


static void enter_mono_sample(void* pr, MonoMethod* method)
{
	UnityProfilerPerThread* prof = UnityProfilerPerThread::ms_InstanceTLS;
	if (prof)
		prof->EnterMonoMethod(method);
}

static void leave_mono_sample(void* pr, MonoMethod* method)
{
	UnityProfilerPerThread* prof = UnityProfilerPerThread::ms_InstanceTLS;
	if (prof)
		prof->LeaveMonoMethod(method);
}


#if ENABLE_MONO_MEMORY_PROFILER

void UnityProfiler::SetupProfilerEvents ()
{
	int flags = kMonoProfilerDefaultFlags;
	if (m_PendingProfilerMode & kProfilerDeepScripts)
		flags |= MONO_PROFILE_ENTER_LEAVE;
	
	mono_profiler_set_events (flags);
}

static void sample_mono_shutdown (void *prof)
{
}
	

void UnityProfilerPerThread::SampleGCMonoCallback (void* pr, int event, int generation)
{
	if (event == 1)
	{
		UnityProfilerPerThread* prof = UnityProfilerPerThread::ms_InstanceTLS;
		if (prof)
		{
			prof->m_GCStartTime = START_TIME;
		}
	}

	if (event == 4)
	{
		UnityProfilerPerThread* prof = UnityProfilerPerThread::ms_InstanceTLS;
		if (prof)
		{
			prof->m_GCCollectTime += GetProfileTime(ELAPSED_TIME(prof->m_GCStartTime));
		}
	}
}

static void sample_gc_resize (void *pr, SInt64 new_size)
{
	//	printf_console("--- GC resize %d\n", (int)new_size);
}

static void sample_allocation (void* pr, MonoObject *obj, MonoClass *klass)
{
	UnityProfilerPerThread* prof = UnityProfilerPerThread::ms_InstanceTLS;
	if (prof)
		prof->SampleGCAllocation(obj, klass);
}


void mono_profiler_startup ()
{
	mono_profiler_install (NULL, sample_mono_shutdown);
	
	mono_profiler_install_gc(UnityProfilerPerThread::SampleGCMonoCallback, sample_gc_resize);
	mono_profiler_install_allocation(sample_allocation);
#if UNITY_EDITOR
	// Deep profiling is only be available in editor
	mono_profiler_install_enter_leave (enter_mono_sample, leave_mono_sample);
#endif
	int flags = kMonoProfilerDefaultFlags;
	mono_profiler_set_events (flags);
}

#endif // ENABLE_MONO_MEMORY_PROFILER



void UnityProfiler::LogFrame(ProfilerFrameData* data)
{
#if !UNITY_PEPPER
	if (m_LogFile.empty())
		return;

	float fps = 1000000.0 / data->m_ThreadData[0].GetRoot()->timeUS;

#if DEBUG_AUTO_PROFILER_LOG
	const int kMinimumFramerate = 20;
	if (fps > kMinimumFramerate)
		return;
#endif

	{	
		string fpsCategory;
		if (fps < 10)
			fpsCategory = "Very Low";
		else if (fps < 20)
			fpsCategory = "Low";
		else if (fps < 25)
			fpsCategory = "Okay";
		else if (fps < 25)
			fpsCategory = "Average";
		else if (fps < 40)
			fpsCategory = "Good";
		else
			fpsCategory = "Very Good";
		std::string output = Format(" -- Frame %d Framerate: %.1f [%s Framerate]\n", ++m_FramesLogged, fps, fpsCategory.c_str());

		m_TextFile->Write(output.c_str(),output.length());
	}

	if(m_BinaryLogEnabled)
	{
#if ENABLE_PLAYERCONNECTION
		/// TODO: make async write. Causes stalls
		dynamic_array<int> buffer;

		SerializeFrameData(*data, buffer);
		int size = buffer.size()*sizeof(int);
		m_DataFile->Write(&size,sizeof(size));
		int threadCount = data->m_ThreadCount;
		m_DataFile->Write(&threadCount, sizeof(threadCount));
		m_DataFile->Write(buffer.begin(), size);
#endif
	}
#endif
}

void UnityProfiler::SetLogPath (std::string logPath)
{
#if WEBPLUG
	if(!logPath.empty())
	{
		std::string name(GetConsoleLogPath());
		ConvertSeparatorsToUnity(name);
		logPath = DeleteLastPathNameComponent(name)+"/profile.log";
	}
#endif
	if (m_LogFile != logPath)
	{
		m_LogFile = logPath;
#if !UNITY_PEPPER
		if (!logPath.empty())
		{
			m_FramesLogged = 0;
			if(!m_TextFile)
				m_TextFile = UNITY_NEW(File, kMemProfiler);
			if(!m_DataFile)
				m_DataFile = UNITY_NEW(File, kMemProfiler);
			m_TextFile->Open(m_LogFile, File::kWritePermission);
			m_DataFile->Open(m_LogFile+".data", File::kWritePermission);
		}
		else
		{
			if(m_TextFile)
				m_TextFile->Close();
			if(m_DataFile)
				m_DataFile->Close();
		}
#endif
	}
}
void UnityProfiler::AddFramesFromFile(string path)
{
#if UNITY_EDITOR
	File dataFile;
	dataFile.Open(path+".data", File::kReadPermission);
	int size;
	dynamic_array<int> buffer;
	while(dataFile.Read(&size,sizeof(size)))
	{
		int threadCount;
		dataFile.Read(&threadCount,sizeof(threadCount));
		buffer.resize_uninitialized(size/sizeof(int));
		dataFile.Read(buffer.begin(),size);
		ProfilerFrameData* frame = UNITY_NEW(ProfilerFrameData, kMemProfiler) (threadCount, 0);
	
		int fileguid = ProfilerConnection::Get().GetConnectedProfiler();
		if( UnityProfiler::DeserializeFrameData(frame,buffer.begin(),size) )
			ProfilerHistory::Get().AddFrameDataAndTransferOwnership(frame, fileguid);
		else
			UNITY_DELETE(frame, kMemProfiler);	
	}
	dataFile.Close();
#endif
}

void UnityProfiler::SerializeFrameData(ProfilerFrameData& frame, dynamic_array<int>& buffer)
{
#if ENABLE_PLAYERCONNECTION
	buffer.push_back(UNITY_LITTLE_ENDIAN);
	buffer.push_back(kProfilerDataStreamVersion);

	frame.Serialize(buffer);

	buffer.push_back(0xAFAFAFAF);
#endif
}

bool UnityProfiler::DeserializeFrameData(ProfilerFrameData* frame, const void* data, int size)
{		
#if ENABLE_PLAYERCONNECTION
	int* buffer = (int*)data;
	int wordsize = size/sizeof(int);
	int* endBuffer = buffer + wordsize;
	int** bitstream = &buffer;

	int dataIsLittleEndian = *((*bitstream)++);
	bool shouldswap = UNITY_LITTLE_ENDIAN ? dataIsLittleEndian == 0 : dataIsLittleEndian != 0;
	if(shouldswap)
	{
		int* ptr = *bitstream;
		while(ptr < endBuffer)
			SwapEndianBytes(*(ptr++));
	}

	int version = *((*bitstream)++);

	if(version != kProfilerDataStreamVersion)
		return false;

	frame->Deserialize(bitstream, shouldswap);
	Assert(**bitstream == 0xAFAFAFAF);
	return true;
#else
	return false;
#endif
}


// --------------------------------------------------------------------------
// Profiler serialization


#if ENABLE_PLAYERCONNECTION
template< class T >
void WriteArray(dynamic_array<int>& bitstream, const dynamic_array<T>& array)
{
	bitstream.push_back(array.size());
	if(array.size() > 0)
	{
		int startindex = bitstream.size();
		bitstream.resize_uninitialized( startindex + array.size() * sizeof(T) / sizeof(int) );
		memcpy( (char*)&bitstream[startindex], (char*)&array[0], sizeof(T) * array.size() );
	}
}

template< class T >
void ReadArray( int** bitstream, dynamic_array<T>& array)
{
	int size = *((*bitstream)++);
	array.resize_uninitialized(size);
	if(size > 0)
	{
		memcpy((char*)&array[0], (char*)*bitstream, sizeof(T) * size);
		*bitstream += sizeof(T) * size / sizeof(int);
	}
}

template< typename T >
void ReadArrayFixup( int** bitstream, dynamic_array<T>& array, bool swapdata)
{
	ReadArray<T>(bitstream, array);
	if (swapdata)
	{
		for (typename dynamic_array<T>::iterator it = array.begin(); it != array.end(); ++it)
		{
			(*it).Fixup();
		}
	}
}

void WriteConditionaly(dynamic_array<int>& bitstream, ProfilerInformation* object)
{
	if (object)
	{
		bitstream.push_back(1);
		ProfilerFrameData::SerializeProfilerInformation(*object, bitstream);
	}
	else
		bitstream.push_back(0);
}

void ReadConditionaly( int** bitstream, ProfilerInformation*& object, bool swapdata)
{
	int condition = *((*bitstream)++);
	if(condition)
		object = ProfilerFrameData::DeserializeProfilerInformation(bitstream, swapdata);
}

static void SerializeString (dynamic_array<int>& bitstream, int len, const char* str)
{
	int startindex = bitstream.size();
	bitstream.resize_initialized( startindex + len/4 + 1);
	memcpy((char*)&bitstream[startindex], str, len+1);
}

static std::string DeserializeString (int**& bitstream, bool swapdata)
{
	char* chars = (char*)*bitstream;
	if (swapdata)
	{
		int wordcount = strlen(chars)/4 + 1;
		for(int i = 0; i < wordcount; i++)
			SwapEndianBytes((*bitstream)[i]);
	}
	std::string name((char*)*bitstream);
	(*bitstream) += name.length()/4 + 1;
	return name;
}


#define HIPART(x) ((x>>32) & 0xFFFFFFFF)
#define LOPART(x) (x & 0xFFFFFFFF)

void ProfilerFrameData::Serialize( dynamic_array<int>& bitstream )
{
	bitstream.push_back(frameIndex);
	bitstream.push_back(realFrame);
	bitstream.push_back(m_StartTimeUS);
	bitstream.push_back(m_TotalCPUTimeInMicroSec);
	bitstream.push_back(m_TotalGPUTimeInMicroSec);
	allStats.Serialize(bitstream);

	bitstream.push_back(m_ThreadCount);
	for (int t = 0; t < m_ThreadCount; ++t)
	{
		const ThreadData& tdata = m_ThreadData[t];
		
		SerializeString(bitstream, tdata.m_ThreadName.size(), tdata.m_ThreadName.c_str());
		bitstream.push_back(tdata.m_AllSamples.size());
		for(int i = 0; i < tdata.m_AllSamples.size(); i++)
		{
			bitstream.push_back(tdata.m_AllSamples[i].timeUS);
			bitstream.push_back(tdata.m_AllSamples[i].startTimeUS);
			bitstream.push_back(tdata.m_AllSamples[i].nbChildren);
		}
		
		bitstream.push_back(tdata.m_GPUTimeSamples.size());
		for(int i = 0; i < tdata.m_GPUTimeSamples.size(); i++)
		{
				bitstream.push_back(tdata.m_GPUTimeSamples[i].gpuTimeInMicroSec);
				bitstream.push_back(tdata.m_GPUTimeSamples[i].relatedSampleIndex);
				bitstream.push_back(tdata.m_GPUTimeSamples[i].gpuSection);
		}
		
		// Don't write m_InstanceIDSamples, since the IDs are not portable
		WriteArray(bitstream, tdata.m_AllocatedGCMemorySamples);
		for(int i = 0; i < tdata.m_AllSamples.size(); i++)
			WriteConditionaly(bitstream,tdata.m_AllSamples[i].information);	

		WriteArray(bitstream, tdata.m_WarningSamples);
	}
}

void ProfilerFrameData::Deserialize( int** bitstream, bool swapdata )
{
	frameIndex = *((*bitstream)++);
	realFrame = *((*bitstream)++);
	m_StartTimeUS = *((*bitstream)++);
	m_TotalCPUTimeInMicroSec = *((*bitstream)++);
	m_TotalGPUTimeInMicroSec = *((*bitstream)++);
	allStats.Deserialize(bitstream, swapdata);

	int threadCount = *((*bitstream)++);
	if (threadCount != m_ThreadCount)
	{
		delete[] m_ThreadData;
		m_ThreadData = new ThreadData[threadCount];
		m_ThreadCount = threadCount;
	}

	for (int t = 0; t < m_ThreadCount; ++t)
	{
		ThreadData& tdata = m_ThreadData[t];

		tdata.m_ThreadName = DeserializeString (bitstream, swapdata);
		
		tdata.m_AllSamples.resize_uninitialized(*((*bitstream)++));
		
		for(int i = 0; i < tdata.m_AllSamples.size(); i++)
		{
			tdata.m_AllSamples[i].timeUS = *((*bitstream)++);
			tdata.m_AllSamples[i].startTimeUS = *((*bitstream)++);
			tdata.m_AllSamples[i].nbChildren = *((*bitstream)++);
			tdata.m_AllSamples[i].information = NULL;
		}

		tdata.m_GPUTimeSamples.resize_uninitialized(*((*bitstream)++));
		for(int i = 0; i < tdata.m_GPUTimeSamples.size(); i++)
		{
			tdata.m_GPUTimeSamples[i].gpuTimeInMicroSec = *((*bitstream)++);
			tdata.m_GPUTimeSamples[i].relatedSampleIndex = *((*bitstream)++);
			tdata.m_GPUTimeSamples[i].gpuSection = (GpuSection)(*((*bitstream)++));
			tdata.m_GPUTimeSamples[i].timerQuery = NULL;
		}
	
		// m_InstanceIDSamples are not written, since the IDs are not portable
		tdata.m_InstanceIDSamples.resize_uninitialized(0);
		ReadArray(bitstream, tdata.m_AllocatedGCMemorySamples);
		for(int i = 0; i < tdata.m_AllSamples.size(); i++)
			ReadConditionaly(bitstream, tdata.m_AllSamples[i].information, swapdata);

		ReadArray(bitstream, tdata.m_WarningSamples);
	}
}


void ProfilerFrameData::SerializeProfilerInformation( const ProfilerInformation& info, dynamic_array<int>& bitstream )
{
	SerializeString (bitstream, strlen(info.name), info.name);
	bitstream.push_back((info.group << 16) | (info.flags << 8) | info.isWarning);
}

ProfilerInformation* ProfilerFrameData::DeserializeProfilerInformation( int** bitstream, bool swapdata )
{
	std::string name = DeserializeString (bitstream, swapdata);

	int groupFlags = *((*bitstream)++);
	UInt16 group = groupFlags >> 16;
	UInt8 flags = (groupFlags & 0xFF00) >> 8;
	UInt8 warn = groupFlags & 0xFF;

	UnityProfilerPerThread* prof = UnityProfilerPerThread::ms_InstanceTLS;
	DebugAssert(prof);
	return prof->GetProfilerInformation(name, group, flags, warn);
}

#endif // #if ENABLE_PLAYERCONNECTION




#endif // #if ENABLE_PROFILER