mirror of
https://github.com/tw93/Mole.git
synced 2026-02-16 16:25:17 +00:00
Fix Intel CPU error
This commit is contained in:
BIN
bin/status-go
BIN
bin/status-go
Binary file not shown.
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
@@ -365,18 +366,37 @@ func formatUptime(secs uint64) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func collectCPU() (CPUStatus, error) {
|
func collectCPU() (CPUStatus, error) {
|
||||||
|
counts, countsErr := cpu.Counts(false)
|
||||||
|
if countsErr != nil || counts == 0 {
|
||||||
|
counts = runtime.NumCPU()
|
||||||
|
}
|
||||||
|
|
||||||
|
logical, logicalErr := cpu.Counts(true)
|
||||||
|
if logicalErr != nil || logical == 0 {
|
||||||
|
logical = runtime.NumCPU()
|
||||||
|
}
|
||||||
|
if logical <= 0 {
|
||||||
|
logical = 1
|
||||||
|
}
|
||||||
|
|
||||||
percents, err := cpu.Percent(cpuSampleInterval, true)
|
percents, err := cpu.Percent(cpuSampleInterval, true)
|
||||||
|
var totalPercent float64
|
||||||
|
if err != nil || len(percents) == 0 {
|
||||||
|
fallbackUsage, fallbackPerCore, fallbackErr := fallbackCPUUtilization(logical)
|
||||||
|
if fallbackErr != nil {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return CPUStatus{}, err
|
return CPUStatus{}, err
|
||||||
}
|
}
|
||||||
if len(percents) == 0 {
|
return CPUStatus{}, fallbackErr
|
||||||
return CPUStatus{}, errors.New("cannot read CPU utilization")
|
|
||||||
}
|
}
|
||||||
totalPercent := 0.0
|
totalPercent = fallbackUsage
|
||||||
|
percents = fallbackPerCore
|
||||||
|
} else {
|
||||||
for _, v := range percents {
|
for _, v := range percents {
|
||||||
totalPercent += v
|
totalPercent += v
|
||||||
}
|
}
|
||||||
totalPercent /= float64(len(percents))
|
totalPercent /= float64(len(percents))
|
||||||
|
}
|
||||||
|
|
||||||
loadStats, loadErr := load.Avg()
|
loadStats, loadErr := load.Avg()
|
||||||
var loadAvg load.AvgStat
|
var loadAvg load.AvgStat
|
||||||
@@ -389,16 +409,6 @@ func collectCPU() (CPUStatus, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
counts, countsErr := cpu.Counts(false)
|
|
||||||
if countsErr != nil || counts == 0 {
|
|
||||||
counts = runtime.NumCPU()
|
|
||||||
}
|
|
||||||
|
|
||||||
logical, logicalErr := cpu.Counts(true)
|
|
||||||
if logicalErr != nil || logical == 0 {
|
|
||||||
logical = runtime.NumCPU()
|
|
||||||
}
|
|
||||||
|
|
||||||
return CPUStatus{
|
return CPUStatus{
|
||||||
Usage: totalPercent,
|
Usage: totalPercent,
|
||||||
PerCore: percents,
|
PerCore: percents,
|
||||||
@@ -466,6 +476,60 @@ func fallbackLoadAvgFromUptime() (load.AvgStat, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func fallbackCPUUtilization(logical int) (float64, []float64, error) {
|
||||||
|
if logical <= 0 {
|
||||||
|
logical = runtime.NumCPU()
|
||||||
|
}
|
||||||
|
if logical <= 0 {
|
||||||
|
logical = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
out, err := runCmd(ctx, "ps", "-Aceo", "pcpu")
|
||||||
|
if err != nil {
|
||||||
|
return 0, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(strings.NewReader(out))
|
||||||
|
total := 0.0
|
||||||
|
lineIndex := 0
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := strings.TrimSpace(scanner.Text())
|
||||||
|
if line == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
lineIndex++
|
||||||
|
if lineIndex == 1 && (strings.Contains(strings.ToLower(line), "cpu") || strings.Contains(line, "%")) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
val, parseErr := strconv.ParseFloat(line, 64)
|
||||||
|
if parseErr != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
total += val
|
||||||
|
}
|
||||||
|
if scanErr := scanner.Err(); scanErr != nil {
|
||||||
|
return 0, nil, scanErr
|
||||||
|
}
|
||||||
|
|
||||||
|
maxTotal := float64(logical * 100)
|
||||||
|
if total < 0 {
|
||||||
|
total = 0
|
||||||
|
} else if total > maxTotal {
|
||||||
|
total = maxTotal
|
||||||
|
}
|
||||||
|
|
||||||
|
perCore := make([]float64, logical)
|
||||||
|
avg := total / float64(logical)
|
||||||
|
for i := range perCore {
|
||||||
|
perCore[i] = avg
|
||||||
|
}
|
||||||
|
return total, perCore, nil
|
||||||
|
}
|
||||||
|
|
||||||
func collectMemory() (MemoryStatus, error) {
|
func collectMemory() (MemoryStatus, error) {
|
||||||
vm, err := mem.VirtualMemory()
|
vm, err := mem.VirtualMemory()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
2
mole
2
mole
@@ -22,7 +22,7 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|||||||
source "$SCRIPT_DIR/lib/common.sh"
|
source "$SCRIPT_DIR/lib/common.sh"
|
||||||
|
|
||||||
# Version info
|
# Version info
|
||||||
VERSION="1.10.7"
|
VERSION="1.10.8"
|
||||||
MOLE_TAGLINE="can dig deep to clean your Mac."
|
MOLE_TAGLINE="can dig deep to clean your Mac."
|
||||||
|
|
||||||
# Check if Touch ID is already configured
|
# Check if Touch ID is already configured
|
||||||
|
|||||||
Reference in New Issue
Block a user