mirror of
https://github.com/IceWhaleTech/CasaOS.git
synced 2025-12-04 12:16:17 +00:00
locate cpu thermal zone based on cpu type, and cache path.
This commit is contained in:
parent
96e9284235
commit
4428f8711b
1
main.go
1
main.go
@ -56,6 +56,7 @@ func init() {
|
|||||||
service.Cache = cache.Init()
|
service.Cache = cache.Init()
|
||||||
|
|
||||||
service.GetToken()
|
service.GetToken()
|
||||||
|
service.GetCPUThermalZone()
|
||||||
|
|
||||||
service.NewVersionApp = make(map[string]string)
|
service.NewVersionApp = make(map[string]string)
|
||||||
route.InitFunction()
|
route.InitFunction()
|
||||||
|
|||||||
@ -16,6 +16,7 @@ import (
|
|||||||
command2 "github.com/IceWhaleTech/CasaOS/pkg/utils/command"
|
command2 "github.com/IceWhaleTech/CasaOS/pkg/utils/command"
|
||||||
"github.com/IceWhaleTech/CasaOS/pkg/utils/common_err"
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/common_err"
|
||||||
"github.com/IceWhaleTech/CasaOS/pkg/utils/file"
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/file"
|
||||||
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/loger"
|
||||||
"github.com/shirou/gopsutil/v3/cpu"
|
"github.com/shirou/gopsutil/v3/cpu"
|
||||||
"github.com/shirou/gopsutil/v3/disk"
|
"github.com/shirou/gopsutil/v3/disk"
|
||||||
"github.com/shirou/gopsutil/v3/host"
|
"github.com/shirou/gopsutil/v3/host"
|
||||||
@ -291,12 +292,46 @@ func (s *systemService) IsServiceRunning(name string) bool {
|
|||||||
return strings.TrimSpace(status) == "running"
|
return strings.TrimSpace(status) == "running"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// find thermal_zone of cpu.
|
||||||
|
// assertions:
|
||||||
|
// * thermal_zone "type" and "temp" are required fields
|
||||||
|
// (https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-class-thermal)
|
||||||
|
func GetCPUThermalZone() string {
|
||||||
|
keyName := "cpu_thermal_zone"
|
||||||
|
|
||||||
|
var path string
|
||||||
|
if result, ok := Cache.Get(keyName); ok {
|
||||||
|
path, ok = result.(string)
|
||||||
|
if ok {
|
||||||
|
return path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
path := "/sys/devices/virtual/thermal/thermal_zone" + strconv.Itoa(i)
|
||||||
|
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
||||||
|
name := strings.TrimSuffix(string(file.ReadFullFile(path + "/type")), "\n")
|
||||||
|
cpu_types := []string{"x86_pkg_temp", "cpu", "CPU", "soc"}
|
||||||
|
for _, s := range cpu_types {
|
||||||
|
if strings.HasPrefix(name, s) {
|
||||||
|
loger.Info(fmt.Sprintf("CPU thermal zone found: %s, path: %s.", name, path))
|
||||||
|
Cache.SetDefault(keyName, path)
|
||||||
|
return path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
loger.Error("CPUThermalZone not found. CPU Temp will not be displayed.")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
func (s *systemService) GetCPUTemperature() int {
|
func (s *systemService) GetCPUTemperature() int {
|
||||||
outPut := ""
|
outPut := ""
|
||||||
if file.Exists("/sys/class/thermal/thermal_zone0/temp") {
|
path := GetCPUThermalZone()
|
||||||
outPut = string(file.ReadFullFile("/sys/class/thermal/thermal_zone0/temp"))
|
if len(path)>0 {
|
||||||
} else if file.Exists("/sys/class/hwmon/hwmon0/temp1_input") {
|
outPut = string(file.ReadFullFile(path + "/temp"))
|
||||||
outPut = string(file.ReadFullFile("/sys/class/hwmon/hwmon0/temp1_input"))
|
|
||||||
} else {
|
} else {
|
||||||
outPut = "0"
|
outPut = "0"
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user