1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-14 20:32:28 +00:00

Make the analysis state more accurate

This commit is contained in:
Tw93
2025-11-24 11:48:37 +08:00
parent 12713dd1f9
commit f60580d679
3 changed files with 55 additions and 35 deletions

Binary file not shown.

View File

@@ -67,13 +67,14 @@ type ProcessInfo struct {
} }
type CPUStatus struct { type CPUStatus struct {
Usage float64 Usage float64
PerCore []float64 PerCore []float64
Load1 float64 PerCoreEstimated bool
Load5 float64 Load1 float64
Load15 float64 Load5 float64
CoreCount int Load15 float64
LogicalCPU int CoreCount int
LogicalCPU int
} }
type GPUStatus struct { type GPUStatus struct {
@@ -381,6 +382,7 @@ func collectCPU() (CPUStatus, error) {
percents, err := cpu.Percent(cpuSampleInterval, true) percents, err := cpu.Percent(cpuSampleInterval, true)
var totalPercent float64 var totalPercent float64
perCoreEstimated := false
if err != nil || len(percents) == 0 { if err != nil || len(percents) == 0 {
fallbackUsage, fallbackPerCore, fallbackErr := fallbackCPUUtilization(logical) fallbackUsage, fallbackPerCore, fallbackErr := fallbackCPUUtilization(logical)
if fallbackErr != nil { if fallbackErr != nil {
@@ -391,6 +393,7 @@ func collectCPU() (CPUStatus, error) {
} }
totalPercent = fallbackUsage totalPercent = fallbackUsage
percents = fallbackPerCore percents = fallbackPerCore
perCoreEstimated = true
} else { } else {
for _, v := range percents { for _, v := range percents {
totalPercent += v totalPercent += v
@@ -410,13 +413,14 @@ func collectCPU() (CPUStatus, error) {
} }
return CPUStatus{ return CPUStatus{
Usage: totalPercent, Usage: totalPercent,
PerCore: percents, PerCore: percents,
Load1: loadAvg.Load1, PerCoreEstimated: perCoreEstimated,
Load5: loadAvg.Load5, Load1: loadAvg.Load1,
Load15: loadAvg.Load15, Load5: loadAvg.Load5,
CoreCount: counts, Load15: loadAvg.Load15,
LogicalCPU: logical, CoreCount: counts,
LogicalCPU: logical,
}, nil }, nil
} }
@@ -597,7 +601,11 @@ func collectDisks() ([]DiskStatus, error) {
if strings.HasPrefix(part.Mountpoint, "/private/") { if strings.HasPrefix(part.Mountpoint, "/private/") {
continue continue
} }
if seenDevice[part.Device] { baseDevice := baseDeviceName(part.Device)
if baseDevice == "" {
baseDevice = part.Device
}
if seenDevice[baseDevice] {
continue continue
} }
usage, err := disk.Usage(part.Mountpoint) usage, err := disk.Usage(part.Mountpoint)
@@ -622,7 +630,7 @@ func collectDisks() ([]DiskStatus, error) {
UsedPercent: usage.UsedPercent, UsedPercent: usage.UsedPercent,
Fstype: part.Fstype, Fstype: part.Fstype,
}) })
seenDevice[part.Device] = true seenDevice[baseDevice] = true
seenVolume[volKey] = true seenVolume[volKey] = true
} }

View File

@@ -188,24 +188,28 @@ func renderCPUCard(cpu CPUStatus) cardData {
lines = append(lines, fmt.Sprintf("Total %s %5.1f%%", progressBar(cpu.Usage), cpu.Usage)) lines = append(lines, fmt.Sprintf("Total %s %5.1f%%", progressBar(cpu.Usage), cpu.Usage))
lines = append(lines, subtleStyle.Render(fmt.Sprintf("%.2f / %.2f / %.2f (%d cores)", cpu.Load1, cpu.Load5, cpu.Load15, cpu.LogicalCPU))) lines = append(lines, subtleStyle.Render(fmt.Sprintf("%.2f / %.2f / %.2f (%d cores)", cpu.Load1, cpu.Load5, cpu.Load15, cpu.LogicalCPU)))
// Show top 3 busiest cores if cpu.PerCoreEstimated {
type coreUsage struct { lines = append(lines, subtleStyle.Render("Per-core data unavailable (using averaged load)"))
idx int } else if len(cpu.PerCore) > 0 {
val float64 // Show top 3 busiest cores
} type coreUsage struct {
var cores []coreUsage idx int
for i, v := range cpu.PerCore { val float64
cores = append(cores, coreUsage{i, v}) }
} var cores []coreUsage
sort.Slice(cores, func(i, j int) bool { return cores[i].val > cores[j].val }) for i, v := range cpu.PerCore {
cores = append(cores, coreUsage{i, v})
}
sort.Slice(cores, func(i, j int) bool { return cores[i].val > cores[j].val })
maxCores := 3 maxCores := 3
if len(cores) < maxCores { if len(cores) < maxCores {
maxCores = len(cores) maxCores = len(cores)
} }
for i := 0; i < maxCores; i++ { for i := 0; i < maxCores; i++ {
c := cores[i] c := cores[i]
lines = append(lines, fmt.Sprintf("Core%-2d %s %5.1f%%", c.idx+1, progressBar(c.val), c.val)) lines = append(lines, fmt.Sprintf("Core%-2d %s %5.1f%%", c.idx+1, progressBar(c.val), c.val))
}
} }
return cardData{icon: iconCPU, title: "CPU", lines: lines} return cardData{icon: iconCPU, title: "CPU", lines: lines}
@@ -232,12 +236,20 @@ func renderMemoryCard(mem MemoryStatus) cardData {
var lines []string var lines []string
lines = append(lines, fmt.Sprintf("Used %s %5.1f%%", progressBar(mem.UsedPercent), mem.UsedPercent)) lines = append(lines, fmt.Sprintf("Used %s %5.1f%%", progressBar(mem.UsedPercent), mem.UsedPercent))
lines = append(lines, subtleStyle.Render(fmt.Sprintf("%s / %s total", humanBytes(mem.Used), humanBytes(mem.Total)))) lines = append(lines, subtleStyle.Render(fmt.Sprintf("%s / %s total", humanBytes(mem.Used), humanBytes(mem.Total))))
lines = append(lines, "")
// Show available memory
available := mem.Total - mem.Used available := mem.Total - mem.Used
freePercent := 100 - mem.UsedPercent freePercent := 100 - mem.UsedPercent
lines = append(lines, fmt.Sprintf("Free %s %5.1f%%", progressBar(freePercent), freePercent)) lines = append(lines, fmt.Sprintf("Free %s %5.1f%%", progressBar(freePercent), freePercent))
lines = append(lines, subtleStyle.Render(fmt.Sprintf("%s available", humanBytes(available)))) lines = append(lines, subtleStyle.Render(fmt.Sprintf("%s available", humanBytes(available))))
if mem.SwapTotal > 0 || mem.SwapUsed > 0 {
var swapPercent float64
if mem.SwapTotal > 0 {
swapPercent = (float64(mem.SwapUsed) / float64(mem.SwapTotal)) * 100.0
}
swapText := subtleStyle.Render(fmt.Sprintf("%s / %s swap", humanBytes(mem.SwapUsed), humanBytes(mem.SwapTotal)))
lines = append(lines, fmt.Sprintf("Swap %s %5.1f%% %s", progressBar(swapPercent), swapPercent, swapText))
} else {
lines = append(lines, fmt.Sprintf("Swap %s", subtleStyle.Render("not in use")))
}
// Memory pressure // Memory pressure
if mem.Pressure != "" { if mem.Pressure != "" {
pressureStyle := okStyle pressureStyle := okStyle