|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path" |
| 8 | + "slices" |
| 9 | + "strings" |
| 10 | + |
| 11 | + log "github.com/sirupsen/logrus" |
| 12 | + "github.com/urfave/cli/v2" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + // LogFile is the path for logging |
| 17 | + LogFile = "/var/log/gpu-driver-util.log" |
| 18 | + // PCIDevicesRoot represents base path for all pci devices under sysfs |
| 19 | + PCIDevicesRoot = "/sys/bus/pci/devices" |
| 20 | + // NVIDIAVendorID represents the NVIDIA PCI vendor id |
| 21 | + NVIDIAVendorID = "0x10de" |
| 22 | + // PCIDeviceClassVGA represents the pci device class code for VGA devices |
| 23 | + PCIDeviceClassVGA = "0x030000" |
| 24 | + // PCIDeviceClassGPU represents the pci device class code for GPU devices |
| 25 | + PCIDeviceClassGPU = "0x030200" |
| 26 | + // DefaultSupportedGpusJsonPath represents the default install location of the supported-gpus.json file |
| 27 | + DefaultSupportedGpusJsonPath = "/usr/share/nvidia-driver-assistant/supported-gpus/supported-gpus.json" |
| 28 | + |
| 29 | + // DriverHintUnknown is used when the gpu device is not found in supported-gpus.json |
| 30 | + DriverHintUnknown = "unknown" |
| 31 | + // DriverHintOpenRequired is used when the gpu device compulsorily needs the OpenRM kernel modules |
| 32 | + DriverHintOpenRequired = "open-required" |
| 33 | + // DriverHintProprietaryRequired is used when the gpu device compulsorily needs the proprietary kernel modules |
| 34 | + DriverHintProprietaryRequired = "proprietary-required" |
| 35 | + // DriverHintAny is used when the gpu device can support either kernel module types |
| 36 | + DriverHintAny = "any-supported" |
| 37 | + |
| 38 | + // DriverFeatureKernelOpen indicates that the gpu device supports OpenRM |
| 39 | + DriverFeatureKernelOpen = "kernelopen" |
| 40 | + // DriverFeatureKernelGSPProprietary indicates that the gpu device has GSP RM and supports proprietary modules |
| 41 | + DriverFeatureKernelGSPProprietary = "gsp_proprietary_supported" |
| 42 | + |
| 43 | + // KernelModuleTypeOpen indicates the OpenRM Kernel Modules of the NVIDIA CUDA driver |
| 44 | + KernelModuleTypeOpen = "kernel-open" |
| 45 | + // KernelModuleTypeProprietary indicates the Closed/Proprietary Kernel Modules of the NVIDIA CUDA driver |
| 46 | + KernelModuleTypeProprietary = "kernel" |
| 47 | +) |
| 48 | + |
| 49 | +var ( |
| 50 | + supportedGpusJsonPath string |
| 51 | + driverBranch int |
| 52 | +) |
| 53 | + |
| 54 | +type GPUDevice struct { |
| 55 | + ID string `json:"devid"` |
| 56 | + Name string `json:"name"` |
| 57 | + LegacyBranch string `json:"legacybranch"` |
| 58 | + Features []string `json:"features"` |
| 59 | +} |
| 60 | + |
| 61 | +type GPUData struct { |
| 62 | + Chips []GPUDevice `json:"chips"` |
| 63 | +} |
| 64 | + |
| 65 | +func main() { |
| 66 | + logFile, err := initializeLogger() |
| 67 | + if err != nil { |
| 68 | + log.Fatal(err.Error()) |
| 69 | + } |
| 70 | + defer logFile.Close() |
| 71 | + |
| 72 | + // Create the top-level CLI app |
| 73 | + c := cli.NewApp() |
| 74 | + c.Name = "gpu-driver-util" |
| 75 | + c.Usage = "NVIDIA GPU Driver Utility Application" |
| 76 | + c.Version = "0.1.0" |
| 77 | + |
| 78 | + getKernelModule := cli.Command{} |
| 79 | + getKernelModule.Name = "get-kernel-module-type" |
| 80 | + getKernelModule.Usage = "Automatically determine the kernel module type based on the GPUs detected." |
| 81 | + getKernelModule.Action = func(c *cli.Context) error { |
| 82 | + return GetKernelModule(c) |
| 83 | + } |
| 84 | + |
| 85 | + getKernelModuleFlags := []cli.Flag{ |
| 86 | + &cli.StringFlag{ |
| 87 | + Name: "supported-gpus-file", |
| 88 | + Aliases: []string{"f"}, |
| 89 | + Usage: "Specify location of the supported-gpus.json file", |
| 90 | + Value: DefaultSupportedGpusJsonPath, |
| 91 | + Destination: &supportedGpusJsonPath, |
| 92 | + Required: false, |
| 93 | + }, |
| 94 | + &cli.IntFlag{ |
| 95 | + Name: "driver-branch", |
| 96 | + Aliases: []string{"b"}, |
| 97 | + Usage: "Specify driver branch", |
| 98 | + EnvVars: []string{"DRIVER_BRANCH"}, |
| 99 | + Destination: &driverBranch, |
| 100 | + Required: true, |
| 101 | + }, |
| 102 | + } |
| 103 | + |
| 104 | + c.Commands = []*cli.Command{ |
| 105 | + &getKernelModule, |
| 106 | + } |
| 107 | + |
| 108 | + getKernelModule.Flags = append([]cli.Flag{}, getKernelModuleFlags...) |
| 109 | + |
| 110 | + // Run the top-level CLI |
| 111 | + if err := c.Run(os.Args); err != nil { |
| 112 | + log.Fatal(fmt.Errorf("error running gpu-driver-util: %w", err)) |
| 113 | + } |
| 114 | + |
| 115 | +} |
| 116 | + |
| 117 | +func initializeLogger() (*os.File, error) { |
| 118 | + logFile, err := os.OpenFile(LogFile, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666) |
| 119 | + if err != nil { |
| 120 | + return nil, fmt.Errorf("error opening file %s: %w", LogFile, err) |
| 121 | + } |
| 122 | + // Log as JSON instead of the default ASCII formatter. |
| 123 | + log.SetFormatter(&log.JSONFormatter{}) |
| 124 | + // Output to file instead of stdout |
| 125 | + log.SetOutput(logFile) |
| 126 | + |
| 127 | + // Only log the warning severity or above. |
| 128 | + log.SetLevel(log.DebugLevel) |
| 129 | + return logFile, nil |
| 130 | +} |
| 131 | + |
| 132 | +func GetKernelModule(c *cli.Context) error { |
| 133 | + log.Infof("Starting the 'get-kernel-module' command of %s", c.App.Name) |
| 134 | + gpuDevices, err := getNvidiaGPUs() |
| 135 | + |
| 136 | + log.Debugf("NVIDIA GPU devices found: %v", gpuDevices) |
| 137 | + |
| 138 | + if err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + |
| 142 | + var gpuData GPUData |
| 143 | + gpuJSONString, err := os.ReadFile(supportedGpusJsonPath) |
| 144 | + if err != nil { |
| 145 | + log.Errorf("error opening the supported gpus file %s: %v", supportedGpusJsonPath, err) |
| 146 | + return err |
| 147 | + } |
| 148 | + |
| 149 | + err = json.Unmarshal(gpuJSONString, &gpuData) |
| 150 | + if err != nil { |
| 151 | + return err |
| 152 | + } |
| 153 | + |
| 154 | + searchMap := buildGPUSearchMap(gpuData) |
| 155 | + |
| 156 | + if len(gpuDevices) > 0 { |
| 157 | + kernelModuleType, err := resolveKernelModuleType(gpuDevices, searchMap) |
| 158 | + if err != nil { |
| 159 | + log.Errorf("error resolving kernel module type: %v", err) |
| 160 | + return err |
| 161 | + } |
| 162 | + fmt.Println(kernelModuleType) |
| 163 | + } |
| 164 | + return nil |
| 165 | +} |
| 166 | + |
| 167 | +func resolveKernelModuleType(gpuDevices []string, searchMap map[string]GPUDevice) (string, error) { |
| 168 | + |
| 169 | + driverHints := getDriverHints(gpuDevices, searchMap) |
| 170 | + log.Debugf("driverHints: %v", driverHints) |
| 171 | + |
| 172 | + // NOTE: driver hint "unknown" is assigned to a device that does not have an entry in supported-gpus.json. |
| 173 | + // In these cases, we assume that the gpu device is new and unreleased, and we default to OpenRM |
| 174 | + requiresOpenRM := slices.Contains(driverHints, DriverHintOpenRequired) || slices.Contains(driverHints, DriverHintUnknown) |
| 175 | + requiresProprietary := slices.Contains(driverHints, DriverHintProprietaryRequired) |
| 176 | + |
| 177 | + if requiresOpenRM && requiresProprietary { |
| 178 | + return "", fmt.Errorf("unsupported GPU topology") |
| 179 | + } else if requiresOpenRM { |
| 180 | + return KernelModuleTypeOpen, nil |
| 181 | + } else if requiresProprietary { |
| 182 | + return KernelModuleTypeProprietary, nil |
| 183 | + } else { |
| 184 | + return getDriverBranchDefault(driverBranch), nil |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +func getDriverHints(gpuDevices []string, searchMap map[string]GPUDevice) []string { |
| 189 | + var driverHints []string |
| 190 | + |
| 191 | + for _, gpuDevice := range gpuDevices { |
| 192 | + if val, ok := searchMap[gpuDevice]; ok { |
| 193 | + gpuFeatures := val.Features |
| 194 | + if slices.Contains(gpuFeatures, DriverFeatureKernelGSPProprietary) && |
| 195 | + slices.Contains(gpuFeatures, DriverFeatureKernelOpen) { |
| 196 | + driverHints = append(driverHints, DriverHintAny) |
| 197 | + } else if slices.Contains(gpuFeatures, DriverFeatureKernelOpen) { |
| 198 | + driverHints = append(driverHints, DriverHintOpenRequired) |
| 199 | + } else { |
| 200 | + driverHints = append(driverHints, DriverHintProprietaryRequired) |
| 201 | + } |
| 202 | + } else { |
| 203 | + driverHints = append(driverHints, DriverHintUnknown) |
| 204 | + } |
| 205 | + } |
| 206 | + return driverHints |
| 207 | +} |
| 208 | + |
| 209 | +func getDriverBranchDefault(driverBranch int) string { |
| 210 | + if driverBranch >= 560 { |
| 211 | + return KernelModuleTypeOpen |
| 212 | + } |
| 213 | + return KernelModuleTypeProprietary |
| 214 | +} |
| 215 | + |
| 216 | +func buildGPUSearchMap(data GPUData) map[string]GPUDevice { |
| 217 | + var gpuMap = make(map[string]GPUDevice) |
| 218 | + for _, gpuDevice := range data.Chips { |
| 219 | + gpuMap[gpuDevice.ID] = gpuDevice |
| 220 | + } |
| 221 | + return gpuMap |
| 222 | +} |
| 223 | + |
| 224 | +func getNvidiaGPUs() ([]string, error) { |
| 225 | + var nvDevices []string |
| 226 | + deviceDirs, err := os.ReadDir(PCIDevicesRoot) |
| 227 | + if err != nil { |
| 228 | + return nil, err |
| 229 | + } |
| 230 | + |
| 231 | + for _, device := range deviceDirs { |
| 232 | + vendor, err := os.ReadFile(path.Join(PCIDevicesRoot, device.Name(), "vendor")) |
| 233 | + if err != nil { |
| 234 | + return nil, fmt.Errorf("failed to read pci device vendor name for %s: %w", device.Name(), err) |
| 235 | + } |
| 236 | + if strings.TrimSpace(string(vendor)) != NVIDIAVendorID { |
| 237 | + log.Tracef("Skipping device %s as it's not from the NVIDIA vendor", device.Name()) |
| 238 | + continue |
| 239 | + } |
| 240 | + class, err := os.ReadFile(path.Join(PCIDevicesRoot, device.Name(), "class")) |
| 241 | + if err != nil { |
| 242 | + return nil, fmt.Errorf("failed to read pci device class name for %s: %w", device.Name(), err) |
| 243 | + } |
| 244 | + if strings.TrimSpace(string(class)) != PCIDeviceClassVGA && strings.TrimSpace(string(class)) != PCIDeviceClassGPU { |
| 245 | + log.Tracef("Skipping NVIDIA device %s as it's not of VGA/GPU device class", device.Name()) |
| 246 | + continue |
| 247 | + } |
| 248 | + b, err := os.ReadFile(path.Join(PCIDevicesRoot, device.Name(), "device")) |
| 249 | + if err != nil { |
| 250 | + return nil, fmt.Errorf("failed to read pci device id for %s: %w", device.Name(), err) |
| 251 | + } |
| 252 | + |
| 253 | + deviceID, err := sanitizeDeviceID(string(b)) |
| 254 | + if err != nil { |
| 255 | + return nil, fmt.Errorf("found invalid device id for %s: %w", device.Name(), err) |
| 256 | + } |
| 257 | + |
| 258 | + nvDevices = append(nvDevices, deviceID) |
| 259 | + } |
| 260 | + return nvDevices, nil |
| 261 | +} |
| 262 | + |
| 263 | +func sanitizeDeviceID(input string) (string, error) { |
| 264 | + var result string |
| 265 | + result = strings.TrimSpace(input) |
| 266 | + |
| 267 | + if len(result) != 6 { |
| 268 | + return "", fmt.Errorf("invalid device id format: %s", input) |
| 269 | + } |
| 270 | + |
| 271 | + // We only uppercase the device after the 0x part of the device id string to match the format in supported-gpus.json |
| 272 | + // For e.g. "0x1db6" becomes "0x1DB6" |
| 273 | + result = fmt.Sprintf("%s%s", result[0:2], strings.ToUpper(result[2:])) |
| 274 | + return result, nil |
| 275 | +} |
0 commit comments