-
Notifications
You must be signed in to change notification settings - Fork 2
/
processor.go
56 lines (46 loc) · 1.38 KB
/
processor.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
package machina
import "slices"
// ProcessorName identifies a processor on the local system by a well-known name.
type ProcessorName string
// ProcessorMap maps processor names to processors on the local system.
type ProcessorMap map[ProcessorName]Processor
// Default returns the name of a default processor from the map.
//
// If the processor map is empty it returns an empty string.
func (m ProcessorMap) Default() ProcessorName {
if len(m) == 0 {
return ""
}
// Build lists of default and non-default processors.
defaults := make([]ProcessorName, 0, len(m))
nonDefaults := make([]ProcessorName, 0, len(m))
for name, processor := range m {
if processor.Default {
defaults = append(defaults, name)
} else {
nonDefaults = append(nonDefaults, name)
}
}
// If we have more than one default, sort them for deterministic
// results.
if len(defaults) > 1 {
slices.Sort(defaults)
}
// If we have at least one default, use it.
if len(defaults) > 0 {
return defaults[0]
}
// If we have more than one non-default, sort them for deterministic
// results.
if len(nonDefaults) > 1 {
slices.Sort(nonDefaults)
}
return nonDefaults[0]
}
// Processor describes a processor that a machine can run on.
type Processor struct {
Brand string `json:"brand"`
Model string `json:"model"`
ThreadsPerCore int `json:"threads"`
Default bool `json:"default"`
}