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

Add docker stats --no-stream show cpu usage #13320

Merged

Conversation

coolljt0725
Copy link
Contributor

Signed-off-by: Lei Jitang leijitang@huawei.com

The CPU % of docker stats --no-stream is always 0.0
even though the real time cpu usage is not zero of the container.
It's quite weird or misleading when user what use docker stats --no-stream
to get the cpu usage of the container. This PR add docker stats --no-stream to
show the cpu usage.

Before:

[l00284783@localhost ~]$ docker stats --no-stream abc
CONTAINER           CPU %               MEM USAGE/LIMIT     MEM %               NET I/O
abc                 0.00%               196.6 kB/4.143 GB   0.00%               734 B/258 B

After:

[l00284783@localhost ~]$ docker stats --no-stream abc
CONTAINER           CPU %               MEM USAGE/LIMIT     MEM %               NET I/O
abc                 5.07%               200.7 kB/4.143 GB   0.00%               664 B/258 B

ping @cpuguy83

@coolljt0725 coolljt0725 force-pushed the add_stats_no_stream_show_cpu_usage branch 2 times, most recently from 7518062 to bb6e67f Compare May 19, 2015 12:42
@@ -29,6 +29,37 @@ type containerStats struct {
}

func (s *containerStats) Collect(cli *DockerCli, streamStats bool) {
first_v := url.Values{}
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if this should be on daemon side.

@coolljt0725 coolljt0725 force-pushed the add_stats_no_stream_show_cpu_usage branch from bb6e67f to 7077d91 Compare May 20, 2015 08:46
@coolljt0725
Copy link
Contributor Author

@LK4D4 Update, move get first stats to daemon side. WDYT?

@cpuguy83
Copy link
Member

This could use a test

@cpuguy83
Copy link
Member

Confirmed this fixes the issue.

@LK4D4
Copy link
Contributor

LK4D4 commented May 29, 2015

Code okay for me. Test would be nice.

@coolljt0725
Copy link
Contributor Author

I'll add test later

Signed-off-by: Lei Jitang <leijitang@huawei.com>
@coolljt0725 coolljt0725 force-pushed the add_stats_no_stream_show_cpu_usage branch from 7077d91 to 8b58860 Compare June 1, 2015 13:49
)

func (s *DockerSuite) TestCliStatsNoStreamGetCpu(c *check.C) {
dockerFile := fmt.Sprintf("FROM busybox\nRUN echo \"#/bin/sh\" > /bin/test.sh && echo \"while true;do\" >> /bin/test.sh && echo \"echo \"Hello world\"\" >> /bin/test.sh && echo \"done\" >> /bin/test.sh\nRUN chmod +x /bin/test.sh\nCMD /bin/test.sh")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this will generate a Dockerfile like this

FROM busybox
RUN echo "#/bin/sh" > /bin/test.sh && echo "while true;do" >> /bin/test.sh && echo "echo "Hello world"" >> /bin/test.sh && echo "done" >> /bin/test.sh
RUN chmod +x /bin/test.sh
CMD /bin/test.sh

and the test.sh is like this

#/bin/sh
while true;do
echo Hello world
done

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the test, but some notes here:
I would prefer to not build an image since it is unnecessary to perform the test, and adds a significant amount of time to the actual test.

out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "while true; do echo 'hello'; sleep 0.01; done")

That ought to be sufficient to create the required test and also not completely saturate the cpu.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is much more simple, thanks, I'll update

@coolljt0725
Copy link
Contributor Author

ping @cpuguy83 @LK4D4 test is add

@coolljt0725 coolljt0725 force-pushed the add_stats_no_stream_show_cpu_usage branch from 8b58860 to 96123a1 Compare June 1, 2015 15:07
@calavera
Copy link
Contributor

calavera commented Jun 1, 2015

code LGTM. I just wonder if the test can be written without streaming the stats, it might be more easy to understand, but definitely not blocking.

@cpuguy83
Copy link
Member

cpuguy83 commented Jun 1, 2015

LGTM

cpuguy83 added a commit that referenced this pull request Jun 1, 2015
…pu_usage

Add docker stats --no-stream show cpu usage
@cpuguy83 cpuguy83 merged commit a7005c4 into moby:master Jun 1, 2015
@cpuguy83
Copy link
Member

cpuguy83 commented Jun 1, 2015

ping @jfrazelle for cherry-pick since this is broken as is in 1.7

@jessfraz
Copy link
Contributor

jessfraz commented Jun 1, 2015

cherry-picked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants