mirror of
https://github.com/containers/podman.git
synced 2025-06-27 21:50:18 +08:00
Merge pull request #5178 from marusak/expose_cpu
stats: Expose CPU usage in API
This commit is contained in:
@ -7,7 +7,6 @@ import (
|
|||||||
|
|
||||||
"github.com/containers/libpod/libpod"
|
"github.com/containers/libpod/libpod"
|
||||||
"github.com/containers/libpod/libpod/define"
|
"github.com/containers/libpod/libpod/define"
|
||||||
"github.com/containers/libpod/pkg/api/handlers"
|
|
||||||
"github.com/containers/libpod/pkg/api/handlers/utils"
|
"github.com/containers/libpod/pkg/api/handlers/utils"
|
||||||
"github.com/containers/libpod/pkg/cgroups"
|
"github.com/containers/libpod/pkg/cgroups"
|
||||||
docker "github.com/docker/docker/api/types"
|
docker "github.com/docker/docker/api/types"
|
||||||
@ -58,17 +57,18 @@ func StatsContainer(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var preRead time.Time
|
var preRead time.Time
|
||||||
var preCPUStats docker.CPUStats
|
var preCPUStats CPUStats
|
||||||
if query.Stream {
|
if query.Stream {
|
||||||
preRead = time.Now()
|
preRead = time.Now()
|
||||||
systemUsage, _ := cgroups.GetSystemCPUUsage()
|
systemUsage, _ := cgroups.GetSystemCPUUsage()
|
||||||
preCPUStats = docker.CPUStats{
|
preCPUStats = CPUStats{
|
||||||
CPUUsage: docker.CPUUsage{
|
CPUUsage: docker.CPUUsage{
|
||||||
TotalUsage: stats.CPUNano,
|
TotalUsage: stats.CPUNano,
|
||||||
PercpuUsage: stats.PerCPU,
|
PercpuUsage: stats.PerCPU,
|
||||||
UsageInKernelmode: stats.CPUSystemNano,
|
UsageInKernelmode: stats.CPUSystemNano,
|
||||||
UsageInUsermode: stats.CPUNano - stats.CPUSystemNano,
|
UsageInUsermode: stats.CPUNano - stats.CPUSystemNano,
|
||||||
},
|
},
|
||||||
|
CPU: stats.CPU,
|
||||||
SystemUsage: systemUsage,
|
SystemUsage: systemUsage,
|
||||||
OnlineCPUs: 0,
|
OnlineCPUs: 0,
|
||||||
ThrottlingData: docker.ThrottlingData{},
|
ThrottlingData: docker.ThrottlingData{},
|
||||||
@ -124,9 +124,8 @@ func StatsContainer(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
systemUsage, _ := cgroups.GetSystemCPUUsage()
|
systemUsage, _ := cgroups.GetSystemCPUUsage()
|
||||||
|
s := StatsJSON{
|
||||||
s := handlers.Stats{StatsJSON: docker.StatsJSON{
|
Stats: Stats{
|
||||||
Stats: docker.Stats{
|
|
||||||
Read: time.Now(),
|
Read: time.Now(),
|
||||||
PreRead: preRead,
|
PreRead: preRead,
|
||||||
PidsStats: docker.PidsStats{
|
PidsStats: docker.PidsStats{
|
||||||
@ -143,13 +142,14 @@ func StatsContainer(w http.ResponseWriter, r *http.Request) {
|
|||||||
IoTimeRecursive: nil,
|
IoTimeRecursive: nil,
|
||||||
SectorsRecursive: nil,
|
SectorsRecursive: nil,
|
||||||
},
|
},
|
||||||
CPUStats: docker.CPUStats{
|
CPUStats: CPUStats{
|
||||||
CPUUsage: docker.CPUUsage{
|
CPUUsage: docker.CPUUsage{
|
||||||
TotalUsage: cgroupStat.CPU.Usage.Total,
|
TotalUsage: cgroupStat.CPU.Usage.Total,
|
||||||
PercpuUsage: cgroupStat.CPU.Usage.PerCPU,
|
PercpuUsage: cgroupStat.CPU.Usage.PerCPU,
|
||||||
UsageInKernelmode: cgroupStat.CPU.Usage.Kernel,
|
UsageInKernelmode: cgroupStat.CPU.Usage.Kernel,
|
||||||
UsageInUsermode: cgroupStat.CPU.Usage.Total - cgroupStat.CPU.Usage.Kernel,
|
UsageInUsermode: cgroupStat.CPU.Usage.Total - cgroupStat.CPU.Usage.Kernel,
|
||||||
},
|
},
|
||||||
|
CPU: stats.CPU,
|
||||||
SystemUsage: systemUsage,
|
SystemUsage: systemUsage,
|
||||||
OnlineCPUs: uint32(len(cgroupStat.CPU.Usage.PerCPU)),
|
OnlineCPUs: uint32(len(cgroupStat.CPU.Usage.PerCPU)),
|
||||||
ThrottlingData: docker.ThrottlingData{
|
ThrottlingData: docker.ThrottlingData{
|
||||||
@ -173,7 +173,7 @@ func StatsContainer(w http.ResponseWriter, r *http.Request) {
|
|||||||
Name: stats.Name,
|
Name: stats.Name,
|
||||||
ID: stats.ContainerID,
|
ID: stats.ContainerID,
|
||||||
Networks: net,
|
Networks: net,
|
||||||
}}
|
}
|
||||||
|
|
||||||
utils.WriteJSON(w, http.StatusOK, s)
|
utils.WriteJSON(w, http.StatusOK, s)
|
||||||
if flusher, ok := w.(http.Flusher); ok {
|
if flusher, ok := w.(http.Flusher); ok {
|
||||||
|
55
pkg/api/handlers/generic/types.go
Normal file
55
pkg/api/handlers/generic/types.go
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
package generic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
docker "github.com/docker/docker/api/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CPUStats aggregates and wraps all CPU related info of container
|
||||||
|
type CPUStats struct {
|
||||||
|
// CPU Usage. Linux and Windows.
|
||||||
|
CPUUsage docker.CPUUsage `json:"cpu_usage"`
|
||||||
|
|
||||||
|
// System Usage. Linux only.
|
||||||
|
SystemUsage uint64 `json:"system_cpu_usage,omitempty"`
|
||||||
|
|
||||||
|
// Online CPUs. Linux only.
|
||||||
|
OnlineCPUs uint32 `json:"online_cpus,omitempty"`
|
||||||
|
|
||||||
|
// Usage of CPU in %. Linux only.
|
||||||
|
CPU float64 `json:"cpu"`
|
||||||
|
|
||||||
|
// Throttling Data. Linux only.
|
||||||
|
ThrottlingData docker.ThrottlingData `json:"throttling_data,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stats is Ultimate struct aggregating all types of stats of one container
|
||||||
|
type Stats struct {
|
||||||
|
// Common stats
|
||||||
|
Read time.Time `json:"read"`
|
||||||
|
PreRead time.Time `json:"preread"`
|
||||||
|
|
||||||
|
// Linux specific stats, not populated on Windows.
|
||||||
|
PidsStats docker.PidsStats `json:"pids_stats,omitempty"`
|
||||||
|
BlkioStats docker.BlkioStats `json:"blkio_stats,omitempty"`
|
||||||
|
|
||||||
|
// Windows specific stats, not populated on Linux.
|
||||||
|
NumProcs uint32 `json:"num_procs"`
|
||||||
|
StorageStats docker.StorageStats `json:"storage_stats,omitempty"`
|
||||||
|
|
||||||
|
// Shared stats
|
||||||
|
CPUStats CPUStats `json:"cpu_stats,omitempty"`
|
||||||
|
PreCPUStats CPUStats `json:"precpu_stats,omitempty"` // "Pre"="Previous"
|
||||||
|
MemoryStats docker.MemoryStats `json:"memory_stats,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type StatsJSON struct {
|
||||||
|
Stats
|
||||||
|
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
ID string `json:"id,omitempty"`
|
||||||
|
|
||||||
|
// Networks request version >=1.21
|
||||||
|
Networks map[string]docker.NetworkStats `json:"networks,omitempty"`
|
||||||
|
}
|
@ -78,10 +78,6 @@ type Container struct {
|
|||||||
docker.ContainerCreateConfig
|
docker.ContainerCreateConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
type ContainerStats struct {
|
|
||||||
docker.ContainerStats
|
|
||||||
}
|
|
||||||
|
|
||||||
type Version struct {
|
type Version struct {
|
||||||
docker.Version
|
docker.Version
|
||||||
}
|
}
|
||||||
@ -143,10 +139,6 @@ type IDResponse struct {
|
|||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Stats struct {
|
|
||||||
docker.StatsJSON
|
|
||||||
}
|
|
||||||
|
|
||||||
type ContainerTopOKBody struct {
|
type ContainerTopOKBody struct {
|
||||||
dockerContainer.ContainerTopOKBody
|
dockerContainer.ContainerTopOKBody
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user