How to calculate the CPU percentage?

Asked

Viewed 1,578 times

0

In the linux, when the system monitor, and in the windows, when the task manager, we have a percentage of CPU. How do we get to this code? How do we get to this calculation using the linux?

4 answers

1

I’ll try to answer that 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’s only one taxi in the whole town, and the cab driver knowing that 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 drop you off wherever we are and pick up someone else to take to the beach. As soon as the line of people is over, I’ll pick you up where I left you and take you for another 10 minutes.

And so he goes on until he takes everyone to the beach.

Well, one might ask: How do I calculate the percentage of taxi use of a certain person going to the beach?

It depends. If we consider that the person either drives a taxi or stops, the instantaneous percentages will always be 100% or 0% respectively. But if you take a time slot, we can calculate a average.

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

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

Of course, the input and output of CPU processes is more complex than simply a stopwatch counting a slice of time. There’s priority, input/output, hardware interruptions, software... It also depends on the type of scheduler, but the CPU usage percentage of a process is calculated from a pre-determined slice of time.

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

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

Source:https://stackoverflow.com/questions/3748136/how-is-cpu-usage-calculated

0

If you don’t mind the use snapshot processor, you can read the /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 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 greater than one, but its "maximum" is the number of machine processors, which you can discover by reading /proc/cpuinfo. If the average is higher than the number of processors, this indicates that the machine is at 100% and still is not able to handle the service.)

  • I wanted to do this related to a specific process. For this I was using C and calculating what I wanted. Only processing is harder to find and calculate because I don’t know exactly this calculation.

0

To know how to calculate the % of CPU usage that remembers the concepts of binary and valve. The processor is either in use =1 or stopped, waiting =0. In a time interval the amount that it is connected is calculated and divided by the total possible.

0

  • The top won’t apply here because I wanted to do it in C. But thanks anyway.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.