-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpu.go
More file actions
88 lines (79 loc) · 2.89 KB
/
Copy pathgpu.go
File metadata and controls
88 lines (79 loc) · 2.89 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
// NVENCForgeGUI — Required Notice: Copyright (c) 2026 burnersen — NVENCForgeGUI
// Licensed under the PolyForm Noncommercial License 1.0.0 (non-commercial use only).
// Full terms: LICENSE.md · https://polyformproject.org/licenses/noncommercial/1.0.0
// gpu.go — welche Grafikkarte steckt im Rechner?
//
// Die Oberfläche zeigt die Karte nur an; sie graut nichts aus und blockiert
// nichts. Ob eine Karte wirklich mit den gewünschten Einstellungen umgehen
// kann, entscheidet weiterhin allein die Start-Probe des Konverters (v1.16.0) —
// die fragt die Karte praktisch und nicht nach ihrem Namen.
package main
import (
"context"
"os/exec"
"strconv"
"strings"
"syscall"
"time"
)
// gpuQueryTimeout begrenzt die Abfrage. nvidia-smi antwortet normalerweise in
// weniger als einer Sekunde; hängt der Treiber, darf das Fenster deswegen nicht
// beim Start stehen bleiben.
const gpuQueryTimeout = 5 * time.Second
// winCreateNoWindow verhindert das kurze schwarze Fenster bei Hilfsaufrufen.
// Für den Konverter selbst ist dieses Flag verboten (siehe wincon.go).
const winCreateNoWindow = 0x08000000
// GPUInfo ist das, was die Oberfläche über die Karte anzeigt.
type GPUInfo struct {
Detected bool `json:"detected"`
Name string `json:"name"`
Driver string `json:"driver"`
MemoryMB int `json:"memoryMB"`
Note string `json:"note"`
}
// queryGPU fragt nvidia-smi nach Name, Treiber und Speicher der ersten Karte.
//
// nvidia-smi gehört zum NVIDIA-Treiber; fehlt es, steckt entweder keine
// NVIDIA-Karte im Rechner oder der Treiber ist nicht installiert. Beides ist
// kein Fehler dieses Programms, deshalb wird es als Hinweis gemeldet und nicht
// als Absturz.
func queryGPU() GPUInfo {
ctx, cancel := context.WithTimeout(context.Background(), gpuQueryTimeout)
defer cancel()
cmd := exec.CommandContext(ctx, "nvidia-smi",
"--query-gpu=name,driver_version,memory.total",
"--format=csv,noheader,nounits")
cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: winCreateNoWindow}
out, err := cmd.Output()
if err != nil {
return GPUInfo{Note: "No NVIDIA GPU detected (nvidia-smi not available)."}
}
return parseGPUQuery(string(out))
}
// parseGPUQuery liest die erste Zeile der nvidia-smi-Ausgabe.
//
// Eigene Funktion, damit sie ohne Grafikkarte testbar ist. Bei mehreren Karten
// zählt die erste — genau die benutzt der Konverter auch.
func parseGPUQuery(raw string) GPUInfo {
for _, line := range strings.Split(raw, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
fields := strings.Split(line, ",")
if len(fields) < 3 {
continue
}
memoryMB, err := strconv.Atoi(strings.TrimSpace(fields[2]))
if err != nil {
memoryMB = 0
}
return GPUInfo{
Detected: true,
Name: strings.TrimSpace(fields[0]),
Driver: strings.TrimSpace(fields[1]),
MemoryMB: memoryMB,
}
}
return GPUInfo{Note: "nvidia-smi answered, but reported no GPU."}
}