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

get docker container cpu usage from cpuacct.usage #581

Merged
merged 2 commits into from
Sep 27, 2018
Merged

Conversation

CMGS
Copy link
Contributor

@CMGS CMGS commented Sep 11, 2018

This PR for:

  • Get cgroup cpu usage from cpuacct.usage, it's a nanoseconds data, so I trans it to seconds.
  • Make CgroupCPUWithContext return same unit like cpu.Times. I saw cpu.Times will trans jiffies to seconds, so here should divide CPUTick.

After this change, we can calculate container CPU percent in host view and container view like this:

interval := Seconds
containerCPUStat1 := CgroupCPUDocker(cid)
hostCPUStat1 := cpu.Times(false)[0]
time.sleep(interval)
hostCPUStat2 := cpu.Times(false)[0]
containerCPUStat2 := CgroupCPUDocker(cid)

percentInHostView := (containerCPUStat2.Usage - containerCPUStat1.Usage) / (hostCPUStat2.Total() - hostCPUStat1.Total())

percentInContainerView := percentInHostView * CPUNumbers

@shirou
Copy link
Owner

shirou commented Sep 16, 2018

Thank you for your PR. but it seems this PR breaks compatibility about CgroupCPU. (*cpu.TimesStat -> CgroupCPUStat) We don't want to change API compatiblity, so it should be merge on v3.

or, because CgroupCPUUsage is just added and not break compatibility, we can merge it if you separate this PR.

@CMGS
Copy link
Contributor Author

CMGS commented Sep 16, 2018

@shirou So, could I add Usage into cpu.TimesStat? In this way, CgroupCPU can return *cpu.TimesStat

@shirou
Copy link
Owner

shirou commented Sep 16, 2018

No. cpuacct is not same meaning in all platform. If you add only CgroupCPUUsage users who use cgroup can get usage without breaking. compatibility. No need to add usage to CgroupCPU, I think.

@CMGS
Copy link
Contributor Author

CMGS commented Sep 20, 2018

OK, let me think about it...

@CMGS
Copy link
Contributor Author

CMGS commented Sep 21, 2018

separate PR done

@shirou
Copy link
Owner

shirou commented Sep 27, 2018

Sorry for late response and thank you for your contribution!

@shirou shirou merged commit a11c78b into shirou:master Sep 27, 2018
@shirou shirou mentioned this pull request Oct 26, 2020
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants