-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcpu.go
170 lines (155 loc) · 5.69 KB
/
cpu.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package cpu
import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"golang.org/x/crypto/sha3"
)
// AdditionalInfo -
type AdditionalInfo map[string]interface{}
// CPU -
type CPU struct {
ID string `json:"id,omitempty"` // SHA-3 hash of on all fields except any arrays or maps
Count int `json:"count,omitempty"` // nr of physical CPUs
CoresPerCPU int `json:"coresPerCPU,omitempty"` // nr of Cores per CPU
ThreadsPerCore int `json:"threadsPerCore,omitempty"` // nr of Threads per Core
TotalThreads int `json:"totalThreads,omitempty"` // nr of CPU Threads from all Cores and all CPUs
Architecture CPUArchitecture `json:"architecture,omitempty"` // ex: `amd64` or `ppc64`
Variant CPUVariant `json:"variant,omitempty"` //
Manufacturer string `json:"manufacturer,omitempty"` // intel, amd, arm
Model string `json:"model,omitempty"` // Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
ByteOrder string `json:"byteOrder,omitempty"` //
Features []string `json:"features,omitempty"` // ex: `SWPB (swap) instructions` for ARM
AdditionalInfo AdditionalInfo `json:"additionalInfo,omitempty"` //
}
func (thisRef *CPU) setID() {
var values = []interface{}{
thisRef.Count,
thisRef.CoresPerCPU,
thisRef.ThreadsPerCore,
thisRef.TotalThreads,
thisRef.Architecture,
thisRef.Variant,
thisRef.Manufacturer,
thisRef.Model,
thisRef.ByteOrder,
}
buffer := new(bytes.Buffer)
for _, value := range values {
err := binary.Write(buffer, binary.BigEndian, []byte(fmt.Sprintf("%v", value)))
if err != nil {
return
}
}
hash := sha3.Sum512(buffer.Bytes())
thisRef.ID = hex.EncodeToString(hash[:])
}
// CPUVariant -
type CPUVariant struct {
Name CPUArchitectureVariant `json:"name,omitempty"` // ex: ARMv8
Detailed CPUArchitectureVariantDetailed `json:"detailed,omitempty"` // ex: ARMv8.2-A
}
// CPUArchitecture -
type CPUArchitecture string
const (
CPU_386 CPUArchitecture = "386" //
CPU_AMD64 = "AMD64" //
CPU_AMD64p32 = "AMD64p32" //
CPU_ARM = "ARM" //
CPU_ARMbe = "ARMbe" //
CPU_ARM64 = "ARM64" //
CPU_ARM64be = "ARM64be" //
CPU_PPC64 = "PPC64" //
CPU_PPC64le = "PPC64le" //
CPU_MIPS = "MIPS" //
CPU_MIPSle = "MIPSle" //
CPU_MIPS64 = "MIPS64" //
CPU_MIPS64le = "MIPS64le" //
CPU_MIPS64p32 = "MIPS64p32" //
CPU_MIPS64p32le = "MIPS64p32le" //
CPU_PPC = "PPC" //
CPU_RISCv = "RISCv" //
CPU_RISCv64 = "RISCv64" //
CPU_S390 = "S390" //
CPU_S390x = "S390x" //
CPU_SPARC = "SPARC" //
CPU_SPARC64 = "SPARC64" //
CPU_WASM = "WASM" //
CPU_Uknown = "Uknown" //
)
var supportedCPUArchitectures = []CPUArchitecture{
CPU_386,
CPU_AMD64,
CPU_AMD64p32,
CPU_ARM,
CPU_ARMbe,
CPU_ARM64,
CPU_ARM64be,
CPU_PPC64,
CPU_PPC64le,
CPU_MIPS,
CPU_MIPSle,
CPU_MIPS64,
CPU_MIPS64le,
CPU_MIPS64p32,
CPU_MIPS64p32le,
CPU_PPC,
CPU_RISCv,
CPU_RISCv64,
CPU_S390,
CPU_S390x,
CPU_SPARC,
CPU_SPARC64,
CPU_WASM,
}
// CPUArchitectureVariant -
type CPUArchitectureVariant string
// https://en.wikipedia.org/wiki/List_of_ARM_microarchitectures
const (
CPUV_ARMv1 CPUArchitectureVariant = "ARM1"
CPUV_ARMv2 = "ARM2"
CPUV_ARMv3 = "ARM3"
CPUV_ARMv4 = "ARM4"
CPUV_ARMv5 = "ARM5"
CPUV_ARMv6 = "ARM6"
CPUV_ARMv7 = "ARM7"
CPUV_ARMv8 = "ARM8"
CPUV_Uknown = "Uknown"
)
var supportedCPUArchitectureVariants = []CPUArchitectureVariant{
CPUV_ARMv1,
CPUV_ARMv2,
CPUV_ARMv3,
CPUV_ARMv4,
CPUV_ARMv5,
CPUV_ARMv6,
CPUV_ARMv7,
CPUV_ARMv8,
}
// CPUArchitectureVariantDetailed -
type CPUArchitectureVariantDetailed string
const (
CPUVD_ARMv2A CPUArchitectureVariantDetailed = "ARMv2a"
CPUVD_ARMv4T = "ARMv4t"
CPUVD_ARMv5T = "ARMv5t"
CPUVD_ARMv5TE = "ARMv5te"
CPUVD_ARMv5TEJ = "ARMv5tej"
CPUVD_ARMv6_M = "ARMv6-m"
CPUVD_ARMv6K = "ARMv6k"
CPUVD_ARMv6T2 = "ARMv6t2"
CPUVD_ARMv6TEJ = "ARMv6tej"
CPUVD_ARMv6Z = "ARMv6z"
CPUVD_ARMv7_A = "ARMv7-a"
CPUVD_ARMv7_M = "ARMv7-m"
CPUVD_ARMv7_R = "ARMv7-r"
CPUVD_ARMv7E_M = "ARMv7e-m"
CPUVD_ARMv8_A = "ARMv8-a"
CPUVD_ARMv8_M = "ARMv8-m"
CPUVD_ARMv8_R = "ARMv8-r"
CPUVD_ARMv8_1_A = "ARMv8.1-a"
CPUVD_ARMv8_2_A = "ARMv8.2-a"
CPUVD_ARMv8_3_A = "ARMv8.3-a"
CPUVD_ARMv8_4_A = "ARMv8.4-a"
CPUVD_Uknown = "Uknown"
)