Skip to content

Commit

Permalink
Fix: display online_cpus in compat REST API
Browse files Browse the repository at this point in the history
Signed-off-by: Boaz Shuster <[email protected]>
  • Loading branch information
boaz0 committed May 31, 2023
1 parent c9c5cb2 commit 5c7d50f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
5 changes: 5 additions & 0 deletions libpod/stats_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions libpod/stats_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,7 @@ func calculateBlockIO(stats *cgroups.Metrics) (read uint64, write uint64) {
}
return
}

func getOnlineCPUs(container *Container) (int, error) {
return 0, nil
}
16 changes: 16 additions & 0 deletions libpod/stats_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
7 changes: 6 additions & 1 deletion pkg/api/handlers/compat/containers_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions test/apiv2/19-stats.at
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5c7d50f

Please sign in to comment.