Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1437217
  • 博文数量: 463
  • 博客积分: 10540
  • 博客等级: 上将
  • 技术积分: 5450
  • 用 户 组: 普通用户
  • 注册时间: 2006-11-12 08:30
文章分类

全部博文(463)

文章存档

2014年(2)

2012年(14)

2011年(42)

2010年(18)

2009年(78)

2008年(35)

2007年(182)

2006年(92)

我的朋友

分类: LINUX

2009-04-22 19:50:28

vmstat与iostat详解
Linux 在具有高稳定性、可靠性的同时,具有很好的可伸缩性和扩展性,能够针对不同的应用和硬件环境调整,优化出满足当前应用需要的最佳性能。因此企业在维护Linux系统、进行系统调优时,了解系统性能分析工具是至关重要的。
  在Linux下有很多系统性能分析工具,比较常见的有top、free、ps、time、timex、uptime等。下文将介绍几个较为重要的性能分析工具vmstat、iostat和sar及其使用。
 用vmstat监视内存使用情况
  vmstat是Virtual Meomory Statistics(虚拟内存统计)的缩写,可对操作系统的虚拟内存、进程、CPU活动进行监视。它是对系统的整体情况进行统计,不足之处是无法对某个进程进行深入分析。
  vmstat的语法如下:
  vmstat [-V] [-n] [delay [count]]
  其中,-V表示打印出版本信息;-n表示在周期性循环输出时,输出的头部信息仅显示一次;delay是两次输出之间的延迟时间;count是指按照这个时间间隔统计的次数。对于vmstat输出各字段的含义,可运行man vmstat查看。
用iostat监视I/O子系统情况
  iostat是I/O statistics(输入/输出统计)的缩写,iostat工具将对系统的磁盘操作活动进行监视。它的特点是汇报磁盘活动统计情况,同时也会汇报出CPU使用情况。同vmstat一样,iostat也有一个弱点,就是它不能对某个进程进行深入分析,仅对系统的整体情况进行分析。
  iostat的语法如下:
iostat [ -c | -d ] [ -k ] [ -t ] [ -V ] [ -x [ device ] ] [ interval [ count ] ]
  其中,-c为汇报CPU的使用情况;-d为汇报磁盘的使用情况;-k表示每秒按kilobytes字节显示数据;-t为打印汇报的时间;-v表示打印出版本信息和用法;-x device指定要统计的设备名称,默认为所有的设备;interval指每次统计间隔的时间;count指按照这个时间间隔统计的次数。
  iostat一般的输出格式如下:
Linux 2.4.18-18smp (builder.linux.com) 2003年03月07日
avg-cpu: %user %nice %sys %idle
4.81 0.01 1.03 94.15
Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn
dev3-0 30.31 1117.68 846.52 16104536 12197374
dev3-1 7.06 229.61 40.40 3308486 582080
对于输出中各字段的含义,iostat的帮助中有详细的说明。
vmstat 命令报告虚拟内存统计信息和CPU负荷:页面调度,交换,任务交换,CPU利用率。命令的语法是:
vmstat [-cisS] [d1 d2 d3 d4] [interval [count]]
当不用选项时,vmstat显示一条曲线表示自从系统启动后活动的信息。如果指定interval(时间间隔),接下来的线是重复显示最后的interval期间活动的情况,直到用户中断命令执行。当同时提供计数器时,统计信息按时间计数来显示。
如果指定了一个磁盘名(如d1,d2,等等。),这些磁盘得到优先显示。通常,系统上的前4个磁盘设备是显示的,因为只有四个设备能在一线条上显示,这个选项允许性能分析员修改缺省显示选项。(*磁盘名通常随id,sd,xd,或xy (取决于类型和I/0界面)加一个数字,如id0,sd2,xd1等等命名而定。)
,vmstat 命令显示若干字段信息:
procs 报表下面三种状态的进程数:
r--在运行队列中等候运行
b--被资源阻塞(I/0,页面调度,等等.)
w--可运行但是被换出的
memory 报告虚拟内存和实存信息:
swap--以千字节为单位的当前可用交换空间的数量
free--以千字节为单位的页自由表大小
page 报告每秒页面调度活动数量的信息:
re-从自由表回收页
mf--次要的错误;地址空间或硬件地址转换错误
pi--页入的千字节数
po -页出的千字节数
fr- 释放的千字节数
de--以千字节为单位的可接受的短期内存不足数
sr--页由时钟算法扫描
disk 可以为四个磁盘报告每秒磁盘I/O的数量

faults 报告每秒系统软件中断和硬件中断的速率
in-设备中断,不包括系统时钟中断
sy-系统调用
cs-CPU任务(上下文)交换
cpu-- CPU故障时间的百分比,在多处理器系统上,这是全部处理器的平均值:
us- 用户时间
sy-- 系统时间
id-- 闲置时间
vmstat命令有四个可选标志可供使用。如果机器有虚拟地址缓存-c标志就改变输出报告缓存刷新统计数据。报告包括自从系统启动后每种缓存刷新全部总量。六个缓存类型是用户,上下文,区域,段,页,部分页。
-i标志 使输出变为报告中断的数量。如果给出设备名,如d1,d2等,监控将在设备级*执行,(*注,参阅第十二章有关打开设备级监控的信息。)并报告每个给定设备的统计信息。
修改"普通"报告来显示交换而非页面调度活动的信息。这选项改变显示的两个字段:si(换入)和so(换出)替代了re和mf字段。
值得注意是,interval 和count选项对-i或-s选项是非法的。
vmstat 参数详解
procs:
r-->在运行队列中等待的进程数
b-->在等待io的进程数
w-->可以进入运行队列但被替换的进程

memoy
swap-->现时可用的交换内存(k表示)
free-->空闲的内存(k表示)

pages
re--》回收的页面
mf--》非严重错误的页面
pi--》进入页面数(k表示)
po--》出页面数(k表示)
fr--》空余的页面数(k表示)
de--》提前读入的页面中的未命中数
sr--》通过时钟算法扫描的页面

disk 显示每秒的磁盘操作。 s表示scsi盘,0表示盘号

fault 显示每秒的中断数
in--》设备中断
sy--》系统中断
cy--》cpu交换

cpu 表示cpu的使用状态

cs--》用户进程使用的时间
sy--》系统进程使用的时间
id--》cpu空闲的时间

FIELD DESCRIPTIONS

Procs

r: The number of processes waiting for run time.
b: The number of processes in uninterruptable sleep.
w: The number of processes swapped out but otherwise runnable.

Thisfield is calculated, but Linux never desperation swaps.

Memory

swpd: the amount of virtual memory used (kB).
free: the amount of idle memory (kB).
buff: the amount of memory used as buffers (kB).

Swap

si: Amount of memory swapped in from disk (kB/s). 虚拟内存的页导入(从SWAP DISK导入RAM)
so: Amount of memory swapped to disk (kB/s). 虚拟内存的页导出.

(从RAM到SWAP DISK)
IO

bi: Blocks sent to a block device (blocks/s).

bo: Blocks received from a block device (blocks/s).

System

in: The number of interrupts per second, including the clock.

cs: The number of context switches per second.
: CPU
These are percentages of total CPU time.

us: user time
sy: system time

id: idle time

如果 r经常大于 4 ,且id经常少于40,表示cpu的负荷很重。
如果pi,po 长期不等于0,表示内存不足。
如果disk 经常不等于0, 且在 b中的队列 大于3, 表示 io性能不好。

由vmstat看服务器

说明:由于STATSPACK并不能获取全面分析性能问题所需要的所有信息,所以需要扩展其收集服务器的统计信息。(本文环境REDHAT Linux7.2)

VMSTAT介绍
通过STATSPACK收集服务器信息,主要通过收集VMSTAT的信息来展现服务器状况。VMSTAT工具是最常见的UNIX监控工具,可以展现给定时间间隔的服务器的状态值。

一般VMSTAT工具的使用是通过两个数字参数来完成的,第一个参数是采样的时间间隔数,单位是秒,第二个参数是采样的次数。如:

[oracle@brucelau oracle]$ vmstat 1 2

procs memory swap io system  CPU

r b w swpd free buff cache  si so bi bo in cs us sy id

1 0 0 0 271844 186052 255852 0 0 2 6 102 10 0 0 100

0 0 0 0 271844 186052 255852 0 0 0 0 104 11 0 0 100
 
(注:目前系统几乎空闲,并且不同操作系统VMSTAT输出内容有所不同)
 
目前说来,对于服务器监控有用处的度量主要有:

r(运行队列)

pi(页导入)

us(用户CPU)

sy(系统CPU)

id(空闲)
 
通过VMSTAT识别CPU瓶颈

r(运行队列)展示了正在执行和等待CPU资源的任务个数。当这个值超过了CPU数目,就会出现CPU瓶颈了。

获得CPU个数的命令(LINUX环境):

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

当r值超过了CPU个数,就会出现CPU瓶颈,解决办法大体几种:

1. 最简单的就是增加CPU个数

2. 通过调整任务执行时间,如大任务放到系统不繁忙的情况下进行执行,进尔平衡系统任务

3. 调整已有任务的优先级
 
通过VMSTAT识别CPU满负荷

首先需要声明一点的是,vmstat中CPU的度量是百分比的。当us+sy的值接近100的时候,表示CPU正在接近满负荷工作。但要注意的是,CPU满负荷工作并不能说明什么,UNIX总是试图要CPU尽可能的繁忙,使得任务的吞吐量最大化。唯一能够确定CPU瓶颈的还是r(运行队列)的值。
 
通过VMSTAT识别RAM瓶颈

数据库服务器都只有有限的RAM,出现内存争用现象是Oracle的常见问题。

首先察看RAM的数量,命令如下(LINUX环境):

[root@brucelau root]#free

total used free shared buffers cached

Mem: 1027348 873312 154036 185736 187496 293964

-/+ buffers/cache: 391852 635496

Swap: 2096440 0 2096440
 
当然可以使用top等其他命令来显示RAM。

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

1. 最简单的,加大RAM

2. 改小SGA,使得对RAM需求减少

3. 减少RAM的需求(如:减少PGA)
 
我们基本的了解了VMSTAT工作,下面是STATSPACK通过vmstat统计收集服务器性能数据。
 
STATSPACK通过vmstat收集服务器信息

首先在perfstat用户下建一个存储服务器信息的表:如

建表:

create table stats$vmstat

(

start_date date, --系统时间

duration date, --时间间隔

server_name varchar2(20), --服务器名称

runque_waits number, --运行队列数据

page_in number, --页导入数据

page_out number, --页导出数据

user_cpu number, --用户cpu数据

system_cpu number, --系统cpu数据

idle_cpu number, --空闲cpu数据

wait_cpu number -等待cpu数据(只是aix存在)

)

tablespace perfstat;

然后,通过UNIX/LINUX的shell变成,利用vmstat的结果来获取相应的服务器信息,并且存放到表中。
 
关于shell编程,可能已经超出本文内容,并且诚实的说,本人并没有shell编程的经验,希望那位兄台可以完成shell编程的内容,并劳驾mail给我共享一下,谢了先!!
 
 

附:

LINUX上VMSTAT的帮助手册:(man vmstat的结果)

VMSTAT(8) Linux Administrator's Manual VMSTAT(8)

NAME

vmstat - Report virtual memory statistics
 
SYNOPSIS

vmstat [-n] [delay [ count]]

vmstat[-V]
 
DESCRIPTION

vmstat 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 sam-

pling period of length delay. The process and memory reports are instantaneous in either case.
 
Options

The -n switch causes the header to be displayed only once rather than periodically.
 
delay is the delay between updates in seconds. If no delay is specified, only one report is printed with the

average values since boot.
 
count is the number of updates. If no count is specified and delay is defined, count defaults to infinity.
 
The -V switch results in displaying version information.
 
FIELD DESCRIPTIONS

Procs

r: The number of processes waiting for run time.

b: The number of processes in uninterruptable sleep.

w: The number of processes swapped out but otherwise runnable. This

field is calculated, but Linux never desperation swaps.
 
Memory

swpd: the amount of virtual memory used (kB).

free: the amount of idle memory (kB).

buff: the amount of memory used as buffers (kB).
 
Swap

si: Amount of memory swapped in from disk (kB/s).

so: Amount of memory swapped to disk (kB/s).
 
IO

bi: Blocks sent to a block device (blocks/s).

bo: Blocks received from a block device (blocks/s).
 
System

in: The number of interrupts per second, including the clock.

cs: The number of context switches per second.

: CPU

These are percentages of total CPU time.

us: user time

sy: system time

id: idle time

NOTES
vmstat 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 1k, except for CD-ROM blocks which are 2k.

FILES
/proc/meminfo
/proc/stat
/proc/*/stat
SEE ALSO
ps(1), top(1), free(1)
BUGS
Does not tabulate the block io per device or count the number of system calls.
阅读(624) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~