search
HomeOperation and MaintenanceLinux Operation and MaintenanceLinux command collection: understanding system load (organized and shared)

本篇文章给大家带来了Linux中负载的概念与问题诊断方法相关知识,其中包括了负载是什么以及线程状态等,希望对大家有帮助。

Linux command collection: understanding system load (organized and shared)

一般在类unix系统上,都会有系统负载(load average)这个指标,用来形容系统的繁忙程度,值越大则代表系统越繁忙。  

查看负载 

$ uptime
19:59:57 up 29 days,  7:08,  1 user,  load average: 0.57, 0.26, 0.18

我们关注load average后的3个值,分别代表1分钟、5分钟、15分钟的系统平均负载,如果1分钟值>5分钟值>15分钟值,则代表近15分钟内系统压力越来越大,反之亦然。 

同样,在top命令的第一行,也能看到系统负载,它的含义和uptime是一样的。 

负载是什么 

一般来说,系统线程基本都在这3个状态上:运行中,可运行,阻塞等待,其中,运行中的线程正在CPU上跑,可运行的线程等待CPU调度,而阻塞的线程等待锁释放或io完成。 

在传统unix系统上(如BSD),系统负载由正在运行的线程以及可运行的线程这2个部分组成。 

它能很好的说明CPU的饱和情况,比如4核的CPU,如果负载一直高于4,那说明CPU资源饱和了。 

而Linux扩大了负载的定义,如下: 

Linux负载由正在运行的线程和可运行的线程,以及D状态的线程(一般是等待io完成)这3个部分组成。 

因为Linux认为,虽然D状态的线程并不消耗CPU资源,但是它会消耗磁盘、网卡等硬件资源以及锁这样的软件资源,因此它也应该被用来计算系统负载,想来也合理,毕竟系统负载是用来描述整个系统的繁忙程度的,而不仅仅是CPU的。 

线程状态D

在Linux里面,线程有如下常见状态: 

  • R: 正在运行或可运行状态  

  • S: 睡眠状态,被阻塞等待唤醒  

  • D: 不可中断睡眠状态,一般是等待io完成   

这里面的R与D状态的线程会影响系统负载,因此,当系统负载较高时,可以通过如下命令了解是哪些线程导致的: 

ps -eLo pid,tid,stat,comm | grep -E " R|D"

小实验:将系统负载升到100

# 使用vfork函数创建一个子进程,子进程如果不调用exec系统调用,它的状态会一直是D。

$ cat uninterruptible.c 
int main() {
    vfork();
    sleep(600);
    return 0;
}
# 编译成可执行程序
$ gcc -o uninterruptible uninterruptible.c
# 运行100个程序
$ for i in {1..100}; do ./uninterruptible &; done

等待1分钟,就会发现系统负载升到了快100,如下:

$ uptime
20:24:42 up 29 days,  7:32,  1 user,  load average: 99.94, 74.82, 35.87
# 可以看到很多D状态的进程
$ ps -eLo pid,tid,stat,pcpu,wchan:32,comm | grep " D"
3774195 3774195 D     0.0 do_fork                          uninterruptible
3774196 3774196 D     0.0 do_fork                          uninterruptible
3774197 3774197 D     0.0 do_fork                          uninterruptible
3774198 3774198 D     0.0 do_fork                          uninterruptible

如上,通过ps命令可以看到线程状态,还有一个wchan字段,它显示的是线程当前被阻塞在什么内核函数上,这能看出一些蛛丝马迹。 

另外,通过/proc/sysrq-trigger可以看到D线程阻塞时的代码路径,如下: 

# 写入一个w即可,需要root权限执行
$ echo w > /proc/sysrq-trigger
# 然后内核会把D状态线程调用栈输出到内核日志,这可以通过dmesg查看
$ dmesg

Linux command collection: understanding system load (organized and shared)

这里就能很清楚的看到,是由于vfork系统调用引起的负载上升。 

之前介绍过bcc工具集里的offcputime工具,它可以用来绘制offcpu火焰图,同样的,诊断高负载问题时,也可以用这个工具,传一个参数,让其只关注D状态线程的offcpu行为即可,如下: 

# ubuntu安装bcc工具集
$ sudo apt install bpfcc-tools
# 使用root身份进入bash
$ sudo bash
# --state 2用于指定抓取TASK_UNINTERRUPTIBLE即D状态线程的offcpu栈
$ offcputime-bpfcc -K --state 2 -f 60  > d_state_offcpu_stack.out
# 绘制为offcpu火焰图
$ awk '{ print $1, $2 / 1000 }' d_state_offcpu_stack.out | ./FlameGraph/flamegraph.pl --color=io --countname=ms > d_state_offcpu.svg

Linux command collection: understanding system load (organized and shared)

相关推荐:《Linux视频教程

The above is the detailed content of Linux command collection: understanding system load (organized and shared). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金. If there is any infringement, please contact admin@php.cn delete
The 5 Core Components of the Linux Operating SystemThe 5 Core Components of the Linux Operating SystemMay 08, 2025 am 12:08 AM

The five core components of the Linux operating system are: 1. Kernel, 2. System libraries, 3. System tools, 4. System services, 5. File system. These components work together to ensure the stable and efficient operation of the system, and together form a powerful and flexible operating system.

The 5 Essential Elements of Linux: ExplainedThe 5 Essential Elements of Linux: ExplainedMay 07, 2025 am 12:14 AM

The five core elements of Linux are: 1. Kernel, 2. Command line interface, 3. File system, 4. Package management, 5. Community and open source. Together, these elements define the nature and functionality of Linux.

Linux Operations: Security and User ManagementLinux Operations: Security and User ManagementMay 06, 2025 am 12:04 AM

Linux user management and security can be achieved through the following steps: 1. Create users and groups, using commands such as sudouseradd-m-gdevelopers-s/bin/bashjohn. 2. Bulkly create users and set password policies, using the for loop and chpasswd commands. 3. Check and fix common errors, home directory and shell settings. 4. Implement best practices such as strong cryptographic policies, regular audits and the principle of minimum authority. 5. Optimize performance, use sudo and adjust PAM module configuration. Through these methods, users can be effectively managed and system security can be improved.

Linux Operations: File System, Processes, and MoreLinux Operations: File System, Processes, and MoreMay 05, 2025 am 12:16 AM

The core operations of Linux file system and process management include file system management and process control. 1) File system operations include creating, deleting, copying and moving files or directories, using commands such as mkdir, rmdir, cp and mv. 2) Process management involves starting, monitoring and killing processes, using commands such as ./my_script.sh&, top and kill.

Linux Operations: Shell Scripting and AutomationLinux Operations: Shell Scripting and AutomationMay 04, 2025 am 12:15 AM

Shell scripts are powerful tools for automated execution of commands in Linux systems. 1) The shell script executes commands line by line through the interpreter to process variable substitution and conditional judgment. 2) The basic usage includes backup operations, such as using the tar command to back up the directory. 3) Advanced usage involves the use of functions and case statements to manage services. 4) Debugging skills include using set-x to enable debugging mode and set-e to exit when the command fails. 5) Performance optimization is recommended to avoid subshells, use arrays and optimization loops.

Linux Operations: Understanding the Core FunctionalityLinux Operations: Understanding the Core FunctionalityMay 03, 2025 am 12:09 AM

Linux is a Unix-based multi-user, multi-tasking operating system that emphasizes simplicity, modularity and openness. Its core functions include: file system: organized in a tree structure, supports multiple file systems such as ext4, XFS, Btrfs, and use df-T to view file system types. Process management: View the process through the ps command, manage the process using PID, involving priority settings and signal processing. Network configuration: Flexible setting of IP addresses and managing network services, and use sudoipaddradd to configure IP. These features are applied in real-life operations through basic commands and advanced script automation, improving efficiency and reducing errors.

Linux: Entering and Exiting Maintenance ModeLinux: Entering and Exiting Maintenance ModeMay 02, 2025 am 12:01 AM

The methods to enter Linux maintenance mode include: 1. Edit the GRUB configuration file, add "single" or "1" parameters and update the GRUB configuration; 2. Edit the startup parameters in the GRUB menu, add "single" or "1". Exit maintenance mode only requires restarting the system. With these steps, you can quickly enter maintenance mode when needed and exit safely, ensuring system stability and security.

Understanding Linux: The Core Components DefinedUnderstanding Linux: The Core Components DefinedMay 01, 2025 am 12:19 AM

The core components of Linux include kernel, shell, file system, process management and memory management. 1) Kernel management system resources, 2) shell provides user interaction interface, 3) file system supports multiple formats, 4) Process management is implemented through system calls such as fork, and 5) memory management uses virtual memory technology.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use