This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
/
trace.d
878 lines (764 loc) · 22.3 KB
/
trace.d
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
/**
* Contains support code for code profiling.
*
* Copyright: Copyright Digital Mars 1995 - 2015.
* License: Distributed under the
* $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
* (See accompanying file LICENSE)
* Authors: Walter Bright, Sean Kelly
* Source: $(DRUNTIMESRC src/rt/_trace.d)
*/
module rt.trace;
private
{
import core.demangle;
import core.stdc.ctype;
import core.stdc.stdio;
import core.stdc.stdlib;
import core.stdc.string;
import rt.util.string;
version (CRuntime_Microsoft)
alias core.stdc.stdlib._strtoui64 strtoull;
}
extern (C):
alias long timer_t;
/////////////////////////////////////
//
struct SymPair
{
SymPair* next;
Symbol* sym; // function that is called
ulong count; // number of times sym is called
}
/////////////////////////////////////
// A Symbol for each function name.
struct Symbol
{
Symbol* Sl, Sr; // left, right children
SymPair* Sfanin; // list of calling functions
SymPair* Sfanout; // list of called functions
timer_t totaltime; // aggregate time
timer_t functime; // time excluding subfunction calls
ubyte Sflags; // SFxxxx
uint recursion; // call recursion level
const(char)[] Sident; // name of symbol
}
enum ubyte SFvisited = 1; // visited
//////////////////////////////////
// Build a linked list of these.
struct Stack
{
Stack* prev;
Symbol* sym;
timer_t starttime; // time when function was entered
timer_t ohd; // overhead of all the bookkeeping code
timer_t subtime; // time used by all subfunctions
}
Symbol* root; // root of symbol table
bool trace_inited;
Stack* stack_freelist;
Stack* trace_tos; // top of stack
__gshared
{
Symbol* groot; // merged symbol table
int gtrace_inited; // !=0 if initialized
timer_t trace_ohd;
string trace_logfilename = "trace.log";
string trace_deffilename = "trace.def";
}
////////////////////////////////////////
// Set file name for output.
// A file name of "" means write results to stdout.
// Returns:
// 0 success
// !=0 failure
void trace_setlogfilename(string name)
{
trace_logfilename = name;
}
////////////////////////////////////////
// Set file name for output.
// A file name of "" means write results to stdout.
// Returns:
// 0 success
// !=0 failure
void trace_setdeffilename(string name)
{
trace_deffilename = name;
}
////////////////////////////////////////
// Output optimal function link order.
private void trace_order(FILE* fpdef, Symbol *s)
{
while (s)
{
trace_place(fpdef, s, 0);
if (s.Sl)
trace_order(fpdef, s.Sl);
s = s.Sr;
}
}
//////////////////////////////////////////////
//
private Stack* stack_push()
{
Stack *s;
if (stack_freelist)
{
s = stack_freelist;
stack_freelist = s.prev;
}
else
{
s = cast(Stack *)trace_malloc(Stack.sizeof);
}
s.prev = trace_tos;
trace_tos = s;
return s;
}
//////////////////////////////////////////////
//
private void stack_free(Stack *s)
{
s.prev = stack_freelist;
stack_freelist = s;
}
//////////////////////////////////////
// Qsort() comparison routine for array of pointers to SymPair's.
private int sympair_cmp(in void* e1, in void* e2)
{
auto count1 = (*cast(SymPair**)e1).count;
auto count2 = (*cast(SymPair**)e2).count;
if (count1 < count2)
return -1;
else if (count1 > count2)
return 1;
return 0;
}
//////////////////////////////////////
// Place symbol s, and then place any fan ins or fan outs with
// counts greater than count.
private void trace_place(FILE* fpdef, Symbol* s, ulong count)
{
if (!(s.Sflags & SFvisited))
{
//printf("\t%.*s\t%llu\n", s.Sident.length, s.Sident.ptr, count);
fprintf(fpdef,"\t%.*s\n", s.Sident.length, s.Sident.ptr);
s.Sflags |= SFvisited;
// Compute number of items in array
size_t num = 0;
for (auto sp = s.Sfanin; sp; sp = sp.next)
num++;
for (auto sp = s.Sfanout; sp; sp = sp.next)
num++;
if (!num)
return;
// Allocate and fill array
auto base = cast(SymPair**)trace_malloc(SymPair.sizeof * num);
size_t u = 0;
for (auto sp = s.Sfanin; sp; sp = sp.next)
base[u++] = sp;
for (auto sp = s.Sfanout; sp; sp = sp.next)
base[u++] = sp;
assert(u == num);
// Sort array
qsort(base, num, (SymPair *).sizeof, &sympair_cmp);
//for (u = 0; u < num; u++)
//printf("\t\t%.*s\t%llu\n", base[u].sym.Sident.length, base[u].sym.Sident.ptr, base[u].count);
// Place symbols
for (u = 0; u < num; u++)
{
if (base[u].count >= count)
{
auto u2 = (u + 1 < num) ? u + 1 : u;
auto c2 = base[u2].count;
if (c2 < count)
c2 = count;
trace_place(fpdef, base[u].sym,c2);
}
else
break;
}
// Clean up
trace_free(base);
}
}
///////////////////////////////////
// Report results.
// Also compute and return number of symbols.
private size_t trace_report(FILE* fplog, Symbol* s)
{
//printf("trace_report()\n");
size_t nsymbols;
while (s)
{
++nsymbols;
if (s.Sl)
nsymbols += trace_report(fplog, s.Sl);
fprintf(fplog,"------------------\n");
ulong count = 0;
for (auto sp = s.Sfanin; sp; sp = sp.next)
{
fprintf(fplog,"\t%5llu\t%.*s\n", sp.count, sp.sym.Sident.length, sp.sym.Sident.ptr);
count += sp.count;
}
fprintf(fplog,"%.*s\t%llu\t%lld\t%lld\n", s.Sident.length, s.Sident.ptr, count, s.totaltime, s.functime);
for (auto sp = s.Sfanout; sp; sp = sp.next)
{
fprintf(fplog,"\t%5llu\t%.*s\n", sp.count, sp.sym.Sident.length, sp.sym.Sident.ptr);
}
s = s.Sr;
}
return nsymbols;
}
////////////////////////////////////
// Allocate and fill array of symbols.
private void trace_array(Symbol*[] psymbols, Symbol *s, ref uint u)
{
while (s)
{
psymbols[u++] = s;
trace_array(psymbols, s.Sl, u);
s = s.Sr;
}
}
//////////////////////////////////////
// Qsort() comparison routine for array of pointers to Symbol's.
private int symbol_cmp(in void* e1, in void* e2)
{
auto ps1 = cast(Symbol **)e1;
auto ps2 = cast(Symbol **)e2;
auto diff = (*ps2).functime - (*ps1).functime;
return (diff == 0) ? 0 : ((diff > 0) ? 1 : -1);
}
///////////////////////////////////
// Report function timings
private void trace_times(FILE* fplog, Symbol*[] psymbols)
{
// Sort array
qsort(psymbols.ptr, psymbols.length, (Symbol *).sizeof, &symbol_cmp);
// Print array
timer_t freq;
QueryPerformanceFrequency(&freq);
fprintf(fplog,"\n======== Timer Is %lld Ticks/Sec, Times are in Microsecs ========\n\n",freq);
fprintf(fplog," Num Tree Func Per\n");
fprintf(fplog," Calls Time Time Call\n\n");
foreach (s; psymbols)
{
timer_t tl,tr;
timer_t fl,fr;
timer_t pl,pr;
timer_t percall;
char[8192] buf = void;
SymPair* sp;
ulong calls;
char[] id;
calls = 0;
id = demangle(s.Sident, buf);
for (sp = s.Sfanin; sp; sp = sp.next)
calls += sp.count;
if (calls == 0)
calls = 1;
tl = (s.totaltime * 1000000) / freq;
fl = (s.functime * 1000000) / freq;
percall = s.functime / calls;
pl = (s.functime * 1000000) / calls / freq;
fprintf(fplog,"%7llu%12lld%12lld%12lld %.*s\n",
calls, tl, fl, pl, id.length, id.ptr);
}
}
///////////////////////////////////
// Initialize.
private void trace_init()
{
synchronized // protects gtrace_inited
{
if (!gtrace_inited)
{
gtrace_inited = 1;
{ // See if we can determine the overhead.
timer_t starttime;
timer_t endtime;
auto st = trace_tos;
trace_tos = null;
QueryPerformanceCounter(&starttime);
uint u;
for (u = 0; u < 100; u++)
{
_c_trace_pro(0,null);
_c_trace_epi();
}
QueryPerformanceCounter(&endtime);
trace_ohd = (endtime - starttime) / u;
//printf("trace_ohd = %lld\n",trace_ohd);
if (trace_ohd > 0)
trace_ohd--; // round down
trace_tos = st;
}
}
}
}
/////////////////////////////////
// Terminate.
static ~this()
{
// Free remainder of the thread local stack
while (trace_tos)
{
auto n = trace_tos.prev;
stack_free(trace_tos);
trace_tos = n;
}
// And free the thread local stack's memory
while (stack_freelist)
{
auto n = stack_freelist.prev;
stack_free(stack_freelist);
stack_freelist = n;
}
synchronized // protects groot
{
// Merge thread local root into global groot
if (!groot)
{
groot = root; // that was easy
root = null;
}
else
{
void mergeSymbol(Symbol** proot, const(Symbol)* s)
{
while (s)
{
auto gs = trace_addsym(proot, s.Sident);
gs.totaltime += s.totaltime;
gs.functime += s.functime;
static void mergeFan(Symbol** proot, SymPair** pgf, const(SymPair)* sf)
{
for (; sf; sf = sf.next)
{
auto sym = trace_addsym(proot, sf.sym.Sident);
for (auto gf = *pgf; 1; gf = gf.next)
{
if (!gf)
{
auto sp = cast(SymPair *)trace_malloc(SymPair.sizeof);
sp.next = *pgf;
*pgf = sp;
sp.sym = sym;
sp.count = sf.count;
break;
}
if (gf.sym == sym)
{
gf.count += sf.count;
break;
}
}
}
}
mergeFan(proot, &gs.Sfanin, s.Sfanin);
mergeFan(proot, &gs.Sfanout, s.Sfanout);
mergeSymbol(proot, s.Sl);
s = s.Sr;
}
}
mergeSymbol(&groot, root);
}
}
// Free the memory for the thread local symbol table (root)
static void freeSymbol(Symbol* s)
{
while (s)
{
freeSymbol(s.Sl);
auto next = s.Sr;
static void freeSymPair(SymPair* sp)
{
while (sp)
{
auto spnext = sp.next;
trace_free(sp);
sp = spnext;
}
}
freeSymPair(s.Sfanin);
freeSymPair(s.Sfanout);
trace_free(s);
s = next;
}
}
freeSymbol(root);
root = null;
}
shared static ~this()
{
//printf("shared static ~this() groot = %p\n", groot);
if (gtrace_inited == 1)
{
gtrace_inited = 2;
// Merge in data from any existing file
trace_merge(&groot);
// Report results
FILE* fplog = trace_logfilename.length == 0 ? stdout :
fopen((trace_logfilename ~ '\0').ptr, "w");
if (fplog)
{
auto nsymbols = trace_report(fplog, groot);
auto p = cast(Symbol **)trace_malloc((Symbol *).sizeof * nsymbols);
auto psymbols = p[0 .. nsymbols];
uint u;
trace_array(psymbols, groot, u);
trace_times(fplog, psymbols);
fclose(fplog);
trace_free(psymbols.ptr);
psymbols = null;
}
else
fprintf(stderr, "cannot write '%s'", trace_logfilename.ptr);
// Output function link order
FILE* fpdef = trace_deffilename.length == 0 ? stdout :
fopen((trace_deffilename ~ '\0').ptr, "w");
if (fpdef)
{
fprintf(fpdef,"\nFUNCTIONS\n");
trace_order(fpdef, groot);
fclose(fpdef);
}
else
fprintf(stderr, "cannot write '%s'", trace_deffilename.ptr);
}
}
/////////////////////////////////
// Our storage allocator.
private void *trace_malloc(size_t nbytes)
{
auto p = malloc(nbytes);
if (!p)
exit(EXIT_FAILURE);
return p;
}
private void trace_free(void *p)
{
free(p);
}
//////////////////////////////////////////////
//
private Symbol* trace_addsym(Symbol** proot, const(char)[] id)
{
//printf("trace_addsym('%s',%d)\n",p,len);
auto parent = proot;
auto rover = *parent;
while (rover !is null) // while we haven't run out of tree
{
auto cmp = dstrcmp(id, rover.Sident);
if (cmp == 0)
{
return rover;
}
parent = (cmp < 0) ? /* if we go down left side */
&(rover.Sl) : /* then get left child */
&(rover.Sr); /* else get right child */
rover = *parent; /* get child */
}
/* not in table, so insert into table */
auto s = cast(Symbol *)trace_malloc(Symbol.sizeof);
memset(s,0,Symbol.sizeof);
s.Sident = id;
*parent = s; // link new symbol into tree
return s;
}
/***********************************
* Add symbol s with count to SymPair list.
*/
private void trace_sympair_add(SymPair** psp, Symbol* s, ulong count)
{
SymPair* sp;
for (; 1; psp = &sp.next)
{
sp = *psp;
if (!sp)
{
sp = cast(SymPair *)trace_malloc(SymPair.sizeof);
sp.sym = s;
sp.count = 0;
sp.next = null;
*psp = sp;
break;
}
else if (sp.sym == s)
{
break;
}
}
sp.count += count;
}
//////////////////////////////////////////////
// This one is called by DMD
private void trace_pro(char[] id)
{
//printf("trace_pro(ptr = %p, length = %lld)\n", id.ptr, id.length);
//printf("trace_pro(id = '%.*s')\n", id.length, id.ptr);
if (!trace_inited)
{
trace_inited = true;
trace_init(); // initialize package
}
timer_t starttime;
QueryPerformanceCounter(&starttime);
if (id.length == 0)
return;
auto tos = stack_push();
auto s = trace_addsym(&root, id);
tos.sym = s;
if (tos.prev)
{
// Accumulate Sfanout and Sfanin
auto prev = tos.prev.sym;
trace_sympair_add(&prev.Sfanout,s,1);
trace_sympair_add(&s.Sfanin,prev,1);
}
timer_t t;
QueryPerformanceCounter(&t);
tos.starttime = starttime;
tos.ohd = trace_ohd + t - starttime;
tos.subtime = 0;
++s.recursion;
//printf("tos.ohd=%lld, trace_ohd=%lld + t=%lld - starttime=%lld\n",
// tos.ohd,trace_ohd,t,starttime);
}
// Called by some old versions of DMD
void _c_trace_pro(size_t idlen, char* idptr)
{
char[] id = idptr[0 .. idlen];
trace_pro(id);
}
/////////////////////////////////////////
// Called by DMD generated code
void _c_trace_epi()
{
//printf("_c_trace_epi()\n");
auto tos = trace_tos;
if (tos)
{
timer_t endtime;
QueryPerformanceCounter(&endtime);
auto starttime = tos.starttime;
auto totaltime = endtime - starttime - tos.ohd;
if (totaltime < 0)
{ //printf("endtime=%lld - starttime=%lld - tos.ohd=%lld < 0\n",
// endtime,starttime,tos.ohd);
totaltime = 0; // round off error, just make it 0
}
// totaltime is time spent in this function + all time spent in
// subfunctions - bookkeeping overhead.
--tos.sym.recursion;
if(tos.sym.recursion == 0)
tos.sym.totaltime += totaltime;
//if (totaltime < tos.subtime)
//printf("totaltime=%lld < tos.subtime=%lld\n",totaltime,tos.subtime);
tos.sym.functime += totaltime - tos.subtime;
auto ohd = tos.ohd;
auto n = tos.prev;
stack_free(tos);
trace_tos = n;
if (n)
{
timer_t t;
QueryPerformanceCounter(&t);
n.ohd += ohd + t - endtime;
n.subtime += totaltime;
//printf("n.ohd = %lld\n",n.ohd);
}
}
}
////////////////////////// FILE INTERFACE /////////////////////////
/////////////////////////////////////
// Read line from file fp.
// Returns:
// trace_malloc'd line buffer
// null if end of file
private char* trace_readline(FILE* fp)
{
int dim;
int i;
char *buf;
//printf("trace_readline(%p)\n", fp);
while (1)
{
if (i == dim)
{
dim += 80;
auto p = cast(char *)trace_malloc(dim);
memcpy(p,buf,i);
trace_free(buf);
buf = p;
}
int c = fgetc(fp);
switch (c)
{
case EOF:
if (i == 0)
{ trace_free(buf);
return null;
}
goto L1;
case '\n':
goto L1;
default:
break;
}
buf[i] = cast(char)c;
i++;
}
L1:
buf[i] = 0;
//printf("line '%s'\n",buf);
return buf;
}
//////////////////////////////////////
// Skip space
private char *skipspace(char *p)
{
while (isspace(*p))
p++;
return p;
}
////////////////////////////////////////////////////////
// Merge in profiling data from existing file.
private void trace_merge(Symbol** proot)
{
FILE *fp;
if (trace_logfilename.length && (fp = fopen(trace_logfilename.ptr,"r")) !is null)
{
char* buf = null;
SymPair* sfanin = null;
auto psp = &sfanin;
char *p;
ulong count;
Symbol *s;
while (1)
{
trace_free(buf);
buf = trace_readline(fp);
if (!buf)
break;
switch (*buf)
{
case '=': // ignore rest of file
trace_free(buf);
goto L1;
case ' ':
case '\t': // fan in or fan out line
count = strtoul(buf,&p,10);
if (p == buf) // if invalid conversion
continue;
p = skipspace(p);
if (!*p)
continue;
s = trace_addsym(proot, p[0 .. strlen(p)]);
trace_sympair_add(psp,s,count);
break;
default:
if (!isalpha(*buf))
{
if (!sfanin)
psp = &sfanin;
continue; // regard unrecognized line as separator
}
goto case;
case '?':
case '_':
case '$':
case '@':
p = buf;
while (isgraph(*p))
p++;
*p = 0;
//printf("trace_addsym('%s')\n",buf);
s = trace_addsym(proot, buf[0 .. strlen(buf)]);
if (s.Sfanin)
{ SymPair *sp;
for (; sfanin; sfanin = sp)
{
trace_sympair_add(&s.Sfanin,sfanin.sym,sfanin.count);
sp = sfanin.next;
trace_free(sfanin);
}
}
else
{ s.Sfanin = sfanin;
}
sfanin = null;
psp = &s.Sfanout;
{
p++;
count = strtoul(p,&p,10);
timer_t t = cast(long)strtoull(p,&p,10);
s.totaltime += t;
t = cast(long)strtoull(p,&p,10);
s.functime += t;
}
break;
}
}
L1:
fclose(fp);
}
}
////////////////////////// COMPILER INTERFACE /////////////////////
version (Windows)
{
extern (Windows)
{
export int QueryPerformanceCounter(timer_t *);
export int QueryPerformanceFrequency(timer_t *);
}
}
else version (D_InlineAsm_X86)
{
extern (D)
{
void QueryPerformanceCounter(timer_t* ctr)
{
asm
{
naked ;
mov ECX,EAX ;
rdtsc ;
mov [ECX],EAX ;
mov 4[ECX],EDX ;
ret ;
}
}
void QueryPerformanceFrequency(timer_t* freq)
{
*freq = 3579545;
}
}
}
else version (D_InlineAsm_X86_64)
{
extern (D)
{
void QueryPerformanceCounter(timer_t* ctr)
{
asm
{
naked ;
rdtsc ;
mov [RDI],EAX ;
mov 4[RDI],EDX ;
ret ;
}
}
void QueryPerformanceFrequency(timer_t* freq)
{
*freq = 3579545;
}
}
}
else
{
static assert(0);
}