CPU statistics calculation algorithm

mah454

I too many search on internet to found algorithm calculated %Us , %Sy , %Id , ... on result of top command . but can not found any documentation .
some documents like this or this calculate cpu utilization but output not equal with output of top command . (too diff !!!)

How do the top or mpstat commands calculate cpu statistics ?

mah454

I calculate with this formula :

result=(CurrentUse-PrevUse)*100/(CurrentTotal-PrevTotal)

this is a example script calculate [us,sys,idle] of cpu .

#!/bin/bash 

prev_total=0 
prev_idle=0
prev_us=0
prev_sys=0

while true
do
        line=$(head -n1 /proc/stat)

        us=$(echo $line | awk '{print $2}') 
        ni=$(echo $line | awk '{print $3}') 
        sy=$(echo $line | awk '{print $4}') 
        id=$(echo $line | awk '{print $5}') 
        io=$(echo $line | awk '{print $6}') 
        irq=$(echo $line | awk '{print $7}') 
        si=$(echo $line | awk '{print $8}') 
        st=$(echo $line | awk '{print $9}') 
        g=$(echo $line | awk '{print $10}') 
        gn=$(echo $line | awk '{print $11}') 

        total=$(expr $us + $ni + $sy + $id + $io + $irq + $si + $st + $g + $gn) 

        let "diff_total=$total-$prev_total"
        let "diff_idle=$id-$prev_idle"
        let "diff_us=$us-$prev_us"
        let "diff_sys=$sy-$prev_sys"

         let "result_us=$diff_us * 100 / $diff_total" 
         let "result_idle=$diff_idle * 100 / $diff_total" 
         let "result_sys=$diff_sys * 100 / $diff_total" 

         echo -en "\rCpu   us:$result_us%  sys:$result_sys%  idle:$result_idle%\b\b"

        prev_total=$total 
        prev_idle=$id
        prev_us=$us
        prev_sys=$sy
        sleep 1
done

output like this :

Cpu   us:1%  sys:0%  idle:97%

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Time complexity calculation of algorithm

From Dev

java algorithm calculation of precision

From Dev

Algorithm for shape calculation (Ellipse)

From Dev

Algorithm Recursive Formula Calculation

From Dev

CRC checksum calculation algorithm

From Dev

Efficient algorithm for matrix calculation

From Dev

R mean aspect calculation (circular statistics)

From Dev

Time complexity calculation for my algorithm

From Dev

Fermat Algorithm for prime factors calculation

From Dev

CPU Scheduler Algorithm

From Dev

Why are the CPU frequency timing statistics all zeros?

From Dev

Why are the CPU frequency timing statistics all zeros?

From Dev

Accurate calculation of CPU usage given in percentage in Linux?

From Dev

How to convert Microsecond Value to CPU cycle calculation?

From Dev

Preemptive Priority CPU scheduling Algorithm

From Java

Understanding Time complexity calculation for Dijkstra Algorithm

From Dev

Need an algorithm written for delay time calculation

From Dev

trouble in calculation the right algorithm in javascript to draw an array

From Dev

Porting CRC Calculation algorithm from C to Java

From Dev

C# Windows form algorithm calculation

From Dev

What does the "nice" value mean in CPU utilization statistics

From Dev

What does the "nice" value mean in CPU utilization statistics

From Dev

Eclipse: Java Monitor won't show cpu statistics

From Dev

How to retrieve specific cpu, memory and interface statistics only

From Dev

Statistics of CPU read versus write instructions ( including reading of program )

From Dev

What is the fastest shadowing algorithm (CPU only)?

From Dev

In docker cpu usage calculation what are: TotalUsage, SystemUsage, PercpuUsage and what does the calculation means?

From Dev

Algorithm for calculation of future good amounts with complex production lines

From Dev

Eigenvalue calculation using TQLI algorithm fails with segmentation fault

Related Related

  1. 1

    Time complexity calculation of algorithm

  2. 2

    java algorithm calculation of precision

  3. 3

    Algorithm for shape calculation (Ellipse)

  4. 4

    Algorithm Recursive Formula Calculation

  5. 5

    CRC checksum calculation algorithm

  6. 6

    Efficient algorithm for matrix calculation

  7. 7

    R mean aspect calculation (circular statistics)

  8. 8

    Time complexity calculation for my algorithm

  9. 9

    Fermat Algorithm for prime factors calculation

  10. 10

    CPU Scheduler Algorithm

  11. 11

    Why are the CPU frequency timing statistics all zeros?

  12. 12

    Why are the CPU frequency timing statistics all zeros?

  13. 13

    Accurate calculation of CPU usage given in percentage in Linux?

  14. 14

    How to convert Microsecond Value to CPU cycle calculation?

  15. 15

    Preemptive Priority CPU scheduling Algorithm

  16. 16

    Understanding Time complexity calculation for Dijkstra Algorithm

  17. 17

    Need an algorithm written for delay time calculation

  18. 18

    trouble in calculation the right algorithm in javascript to draw an array

  19. 19

    Porting CRC Calculation algorithm from C to Java

  20. 20

    C# Windows form algorithm calculation

  21. 21

    What does the "nice" value mean in CPU utilization statistics

  22. 22

    What does the "nice" value mean in CPU utilization statistics

  23. 23

    Eclipse: Java Monitor won't show cpu statistics

  24. 24

    How to retrieve specific cpu, memory and interface statistics only

  25. 25

    Statistics of CPU read versus write instructions ( including reading of program )

  26. 26

    What is the fastest shadowing algorithm (CPU only)?

  27. 27

    In docker cpu usage calculation what are: TotalUsage, SystemUsage, PercpuUsage and what does the calculation means?

  28. 28

    Algorithm for calculation of future good amounts with complex production lines

  29. 29

    Eigenvalue calculation using TQLI algorithm fails with segmentation fault

HotTag

Archive