-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
542 lines (453 loc) · 16.3 KB
/
server.go
File metadata and controls
542 lines (453 loc) · 16.3 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
// Copyright (C) 2025 Intel Corporation
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License version 2 or later, as published
// by the Free Software Foundation.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, see <http://www.gnu.org/licenses/>.
// SPDX-License-Identifier: GPL-2.0-or-later
package main
import (
"context"
"fmt"
"log/slog"
"net"
"os"
"path"
"slices"
"time"
"google.golang.org/grpc"
hlml "github.com/HabanaAI/gohlml"
pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
)
// HabanalabsDevicePlugin implements the Kubernetes device plugin API
type HabanalabsDevicePlugin struct {
pluginapi.UnimplementedDevicePluginServer
ResourceManager
log *slog.Logger
stop chan interface{}
healthy chan *pluginapi.Device
unhealthy chan *pluginapi.Device
server *grpc.Server
resourceName string
socket string
devs []*pluginapi.Device
unhealthyDevs map[string]struct{}
useCdi bool
hookPath string
}
// GetPreferredAllocation returns a preferred set of devices to allocate
// from a list of available ones. The resulting preferred allocation is not
// guaranteed to be the allocation ultimately performed by the
// devicemanager. It is only designed to help the devicemanager make a more
// informed allocation decision when possible.
func (m *HabanalabsDevicePlugin) GetPreferredAllocation(ctx context.Context, request *pluginapi.PreferredAllocationRequest) (*pluginapi.PreferredAllocationResponse, error) {
response := &pluginapi.PreferredAllocationResponse{}
for _, req := range request.ContainerRequests {
m.log.Info("GetPreferredAllocation called", "available_count", len(req.AvailableDeviceIDs), "requested_count", int(req.AllocationSize))
if req.AllocationSize <= 0 {
return nil, fmt.Errorf("invalid allocation size: %d, must be positive", req.AllocationSize)
}
// Get all available devices
availableDevices := make([]*pluginapi.Device, 0, len(req.AvailableDeviceIDs))
for _, id := range req.AvailableDeviceIDs {
device := getDevice(m.devs, id)
if device != nil {
availableDevices = append(availableDevices, device)
}
}
if len(availableDevices) < int(req.AllocationSize) {
m.log.Error("Not enough available devices", "requested", req.AllocationSize, "available", len(availableDevices))
return nil, fmt.Errorf("not enough available devices: requested %d, available %d",
req.AllocationSize, len(availableDevices))
}
// Group devices by NUMA node
numaDevices := groupDevicesByNuma(availableDevices)
m.log.Info("Devices grouped by NUMA", "numa_groups", len(numaDevices))
// Calculate total devices per NUMA node
totalDevicesPerNuma := make(map[int64]int)
allDevicesByNuma := groupDevicesByNuma(m.devs)
for numaID, devices := range allDevicesByNuma {
totalDevicesPerNuma[numaID] = len(devices)
}
// Find NUMA nodes that already have allocations (available devices < total devices)
var partiallyAllocatedNumas []int64
for numaID, devices := range numaDevices {
if len(devices) < totalDevicesPerNuma[numaID] {
partiallyAllocatedNumas = append(partiallyAllocatedNumas, numaID)
}
}
m.log.Info("Partially allocated NUMA nodes", "numas", partiallyAllocatedNumas)
var preferredDevices []string
// For 1, 2, or 4 cards, try to allocate from the same NUMA node
if req.AllocationSize <= 4 {
// First try to allocate from already partially allocated NUMA nodes
if len(partiallyAllocatedNumas) > 0 {
bestSuitableNuma := int64(-1)
for _, numaID := range partiallyAllocatedNumas {
devices := numaDevices[numaID]
if len(devices) >= int(req.AllocationSize) {
if bestSuitableNuma == -1 || len(devices) < len(numaDevices[bestSuitableNuma]) {
bestSuitableNuma = numaID
}
}
}
if bestSuitableNuma == -1 {
m.log.Info("No partially allocated NUMA node has enough devices")
} else {
devices := numaDevices[bestSuitableNuma]
m.log.Info("Allocating from partially allocated NUMA node", "numa_id", bestSuitableNuma,
"available", len(devices), "requested", req.AllocationSize)
for i := 0; i < int(req.AllocationSize); i++ {
preferredDevices = append(preferredDevices, devices[i].ID)
}
}
}
// If no partially allocated NUMA node has enough devices, try unallocated NUMA nodes
if len(preferredDevices) != int(req.AllocationSize) {
for numaID, devices := range numaDevices {
// Skip already tried partially allocated NUMA nodes
alreadyTried := false
for _, allocatedNuma := range partiallyAllocatedNumas {
if numaID == allocatedNuma {
alreadyTried = true
break
}
}
if alreadyTried {
continue
}
if len(devices) >= int(req.AllocationSize) {
m.log.Info("Found NUMA node with enough devices", "numa_id", numaID,
"available", len(devices), "requested", req.AllocationSize)
// Allocate the requested number of devices
for i := 0; i < int(req.AllocationSize); i++ {
preferredDevices = append(preferredDevices, devices[i].ID)
}
break
}
}
}
// If still couldn't find a suitable NUMA node, fall back to cross-NUMA allocation
if len(preferredDevices) != int(req.AllocationSize) {
m.log.Warn("Could not allocate requested devices from a single NUMA node, using cross-NUMA allocation",
"requested", req.AllocationSize)
}
}
// For more than 4 cards or if same-NUMA allocation failed, allocate across NUMA nodes
if len(preferredDevices) != int(req.AllocationSize) {
m.log.Info("Allocating devices across NUMA nodes", "requested", req.AllocationSize)
preferredDevices = allocateAcrossNuma(numaDevices, int(req.AllocationSize))
}
// Final check if we have enough devices
if len(preferredDevices) != int(req.AllocationSize) {
m.log.Error("Failed to allocate requested number of devices",
"requested", req.AllocationSize, "allocated", len(preferredDevices))
return nil, fmt.Errorf("could not allocate requested number of devices: requested %d, allocated %d",
req.AllocationSize, len(preferredDevices))
}
m.log.Info("Preferred allocation", "devices", preferredDevices)
resp := &pluginapi.ContainerPreferredAllocationResponse{
DeviceIDs: preferredDevices,
}
response.ContainerResponses = append(response.ContainerResponses, resp)
}
return response, nil
}
// allocateAcrossNuma allocates devices across NUMA nodes
func allocateAcrossNuma(numaDevices map[int64][]*pluginapi.Device, count int) []string {
result := make([]string, 0, count)
// Sort NUMA nodes by number of devices (descending)
type numaGroup struct {
numaID int64
devices []*pluginapi.Device
}
numaGroups := make([]numaGroup, 0, len(numaDevices))
for numaID, devices := range numaDevices {
numaGroups = append(numaGroups, numaGroup{numaID, devices})
}
// Sort by number of devices (descending)
slices.SortFunc(numaGroups, func(a, b numaGroup) int {
return len(b.devices) - len(a.devices)
})
// Allocate devices from NUMA nodes with the most devices first
remaining := count
for _, group := range numaGroups {
numToAllocate := min(remaining, len(group.devices))
for i := 0; i < numToAllocate; i++ {
result = append(result, group.devices[i].ID)
}
remaining -= numToAllocate
if remaining == 0 {
break
}
}
return result
}
// groupDevicesByNuma groups devices by their NUMA node
func groupDevicesByNuma(devices []*pluginapi.Device) map[int64][]*pluginapi.Device {
numaDevices := make(map[int64][]*pluginapi.Device)
for _, device := range devices {
var numaID int64 = -1
if device.Topology != nil && len(device.Topology.Nodes) > 0 {
numaID = device.Topology.Nodes[0].ID
}
numaDevices[numaID] = append(numaDevices[numaID], device)
}
return numaDevices
}
// NewHabanalabsDevicePlugin returns an initialized HabanalabsDevicePlugin.
func NewHabanalabsDevicePlugin(log *slog.Logger, resourceManager ResourceManager, resourceName string, socket string, useCdi bool, hookPath string) *HabanalabsDevicePlugin {
return &HabanalabsDevicePlugin{
log: log,
ResourceManager: resourceManager,
resourceName: resourceName,
socket: socket,
stop: make(chan interface{}),
healthy: make(chan *pluginapi.Device),
unhealthy: make(chan *pluginapi.Device),
unhealthyDevs: make(map[string]struct{}),
// will be initialized on every server restart.
devs: nil,
useCdi: useCdi,
hookPath: hookPath,
}
}
// GetDevicePluginOptions returns the device plugin options.
func (m *HabanalabsDevicePlugin) GetDevicePluginOptions(context.Context, *pluginapi.Empty) (*pluginapi.DevicePluginOptions, error) {
return &pluginapi.DevicePluginOptions{
GetPreferredAllocationAvailable: true, // Indicate to kubelet we have an implementation.
}, nil
}
// dial establishes the gRPC communication with the registered device plugin.
func dial(unixSocketPath string, timeout time.Duration) (*grpc.ClientConn, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
c, err := grpc.DialContext(ctx, unixSocketPath,
grpc.WithInsecure(),
grpc.WithContextDialer(func(ctx context.Context, s string) (net.Conn, error) {
return net.DialTimeout("unix", s, timeout)
}),
)
if err != nil {
return nil, err
}
return c, nil
}
// Start starts the gRPC server of the device plugin
func (m *HabanalabsDevicePlugin) Start() error {
err := m.cleanup()
if err != nil {
return err
}
if m.stop == nil {
m.stop = make(chan interface{})
}
// initialize Devices
m.devs, err = m.Devices()
if err != nil {
return err
}
sock, err := net.Listen("unix", m.socket)
if err != nil {
return err
}
// First start serving the gRPC connection before registering.
// It is required since kubernetes 1.26. Change is backward compatible.
m.server = grpc.NewServer([]grpc.ServerOption{}...)
pluginapi.RegisterDevicePluginServer(m.server, m)
// Ignore error returns since the next block will fail if Serve fails.
go func() {
err := m.server.Serve(sock)
if err != nil {
m.log.Error("Failed to start gRPC server", "error", err)
}
}()
// Wait for server to start by launching a blocking connection
conn, err := dial(m.socket, 5*time.Second)
if err != nil {
return err
}
err = conn.Close()
if err != nil {
return fmt.Errorf("failed to close connection to gRPC server: %w", err)
}
go m.healthcheck()
// Remove any pre-existing Gaudi CDI specs
if m.useCdi {
m.cdiSpecCleanup()
}
return nil
}
// Stop gRPC server
func (m *HabanalabsDevicePlugin) Stop() error {
if m.server == nil {
return nil
}
m.log.Info("Stoppping device plugin", "resource_name", m.resourceName, "socket", m.socket)
m.server.Stop()
m.server = nil
close(m.stop)
m.stop = nil
return m.cleanup()
}
// Register registers the device plugin for the given resourceName with Kubelet.
func (m *HabanalabsDevicePlugin) Register() error {
conn, err := dial(pluginapi.KubeletSocket, 5*time.Second)
if err != nil {
return err
}
defer conn.Close()
client := pluginapi.NewRegistrationClient(conn)
reqt := &pluginapi.RegisterRequest{
Version: pluginapi.Version,
Endpoint: path.Base(m.socket),
ResourceName: m.resourceName,
}
_, err = client.Register(context.Background(), reqt)
if err != nil {
return err
}
return nil
}
// ListAndWatch lists devices and update that list according to the health status
func (m *HabanalabsDevicePlugin) ListAndWatch(e *pluginapi.Empty, s pluginapi.DevicePlugin_ListAndWatchServer) error {
err := s.Send(&pluginapi.ListAndWatchResponse{Devices: m.devs})
if err != nil {
return err
}
for {
select {
case <-m.stop:
return nil
case d := <-m.unhealthy:
d.Health = pluginapi.Unhealthy
m.log.Info("Device is unhealthy", "resource", m.resourceName, "id", d.ID)
if err := s.Send(&pluginapi.ListAndWatchResponse{Devices: m.devs}); err != nil {
m.log.Error("Failed sending ListAndWatch to kubelet", "error", err)
}
case d := <-m.healthy:
d.Health = pluginapi.Healthy
m.log.Info("Device is healthy", "resource", m.resourceName, "id", d.ID)
if err := s.Send(&pluginapi.ListAndWatchResponse{Devices: m.devs}); err != nil {
m.log.Error("Failed sending ListAndWatch to kubelet", "error", err)
}
}
}
}
func (m *HabanalabsDevicePlugin) setUnhealthy(dev *pluginapi.Device) {
m.unhealthyDevs[dev.ID] = struct{}{}
m.unhealthy <- dev
}
func (m *HabanalabsDevicePlugin) setHealthy(dev *pluginapi.Device) {
delete(m.unhealthyDevs, dev.ID)
m.healthy <- dev
}
// Allocate which return list of devices.
func (m *HabanalabsDevicePlugin) Allocate(ctx context.Context, reqs *pluginapi.AllocateRequest) (*pluginapi.AllocateResponse, error) {
devs := m.devs
response := pluginapi.AllocateResponse{ContainerResponses: []*pluginapi.ContainerAllocateResponse{}}
for _, req := range reqs.ContainerRequests {
devEntitys := make([]deviceEntity, 0, len(req.DevicesIds))
for _, id := range req.DevicesIds {
device := getDevice(devs, id)
if device == nil {
return nil, fmt.Errorf("invalid request for %q: device unknown: %s", m.resourceName, id)
}
m.log.Info("Preparing device for registration", "device", device)
m.log.Info("Getting device handle from hlml")
deviceHandle, err := hlml.DeviceHandleBySerial(id)
if err != nil {
m.log.Error(err.Error())
return nil, err
}
m.log.Info("Getting device minor number")
minor, err := deviceHandle.MinorNumber()
if err != nil {
m.log.Error(err.Error())
return nil, err
}
m.log.Info("Getting device module id")
moduleID, err := deviceHandle.ModuleID()
if err != nil {
m.log.Error(err.Error())
return nil, err
}
// Gather devices here and convert them to normal DP devicespecs or
// CDI device names after the loop.
devEntity := deviceEntity{
moduleId: moduleID,
deviceId: minor,
uuid: id,
devicePaths: []string{
fmt.Sprintf("/dev/accel/accel%d", minor),
fmt.Sprintf("/dev/accel/accel_controlD%d", minor),
},
}
// DP's DeviceSpec has not included uverbs, but CDI will.
if m.useCdi {
uverbDevPath := m.uverbsForAccelerator(minor)
if uverbDevPath != "" {
devEntity.devicePaths = append(devEntity.devicePaths, uverbDevPath)
}
}
devEntitys = append(devEntitys, devEntity)
}
subset := bool(len(devEntitys) < len(m.devs))
envMap := envsFromDevices(devEntitys, subset)
// Depending on the mode, return CDI device names or DeviceSpecs.
if m.useCdi {
response.ContainerResponses = append(response.ContainerResponses, &pluginapi.ContainerAllocateResponse{
CdiDevices: m.writeCdiSpecAndReturnDevices(devEntitys, envMap),
})
} else {
response.ContainerResponses = append(response.ContainerResponses, &pluginapi.ContainerAllocateResponse{
Devices: m.createDeviceSpecs(devEntitys),
Envs: envMap,
})
}
}
return &response, nil
}
// PreStartContainer performs actions before the container start
func (m *HabanalabsDevicePlugin) PreStartContainer(context.Context, *pluginapi.PreStartContainerRequest) (*pluginapi.PreStartContainerResponse, error) {
return &pluginapi.PreStartContainerResponse{}, nil
}
func (m *HabanalabsDevicePlugin) cleanup() error {
if err := os.Remove(m.socket); err != nil && !os.IsNotExist(err) {
return err
}
// Remove CDI specs that were created during the life cycle of the plugin
if m.useCdi {
m.cdiSpecCleanup()
}
return nil
}
// TODO: pass context from the main app context
func (m *HabanalabsDevicePlugin) healthcheck() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go m.watchXIDs(ctx, m.devs)
<-m.stop
}
// Serve starts the gRPC server and register the device plugin to Kubelet
func (m *HabanalabsDevicePlugin) Serve() error {
err := m.Start()
if err != nil {
return fmt.Errorf("could not start device plugln: %w", err)
}
m.log.Info("Starting to serve", "socket", m.socket)
err = m.Register()
if err != nil {
err = m.Stop()
if err != nil {
m.log.Error("Failed to stop device plugin after failed registration", "error", err)
}
return fmt.Errorf("could not register device plugin: %w", err)
}
m.log.Info("Registered device plugin with Kubelet")
return nil
}