Calculating CPU usage of a cgroup over a period of time

Dylan

If I have a cgroup like user.slice and have CPUAccounting=true set up, I'm wondering how one would calculate the CPU Usage of that particular cgroup over a period of time (such as 1s), using either cpuacct.usage or cpuacct.stat. I know cpuacct.usage provides the CPU time (in nanoseconds), which could be used to calculate the CPU percentage by subtracting two snapshots of it during a period of time and dividing it by the total CPU time of the system over the same period of time (correct me if I'm wrong), but I'm unsure how to get the total CPU time over a period of time.

Is there a way I can get the total CPU time over a period of time or use a different formula/cgroup feature to calculate the CPU Usage? Thanks in advance :)

Note: I am aware of systemd-cgtop, which provides some of this information, however I want to calculate the CPU Usage programatically without having to parse systemd-cgtop

phemmer

CPU usage percentage is the ratio of the total time the CPU was active, to the elapsed time of the clock on your wall. If you try and compare one CPU time to another CPU time, this isn't usage percentage. There is no name for this, it's just the ratio of one CPU time to another CPU time.

You've already stated you know how to get elapsed CPU time (by taking the difference of CPU time before & after your period), so just divide that by wall clock time.

cgroup=$(awk -F: '$2 == "cpu,cpuacct" { print $3 }' /proc/self/cgroup)

tstart=$(date +%s%N)
cstart=$(cat /sys/fs/cgroup/cpu/$cgroup/cpuacct.usage)

sleep 5

tstop=$(date +%s%N)
cstop=$(cat /sys/fs/cgroup/cpu/$cgroup/cpuacct.usage)

bc -l <<EOF
($cstop - $cstart) / ($tstop - $tstart) * 100
EOF

If you want to avoid the call to date (and cat), you could use something like: read tstart _ < /proc/uptime.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Calculating CPU usage for all PIDs

分類Dev

MySQL Calculating sum over pairwise time differences of log file

分類Dev

DAX / PowerPivot query functions to spread aggregated values over time period

分類Dev

Calculating Average over date

分類Dev

Count events over a period of time and yield the sum once every second in RxJS

分類Dev

Azure web app application insights query for Operation/Dependency time, Duration and count over a period

分類Dev

Calculating time difference in hours

分類Dev

Check if Time Period Overlaps

分類Dev

MySQL high CPU usage

分類Dev

Why are Xcode and Time Profiler reporting higher CPU usage for faster iOS devices?

分類Dev

Calculating time complexity of inner loops

分類Dev

R time series period subtract?

分類Dev

Generate alert on high CPU usage

分類Dev

Jenkins High CPU Usage Khugepageds

分類Dev

Get CPU usage IOS Swift

分類Dev

Heavy CPU Usage by scrapy crawler

分類Dev

'htop' process and threads cpu usage?

分類Dev

Cgroupを使用してCPUの使用を制限する

分類Dev

cgroupと外部との間でCPUコアを配布する

分類Dev

Calculating time depending on string variable in other column

分類Dev

Calculating time elapsed within grouped sets of rows

分類Dev

Postgresql calculating minimal difference in time between records

分類Dev

Calculating 'on time performance' between scheduled and actual times

分類Dev

calculating time differences using moment.js

分類Dev

C# Trouble Calculating Delta Time

分類Dev

Calculating time complexity by just seeing the algorithm code

分類Dev

Convert frequency to java.time.Period type

分類Dev

Laravel - caching external data in a model for a period of time

分類Dev

Function not waiting for intended period of time - Python Selenium

Related 関連記事

  1. 1

    Calculating CPU usage for all PIDs

  2. 2

    MySQL Calculating sum over pairwise time differences of log file

  3. 3

    DAX / PowerPivot query functions to spread aggregated values over time period

  4. 4

    Calculating Average over date

  5. 5

    Count events over a period of time and yield the sum once every second in RxJS

  6. 6

    Azure web app application insights query for Operation/Dependency time, Duration and count over a period

  7. 7

    Calculating time difference in hours

  8. 8

    Check if Time Period Overlaps

  9. 9

    MySQL high CPU usage

  10. 10

    Why are Xcode and Time Profiler reporting higher CPU usage for faster iOS devices?

  11. 11

    Calculating time complexity of inner loops

  12. 12

    R time series period subtract?

  13. 13

    Generate alert on high CPU usage

  14. 14

    Jenkins High CPU Usage Khugepageds

  15. 15

    Get CPU usage IOS Swift

  16. 16

    Heavy CPU Usage by scrapy crawler

  17. 17

    'htop' process and threads cpu usage?

  18. 18

    Cgroupを使用してCPUの使用を制限する

  19. 19

    cgroupと外部との間でCPUコアを配布する

  20. 20

    Calculating time depending on string variable in other column

  21. 21

    Calculating time elapsed within grouped sets of rows

  22. 22

    Postgresql calculating minimal difference in time between records

  23. 23

    Calculating 'on time performance' between scheduled and actual times

  24. 24

    calculating time differences using moment.js

  25. 25

    C# Trouble Calculating Delta Time

  26. 26

    Calculating time complexity by just seeing the algorithm code

  27. 27

    Convert frequency to java.time.Period type

  28. 28

    Laravel - caching external data in a model for a period of time

  29. 29

    Function not waiting for intended period of time - Python Selenium

ホットタグ

アーカイブ