instruments-profiling
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseInstruments Profiling (macOS/iOS)
Instruments Profiling (macOS/iOS)
Use this skill when the user wants performance profiling or stack analysis for native apps.
Focus: Time Profiler, CLI, and picking the correct binary/app instance.
xctrace当用户需要对原生应用进行性能分析或堆栈分析时,可使用本技能。
核心内容:Time Profiler、 CLI工具,以及如何选择正确的二进制文件/应用实例。
xctraceQuick Start (CLI)
快速入门(CLI)
- List templates:
xcrun xctrace list templates - Record Time Profiler (launch):
xcrun xctrace record --template 'Time Profiler' --time-limit 60s --output /tmp/App.trace --launch -- /path/To/App.app
- Record Time Profiler (attach):
- Launch app yourself, get PID, then:
xcrun xctrace record --template 'Time Profiler' --time-limit 60s --output /tmp/App.trace --attach <pid>
- Open trace in Instruments:
open -a Instruments /tmp/App.trace
Note: is not a valid subcommand. Use .
xcrun xctrace --helpxcrun xctrace help record- 列出模板:
xcrun xctrace list templates - 录制时间分析器(启动应用):
xcrun xctrace record --template 'Time Profiler' --time-limit 60s --output /tmp/App.trace --launch -- /path/To/App.app
- 录制时间分析器(附加到运行中进程):
- 自行启动应用,获取PID,然后执行:
xcrun xctrace record --template 'Time Profiler' --time-limit 60s --output /tmp/App.trace --attach <pid>
- 在Instruments中打开跟踪文件:
open -a Instruments /tmp/App.trace
注意:不是有效的子命令,请使用。
xcrun xctrace --helpxcrun xctrace help recordPicking the Correct Binary (Critical)
选择正确的二进制文件(至关重要)
Gotcha: Instruments may profile the wrong app (e.g., one in ) if LaunchServices resolves a different bundle.
Use these rules:
/Applications- Prefer direct binary path for deterministic launch:
xcrun xctrace record ... --launch -- /path/App.app/Contents/MacOS/App
- If launching , ensure it’s the intended bundle:
.appopen -n /path/App.app- Verify with
ps -p <pid> -o comm= -o command=
- If both and a local build exist, explicitly target the local build path.
/Applications/App.app - After launch, confirm the process path before trusting the trace.
常见问题:Instruments可能会分析错误的应用(例如,目录中的版本),如果LaunchServices解析到了不同的应用包。
请遵循以下规则:
/Applications- 优先使用直接的二进制文件路径以确保启动的确定性:
xcrun xctrace record ... --launch -- /path/App.app/Contents/MacOS/App
- 如果启动包,请确保它是目标包:
.appopen -n /path/App.app- 使用验证
ps -p <pid> -o comm= -o command=
- 如果同时存在和本地构建版本,请明确指定本地构建的路径。
/Applications/App.app - 启动应用后,在信任跟踪结果之前,请先确认进程路径。
Command Arguments (xctrace)
xctrace命令参数
- : template name from
--template 'Time Profiler'.xctrace list templates - : everything after
--launch -- <cmd>is the target command (binary or app bundle).-- - : attach to running process.
--attach <pid|name> - :
--output <path>output. If omitted, file saved in CWD..trace - : set capture duration.
--time-limit 60s|5m - : required for iOS device runs.
--device <name|UDID> - : stream launched process stdout to terminal (useful for CLI tools).
--target-stdout -
- :模板名称来自
--template 'Time Profiler'的输出。xctrace list templates - :
--launch -- <cmd>之后的所有内容都是目标命令(二进制文件或应用包)。-- - :附加到运行中的进程。
--attach <pid|name> - :
--output <path>格式的输出文件路径。如果省略,文件将保存在当前工作目录。.trace - :设置捕获时长。
--time-limit 60s|5m - :在iOS设备上运行时必须指定。
--device <name|UDID> - :将启动进程的标准输出流传输到终端(对CLI工具非常有用)。
--target-stdout -
Exporting Stacks (CLI)
导出堆栈信息(CLI)
- Inspect trace tables:
xcrun xctrace export --input /tmp/App.trace --toc
- Export raw time-profile samples:
xcrun xctrace export --input /tmp/App.trace --xpath '/trace-toc/run[@number="1"]/data/table[@schema="time-profile"]' --output /tmp/time-profile.xml
- Post-process in a script (Python/Rust) to aggregate stacks.
- 查看跟踪文件目录:
xcrun xctrace export --input /tmp/App.trace --toc
- 导出原始时间分析样本:
xcrun xctrace export --input /tmp/App.trace --xpath '/trace-toc/run[@number="1"]/data/table[@schema="time-profile"]' --output /tmp/time-profile.xml
- 使用脚本(Python/Rust)进行后处理以聚合堆栈信息。
Instruments UI Workflow
Instruments UI工作流
- Template: Time Profiler
- Use “Record” and capture the slow path (startup vs steady-state)
- Call Tree tips:
- Hide System Libraries
- Invert Call Tree
- Separate by Thread
- Focus on hot frames and call counts
- 模板:Time Profiler
- 点击“录制”并捕获慢路径(启动阶段 vs 稳定运行阶段)
- 调用树技巧:
- 隐藏系统库
- 反转调用树
- 按线程分离
- 重点关注热点帧和调用次数
Gotchas & Fixes
常见问题与解决方案
- Wrong app profiled: LaunchServices resolves installed app instead of local build.
- Fix: use direct binary path or with known PID.
--attach
- Fix: use direct binary path or
- No samples / empty trace: App exits quickly or never hits work.
- Fix: longer capture, trigger workload during recording.
- Privacy prompts: may need Developer Tools permission.
xctrace- Fix: System Settings → Privacy & Security → Developer Tools → allow Terminal/Xcode.
- Large XML exports: exports are huge.
time-profile- Fix: filter with XPath and aggregate offline; don’t print to terminal.
- 分析了错误的应用:LaunchServices解析到了已安装的应用而非本地构建版本。
- 解决方案:使用直接的二进制文件路径,或通过已知PID使用选项。
--attach
- 解决方案:使用直接的二进制文件路径,或通过已知PID使用
- 无样本/空跟踪文件:应用快速退出或未执行任何工作。
- 解决方案:延长捕获时长,在录制过程中触发工作负载。
- 隐私提示:可能需要开发者工具权限。
xctrace- 解决方案:系统设置 → 隐私与安全性 → 开发者工具 → 允许终端/Xcode访问。
- XML导出文件过大:导出的文件体积庞大。
time-profile- 解决方案:使用XPath过滤并离线聚合;不要直接打印到终端。
iOS Specific Notes
iOS专属注意事项
- Device: use and
xcrun xctrace list devices.--device <UDID> - Launch via Xcode if needed; attach with .
xctrace --attach - Ensure debug symbols for meaningful stacks.
- 设备:使用获取设备信息,并使用
xcrun xctrace list devices指定。--device <UDID> - 如有需要,可通过Xcode启动应用;使用附加到进程。
xctrace --attach - 确保存在调试符号以获得有意义的堆栈信息。
Verification Checklist
验证清单
- Confirm trace process path matches target build.
- Confirm stacks show expected app frames.
- Capture covers the slow operation (startup/refresh).
- Export stacks for automated diffing if optimizing.
- 确认跟踪文件中的进程路径与目标构建版本匹配。
- 确认堆栈信息显示了预期的应用帧。
- 捕获的内容覆盖了慢操作(启动/刷新)。
- 若进行优化,可导出堆栈信息以进行自动化对比。