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

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

Jan 10, 2022 pm 06:45 PM
linux

本篇文章给大家带来了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
Copy after login

我们关注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"
Copy after login

小实验:将系统负载升到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
Copy after login

等待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
Copy after login

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

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

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

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
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use docker desktop How to use docker desktop Apr 15, 2025 am 11:45 AM

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

Difference between centos and ubuntu Difference between centos and ubuntu Apr 14, 2025 pm 09:09 PM

The key differences between CentOS and Ubuntu are: origin (CentOS originates from Red Hat, for enterprises; Ubuntu originates from Debian, for individuals), package management (CentOS uses yum, focusing on stability; Ubuntu uses apt, for high update frequency), support cycle (CentOS provides 10 years of support, Ubuntu provides 5 years of LTS support), community support (CentOS focuses on stability, Ubuntu provides a wide range of tutorials and documents), uses (CentOS is biased towards servers, Ubuntu is suitable for servers and desktops), other differences include installation simplicity (CentOS is thin)

What to do if the docker image fails What to do if the docker image fails Apr 15, 2025 am 11:21 AM

Troubleshooting steps for failed Docker image build: Check Dockerfile syntax and dependency version. Check if the build context contains the required source code and dependencies. View the build log for error details. Use the --target option to build a hierarchical phase to identify failure points. Make sure to use the latest version of Docker engine. Build the image with --t [image-name]:debug mode to debug the problem. Check disk space and make sure it is sufficient. Disable SELinux to prevent interference with the build process. Ask community platforms for help, provide Dockerfiles and build log descriptions for more specific suggestions.

How to view the docker process How to view the docker process Apr 15, 2025 am 11:48 AM

Docker process viewing method: 1. Docker CLI command: docker ps; 2. Systemd CLI command: systemctl status docker; 3. Docker Compose CLI command: docker-compose ps; 4. Process Explorer (Windows); 5. /proc directory (Linux).

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

Detailed explanation of docker principle Detailed explanation of docker principle Apr 14, 2025 pm 11:57 PM

Docker uses Linux kernel features to provide an efficient and isolated application running environment. Its working principle is as follows: 1. The mirror is used as a read-only template, which contains everything you need to run the application; 2. The Union File System (UnionFS) stacks multiple file systems, only storing the differences, saving space and speeding up; 3. The daemon manages the mirrors and containers, and the client uses them for interaction; 4. Namespaces and cgroups implement container isolation and resource limitations; 5. Multiple network modes support container interconnection. Only by understanding these core concepts can you better utilize Docker.

What is vscode What is vscode for? What is vscode What is vscode for? Apr 15, 2025 pm 06:45 PM

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages ​​and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

See all articles