cpu.go RawHistoryFolder
1package system
2
3import "errors"
4
5// CPUVendor represents the CPU vendor
6type CPUVendor int
7
8const (
9 // CPUVendorUnknown represents an unknown CPU vendor
10 CPUVendorUnknown CPUVendor = iota
11
12 // CPUVendorIntel represents Intel CPUs
13 CPUVendorIntel
14
15 // CPUVendorAMD represents AMD CPUs
16 CPUVendorAMD
17
18 // CPUVendorARM represents ARM CPUs
19 CPUVendorARM
20)
21
22// String returns human-readable vendor name
23func (v CPUVendor) String() string {
24 switch v {
25 case CPUVendorIntel:
26 return "Intel"
27 case CPUVendorAMD:
28 return "AMD"
29 case CPUVendorARM:
30 return "ARM"
31 default:
32 return "Unknown"
33 }
34}
35
36// Microcode represents CPU microcode package
37type Microcode int
38
39const (
40 // MicrocodeNone means no microcode updates
41 MicrocodeNone Microcode = iota
42
43 // MicrocodeIntel for Intel CPUs
44 MicrocodeIntel
45
46 // MicrocodeAMD for AMD CPUs
47 MicrocodeAMD
48)
49
50// String returns microcode package name
51func (m Microcode) String() string {
52 switch m {
53 case MicrocodeIntel:
54 return "intel-ucode"
55 case MicrocodeAMD:
56 return "amd-ucode"
57 default:
58 return ""
59 }
60}
61
62// CPUInfo is an immutable value object representing CPU information
63// It includes vendor, model, and microcode availability
64type CPUInfo struct {
65 vendor CPUVendor
66 model string // CPU model name
67 microcode Microcode
68}
69
70// NewCPUInfo creates a new CPUInfo value object
71func NewCPUInfo(vendor CPUVendor, model string, microcode Microcode) (*CPUInfo, error) {
72 if model == "" {
73 return nil, errors.New("CPU model cannot be empty")
74 }
75
76 if len(model) > 128 {
77 return nil, errors.New("CPU model name too long")
78 }
79
80 // Validate microcode matches vendor
81 if vendor == CPUVendorIntel && microcode == MicrocodeAMD {
82 return nil, errors.New("AMD microcode cannot be used with Intel CPU")
83 }
84
85 if vendor == CPUVendorAMD && microcode == MicrocodeIntel {
86 return nil, errors.New("intel microcode cannot be used with AMD CPU")
87 }
88
89 return &CPUInfo{
90 vendor: vendor,
91 model: model,
92 microcode: microcode,
93 }, nil
94}
95
96// Vendor returns the CPU vendor
97func (c *CPUInfo) Vendor() CPUVendor {
98 return c.vendor
99}
100
101// Model returns the CPU model name
102func (c *CPUInfo) Model() string {
103 return c.model
104}
105
106// Microcode returns the CPU microcode package
107func (c *CPUInfo) Microcode() Microcode {
108 return c.microcode
109}
110
111// RequiresMicrocode returns true if this CPU needs microcode updates
112func (c *CPUInfo) RequiresMicrocode() bool {
113 return c.microcode != MicrocodeNone
114}
115
116// String returns human-readable representation
117func (c *CPUInfo) String() string {
118 return "CPUInfo(vendor=" + c.vendor.String() +
119 ", model=" + c.model +
120 ", microcode=" + c.microcode.String() + ")"
121}
122
123// Equals checks if two CPUInfo objects are equal
124func (c *CPUInfo) Equals(other *CPUInfo) bool {
125 if other == nil {
126 return false
127 }
128 return c.vendor == other.vendor &&
129 c.model == other.model &&
130 c.microcode == other.microcode
131}