mirror of https://github.com/containers/podman.git
libpod: Move getCPUUtilization to info_linux.go
The Linux implementation uses /proc/stat - the FreeBSD equivalent is quite different where this information is exposed via sysctl. [NO NEW TESTS NEEDED] Signed-off-by: Doug Rabson <dfr@rabson.org>
This commit is contained in:
parent
694cbaca37
commit
ff20c74e97
|
|
@ -11,7 +11,6 @@ import (
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -294,43 +293,3 @@ func (r *Runtime) GetHostDistributionInfo() define.DistributionInfo {
|
||||||
}
|
}
|
||||||
return dist
|
return dist
|
||||||
}
|
}
|
||||||
|
|
||||||
// getCPUUtilization Returns a CPUUsage object that summarizes CPU
|
|
||||||
// usage for userspace, system, and idle time.
|
|
||||||
func getCPUUtilization() (*define.CPUUsage, error) {
|
|
||||||
f, err := os.Open("/proc/stat")
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
scanner := bufio.NewScanner(f)
|
|
||||||
// Read first line of /proc/stat that has entries for system ("cpu" line)
|
|
||||||
for scanner.Scan() {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
// column 1 is user, column 3 is system, column 4 is idle
|
|
||||||
stats := strings.Fields(scanner.Text())
|
|
||||||
return statToPercent(stats)
|
|
||||||
}
|
|
||||||
|
|
||||||
func statToPercent(stats []string) (*define.CPUUsage, error) {
|
|
||||||
userTotal, err := strconv.ParseFloat(stats[1], 64)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("unable to parse user value %q: %w", stats[1], err)
|
|
||||||
}
|
|
||||||
systemTotal, err := strconv.ParseFloat(stats[3], 64)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("unable to parse system value %q: %w", stats[3], err)
|
|
||||||
}
|
|
||||||
idleTotal, err := strconv.ParseFloat(stats[4], 64)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("unable to parse idle value %q: %w", stats[4], err)
|
|
||||||
}
|
|
||||||
total := userTotal + systemTotal + idleTotal
|
|
||||||
s := define.CPUUsage{
|
|
||||||
UserPercent: math.Round((userTotal/total*100)*100) / 100,
|
|
||||||
SystemPercent: math.Round((systemTotal/total*100)*100) / 100,
|
|
||||||
IdlePercent: math.Round((idleTotal/total*100)*100) / 100,
|
|
||||||
}
|
|
||||||
return &s, nil
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,12 @@
|
||||||
package libpod
|
package libpod
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/containers/common/pkg/apparmor"
|
"github.com/containers/common/pkg/apparmor"
|
||||||
|
|
@ -86,3 +90,43 @@ func (r *Runtime) setPlatformHostInfo(info *define.HostInfo) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func statToPercent(stats []string) (*define.CPUUsage, error) {
|
||||||
|
userTotal, err := strconv.ParseFloat(stats[1], 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to parse user value %q: %w", stats[1], err)
|
||||||
|
}
|
||||||
|
systemTotal, err := strconv.ParseFloat(stats[3], 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to parse system value %q: %w", stats[3], err)
|
||||||
|
}
|
||||||
|
idleTotal, err := strconv.ParseFloat(stats[4], 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to parse idle value %q: %w", stats[4], err)
|
||||||
|
}
|
||||||
|
total := userTotal + systemTotal + idleTotal
|
||||||
|
s := define.CPUUsage{
|
||||||
|
UserPercent: math.Round((userTotal/total*100)*100) / 100,
|
||||||
|
SystemPercent: math.Round((systemTotal/total*100)*100) / 100,
|
||||||
|
IdlePercent: math.Round((idleTotal/total*100)*100) / 100,
|
||||||
|
}
|
||||||
|
return &s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getCPUUtilization Returns a CPUUsage object that summarizes CPU
|
||||||
|
// usage for userspace, system, and idle time.
|
||||||
|
func getCPUUtilization() (*define.CPUUsage, error) {
|
||||||
|
f, err := os.Open("/proc/stat")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
scanner := bufio.NewScanner(f)
|
||||||
|
// Read first line of /proc/stat that has entries for system ("cpu" line)
|
||||||
|
for scanner.Scan() {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// column 1 is user, column 3 is system, column 4 is idle
|
||||||
|
stats := strings.Fields(scanner.Text())
|
||||||
|
return statToPercent(stats)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue