1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-05-04 22:17:34 +08:00
shirou_gopsutil/sensors/sensors_darwin_cgo.go

96 lines
1.9 KiB
Go
Raw Normal View History

// SPDX-License-Identifier: BSD-3-Clause
2021-12-22 21:54:41 +00:00
//go:build darwin && cgo
2019-12-13 16:03:44 +01:00
package sensors
2019-12-13 16:03:44 +01:00
// #cgo CFLAGS: -x objective-c
// #cgo LDFLAGS: -framework Foundation -framework IOKit
// #include "smc_darwin.h"
// #include "darwin_arm_sensors.h"
2019-12-13 16:03:44 +01:00
import "C"
2024-06-02 14:22:22 +03:00
import (
"bufio"
2024-06-02 14:22:22 +03:00
"context"
"math"
"runtime"
"strconv"
"strings"
2024-06-02 14:22:22 +03:00
"unsafe"
)
2019-12-13 16:03:44 +01:00
func ReadTemperaturesArm() []TemperatureStat {
cStr := C.getThermals()
defer C.free(unsafe.Pointer(cStr))
var stats []TemperatureStat
goStr := C.GoString(cStr)
scanner := bufio.NewScanner(strings.NewReader(goStr))
for scanner.Scan() {
split := strings.Split(scanner.Text(), ":")
if len(split) != 2 {
continue
}
val, err := strconv.ParseFloat(split[1], 32)
if err != nil {
continue
}
sensorKey := strings.Split(split[0], " ")[0]
val = math.Abs(val)
stats = append(stats, TemperatureStat{
SensorKey: sensorKey,
Temperature: float64(val),
})
}
return stats
}
func TemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
if runtime.GOARCH == "arm64" {
return ReadTemperaturesArm(), nil
}
2019-12-13 16:03:44 +01:00
temperatureKeys := []string{
C.AMBIENT_AIR_0,
C.AMBIENT_AIR_1,
C.CPU_0_DIODE,
C.CPU_0_HEATSINK,
C.CPU_0_PROXIMITY,
C.ENCLOSURE_BASE_0,
C.ENCLOSURE_BASE_1,
C.ENCLOSURE_BASE_2,
C.ENCLOSURE_BASE_3,
C.GPU_0_DIODE,
C.GPU_0_HEATSINK,
C.GPU_0_PROXIMITY,
C.HARD_DRIVE_BAY,
C.MEMORY_SLOT_0,
C.MEMORY_SLOTS_PROXIMITY,
C.NORTHBRIDGE,
C.NORTHBRIDGE_DIODE,
C.NORTHBRIDGE_PROXIMITY,
C.THUNDERBOLT_0,
C.THUNDERBOLT_1,
C.WIRELESS_MODULE,
}
var temperatures []TemperatureStat
C.gopsutil_v4_open_smc()
defer C.gopsutil_v4_close_smc()
2019-12-13 16:03:44 +01:00
for _, key := range temperatureKeys {
2024-06-02 14:22:22 +03:00
ckey := C.CString(key)
defer C.free(unsafe.Pointer(ckey))
2019-12-13 16:03:44 +01:00
temperatures = append(temperatures, TemperatureStat{
SensorKey: key,
2024-06-02 14:22:22 +03:00
Temperature: float64(C.gopsutil_v4_get_temperature(ckey)),
2019-12-13 16:03:44 +01:00
})
}
2019-12-13 16:03:44 +01:00
return temperatures, nil
}