Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to check CPU utilization in Linux

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "how to check CPU utilization in Linux", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to check CPU utilization in Linux" article.

1. Index range

1.1 User mode CPU utilization+ System mode CPU utilization

Reasonable value: 60-85%. If the us+sy time exceeds 85% in a multi-user system, the process may spend time waiting in the run queue, resulting in damage to response time and business throughput. Us is too large, indicating that user processes take up a lot of cpu time, and other hardware and software factors need to be further analyzed. Sy is too large, indicating that a lot of time has been spent in system management, indicating that a subsystem in the system has a bottleneck, and other software and hardware factors need to be further analyzed.

1.2 Wa (wait)

Reference value: less than 25%, more than 25% wa can indicate that the subsystem may not be balanced correctly, or it may be the result of a disk-intensive workload. There may be a problem with the system disk or other wa, which can be further analyzed by using the iostat/SAR-C command.

1.3 Id (idle)

Reference value: greater than 40. If r is often greater than 4 and id is often less than 40, it means that the load of cpu is very heavy.

1.4 r

Reference value: less than 4, queue greater than 4, indicating that there may be a problem with the cpu or memory of the system. If r is often greater than 4, and id is often less than 40, it means that the load of cpu is very heavy. When the queue becomes longer, the process in the queue takes longer to wait for the cpu schedule to execute.

1.5 methods to determine the bottleneck of cpu

Slow response time (slow response time)

Cpu has zero idle time (zero percent idle cpu)

Excessive user occupancy of cpu time (high percent user cpu)

Excessive system occupancy of cpu time (high percent system cpu)

A long queue of running processes (large run queue size sustained over time) for a long time

two。 How to view cpu utilization

2.1 use the top command to view

Data from / proc/stat file

% us = (User time + Nice time) / CPU time * 100% sy= (System time + Hardirq time + Softirq time) / CPU time * 100% id= (Idle time) / CPU time * 100% ni= (Nice time) / CPU time * 100% wa= (Waiting time) / CPU time * 100% hi= (Hardirq time) / CPU time * 100% si= (Softirq time) / CPU time * 100% st= (Steal time) / CPU time * 100%

Note: by default, the top command is refreshed every 3 seconds. You can also specify the refresh rate through top-d, such as top-d 0.1 or top-d 0.01, etc. When top executes, you can also press the "s" key to modify the time interval.

2.2 use vmstat to view

R represents the size of the running queue, b represents the number of threads due to IO waiting, in represents the number of interrupts, and cs represents the number of context switches.

2.3 other ways of viewing

Iostat, sar-Q, sar-u, etc.

3. CPU introduction

3.1 time in the kernel

HZ is the number of times the system clock sends out clock interrupts in a second. HZ can be configured before compiling the kernel, so you can check the clock interrupt frequency of the current system by using the following command: cat / boot/config- `uname-r` | grep CONFIG_HZ

Tick is the time per "tick" of the system clock, with a value of (1/HZ) seconds. That is, the time interval between two consecutive clock interrupts.

Jiffies is used to calculate the number of tick since the system was started, that is, each time the system clock produces a clock interrupt, the value of this variable is increased.

3.2 CPU time composition

The working time of CPU consists of three parts: user state time, system state time and idle state time. The specific composition is as follows:

CPU time includes User time, System time, Nice time, Idle time, Waiting time, Hardirq time, Softirq time, Steal time

Idle time = = idle time

User mode time = = user time+ Nice time.

Time of kernel state = = system time+ Hardirq time+ Softirq time.

User time . Refers to the time that CPU executes a process in user mode.

System time . Refers to the time that CPU is running in the kernel.

Nice time . Refers to the time spent by the system in adjusting the priority of the process.

Idle time . The system is idle, waiting for the process to run.

Waiting time . Refers to the total time that CPU spends waiting for the ed O operation, similar to the time spent on it.

Steal time . The time spent when the current CPU is forced (involuntary wait) to wait for another virtual CPU to be processed while the hypervisor is serving another virtual processor.

Softirq time 、 Hardirq time . Respectively corresponding to the CPU time spent by the system in dealing with soft and hard interrupts.

3.3 User mode CPU utilization

% usr. Shows the percentage of CPU time spent in user mode. The processes that users use CPU include: cpu running regular user processes, cpu running niced process,cpu running real-time processes. A process can be executed in user mode or in system (kernel) mode. When a process is running in kernel code, we call it in kernel state; when a process is executing user's own code, we say it is in user mode. When executed in user mode, the process executes in its own application code and does not need kernel resources to calculate, manage memory or set variables.

3.4 System mode CPU utilization

Shows the percentage of cpu time spent in the system mode, including cpu resources consumed by kernel processes (kprocs) and other processes that need to access kernel resources. The processes that the system uses cpu include: for system calls, for Ihop O management (interrupt and driver), for memory management (paging and swapping), for process management (context switch and process start), if a process needs kernel resources. It must perform a system call and thus switch to system mode to make the resource available.

3.5% wa (wait)

It shows that the percentage of cpu idle of the disk loaded by the local disk Ihampo and NFS is the ratio that the cpu is idle because the process waits for it, which mainly includes the following: IMagingraw IGraft OMGRAPING If there is at least one unfinished disk Ipicuro while the wait is running, the event is classified as the Icano wait time, and a request to the disk will cause the called process to block (or sleep) until the request is completed, and once the process's Icando request is completed, the process is placed in the run queue. If the cpu O completes quickly, the process can use more time.

3.6% id (idle)

The idle situation other than the WIO above shows the percentage of time that cpu is idle or waiting when there is no local cpu O. If there are no threads to execute (the run queue is listed as empty), the system dispatches a thread called wait, which can be called idle kproc. If the ps report shows that the total time of this thread is high, it indicates that there is a time period in which no other thread is ready to run or wait for execution on the cpu. The system therefore spends most of its time idle or waiting for a new task.

3.7r (runq-sz)

The length of the queue for the running process. For the size of the number of processes that can be run, these processes are ready in memory

4. Concept introduction

4.1 user mode + kernel mode

Generally speaking, a process can run in two modes on CPU, either in user mode or in kernel mode (that is, the process works in user mode and kernel mode, respectively, and kernel mode is still this process, unless the process is switched). Usually the operating system divides the virtual address space into user space and kernel space. For example, the virtual address space of Linux system on x86 platform is 0x00000000~0xffffffff, the pre-3GB (0x00000000~0xbfffffff) is the user space, and the post-1GB (0xc0000000~0xffffffff) is the kernel space. The user loads into user space, executes in user mode, cannot access data in the kernel, and cannot jump to kernel code for execution. This protects the kernel. If a process accesses an illegal address, at most one process crashes without affecting the stability of the kernel and the entire system.

When an interrupt or exception occurs, Cpu will not only jump to the west of the interrupt or exception service city, but also automatically switch mode, from user mode to privileged mode, so that programs from interrupts or exceptions can be jumped to kernel code for execution. In fact, the entire kernel is made up of various interrupt and exception handlers. That is, under normal circumstances, the processor executes the user program in user mode, in case of interrupt or exception, the processor switches to privileged mode to execute the kernel program, and after handling the interrupt or exception, the processor returns to the user mode to continue executing the user program. For example, user process A calls the kernel system call to get the current clock tick, and saves the IP of the current user process when executing the system call instruction in user process A. CS and other current state, and then jump to the kernel space (that is, the kernel code area) to execute system call functions such as should, to get the current clock tick. After execution, it is returned to process A through the IRET instruction (that is, the information saved on entry is reset to the corresponding register), and then the instructions of process An are executed from the CS:EIP address.

When the process is created, in addition to creating the control block of the process, the kernel stack of the process is also created in the kernel. After the process enters the kernel through system calls (such as fopen () or open ()), the processor executes in the (level 0) kernel code at the privileged level. When the process is in the kernel state, the kernel code executed will use the kernel stack of the current process and point to the context of the process.

Permissions in kernel mode are higher than those in user mode.

User level. System users can interact with them, such as running applications and system commands, and the user-level accesses the kernel level through the system call interface; kernel level. The operating system automatically runs some functions, which mainly operate on the hardware

4.2 process scheduling

Any process that wants to take possession of the CPU so that it is really in the state of execution must be scheduled by the process. Process scheduling mechanism is mainly related to scheduling mode, scheduling timing and scheduling strategy.

1. Dispatching mode

The scheduling mode of the Linux kernel basically adopts the "preemptive priority" mode, that is, when the process runs in user mode, regardless of whether it is voluntary or not, under certain conditions (such as running out of time slices or waiting for Imax O), the core can temporarily deprive it of running and schedule other processes to run. However, once the process switches to kernel mode, it runs without the above restrictions, and process scheduling does not occur until it returns to user mode.

The scheduling strategy in Linux system basically inherits the priority-based scheduling of Unix. That is, the core calculates a priority for each process in the system, which reflects the eligibility of a process to obtain the right to use CPU, that is, high-priority processes are given priority to run. The core selects a priority process from the process ready queue and assigns it a CPU time slice to put it into operation. During the run, the priority of the current process decreases over time, thus achieving "negative feedback": after a period of time, the previously lower-level process is relatively "upgraded" and has a chance to run. When the priority of all processes becomes 0, the priority of all processes is recalculated.

two。 Scheduling strategy

Linux system provides three different scheduling strategies for different types of processes, namely, SCHED_FIFO, SCHED_RR and SCHED_OTHER.

SCHED_FIFO is suitable for real-time processes, which require strong timeliness, and each run takes a short time. Once this process is scheduled to start running, it will run until it voluntarily gives up the CPU or is preempted by a higher priority process.

SCHED_RR corresponds to "time slice rotation", which is suitable for real-time processes that take a long time to run each time. A running process allocates a time slice (such as 200ms). When the time slice runs out, the CPU is preempted by another process, and the process is sent back to the end of the same priority queue. SCHED_OTHER is a traditional Unix scheduling strategy, which is suitable for interactive time-sharing processes. The priority of this kind of process depends on two factors, one is the remaining time quota of the process, if the process runs out of allotted time, the corresponding priority is 0; the other is the priority number of the process nice, which is inherited from the Unix system, the smaller the number of priority, the higher the priority.

The value range of nice is 19-20. The user can use the nice command to set the nice value of the process. However, ordinary users can only set a positive value, thus actively lowering their priority; only privileged users can set the value of nice to a negative number. The priority of the process is the sum of the above two. The core dynamically adjusts the priority of the user-mode process. In this way, a process needs to go through multiple feedback loops from creation to termination after completing the task. When the process is scheduled to run again, it continues to execute from the last breakpoint. For real-time processes, the priority value is (the positive value set by 1000 +), so it is at least 1000. Therefore, the priority of real-time processes takes precedence over other types of processes. In addition, time quotas and nice values have nothing to do with the priority of real-time processes. If there are real-time processes in the system that are ready, non-real-time processes cannot be scheduled to run until all real-time processes are completed and non-real-time processes have a chance to occupy CPU.

Background commands (with the & symbol at the end of the command, such as gcc f1.c &) correspond to background processes (also known as background jobs), which have a lower priority than any interactive (foreground) process. Therefore, the background process is scheduled to run only when there is no runnable interactive process in the system. Background processes are often scheduled to run in batch mode.

3. Scheduling opportunity

The timing of process scheduling by the core has the following situations:

(1) the current process calls nanosleep () or pause () to put itself to sleep, and actively gives up the right to use CPU for a period of time.

(2) terminate the process and give up the use of CPU.

(3) during the execution of the clock interrupt handler, the continuous running time of the current process is found to be too long.

(4) when a sleep process is awakened, it is found that the awakened process is more qualified to run than the current process.

(5) A process changes its scheduling policy or lowers its priority (such as the nice command) by executing system calls, resulting in immediate scheduling.

4. Scheduling algorithm

The algorithm of process scheduling should be relatively simple in order to reduce the system overhead of frequent scheduling. When Linux performs process scheduling, it first looks for all the processes in the ready queue and selects a process with a priority of * * and in memory. If there is a real-time process in the queue, the real-time process will run first. If the process that needs to be run most is not the current process, the current process is suspended and all machine states involved in its site, including counters and CPU registers, are saved, and then the running site is resumed for the selected process.

4.3 user-level threads and kernel-level threads

In many Unix-like systems, such as Linux, FreeBSD, Solaris, etc., the process has always been the smallest unit of kernel calls in the operating system, and the multi-process model is also adopted. Later, the concept of threads was introduced, and there are two concepts of threads:

User-level thread (User-Level Thread,ULT). The application process uses the thread library to create and manage, does not implement threads in the kernel, only simulates multithreading in the user mode, does not depend on the core, and the operating system kernel is completely unaware of the existence of multithreading.

Kernel thread (Kernel-Level Thread,KLT) is also called kernel-supported thread or lightweight process. It is implemented in the core space, and the kernel sets up a thread control block for each thread in the core space, which is used to register the thread identifier, value, state, priority and other information of the thread. All operations on the thread, such as creation, revocation and switching, are completed by the corresponding processing in the kernel through system function calls, and the kernel maintains the context switching of processes and threads as well as thread switching. Class systems are generally implemented by modifying the implementation of processes, and incomplete process creation can be used to create processes that share data space. This system call is clone () in Linux and rfork () in FreeBSD.

5. Common misunderstandings

5.1 High utilization of Cpu means insufficient cpu resources

When the cpu counter is out of range, it is not necessarily due to insufficient cpu resources, because other resources will also cause, for example, when there is not enough memory, cpu will be busy with memory management. On the surface, it may be that the utilization of cpu is 100%.

The above is about the content of this article on "how to check CPU utilization in Linux". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report