Instruments Profiling (macOS/iOS)
Use this skill when the user wants performance profiling or stack analysis for native apps.
Focus: Time Profiler, xctrace CLI, and picking the correct binary/app instance.
Quick Start (CLI)
- - List templates: INLINECODE1
- 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:
- INLINECODE4
Note: xcrun xctrace --help is not a valid subcommand. Use xcrun xctrace help record.
Picking the Correct Binary (Critical)
Gotcha: Instruments may profile the wrong app (e.g., one in /Applications) if LaunchServices resolves a different bundle.
Use these rules:
- - Prefer direct binary path for deterministic launch:
-
xcrun xctrace record ... --launch -- /path/App.app/Contents/MacOS/App
- - If launching
.app, ensure it’s the intended bundle:
-
open -n /path/App.app
- Verify with
ps -p <pid> -o comm= -o command=
- - If both
/Applications/App.app and a local build exist, explicitly target the local build path. - After launch, confirm the process path before trusting the trace.
Command Arguments (xctrace)
- -
--template 'Time Profiler': template name from xctrace list templates. - INLINECODE15 : everything after
-- is the target command (binary or app bundle). - INLINECODE17 : attach to running process.
- INLINECODE18 :
.trace output. If omitted, file saved in CWD. - INLINECODE20 : set capture duration.
- INLINECODE21 : required for iOS device runs.
- INLINECODE22 : stream launched process stdout to terminal (useful for CLI tools).
Exporting Stacks (CLI)
-
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.
Instruments UI Workflow
- - 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
Gotchas & Fixes
- - Wrong app profiled: LaunchServices resolves installed app instead of local build.
- Fix: use direct binary path or
--attach with known PID.
- - No samples / empty trace: App exits quickly or never hits work.
- Fix: longer capture, trigger workload during recording.
- - Privacy prompts:
xctrace may need Developer Tools permission.
- Fix: System Settings → Privacy & Security → Developer Tools → allow Terminal/Xcode.
- - Large XML exports:
time-profile exports are huge.
- Fix: filter with XPath and aggregate offline; don’t print to terminal.
iOS Specific Notes
- - Device: use
xcrun xctrace list devices and --device <UDID>. - Launch via Xcode if needed; attach with
xctrace --attach. - Ensure debug symbols for meaningful stacks.
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.
性能分析工具 (macOS/iOS)
当用户需要对原生应用进行性能分析或堆栈分析时使用此技能。
重点:时间分析器、xctrace 命令行工具,以及选择正确的二进制/应用实例。
快速入门 (命令行)
- - 列出模板: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
- open -a Instruments /tmp/App.trace
注意:xcrun xctrace --help 不是有效的子命令。请使用 xcrun xctrace help record。
选择正确的二进制文件(关键)
陷阱:如果LaunchServices解析了不同的bundle,Instruments可能会分析错误的应用程序(例如,/Applications中的那个)。
请遵循以下规则:
- xcrun xctrace record ... --launch -- /path/App.app/Contents/MacOS/App
- open -n /path/App.app
- 使用 ps -p -o comm= -o command= 验证
- - 如果同时存在 /Applications/App.app 和本地构建版本,明确指定本地构建路径。
- 启动后,在信任跟踪结果前确认进程路径。
命令参数 (xctrace)
- - --template Time Profiler:来自 xctrace list templates 的模板名称。
- --launch -- :-- 之后的所有内容都是目标命令(二进制文件或应用bundle)。
- --attach :附加到正在运行的进程。
- --output :.trace 输出文件。如果省略,文件保存在当前工作目录。
- --time-limit 60s|5m:设置捕获持续时间。
- --device :iOS设备运行必需。
- --target-stdout -:将启动进程的标准输出流式传输到终端(对命令行工具很有用)。
导出堆栈 (命令行)
- 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 工作流程
- - 模板:时间分析器
- 使用录制并捕获慢路径(启动 vs 稳态)
- 调用树提示:
- 隐藏系统库
- 反转调用树
- 按线程分离
- 关注热点帧和调用次数
陷阱与修复
- - 分析了错误的应用程序:LaunchServices解析已安装的应用而不是本地构建版本。
- 修复:使用直接二进制路径或使用已知PID的 --attach。
- - 没有样本/空跟踪:应用快速退出或从未执行工作负载。
- 修复:更长的捕获时间,在录制期间触发工作负载。
- - 隐私提示:xctrace 可能需要开发者工具权限。
- 修复:系统设置 → 隐私与安全性 → 开发者工具 → 允许终端/Xcode。
- - 大型XML导出:time-profile 导出文件很大。
- 修复:使用XPath过滤并离线聚合;不要打印到终端。
iOS 特定说明
- - 设备:使用 xcrun xctrace list devices 和 --device 。
- 如果需要,通过Xcode启动;使用 xctrace --attach 附加。
- 确保调试符号以获得有意义的堆栈。
验证检查清单
- - 确认跟踪进程路径与目标构建版本匹配。
- 确认堆栈显示预期的应用帧。
- 捕获覆盖慢操作(启动/刷新)。
- 导出堆栈以进行自动化差异比较(如果正在优化)。