How to calculate the CPU percentage?

0

When you open the system monitor , and windows , when you open the linux / em>, we have a percentage of CPU. How do you get to this code? How to get to this calculation using linux calls?

    
asked by anonymous 09.06.2015 / 21:05

3 answers

1

I'll try to answer this question with an analogy.

Imagine that you have to go from your house to the beach by taxi, and you are disputing this taxi with other people who also want to go to the beach. There is only one taxi in every city, and the taxi driver knowing this says the following:

- I can take you to the beach, but you can only walk 10 minutes at a time in my taxi. Every 10 minutes I'll leave you wherever we are and pick up another person to take to the beach. As soon as the line of people runs out, I'll pick you up where I left off and take you for another 10 minutes.

And so he does until he gets everyone to the beach.

Well, someone may ask: How do I calculate the percentage of a person's taxi use going to the beach?

It depends. If we consider that either the person walks by taxi or stands still, the instant percentages will always be 100% or 0% respectively. But if you take a time slot, we can calculate a average .

For example, you trigger the stopwatch when you enter the taxi, and as soon as you exit the stopwatch. On the average of the last 10 minutes the taxi use was 100% (considering that you did not ask to leave the taxi and not even the taxi driver told you to get off before 10 minutes). You can pick up a slightly larger slot, say 20 minutes, so taking into account that you've ridden the taxi in the first 10 minutes and been dropped off on the street after that for the taxi driver to pick up another passenger, then you walked 50% of the time two 20 minutes) by taxi.

This is how the CPU usage percentage of the processes that appear in the TOP is calculated. It is simply the ratio of the CPU usage time (time within the taxi) and the total time of a predetermined time slot (stopwatch time).

Of course, CPU input and output is more complex than simply a timer counting a slice of time. There are priority, input / output, hardware interrupts, software ... It also depends on the type of scheduler, but the CPU usage percentage of a process is calculated from a predetermined slice of time.

I'll kick it here: Calculate every 2 seconds (the time the top updates the list) and see the results you get.

ps: if it has not been clear, the taxi is the processor and the passengers processes.

Source: link

    
19.06.2015 / 15:36
0

To calculate I imagine that it checks how much data is being processed by processor capacity.

To retrieve the information (from the top) to display, do the following: link

I hope this solves your problem.

    
09.06.2015 / 21:41
0

If you do not question the snapshot of the processor, you can read /proc/loadavg : it is a one-line file (which you can read normally with fopen , fscanf , etc.) in the format

0.78 0.62 0.60 1/349 5082

The first three numbers are the average usage over the first one, five, and ten minutes; the fourth group tells you how many processes are actively running / how many processes in total are loaded, and the last number is the PID of the last process that used the CPU.

(Note that this average usage may be larger than one, but the "maximum" is the number of processors on the machine, which you can find by reading /proc/cpuinfo . If the average is greater than the number of processors , this indicates that the machine is 100% and still can not handle the service.)

    
13.06.2015 / 01:35