-
Notifications
You must be signed in to change notification settings - Fork 451
Expand file tree
/
Copy pathcpu.go
More file actions
39 lines (32 loc) · 1.27 KB
/
cpu.go
File metadata and controls
39 lines (32 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package cpu
import (
"os"
"runtime"
"github.com/shirou/gopsutil/v4/process"
)
// Use existing cpuCount from the package
var cpuCount = runtime.GOMAXPROCS(0)
// ProbeLoad checks if the current process CPU usage is below a threshold relative to available cores
// Returns true if process CPU usage is NOT too high
// Returns false if process is already under heavy CPU load
func ProbeLoad(maxLoadFactor float64) bool {
// Get the current process
pid := os.Getpid()
proc, err := process.NewProcess(int32(pid))
if err != nil {
return false // Error getting process, don't scale
}
// Get CPU percent for this process
// Note: this returns percent across all cores, so 100% per core * number of cores is the max
cpuPercent, err := proc.CPUPercent()
if err != nil {
return false // Error getting CPU usage, don't scale
}
// Calculate maximum CPU percentage allowed based on the factor and available cores
// For example, with 4 cores and maxLoadFactor of 0.7, maxAllowedPercent would be 280%
// (representing 70% utilization of all cores)
maxAllowedPercent := float64(cpuCount) * 100.0 * maxLoadFactor
// Return true if CPU usage is below threshold (scaling is recommended)
// Return false if CPU usage is already high (avoid scaling)
return cpuPercent < maxAllowedPercent
}