forked from skalenetwork/skale-consensus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
microprofile.cpp
6941 lines (6181 loc) · 249 KB
/
microprofile.cpp
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
#define MICROPROFILE_IMPL
#include "microprofile.h"
#if MICROPROFILE_ENABLED
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#pragma GCC diagnostic ignored "-Wformat-truncation="
#pragma GCC diagnostic ignored "-Wimplicit-fallthrough="
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <atomic>
#include <mutex>
#include <thread>
#define MICROPROFILE_MAX_COUNTERS 512
#define MICROPROFILE_MAX_COUNTER_NAME_CHARS ( MICROPROFILE_MAX_COUNTERS * 16 )
#define MICROPROFILE_MAX_GROUP_INTS ( MICROPROFILE_MAX_GROUPS / 32 )
#define MICROPROFILE_MAX_CATEGORIES 16
#define MICROPROFILE_MAX_GRAPHS 5
#define MICROPROFILE_GRAPH_HISTORY 128
#define MICROPROFILE_BUFFER_SIZE \
( ( MICROPROFILE_PER_THREAD_BUFFER_SIZE ) / sizeof( MicroProfileLogEntry ) )
#define MICROPROFILE_GPU_BUFFER_SIZE \
( ( MICROPROFILE_PER_THREAD_GPU_BUFFER_SIZE ) / sizeof( MicroProfileLogEntry ) )
#define MICROPROFILE_MAX_CONTEXT_SWITCH_THREADS 256
#define MICROPROFILE_STACK_MAX 1200
#define MICROPROFILE_WEBSOCKET_BUFFER_SIZE ( 10 << 10 )
#define MICROPROFILE_INVALID_TICK ( ( uint64_t ) -1 )
#define MICROPROFILE_INVALID_FRAME ( ( uint32_t ) -1 )
#define MICROPROFILE_GROUP_MASK_ALL 0xffffffff
#define MP_LOG_TICK_MASK 0x0000ffffffffffff
#define MP_LOG_INDEX_MASK 0x3fff000000000000
#define MP_LOG_BEGIN_MASK 0xc000000000000000
#define MP_LOG_EXTRA_DATA 0x3
#define MP_LOG_ENTER 0x1
#define MP_LOG_LEAVE 0x0
#define MP_LOG_PAYLOAD 0x2
static_assert(
0 == ( MICROPROFILE_MAX_GROUPS % 32 ), "MICROPROFILE_MAX_GROUPS must be divisible by 32" );
enum EMicroProfileTokenSpecial {
ETOKEN_GPU_CPU_TIMESTAMP = 0x3fff,
ETOKEN_GPU_CPU_SOURCE_THREAD = 0x3ffe,
ETOKEN_META_MARKER = 0x3ffd,
ETOKEN_CUSTOM_NAME = 0x3ffc,
ETOKEN_CUSTOM_COLOR = 0x3ffb,
ETOKEN_CUSTOM_ID = 0x3ffa,
ETOKEN_MAX = 0x3ff0,
};
// web view:
// *turn tooltip back on
// *move tooltip out of the way
// *remove meta
// *fix gpu extra data
// *remove help
// *fix on webside.
// timeline track
// *basic features
// *figure out length
// *clean up dump code.
// *clean up draw code.
// *tooltip
// *demo
// *enter/leave without tokens, but strings.
// bookmarks
// live view:
// capture indicators.
// longer stats
// counters i graph view?
// call count i graph view?
enum {
MICROPROFILE_WEBSOCKET_DIRTY_MENU,
MICROPROFILE_WEBSOCKET_DIRTY_ENABLED,
};
#ifndef MICROPROFILE_ALLOC // redefine all if overriding
#define MICROPROFILE_ALLOC( nSize, nAlign ) MicroProfileAllocAligned( nSize, nAlign );
#define MICROPROFILE_REALLOC( p, s ) realloc( p, s )
#define MICROPROFILE_FREE( p ) MicroProfileFreeAligned( p )
#endif
#define MP_ALLOC( nSize, nAlign ) MicroProfileAllocInternal( nSize, nAlign )
#define MP_REALLOC( p, s ) MicroProfileReallocInternal( p, s )
#define MP_FREE( p ) MicroProfileFreeInternal( p )
#define MP_ALLOC_OBJECT( T ) ( T* ) MP_ALLOC( sizeof( T ), alignof( T ) )
typedef uint64_t MicroProfileLogEntry;
void MicroProfileSleep( uint32_t nMs );
template < typename T >
T MicroProfileMin( T a, T b );
template < typename T >
T MicroProfileMax( T a, T b );
template < typename T >
T MicroProfileClamp( T a, T min_, T max_ );
int64_t MicroProfileMsToTick( float fMs, int64_t nTicksPerSecond );
float MicroProfileTickToMsMultiplier( int64_t nTicksPerSecond );
uint64_t MicroProfileLogGetType( MicroProfileLogEntry Index );
uint64_t MicroProfileLogGetTimerIndex( MicroProfileLogEntry Index );
MicroProfileLogEntry MicroProfileMakeLogIndex(
uint64_t nBegin, MicroProfileToken nToken, int64_t nTick );
int64_t MicroProfileLogTickDifference( MicroProfileLogEntry Start, MicroProfileLogEntry End );
int64_t MicroProfileLogSetTick( MicroProfileLogEntry e, int64_t nTick );
uint16_t MicroProfileGetTimerIndex( MicroProfileToken t );
uint32_t MicroProfileGetGroupMask( MicroProfileToken t );
MicroProfileToken MicroProfileMakeToken(
uint64_t nGroupMask, uint32_t nGroupIndex, uint16_t nTimer );
bool MicroProfileAnyGroupActive();
//////////////////////////////////////////////////////////////////////////
// platform IMPL
void* MicroProfileAllocInternal( size_t nSize, size_t nAlign );
void MicroProfileFreeInternal( void* pPtr );
void* MicroProfileReallocInternal( void* pPtr, size_t nSize );
void* MicroProfileAllocAligned( size_t nSize, size_t nAlign );
void MicroProfileFreeAligned( void* pMem );
#if defined( __APPLE__ )
#include <TargetConditionals.h>
#include <libkern/OSAtomic.h>
#include <mach/mach.h>
#include <mach/mach_time.h>
#include <unistd.h>
#if TARGET_OS_IPHONE
#define MICROPROFILE_IOS
#endif
#define MP_TICK() mach_absolute_time()
inline int64_t MicroProfileTicksPerSecondCpu() {
static int64_t nTicksPerSecond = 0;
if ( nTicksPerSecond == 0 ) {
mach_timebase_info_data_t sTimebaseInfo;
mach_timebase_info( &sTimebaseInfo );
nTicksPerSecond = 1000000000ll * sTimebaseInfo.denom / sTimebaseInfo.numer;
}
return nTicksPerSecond;
}
inline uint64_t MicroProfileGetCurrentThreadId() {
uint64_t tid;
pthread_threadid_np( pthread_self(), &tid );
return tid;
}
#include <stdlib.h>
#define MP_BREAK() __builtin_trap()
#define MP_THREAD_LOCAL __thread
#define MP_STRCASECMP strcasecmp
#define MP_GETCURRENTTHREADID() MicroProfileGetCurrentThreadId()
typedef uint64_t MicroProfileThreadIdType;
void* MicroProfileAllocAligned( size_t nSize, size_t nAlign ) {
void* p;
posix_memalign( &p, nAlign, nSize );
return p;
}
void MicroProfileFreeAligned( void* pMem ) {
free( pMem );
}
#elif defined( _WIN32 )
#include <winsock2.h>
#include <ws2tcpip.h>
int64_t MicroProfileGetTick();
#define MP_TICK() MicroProfileGetTick()
#define MP_BREAK() __debugbreak()
#define MP_THREAD_LOCAL __declspec( thread )
#define MP_STRCASECMP _stricmp
#define MP_GETCURRENTTHREADID() GetCurrentThreadId()
void* MicroProfileAllocAligned( size_t nSize, size_t nAlign ) {
return _aligned_malloc( nSize, nAlign );
}
void MicroProfileFreeAligned( void* pMem ) {
_aligned_free( pMem );
}
#else
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
inline int64_t MicroProfileTicksPerSecondCpu() {
return 1000000000ll;
}
inline int64_t MicroProfileGetTick() {
timespec ts;
clock_gettime( CLOCK_REALTIME, &ts );
return 1000000000ll * ts.tv_sec + ts.tv_nsec;
}
#define MP_TICK() MicroProfileGetTick()
#define MP_BREAK() __builtin_trap()
#define MP_THREAD_LOCAL __thread
#define MP_STRCASECMP strcasecmp
#define MP_GETCURRENTTHREADID() ( uint64_t ) pthread_self()
typedef uint64_t MicroProfileThreadIdType;
void* MicroProfileAllocAligned( size_t nSize, size_t nAlign ) {
void* p;
posix_memalign( &p, nAlign, nSize );
return p;
}
void MicroProfileFreeAligned( void* pMem ) {
free( pMem );
}
#endif
#ifdef MICROPROFILE_PS4
#define MICROPROFILE_PS4_DECL
#include "microprofile_ps4.h"
#endif
#ifdef MICROPROFILE_XBOXONE
#define MICROPROFILE_XBOXONE_DECL
#include "microprofile_xboxone.h"
#else
#ifdef _WIN32
#include <d3d11_1.h>
#endif
#endif
#define MP_ASSERT( a ) \
do { \
if ( !( a ) ) { \
MP_BREAK(); \
} \
} while ( 0 )
#ifdef _WIN32
#include <basetsd.h>
typedef UINT_PTR MpSocket;
#else
typedef int MpSocket;
#endif
#ifndef _WIN32
typedef pthread_t MicroProfileThread;
#elif defined( _WIN32 )
#if _MSC_VER == 1900
typedef void* HANDLE;
#endif
typedef HANDLE MicroProfileThread;
#else
typedef std::thread* MicroProfileThread;
#endif
struct MicroProfileTimer {
uint64_t nTicks;
uint32_t nCount;
};
struct MicroProfileCategory {
char pName[MICROPROFILE_NAME_MAX_LEN];
uint32_t nGroupMask[MICROPROFILE_MAX_GROUP_INTS];
};
struct MicroProfileGroupInfo {
char pName[MICROPROFILE_NAME_MAX_LEN];
uint32_t nNameLen;
uint32_t nGroupIndex;
uint32_t nNumTimers;
uint32_t nMaxTimerNameLen;
uint32_t nColor;
uint32_t nCategory;
MicroProfileTokenType Type;
};
struct MicroProfileTimerInfo {
MicroProfileToken nToken;
uint32_t nTimerIndex;
uint32_t nGroupIndex;
char pName[MICROPROFILE_NAME_MAX_LEN];
char pNameExt[MICROPROFILE_NAME_MAX_LEN];
uint32_t nNameLen;
uint32_t nColor;
int nWSNext;
bool bGraph;
bool bWSEnabled;
MicroProfileTokenType Type;
};
struct MicroProfileCounterInfo {
int nParent;
int nSibling;
int nFirstChild;
uint16_t nNameLen;
uint8_t nLevel;
char* pName;
uint32_t nFlags;
int64_t nLimit;
MicroProfileCounterFormat eFormat;
};
struct MicroProfileCounterHistory {
uint32_t nPut;
uint64_t nHistory[MICROPROFILE_GRAPH_HISTORY];
};
struct MicroProfileCounterSource {
void* pSource;
uint32_t nSourceSize;
};
struct MicroProfileGraphState {
int64_t nHistory[MICROPROFILE_GRAPH_HISTORY];
MicroProfileToken nToken;
int32_t nKey;
};
struct MicroProfileContextSwitch {
MicroProfileThreadIdType nThreadOut;
MicroProfileThreadIdType nThreadIn;
int64_t nCpu : 8;
int64_t nTicks : 56;
};
struct MicroProfileFrameState {
int64_t nFrameStartCpu;
int64_t nFrameStartGpu;
uint64_t nFrameId;
uint32_t nGpuPending;
uint32_t nLogStart[MICROPROFILE_MAX_THREADS];
uint32_t nLogStartTimeline;
uint32_t nTimelineFrameMax;
int32_t nHistoryTimeline;
};
struct MicroProfileThreadLog {
MicroProfileLogEntry Log[MICROPROFILE_BUFFER_SIZE];
std::atomic< uint32_t > nPut;
std::atomic< uint32_t > nGet;
uint32_t nStackPut;
uint32_t nStackScope;
MicroProfileScopeStateC ScopeState[MICROPROFILE_STACK_MAX];
uint32_t nActive;
uint32_t nGpu;
MicroProfileThreadIdType nThreadId;
uint32_t nLogIndex;
uint32_t nCustomId;
MicroProfileLogEntry nStackLogEntry[MICROPROFILE_STACK_MAX];
int64_t nChildTickStack[MICROPROFILE_STACK_MAX];
uint32_t nStackPos;
uint8_t nGroupStackPos[MICROPROFILE_MAX_GROUPS];
int64_t nGroupTicks[MICROPROFILE_MAX_GROUPS];
int64_t nAggregateGroupTicks[MICROPROFILE_MAX_GROUPS];
enum {
THREAD_MAX_LEN = 64,
};
char ThreadName[64];
int nFreeListNext;
};
struct MicroProfileWebSocketBuffer {
char Header[20];
char Buffer[MICROPROFILE_WEBSOCKET_BUFFER_SIZE];
uint32_t nPut;
MpSocket Socket;
char SendBuffer[MICROPROFILE_WEBSOCKET_BUFFER_SIZE];
std::atomic< uint32_t > nSendPut;
std::atomic< uint32_t > nSendGet;
};
// linear, per-frame per-thread gpu log
struct MicroProfileThreadLogGpu {
MicroProfileLogEntry Log[MICROPROFILE_GPU_BUFFER_SIZE];
uint32_t nPut;
uint32_t nStart;
uint32_t nId;
void* pContext;
uint32_t nAllocated;
uint32_t nStackScope;
MicroProfileScopeStateC ScopeState[MICROPROFILE_STACK_MAX];
};
struct MicroProfileGpuTimerState;
#if MICROPROFILE_GPU_TIMERS_D3D11
struct MicroProfileD3D11Frame {
uint32_t m_nQueryStart;
uint32_t m_nQueryCountMax;
std::atomic< uint32_t > m_nQueryCount;
uint32_t m_nRateQueryStarted;
void* m_pRateQuery;
};
struct MicroProfileGpuTimerState {
uint32_t bInitialized;
void* m_pDevice;
void* m_pImmediateContext;
void* m_pQueries[MICROPROFILE_D3D_MAX_QUERIES];
int64_t m_nQueryResults[MICROPROFILE_D3D_MAX_QUERIES];
uint32_t m_nQueryPut;
uint32_t m_nQueryGet;
uint32_t m_nQueryFrame;
int64_t m_nQueryFrequency;
void* pSyncQuery;
MicroProfileD3D11Frame m_QueryFrames[MICROPROFILE_GPU_FRAME_DELAY];
};
#elif defined( MICROPROFILE_GPU_TIMERS_D3D12 )
#include <d3d12.h>
#ifndef MICROPROFILE_D3D_MAX_QUERIES
#define MICROPROFILE_D3D_MAX_QUERIES ( 32 << 10 )
#endif
#define MICROPROFILE_D3D_MAX_NODE_COUNT 4
#define MICROPROFILE_D3D_INTERNAL_DELAY 8
#define MP_NODE_MASK_ALL( n ) ( ( 1u << ( n ) ) - 1u )
#define MP_NODE_MASK_ONE( n ) ( 1u << ( n ) )
struct MicroProfileD3D12Frame {
uint32_t nBegin;
uint32_t nCount;
uint32_t nNode;
ID3D12GraphicsCommandList* pCommandList[MICROPROFILE_D3D_MAX_NODE_COUNT];
ID3D12CommandAllocator* pCommandAllocator;
};
struct MicroProfileGpuTimerState {
ID3D12Device* pDevice;
uint32_t nNodeCount;
uint32_t nCurrentNode;
uint64_t nFrame;
uint64_t nPendingFrame;
uint32_t nFrameStart;
std::atomic< uint32_t > nFrameCount;
int64_t nFrequency;
ID3D12Resource* pBuffer;
struct {
ID3D12CommandQueue* pCommandQueue;
ID3D12QueryHeap* pHeap;
ID3D12Fence* pFence;
} NodeState[MICROPROFILE_D3D_MAX_NODE_COUNT];
uint16_t nQueryFrames[MICROPROFILE_D3D_MAX_QUERIES];
int64_t nResults[MICROPROFILE_D3D_MAX_QUERIES];
MicroProfileD3D12Frame Frames[MICROPROFILE_D3D_INTERNAL_DELAY];
};
#elif MICROPROFILE_GPU_TIMERS_GL
struct MicroProfileGpuTimerState {
uint32_t GLTimers[MICROPROFILE_GL_MAX_QUERIES];
uint32_t GLTimerPos;
};
#endif
struct MicroProfile {
uint32_t nTotalTimers;
uint32_t nGroupCount;
uint32_t nCategoryCount;
uint32_t nAggregateClear;
uint32_t nAggregateFlip;
uint32_t nAggregateFlipCount;
uint32_t nAggregateFrames;
uint64_t nAggregateFlipTick;
uint32_t nDisplay;
uint32_t nBars;
uint32_t nActiveGroups[MICROPROFILE_MAX_GROUP_INTS];
uint32_t nFrozen;
uint32_t nWasFrozen;
uint32_t nPlatformMarkersEnabled;
uint32_t nForceEnable;
uint32_t nForceGroups[MICROPROFILE_MAX_GROUP_INTS];
uint32_t nActiveGroupsWanted[MICROPROFILE_MAX_GROUP_INTS];
uint32_t nGroupMask[MICROPROFILE_MAX_GROUP_INTS];
uint32_t nStartEnabled;
uint32_t nAllThreadsWanted;
uint32_t nOverflow;
uint32_t nMaxGroupSize;
uint32_t nDumpFileNextFrame;
uint32_t nDumpFileCountDown;
uint32_t nDumpSpikeMask;
uint32_t nAutoClearFrames;
float fDumpCpuSpike;
float fDumpGpuSpike;
char HtmlDumpPath[512];
char CsvDumpPath[512];
int64_t nPauseTicks;
float fReferenceTime;
float fRcpReferenceTime;
MicroProfileCategory CategoryInfo[MICROPROFILE_MAX_CATEGORIES];
MicroProfileGroupInfo GroupInfo[MICROPROFILE_MAX_GROUPS];
MicroProfileTimerInfo TimerInfo[MICROPROFILE_MAX_TIMERS];
uint32_t TimerToGroup[MICROPROFILE_MAX_TIMERS];
MicroProfileTimer AccumTimers[MICROPROFILE_MAX_TIMERS];
uint64_t AccumMaxTimers[MICROPROFILE_MAX_TIMERS];
uint64_t AccumMinTimers[MICROPROFILE_MAX_TIMERS];
uint64_t AccumTimersExclusive[MICROPROFILE_MAX_TIMERS];
uint64_t AccumMaxTimersExclusive[MICROPROFILE_MAX_TIMERS];
MicroProfileTimer Frame[MICROPROFILE_MAX_TIMERS];
uint64_t FrameExclusive[MICROPROFILE_MAX_TIMERS];
MicroProfileTimer Aggregate[MICROPROFILE_MAX_TIMERS];
uint64_t AggregateMax[MICROPROFILE_MAX_TIMERS];
uint64_t AggregateMin[MICROPROFILE_MAX_TIMERS];
uint64_t AggregateExclusive[MICROPROFILE_MAX_TIMERS];
uint64_t AggregateMaxExclusive[MICROPROFILE_MAX_TIMERS];
uint64_t FrameGroup[MICROPROFILE_MAX_GROUPS];
uint64_t AccumGroup[MICROPROFILE_MAX_GROUPS];
uint64_t AccumGroupMax[MICROPROFILE_MAX_GROUPS];
uint64_t AggregateGroup[MICROPROFILE_MAX_GROUPS];
uint64_t AggregateGroupMax[MICROPROFILE_MAX_GROUPS];
MicroProfileGraphState Graph[MICROPROFILE_MAX_GRAPHS];
uint32_t nGraphPut;
uint32_t nThreadActive[MICROPROFILE_MAX_THREADS];
MicroProfileThreadLog* Pool[MICROPROFILE_MAX_THREADS];
MicroProfileThreadLogGpu* PoolGpu[MICROPROFILE_MAX_THREADS];
MicroProfileThreadLog TimelineLog;
uint32_t TimelineTokenFrame[MICROPROFILE_TIMELINE_MAX_TOKENS];
uint32_t TimelineToken[MICROPROFILE_TIMELINE_MAX_TOKENS];
const char* TimelineTokenStaticString[MICROPROFILE_TIMELINE_MAX_TOKENS];
uint32_t nTimelineFrameMax;
uint32_t nNumLogs;
uint32_t nNumLogsGpu;
uint32_t nMemUsage;
int nFreeListHead;
// uint32_t TimelineRangePut;
// struct
// {
// uint32_t nIndex;
// uint32_t nToken;
// } TimelineActive[MICROPROFILE_MAX_TIMELINE_ACTIVE];
uint32_t nFrameCurrent;
uint32_t nFrameCurrentIndex;
uint32_t nFramePut;
uint32_t nFrameNext;
uint64_t nFramePutIndex;
MicroProfileFrameState Frames[MICROPROFILE_MAX_FRAME_HISTORY];
uint64_t nFlipTicks;
uint64_t nFlipAggregate;
uint64_t nFlipMax;
uint64_t nFlipAggregateDisplay;
uint64_t nFlipMaxDisplay;
MicroProfileThread ContextSwitchThread;
bool bContextSwitchRunning;
bool bContextSwitchStop;
bool bContextSwitchAllThreads;
bool bContextSwitchNoBars;
uint32_t nContextSwitchUsage;
uint32_t nContextSwitchLastPut;
int64_t nContextSwitchHoverTickIn;
int64_t nContextSwitchHoverTickOut;
uint32_t nContextSwitchHoverThread;
uint32_t nContextSwitchHoverThreadBefore;
uint32_t nContextSwitchHoverThreadAfter;
uint8_t nContextSwitchHoverCpu;
uint8_t nContextSwitchHoverCpuNext;
uint32_t nContextSwitchPut;
MicroProfileContextSwitch ContextSwitch[MICROPROFILE_CONTEXT_SWITCH_BUFFER_SIZE];
MpSocket ListenerSocket;
uint32_t nWebServerPort;
char WebServerBuffer[MICROPROFILE_WEBSERVER_SOCKET_BUFFER_SIZE];
uint32_t WebServerPut;
uint64_t nWebServerDataSent;
int WebSocketTimers;
uint32_t nWebSocketDirty;
MpSocket WebSockets[1];
int64_t WebSocketFrameLast[1];
uint32_t nNumWebSockets;
uint32_t nSocketFail; // for error propagation.
MicroProfileThread WebSocketSendThread;
bool WebSocketThreadRunning;
bool WebSocketThreadJoined;
uint32_t WSCategoriesSent;
uint32_t WSGroupsSent;
uint32_t WSTimersSent;
uint32_t WSCountersSent;
MicroProfileWebSocketBuffer WSBuf;
char* pJsonSettings;
bool bJsonSettingsReadOnly;
uint32_t nJsonSettingsPending;
uint32_t nJsonSettingsBufferSize;
uint32_t nWSWasConnected;
uint32_t nMicroProfileShutdown;
uint32_t nWSViewMode;
char CounterNames[MICROPROFILE_MAX_COUNTER_NAME_CHARS];
MicroProfileCounterInfo CounterInfo[MICROPROFILE_MAX_COUNTERS];
MicroProfileCounterSource CounterSource[MICROPROFILE_MAX_COUNTERS];
uint32_t nNumCounters;
uint32_t nCounterNamePos;
std::atomic< int64_t > Counters[MICROPROFILE_MAX_COUNTERS];
#if MICROPROFILE_COUNTER_HISTORY // uses 1kb per allocated counter. 512kb for default counter count
uint32_t nCounterHistoryPut;
int64_t nCounterHistory[MICROPROFILE_GRAPH_HISTORY][MICROPROFILE_MAX_COUNTERS]; // flipped to
// make
// swapping
// cheap,
// drawing more
// expensive.
int64_t nCounterMax[MICROPROFILE_MAX_COUNTERS];
int64_t nCounterMin[MICROPROFILE_MAX_COUNTERS];
#endif
int GpuQueue;
MicroProfileThreadLogGpu* pGpuGlobal;
MicroProfileGpuTimerState* pGPU;
};
inline uint64_t MicroProfileLogGetType( MicroProfileLogEntry Index ) {
return ( ( MP_LOG_BEGIN_MASK & Index ) >> 62 ) & 0x3;
}
inline uint64_t MicroProfileLogGetTimerIndex( MicroProfileLogEntry Index ) {
return ( 0x3fff & ( Index >> 48 ) );
}
inline MicroProfileLogEntry MicroProfileMakeLogIndex(
uint64_t nBegin, MicroProfileToken nToken, int64_t nTick ) {
MicroProfileLogEntry Entry =
( nBegin << 62 ) | ( ( 0x3fff & nToken ) << 48 ) | ( MP_LOG_TICK_MASK & nTick );
uint64_t t = MicroProfileLogGetType( Entry );
uint64_t nTimerIndex = MicroProfileLogGetTimerIndex( Entry );
MP_ASSERT( t == nBegin );
MP_ASSERT( nTimerIndex == ( nToken & 0x3fff ) );
return Entry;
}
namespace {
struct MicroProfilePayloadPack {
union {
struct {
#if MICROPROFILE_BIG_ENDIAN /// NOT implemented.
char h;
char message[7];
#else
char message[7];
char h;
#endif
};
uint64_t LogEntry;
};
};
}; // namespace
inline MicroProfileLogEntry MicroProfileMakeLogPayload( const char* pData, uint64_t nSize ) {
MP_ASSERT( nSize < 8 );
MicroProfilePayloadPack P;
P.LogEntry = 0;
memcpy( P.message, pData, nSize );
MicroProfileLogEntry LE = P.LogEntry;
LE |= ( ( uint64_t ) MP_LOG_PAYLOAD ) << 62llu;
return LE;
}
inline void MicroProfileGetLogPayload( uint64_t nMessage, char* pData ) {
MicroProfilePayloadPack P;
P.LogEntry = nMessage;
memcpy( pData, P.message, 7 );
}
inline int64_t MicroProfileLogTickDifference(
MicroProfileLogEntry Start, MicroProfileLogEntry End ) {
uint64_t nStart = Start;
uint64_t nEnd = End;
int64_t nDifference = ( ( nEnd << 16 ) - ( nStart << 16 ) );
return nDifference >> 16;
}
inline int64_t MicroProfileLogGetTick( MicroProfileLogEntry e ) {
return MP_LOG_TICK_MASK & e;
}
inline int64_t MicroProfileLogSetTick( MicroProfileLogEntry e, int64_t nTick ) {
return ( MP_LOG_TICK_MASK & nTick ) | ( e & ~MP_LOG_TICK_MASK );
}
inline uint16_t MicroProfileGetTimerIndex( MicroProfileToken t ) {
return ( t & 0xffff );
}
inline uint32_t MicroProfileGetGroupMask( MicroProfileToken t ) {
return ( uint32_t )( ( t >> 16 ) & MICROPROFILE_GROUP_MASK_ALL );
}
inline uint32_t MicroProfileGetGroupMaskIndex( MicroProfileToken t ) {
return ( uint32_t )( t >> 48 );
}
inline MicroProfileToken MicroProfileMakeToken(
uint32_t nGroupMask, uint16_t nGroupIndex, uint16_t nTimer ) {
return ( ( uint64_t ) nGroupIndex << 48llu ) | ( ( uint64_t ) nGroupMask << 16llu ) | nTimer;
}
template < typename T >
T MicroProfileMin( T a, T b ) {
return a < b ? a : b;
}
template < typename T >
T MicroProfileMax( T a, T b ) {
return a > b ? a : b;
}
template < typename T >
T MicroProfileClamp( T a, T min_, T max_ ) {
return MicroProfileMin( max_, MicroProfileMax( min_, a ) );
}
inline int64_t MicroProfileMsToTick( float fMs, int64_t nTicksPerSecond ) {
return ( int64_t )( fMs * 0.001f * nTicksPerSecond );
}
inline float MicroProfileTickToMsMultiplier( int64_t nTicksPerSecond ) {
return 1000.f / nTicksPerSecond;
}
float MicroProfileTickToMsMultiplierCpu() {
return MicroProfileTickToMsMultiplier( MicroProfileTicksPerSecondCpu() );
}
float MicroProfileTickToMsMultiplierGpu() {
return MicroProfileTickToMsMultiplier( MicroProfileTicksPerSecondGpu() );
}
uint16_t MicroProfileGetGroupIndex( MicroProfileToken t ) {
return ( uint16_t ) MicroProfileGet()->TimerToGroup[MicroProfileGetTimerIndex( t )];
}
uint64_t MicroProfileTick() {
return MP_TICK();
}
#ifdef _WIN32
#include <windows.h>
#define snprintf _snprintf
#pragma warning( push )
#pragma warning( disable : 4244 )
#pragma warning( disable : 4100 )
int64_t MicroProfileTicksPerSecondCpu() {
static int64_t nTicksPerSecond = 0;
if ( nTicksPerSecond == 0 ) {
QueryPerformanceFrequency( ( LARGE_INTEGER* ) &nTicksPerSecond );
}
return nTicksPerSecond;
}
int64_t MicroProfileGetTick() {
int64_t ticks;
QueryPerformanceCounter( ( LARGE_INTEGER* ) &ticks );
return ticks;
}
#endif
#if 1
typedef void* ( *MicroProfileThreadFunc )( void* );
#ifndef _WIN32
typedef pthread_t MicroProfileThread;
void MicroProfileThreadStart( MicroProfileThread* pThread, MicroProfileThreadFunc Func ) {
pthread_attr_t Attr;
int r = pthread_attr_init( &Attr );
MP_ASSERT( r == 0 );
pthread_create( pThread, &Attr, Func, 0 );
}
void MicroProfileThreadJoin( MicroProfileThread* pThread ) {
int r = pthread_join( *pThread, 0 );
MP_ASSERT( r == 0 );
}
#elif defined( _WIN32 )
typedef HANDLE MicroProfileThread;
DWORD _stdcall ThreadTrampoline( void* pFunc ) {
MicroProfileThreadFunc F = ( MicroProfileThreadFunc ) pFunc;
return ( uint32_t )( uintptr_t ) F( 0 );
}
void MicroProfileThreadStart( MicroProfileThread* pThread, MicroProfileThreadFunc Func ) {
*pThread = CreateThread( 0, 0, ThreadTrampoline, Func, 0, 0 );
}
void MicroProfileThreadJoin( MicroProfileThread* pThread ) {
WaitForSingleObject( *pThread, INFINITE );
CloseHandle( *pThread );
}
#else
#include <thread>
typedef std::thread* MicroProfileThread;
inline void MicroProfileThreadStart( MicroProfileThread* pThread, MicroProfileThreadFunc Func ) {
*pThread = MP_ALLOC_OBJECT( std::thread );
new ( *pThread ) std::thread( Func, nullptr );
}
inline void MicroProfileThreadJoin( MicroProfileThread* pThread ) {
( *pThread )->join();
( *pThread )->~thread();
MP_FREE( *pThread );
*pThread = 0;
}
#endif
#endif
#if MICROPROFILE_WEBSERVER
#ifdef _WIN32
#define MP_INVALID_SOCKET( f ) ( f == INVALID_SOCKET )
#else
#include <fcntl.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define MP_INVALID_SOCKET( f ) ( f < 0 )
#endif
void MicroProfileWebServerStart();
void MicroProfileWebServerStop();
void MicroProfileWebServerJoin();
bool MicroProfileWebServerUpdate();
void MicroProfileDumpToFile();
#else
#define MicroProfileWebServerStart() \
do { \
} while ( 0 )
#define MicroProfileWebServerStop() \
do { \
} while ( 0 )
#define MicroProfileWebServerJoin() \
do { \
} while ( 0 )
#define MicroProfileWebServerUpdate() false
#define MicroProfileDumpToFile() \
do { \
} while ( 0 )
#endif
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#ifndef MICROPROFILE_DEBUG
#define MICROPROFILE_DEBUG 0
#endif
#define S g_MicroProfile
MicroProfile g_MicroProfile;
#ifdef MICROPROFILE_IOS
// iOS doesn't support __thread
static pthread_key_t g_MicroProfileThreadLogKey;
static pthread_once_t g_MicroProfileThreadLogKeyOnce = PTHREAD_ONCE_INIT;
static void MicroProfileCreateThreadLogKey() {
pthread_key_create( &g_MicroProfileThreadLogKey, NULL );
}
#else
MP_THREAD_LOCAL MicroProfileThreadLog* g_MicroProfileThreadLogThreadLocal = 0;
#endif
static bool g_bUseLock = false; /// This is used because windows does not support using mutexes
/// under dll init(which is where global initialization is handled)
MICROPROFILE_DEFINE( g_MicroProfileFlip, "MicroProfile", "MicroProfileFlip", MP_GREEN4 );
MICROPROFILE_DEFINE( g_MicroProfileThreadLoop, "MicroProfile", "ThreadLoop", MP_GREEN4 );
MICROPROFILE_DEFINE( g_MicroProfileClear, "MicroProfile", "Clear", MP_GREEN4 );
MICROPROFILE_DEFINE( g_MicroProfileAccumulate, "MicroProfile", "Accumulate", MP_GREEN4 );
MICROPROFILE_DEFINE(
g_MicroProfileContextSwitchSearch, "MicroProfile", "ContextSwitchSearch", MP_GREEN4 );
MICROPROFILE_DEFINE(
g_MicroProfileGpuSubmit, "MicroProfile", "MicroProfileGpuSubmit", MP_HOTPINK2 );
MICROPROFILE_DEFINE(
g_MicroProfileSendLoop, "MicroProfile", "MicroProfileSocketSendLoop", MP_GREEN4 );
inline std::recursive_mutex& MicroProfileMutex() {
static std::recursive_mutex Mutex;
return Mutex;
}
std::recursive_mutex& MicroProfileGetMutex() {
return MicroProfileMutex();
}
inline std::recursive_mutex& MicroProfileTimelineMutex() {
static std::recursive_mutex Mutex;
return Mutex;
}
MICROPROFILE_API MicroProfile* MicroProfileGet() {
return &g_MicroProfile;
}
MicroProfileThreadLog* MicroProfileCreateThreadLog( const char* pName );
MicroProfileThreadLogGpu* MicroProfileThreadLogGpuAllocInternal();
void* MicroProfileSocketSenderThread( void* );
void MicroProfileInit() {
static bool bOnce = true;
if ( !bOnce ) {
return;
}
std::recursive_mutex& mutex = MicroProfileMutex();
bool bUseLock = g_bUseLock;
if ( bUseLock )
mutex.lock();
if ( bOnce ) {
S.nMemUsage += sizeof( S );
bOnce = false;
memset( ( void* ) &S, 0, sizeof( S ) );
for ( int i = 0; i < MICROPROFILE_MAX_GROUPS; ++i ) {
S.GroupInfo[i].pName[0] = '\0';
}
for ( int i = 0; i < MICROPROFILE_MAX_CATEGORIES; ++i ) {
S.CategoryInfo[i].pName[0] = '\0';
memset( S.CategoryInfo[i].nGroupMask, 0, sizeof( S.CategoryInfo[i].nGroupMask ) );
}
strcpy( &S.CategoryInfo[0].pName[0], "default" );
S.nCategoryCount = 1;
for ( int i = 0; i < MICROPROFILE_MAX_TIMERS; ++i ) {
S.TimerInfo[i].pName[0] = '\0';
}
S.nGroupCount = 0;