libpod: Implement getOnlineCPUs() on FreeBSD

Include an explicit container state check.  Otherwise the
containers/stats endpoint will return all-zero stats for a stopped
container even when in non-streaming mode, which breaks some consumers
of the API, particularly nomad's podman driver.

Implement the interface by just returning the number of host CPUs.  A
bit more sophisticated would be to fetch the jail's cpuset, but it's not
very important for now.

Signed-off-by: Mark Johnston <mark.johnston@klarasystems.com>
This commit is contained in:
Mark Johnston committed 2025-09-18 14:24:35 +00:00
1 parent 0215832852
commit 602ba415c6
1 file changed
+6 -1
+6 -1
View File
@@ -11,6 +11,7 @@ import (
"github.com/containers/podman/v5/pkg/rctl"
"github.com/sirupsen/logrus"
"go.podman.io/storage/pkg/system"
"golang.org/x/sys/unix"
)
// getPlatformContainerStats gets the platform-specific running stats
@@ -127,5 +128,9 @@ func calculateCPUPercent(currentCPU, previousCPU, now, previousSystem uint64) fl
}
func getOnlineCPUs(container *Container) (int, error) {
return 0, nil
if container.state.State != define.ContainerStateRunning {
return -1, fmt.Errorf("container %s is not running: %w", container.ID(), define.ErrCtrStopped)
}
n, err := unix.SysctlUint32("hw.ncpu")
return int(n), err
}