Fix: display online_cpus in compat REST API

Signed-off-by: Boaz Shuster <boaz.shuster.github@gmail.com>
This commit is contained in:
Boaz Shuster 2023-05-30 23:51:51 +03:00
parent c9c5cb2224
commit 5c7d50f08c
5 changed files with 42 additions and 1 deletions

View file

@ -47,3 +47,8 @@ func (c *Container) GetContainerStats(previousStats *define.ContainerStats) (*de
}
return stats, nil
}
// GetOnlineCPUs returns the number of online CPUs as set in the container cpu-set using sched_getaffinity
func GetOnlineCPUs(container *Container) (int, error) {
return getOnlineCPUs(container)
}

View file

@ -147,3 +147,7 @@ func calculateBlockIO(stats *cgroups.Metrics) (read uint64, write uint64) {
}
return
}
func getOnlineCPUs(container *Container) (int, error) {
return 0, nil
}

View file

@ -13,6 +13,7 @@ import (
"github.com/containers/common/pkg/cgroups"
"github.com/containers/podman/v4/libpod/define"
"golang.org/x/sys/unix"
)
// getPlatformContainerStats gets the platform-specific running stats
@ -129,3 +130,18 @@ func calculateBlockIO(stats *runccgroup.Stats) (read uint64, write uint64) {
}
return
}
func getOnlineCPUs(container *Container) (int, error) {
ctrPID, err := container.PID()
if err != nil {
return -1, fmt.Errorf("failed to obtain Container %s PID: %w", container.Name(), err)
}
if ctrPID == 0 {
return ctrPID, define.ErrCtrStopped
}
var cpuSet unix.CPUSet
if err := unix.SchedGetaffinity(ctrPID, &cpuSet); err != nil {
return -1, fmt.Errorf("failed to obtain Container %s online cpus: %w", container.Name(), err)
}
return cpuSet.Count(), nil
}

View file

@ -80,6 +80,11 @@ func StatsContainer(w http.ResponseWriter, r *http.Request) {
ThrottlingData: docker.ThrottlingData{},
}
}
onlineCPUs, err := libpod.GetOnlineCPUs(ctnr)
if err != nil {
utils.InternalServerError(w, err)
return
}
streamLabel: // A label to flatten the scope
select {
@ -178,7 +183,7 @@ streamLabel: // A label to flatten the scope
},
CPU: stats.CPU,
SystemUsage: systemUsage,
OnlineCPUs: uint32(len(cgroupStat.CpuStats.CpuUsage.PercpuUsage)),
OnlineCPUs: uint32(onlineCPUs),
ThrottlingData: docker.ThrottlingData{
Periods: 0,
ThrottledPeriods: 0,

11
test/apiv2/19-stats.at Normal file
View file

@ -0,0 +1,11 @@
# -*- sh -*-
#
# test 'stats' endpoints
#
if root; then
podman run -dt --name container1 --cpuset-cpus=0 $IMAGE top &>/dev/null
# regression for https://github.com/containers/podman/issues/15754
t GET libpod/containers/container1/stats?stream=false 200 .cpu_stats.online_cpus=1
fi