aboutsummaryrefslogtreecommitdiff
path: root/libpod
diff options
context:
space:
mode:
authorDoug Rabson <dfr@rabson.org>2022-08-19 11:16:23 +0100
committerDoug Rabson <dfr@rabson.org>2022-08-24 10:55:52 +0100
commitff20c74e973abd7090b1ef76bc0d4de420513d34 (patch)
tree93a10b81c49aa4eb198a089c6aa1b40f9bd2f741 /libpod
parent694cbaca3745ca8fab8665d8ba88af6056dc16f8 (diff)
downloadpodman-ff20c74e973abd7090b1ef76bc0d4de420513d34.tar.gz
podman-ff20c74e973abd7090b1ef76bc0d4de420513d34.tar.bz2
podman-ff20c74e973abd7090b1ef76bc0d4de420513d34.zip
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>
Diffstat (limited to 'libpod')
-rw-r--r--libpod/info.go41
-rw-r--r--libpod/info_linux.go44
2 files changed, 44 insertions, 41 deletions
diff --git a/libpod/info.go b/libpod/info.go
index cdf453135..20ea007cf 100644
--- a/libpod/info.go
+++ b/libpod/info.go
@@ -11,7 +11,6 @@ import (
"math"
"os"
"runtime"
- "strconv"
"strings"
"syscall"
"time"
@@ -294,43 +293,3 @@ func (r *Runtime) GetHostDistributionInfo() define.DistributionInfo {
}
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
-}
diff --git a/libpod/info_linux.go b/libpod/info_linux.go
index c906cae86..801dcdb43 100644
--- a/libpod/info_linux.go
+++ b/libpod/info_linux.go
@@ -1,8 +1,12 @@
package libpod
import (
+ "bufio"
"fmt"
+ "math"
+ "os"
"os/exec"
+ "strconv"
"strings"
"github.com/containers/common/pkg/apparmor"
@@ -86,3 +90,43 @@ func (r *Runtime) setPlatformHostInfo(info *define.HostInfo) error {
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)
+}