|
| 1 | +/* |
| 2 | + * Copyright 2025 The Kubernetes Authors. |
| 3 | + * Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package main |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "fmt" |
| 23 | + "sync" |
| 24 | + |
| 25 | + "github.com/NVIDIA/go-nvml/pkg/nvml" |
| 26 | + "k8s.io/klog/v2" |
| 27 | +) |
| 28 | + |
| 29 | +type deviceHealthMonitor struct { |
| 30 | + nvdevlib *deviceLib |
| 31 | + allocatable AllocatableDevices |
| 32 | + eventSet nvml.EventSet |
| 33 | + unhealthy chan *AllocatableDevice |
| 34 | + stop chan struct{} |
| 35 | + wg sync.WaitGroup |
| 36 | +} |
| 37 | + |
| 38 | +func newDeviceHealthMonitor(ctx context.Context, config *Config) (*deviceHealthMonitor, error) { |
| 39 | + containerDriverRoot := root(config.flags.containerDriverRoot) |
| 40 | + nvdevlib, err := newDeviceLib(containerDriverRoot) |
| 41 | + if err != nil { |
| 42 | + return nil, fmt.Errorf("failed to create device library: %w", err) |
| 43 | + } |
| 44 | + |
| 45 | + if err := nvdevlib.Init(); err != nil { |
| 46 | + return nil, fmt.Errorf("failed to initialize NVML: %w", err) |
| 47 | + } |
| 48 | + //defer nvdevlib.alwaysShutdown() |
| 49 | + |
| 50 | + allocatable, err := nvdevlib.enumerateAllPossibleDevices(config) |
| 51 | + if err != nil { |
| 52 | + return nil, fmt.Errorf("error enumerating all possible devices: %w", err) |
| 53 | + } |
| 54 | + |
| 55 | + eventSet, err := nvdevlib.nvmllib.EventSetCreate() |
| 56 | + if err != nvml.SUCCESS { |
| 57 | + return nil, fmt.Errorf("failed to create event set: %w", err) |
| 58 | + } |
| 59 | + |
| 60 | + monitor := &deviceHealthMonitor{ |
| 61 | + nvdevlib: nvdevlib, |
| 62 | + allocatable: allocatable, |
| 63 | + eventSet: eventSet, |
| 64 | + unhealthy: make(chan *AllocatableDevice, len(allocatable)), |
| 65 | + stop: make(chan struct{}), |
| 66 | + } |
| 67 | + |
| 68 | + if err := monitor.registerDevicesForEvents(); err != nil { |
| 69 | + monitor.eventSet.Free() |
| 70 | + return nil, fmt.Errorf("failed to register devices for health monitoring: %w", err) |
| 71 | + } |
| 72 | + |
| 73 | + monitor.start() |
| 74 | + return monitor, nil |
| 75 | +} |
| 76 | + |
| 77 | +func (m *deviceHealthMonitor) registerDevicesForEvents() error { |
| 78 | + nvmllib := m.nvdevlib.nvmllib |
| 79 | + eventMask := uint64(nvml.EventTypeXidCriticalError | nvml.EventTypeDoubleBitEccError | nvml.EventTypeSingleBitEccError) |
| 80 | + |
| 81 | + for _, uuid := range m.allocatable.UUIDs() { |
| 82 | + gpu, err := nvmllib.DeviceGetHandleByUUID(uuid) |
| 83 | + if err != nvml.SUCCESS { |
| 84 | + klog.Infof("Unable to get NVML handle for UUID %s: %v; skipping health check for this device", uuid, err) |
| 85 | + continue |
| 86 | + } |
| 87 | + |
| 88 | + if err := gpu.RegisterEvents(eventMask, m.eventSet); err != nvml.SUCCESS { |
| 89 | + klog.Infof("Failed to register events for device %s: %v; skipping health check for this device", uuid, err) |
| 90 | + } |
| 91 | + } |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +func (m *deviceHealthMonitor) start() { |
| 96 | + m.wg.Add(1) |
| 97 | + go m.run() |
| 98 | +} |
| 99 | + |
| 100 | +func (m *deviceHealthMonitor) Stop() { |
| 101 | + if m == nil { |
| 102 | + return |
| 103 | + } |
| 104 | + close(m.stop) |
| 105 | + m.wg.Wait() |
| 106 | + close(m.unhealthy) |
| 107 | + m.eventSet.Free() |
| 108 | + |
| 109 | + if m.nvdevlib != nil { |
| 110 | + m.nvdevlib.alwaysShutdown() |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func (m *deviceHealthMonitor) run() { |
| 115 | + defer m.wg.Done() |
| 116 | + |
| 117 | + uuidToDeviceMap := make(map[string]*AllocatableDevice) |
| 118 | + for _, device := range m.allocatable { |
| 119 | + uuid := device.getUUID() |
| 120 | + if uuid != "" { |
| 121 | + uuidToDeviceMap[uuid] = device |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + klog.Info("Starting event-driven GPU health monitor...") |
| 126 | + |
| 127 | + for { |
| 128 | + select { |
| 129 | + case <-m.stop: |
| 130 | + klog.Info("Stopping event-driven GPU health monitor...") |
| 131 | + return |
| 132 | + default: |
| 133 | + event, err := m.eventSet.Wait(5000) |
| 134 | + if err == nvml.ERROR_TIMEOUT { |
| 135 | + continue |
| 136 | + } |
| 137 | + if err != nvml.SUCCESS { |
| 138 | + klog.Infof("Error waiting for event: %v; Marking all devices as unhealthy", err) |
| 139 | + for _, dev := range m.allocatable { |
| 140 | + m.unhealthy <- dev |
| 141 | + } |
| 142 | + continue |
| 143 | + } |
| 144 | + |
| 145 | + // Process health events |
| 146 | + switch event.EventType { |
| 147 | + case nvml.EventTypeXidCriticalError: |
| 148 | + klog.Warningf("Critical XID error detected on device") |
| 149 | + case nvml.EventTypeDoubleBitEccError: |
| 150 | + klog.Warningf("Double-bit ECC error detected on device") |
| 151 | + case nvml.EventTypeSingleBitEccError: |
| 152 | + klog.Infof("Single-bit ECC error detected on device") |
| 153 | + default: |
| 154 | + continue |
| 155 | + } |
| 156 | + |
| 157 | + eventUUID, err := event.Device.GetUUID() |
| 158 | + if err != nvml.SUCCESS { |
| 159 | + klog.Infof("Failed to determine uuid for event %v: %v; Marking all devices as unhealthy.", event, err) |
| 160 | + for _, dev := range m.allocatable { |
| 161 | + m.unhealthy <- dev |
| 162 | + } |
| 163 | + continue |
| 164 | + } |
| 165 | + |
| 166 | + device, exists := uuidToDeviceMap[eventUUID] |
| 167 | + if !exists { |
| 168 | + continue |
| 169 | + } |
| 170 | + |
| 171 | + // Send notification to driver |
| 172 | + klog.Infof("Sending unhealthy notification for device %s due to event type %v", eventUUID, event.EventType) |
| 173 | + select { |
| 174 | + case m.unhealthy <- device: |
| 175 | + // Successfully sent notification |
| 176 | + default: |
| 177 | + // Channel full, log and continue |
| 178 | + klog.Warningf("Health notification channel full, dropping event for device %s", eventUUID) |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +func (m *deviceHealthMonitor) Unhealthy() <-chan *AllocatableDevice { |
| 185 | + return m.unhealthy |
| 186 | +} |
| 187 | + |
| 188 | +func (d *AllocatableDevice) getUUID() string { |
| 189 | + if d.Gpu != nil { |
| 190 | + return d.Gpu.UUID |
| 191 | + } |
| 192 | + if d.Mig != nil { |
| 193 | + return d.Mig.UUID |
| 194 | + } |
| 195 | + return "" |
| 196 | +} |
0 commit comments