vmstat 的用法和判别系统瓶颈

2023-10-07 18:32

本文主要是介绍vmstat 的用法和判别系统瓶颈,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

vmstat 可以侦测『 CPU / 内存 / 磁碟输入输出状态 』,找出系统瓶颈
常用的参数是两个数字,第一个参数是采样的时间间隔数,单位是秒,第二个参数是采样的次数。

基本语法:

vmstat [-a] [-n] [-t] [-S unit] [delay [ count]]
vmstat [-s] [-n] [-S unit]
vmstat [-m] [-n] [delay [ count]]
vmstat [-d] [-n] [delay [ count]]
vmstat [-p disk partition] [-n] [delay [ count]]
vmstat [-f]
vmstat [-V]

选项与参数:
-a :使用 inactive/active(活跃与否) 取代 buffer/cache 的内存输出资讯;
-f :启动到目前为止,系统复制 (fork) 的程序数;
-s :将一些事件 (启动至目前为止) 导致的内存变化情况列表说明;
-S :后面可以接单位,让显示的数据有单位。例如 K/M 取代 bytes 的容量;
-d :列出磁碟的读写总量统计表
-p :后面列出分割槽,可显示该分割槽的读写总量统计表

例一: 2 秒采集一次,总共采集 10 次

 [root@localhost test]# vmstat 2 10
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st0  0      0 1664836  55140 689764    0    0    25    10  210  712  9  1 88  1  0   0  0      0 1664348  55140 690084    0    0     0     0 1102 3035 12  2 86  0  0   

表示 2 秒采集一次,总共采集 10 次,如果没有第二个参数

例二: 2 秒采集一次,直到按下 ctr+c

[root@localhost test]# vmstat 2
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st0  1      0 1705448  55872 689632    0    0    23    10  225  770 10  1 88  1  0   2  0      0 1705140  55872 689952    0    0     0    16 1166 3422 13  2 85  0  0   3  0      0 1707124  55872 682612    0    0     0     0 1148 3191 13  2 85  0  0   

例三: 2 秒采集一次,结果写入文件,留待以后分析

[root@localhost test]# vmstat 2 >> /tmp/vmstat &

字段说明:

Procs(进程):

r: 运行队列中进程数量,这个值也可以判断是否需要增加CPU。(长期大于1)
b: 等待IO的进程数量

Memory(内存):

swpd: 使用虚拟内存大小注意:如果swpd的值不为0,但是SI,SO的值长期为0,这种情况不会影响系统性能。
free: 空闲物理内存大小
buff: 用作缓冲的内存大小
cache: 用作缓存的内存大小注意:如果cache的值大的时候,说明cache处的文件数多,如果频繁访问到的文件都能被cache处,那么磁盘的读IO bi会非常小。
inact: 非活跃内存大小(当使用-a选项时显示)
active: 活跃的内存大小(当使用-a选项时显示)

Swap:

si: 每秒从交换区写到内存的大小,由磁盘调入内存
so: 每秒写入交换区的内存大小,由内存调入磁盘注意:内存够用的时候,这2个值都是0,如果这2个值长期大于0时,系统性能会受到影响,磁盘IO和CPU资源都会被消耗。有些朋友看到空闲内存(free)很少的或接近于0时,就认为内存不够用了,不能光看这一点,还要结合si和so,如果free很少,但是si和so也很少(大多时候是0),那么不用担心,系统性能这时不会受到影响的。

IO:(现在的Linux版本块的大小为1kb)

bi: 每秒读取的块数
bo: 每秒写入的块数注意:随机磁盘读写的时候,这2个值越大(如超出1024k),能看到CPU在IO等待的值也会越大。

SYS:

in: 每秒中断数,包括时钟中断。
cs: 每秒上下文切换数。注意:上面2个值越大,会看到由内核消耗的CPU时间会越大。

CPU(以百分比表示):

us: 用户进程执行时间百分比(user time)注意: us的值比较高时,说明用户进程消耗的CPU时间多,但是如果长期超50%的使用,那么我们就该考虑优化程序算法或者进行加速。sy: 内核系统进程执行时间百分比(system time)注意:sy的值高时,说明系统内核消耗的CPU资源多,这并不是良性表现,我们应该检查原因。wa: IO等待时间百分比注意:wa的值高时,说明IO等待比较严重,这可能由于磁盘大量作随机访问造成,也有可能磁盘出现瓶颈(块操作)。id: 空闲时间百分比

总结:
目前说来,对于服务器监控有用处的度量主要有:
r(运行队列)
pi(页导入)
us(用户CPU)
sy(系统CPU)
id(空闲)
注意:如果r经常大于4 ,且id经常少于40,表示cpu的负荷很重。如果bi,bo 长期不等于0,表示内存不足。

通过VMSTAT识别CPU瓶颈:
r(运行队列)展示了正在执行和等待CPU资源的任务个数。当这个值超过了CPU数目,就会出现CPU瓶颈了。
CPU 100% 并不能说明什么,Linux总是试图要CPU尽可能的繁忙,使得任务的吞吐量最大化。唯一能够确定CPU瓶颈的还是r(运行队列)的值。

Linux下查看CPU核心数的命令:

cat /proc/cpuinfo|grep processor|wc -l

解决办法大体几种:
1. 最简单的就是增加CPU个数和核数
2. 通过调整任务执行时间,如大任务放到系统不繁忙的情况下进行执行,进尔平衡系统任务
3. 调整已有任务的优先级

通过vmstat识别RAM瓶颈:

首先用free查看RAM的数量:

[oracle@oracle-db02 ~]$ free
total       used       free     shared    buffers     cached
Mem:       2074924    2071112       3812          0      40616    1598656
-/+ buffers/cache:     431840    1643084
Swap:      3068404     195804    2872600

当内存的需求大于RAM的数量,服务器启动了虚拟内存机制,通过虚拟内存,可以将RAM段移到SWAP DISK的特殊磁盘段上,这样会出现虚拟内存的页导出和页导入现象,页导出并不能说明RAM瓶颈,虚拟内存系统经常会对内存段进行页导出,但页导入操作就表明了服务器需要更多的内存了, 页导入需要从SWAP DISK上将内存段复制回RAM,导致服务器速度变慢。

解决的办法有几种:
1. 最简单的,加大RAM;
2. 改小SGA,使得对RAM需求减少;
3. 减少RAM的需求。(如:减少PGA)

详细语法

NAMEvmstat - Report virtual memory statisticsSYNOPSISvmstat [-a] [-n] [-t] [-S unit] [delay [ count]]vmstat [-s] [-n] [-S unit]vmstat [-m] [-n] [delay [ count]]vmstat [-d] [-n] [delay [ count]]vmstat [-p disk partition] [-n] [delay [ count]]vmstat [-f]vmstat [-V]DESCRIPTIONvmstat reports information about processes, memory, paging, block IO, traps, and cpu activity.The  first  report produced gives averages since the last reboot.  Additional reports give information on a sampling period of lengthdelay.  The process and memory reports are instantaneous in either case.OptionsThe -a switch displays active/inactive memory, given a 2.5.41 kernel or better.The -f switch displays the number of forks since boot.  This includes the fork, vfork, and clone system calls, and is  equivalent  tothe  total  number  of tasks created. Each process is represented by one or more tasks, depending on thread usage.  This display doesnot repeat.The -t switch adds timestamp to the output.The -m switch displays slabinfo.The -n switch causes the header to be displayed only once rather than periodically.The -s switch displays a table of various event counters and memory statistics. This display does not repeat.delay is the delay between updates in seconds.  If no delay is specified, only one report is printed with the  average  values  sinceboot.count is the number of updates.  If no count is specified and delay is defined, count defaults to infinity.The -d reports disk statistics (2.5.70 or above required)The -w enlarges field width for big memory sizesThe -p followed by some partition name for detailed statistics (2.5.70 or above required)The -S followed by k or K or m or M switches outputs between 1000, 1024, 1000000, or 1048576 bytesThe -V switch results in displaying version information.FIELD DESCRIPTION FOR VM MODEProcsr: The number of processes waiting for run time.b: The number of processes in uninterruptible sleep.Memoryswpd: the amount of virtual memory used.free: the amount of idle memory.buff: the amount of memory used as buffers.cache: the amount of memory used as cache.inact: the amount of inactive memory. (-a option)active: the amount of active memory. (-a option)Swapsi: Amount of memory swapped in from disk (/s).so: Amount of memory swapped to disk (/s).IObi: Blocks received from a block device (blocks/s).bo: Blocks sent to a block device (blocks/s).Systemin: The number of interrupts per second, including the clock.cs: The number of context switches per second.CPUThese are percentages of total CPU time.us: Time spent running non-kernel code. (user time, including nice time)sy: Time spent running kernel code. (system time)id: Time spent idle. Prior to Linux 2.5.41, this includes IO-wait time.wa: Time spent waiting for IO. Prior to Linux 2.5.41, included in idle.st: Time stolen from a virtual machine. Prior to Linux 2.6.11, unknown.FIELD DESCRIPTION FOR DISK MODEReadstotal: Total reads completed successfullymerged: grouped reads (resulting in one I/O)sectors: Sectors read successfullyms: milliseconds spent readingWritestotal: Total writes completed successfullymerged: grouped writes (resulting in one I/O)sectors: Sectors written successfullyms: milliseconds spent writing
IOcur: I/O in progresss: seconds spent for I/OFIELD DESCRIPTION FOR DISK PARTITION MODEreads: Total number of reads issued to this partitionread sectors: Total read sectors for partitionwrites : Total number of writes issued to this partitionrequested writes: Total number of write requests made for partitionFIELD DESCRIPTION FOR SLAB MODEcache: Cache namenum: Number of currently active objectstotal: Total number of available objectssize: Size of each objectpages: Number of pages with at least one active objecttotpages: Total number of allocated pagespslab: Number of pages per slabNOTESvmstat does not require special permissions.These reports are intended to help identify system bottlenecks.  Linux vmstat does not count itself as a running process.All linux blocks are currently 1024 bytes. Old kernels may report blocks as 512 bytes, 2048 bytes, or 4096 bytes.Since procps 3.1.9, vmstat lets you choose units (k, K, m, M) default is K (1024 bytes) in the default modevmstat uses slabinfo 1.1    FIXME

这篇关于vmstat 的用法和判别系统瓶颈的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/159660

相关文章

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详

在不同系统间迁移Python程序的方法与教程

《在不同系统间迁移Python程序的方法与教程》本文介绍了几种将Windows上编写的Python程序迁移到Linux服务器上的方法,包括使用虚拟环境和依赖冻结、容器化技术(如Docker)、使用An... 目录使用虚拟环境和依赖冻结1. 创建虚拟环境2. 冻结依赖使用容器化技术(如 docker)1. 创

MyBatis-Flex BaseMapper的接口基本用法小结

《MyBatis-FlexBaseMapper的接口基本用法小结》本文主要介绍了MyBatis-FlexBaseMapper的接口基本用法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具... 目录MyBATis-Flex简单介绍特性基础方法INSERT① insert② insertSelec

CentOS系统Maven安装教程分享

《CentOS系统Maven安装教程分享》本文介绍了如何在CentOS系统中安装Maven,并提供了一个简单的实际应用案例,安装Maven需要先安装Java和设置环境变量,Maven可以自动管理项目的... 目录准备工作下载并安装Maven常见问题及解决方法实际应用案例总结Maven是一个流行的项目管理工具

深入解析Spring TransactionTemplate 高级用法(示例代码)

《深入解析SpringTransactionTemplate高级用法(示例代码)》TransactionTemplate是Spring框架中一个强大的工具,它允许开发者以编程方式控制事务,通过... 目录1. TransactionTemplate 的核心概念2. 核心接口和类3. TransactionT

数据库使用之union、union all、各种join的用法区别解析

《数据库使用之union、unionall、各种join的用法区别解析》:本文主要介绍SQL中的Union和UnionAll的区别,包括去重与否以及使用时的注意事项,还详细解释了Join关键字,... 目录一、Union 和Union All1、区别:2、注意点:3、具体举例二、Join关键字的区别&php

C#实现系统信息监控与获取功能

《C#实现系统信息监控与获取功能》在C#开发的众多应用场景中,获取系统信息以及监控用户操作有着广泛的用途,比如在系统性能优化工具中,需要实时读取CPU、GPU资源信息,本文将详细介绍如何使用C#来实现... 目录前言一、C# 监控键盘1. 原理与实现思路2. 代码实现二、读取 CPU、GPU 资源信息1.

在C#中获取端口号与系统信息的高效实践

《在C#中获取端口号与系统信息的高效实践》在现代软件开发中,尤其是系统管理、运维、监控和性能优化等场景中,了解计算机硬件和网络的状态至关重要,C#作为一种广泛应用的编程语言,提供了丰富的API来帮助开... 目录引言1. 获取端口号信息1.1 获取活动的 TCP 和 UDP 连接说明:应用场景:2. 获取硬

JAVA系统中Spring Boot应用程序的配置文件application.yml使用详解

《JAVA系统中SpringBoot应用程序的配置文件application.yml使用详解》:本文主要介绍JAVA系统中SpringBoot应用程序的配置文件application.yml的... 目录文件路径文件内容解释1. Server 配置2. Spring 配置3. Logging 配置4. Ma