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

@ -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
}