


Linux command collection: understanding system load (organized and shared)
本篇文章给大家带来了Linux中负载的概念与问题诊断方法相关知识,其中包括了负载是什么以及线程状态等,希望对大家有帮助。
一般在类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
这里就能很清楚的看到,是由于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视频教程》
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

VS Code One-step/Next step shortcut key usage: One-step (backward): Windows/Linux: Ctrl ←; macOS: Cmd ←Next step (forward): Windows/Linux: Ctrl →; macOS: Cmd →

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)
