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
Linux Operations: Utilizing the Maintenance ModeLinux Operations: Utilizing the Maintenance ModeApr 19, 2025 am 12:08 AM

Linux maintenance mode can be entered through the GRUB menu. The specific steps are: 1) Select the kernel in the GRUB menu and press 'e' to edit, 2) Add 'single' or '1' at the end of the 'linux' line, 3) Press Ctrl X to start. Maintenance mode provides a secure environment for tasks such as system repair, password reset and system upgrade.

Linux: How to Enter Recovery Mode (and Maintenance)Linux: How to Enter Recovery Mode (and Maintenance)Apr 18, 2025 am 12:05 AM

The steps to enter Linux recovery mode are: 1. Restart the system and press the specific key to enter the GRUB menu; 2. Select the option with (recoverymode); 3. Select the operation in the recovery mode menu, such as fsck or root. Recovery mode allows you to start the system in single-user mode, perform file system checks and repairs, edit configuration files, and other operations to help solve system problems.

Linux's Essential Components: Explained for BeginnersLinux's Essential Components: Explained for BeginnersApr 17, 2025 am 12:08 AM

The core components of Linux include the kernel, file system, shell and common tools. 1. The kernel manages hardware resources and provides basic services. 2. The file system organizes and stores data. 3. Shell is the interface for users to interact with the system. 4. Common tools help complete daily tasks.

Linux: A Look at Its Fundamental StructureLinux: A Look at Its Fundamental StructureApr 16, 2025 am 12:01 AM

The basic structure of Linux includes the kernel, file system, and shell. 1) Kernel management hardware resources and use uname-r to view the version. 2) The EXT4 file system supports large files and logs and is created using mkfs.ext4. 3) Shell provides command line interaction such as Bash, and lists files using ls-l.

Linux Operations: System Administration and MaintenanceLinux Operations: System Administration and MaintenanceApr 15, 2025 am 12:10 AM

The key steps in Linux system management and maintenance include: 1) Master the basic knowledge, such as file system structure and user management; 2) Carry out system monitoring and resource management, use top, htop and other tools; 3) Use system logs to troubleshoot, use journalctl and other tools; 4) Write automated scripts and task scheduling, use cron tools; 5) implement security management and protection, configure firewalls through iptables; 6) Carry out performance optimization and best practices, adjust kernel parameters and develop good habits.

Understanding Linux's Maintenance Mode: The EssentialsUnderstanding Linux's Maintenance Mode: The EssentialsApr 14, 2025 am 12:04 AM

Linux maintenance mode is entered by adding init=/bin/bash or single parameters at startup. 1. Enter maintenance mode: Edit the GRUB menu and add startup parameters. 2. Remount the file system to read and write mode: mount-oremount,rw/. 3. Repair the file system: Use the fsck command, such as fsck/dev/sda1. 4. Back up the data and operate with caution to avoid data loss.

How Debian improves Hadoop data processing speedHow Debian improves Hadoop data processing speedApr 13, 2025 am 11:54 AM

This article discusses how to improve Hadoop data processing efficiency on Debian systems. Optimization strategies cover hardware upgrades, operating system parameter adjustments, Hadoop configuration modifications, and the use of efficient algorithms and tools. 1. Hardware resource strengthening ensures that all nodes have consistent hardware configurations, especially paying attention to CPU, memory and network equipment performance. Choosing high-performance hardware components is essential to improve overall processing speed. 2. Operating system tunes file descriptors and network connections: Modify the /etc/security/limits.conf file to increase the upper limit of file descriptors and network connections allowed to be opened at the same time by the system. JVM parameter adjustment: Adjust in hadoop-env.sh file

How to learn Debian syslogHow to learn Debian syslogApr 13, 2025 am 11:51 AM

This guide will guide you to learn how to use Syslog in Debian systems. Syslog is a key service in Linux systems for logging system and application log messages. It helps administrators monitor and analyze system activity to quickly identify and resolve problems. 1. Basic knowledge of Syslog The core functions of Syslog include: centrally collecting and managing log messages; supporting multiple log output formats and target locations (such as files or networks); providing real-time log viewing and filtering functions. 2. Install and configure Syslog (using Rsyslog) The Debian system uses Rsyslog by default. You can install it with the following command: sudoaptupdatesud

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

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.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)