NVSentinel gpu-health-monitor模块之dcgm_watcher
dcgm.py 是 GPU Health Monitor 的核心:通过 DCGM 周期性检查 GPU/Switch 健康,并把结果异步通知回调(如 Platform Connector)。
整体定位
DCGMWatcher 负责:
- 连接 DCGM(远程服务或进程内 embedded hostengine)
- 创建包含所有 GPU + NVSwitch 的 monitoring group
- 按间隔跑 health check,可选评估热裕度
- 过滤需抑制的错误码
- 通过
CallbackInterface把结果发出去
数据流大致是:
markdown
DCGM ←→ DCGMWatcher.start() 轮询循环
↓
health_status + gpu_ids
↓
CallbackInterface(如 PlatformConnector)
模块组成
1. 连接方式:_create_dcgm_handle / _run_dcgm_server
两种 mode:
| Mode | 行为 |
|---|---|
local-managed |
进程内起 embedded DCGM,再把 hostengine 暴露到 loopback TCP |
默认 remote |
用 addr 连已有 DCGM(K8s DCGM Exporter/服务或本机地址) |
_run_dcgm_server 兼容 DCGM 4.x(dcgmServerRun)和 3.3.7(dcgmEngineRun)。
2. 能力发现
构造时会扫描 DCGM Python bindings:
_get_available_health_watches:所有DCGM_HEALTH_WATCH_*_get_available_error_codes:所有DCGM_FR_*错误码(会绕过 bindings 里已知的重复定义 bug)
用于把 DCGM 数值映射成可读的 watch / error 名称。
3. 健康检查:_perform_health_check
核心逻辑:
- 调用
dcgm_group.health.Check() - 遍历 incidents,按 watch + GPU 聚合错误信息(多条 message 用
;拼接) - 输出
dict[watch_name → HealthDetails] - timeout / 异常时标记
connectivity_success=False,触发重连
4. 可选热裕度监控:_evaluate_gpu_thermal_margin
若启用 thermal_margin_enabled 且 bindings 有 field 153(DCGM_FI_DEV_GPU_TEMP_TLIMIT):
- 订阅该 field
- 用
MetadataReader读每张卡的 HW slowdown T.Limit - 当
margin < slowdown_threshold时记为GPU_TEMP_HW_SLOWDOWN_VIOLATION
这是独立于标准 DCGM_HEALTH_WATCH_* 的自定义监视,通过 DCGM_FIELDS_MONITORING 注册表扩展。
5. 错误抑制:_suppress_configured_error_codes
对配置里 suppressed_error_codes 中的高频、不可操作事件:从 failures 里删掉;若该 watch 不再有 failure,状态改回 PASS。
6. 回调:_fire_callback_funcs
用线程池异步调用每个 callback,避免阻塞轮询;成功/失败记 Prometheus metrics。
接口在 types.py:
health_event_occurred(health_details, gpu_ids)dcgm_connectivity_failed()
7. 主循环:start
vbnet
while not exit:
wait(poll_interval)
if 无 handle:
连接 + 初始化 group / field watch
失败 → dcgm_connectivity_failed
else:
health check
连通失败 → 清理资源,置空 handle(下轮重连)
成功 → 热裕度评估 → 抑制错误 → health_event_occurred
finally:
cleanup + 关闭线程池
初始化失败会 rollback(unwatch / delete field group / delete group),避免 DCGM 服务端资源泄漏。
关键类型(types.py)
HealthStatus:PASS / WARN / FAILHealthDetails:某 watch 的整体状态 +entity_failures[gpu_id]ErrorDetails:错误码 + 消息DCGMFieldMonitor:自定义 field 监视配置CallbackInterface:下游消费方契约
一句话总结
这个文件把 DCGM 采数/健康检查 做成可重连的轮询 watcher,再通过回调把「哪些 GPU、哪些 watch 出了什么问题」交给上层(Platform Connector)去做事件上报与处置。