Skip to content
Skillv1.0.0

profiling

性能分析

by chaterm(0) 0 installs
Free
Sign in to install

Free account. Installing gives you the manifest plus copy-paste snippets.

See reviews

About

Imported from chaterm/terminal-skills (performance/profiling/SKILL.md). Install upstream with npx skills add chaterm/terminal-skills --skill profiling. Copyright stays with the author.

性能分析

概述

CPU/内存分析、火焰图、追踪技能。

perf 工具

基础命令

# 安装
apt install linux-tools-common linux-tools-$(uname -r)

# CPU 采样
perf record -g -p PID
perf record -g -a -- sleep 30

# 查看报告
perf report
perf report --stdio

# 实时统计
perf top
perf top -p PID

# 统计事件
perf stat command
perf stat -p PID sleep 10

常用事件

# CPU 周期
perf record -e cycles -p PID

# 缓存未命中
perf record -e cache-misses -p PID

# 上下文切换
perf record -e context-switches -p PID

# 列出可用事件
perf list

火焰图

# 采集数据
perf record -F 99 -g -p PID -- sleep 30

# 生成火焰图
perf script | stackcollapse-perf.pl | flamegraph.pl > flamegraph.svg

# 或使用 FlameGraph 工具
git clone https://github.com/brendangregg/FlameGraph
perf script | ./FlameGraph/stackcollapse-perf.pl | ./FlameGraph/flamegraph.pl > out.svg

strace 追踪

基础用法

# 追踪进程
strace -p PID

# 追踪命令
strace command

# 统计系统调用
strace -c command
strace -c -p PID

# 追踪特定调用
strace -e open,read,write command
strace -e trace=network command
strace -e trace=file command

高级选项

# 显示时间戳
strace -t command
strace -tt command      # 微秒

# 显示耗时
strace -T command

# 跟踪子进程
strace -f command

# 输出到文件
strace -o trace.log command

ltrace 库调用

# 追踪库调用
ltrace command
ltrace -p PID

# 统计
ltrace -c command

# 特定库
ltrace -l libc.so.6 command

内存分析

valgrind

# 内存泄漏检测
valgrind --leak-check=full ./program

# 内存错误
valgrind --tool=memcheck ./program

# 缓存分析
valgrind --tool=cachegrind ./program

# 调用图
valgrind --tool=callgrind ./program
kcachegrind callgrind.out.*

pmap

# 查看进程内存映射
pmap PID
pmap -x PID

# 详细信息
pmap -XX PID

smem

# 内存使用统计
smem
smem -u          # 按用户
smem -p          # 按进程
smem -k          # 人类可读

系统分析

vmstat

# 每秒刷新
vmstat 1

# 输出说明
# r: 运行队列
# b: 阻塞进程
# si/so: 交换
# bi/bo: 块 IO
# us/sy/id/wa: CPU 使用

iostat

# 磁盘统计
iostat -x 1

# 输出说明
# %util: 设备利用率
# await: 平均等待时间
# r/s, w/s: 读写 IOPS

pidstat

# CPU 使用
pidstat -u 1

# 内存使用
pidstat -r 1

# IO 使用
pidstat -d 1

# 特定进程
pidstat -p PID 1

常见场景

场景 1:CPU 热点分析

#!/bin/bash
PID=$1

# 采集 30 秒
perf record -F 99 -g -p $PID -- sleep 30

# 生成报告
perf report --stdio > cpu_report.txt

# 生成火焰图
perf script | stackcollapse-perf.pl | flamegraph.pl > cpu_flame.svg

场景 2:IO 延迟分析

#!/bin/bash
# 使用 biolatency (bcc-tools)
biolatency 10 1

# 或使用 iostat
iostat -x 1 10

场景 3:系统调用分析

#!/bin/bash
PID=$1

# 统计系统调用
strace -c -p $PID -o syscall_stat.txt &
sleep 60
kill %1

cat syscall_stat.txt

工具对比

工具 用途 开销
perf CPU 分析
strace 系统调用
valgrind 内存分析 很高
pidstat 进程统计

故障排查

# perf 权限问题
echo 0 > /proc/sys/kernel/perf_event_paranoid

# 符号缺失
apt install linux-tools-$(uname -r)-dbgsym

# strace 附加失败
echo 0 > /proc/sys/kernel/yama/ptrace_scope

Use it

Copy one of these into your project. Installing also returns the manifest and these snippets.

yaml
targets:
  - https://api.opensmartroute.ai/api/v1/registry/chaterm-terminal-skills-profiling/manifest   # or paste the manifest below

Manifest

An Open Capability Manifest: the router reads it to know what this does, what it costs and when to pick it.

chaterm-terminal-skills-profiling.ocm.jsonjson
{
  "ocm": "1",
  "id": "chaterm-terminal-skills-profiling",
  "kind": "skill",
  "name": "profiling",
  "description": "性能分析",
  "publisher": "chaterm",
  "version": "1.0.0",
  "capabilities": {
    "domains": [
      "general"
    ],
    "tags": [
      "skill-md",
      "performance",
      "profiling",
      "perf",
      "flamegraph",
      "strace",
      "cpu",
      "skills-sh"
    ],
    "languages": [
      "en"
    ]
  },
  "quality_prior": 0.6,
  "examples": [
    "性能分析"
  ],
  "primary": false,
  "metadata": {
    "source": {
      "provider": "skills.sh",
      "repository": "https://github.com/chaterm/terminal-skills",
      "path": "performance/profiling/SKILL.md",
      "ref": "HEAD",
      "url": "https://github.com/chaterm/terminal-skills/blob/HEAD/performance/profiling/SKILL.md",
      "key": "chaterm/terminal-skills/performance/profiling/SKILL.md"
    }
  },
  "instructions": "# 性能分析\n\n## 概述\nCPU/内存分析、火焰图、追踪技能。\n\n## perf 工具\n\n### 基础命令\n```bash\n# 安装\napt install linux-tools-common linux-tools-$(uname -r)\n\n# CPU 采样\nperf record -g -p PID\nperf record -g -a -- sleep 30\n\n# 查看报告\nperf report\nperf report --stdio\n\n# 实时统计\nperf top\nperf top -p PID\n\n# 统计事件\nperf stat command\nperf stat -p PID sleep 10\n```\n\n### 常用事件\n```bash\n# CPU 周期\nperf record -e cycles -p PID\n\n# 缓存未命中\nperf record -e cache-misses -p PID\n\n# 上下文切换\nperf record -e context-switches -p PID\n\n# 列出可用事件\nperf list\n```\n\n### 火焰图\n```bash\n# 采集数据\nperf record -F 99 -g -p PID -- sleep 30\n\n# 生成火焰图\nperf script | stackcollapse-perf.pl | fla",
  "cost": {
    "context_tokens": 723
  }
}

Fetch it by URL: GET /api/v1/registry/chaterm-terminal-skills-profiling/manifest?version=1.0.0

Reviews

Star ratings from people who tried it. One review per account; edit yours any time.

No reviews yet. Install it, try it, and be the first to rate it.