mcpField
N
native-app-profiling

native-app-profiling

github.com/patrickserrano开发1363 次安装41 周前更新
代码仓库

关于 native-app-profiling

通过 `xcrun xctrace` 记录原生应用性能、导出采样并符号化堆栈帧,无需打开 Instruments。适合排查 macOS 应用和 iOS 模拟器应用的性能热点。

主要功能

  1. 01记录 Time Profiler 跟踪
  2. 02将采样导出为 XML
  3. 03使用 `atos` 符号化堆栈帧
  4. 04定位 CPU 热点
  5. 05分析 iOS 模拟器应用

适用场景

  1. 01调查应用操作缓慢原因
  2. 02检查跟踪中的 CPU 采样
  3. 03将堆栈地址映射到符号

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/patrickserrano/skills --skill native-app-profiling

安装后在 Claude Code 中输入/native-app-profiling确认已加载。

详细介绍

原生应用性能分析(CLI)

概述

通过 xctrace 录制 Time Profiler,提取采样数据、符号化并识别热点,无需打开 Instruments。

快速开始

1) 录制 Time Profiler

附加到正在运行的进程:

# Get the PID first
pgrep -x "AppName"

# Record for 90 seconds
xcrun xctrace record \
    --template 'Time Profiler' \
    --time-limit 90s \
    --output /tmp/App.trace \
    --attach <pid>

启动并录制:

xcrun xctrace record \
    --template 'Time Profiler' \
    --time-limit 90s \
    --output /tmp/App.trace \
    --launch -- /path/to/App.app/Contents/MacOS/App
2) 导出时间采样数据

列出跟踪记录中可用的架构:

xcrun xctrace export --input /tmp/App.trace --toc

导出时间分析数据:

xcrun xctrace export \
    --input /tmp/App.trace \
    --xpath '/trace-toc/run/data/table[@schema="time-profile"]' \
    --output /tmp/time-profile.xml
3) 获取符号化所需的加载地址

应用运行时,获取 __TEXT 段的加载地址:

vmmap <pid> | grep "__TEXT"

查找加载地址(通常以 0x1... 开头)。

4) 符号化堆栈帧

使用 atos 对地址进行符号化:

atos -o /path/to/App.app/Contents/MacOS/App -l 0x100000000 <address>

工作流说明

  • 正确的二进制文件:确认分析的是正确的构建版本(本地版本还是 /Applications 中的版本)
  • 触发慢速路径:录制期间,执行运行缓慢的操作
  • 录制时长:如果堆栈为空,请延长录制时间或避免空闲时间
  • 符号匹配:二进制文件的符号必须与跟踪记录匹配(同一构建版本)

可用模板

列出所有性能分析模板:

xcrun xctrace list templates

常见模板:

  • Time Profiler - CPU 采样
  • Allocations - 内存分配
  • Leaks - 内存泄漏检测
  • System Trace - 系统级活动
  • Animation Hitches - UI 性能

常用命令

任务命令
列出模板xcrun xctrace list templates
列出设备xcrun xctrace list devices
查看录制帮助xcrun xctrace help record
查看导出帮助xcrun xctrace help export
获取 PIDpgrep -x "AppName"
获取加载地址vmmap <pid> | grep __TEXT
符号化atos -o <binary> -l <load-addr> <address>

分析结果

手动分析
  1. 将跟踪记录导出为 XML
  2. 解析调用树数据
  3. 查找采样数较高的帧
  4. 聚焦于应用自身的代码(过滤掉系统框架)
识别热点

查找:

  • 自身耗时较高的函数(在函数本身花费的时间)
  • 指示算法效率低下的深层调用栈
  • 暗示存在优化机会的重复模式

注意事项

  • ASLR:运行时 __TEXT 加载地址会在每次启动时变化——从 vmmap 获取
  • 构建版本不匹配:符号必须与被分析的确切构建版本匹配
  • 空闲时间:分析空闲的应用会产生空数据或无用数据
  • 权限:某些操作可能需要使用 sudo 运行

iOS 性能分析

对模拟器上的 iOS 应用进行分析:

xcrun xctrace record \
    --template 'Time Profiler' \
    --device <simulator-udid> \
    --time-limit 60s \
    --output /tmp/iOS-App.trace \
    --launch -- <bundle-id>

获取模拟器 UDID:

xcrun simctl list devices | grep Booted

自动化脚本

基本录制脚本:

#!/bin/bash
set -e

APP_NAME="$1"
DURATION="${2:-60}"
OUTPUT="${3:-/tmp/$APP_NAME.trace}"

if [ -z "$APP_NAME" ]; then
    echo "Usage: $0 <app-name> [duration-seconds] [output-path]"
    exit 1
fi

PID=$(pgrep -x "$APP_NAME" || true)

if [ -n "$PID" ]; then
    echo "Attaching to running $APP_NAME (PID: $PID)"
    xcrun xctrace record \
        --template 'Time Profiler' \
        --time-limit "${DURATION}s" \
        --output "$OUTPUT" \
        --attach "$PID"
else
    echo "App not running. Please start $APP_NAME first."
    exit 1
fi

echo "Trace saved to: $OUTPUT"
echo "To analyze: xcrun xctrace export --input $OUTPUT --toc"

检查清单

  • 已确认正确的二进制文件路径
  • 应用正在运行或已准备好启动命令
  • 可复现慢速路径
  • 已在出现问题的行为期间录制跟踪记录
  • 已捕获用于符号化的加载地址
  • 已分析结果并识别热点

评论

支持 Markdown