forked from mafemergency/byond-tracy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
prof.c
1668 lines (1431 loc) · 62.4 KB
/
prof.c
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
/* c11 minimum */
#if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 201112L)
# pragma message("minimum supported c standard is c11")
#endif
#if defined(__cplusplus)
# pragma message("compiling as c++ is ill-advised")
#endif
/* platform identification */
#if defined(_WIN32)
# define UTRACY_WINDOWS
# define _CRT_SECURE_NO_WARNINGS
# if defined(_WIN64)
# error 64 bit not supported
# endif
# if !defined(_WIN32_WINNT)
# define _WIN32_WINNT 0x0601
# endif
# if !defined(WINVER)
# define WINVER 0x0601
# endif
#elif defined(__linux__)
# define UTRACY_LINUX
# if defined(__x86_64__)
# error 64 bit not supported
# endif
#else
# error platform not detected
#endif
/* compiler identification */
#if defined(__clang__)
# define UTRACY_CLANG
#elif defined(__GNUC__)
# define UTRACY_GCC
#elif defined(_MSC_VER)
# define UTRACY_MSVC
#else
# error compiler not detected
#endif
#if defined(UTRACY_CLANG) || defined(UTRACY_GCC)
# define likely(expr) __builtin_expect(((expr) != 0), 1)
# define unlikely(expr) __builtin_expect(((expr) != 0), 0)
#else
# define likely(expr) (expr)
# define unlikely(expr) (expr)
#endif
/* data type size check */
_Static_assert(sizeof(void *) == 4, "incorrect size");
_Static_assert(sizeof(int) == 4, "incorrect size");
_Static_assert(sizeof(long long) == 8, "incorrect size");
/* linkage and exports */
#if defined(UTRACY_WINDOWS)
# if defined(UTRACY_CLANG) || defined(UTRACY_GCC)
# define UTRACY_INTERNAL static
# define UTRACY_EXTERNAL __attribute__((visibility("default"))) __attribute__((dllexport))
# define UTRACY_INLINE inline __attribute__((always_inline))
# elif defined(UTRACY_MSVC)
# define UTRACY_INTERNAL static
# define UTRACY_EXTERNAL __declspec(dllexport)
# define UTRACY_INLINE inline __forceinline
# endif
#elif defined(UTRACY_LINUX)
# if defined(UTRACY_CLANG) || defined(UTRACY_GCC)
# define UTRACY_INTERNAL static
# define UTRACY_EXTERNAL __attribute__((visibility("default")))
# define UTRACY_INLINE inline __attribute__((always_inline))
# endif
#endif
/* calling conventions */
#if defined(UTRACY_WINDOWS)
# define UTRACY_WINDOWS_CDECL __cdecl
# define UTRACY_WINDOWS_STDCALL __stdcall
# define UTRACY_WINDOWS_THISCALL __thiscall
# define UTRACY_LINUX_CDECL
# define UTRACY_LINUX_STDCALL
# define UTRACY_LINUX_THISCALL
# define UTRACY_LINUX_REGPARM(a)
#elif defined(UTRACY_LINUX)
# define UTRACY_WINDOWS_CDECL
# define UTRACY_WINDOWS_STDCALL
# define UTRACY_WINDOWS_THISCALL
# define UTRACY_LINUX_CDECL __attribute__((cdecl))
# define UTRACY_LINUX_STDCALL __attribute__((stdcall))
# define UTRACY_LINUX_THISCALL __attribute__((thiscall))
# define UTRACY_LINUX_REGPARM(a) __attribute__((regparm(a)))
#endif
/* headers */
#if defined(UTRACY_WINDOWS)
# define NOMINMAX
# include <winsock2.h>
# include <ws2tcpip.h>
# include <windows.h>
#elif defined(UTRACY_LINUX)
# define _GNU_SOURCE
# include <errno.h>
# include <unistd.h>
# include <time.h>
# include <sys/syscall.h>
# include <sys/types.h>
# include <sys/socket.h>
# include <sys/mman.h>
# include <sys/stat.h>
# include <sys/eventfd.h>
# include <netdb.h>
# include <pthread.h>
# include <dlfcn.h>
# include <link.h>
# include <netinet/ip.h>
# include <arpa/inet.h>
# include <poll.h>
# include <fcntl.h>
#endif
#include <stddef.h>
#include <assert.h>
#include <stdio.h>
#include <stdbool.h>
#if (__STDC_HOSTED__ == 1)
# include <stdlib.h>
# include <string.h>
#endif
#if !defined(__STDC_NO_ATOMICS__)
# include <stdatomic.h>
#endif
#if (__STDC_HOSTED__ == 0)
void *memset(void *const a, int value, size_t len) {
for(size_t i=0; i<len; i++) {
*((char *) a + i) = value;
}
return a;
}
void *memcpy(void *const restrict dst, void const *const restrict src, size_t len) {
for(size_t i=0; i<len; i++) {
*((char *) dst + i) = *((char *) src + i);
}
return dst;
}
int memcmp(void const *a, void const *b, size_t len) {
for(size_t i=0; i<len; i++) {
char unsigned _a = *(char unsigned *) a;
char unsigned _b = *(char unsigned *) b;
if(_a != _b) {
return (_a - _b);
}
}
return 0;
}
size_t strlen(char const *const a) {
size_t len = 0;
for(char const *p=a; *p; p++) {
len++;
}
return len;
}
#endif
#if defined(max)
# undef max
#endif
#if defined(UTRACY_CLANG) || defined(UTRACY_GCC)
# define max(a, b) ({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
_a > _b ? _a : _b; \
})
#elif defined(UTRACY_MSVC)
# define max(a, b) (((a) > (b)) ? (a) : (b))
#endif
#if defined(min)
# undef min
#endif
#if defined(UTRACY_CLANG) || defined(UTRACY_GCC)
# define min(a, b) ({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
_a < _b ? _a : _b; \
})
#elif defined(UTRACY_MSVC)
# define min(a, b) (((a) < (b)) ? (a) : (b))
#endif
#if defined(UTRACY_CLANG) || defined(UTRACY_GCC)
# if __has_builtin(__builtin_memcpy)
# define UTRACY_MEMCPY __builtin_memcpy
# else
# define UTRACY_MEMCPY memcpy
# endif
# if __has_builtin(__builtin_memset)
# define UTRACY_MEMSET __builtin_memset
# else
# define UTRACY_MEMSET memset
# endif
# if __has_builtin(__builtin_memcmp)
# define UTRACY_MEMCMP __builtin_memcmp
# else
# define UTRACY_MEMCMP memcmp
# endif
#else
# define UTRACY_MEMCPY memcpy
# define UTRACY_MEMSET memset
# define UTRACY_MEMCMP memcmp
#endif
/* debugging */
#if defined(UTRACY_DEBUG) || defined(DEBUG)
# include <stdio.h>
# define LOG_DEBUG_ERROR fprintf(stderr, "err: %s %s:%d\n", __func__, __FILE__, __LINE__)
# define LOG_INFO(...) fprintf(stdout, __VA_ARGS__)
#else
# define LOG_DEBUG_ERROR
# define LOG_INFO(...)
#endif
/* config */
#define EVENT_QUEUE_CAPACITY (1u << 18u)
_Static_assert((EVENT_QUEUE_CAPACITY & (EVENT_QUEUE_CAPACITY - 1)) == 0, "EVENT_QUEUE_CAPACITY must be a power of 2");
/* byond types */
struct object {
union {
int unsigned padding;
char unsigned type;
};
union {
int unsigned i;
float f;
};
};
struct string {
char *data;
int unsigned id;
struct string *left;
struct string *right;
int unsigned refcount;
int unsigned unk0;
int unsigned len;
};
struct procdef {
int unsigned path;
int unsigned name;
int unsigned desc;
int unsigned category;
int unsigned flags;
int unsigned unk0;
int unsigned bytecode;
int unsigned locals;
int unsigned parameters;
};
struct misc {
struct {
short unsigned len;
int unsigned unk0;
int unsigned *bytecode;
} bytecode;
struct {
short unsigned len;
int unsigned unk0;
int unsigned *locals;
} locals;
struct {
short unsigned len;
int unsigned unk0;
int unsigned *params;
} params;
};
struct proc {
int unsigned procdef;
char unsigned flags;
char unsigned supers;
short unsigned unused;
struct object usr;
struct object src;
struct execution_context *ctx;
int unsigned sequence;
void (*callback)(struct object, int unsigned);
int unsigned callback_arg;
int unsigned argc;
struct object *argv;
int unsigned unk0;
};
/* byond type size check */
_Static_assert(sizeof(struct object) == 8, "incorrect size");
_Static_assert(sizeof(struct string) == 28, "incorrect size");
_Static_assert(sizeof(struct procdef) >= 4, "incorrect size");
_Static_assert(sizeof(struct misc) == 36, "incorrect size");
_Static_assert(sizeof(struct proc) >= 4, "incorrect size");
/* queue */
#if defined(__STDC_NO_ATOMICS__)
# define atomic_load_relaxed(a) *(a)
# define atomic_load_acquire(a) *(a)
# define atomic_store_seqcst(a, b) *(a) = (b)
# define atomic_store_release(a, b) *(a) = (b)
#else
# define atomic_load_relaxed(a) atomic_load_explicit((a), memory_order_relaxed)
# define atomic_load_acquire(a) atomic_load_explicit((a), memory_order_acquire)
# define atomic_store_seqcst(a, b) atomic_store_explicit((a), (b), memory_order_seq_cst)
# define atomic_store_release(a, b) atomic_store_explicit((a), (b), memory_order_release)
#endif
#if defined(UTRACY_WINDOWS)
typedef HANDLE event_pipe_t;
#elif defined(UTRACY_LINUX)
typedef int event_pipe_t;
#endif
struct event_zone_begin {
int unsigned tid;
int unsigned srcloc;
_Alignas(8) long long timestamp;
};
struct event_zone_end {
int unsigned tid;
_Alignas(8) long long timestamp;
};
struct event_zone_color {
int unsigned tid;
int unsigned color;
};
struct event_frame_mark {
void *name;
_Alignas(8) long long timestamp;
};
struct event_plot {
void *name;
float f;
_Alignas(8) long long timestamp;
};
struct event {
char unsigned type;
union {
struct event_zone_begin zone_begin;
struct event_zone_end zone_end;
struct event_zone_color zone_color;
struct event_frame_mark frame_mark;
struct event_plot plot;
};
};
/* data */
static struct {
struct string ***strings;
int unsigned *strings_len;
struct misc ***miscs;
int unsigned *miscs_len;
/* procdef array address */
char **procdefs;
/* procdef array length */
int unsigned *procdefs_len;
/* procdef descriptor */
struct {
/* total size */
int unsigned size;
/* offsets */
int unsigned path;
int unsigned bytecode;
} procdef_desc;
void *exec_proc;
struct object (UTRACY_WINDOWS_CDECL UTRACY_LINUX_REGPARM(3) *orig_exec_proc)(struct proc *);
void *server_tick;
int (UTRACY_WINDOWS_STDCALL UTRACY_LINUX_CDECL *orig_server_tick)(void);
void *send_maps;
void (UTRACY_WINDOWS_CDECL UTRACY_LINUX_CDECL *orig_send_maps)(void);
} byond;
static struct {
struct {
long long init_begin;
long long init_end;
double multiplier;
long long resolution;
long long delay;
long long epoch;
long long exec_time;
} info;
#if defined(UTRACY_WINDOWS)
HANDLE thread;
#elif defined(UTRACY_LINUX)
pthread_t thread;
#endif
event_pipe_t quit;
FILE* fstream;
struct {
int unsigned producer_tail_cache;
int unsigned consumer_head_cache;
struct event events[EVENT_QUEUE_CAPACITY];
#if defined(__STDC_NO_ATOMICS__)
long volatile head;
long volatile tail;
#else
_Alignas(64) atomic_uint head;
_Alignas(64) atomic_uint tail;
_Alignas(64) int padding;
#endif
} queue;
} utracy;
#if defined(UTRACY_WINDOWS)
UTRACY_INTERNAL
event_pipe_t create_event_pipe(bool manual_reset, int initial_state, const char* name) {
return CreateEvent(NULL, manual_reset, initial_state, name ? TEXT(name) : NULL);
}
UTRACY_INTERNAL
void close_event_pipe(event_pipe_t event_pipe) {
CloseHandle(event_pipe);
}
UTRACY_INTERNAL
int wait_for_event_pipe(event_pipe_t event_pipe, int timeout_ms) {
DWORD result = WaitForSingleObject(event_pipe, timeout_ms);
switch (result) {
case WAIT_OBJECT_0: return 0;
case WAIT_TIMEOUT: return 1;
default: return -1;
}
}
UTRACY_INTERNAL
void set_event_pipe(event_pipe_t event_pipe) {
SetEvent(event_pipe);
}
#elif defined(UTRACY_LINUX)
UTRACY_INTERNAL
event_pipe_t create_event_pipe(bool manual_reset, int initial_state, const char* name) {
(void)manual_reset; // Unused on Linux
(void)name; // Unused on Linux
return eventfd(initial_state ? 1 : 0, EFD_CLOEXEC);
}
UTRACY_INTERNAL
void close_event_pipe(event_pipe_t event_pipe) {
close(event_pipe);
}
UTRACY_INTERNAL
int wait_for_event_pipe(event_pipe_t event_pipe, int timeout_ms) {
fd_set fds;
struct timeval tv;
uint64_t value;
FD_ZERO(&fds);
FD_SET(event_pipe, &fds);
tv.tv_sec = timeout_ms / 1000;
tv.tv_usec = (timeout_ms % 1000) * 1000;
int result = select(event_pipe + 1, &fds, NULL, NULL, timeout_ms >= 0 ? &tv : NULL);
if (result > 0) {
if (read(event_pipe, &value, sizeof(value)) == sizeof(value)) {
return 0;
}
} else if (result == 0) {
return 1; // Timeout
}
return -1; // Error
}
UTRACY_INTERNAL
void set_event_pipe(event_pipe_t event_pipe) {
uint64_t value = 1;
(void) write(event_pipe, &value, sizeof(value));
}
#endif
/* queue api */
UTRACY_INTERNAL UTRACY_INLINE
int event_queue_init(void) {
utracy.queue.producer_tail_cache = 0;
utracy.queue.consumer_head_cache = 0;
atomic_store_seqcst(&utracy.queue.head, 1);
atomic_store_seqcst(&utracy.queue.tail, 0);
return 0;
}
UTRACY_INTERNAL UTRACY_INLINE
void event_queue_push(struct event const *const event) {
int unsigned store = atomic_load_relaxed(&utracy.queue.head);
int unsigned next_store = store + 1;
if(next_store == EVENT_QUEUE_CAPACITY) {
next_store = 0;
}
while(unlikely(next_store == utracy.queue.producer_tail_cache)) {
utracy.queue.producer_tail_cache = atomic_load_acquire(&utracy.queue.tail);
}
utracy.queue.events[store] = *event;
atomic_store_release(&utracy.queue.head, next_store);
}
UTRACY_INTERNAL UTRACY_INLINE
int event_queue_pop(struct event *const event) {
int unsigned load = atomic_load_relaxed(&utracy.queue.tail);
int unsigned next_load = load + 1;
if(load == utracy.queue.consumer_head_cache) {
utracy.queue.consumer_head_cache = atomic_load_acquire(&utracy.queue.head);
if(load == utracy.queue.consumer_head_cache) {
return -1;
}
}
*event = utracy.queue.events[load];
if(next_load == EVENT_QUEUE_CAPACITY) {
next_load = 0;
}
atomic_store_release(&utracy.queue.tail, next_load);
return 0;
}
/* profiler */
UTRACY_INTERNAL UTRACY_INLINE
long long utracy_tsc(void) {
#if defined(UTRACY_CLANG) || defined(UTRACY_GCC)
return (long long) __builtin_ia32_rdtsc();
#elif defined(UTRACY_MSVC)
return (long long) __rdtsc();
#else
int unsigned eax, edx;
__asm__ __volatile__("rdtsc;" :"=a"(eax), "=d"(edx));
return ((long long) edx << 32) + eax;
#endif
}
#if defined(UTRACY_LINUX)
static int unsigned linux_main_tid;
#endif
UTRACY_INTERNAL UTRACY_INLINE
int unsigned utracy_tid(void) {
#if defined(UTRACY_WINDOWS)
# if defined(UTRACY_CLANG) || defined(UTRACY_GCC)
int unsigned tid;
__asm__("mov %%fs:0x24, %0;" :"=r"(tid));
return tid;
# elif defined(UTRACY_MSVC)
__asm {
mov eax, fs:[0x24];
}
# else
return GetCurrentThreadId();
# endif
#elif defined(UTRACY_LINUX)
/* too damn slow
return syscall(__NR_gettid); */
return linux_main_tid;
#endif
}
UTRACY_INTERNAL
double calibrate_multiplier(void) {
#if defined(UTRACY_WINDOWS)
LARGE_INTEGER li_freq, li_t0, li_t1;
if(0 == QueryPerformanceFrequency(&li_freq)) {
LOG_DEBUG_ERROR;
return 1.0;
}
if(0 == QueryPerformanceCounter(&li_t0)) {
LOG_DEBUG_ERROR;
return 1.0;
}
long long clk0 = utracy_tsc();
Sleep(100);
if(0 == QueryPerformanceCounter(&li_t1)) {
LOG_DEBUG_ERROR;
return 1.0;
}
long long clk1 = utracy_tsc();
double const freq = li_freq.QuadPart;
double const t0 = li_t0.QuadPart;
double const t1 = li_t1.QuadPart;
double const dt = ((t1 - t0) * 1000000000.0) / freq;
double const dclk = clk1 - clk0;
if(clk0 >= clk1) {
LOG_DEBUG_ERROR;
return 1.0;
}
if(t0 >= t1) {
LOG_DEBUG_ERROR;
return 1.0;
}
if(0.0 >= dclk) {
LOG_DEBUG_ERROR;
return 1.0;
}
return dt / dclk;
#elif defined(UTRACY_LINUX)
struct timespec ts_t0, ts_t1;
interrupted:
if(-1 == clock_gettime(CLOCK_MONOTONIC_RAW, &ts_t0)) {
LOG_DEBUG_ERROR;
return 1.0;
}
long long clk0 = utracy_tsc();
if(-1 == usleep(100000)) {
LOG_DEBUG_ERROR;
if(EINTR == errno) {
goto interrupted;
}
return 1.0;
}
if(-1 == clock_gettime(CLOCK_MONOTONIC_RAW, &ts_t1)) {
LOG_DEBUG_ERROR;
return 1.0;
}
long long clk1 = utracy_tsc();
double const t0 = ts_t0.tv_sec * 1000000000.0 + ts_t0.tv_nsec;
double const t1 = ts_t1.tv_sec * 1000000000.0 + ts_t1.tv_nsec;
double const dt = t1 - t0;
double const dclk = clk1 - clk0;
if(clk0 >= clk1) {
LOG_DEBUG_ERROR;
return 1.0;
}
if(t0 >= t1) {
LOG_DEBUG_ERROR;
return 1.0;
}
if(0.0 >= dclk) {
LOG_DEBUG_ERROR;
return 1.0;
}
return dt / dclk;
#endif
}
UTRACY_INTERNAL
long long calibrate_resolution(void) {
/* many iterations may be required to allow the thread time to migrate
to a suitable cpu / C-state / P-state */
int const iterations = 1000000;
long long resolution = 0x7FFFFFFFFFFFFFFFll;
for(int i=0; i<iterations; i++) {
long long clk0 = utracy_tsc();
long long clk1 = utracy_tsc();
long long dclk = clk1 - clk0;
resolution = dclk < resolution ? dclk : resolution;
}
return resolution;
}
/* seconds since unix epoch */
UTRACY_INTERNAL
long long unix_timestamp(void) {
#if defined(UTRACY_WINDOWS)
/* thanks Ian Boyd https://stackoverflow.com/a/46024468 */
long long const UNIX_TIME_START = 0x019DB1DED53E8000ll;
long long const TICKS_PER_SECOND = 10000000ll;
FILETIME ft_timestamp;
GetSystemTimeAsFileTime(&ft_timestamp);
LARGE_INTEGER li_timestamp = {
.LowPart = ft_timestamp.dwLowDateTime,
.HighPart = ft_timestamp.dwHighDateTime
};
return (li_timestamp.QuadPart - UNIX_TIME_START) / TICKS_PER_SECOND;
#elif defined(UTRACY_LINUX)
struct timespec t;
if(-1 == clock_gettime(CLOCK_REALTIME, &t)) {
LOG_DEBUG_ERROR;
return 0;
}
return t.tv_sec;
#endif
}
#define UTRACY_EVT_ZONEBEGIN (15)
#define UTRACY_EVT_ZONEEND (17)
#define UTRACY_EVT_PLOTDATA (43)
#define UTRACY_EVT_THREADCONTEXT (57)
#define UTRACY_EVT_ZONECOLOR (62)
#define UTRACY_EVT_FRAMEMARKMSG (64)
struct utracy_source_location {
char const *name;
char const *function;
char const *file;
int unsigned line;
int unsigned color;
};
static struct utracy_source_location srclocs[0x10002];
UTRACY_INTERNAL UTRACY_INLINE
void utracy_emit_zone_begin(int unsigned proc) {
event_queue_push(&(struct event) {
.type = UTRACY_EVT_ZONEBEGIN,
.zone_begin.tid = utracy_tid(),
.zone_begin.timestamp = utracy_tsc(),
.zone_begin.srcloc = proc/*(void *) srcloc*/
});
}
UTRACY_INTERNAL UTRACY_INLINE
void utracy_emit_zone_end(void) {
event_queue_push(&(struct event) {
.type = UTRACY_EVT_ZONEEND,
.zone_end.tid = utracy_tid(),
.zone_end.timestamp = utracy_tsc()
});
}
UTRACY_INTERNAL UTRACY_INLINE
void utracy_emit_zone_color(int unsigned color) {
event_queue_push(&(struct event) {
.type = UTRACY_EVT_ZONECOLOR,
.zone_color.tid = utracy_tid(),
.zone_color.color = color
});
}
UTRACY_INTERNAL UTRACY_INLINE
void utracy_emit_frame_mark(char *const name) {
event_queue_push(&(struct event) {
.type = UTRACY_EVT_FRAMEMARKMSG,
.frame_mark.name = name,
.frame_mark.timestamp = utracy_tsc()
});
}
UTRACY_INTERNAL
long long calibrate_delay(void) {
(void) UTRACY_MEMSET(utracy.queue.events, 0, sizeof(utracy.queue.events));
int unsigned const iterations = (EVENT_QUEUE_CAPACITY / 2u) - 1u;
long long clk0 = utracy_tsc();
for(int unsigned i=0; i<iterations; i++) {
utracy_emit_zone_begin(0);
utracy_emit_zone_end();
}
long long clk1 = utracy_tsc();
long long dclk = clk1 - clk0;
struct event evt;
while(0 == event_queue_pop(&evt));
return dclk / (long long) (iterations * 2);
}
#if 0
static int utracy_write(void const *const buf, size_t size) {
DWORD written;
size_t offset = 0;
while(offset < size) {
if(FALSE == WriteFile(utracy.stream, (char *const) buf + offset, size - offset, &written, NULL)) {
LOG_DEBUG_ERROR;
return -1;
}
if(0 == written) {
LOG_DEBUG_ERROR;
return -1;
}
offset += written;
}
return 0;
}
#else
static int utracy_write(void const *const buf, size_t size) {
fwrite(buf, 1, size, utracy.fstream);
return 0;
}
#endif
UTRACY_INTERNAL
#if defined(UTRACY_WINDOWS)
DWORD WINAPI utracy_server_thread_start(void* arg) {
#elif defined(UTRACY_LINUX)
void *utracy_server_thread_start(void* arg) {
#endif
event_pipe_t profiler_connected_event = (event_pipe_t)arg;
if (profiler_connected_event) {
set_event_pipe(profiler_connected_event);
}
{
struct {
_Alignas(8) long long unsigned signature;
int unsigned version;
int unsigned padding1;
double multiplier;
_Alignas(8) long long init_begin;
_Alignas(8) long long init_end;
_Alignas(8) long long delay;
_Alignas(8) long long resolution;
_Alignas(8) long long epoch;
_Alignas(8) long long exec_time;
_Alignas(8) long long pid;
_Alignas(8) long long sampling_period;
char unsigned flags;
char unsigned cpu_arch;
char cpu_manufacturer[12];
int unsigned cpu_id;
char program_name[64];
char host_info[1024];
int unsigned padding2;
} header = {0};
_Static_assert(sizeof(header) == 1200, "header size changed!");
_Static_assert(sizeof(struct event) == 24, "event size changed!");
header.signature = 0x6D64796361727475llu;
header.version = 2;
header.multiplier = utracy.info.multiplier;
header.init_begin = utracy.info.init_begin;
header.init_end = utracy.info.init_end;
header.delay = utracy.info.delay;
header.resolution = utracy.info.resolution;
header.epoch = utracy.info.epoch;
header.exec_time = utracy.info.exec_time;
header.pid = 0;
header.sampling_period = 0;
header.flags = 0;
header.cpu_arch = 0;
(void) memcpy(header.cpu_manufacturer, "???", 3);
header.cpu_id = 0;
(void) memcpy(header.program_name, "dreamdaemon.exe", 15);
(void) memcpy(header.host_info, "???", 3);
(void) utracy_write(&header, sizeof(header));
}
int unsigned srclocs_len = sizeof(srclocs) / sizeof(*srclocs);
(void) utracy_write(&srclocs_len, sizeof(srclocs_len));
for(int unsigned i=0; i<srclocs_len; i++) {
struct utracy_source_location srcloc = srclocs[i];
if(NULL != srcloc.name) {
int unsigned name_len = strlen(srcloc.name);
(void) utracy_write(&name_len, sizeof(name_len));
(void) utracy_write(srcloc.name, name_len);
} else {
int unsigned name_len = 0;
(void) utracy_write(&name_len, sizeof(name_len));
}
if(NULL != srcloc.function) {
int unsigned function_len = strlen(srcloc.function);
(void) utracy_write(&function_len, sizeof(function_len));
(void) utracy_write(srcloc.function, function_len);
} else {
int unsigned function_len = 0;
(void) utracy_write(&function_len, sizeof(function_len));
}
if(NULL != srcloc.file) {
int unsigned file_len = strlen(srcloc.file);
(void) utracy_write(&file_len, sizeof(file_len));
(void) utracy_write(srcloc.file, file_len);
} else {
int unsigned file_len = 0;
(void) utracy_write(&file_len, sizeof(file_len));
}
(void) utracy_write(&srcloc.line, sizeof(srcloc.line));
(void) utracy_write(&srcloc.color, sizeof(srcloc.color));
}
bool quitting = false;
while(!quitting) {
struct event evt;
while(0 == event_queue_pop(&evt)) {
(void) utracy_write(&evt, sizeof(evt));
}
switch (wait_for_event_pipe(utracy.quit, 1)) {
case 0:
quitting = true;
break;
case 1:
break;
default:
LOG_DEBUG_ERROR;
break;
}
}
#if defined(UTRACY_WINDOWS)
CloseHandle(utracy.thread);
CloseHandle(utracy.quit);
ExitThread(0);
#elif defined(UTRACY_LINUX)
close(utracy.quit);
pthread_exit(NULL);
#endif
}
/* byond hooks */
UTRACY_INTERNAL
struct object UTRACY_WINDOWS_CDECL UTRACY_LINUX_REGPARM(3) exec_proc(struct proc *proc) {
if(likely(proc->procdef < 0x10000)) {
utracy_emit_zone_begin(proc->procdef);
/* procs with pre-existing contexts are resuming from sleep */
if(unlikely(proc->ctx != NULL)) {
utracy_emit_zone_color(0xAF4444);
}
struct object result = byond.orig_exec_proc(proc);
utracy_emit_zone_end();
return result;
}
return byond.orig_exec_proc(proc);
}
UTRACY_INTERNAL
int UTRACY_WINDOWS_STDCALL UTRACY_LINUX_CDECL server_tick(void) {
/* server tick is the end of a frame and the beginning of the next frame */
utracy_emit_frame_mark(NULL);
utracy_emit_zone_begin(0x10000);
int interval = byond.orig_server_tick();
utracy_emit_zone_end();
return interval;