graphics.go 4.72 KB
Newer Older
songlinfeng's avatar
songlinfeng committed
1
2
3
4
5
6
7
/**
# Copyright (c) 2024, HCUOpt CORPORATION.  All rights reserved.
**/

package modifier

import (
8
9
10
11
12
13
14
15
	"dcu-container-toolkit/internal/config"
	"dcu-container-toolkit/internal/config/image"
	"dcu-container-toolkit/internal/dcu-tracker"
	"dcu-container-toolkit/internal/discover"
	"dcu-container-toolkit/internal/logger"
	"dcu-container-toolkit/internal/lookup"
	"dcu-container-toolkit/internal/lookup/root"
	"dcu-container-toolkit/internal/oci"
songlinfeng's avatar
songlinfeng committed
16
17
	"fmt"
	"path/filepath"
songlinfeng's avatar
songlinfeng committed
18
	"slices"
songlinfeng's avatar
songlinfeng committed
19
20
21
22
23
24
25
26
	"sort"
	"strconv"
)

// NewGraphicsModifier constructs a modifier that injects graphics-related modifications into an OCI runtime specification.
// The value of the DTK_DRIVER_CAPABILITIES environment variable is checked to determine if this modification should be made.
func NewGraphicsModifier(logger logger.Interface, cfg *config.Config, containerImage image.DTK, driver *root.Driver, isMount bool) (oci.SpecModifier, error) {
	dtkCDIHookPath := cfg.DTKCTKConfig.Path
songlinfeng's avatar
songlinfeng committed
27
28
	value := containerImage.Getenv(image.EnvVarDTKVisibleDevices)

songlinfeng's avatar
songlinfeng committed
29
	if len(value) > 0 {
songlinfeng's avatar
songlinfeng committed
30
31
32
33
34
35
36
		dcuTracker, err := dcuTracker.New()
		if err == nil {
			_, err = dcuTracker.ReserveDCUs(value, containerImage.ContainerId)
			logger.Infof("ReserveDCUs %s", value)
			if err != nil {
				return nil, fmt.Errorf("failed to reserve DCUs: %v", err)
			}
songlinfeng's avatar
songlinfeng committed
37
38
		}
	}
39

songlinfeng's avatar
songlinfeng committed
40
41
42
43
44
45
46
47
48
49
50
51
	value = containerImage.Getenv(image.EnvVarNvidiaVisibleDevices)

	if len(value) > 0 {
		dcuTracker, err := dcuTracker.New()
		if err == nil {
			_, err = dcuTracker.ReserveDCUs(value, containerImage.ContainerId)
			logger.Infof("ReserveDCUs %s", value)
			if err != nil {
				return nil, fmt.Errorf("failed to reserve DCUs: %v", err)
			}
		}
	}
songlinfeng's avatar
songlinfeng committed
52
53
54
55
56
	comDiscoverer, err := discover.NewCommonHCUDiscoverer(
		logger,
		dtkCDIHookPath,
		driver,
		isMount,
songlinfeng's avatar
songlinfeng committed
57
		containerImage,
songlinfeng's avatar
songlinfeng committed
58
59
60
61
62
63
	)
	if err != nil {
		return nil, fmt.Errorf("failed to create mounts discoverer: %v", err)
	}

	visibleDevices := containerImage.DevicesFromEnvvars(image.EnvVarDTKVisibleDevices, image.EnvVarNvidiaVisibleDevices)
songlinfeng's avatar
songlinfeng committed
64
65
66
	var vdcuDevices = containerImage.VdcuFromEnv(image.EnvVarVDTKVisibleDevices)

	if len(visibleDevices.List()) > 0 && len(vdcuDevices) > 0 {
songlinfeng's avatar
songlinfeng committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
		logger.Info("No devices requested")
		return nil, nil
	}

	busIds, err := getDevicesFromDriver()
	if err != nil {
		logger.Errorf("No hcu found")
		return nil, err
	}

	err = checkRequestDevices(logger, visibleDevices, busIds)
	if err != nil {
		return nil, err
	}

	var selectedBusIds []string
	for i, busId := range busIds {
		if visibleDevices.Has(fmt.Sprintf("%d", i)) || visibleDevices.Has(busId) {
			selectedBusIds = append(selectedBusIds, busId)
		}
	}

songlinfeng's avatar
songlinfeng committed
89
90
91
92
93
	for _, vdcuDevice := range vdcuDevices {
		if !slices.Contains(selectedBusIds, vdcuDevice) {
			selectedBusIds = append(selectedBusIds, vdcuDevice)
		}
	}
songlinfeng's avatar
songlinfeng committed
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
	// In standard usage, the devRoot is the same as the driver.Root.
	devRoot := driver.Root
	drmNodes, err := discover.NewDRMNodesDiscoverer(
		logger,
		busIds,
		selectedBusIds,
		devRoot,
	)
	if err != nil {
		return nil, fmt.Errorf("failed to construct discoverer: %v", err)
	}

	drmByPathLinks := discover.NewCreateDRMByPathSymlinks(logger, drmNodes, devRoot, dtkCDIHookPath)

	pciMounts := discover.NewPciMounts(
		logger,
		lookup.NewDirectoryLocator(
			lookup.WithLogger(logger),
			lookup.WithCount(1),
			lookup.WithSearchPaths("/sys/bus/pci/devices"),
		),
		driver.Root,
		selectedBusIds,
	)

	d := discover.Merge(
		comDiscoverer,
		drmNodes,
		drmByPathLinks,
		pciMounts,
	)
	return NewModifierFromDiscoverer(logger, d)
}

// getDevicesFromDriver query all HCU devices bus id
func getDevicesFromDriver() ([]string, error) {
	var devices []string

	matches, err := filepath.Glob("/sys/module/hy*cu/drivers/pci:hy*cu/[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]:*")
	if err != nil {
		return devices, fmt.Errorf("failed to find devices bus id: %v", err)
	}
songlinfeng's avatar
songlinfeng committed
136
	if len(matches) == 0 {
songlinfeng's avatar
songlinfeng committed
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
		m, err := filepath.Glob("/sys/module/hy*cu/drivers/pci:amdgpu/[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]:*")
		if err != nil {
			return devices, fmt.Errorf("failed to find devices bus id: %v", err)
		}
		matches = append(matches, m...)
	}

	for _, path := range sort.StringSlice(matches) {
		devices = append(devices, filepath.Base(path))
	}

	return devices, nil
}

func checkRequestDevices(logger logger.Interface, devices image.VisibleDevices, busIds []string) error {
	for _, device := range devices.List() {
		if device == "all" || device == "" {
			break
		}
		deviceId, err := strconv.Atoi(device)
		if err != nil {
			found := false
			for _, busId := range busIds {
				if device == busId {
					found = true
					break
				}
			}
			if !found {
				return fmt.Errorf("request device %s not found", device)
			}
		} else if deviceId >= len(busIds) {
			logger.Errorf("Request device %s is invalid", device)
			return fmt.Errorf("request device %s not found", device)
		}
	}

	return nil
}