profiler:基于 Python 的 Linux 进程树性能分析工具项目

可用于分析编译器性能,通过采样 /proc 监控进程墙时间、CPU 时间、内存等指标。无需 root 或特殊环境,支持生成终端报告、CSV、JSON 及 Chrome Trace 等多种输出,帮助定位性能瓶颈。【此简介由AI生成】

Branch1Tags0

profiler

Linux process-tree profiler for compiler performance analysis.

This tool launches a command, samples /proc, and reports where wall time, CPU time, subprocess time, memory, and wait states are spent. It does not need root, perf, eBPF, ptrace, or an external hivmc environment.

BishengIR example

Run this from your BishengIR build directory:

python3 /home/liupengcheng/workspace/npuir/profiler/profile_process_tree.py \
  --brief \
  --interval 0.01 \
  --csv /tmp/bishengir-process-profile.csv \
  --json /tmp/bishengir-process-profile.json \
  --trace-json /tmp/bishengir-process-trace.json \
  --strace-hint \
  -- \
  ./bin/bishengir-compile \
  --target=Ascend950PR_9589 \
  --enable-auto-multi-buffer=False \
  --enable-auto-bind-sub-block=True \
  --disable-ffts \
  --set-workspace-multibuffer=2 \
  --enable-auto-blockify-loop \
  --enable-hfusion-compile=true \
  --enable-triton-kernel-compile=true \
  --enable-vf-merge-level=1 \
  ./idk/1.mlir \
  --mlir-timing \
  --enable-tuning-mode

The command after -- is the profiled command. By default command lines are no longer truncated. Use --brief for a compact terminal report: command columns show only binary names, state/wchan are hidden, dead/hidden/subtree CPU columns are omitted, and only one top table is printed. Use --cmd-width 180 if you prefer truncation in detailed mode.

Add --state only when you want sampled process states and kernel wait sites in the terminal report:

--brief --state

When you need exact process syscall events, add:

--strace-process-log /tmp/bishengir-proc.strace

This runs the target under strace -ff -ttT -e trace=process and writes /tmp/bishengir-proc.strace.<pid> files. Use it for fork/exec/wait timing, not for baseline performance numbers, because strace adds overhead.

Outputs

  • Terminal report: process tree plus top lists. With --brief, it prints a compact process tree and one top table using basename-only commands.
  • CSV: sortable summary table for spreadsheets or scripts.
  • Summary JSON: machine-readable version of the same process summary.
  • Chrome Trace JSON: process lifetime and sampled state timeline view for chrome://tracing, Perfetto, or other trace viewers that accept Chrome trace event JSON.

Open the trace with:

chrome://tracing

or upload /tmp/bishengir-process-trace.json to a local Perfetto UI. The trace shows when bishengir-compile, Python glue, hivmc, bisheng, and ld.lld overlap or wait on each other.

Metrics

  • wall: elapsed time between first and last observation of a process.
  • user: user-mode CPU time from /proc/[pid]/stat.
  • sys: kernel-mode CPU time from /proc/[pid]/stat.
  • cpu: user + sys.
  • dead_child_cpu: CPU time accumulated from already-exited children via cutime + cstime.
  • observed_child_cpu: CPU time attributed to direct child processes that the sampler observed.
  • hidden_child_cpu: dead_child_cpu - observed_child_cpu, clamped at zero. This is the best-effort signal for short-lived child processes that were not sampled by PID.
  • subtree_cpu: sum of observed live child records under that process.
  • rss: max resident memory observed for the process.
  • state: sampled process-state distribution, such as R:95%,S:5%.
  • wchan: sampled kernel wait function distribution for sleeping/blocking processes.

Interpretation:

  • High user means the process is mostly computing, commonly compiler optimization or code generation work.
  • High sys means kernel overhead is significant, commonly file I/O, process management, memory mapping, allocation, or page fault pressure.
  • High dead_child_cpu means the parent waited for children that have already exited.
  • High hidden_child_cpu means some child CPU was not attributable to observed child PIDs. This is when strace is most useful.
  • A state distribution dominated by D usually means uninterruptible kernel wait, often disk or filesystem I/O.
  • A state distribution dominated by S with low CPU usually means waiting on child processes, pipes, futexes, or timers.
  • wchan=do_wait usually means the process is blocked waiting for a child.
  • wchan=hrtimer_nanosleep usually means an explicit sleep or timer wait.

Design

The script is intentionally a single Python file with three layers:

  • ProcessTreeSampler: owns command execution and /proc sampling.
  • ProcSample / ProcRecord: normalized snapshots and aggregated metrics.
  • Report writers: terminal, CSV, summary JSON, and Chrome Trace JSON.

Each process is keyed by (pid, start_ticks) instead of pid alone, so PID reuse cannot merge two unrelated records. Parent-child edges are discovered from ppid during sampling. The sampler expands the live tree by reading /proc/[pid]/task/*/children from known processes instead of scanning all of /proc every interval. This keeps profiler overhead lower and catches compiler subprocesses more reliably on busy machines.

Command lines are merged conservatively across samples. When an exited process turns into a zombie, /proc/[pid]/cmdline can become empty, so the profiler keeps the earlier full command line instead of replacing it with the short comm name.

The profiler is sampling based. A child that starts and exits between two samples cannot always be named. cutime and cstime make that blind spot measurable: when a parent has high hidden_child_cpu, switch to --strace-process-log or the printed --strace-hint command for exact fork/exec/exit timing.

The Chrome Trace output contains both process lifetime spans and sampled state spans. Use it to see whether bishengir-compile, Python glue, hivmc, bisheng, and ld.lld overlap, run, sleep, or wait on children.

Tuning

Use a smaller interval to catch shorter subprocesses:

--interval 0.005

or:

--interval 0.001

Smaller intervals increase profiler overhead because every sample reads more /proc files from the observed process tree.

Introduction

可用于分析编译器性能,通过采样 /proc 监控进程墙时间、CPU 时间、内存等指标。无需 root 或特殊环境,支持生成终端报告、CSV、JSON 及 Chrome Trace 等多种输出,帮助定位性能瓶颈。【此简介由AI生成】

Customize your domain