synapse
A component of the AienOS ecosystem.
Brought by drakestapleton
synapse / scripts / p65-sample-report.sh
| 1 | #!/usr/bin/env bash |
| 2 | # Issue #65 Stage A: fold gdb stack samples (from p65-gdb-sample.sh) into thread-sample counts. |
| 3 | # Usage: p65-sample-report.sh <samples.txt> <anchor-function> |
| 4 | # Only threads whose stack contains <anchor-function> (for example synapse_submit) are counted. |
| 5 | # Every such thread-sample gets: |
| 6 | # state : run (user code) or wait:<syscall> <- <first recognised caller> when frame #0 is a |
| 7 | # system call wrapper (futex, fdatasync, pwrite, ...) |
| 8 | # phase : the callee frame directly under the anchor (where in the operation the thread is) |
| 9 | # leaf : for run samples, the leaf-most frame that names a crate or libc area. Frames from |
| 10 | # std/core and frames with crate-relative paths (src/...) are skipped so that inlined |
| 11 | # helpers are charged to the crate that called them. |
| 12 | # incl : for run samples, every crate present between frame #0 and the anchor (counted once |
| 13 | # per thread-sample), i.e. inclusive time. |
| 14 | set -euo pipefail |
| 15 | samples=$1; anchor=$2 |
| 16 | awk -v anchor="$anchor" ' |
| 17 | function base(f, a,n){ sub(/:[0-9]+$/,"",f); n=split(f,a,"/"); return a[n] } |
| 18 | function crate(fn,file, a,b,n,i,c){ |
| 19 | if (file ~ /registry\/src\//) { n=split(file,a,"/"); for (i=1;i<n;i++) if (a[i] ~ /^index\.crates\.io/) { c=a[i+1]; sub(/-[0-9][0-9.]*$/,"",c); return c } } |
| 20 | if (file ~ /crates\/synapse-/) { split(file,a,"crates/"); split(a[2],b,"/"); return b[1] ":" base(file) } |
| 21 | if (file ~ /malloc\// || fn ~ /malloc|^__libc_free|^free$|calloc|realloc|cfree|_int_free|tcache/) return "libc:malloc/free"; |
| 22 | if (file ~ /string\// || fn ~ /memcpy|memmove|memset|memcmp|strlen/) return "libc:mem*"; |
| 23 | if (file ~ /^\.\.\/sysdeps|^\.\/nptl/) return "libc:" fn; |
| 24 | if (file ~ /library\/alloc\//) return "rust:alloc"; |
| 25 | return "" |
| 26 | } |
| 27 | function flush( i,ai,st,ph,lf,c,f0,seen){ |
| 28 | if (!inth || nf == 0) { inth=0; nf=0; return } |
| 29 | ai=-1; for (i=0;i<nf;i++) if (fname[i]==anchor) { ai=i; break } |
| 30 | if (ai >= 0) { |
| 31 | total++ |
| 32 | f0=fname[0]; st="run" |
| 33 | if (f0 ~ /^(syscall|__GI_|__libc_|__futex|futex|fsync|fdatasync|__lll|pthread_cond|__pthread|clock_nanosleep|epoll|poll|sched_yield)/ && f0 !~ /malloc|free|calloc|realloc|mem/) { |
| 34 | st="wait:" f0 |
| 35 | for (i=1;i<ai;i++) { if (fname[i] ~ /futex_wait|lock_contended|wait_optional_timeout|wait<|sync_data|sync_all|write_all|pwrite|rename|open/) { st=st " <- " fname[i] "@" base(ffile[i]) ":" fline[i]; break } } |
| 36 | } |
| 37 | ph = (ai>0) ? fname[ai-1] "@" base(ffile[ai-1]) : "self" |
| 38 | PS[ph " | " (st=="run" ? "run" : "wait")]++ |
| 39 | S[st]++ |
| 40 | if (st=="run") { |
| 41 | runs++ |
| 42 | lf=""; for (i=0;i<ai;i++) { c=crate(fname[i],ffile[i]); if (c!="") { lf=c; break } } |
| 43 | if (lf=="") lf="(std/core only) " fname[0] |
| 44 | L[lf]++ |
| 45 | split("",seen); for (i=0;i<ai;i++) { c=crate(fname[i],ffile[i]); sub(/:.*/,"",c); if (c!="" && !(c in seen)) { seen[c]=1; I[c]++ } } |
| 46 | } |
| 47 | } |
| 48 | inth=0; nf=0 |
| 49 | } |
| 50 | /^==SAMPLE==/ { flush(); nsamp++; next } |
| 51 | /^Thread [0-9]+ / { flush(); inth=1; nf=0; next } |
| 52 | /^#[0-9]+ / { if (!inth) next; line=$0; sub(/^#[0-9]+ +(0x[0-9a-f]+ in )?/,"",line); fn=line; sub(/ \(.*$/,"",fn); |
| 53 | file=""; ln=""; if (match(line,/ at [^ ]+$/)) { file=substr(line,RSTART+4); ln=file; sub(/^.*:/,"",ln) } |
| 54 | fname[nf]=fn; ffile[nf]=file; fline[nf]=ln; nf++; next } |
| 55 | END { |
| 56 | flush() |
| 57 | printf "stack samples (all threads stopped): %d\nthread-samples with %s on stack: %d (run %d)\n", nsamp, anchor, total, runs |
| 58 | print "\n# state"; for (k in S) printf "%7d %s\n", S[k], k | "sort -rn"; close("sort -rn") |
| 59 | print "\n# phase (callee under anchor) | run or wait"; for (k in PS) printf "%7d %s\n", PS[k], k | "sort -rn"; close("sort -rn") |
| 60 | print "\n# leaf (self) of run samples"; for (k in L) printf "%7d %s\n", L[k], k | "sort -rn"; close("sort -rn") |
| 61 | print "\n# inclusive crate presence in run samples"; for (k in I) printf "%7d %s\n", I[k], k | "sort -rn"; close("sort -rn") |
| 62 | }' "$samples" |