-
Notifications
You must be signed in to change notification settings - Fork 39
/
state.go
240 lines (201 loc) · 7.18 KB
/
state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package state
import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/NVIDIA/mig-parted/internal/nvlib"
"github.com/NVIDIA/mig-parted/internal/nvml"
"github.com/NVIDIA/mig-parted/pkg/mig/config"
"github.com/NVIDIA/mig-parted/pkg/mig/mode"
"github.com/NVIDIA/mig-parted/pkg/types"
)
// Manager represents the set of operations for fetching / restoring the full MIG state of all GPUs on a node.
type Manager interface {
Fetch() (*types.MigState, error)
RestoreMode(state *types.MigState) error
RestoreConfig(state *types.MigState) error
}
type migStateManager struct {
nvml nvml.Interface
nvlib nvlib.Interface
mode mode.Manager
config config.Manager
}
var _ Manager = (*migStateManager)(nil)
func tryNvmlShutdown(nvmlLib nvml.Interface) {
ret := nvmlLib.Shutdown()
if ret.Value() != nvml.SUCCESS {
log.Warnf("Error shutting down NVML: %v", ret)
}
}
// NewMigStateManager creates a new MIG state Manager.
func NewMigStateManager() Manager {
return &migStateManager{
nvml.New(),
nvlib.New(),
mode.NewNvmlMigModeManager(),
config.NewNvmlMigConfigManager(),
}
}
// NewMockMigStateManager creates a MIG state Manager using 'nvml' as the underlying NVML library to mock out its calls.
func NewMockMigStateManager(nvml nvml.Interface) Manager {
return &migStateManager{
nvml,
nvlib.NewMock(nvml),
mode.NewMockNvmlMigModeManager(nvml),
config.NewMockNvmlMigConfigManager(nvml),
}
}
// Fetch collects the full MIG state of all GPUs on a node and returns it in a 'MigState' struct.
func (m *migStateManager) Fetch() (*types.MigState, error) {
ret := m.nvml.Init()
if ret.Value() != nvml.SUCCESS {
return nil, fmt.Errorf("error initializing NVML: %v", ret)
}
defer tryNvmlShutdown(m.nvml)
numGPUs, ret := m.nvml.DeviceGetCount()
if ret.Value() != nvml.SUCCESS {
return nil, fmt.Errorf("error getting device count: %v", ret)
}
var migState types.MigState
for gpu := 0; gpu < numGPUs; gpu++ {
capable, err := m.mode.IsMigCapable(gpu)
if err != nil {
return nil, fmt.Errorf("error checking MIG capable: %v", err)
}
if !capable {
continue
}
device, ret := m.nvml.DeviceGetHandleByIndex(gpu)
if ret.Value() != nvml.SUCCESS {
return nil, fmt.Errorf("error getting device handle: %v", ret)
}
uuid, ret := device.GetUUID()
if ret.Value() != nvml.SUCCESS {
return nil, fmt.Errorf("error getting device uuid: %v", ret)
}
deviceState := types.DeviceState{
UUID: uuid,
}
deviceState.MigMode, err = m.mode.GetMigMode(gpu)
if err != nil {
return nil, fmt.Errorf("error getting MIG mode: %v", err)
}
if deviceState.MigMode == mode.Disabled {
migState.Devices = append(migState.Devices, deviceState)
continue
}
err = m.nvlib.Mig.Device(device).WalkGpuInstances(func(gi nvml.GpuInstance, giProfileID int, giProfileInfo nvml.GpuInstanceProfileInfo) error {
giInfo, ret := gi.GetInfo()
if ret.Value() != nvml.SUCCESS {
return fmt.Errorf("error getting GPU instance info for '%v': %v", giProfileID, ret)
}
giState := types.GpuInstanceState{
ProfileID: giProfileID,
Placement: giInfo.Placement,
}
err := m.nvlib.Mig.GpuInstance(gi).WalkComputeInstances(func(ci nvml.ComputeInstance, ciProfileID int, ciEngProfileID int, ciProfileInfo nvml.ComputeInstanceProfileInfo) error {
ciState := types.ComputeInstanceState{
ProfileID: ciProfileID,
EngProfileID: ciEngProfileID,
}
giState.ComputeInstances = append(giState.ComputeInstances, ciState)
return nil
})
if err != nil {
return fmt.Errorf("error walking compute instances for '%v': %v", giProfileID, err)
}
deviceState.GpuInstances = append(deviceState.GpuInstances, giState)
return nil
})
if err != nil {
return nil, fmt.Errorf("error walking gpu instances for '%v': %v", gpu, err)
}
migState.Devices = append(migState.Devices, deviceState)
}
return &migState, nil
}
// RestoreMode restores just the MIG mode state of all GPUs represented in the provided 'MigState'.
func (m *migStateManager) RestoreMode(state *types.MigState) error {
ret := m.nvml.Init()
if ret.Value() != nvml.SUCCESS {
return fmt.Errorf("error initializing NVML: %v", ret)
}
defer tryNvmlShutdown(m.nvml)
for _, deviceState := range state.Devices {
device, ret := m.nvml.DeviceGetHandleByUUID(deviceState.UUID)
if ret.Value() != nvml.SUCCESS {
return fmt.Errorf("error getting device handle: %v", ret)
}
index, ret := device.GetIndex()
if ret.Value() != nvml.SUCCESS {
return fmt.Errorf("error getting device index: %v", ret)
}
err := m.mode.SetMigMode(index, deviceState.MigMode)
if err != nil {
return fmt.Errorf("error setting MIG mode on device '%v': %v", deviceState.UUID, err)
}
}
return nil
}
// RestoreMode restores the full MIG configuration of all GPUs represented in the provided 'MigState'.
func (m *migStateManager) RestoreConfig(state *types.MigState) error {
ret := m.nvml.Init()
if ret.Value() != nvml.SUCCESS {
return fmt.Errorf("error initializing NVML: %v", ret)
}
defer tryNvmlShutdown(m.nvml)
for _, deviceState := range state.Devices {
if deviceState.MigMode == mode.Disabled {
continue
}
device, ret := m.nvml.DeviceGetHandleByUUID(deviceState.UUID)
if ret.Value() != nvml.SUCCESS {
return fmt.Errorf("error getting device handle: %v", ret)
}
index, ret := device.GetIndex()
if ret.Value() != nvml.SUCCESS {
return fmt.Errorf("error getting device index: %v", ret)
}
err := m.config.ClearMigConfig(index)
if err != nil {
return fmt.Errorf("error clearing existing MIG config: %v", err)
}
for _, giState := range deviceState.GpuInstances {
giProfileInfo, ret := device.GetGpuInstanceProfileInfo(giState.ProfileID)
if ret.Value() != nvml.SUCCESS {
return fmt.Errorf("error getting GPU instance profile info for '%v': %v", giState.ProfileID, ret)
}
placement := giState.Placement
gi, ret := device.CreateGpuInstanceWithPlacement(&giProfileInfo, &placement)
if ret.Value() != nvml.SUCCESS {
return fmt.Errorf("error creating GPU instance for '%v': %v", giState.ProfileID, ret)
}
for _, ciState := range giState.ComputeInstances {
ciProfileInfo, ret := gi.GetComputeInstanceProfileInfo(ciState.ProfileID, ciState.EngProfileID)
if ret.Value() != nvml.SUCCESS {
return fmt.Errorf("error getting Compute instance profile info for '(%v, %v)': %v", ciState.ProfileID, ciState.EngProfileID, ret)
}
_, ret = gi.CreateComputeInstance(&ciProfileInfo)
if ret.Value() != nvml.SUCCESS {
return fmt.Errorf("error creating Compute instance for '(%v, %v)': %v", ciState.ProfileID, ciState.EngProfileID, ret)
}
}
}
}
return nil
}