From 3fcab5a42054c84895c4f6da5097360d35414fde Mon Sep 17 00:00:00 2001 From: "Sean E. Russell" Date: Wed, 22 Apr 2020 13:33:51 -0500 Subject: Switch from smc.c to iSMC. --- CHANGELOG.md | 1 + cmd/gotop/main.go | 3 +- devices/temp_darwin.go | 41 +++----------- go.mod | 1 + go.sum | 4 ++ make.sh | 145 ------------------------------------------------- scripts/make.sh | 145 +++++++++++++++++++++++++++++++++++++++++++++++++ scripts/makeDarwin.sh | 11 ++++ 8 files changed, 173 insertions(+), 178 deletions(-) delete mode 100755 make.sh create mode 100755 scripts/make.sh create mode 100755 scripts/makeDarwin.sh diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d2670e..f61b2c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Merged fix from @markuspeloquin for custom color scheme loading crash - Memory line colors were inconsistently assigned (#91) - The disk code was truncating values instead of rounding (#90) +- Temperatures on Darwin were all over the place, and wrong (#48) ## [3.5.1] - 2020-04-09 diff --git a/cmd/gotop/main.go b/cmd/gotop/main.go index 5728128..f44d7a2 100644 --- a/cmd/gotop/main.go +++ b/cmd/gotop/main.go @@ -38,7 +38,7 @@ const ( var ( // TODO: Set this at compile time; having to check this in sucks. - Version = "3.6.0" + Version = "3.6.dev" conf gotop.Config help *w.HelpMenu bar *w.StatusBar @@ -51,6 +51,7 @@ var ( // TODO: state:deferred 157 FreeBSD fixes & Nvidia GPU support (kraust/master). Significant CPU use impact for NVidia changes. // TODO: Virtual devices from Prometeus metrics @feature // TODO: Abstract out the UI toolkit. mum4k/termdash, VladimirMarkelov/clui, gcla/gowid, rivo/tview, marcusolsson/tui-go might work better for some OS/Archs. Performance/memory use comparison would be interesting. +// TODO: Add fans func parseArgs(conf *gotop.Config) error { cds := conf.ConfigDir.QueryFolders(configdir.All) cpaths := make([]string, len(cds)) diff --git a/devices/temp_darwin.go b/devices/temp_darwin.go index e60b4be..4e068a4 100644 --- a/devices/temp_darwin.go +++ b/devices/temp_darwin.go @@ -2,45 +2,22 @@ package devices -// #cgo LDFLAGS: -framework IOKit -// #include "include/smc.c" -import "C" +import smc "github.com/xxxserxxx/iSMC" func init() { RegisterTemp(update) + ts = make(map[string]float32) } +var ts map[string]float32 + func update(temps map[string]int) map[string]error { - temperatureKeys := map[string]string{ - C.AMBIENT_AIR_0: "ambient_air_0", - C.AMBIENT_AIR_1: "ambient_air_1", - C.CPU_0_DIODE: "cpu_0_diode", - C.CPU_0_HEATSINK: "cpu_0_heatsink", - C.CPU_0_PROXIMITY: "cpu_0_proximity", - C.ENCLOSURE_BASE_0: "enclosure_base_0", - C.ENCLOSURE_BASE_1: "enclosure_base_1", - C.ENCLOSURE_BASE_2: "enclosure_base_2", - C.ENCLOSURE_BASE_3: "enclosure_base_3", - C.GPU_0_DIODE: "gpu_0_diode", - C.GPU_0_HEATSINK: "gpu_0_heatsink", - C.GPU_0_PROXIMITY: "gpu_0_proximity", - C.HARD_DRIVE_BAY: "hard_drive_bay", - C.MEMORY_SLOT_0: "memory_slot_0", - C.MEMORY_SLOTS_PROXIMITY: "memory_slots_proximity", - C.NORTHBRIDGE: "northbridge", - C.NORTHBRIDGE_DIODE: "northbridge_diode", - C.NORTHBRIDGE_PROXIMITY: "northbridge_proximity", - C.THUNDERBOLT_0: "thunderbolt_0", - C.THUNDERBOLT_1: "thunderbolt_1", - C.WIRELESS_MODULE: "wireless_module", + err := smc.GetTemp(ts) + if err != nil { + return map[string]error{"temps": err} } - - C.open_smc() - defer C.close_smc() - - for key, val := range temperatureKeys { - temps[val] = int(C.get_tmp(C.CString(key), C.CELSIUS)) + for k, v := range ts { + temps[k] = int(v + 0.5) } - return nil } diff --git a/go.mod b/go.mod index 2b53f3a..c81ba92 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/shirou/gopsutil v2.18.11+incompatible github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4 // indirect github.com/stretchr/testify v1.4.0 + github.com/xxxserxxx/iSMC v1.0.1 howett.net/plist v0.0.0-20181124034731-591f970eefbb // indirect ) diff --git a/go.sum b/go.sum index 0a20cb4..c3bef32 100644 --- a/go.sum +++ b/go.sum @@ -63,6 +63,8 @@ github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3Rllmb github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d h1:x3S6kxmy49zXVVyhcnrFqxvNVCBPb2KZ9hV2RBdS840= github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ= +github.com/panotza/gosmc v0.0.0-20190601191911-810267459a2a h1:P0QSyHOubLI2e6hccBBEjVX0vPWXoXui5QCAVNWdJSk= +github.com/panotza/gosmc v0.0.0-20190601191911-810267459a2a/go.mod h1:u8Q8dpnMAam0MElxP2KjEROzXMk9G8X168RTpAZ9tPc= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -98,6 +100,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/xxxserxxx/iSMC v1.0.1 h1:M9Gkwnnkl+evvnugoB5yRYrbUP+cRIVOPM+xrHZc3Hs= +github.com/xxxserxxx/iSMC v1.0.1/go.mod h1:TGgNjU7BF2DZSuxiTft+BdzxzcujFJYqFfMCzcTl/aY= golang.org/x/arch v0.0.0-20181203225421-5a4828bb7045/go.mod h1:cYlCBUl1MsqxdiKgmc4uh7TxZfWSFLOGSRR090WDxt8= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= diff --git a/make.sh b/make.sh deleted file mode 100755 index 46ecbce..0000000 --- a/make.sh +++ /dev/null @@ -1,145 +0,0 @@ -#!/bin/bash - -export VERSION=$(go run ./cmd/gotop -V) - -rm -f build.log - -# Set up some common environment variables -function out() { - export GOOS=$1 - export GOARCH=$2 - OUT=build/gotop_${VERSION}_${GOOS}_${GOARCH} - if [[ -n $3 ]]; then - export GOARM=$3 - OUT=${OUT}_v${GOARM} - fi - D=build/gotop - if [[ $GOOS == "windows" ]]; then - D=${D}.exe - AR=${OUT}.zip - else - AR=${OUT}.tgz - fi -} -# Clean up environment variables -function uset() { - unset GOOS GOARCH GOARM CGO_ENABLED OUT D AR -} -# Compile something that can be cross-compiled without docker -function c() { - out $1 $2 $3 - go build -o $D ./cmd/gotop >> build.log 2>&1 - if [[ $? -ne 0 ]]; then - printenv | grep GO >> build.log - echo "############### FAILED ###############" - echo FAILED COMPILE $OUT - fi -} -# Zip up something that's been compiled -function z() { - out $1 $2 $3 - if [[ -e $AR ]]; then - echo SKIP $AR - return - fi - cd build - if [[ $GOOS == "windows" ]]; then - zip -q $(basename $AR) $(basename $D) - else - tar -czf $(basename $AR) $(basename $D) - fi - if [[ $? -ne 0 ]]; then - echo "############### FAILED ###############" - echo FAILED ARCHIVE $AR - cd .. - return - fi - cd .. - echo BUILT $AR -} -# Do c(), then z(), and then clean up. -function candz() { - unset OUT - out $1 $2 $3 - local AR=${OUT}.zip - if [[ -e $AR ]]; then - echo SKIP $AR - return - fi - c $1 $2 $3 - z $1 $2 $3 - rm -f $D - uset -} -# Build the deb and rpm archives -function nfpmAR() { - out $1 $2 $3 - sed -i "s/arch: .*/arch: \"${GOARCH}\"/" build/nfpm.yml - for y in rpm deb; do - local AR=build/gotop_${VERSION}_linux_${GOARCH}.${y} - if [[ -e $AR ]]; then - echo SKIP $AR - else - echo Building $AR - nfpm pkg -t ${AR} -f build/nfpm.yml - fi - done -} -# Cross-compile the darwin executable. This requires docker. -function cdarwinz() { - if [[ ! -d darwin ]]; then - git clone . darwin - cd darwin - else - cd darwin - git checkout -- . - git pull - fi - export CGO_ENABLED=1 - if [[ `docker images -q gotopdarwin` == "" ]]; then - docker run -it --name osxcross --mount type=bind,source="$(pwd)",target=/mnt dockercore/golang-cross /bin/bash /mnt/build/osx.sh - local ID=`docker commit osxcross dockercore/golang-cross | cut -d : -f 2` - docker tag $ID gotopdarwin - else - docker run -it --name osxcross --mount type=bind,source="$(pwd)",target=/mnt gotopdarwin /bin/bash /mnt/build/osx.sh - fi - cd .. - mv darwin/build/gotop*darwin*.tgz build/ - docker rm osxcross - uset -} - -########################################################## -# Iterate through the OS/ARCH permutations and build an -# archive for each, using the previously defined functions. -if [[ $1 == "darwin" ]]; then - export MACOSX_DEPLOYMENT_TARGET=10.10.0 - export CC=o64-clang - export CXX=o64-clang++ - export CGO_ENABLED=1 - c darwin 386 - z darwin 386 - c darwin amd64 - z darwin amd64 -else - candz linux arm64 - - for x in 5 6 7; do - candz linux arm $x - done - - for x in 386 amd64; do - c linux $x - z linux $x - nfpmAR linux $x - rm -f $D - uset - - candz windows $x - candz freebsd $x - - # TODO Preliminary OpenBSD support [#112] [#117] [#118] - # candz openbsd $x - done - cdarwinz -fi diff --git a/scripts/make.sh b/scripts/make.sh new file mode 100755 index 0000000..46ecbce --- /dev/null +++ b/scripts/make.sh @@ -0,0 +1,145 @@ +#!/bin/bash + +export VERSION=$(go run ./cmd/gotop -V) + +rm -f build.log + +# Set up some common environment variables +function out() { + export GOOS=$1 + export GOARCH=$2 + OUT=build/gotop_${VERSION}_${GOOS}_${GOARCH} + if [[ -n $3 ]]; then + export GOARM=$3 + OUT=${OUT}_v${GOARM} + fi + D=build/gotop + if [[ $GOOS == "windows" ]]; then + D=${D}.exe + AR=${OUT}.zip + else + AR=${OUT}.tgz + fi +} +# Clean up environment variables +function uset() { + unset GOOS GOARCH GOARM CGO_ENABLED OUT D AR +} +# Compile something that can be cross-compiled without docker +function c() { + out $1 $2 $3 + go build -o $D ./cmd/gotop >> build.log 2>&1 + if [[ $? -ne 0 ]]; then + printenv | grep GO >> build.log + echo "############### FAILED ###############" + echo FAILED COMPILE $OUT + fi +} +# Zip up something that's been compiled +function z() { + out $1 $2 $3 + if [[ -e $AR ]]; then + echo SKIP $AR + return + fi + cd build + if [[ $GOOS == "windows" ]]; then + zip -q $(basename $AR) $(basename $D) + else + tar -czf $(basename $AR) $(basename $D) + fi + if [[ $? -ne 0 ]]; then + echo "############### FAILED ###############" + echo FAILED ARCHIVE $AR + cd .. + return + fi + cd .. + echo BUILT $AR +} +# Do c(), then z(), and then clean up. +function candz() { + unset OUT + out $1 $2 $3 + local AR=${OUT}.zip + if [[ -e $AR ]]; then + echo SKIP $AR + return + fi + c $1 $2 $3 + z $1 $2 $3 + rm -f $D + uset +} +# Build the deb and rpm archives +function nfpmAR() { + out $1 $2 $3 + sed -i "s/arch: .*/arch: \"${GOARCH}\"/" build/nfpm.yml + for y in rpm deb; do + local AR=build/gotop_${VERSION}_linux_${GOARCH}.${y} + if [[ -e $AR ]]; then + echo SKIP $AR + else + echo Building $AR + nfpm pkg -t ${AR} -f build/nfpm.yml + fi + done +} +# Cross-compile the darwin executable. This requires docker. +function cdarwinz() { + if [[ ! -d darwin ]]; then + git clone . darwin + cd darwin + else + cd darwin + git checkout -- . + git pull + fi + export CGO_ENABLED=1 + if [[ `docker images -q gotopdarwin` == "" ]]; then + docker run -it --name osxcross --mount type=bind,source="$(pwd)",target=/mnt dockercore/golang-cross /bin/bash /mnt/build/osx.sh + local ID=`docker commit osxcross dockercore/golang-cross | cut -d : -f 2` + docker tag $ID gotopdarwin + else + docker run -it --name osxcross --mount type=bind,source="$(pwd)",target=/mnt gotopdarwin /bin/bash /mnt/build/osx.sh + fi + cd .. + mv darwin/build/gotop*darwin*.tgz build/ + docker rm osxcross + uset +} + +########################################################## +# Iterate through the OS/ARCH permutations and build an +# archive for each, using the previously defined functions. +if [[ $1 == "darwin" ]]; then + export MACOSX_DEPLOYMENT_TARGET=10.10.0 + export CC=o64-clang + export CXX=o64-clang++ + export CGO_ENABLED=1 + c darwin 386 + z darwin 386 + c darwin amd64 + z darwin amd64 +else + candz linux arm64 + + for x in 5 6 7; do + candz linux arm $x + done + + for x in 386 amd64; do + c linux $x + z linux $x + nfpmAR linux $x + rm -f $D + uset + + candz windows $x + candz freebsd $x + + # TODO Preliminary OpenBSD support [#112] [#117] [#118] + # candz openbsd $x + done + cdarwinz +fi diff --git a/scripts/makeDarwin.sh b/scripts/makeDarwin.sh new file mode 100755 index 0000000..638f58a --- /dev/null +++ b/scripts/makeDarwin.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +GOOS=darwin +GOARCH=amd64 +CGO_ENABLED=1 +MACOSX_DEPLOYMENT_TARGET=10.10.0 +CC=o64-clang +CXX=o64-clang++ + +export GOOS GOARCH CGO_ENABLED MACOSX_DEPLOYMENT_TARGET CC CXX +go build -o gotop.darwin ./cmd/gotop -- cgit v1.2.3