Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: display online_cpus in compat REST API #15867

Merged
merged 1 commit into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
mheon marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return -1, fmt.Errorf("failed to obtain Container %s PID: %w", container.Name(), err)
}
Comment on lines +136 to +138
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the err != nil branch should come before pid == 0

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs a return after the InternalServerError call otherwise we just continue which will result in incorrect REST API responses.

return
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to move this logic into GetOnlineCPUs in stats_common.go and then call that down below? Or perhaps change this to a function in container_stats.go and call it from below. It just seems like a lot of processing goes on from the time that onlineCPUs is set to when it's used.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure no problem.


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