mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2025-10-13 11:39:13 +00:00
first try native CPU implementation
This commit is contained in:
parent
90c3529301
commit
e7cfee570b
1 changed files with 22 additions and 18 deletions
|
@ -110,6 +110,7 @@ type ServerService struct {
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
lastCPUTimes cpu.TimesStat
|
lastCPUTimes cpu.TimesStat
|
||||||
hasLastCPUSample bool
|
hasLastCPUSample bool
|
||||||
|
hasNativeCPUSample bool
|
||||||
emaCPU float64
|
emaCPU float64
|
||||||
cpuHistory []CPUSample
|
cpuHistory []CPUSample
|
||||||
cachedCpuSpeedMhz float64
|
cachedCpuSpeedMhz float64
|
||||||
|
@ -432,23 +433,27 @@ func (s *ServerService) AppendCpuSample(t time.Time, v float64) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ServerService) sampleCPUUtilization() (float64, error) {
|
func (s *ServerService) sampleCPUUtilization() (float64, error) {
|
||||||
// Prefer native Windows API to avoid external deps for CPU percent
|
// Try native platform-specific CPU implementation first (Windows, Linux, macOS)
|
||||||
if runtime.GOOS == "windows" {
|
if pct, err := sys.CPUPercentRaw(); err == nil {
|
||||||
if pct, err := sys.CPUPercentRaw(); err == nil {
|
s.mu.Lock()
|
||||||
s.mu.Lock()
|
// First call to native method returns 0 (initializes baseline)
|
||||||
// Smooth with EMA
|
if !s.hasNativeCPUSample {
|
||||||
const alpha = 0.3
|
s.hasNativeCPUSample = true
|
||||||
if s.emaCPU == 0 {
|
|
||||||
s.emaCPU = pct
|
|
||||||
} else {
|
|
||||||
s.emaCPU = alpha*pct + (1-alpha)*s.emaCPU
|
|
||||||
}
|
|
||||||
val := s.emaCPU
|
|
||||||
s.mu.Unlock()
|
s.mu.Unlock()
|
||||||
return val, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
// If native call fails, fall back to gopsutil times
|
// Smooth with EMA
|
||||||
|
const alpha = 0.3
|
||||||
|
if s.emaCPU == 0 {
|
||||||
|
s.emaCPU = pct
|
||||||
|
} else {
|
||||||
|
s.emaCPU = alpha*pct + (1-alpha)*s.emaCPU
|
||||||
|
}
|
||||||
|
val := s.emaCPU
|
||||||
|
s.mu.Unlock()
|
||||||
|
return val, nil
|
||||||
}
|
}
|
||||||
|
// If native call fails, fall back to gopsutil times
|
||||||
// Read aggregate CPU times (all CPUs combined)
|
// Read aggregate CPU times (all CPUs combined)
|
||||||
times, err := cpu.Times(false)
|
times, err := cpu.Times(false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -471,17 +476,16 @@ func (s *ServerService) sampleCPUUtilization() (float64, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compute busy and total deltas
|
// Compute busy and total deltas
|
||||||
|
// Note: Guest and GuestNice times are already included in User and Nice respectively,
|
||||||
|
// so we exclude them to avoid double-counting (Linux kernel accounting)
|
||||||
idleDelta := cur.Idle - s.lastCPUTimes.Idle
|
idleDelta := cur.Idle - s.lastCPUTimes.Idle
|
||||||
// Sum of busy deltas (exclude Idle)
|
|
||||||
busyDelta := (cur.User - s.lastCPUTimes.User) +
|
busyDelta := (cur.User - s.lastCPUTimes.User) +
|
||||||
(cur.System - s.lastCPUTimes.System) +
|
(cur.System - s.lastCPUTimes.System) +
|
||||||
(cur.Nice - s.lastCPUTimes.Nice) +
|
(cur.Nice - s.lastCPUTimes.Nice) +
|
||||||
(cur.Iowait - s.lastCPUTimes.Iowait) +
|
(cur.Iowait - s.lastCPUTimes.Iowait) +
|
||||||
(cur.Irq - s.lastCPUTimes.Irq) +
|
(cur.Irq - s.lastCPUTimes.Irq) +
|
||||||
(cur.Softirq - s.lastCPUTimes.Softirq) +
|
(cur.Softirq - s.lastCPUTimes.Softirq) +
|
||||||
(cur.Steal - s.lastCPUTimes.Steal) +
|
(cur.Steal - s.lastCPUTimes.Steal)
|
||||||
(cur.Guest - s.lastCPUTimes.Guest) +
|
|
||||||
(cur.GuestNice - s.lastCPUTimes.GuestNice)
|
|
||||||
|
|
||||||
totalDelta := busyDelta + idleDelta
|
totalDelta := busyDelta + idleDelta
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue