| | @@ -7,7 +7,9 @@ |
| 7 | 7 | the calls under way and their variables, from the frames' chain of |
| 8 | 8 | rbp and the compiler's tables in places. A program run with |
| 9 | 9 | 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. |
| 11 | 13 | The handlers are Linux's on x86-64, set by system calls: what the |
| 12 | 14 | kernel hands them is laid out as it is there. *) |
| 13 | 15 | module Rt; |
| | @@ -27,6 +29,7 @@ |
| 27 | 29 | MaxSamples = 100000; |
| 28 | 30 | MaxProcs = 8192; |
| 29 | 31 | MaxPlaces = 65536; |
| 32 | + Depth = 24; (* the callers noted with each sample, at most *) |
| 30 | 33 | (* SA_SIGINFO, SA_ONSTACK, SA_RESTORER and SA_RESETHAND *) |
| 31 | 34 | Flags = 4H + 8000000H + 4000000H + 80000000H; |
| 32 | 35 | |
| | @@ -72,9 +75,13 @@ |
| 72 | 75 | (* the profile: where the program was at each sample, and how many |
| 73 | 76 | samples fell in each procedure and at each place *) |
| 74 | 77 | 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; |
| 75 | 81 | numSamples: INT; |
| 76 | 82 | profiling: BOOL; |
| 77 | 83 | procCount: [MaxProcs]INT; |
| 84 | + procTotal: [MaxProcs]INT; (* the samples each procedure was on the chain of *) |
| 78 | 85 | placeCount: [MaxPlaces]INT; |
| 79 | 86 | timerv: [4]INT; (* setitimer's interval and value *) |
| 80 | 87 | |
| | @@ -483,16 +490,50 @@ |
| 483 | 490 | calls(ip, bp, why # "a stack overflow"); |
| 484 | 491 | end; |
| 485 | 492 | |
| 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 | + |
| 486 | 499 | (* 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. *) |
| 488 | 503 | 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; |
| 490 | 505 | begin |
| 491 | 506 | n := XADD(ADR(numSamples), 1); |
| 507 | + if n >= MaxSamples then return; end; |
| 508 | + q := ^UINT(ctx + 120); (* uc_mcontext's rbp *) |
| 509 | + bp := q^; |
| 492 | 510 | 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; |
| 494 | 526 | end; |
| 495 | 527 | |
| 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 | + |
| 496 | 537 | (* The index of the place the code at address a comes from, or -1 *) |
| 497 | 538 | proc placeAt(a: UINT): INT; |
| 498 | 539 | var p: ^Place; i, best: INT; |
| | @@ -505,8 +546,19 @@ |
| 505 | 546 | return best; |
| 506 | 547 | end; |
| 507 | 548 | |
| 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); |
| 510 | 562 | var i, k, best: INT; p: ^Proc; q: ^Place; |
| 511 | 563 | begin |
| 512 | 564 | p := ^Proc(^U8(places + 5) + 16 * places[0]); |
| | @@ -517,10 +569,9 @@ |
| 517 | 569 | if count[i] > count[best] then best := i; end; |
| 518 | 570 | end; |
| 519 | 571 | 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(" "); |
| 524 | 575 | if byPlace then |
| 525 | 576 | putStr(names() + INT(q[best].path)); |
| 526 | 577 | put(":"); putInt(INT(q[best].line)); |
| | @@ -535,7 +586,7 @@ |
| 535 | 586 | (* At the end of a profiled run: which procedures, and which places in |
| 536 | 587 | the source, took the time, as shares of the samples *) |
| 537 | 588 | 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; |
| 539 | 590 | begin |
| 540 | 591 | t := [0, 0, 0, 0]; |
| 541 | 592 | _ := SYSCALL(38, 2, ADR(t), 0); (* setitimer(ITIMER_PROF): no more *) |
| | @@ -544,22 +595,34 @@ |
| 544 | 595 | if n > MaxSamples then n := MaxSamples; end; |
| 545 | 596 | if (places = nil) or (n = 0) then return; end; |
| 546 | 597 | 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; |
| 551 | 614 | end; |
| 552 | 615 | k := placeAt(samples[i]); |
| 553 | 616 | if (k >= 0) and (k < MaxPlaces) then INC(placeCount[k]); end; |
| 554 | 617 | end; |
| 555 | 618 | put("profile: "); |
| 556 | 619 | putInt(n); |
| 557 | | - put(" samples of a millisecond"); |
| 620 | + put(" samples of a millisecond; each procedure's own, then with its calls'"); |
| 558 | 621 | say; |
| 559 | | - top(procCount[0..places[3]], n, false); |
| 622 | + top(procCount[0..places[3]], procTotal[0..places[3]], n, false); |
| 560 | 623 | put("by place:"); |
| 561 | 624 | say; |
| 562 | | - top(placeCount[0..places[0]], n, true); |
| 625 | + top(placeCount[0..places[0]], placeCount[0..0], n, true); |
| 563 | 626 | end; |
| 564 | 627 | |
| 565 | 628 | begin |
| | @@ -569,6 +632,9 @@ |
| 569 | 632 | _ := SYSCALL(13, SigSegv, ADR(act), 0, 8); (* rt_sigaction *) |
| 570 | 633 | _ := SYSCALL(13, SigFpe, ADR(act), 0, 8); |
| 571 | 634 | if LEN(Sys.env("RAZ_PROFILE")) > 0 then |
| 635 | + chains := ^UINT(Sys.mmap(MaxSamples * Depth * SIZE(UINT))); |
| 636 | + end; |
| 637 | + if chains # nil then |
| 572 | 638 | act := {handler: onProf, flags: ProfFlags, restorer: sigReturn}; |
| 573 | 639 | _ := SYSCALL(13, SigProf, ADR(act), 0, 8); |
| 574 | 640 | timerv := [0, 1000, 0, 1000]; (* every millisecond, from one on *) |