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

How to compute CPU usage based on the info of "docker stats"? #29306

Closed
ling-pan opened this issue Dec 11, 2016 · 13 comments

Comments

Projects
None yet
5 participants
@ling-pan
Copy link

commented Dec 11, 2016

Hi. I use the "/containers/(id)/stats" API to collect the information of a container.
What I get is as below:

{"read":"2016-12-11T02:18:01.857174651Z","preread":"0001-01-01T00:00:00Z","pids_stats":{},"blkio_stats":{"io_service_bytes_recursive":[],"io_serviced_recursive":[],"io_queue_recursive":[],"io_service_time_recursive":[],"io_wait_time_recursive":[],"io_merged_recursive":[],"io_time_recursive":[],"sectors_recursive":[]},"num_procs":0,"storage_stats":{},"cpu_stats":{"cpu_usage":{"total_usage":723671739199,"percpu_usage":[158849409169,105266278971,265007316045,194548735014],"usage_in_kernelmode":1360000000,"usage_in_usermode":724010000000},"system_cpu_usage":23733220000000,"throttling_data":{"periods":0,"throttled_periods":0,"throttled_time":0}},"precpu_stats":{"cpu_usage":{"total_usage":0,"usage_in_kernelmode":0,"usage_in_usermode":0},"throttling_data":{"periods":0,"throttled_periods":0,"throttled_time":0}},"memory_stats":{"usage":856064,"max_usage":2486272,"stats":{"active_anon":610304,"active_file":0,"cache":0,"dirty":0,"hierarchical_memory_limit":8796093018112,"inactive_anon":0,"inactive_file":0,"mapped_file":0,"pgfault":1700,"pgmajfault":0,"pgpgin":836,"pgpgout":687,"rss":610304,"rss_huge":0,"total_active_anon":610304,"total_active_file":0,"total_cache":0,"total_dirty":0,"total_inactive_anon":0,"total_inactive_file":0,"total_mapped_file":0,"total_pgfault":1700,"total_pgmajfault":0,"total_pgpgin":836,"total_pgpgout":687,"total_rss":610304,"total_rss_huge":0,"total_unevictable":0,"total_writeback":0,"unevictable":0,"writeback":0},"limit":970485760},"name":"/ser2.1.8be6xmrx7mshozj5suaxdzl7a","id":"9e84fd9c570c2952f6ef272e20af1d639a8e0cb80ca33c6679a42b9cf59d9a4d","networks":{"eth0":{"rx_bytes":30954,"rx_packets":120,"rx_errors":0,"rx_dropped":2,"tx_bytes":906,"tx_packets":11,"tx_errors":0,"tx_dropped":0}}}

By the command "docker stats", what I get is as below:

CONTAINER           CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
9e84fd9c570c        57.69%              836 KiB / 925.5 MiB   0.09%               31.7 kB / 906 B     0 B / 0 B           0

I wonder how to calculate the CPU usage in percentage based on the above information.

Thanks a lot.

@ling-pan ling-pan closed this Dec 11, 2016

@ling-pan ling-pan reopened this Dec 11, 2016

@thaJeztah

This comment has been minimized.

Copy link
Member

commented Dec 11, 2016

You can see the calculation used here; https://github.com/docker/docker/blob/eb131c5383db8cac633919f82abad86c99bffbe5/cli/command/container/stats_helpers.go#L175

To find how the JSON properties are mapped to the properties used in that function, look here; https://github.com/docker/docker/blob/801230ce315ef51425da53cc5712eb6063deee95/api/types/stats.go#L164

@ling-pan

This comment has been minimized.

Copy link
Author

commented Dec 12, 2016

Thank you very much. It really helps.

@ling-pan ling-pan closed this Dec 12, 2016

@thaJeztah

This comment has been minimized.

Copy link
Member

commented Dec 12, 2016

you're welcome!

@getvivekv

This comment has been minimized.

Copy link

commented Aug 22, 2017

@thaJeztah The api returns the cpu % for a second. Is there any way to find the average cpu stats of a container for 1 minute without hitting the api 60 times a minute? (even hitting 60 times a minute wouldn't get me 60 results as sometimes one request might take more than 1 seconds to process)

@Vacant0mens

This comment has been minimized.

Copy link

commented Sep 12, 2017

@thaJeztah Is the cpu_usage.system_cpu_usage stat available in Windows/EE? I don't see it in any of my API responses. If not, how do I calculate container usage?

@thaJeztah

This comment has been minimized.

Copy link
Member

commented Sep 12, 2017

@Vacant0mens information on the Windows platform may be slightly different, but you can find the calculation for Windows here; https://github.com/docker/cli/blob/5c5cdd0e3665f9dfa32eb2f0de136c262b811803/cli/command/container/stats_helpers.go#L187-L201

@Vacant0mens

This comment has been minimized.

Copy link

commented Sep 12, 2017

Thanks @thaJeztah , that's exactly what I need!
The only problem is that I don't know Go. I think I got most of it, but could you explain what's happening with this line: possIntervals := uint64(v.Read.Sub(v.PreRead).Nanoseconds())?

It looks like it's taking the read stat, pulling a substring, then converting the substring to nanoseconds. Is that right?
or is it subtracting preread from read?

Also, is dividing by 100, then multiplying by 100 necessary? Seems redundant. (lines 193 and 201)

@thaJeztah

This comment has been minimized.

Copy link
Member

commented Sep 12, 2017

I'm not entirely familiar with the Windows calculation, but perhaps @johnstep or @jhowardmsft can assist 😊

@thoniTUB

This comment has been minimized.

Copy link

commented Jul 16, 2018

Hey @thaJeztah, I was wondering why you were multiplying the ratio of the CPU usage by the number of CPUs in

cpuPercent = (cpuDelta / systemDelta) * float64(len(v.CPUStats.CPUUsage.PercpuUsage)) * 100.0

Aren't cpuDelta and systemDelta taking these into account?

@thaJeztah

This comment has been minimized.

Copy link
Member

commented Jul 16, 2018

If I'm not mistaken, it's so that (e.g.) 100% utilisation on 4 cores/cpus shows as 400%

@Vacant0mens

This comment has been minimized.

Copy link

commented Jul 16, 2018

Wouldn't it be more helpful to show usage by total percentage of overall resources? ... I mean, you wouldn't divide memory usage into 800% if you were running with 8gb of ram, especially if a container was using more than 1gb by design.

Maybe I'm missing the purpose of doing 100% per core, rather than 100% overall.

@thaJeztah

This comment has been minimized.

Copy link
Member

commented Jul 16, 2018

That's not really something that can be changed easily, as it will break many users; this output was modelled after how top works on Linux; https://unix.stackexchange.com/questions/34435/top-output-cpu-usage-100

@thoniTUB

This comment has been minimized.

Copy link

commented Jul 17, 2018

Ah, I didn't thought of that, however it's reasonable that you did it this way. Thank you!

Just asking myself, how it's solved on heterogeneous architectures like ARM's LITTLE.big.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.