raz / commit 051844ee

commit
051844eeffb59304645e4647e2c4d0fa9af2f868
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
4fb63340
The profile says each procedure's total, with its calls'

By the owner's word. With RAZ_PROFILE set, onProf also notes the calls
under way at each sample, up the chain of frames while it stays in the
program's own code (C's may use rbp for anything), into rows mapped
only when profiling; profile counts each procedure once for each
sample it was on the chain of, and says its share beside its own:

   16%     27%  Screen.put
    9%     93%  Parser.feed

A program not profiled runs as before (rat 516 and 522 ms, within
noise; profiled, 523 and 526); hello's text grows 1.8 KB.

patch · log

3 files changed, +91 -22

README.md (history) modified +2 -1

@@ -105,7 +105,8 @@
105105
106106 A program run with `RAZ_PROFILE` set in its environment is sampled
107107 every millisecond of its time; at its end it says which procedures, and
108-which places in the source, took the most of it.
108+which places in the source, took the most of it: each procedure's time
109+in its own code, and its total, with the procedures it called.
109110
110111 `raz run Main.raz a b` compiles the same way, then runs the program
111112 with the arguments `a b`, writing no file: its output and exit status

doc/compiler.md (history) modified +5 -3

@@ -761,9 +761,11 @@
761761 `Rt.trace` does the same on demand. A thread's first frame has `rbp`
762762 zero, which ends the chain. With `RAZ_PROFILE` set, `Rt`'s body has the
763763 kernel send SIGPROF every millisecond of the program's time
764-(`setitimer`); `onProf` notes the address it interrupted, and `exit`
765-has `profile` say which procedures and places those fell in, by the
766-same tables.
764+(`setitimer`); `onProf` notes the address it interrupted and the
765+calls under way, up the chain of frames while it stays in the program's
766+own code, and `exit` has `profile` say which procedures and places
767+those fell in, by the same tables: each procedure's samples in its own
768+code, and its total, counting once each sample it was on the chain of.
767769
768770 The layout is chosen so the file can be mapped as it lies: text just
769771 after the headers, rodata on the next page, then a writable page that

lib/Rt.raz (history) modified +84 -18

@@ -7,7 +7,9 @@
77 the calls under way and their variables, from the frames' chain of
88 rbp and the compiler's tables in places. A program run with
99 RAZ_PROFILE set is sampled every millisecond of its time, and at its
10- end Rt says which procedures and places in the source took it.
10+ end Rt says which procedures and places in the source took it: each
11+ procedure's samples in its own code, and in all, with those in the
12+ procedures it was calling.
1113 The handlers are Linux's on x86-64, set by system calls: what the
1214 kernel hands them is laid out as it is there. *)
1315 module Rt;
@@ -27,6 +29,7 @@
2729 MaxSamples = 100000;
2830 MaxProcs = 8192;
2931 MaxPlaces = 65536;
32+ Depth = 24; (* the callers noted with each sample, at most *)
3033 (* SA_SIGINFO, SA_ONSTACK, SA_RESTORER and SA_RESETHAND *)
3134 Flags = 4H + 8000000H + 4000000H + 80000000H;
3235
@@ -72,9 +75,13 @@
7275 (* the profile: where the program was at each sample, and how many
7376 samples fell in each procedure and at each place *)
7477 samples: [MaxSamples]UINT;
78+ (* the callers at each sample, by their return addresses: a row of
79+ Depth for each, a short one ended by 0; mapped when profiling *)
80+ chains: ^UINT;
7581 numSamples: INT;
7682 profiling: BOOL;
7783 procCount: [MaxProcs]INT;
84+ procTotal: [MaxProcs]INT; (* the samples each procedure was on the chain of *)
7885 placeCount: [MaxPlaces]INT;
7986 timerv: [4]INT; (* setitimer's interval and value *)
8087
@@ -483,16 +490,50 @@
483490 calls(ip, bp, why # "a stack overflow");
484491 end;
485492
493+(* Whether a is in the program's own code, not C's *)
494+proc inCode(a: UINT): BOOL;
495+begin
496+ return (a >= UINT(places[1])) and (a < UINT(places[2]));
497+end;
498+
486499 (* SIGPROF, every millisecond the program runs, in any of its threads:
487- notes where it was *)
500+ notes where it was, and the calls under way, up the chain of frames.
501+ The chain is followed only through the program's own code, whose
502+ frames all keep it in rbp: C's code may use rbp for anything. *)
488503 proc onProf(_sig: INT; _info, ctx: ^U8);
489-var n: INT; q: ^UINT;
504+var n, k: INT; q, f, row: ^UINT; ip, bp: UINT;
490505 begin
491506 n := XADD(ADR(numSamples), 1);
507+ if n >= MaxSamples then return; end;
508+ q := ^UINT(ctx + 120); (* uc_mcontext's rbp *)
509+ bp := q^;
492510 q := ^UINT(ctx + 168); (* uc_mcontext's rip *)
493- if n < MaxSamples then samples[n] := q^; end;
511+ ip := q^;
512+ samples[n] := ip;
513+ row := chains + n * Depth;
514+ k := 0;
515+ if (places # nil) and inCode(ip) then
516+ while (k < Depth) and (bp # 0) and (bp & 7 = 0) do
517+ f := ^UINT(bp);
518+ if not inCode(f[1]) then break; end;
519+ row[k] := f[1] - 1; (* the call, by its return address *)
520+ INC(k);
521+ if f[0] <= bp then break; end;
522+ bp := f[0];
523+ end;
524+ end;
525+ if k < Depth then row[k] := 0; end;
494526 end;
495527
528+(* The number of the procedure the code at address a is in, or -1 *)
529+proc procNum(a: UINT): INT;
530+var p: ^Proc;
531+begin
532+ p := procAt(a);
533+ if p = nil then return -1; end;
534+ return p - ^Proc(^U8(places + 5) + 16 * places[0]);
535+end;
536+
496537 (* The index of the place the code at address a comes from, or -1 *)
497538 proc placeAt(a: UINT): INT;
498539 var p: ^Place; i, best: INT;
@@ -505,8 +546,19 @@
505546 return best;
506547 end;
507548
508-(* Says the ten that took most of count's n samples, by name *)
509-proc top(count: []INT; n: INT; byPlace: BOOL);
549+(* Puts the share of n that k is, as a percentage four wide *)
550+proc putShare(k, n: INT);
551+var i: INT;
552+begin
553+ i := k * 100 div n;
554+ if i < 10 then put(" "); elsif i < 100 then put(" "); else put(" "); end;
555+ putInt(i);
556+ put("%");
557+end;
558+
559+(* Says the ten that took most of count's n samples, by name, with
560+ their totals beside them if total isn't empty *)
561+proc top(count, total: []INT; n: INT; byPlace: BOOL);
510562 var i, k, best: INT; p: ^Proc; q: ^Place;
511563 begin
512564 p := ^Proc(^U8(places + 5) + 16 * places[0]);
@@ -517,10 +569,9 @@
517569 if count[i] > count[best] then best := i; end;
518570 end;
519571 if count[best] = 0 then return; end;
520- i := count[best] * 100 div n;
521- if i < 10 then put(" "); elsif i < 100 then put(" "); else put(" "); end;
522- putInt(i);
523- put("% ");
572+ putShare(count[best], n);
573+ if LEN(total) > 0 then put(" "); putShare(total[best], n); end;
574+ put(" ");
524575 if byPlace then
525576 putStr(names() + INT(q[best].path));
526577 put(":"); putInt(INT(q[best].line));
@@ -535,7 +586,7 @@
535586 (* At the end of a profiled run: which procedures, and which places in
536587 the source, took the time, as shares of the samples *)
537588 proc profile;
538-var t: [4]INT; i, n, k: INT; p: ^Proc;
589+var t: [4]INT; i, n, k, j, m, s: INT; seen: [Depth + 1]INT; row: ^UINT;
539590 begin
540591 t := [0, 0, 0, 0];
541592 _ := SYSCALL(38, 2, ADR(t), 0); (* setitimer(ITIMER_PROF): no more *)
@@ -544,22 +595,34 @@
544595 if n > MaxSamples then n := MaxSamples; end;
545596 if (places = nil) or (n = 0) then return; end;
546597 for i := 0 to n - 1 do
547- p := procAt(samples[i]);
548- if p # nil then
549- k := (p - ^Proc(^U8(places + 5) + 16 * places[0]));
550- if k < MaxProcs then INC(procCount[k]); end;
598+ k := procNum(samples[i]);
599+ if (k >= 0) and (k < MaxProcs) then INC(procCount[k]); end;
600+ (* each procedure on the sample's chain counts once in its
601+ total, however often it is there *)
602+ row := chains + i * Depth;
603+ m := 0;
604+ for j := -1 to Depth - 1 do
605+ if j < 0 then k := procNum(samples[i]);
606+ elsif row[j] = 0 then break;
607+ else k := procNum(row[j]);
608+ end;
609+ if (k >= 0) and (k < MaxProcs) then
610+ s := 0;
611+ while (s < m) and (seen[s] # k) do INC(s); end;
612+ if s = m then seen[m] := k; INC(m); INC(procTotal[k]); end;
613+ end;
551614 end;
552615 k := placeAt(samples[i]);
553616 if (k >= 0) and (k < MaxPlaces) then INC(placeCount[k]); end;
554617 end;
555618 put("profile: ");
556619 putInt(n);
557- put(" samples of a millisecond");
620+ put(" samples of a millisecond; each procedure's own, then with its calls'");
558621 say;
559- top(procCount[0..places[3]], n, false);
622+ top(procCount[0..places[3]], procTotal[0..places[3]], n, false);
560623 put("by place:");
561624 say;
562- top(placeCount[0..places[0]], n, true);
625+ top(placeCount[0..places[0]], placeCount[0..0], n, true);
563626 end;
564627
565628 begin
@@ -569,6 +632,9 @@
569632 _ := SYSCALL(13, SigSegv, ADR(act), 0, 8); (* rt_sigaction *)
570633 _ := SYSCALL(13, SigFpe, ADR(act), 0, 8);
571634 if LEN(Sys.env("RAZ_PROFILE")) > 0 then
635+ chains := ^UINT(Sys.mmap(MaxSamples * Depth * SIZE(UINT)));
636+ end;
637+ if chains # nil then
572638 act := {handler: onProf, flags: ProfFlags, restorer: sigReturn};
573639 _ := SYSCALL(13, SigProf, ADR(act), 0, 8);
574640 timerv := [0, 1000, 0, 1000]; (* every millisecond, from one on *)