1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-15 18:05:05 +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

@@ -69,6 +69,7 @@ type ProcessInfo struct {
type CPUStatus struct { type CPUStatus struct {
Usage float64 Usage float64
PerCore []float64 PerCore []float64
PerCoreEstimated bool
Load1 float64 Load1 float64
Load5 float64 Load5 float64
Load15 float64 Load15 float64
@@ -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
@@ -412,6 +415,7 @@ func collectCPU() (CPUStatus, error) {
return CPUStatus{ return CPUStatus{
Usage: totalPercent, Usage: totalPercent,
PerCore: percents, PerCore: percents,
PerCoreEstimated: perCoreEstimated,
Load1: loadAvg.Load1, Load1: loadAvg.Load1,
Load5: loadAvg.Load5, Load5: loadAvg.Load5,
Load15: loadAvg.Load15, Load15: loadAvg.Load15,
@@ -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,6 +188,9 @@ 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)))
if cpu.PerCoreEstimated {
lines = append(lines, subtleStyle.Render("Per-core data unavailable (using averaged load)"))
} else if len(cpu.PerCore) > 0 {
// Show top 3 busiest cores // Show top 3 busiest cores
type coreUsage struct { type coreUsage struct {
idx int idx int
@@ -207,6 +210,7 @@ func renderCPUCard(cpu CPUStatus) cardData {
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