forked from clemsonciti/jobperf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodes.go
86 lines (79 loc) · 1.83 KB
/
nodes.go
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
package jobperf
import (
"fmt"
"strconv"
"strings"
"time"
"unicode"
)
type Bytes int64
// ParseBytes will parse a string into bytes. It assumes any units are in IEC (i.e. powers of 2).
func ParseBytes(in string) (Bytes, error) {
s := strings.TrimSpace(in)
firstNonDigit := strings.IndexFunc(s, func(r rune) bool {
return !unicode.IsDigit(r)
})
if firstNonDigit == -1 {
firstNonDigit = len(s)
}
value, err := strconv.Atoi(s[:firstNonDigit])
if err != nil {
return 0, fmt.Errorf("failed to parse string %s: %w", in, err)
}
var unit string
if firstNonDigit == len(s) {
unit = "b"
} else {
unit = strings.ToLower(strings.TrimSpace(s[firstNonDigit:]))
}
unitTable := map[string]int{
"b": 1,
"kb": 1 << 10,
"kib": 1 << 10,
"mb": 1 << 20,
"mib": 1 << 20,
"gb": 1 << 30,
"gib": 1 << 30,
"tb": 1 << 40,
"tib": 1 << 40,
}
mult, found := unitTable[unit]
if !found {
return 0, fmt.Errorf("failed to parse bytes string %s: unknown unit %v", in, unit)
}
scaledVal := value * mult
if value != 0 && scaledVal/value != mult {
return 0, fmt.Errorf("failed to parse bytes string %s: does not fit in int64", in)
}
return Bytes(scaledVal), nil
}
func (b Bytes) String() string {
if b < (1 << 10) {
return fmt.Sprintf("%d B", b)
}
if b < (1 << 20) {
return fmt.Sprintf("%.2f KiB", float64(b)/1024.0)
}
if b < (1 << 30) {
return fmt.Sprintf("%.2f MiB", float64(b)/1024.0/1024.0)
}
if b < (1 << 40) {
return fmt.Sprintf("%.2f GiB", float64(b)/1024.0/1024.0/1024.0)
}
return fmt.Sprintf("%.2f TiB", float64(b)/1024.0/1024.0/1024.0/1024.0)
}
type Node struct {
Hostname string
NCores int
Memory Bytes
NGPUs int
}
type GPUStat struct {
ProductName string
ComputeUsage int // In percent
ID string
MemUsageBytes Bytes
MemTotalBytes Bytes
SampleTime time.Time
Hostname string
}