dcu.go 1.82 KB
Newer Older
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
package logic

import (
	"get-container/utils"
	"os/exec"
	"regexp"
	"strconv"
	"strings"
)

func have_dcu() bool {
	b, _ := utils.DetectCmd("hy-smi")
	return b
}

var (
	ReEmpty    = regexp.MustCompile(`^$`)
	ReUseless  = regexp.MustCompile(`^=.*$`)
	ReHCUIndex = regexp.MustCompile(`^(?i)\s+hcu index:\s+\[(.*)\]$`)
)

func DCUInfo() map[int]*CardInfo {
	if !have_dcu() {
		return nil
	}
	output, err := exec.Command("hy-smi", "--showpids").CombinedOutput()
	if err != nil {
		return nil
	}
	return parse_dcu_info(string(output))
}

func parse_dcu_info(input string) map[int]*CardInfo {
	lines := strings.Split(strings.Trim(input, "\n"), "\n")
	useful := make([]string, 0, len(lines)/8)
	for _, v := range lines {
		if ReEmpty.MatchString(v) || ReUseless.MatchString(v) {
			continue
		}
		v = strings.Trim(v, " ")
		if strings.HasPrefix(v, "PID:") || ReHCUIndex.MatchString(v) {
			useful = append(useful, v)
		}
	}
	result := make(map[int]*CardInfo)
	var pid int = 0
	for _, v := range useful {
		if strings.HasPrefix(v, "PID:") {
			p := strings.TrimPrefix(v, "PID: ")
			pp, err := strconv.Atoi(p)
			if err == nil {
				pid = pp
			} else {
				pid = 0
			}
			continue
		}
		if pid != 0 {
			f := ReHCUIndex.FindStringSubmatch(v)
			dcuStr := strings.Fields(strings.ReplaceAll(strings.ReplaceAll(f[1], "'", " "), ",", " "))
			dcuIndexs := make([]int, 0, len(dcuStr))
			for _, d := range dcuStr {
				pp, err := strconv.Atoi(d)
				if err == nil {
					dcuIndexs = append(dcuIndexs, pp)
				}
			}
			for _, dcuIndex := range dcuIndexs {
				cinfo, have := result[dcuIndex]
				if have {
					cinfo.Pids = append(cinfo.Pids, pid)
				} else {
					cinfo = &CardInfo{
						Type:  0,
						Index: uint8(dcuIndex),
						Pids:  make([]int, 0),
					}
					cinfo.Pids = append(cinfo.Pids, pid)
					result[dcuIndex] = cinfo
				}
			}
		}
	}
	return result
}