-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Conversation
PR does not include changes in the 'tests' directory |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Please add a test and a description of what the change fixes.
@boaz0 could you please add a test for your PR? It is failing because no test was added. |
and also a proper commit message |
Sure, I'll be working on it today and tomorrow. Sorry for the late response. |
@edsantiago can you review the test I added.
|
@boaz0 this looks like it belongs in There's a big red kind/api-change button on this PR. |
@containers/podman-maintainers CI is green. Test LGTM (fails on |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like online_cpus are now displayed! I don't know how to confirm that the number is correct though, since it looks like it always displays 0?
@benoitf does this look correct? Do we have an example where we expect online_cpus to be non-zero?
curl --unix-socket /run/user/1000/podman/podman.sock http:/v1.41/containers/$myContainer/stats?stream=false | jq .cpu_stats
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1105 0 1105 0 0 73829 0 --:--:-- --:--:-- --:--:-- 78928
{
"cpu_usage": {
"total_usage": 67664000,
"usage_in_kernelmode": 50612,
"usage_in_usermode": 67613388
},
"system_cpu_usage": 53967013582000,
"online_cpus": 0,
"cpu": 0,
"throttling_data": {
"periods": 0,
"throttled_periods": 0,
"throttled_time": 0
}
}
@Luap99 @mheon @ashley-cui Does this break API compatibility? |
@ashley-cui it should return the number of CPUs of the machine for example Here is the output with docker if the 'docker vm' has 3 CPUs
by default, podman machine have 2 cpus assigned so it should return 2 instead of 0 |
@benoitf @ashley-cui the number of CPUs is read from the cgroup file inside the container. I can look more and see what went wrong. |
Thanks @boaz0! |
@giuseppe Would you happen to know why the CPUs is always displayed as 0? |
I think this might need |
@flouthoc |
@lukasmrtvy Yes my suggestion for v2 and where |
@giuseppe PTAL ^^ |
Could we use Something like: package main
import (
"fmt"
"golang.org/x/sys/unix"
)
func getNumOnlineCPUs() (int, error) {
var cpuSet unix.CPUSet
if err := unix.SchedGetaffinity(0, &cpuSet); err != nil {
return -1, err
}
return cpuSet.Count(), nil
}
func main(){
n, err := getNumOnlineCPUs()
if err != nil {
panic(err)
}
fmt.Printf("Online CPUs: %d\n", n)
} with the code above I have:
It doesn't require any privilege to run |
@giuseppe I am probably wrong but ./bin/podman run --cpus 2 busybox sh -c 'while true;do sleep 1s; echo "hello";done' tells me I have 8 online_cpus, I was hoping to have 2. ./bin/podman run --cpuset-cpus 0,3 busybox sh -c 'while true;do sleep 1s; echo "hello";done' but got the same results. This is the code: func StatsContainer(w http.ResponseWriter, r *http.Request) {
...
onlineCPUs, err := libpod.GetOnlineCPUs(0)
if err != nil {
logrus.Errorf("Unable to get online cpus: %v", err)
}
}
func GetOnlineCPUs(pid uint64) (int, error) {
var cpuSet unix.CPUSet
if err := unix.SchedGetaffinity(int(pid), &cpuSet); err != nil {
return -1, err
}
return cpuSet.Count(), nil
}
`` |
you'll need to replace the PID here: |
@rhatdan @vrothberg can you run CI? I did some changes, I hope it will make CI green again. |
Tests aren't happy yet |
|
Thanks @edsantiago |
LGTM but I'm not comfortable approving it. I did confirm that the new test fails on main (i.e. that it tests the new functionality). |
One comment from me, otherwise LGTM |
Thanks @mheon. Updated the code. |
@boaz0 please squash your commits as a courtesy to future maintainers, thank you |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And as @edsantiago said please squash the commits.
@@ -80,6 +80,10 @@ func StatsContainer(w http.ResponseWriter, r *http.Request) { | |||
ThrottlingData: docker.ThrottlingData{}, | |||
} | |||
} | |||
onlineCPUs, err := libpod.GetOnlineCPUs(ctnr) | |||
if err != nil { | |||
utils.InternalServerError(w, err) |
There was a problem hiding this comment.
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.
if err != nil { | ||
return -1, fmt.Errorf("failed to obtain Container %s PID: %w", container.Name(), err) | ||
} |
There was a problem hiding this comment.
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
test/apiv2/19-stats.at
Outdated
# | ||
|
||
if root; then | ||
podman pull $IMAGE &>/dev/null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the pull is not necessary, podman run will pull if required.
Signed-off-by: Boaz Shuster <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: boaz0, Luap99 The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
LGTM |
/lgtm |
Thanks for the contribution @boaz0 |
closes #15754
Does this PR introduce a user-facing change?