mirror of
https://github.com/oneclickvirt/basics.git
synced 2025-10-22 08:09:28 +08:00
update
This commit is contained in:
69
system/cpu_linux.go
Normal file
69
system/cpu_linux.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/oneclickvirt/basics/system/model"
|
||||
"github.com/shirou/gopsutil/cpu"
|
||||
)
|
||||
|
||||
func checkCPUFeatureLinux(filename string, feature string) (string, bool) {
|
||||
content, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return "Error reading file", false
|
||||
}
|
||||
lines := strings.Split(string(content), "\n")
|
||||
for _, line := range lines {
|
||||
if strings.Contains(line, feature) {
|
||||
return "✔️ Enabled", true
|
||||
}
|
||||
}
|
||||
return "❌ Disabled", false
|
||||
}
|
||||
|
||||
func checkCPUFeature(filename string, feature string) (string, bool) {
|
||||
if runtime.GOOS == "linux" {
|
||||
return checkCPUFeatureLinux(filename, feature)
|
||||
}
|
||||
return "Unsupported OS", false
|
||||
}
|
||||
|
||||
func getCpuInfo(ret *model.SystemInfo, cpuType string) (*model.SystemInfo, error) {
|
||||
var aesFeature, virtFeature, hypervFeature string
|
||||
var st bool
|
||||
ci, err := cpu.Info()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cpu.Info error: %v", err.Error())
|
||||
} else {
|
||||
ret.CpuModel = ""
|
||||
for i := 0; i < len(ci); i++ {
|
||||
if len(ret.CpuModel) < len(ci[i].ModelName) {
|
||||
ret.CpuModel = ci[i].ModelName + fmt.Sprintf("%d %s Core", len(ci), cpuType) + " @ " +
|
||||
strconv.FormatFloat(ci[i].Mhz, 'f', 2, 64) + " MHz"
|
||||
ret.CpuCores = fmt.Sprintf("%d vCPU(s)", int(ci[i].Cores))
|
||||
if ci[i].CacheSize != 0 { // Windows查不到CPU的三缓
|
||||
ret.CpuCache = string(ci[i].CacheSize)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if runtime.GOOS == "windows" {
|
||||
aesFeature = `HARDWARE\DESCRIPTION\System\CentralProcessor\0`
|
||||
virtFeature = `HARDWARE\DESCRIPTION\System\CentralProcessor\0`
|
||||
hypervFeature = `SYSTEM\CurrentControlSet\Control\Hypervisor\0`
|
||||
} else if runtime.GOOS == "linux" {
|
||||
aesFeature = "/proc/cpuinfo"
|
||||
virtFeature = "/proc/cpuinfo"
|
||||
hypervFeature = "/proc/cpuinfo"
|
||||
}
|
||||
ret.CpuAesNi, _ = checkCPUFeature(aesFeature, "aes")
|
||||
ret.CpuVAH, st = checkCPUFeature(virtFeature, "vmx")
|
||||
if !st {
|
||||
ret.CpuVAH, _ = checkCPUFeature(hypervFeature, "hypervisor")
|
||||
}
|
||||
return ret, nil
|
||||
}
|
Reference in New Issue
Block a user