Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 3 additions & 56 deletions systeminfo1/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const (
cpuKeyHardware = "Hardware"
lscpuKeyMaxMHz = "CPU max MHz"
lscpuKeyModelName = "Model name"
lscpuKeyCount = "CPU(s)"
lscpuKeyCPUfamily = "CPU family"
lscpuKeyModel = "Model"
lscpuKeyStepping = "Stepping"
Expand All @@ -35,21 +34,10 @@ func getProcessorByLscpuExt(data map[string]string, freq float64) (string, error
return "", fmt.Errorf("can not find the key %q", lscpuKeyModelName)
}

cpuCountStr, ok := data[lscpuKeyCount]
if !ok {
logger.Warningf("can not find the key %q", lscpuKeyCount)
return modelName, nil
}

cpuCount, err := strconv.ParseInt(cpuCountStr, 10, 64)
if err != nil {
logger.Warning(err)
return modelName, nil
}
if strings.Contains(modelName, "Hz") {
return fmt.Sprintf("%s x %d", modelName, cpuCount), nil
return modelName, nil
} else {
return fmt.Sprintf("%s @ %.2fGHz x %d", modelName, freq, cpuCount), nil
return fmt.Sprintf("%s @ %.2fGHz", modelName, freq), nil
}
}

Expand All @@ -59,19 +47,7 @@ func getProcessorByLscpu(data map[string]string) (string, error) {
return "", fmt.Errorf("can not find the key %q", lscpuKeyModelName)
}

cpuCountStr, ok := data[lscpuKeyCount]
if !ok {
logger.Warningf("can not find the key %q", lscpuKeyCount)
return modelName, nil
}

cpuCount, err := strconv.ParseInt(cpuCountStr, 10, 64)
if err != nil {
logger.Warning(err)
return modelName, nil
}

return fmt.Sprintf("%s x %d", modelName, cpuCount), nil
return modelName, nil
}

func getCPUMaxMHzByLscpu(data map[string]string) (float64, error) {
Expand Down Expand Up @@ -125,11 +101,6 @@ func swCPUInfo(data map[string]string) string {
cpu = fmt.Sprintf("%s %.2fGHz", cpu, hz)
}

number, _ := getCPUNumber(cpuKeyActive, data)
if number != 1 {
cpu = fmt.Sprintf("%s x %v", cpu, number)
}

return cpu
}

Expand All @@ -139,11 +110,6 @@ func hwKirinCPUInfo(data map[string]string) string {
return ""
}

number, _ := getCPUNumber(cpuKeyProcessor, data)
if number != 1 {
cpu = fmt.Sprintf("%s x %v", cpu, number+1)
}

return cpu
}

Expand All @@ -153,11 +119,6 @@ func getCPUInfoFromMap(nameKey, numKey string, data map[string]string) (string,
return "", err
}

number, _ := getCPUNumber(numKey, data)
if number != 0 {
name = fmt.Sprintf("%s x %v", name, number+1)
}

return name, nil
}

Expand All @@ -182,20 +143,6 @@ func getCPUName(key string, data map[string]string) (string, error) {
return name, nil
}

func getCPUNumber(key string, data map[string]string) (int, error) {
value, ok := data[key]
if !ok {
return 0, fmt.Errorf("can not find the key %q", key)
}

number, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return 0, err
}

return int(number), nil
}

func getCPUHz(key string, data map[string]string) (float64, error) {
value, ok := data[key]
if !ok {
Expand Down
66 changes: 61 additions & 5 deletions systeminfo1/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package systeminfo

import (
"fmt"
"regexp"
"sync"
"time"

Expand Down Expand Up @@ -193,11 +195,6 @@ func NewSystemInfo() *SystemInfo {

func (info *SystemInfo) init() {
var err error
info.Processor, err = GetCPUInfo("/proc/cpuinfo")
if err != nil {
logger.Warning("Get cpu info failed:", err)
}

info.Version, err = getVersion()
if err != nil {
logger.Warning("Get version failed:", err)
Expand Down Expand Up @@ -229,6 +226,11 @@ func (info *SystemInfo) init() {
logger.Warning("Get disk capacity failed:", err)
}

info.Processor, err = GetCPUInfo("/proc/cpuinfo")
if err != nil {
logger.Warning("Get cpu info failed:", err)
}

lscpuRes, err := runLscpu()
if err != nil {
logger.Warning("run lscpu failed:", err)
Expand Down Expand Up @@ -264,6 +266,60 @@ func (info *SystemInfo) init() {
}
}
}

// Supplement missing frequency information in Processor
info.Processor = supplementProcessorFrequency(info.Processor, info.CurrentSpeed, info.CPUMaxMHz)
}

// supplementProcessorFrequency supplements missing frequency information to the processor string.
// It checks if the processor string already contains frequency info ("number+[MGT]Hz" pattern).
// If not, it adds frequency from currentSpeed (in MHz) or cpuMaxMHz (in GHz).
// Parameters:
// - processor: the processor string
// - currentSpeed: current CPU speed in MHz
// - cpuMaxMHz: max CPU frequency in MHz
//
// Returns the processor string with frequency information added if missing.
func supplementProcessorFrequency(processor string, currentSpeed uint64, cpuMaxMHz float64) string {
if processor == "" {
return processor
}

// Use regular expression to check if frequency information is already included
// (detect "number+unit+Hz" pattern, e.g., "3.20GHz", "@ 3.20GHz", "1400MHz")
freqPattern := `\d+\.?\d*\s*[MGT]Hz`
matched, err := regexp.MatchString(freqPattern, processor)
if err != nil {
logger.Warning("Failed to match frequency pattern:", err)
return processor
}

if matched {
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
return processor
}

var freqGHz float64
// Prioritize CurrentSpeed, then CPUMaxMHz
if currentSpeed != 0 {
freqGHz = float64(currentSpeed) / 1000
} else if !isFloatEqual(cpuMaxMHz, 0.0) {
freqGHz = cpuMaxMHz / 1000
}

if freqGHz > 0 {
var source string
if currentSpeed != 0 {
source = "CurrentSpeed"
} else {
source = "CPUMaxMHz"
}
formattedProcessor := fmt.Sprintf("%s @ %.2fGHz", processor, freqGHz)
logger.Debugf("Added frequency to Processor: %s (from %s)",
formattedProcessor, source)
return formattedProcessor
}
Comment thread
electricface marked this conversation as resolved.

return processor
}

func (info *SystemInfo) isValidity() bool {
Expand Down
98 changes: 82 additions & 16 deletions systeminfo1/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ import (
func TestCPUInfo(t *testing.T) {
cpu, err := GetCPUInfo("testdata/cpuinfo")
assert.Equal(t, cpu,
"Intel(R) Core(TM) i3 CPU M 330 @ 2.13GHz x 4")
"Intel(R) Core(TM) i3 CPU M 330 @ 2.13GHz")
assert.NoError(t, err)

cpu, err = GetCPUInfo("testdata/sw-cpuinfo")
assert.Equal(t, cpu, "sw 1.40GHz x 4")
assert.Equal(t, cpu, "sw 1.40GHz")
assert.NoError(t, err)

cpu, err = GetCPUInfo("testdata/arm-cpuinfo")
assert.Equal(t, cpu, "NANOPI2 x 4")
assert.Equal(t, cpu, "NANOPI2")
assert.NoError(t, err)

cpu, err = GetCPUInfo("testdata/hw_kirin-cpuinfo")
assert.Equal(t, cpu, "HUAWEI Kirin 990 x 8")
assert.Equal(t, cpu, "HUAWEI Kirin 990")
assert.NoError(t, err)
}

Expand Down Expand Up @@ -100,20 +100,86 @@ func TestGetProcessorByLscpuu(t *testing.T) {
assert.NoError(t, err)
v, err := getProcessorByLscpu(ret)
assert.NoError(t, err)
assert.Equal(t, v, "Intel(R) Core(TM) i5-4570 CPU @ 3.20GHz x 4")
assert.Equal(t, v, "Intel(R) Core(TM) i5-4570 CPU @ 3.20GHz")
}

func TestDoReadCache(t *testing.T) {
ret, err := doReadCache("testdata/systeminfo.cache")
// 创建测试数据
expectedInfo := &SystemInfo{
Version: "20 专业版",
DistroID: "uos",
DistroDesc: "UnionTech OS 20",
DistroVer: "20",
Processor: "Intel(R) Core(TM) i5-4570 CPU @ 3.20GHz",
DiskCap: uint64(500107862016),
MemoryCap: uint64(8280711168),
Comment thread
electricface marked this conversation as resolved.
SystemType: int64(64),
CPUMaxMHz: float64(3600),
CurrentSpeed: uint64(0),
}

// 创建临时缓存文件
tmpFile := t.TempDir() + "/systeminfo.cache"
err := doSaveCache(expectedInfo, tmpFile)
assert.NoError(t, err)

// 读取并验证
ret, err := doReadCache(tmpFile)
assert.NoError(t, err)
assert.Equal(t, ret.Version, "20 专业版")
assert.Equal(t, ret.DistroID, "uos")
assert.Equal(t, ret.DistroDesc, "UnionTech OS 20")
assert.Equal(t, ret.DistroVer, "20")
assert.Equal(t, ret.Processor, "Intel(R) Core(TM) i5-4570 CPU @ 3.20GHz x 4")
assert.Equal(t, ret.DiskCap, uint64(500107862016))
assert.Equal(t, ret.MemoryCap, uint64(8280711168))
assert.Equal(t, ret.SystemType, int64(64))
assert.Equal(t, ret.CPUMaxMHz, float64(3600))
assert.Equal(t, ret.CurrentSpeed, uint64(0))
assert.Equal(t, expectedInfo.Version, ret.Version)
assert.Equal(t, expectedInfo.DistroID, ret.DistroID)
assert.Equal(t, expectedInfo.DistroDesc, ret.DistroDesc)
assert.Equal(t, expectedInfo.DistroVer, ret.DistroVer)
assert.Equal(t, expectedInfo.Processor, ret.Processor)
assert.Equal(t, expectedInfo.DiskCap, ret.DiskCap)
assert.Equal(t, expectedInfo.MemoryCap, ret.MemoryCap)
assert.Equal(t, expectedInfo.SystemType, ret.SystemType)
assert.Equal(t, expectedInfo.CPUMaxMHz, ret.CPUMaxMHz)
assert.Equal(t, expectedInfo.CurrentSpeed, ret.CurrentSpeed)
}

func TestSupplementProcessorFrequency(t *testing.T) {
// Test case 1: Processor already contains GHz frequency
result := supplementProcessorFrequency("Intel(R) Core(TM) i5-4570 CPU @ 3.20GHz", 0, 0)
assert.Equal(t, "Intel(R) Core(TM) i5-4570 CPU @ 3.20GHz", result)

// Test case 2: Processor already contains MHz frequency
result = supplementProcessorFrequency("Intel Xeon 1400MHz", 0, 0)
assert.Equal(t, "Intel Xeon 1400MHz", result)

// Test case 3: Processor already contains GHz without @ symbol
result = supplementProcessorFrequency("sw 1.40GHz", 0, 0)
assert.Equal(t, "sw 1.40GHz", result)

// Test case 4: Processor already contains frequency with space
result = supplementProcessorFrequency("AMD Ryzen 3.5 GHz", 0, 0)
assert.Equal(t, "AMD Ryzen 3.5 GHz", result)

// Test case 5: Add frequency from CurrentSpeed (priority)
result = supplementProcessorFrequency("Intel Core i7", 2300, 3600)
assert.Equal(t, "Intel Core i7 @ 2.30GHz", result)

// Test case 6: Add frequency from CPUMaxMHz (fallback)
result = supplementProcessorFrequency("Intel Core i7", 0, 3600)
assert.Equal(t, "Intel Core i7 @ 3.60GHz", result)

// Test case 7: Empty processor string
result = supplementProcessorFrequency("", 2300, 3600)
assert.Equal(t, "", result)

// Test case 8: No frequency information available
result = supplementProcessorFrequency("Unknown CPU", 0, 0)
assert.Equal(t, "Unknown CPU", result)

// Test case 9: Processor with THz frequency
result = supplementProcessorFrequency("Future CPU 2.5THz", 0, 0)
assert.Equal(t, "Future CPU 2.5THz", result)

// Test case 10: CPUMaxMHz is very small (near zero)
result = supplementProcessorFrequency("Low Power CPU", 0, 0.0000001)
assert.Equal(t, "Low Power CPU", result)

// Test case 11: Add frequency with decimal CurrentSpeed
result = supplementProcessorFrequency("ARM Cortex", 1500, 0)
assert.Equal(t, "ARM Cortex @ 1.50GHz", result)
}
Binary file removed systeminfo1/testdata/systeminfo.cache
Binary file not shown.