Tech
2024-11-25
How is CPU usage measured

Table of contents

One of the most basic and important indicators in monitoring CPU usageThis is it. Watap also supports this key indicator. So what do CPU usage metrics mean, and how is this data generated and managed? In this article, we'll take a closer look at the principles and management methods of CPU data.

/proc/stat

On Linux systems /proc/stat A file is a virtual file that provides various system performance information such as CPU usage, system load, and process statistics. In particular, CPU usage information provides essential data for system monitoring and performance analysis.

/proc/stat The role and structure of files

/proc/stat The file virtual file system (procfs)As part of, it provides statistical information about systems and processes. By reading this file, you can get the latest information reflecting the current state of the system. The contents of the file are organized as follows:

What each field means

  • user: Time spent running in user mode (excluding nice values)
  • Nice: time in user mode running with low priority (nice)
  • System: Time spent running in kernel mode
  • Idle: Time spent in an idle state
  • iowait: Time to wait for an I/O operation to complete
  • IRQ: Time to handle hardware interrupts
  • softirq: Time to handle software interrupts
  • Steal: Time spent by other OSes in a virtualized environment
  • guest: The time it has run on the guest OS
  • guest_nice: Time on a guest OS running at a lower priority

We are among these indicators userI want to know what percentage it is but /proc/statThe indicators provided in appear in the form of a number such as 4705 instead of%, which represents a specific unit rather than a ratio. What unit does it mean?

CPU user (user) data generation process

1. Accumulation of time based on the state of the process

When a process is running, the kernel increases the appropriate time counter depending on the execution mode of the process. When running in user mode user The counter will increase.

2. /proc/statData reflected in

  • The kernel maintains internal CPU time accumulation information /proc/stat It is provided to the user space via a file.
  • Each time a file is read, the kernel outputs the current accumulated CPU time value.
  • Since these data are cumulative values since the system boots, differences with previous values must be compared in order to calculate CPU usage over a specific period of time.

CPU user data update cycle

Timer Tick-based updates

  • The CPU time counter is updated every time a timer tick occurs.
  • Therefore, the HZ value determines the update interval.

CPU time tracking in the Linux kernel

1. Timer ticks and HZ values

The Linux kernel tracks CPU time through timer ticks. Timer ticks are interrupts that occur at regular intervals, and the kernel uses them to schedule processes, calculate time, and accumulate CPU time.

  • HZ value: A value that determines the frequency of the timer tick, usually 100Hz, 250Hz, 1000Hz etc. are used.

How to check the HZ value:

You can check the HZ value of the current system with the above command.

2. CPU status tracking mechanism

While a process is running, the kernel accumulates CPU time based on the state of that process.

  • User mode (user mode): Mode in which normal applications run.
  • Kernel mode (kernel mode): A mode in which system calls or internal kernel tasks are executed.
  • Each process increases its CPU time counter according to the mode it is currently running in.

Examples of actual data generation

1. CPU time accumulation process

  1. Run a process: The process runs in user mode.
  2. Timer tick occurred: A timer tick interrupt occurs every scheduled cycle.
  3. Increase the time counter: Depending on the execution mode of the current process user Or increase another counter.
  4. Save accumulated time: This process is repeated and the accumulated time is stored in each counter.

2. /proc/stat File output

  • Each time a file is read, the value of each CPU time counter currently accumulated is output.
  • user The value accumulated in the counter is the total time spent in user mode since the system booted.

CPU usage calculation

Now /proc/statI know what kind of data you see in So how is the% value actually calculated? primer /proc/statThe CPU time of is cumulative, so the difference must be calculated by reading the values at regular intervals. For example, at 1-second intervals user Read the value and calculate the difference between the two values to find the user-mode CPU usage time for that period.

Percent calculations require values that are parameters. The parameters TotalThis is the sum of the differences between all counters, such as system, user, io wait, and nice. Total also calculates the difference by reading values at equal intervals. Thus, the user mode CPU usage rate (delta_user/ delta_total) * 100It can be calculated as

  • CPU usage calculation example:

finishing

Measuring CPU usage is a key task in maintaining system performance and stability. /proc/statUsing the data in the file, CPU utilization can be calculated by calculating data differences at regular intervals based on user mode CPU time accumulated since the system boots. In this process, understanding the concepts of timer ticks and HZ values enables more accurate analysis, and a real-time monitoring system can be built through this. CPU usage metrics are more than just numbers; when used effectively, they can provide important insights for optimizing system performance and diagnosing problems.

Experience Monitoring with WhaTap!