排队理论
L=A*W=A(Q+S)=A(Q+Tsys+Tuser)
下面这个公式进行解释
.Queue length(
L): average number of requests waiting in the system. 队列长度:在系统中等待的请求的平均数目;reqs
.
Arrival rate: the rate at which requests enter a system. 达到率:一次性达到系统的请求的个数;reqs/s
.
Wait time: average time to satisfy a request. Also know as wall clock, latency,response time, or residence time; 等待时间:完成(satisfy)一个请求的平均时间;也可以称之为延迟等;
.Queue time: time waiting for a resource to become available 队列时间:一个资源在队列中等待直到可用的时间
.Service time: time for a resource to process a request. 服务时间:使用资源完成一个请求的时间;
我就对上面进行翻译,然后进行举例
400 req = 200req/s * 2s
Wait time包括queue time 和service time;service time包括 system time(Tsys)和user time(Tuser);
等待时间包括队列时间和服务时间,服务时间包括Tsys和Tuser;
Tsys: time in kernel mode.
Tuser: time in user mode.
Requests are buffered in memory;请求缓存在内存中;
Reduce queue time, reduce service time;减少队列时间,减少服务时间;
Real time is how long the program took to complete,
real time(Wait time)是程序执行完毕所需要的时间,
真实时间也就是W时间;
User time is how many seconds of CPU time the program needed to execute its instructions.
user time是程序执行期间cpu指令运行需要的时间,应用程序时间
System time is how mnay seconds of CPU time the program spent executing kernel system calls or waiting for IO.
system time执行
内核系统调用的cpu时间或者是等待IO的时间,或者叫内核时间
进行系统调优我们还必须学会两个命令:sar iostat
实例:计算命令或者程序运行的时间,包括各种时间,最后计算队列时间
[root@nagiosadmin tmp]# export TIME="%e
%U %S"
[root@nagiosadmin tmp]# /usr/bin/time tar czf /tmp/demo.tgz /etc 2>&1 |tail -1 | awk 'BEGIN{print "W", "S", "U", "Q"}{print $1, $2, $3, $1 - $2 - $3}'
W S U Q
6.58 6.39 0.30 -0.11
[root@nagiosadmin tmp]#解释:这是man time之后的结果
When the -p option is given the (portable) output format
real %e
user %U
sys %S
is used.
%e
(Not in tcsh.) Elapsed real time (in seconds).
%E
Elapsed real time (in [hours:]minutes:seconds).
%S
Total number of CPU-seconds that the process spent in kernel mode.
%U
Total number of CPU-seconds that the process spent in user mode.
9.
查找程序运行的热点
判断某个脚本执行过程中,什么动作执行的次数最多,那些动作执行的时间最长,查找代码中的热点
我们要使用跟踪命令
这个就进行举例说明吧
[root@shmnginx ~]# strace -fc dd if=/dev/zero of=/tmp/test bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 0.324779 seconds, 323 MB/s
% time
seconds usecs/call calls errors syscall
------ ----------- ----------- --------- --------- ----------------
99.84
0.298930 2902 103 write
0.16
0.000471 4 105 read
0.00
0.000000 0 14 6 open
0.00
0.000000 0 11 1 close
0.00
0.000000 0 6 fstat
0.00
0.000000 0 1 lseek
0.00
0.000000 0 16 mmap
0.00
0.000000 0 7 mprotect
0.00
0.000000 0 3 munmap
0.00
0.000000 0 3 brk
0.00
0.000000 0 6 rt_sigaction
0.00
0.000000 0 1 rt_sigprocmask
0.00
0.000000 0 1 1 access
0.00
0.000000 0 1 execve
0.00
0.000000 0 1 getrlimit
0.00
0.000000 0 1 arch_prctl
0.00
0.000000 0 1 futex
0.00
0.000000 0 1 set_tid_address
0.00
0.000000 0 2 clock_gettime
0.00
0.000000 0 1 set_robust_list
------ ----------- ----------- --------- --------- ----------------
100.00
0.299401 285 8 total
[root@shmnginx ~]#
可以看出写的操作和系统调用还有时间比较多;
10.
利用sar和iostat命令进行系统数据测试和计算
首先搞明白单位之间的换算:
b bit 位
B Byte字节
Mbps Million bit per second 兆位/秒
1Byte=8bit
100Mbps=100*1024*1024bit/s=100*1024*1024/8Byte/s=100/8MB/s=12.5MB/s
Bandwidth: data + overhead;
Throughput(
X): data only (eg..Mbps)
Overhead: cost of doing work(eg..Mbps)
B=X+O
带宽=throughput+开销
Arrival rate(A): rate at which work is requested.
Completion rateZ(C): rate at which work is performanced.
Steady state: condition in which A=C比较理想的状态时到达率=完成率
Utilization law = service time * arrival rate
U=S*A
在理想状态下,U=1,A=C,那么CS=1
根据上面的公式和概念我们来计算下面
[root@bbsdatabase ~]# iostat -x 1 2
Linux 2.6.18-238.19.1.el5PAE (bbsdatabase)
2012年11月22日
avg-cpu: %user
%nice %system %iowait %steal %idle
2.17 0.01 4.91 0.20 0.00 92.71
Device:
rrqm/s wrqm/s r/s w/s rsec/s wsec/s avgrq-sz avgqu-sz await svctm %util
sda
0.45 84.68 5.70 8.82 244.06 748.11 68.33 0.12 7.93 1.05 1.53
sda1
0.00 0.00 0.00 0.00 0.01 0.00 27.64 0.00 3.82 3.21 0.00
sda2
0.45 84.68 5.70 8.82 244.05 748.11 68.33 0.12 7.93 1.05 1.53
dm-0
0.00 0.00 6.14 93.51 244.04 748.11 9.96 5.81 58.28 0.15 1.52
dm-1
0.00 0.00 0.00 0.00 0.00 0.00 8.00 0.00 5.97 0.81 0.00
-x
Display extended statistics.
%user
Show the percentage of CPU utilization that occurred while executing at the user level (application).用户级别,程序
%nice
Show the percentage of CPU utilization that occurred while executing at the user level with nice priority.计算程序优先级的时间
%system Show the percentage of CPU utilization that occurred while executing at the s
ystem level (kernel).内核级别
%iowait
Show the percentage of time that the CPU or CPUs were idle during which the system had an outstanding disk I/O request.磁盘io等待
%steal
Show the percentage of time spent in involuntary wait by the virtual CPU or CPUs while the hypervisor was servicing another vir-tual processor.cpu指令转换
%idle
Show the percentage of time that the CPU or CPUs were idle and the system did not have an outstanding disk I/O request.空闲
Tps
Indicate the number of transfers per second that were issued to the device. A transfer is an I/O request to the device. 每秒发送到设备的IO的请求数量
Multiple logical requests can be combined into a single I/O request to the device. A transfer is of indeterminate size.多个请求可以合并成一个请求IO请求,这时候transfer就是他的大小;
Blk_read/s
Indicate the amount of data read from the device expressed in a number of blocks per second. Blocks are equivalent to sectors
with 2.4 kernels and newer and therefore have a size of 512 bytes. With older kernels, a block is of indeterminate size.每秒读取的block数量
Blk_wrtn/s
Indicate the amount of data written to the device expressed in a number of blocks per second.
Blk_read
The total number of blocks read.
Blk_wrtn
The total number of blocks written.
r/s
The number of read requests that were issued to the device per second.每秒到达设备的读请求的数量
w/s
The number of write requests that were issued to the device per second.
rsec/s
The number of sectors read from the device per second.每秒读取的扇区数
wsec/s
The number of sectors written to the device per second.
avgrq-sz
The average size (in sectors) of the requests that were issued to the device.每个请求的平均大小,单位是sectors
avgqu-sz
The average queue length of the requests that were issued to the device.请求的平均队列长度大小
Await
The average time (in milliseconds) for I/O requests issued to the device to be served. This includes the time spent by the requests in queue and the time spent servicing them.平均等待时间,单位ms
Svctm
The average service time (in milliseconds) for I/O requests that were issued to the device.平均服务时间
单位ms
%util
Percentage of CPU time during which I/O requests were issued to the device (bandwidth utilization for the device). Device satura-tion occurs when this value is close to 100%.
对于上面每一个选项我都熟悉的情况下我们来进行计算:
根据第一行的数据,我们来计算队列长度:L
队列长度:L= A*W=[(5.70+8.82)requests/s ] * [7.93ms] = .1151436 requests
磁盘吞吐量:
Xdisk=(244.06+748.11)sectors/s=992.17sectors/s=992.17*512/1024/1024MB/s= 496.085KiB/s
利用率:U=A*S=[(5.70+8.82)requests/s ]*1.05ms=15.2460/1000=0.0152460