使用 py-spy 进行性能分析#
堆栈跟踪和 CPU 性能分析#
py-spy 是一个 Python 程序采样分析器。它允许您在不重新启动程序或以任何方式修改代码的情况下可视化 Python 程序的时间消耗。本节介绍如何配置 RayCluster YAML 文件以启用 py-spy,并在 Ray Dashboard 上查看堆栈跟踪和 CPU 火焰图。
先决条件#
py-spy 需要 SYS_PTRACE 能力来读取进程内存。然而,Kubernetes 默认会省略此能力。要启用性能分析,请在头 Pod 和工作 Pod 的 template.spec.containers 中添加以下内容。
securityContext:
capabilities:
add:
- SYS_PTRACE
注意
“
baseline” 和 “restricted” Pod 安全标准禁止添加SYS_PTRACE。有关更多详细信息,请参阅 Pod 安全标准。
在 Ray Dashboard 上检查 CPU 火焰图和堆栈跟踪#
步骤 1:创建 Kind 集群#
kind create cluster
步骤 2:安装 KubeRay operator#
请按照 本文档 使用 Helm 仓库安装最新的稳定版 KubeRay Operator。
步骤 3:创建具有 SYS_PTRACE 能力的 RayCluster#
kubectl apply -f https://raw.githubusercontent.com/ray-project/kuberay/master/ray-operator/config/samples/ray-cluster.py-spy.yaml
步骤 4:转发 dashboard 端口#
kubectl port-forward svc/raycluster-py-spy-head-svc 8265:8265
步骤 5:在头 Pod 中运行示例作业#
# Log in to the head Pod
kubectl exec -it ${YOUR_HEAD_POD} -- bash
# (Head Pod) Run a sample job in the Pod
# `long_running_task` includes a `while True` loop to ensure the task remains actively running indefinitely.
# This allows you ample time to view the Stack Trace and CPU Flame Graph via Ray Dashboard.
python3 samples/long_running_task.py
注意
如果您正在运行自己的示例,并在查看 CPU 火焰图时遇到
Failed to write flamegraph: I/O error: No stack counts found错误,这可能是因为进程处于空闲状态。值得注意的是,使用sleep函数可能导致此状态。在这种情况下,py-spy 会过滤掉空闲的堆栈跟踪。有关更多信息,请参阅此 问题。
步骤 6:使用 Ray Dashboard 进行性能分析#
访问 https://:8265/#/cluster。
为
ray::long_running_task点击“Stack Trace”。
为
ray::long_running_task点击“CPU Flame Graph”。
有关使用分析器的更多详细信息,请参阅 Dashboard 中的 Python CPU 性能分析。
步骤 7:清理#
kubectl delete -f https://raw.githubusercontent.com/ray-project/kuberay/master/ray-operator/config/samples/ray-cluster.py-spy.yaml
helm uninstall kuberay-operator