mcpField
M
macos-perf

macos-perf

github.com/alphaonedev开发50 次安装
代码仓库

关于 macos-perf

macos-perf 是用于监控和分析 macOS 性能的技能,适合诊断应用和设备的资源瓶颈。它使用 Instruments、vm_stat 和 powermetrics 等原生工具检查 CPU、内存、温度及电源数据。

主要功能

  1. 01监控 CPU、内存和网络使用情况
  2. 02使用 Instruments 分析应用
  3. 03通过 vm_stat 检查内存压力
  4. 04使用 powermetrics 查看温度指标
  5. 05分析电源消耗

适用场景

  1. 01排查应用 CPU 使用率过高
  2. 02分析应用内存以查找泄漏
  3. 03检查负载下的温度和电源表现

安装

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

$npx skills add https://github.com/alphaonedev/openclaw-graph --skill macos-perf

安装后在 Claude Code 中输入/macos-perf确认已加载。

详细介绍

macos-perf

用途

此技能可使用原生工具监控和分析 macOS 系统性能,重点关注 CPU、内存、热状态和电源指标,以识别瓶颈并优化应用。

适用场景

诊断应用的高 CPU 使用率、调查内存泄漏、分析资源密集型进程、检查高负载期间的热状态,或分析 macOS 设备的电源消耗时,可使用此技能。

主要功能

  • 通过“活动监视器”或 top/htop 监控 CPU、内存和网络使用情况等实时系统指标。
  • 使用 Instruments 分析应用,捕获 CPU、内存和能耗的详细跟踪记录。
  • 使用 vm_stat 检查内存压力,以检测内存不足的情况。
  • 通过 powermetrics 查询热状态,包括 CPU/GPU 温度和风扇转速。
  • 使用 powermetrics 分析电源指标,以衡量电池耗电量和能效。
  • 使用 top -o cpu 等命令行工具查看排序后的进程列表,或通过 instruments -s devices 查看可用模板。

使用模式

在脚本中调用此技能以实现自动监控,或将其集成到 AI 工作流中进行实时分析。例如,可在类似服务器的 macOS 环境中循环定期检查,或在应用超过资源阈值时触发性能分析。务必根据任务指定准确的工具和标志;例如,快速查看时使用 top,深入分析时使用 Instruments。如果集成到自动化流程中,请将数据导出为 JSON 以便解析。

常用命令/API

  • 使用 top -o cpu -s 1 按 CPU 使用率对进程排序并每秒更新;将输出传递给 grep 进行筛选,例如:top -o cpu | grep "MyApp"。
  • 使用 vm_stat | grep "Pages" 检查内存压力以获取空闲页面;示例代码:
    output = subprocess.run(['vm_stat'], capture_output=True).stdout
    free_pages = int(re.search(r'free:\s+(\d+)', output.decode()).group(1))
    
  • 运行 powermetrics --samplers cpu,thermal -i 1000,每 1 秒获取一次 CPU 和热状态数据。
  • 通过 CLI 启动 Instruments:instruments -w <device> -t Time Profiler -D output.trace /path/to/app;使用导出的 .trace 文件进行分析。
  • 对于热状态,可使用 ioreg -l | grep "Ambient" 从系统注册表中解析环境温度。
  • 这些工具无需 API 密钥,可在 macOS 上原生运行。

集成说明

可通过 Python 或 shell 脚本封装命令进行集成;为自定义路径设置环境变量,例如:export INSTRUMENTS_PATH=/Applications/Xcode.app/Contents/Developer/usr/bin/instruments。如需导出数据,可使用 plutil 将 .plist 输出转换为 JSON。若与其他技能组合使用,请确保 macOS 版本兼容(例如,Instruments 需要 Xcode);使用 sw_vers -productVersion 检查版本。避免同时运行多个高负载工具,以防资源争用。

错误处理

检查命令退出代码;例如,如果 top 失败,请使用 echo $? 验证,并通过记录错误或重试来处理。对于 Instruments,请解析 stderr 中的消息,例如 "Template not found",并回退到其他方案。在脚本中使用 try-except,例如:

try:
    result = subprocess.run(['instruments', '-t', 'Time Profiler'], check=True)
except subprocess.CalledProcessError as e:
    print(f"Error: {e.returncode} - {e.output}")

常见问题包括权限错误(使用 sudo 运行)或缺少依赖项(通过 xcode-select --install 安装 Xcode 命令行工具)。

具体使用示例

  1. 监控特定进程的 CPU 使用率:在循环脚本中运行 top -pid <processID> -o cpu,然后分析输出,并在使用率 > 80% 时发出警报;脚本示例行:while true; do top -pid 1234 -l 1 | grep CPU; sleep 5; done。
  2. 分析应用的内存:使用 instruments -t Allocations -D profile.trace /Applications/MyApp.app,然后在 Instruments GUI 中打开跟踪记录以识别泄漏;可通过脚本集成:instruments ... && open profile.trace。

关系图

  • 相关项:macos 集群(例如,用于基本系统操作的 macos-core)、performance 标签(关联通用性能分析技能)、profiling 标签(关联应用优化工具)。
  • 依赖项:需要 macos 集群技能提供基础访问;没有直接 API 链接。

评论

支持 Markdown