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

package modifier

import (
	"dtk-container-toolkit/internal/config"
	"dtk-container-toolkit/internal/config/image"
songlinfeng's avatar
songlinfeng committed
10
	"dtk-container-toolkit/internal/dcu-tracker"
songlinfeng's avatar
songlinfeng committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
	"dtk-container-toolkit/internal/discover"
	"dtk-container-toolkit/internal/logger"
	"dtk-container-toolkit/internal/lookup"
	"dtk-container-toolkit/internal/lookup/root"
	"dtk-container-toolkit/internal/oci"
	"fmt"
	"path/filepath"
	"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
26
27
28
29
30
31
32
33
34
35
36
        value := containerImage.Getenv(image.EnvVarDTKVisibleDevices)
	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
37
38
39
40
41
	comDiscoverer, err := discover.NewCommonHCUDiscoverer(
		logger,
		dtkCDIHookPath,
		driver,
		isMount,
songlinfeng's avatar
songlinfeng committed
42
		containerImage,
songlinfeng's avatar
songlinfeng committed
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
	)
	if err != nil {
		return nil, fmt.Errorf("failed to create mounts discoverer: %v", err)
	}

	visibleDevices := containerImage.DevicesFromEnvvars(image.EnvVarDTKVisibleDevices, image.EnvVarNvidiaVisibleDevices)
	if len(visibleDevices.List()) == 0 {
		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)
		}
	}

	// 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)
	}
        if len(matches) == 0 {
		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
}