linux performance tuning

Stand-alone Tuning:
Cause analysis of performance bottlenecks, fix it.
 
cpu subsystem
Memory Subsystem
IO Subsystem
network system
 
 
 
@cpu subsystem tuning
Technical indicators cpu xeon E5520 2.27GHz 8192kb
# cat /proc/cpuinfo
# dmidecode
# dmidecode --type cache
# dmidecode --type processor
--- interrupt an event occurs, cpu stop execution of the current instruction, the instruction that caused the event in favor of the implementation of the program produced, finished, restore instruction before execution is suspended. Causing IO (storage devices, network transmission)
 
Context switching
 
--- kernel process control priority
In addition to calling various kernel driver to complete the driver of the hardware
The deployment of a variety of system resources:
Hardware Resources
The priority of each process of adjustment, to be scheduled.
Complete the above task is completed by the corresponding processes, which are known as system processes (kernel processes)
 
User Process
 
Run queue (the system load) Uptime
 
# uptime
14:24:16 up 4:20, 5 users, load average: 0.06, 0.09, 0.18
1 minute, 5 minutes, 15 minutes
Load, pressure system is representative of the past, the larger the number, indicating that the more busy system.
Load running is to represent the average queue length. If the higher the number, indicating that the more waiting in line cpu processing process.
 
Single core cpu:
Over the past 4 years, the average length of the run queue is four. (In fact three in the waiting process, a process that is being processed cpu). In other words, the load is 4.
Usually a core cpu its load (run queue) should not be greater than 3.
 
Dual-core cpu:
Over the past 4 years, average core cpu run queue length is 4/2 = 2.
Because the average download, index 3 lower than usual, so the system is not busy.
 
 
cpu utilization
A user process us, the program performed by the user
Kernel scheduler sy, kernel scheduling (process interrupts, context switching)
Free id
Etc. wait io wa
vmstat,top,uptime,mpstat,dstat,sar -u / -q
 
 
# top
可以交互界面下,动态查看负载,系统的进程等信息。
在查找性能瓶颈的时候,主要用它来查看当前哪些进程最活跃。最活跃进程通常都是占用资源最多的进程。
shift + M 以内存使用百分比排序
shift + P 以cpu使用百分比排序
 
# vmstat 2 <---每2秒中输出一次最新的数据。
 
# vmstat 2 3 <---只输出3次数据
 
procs --system-- -----cpu-----
r b in cs us sy id wa st
0 0 728 913 29 2 68 0 0
0 0 301 255 2 1 98 0 0
0 0 472 569 1 1 98 0 0
 
r 当前运行队列中有多少个进程(uptime看到的负载)
b 被阻塞进程有多少个,只要看到数字,都应该留意,一般被阻塞的进程都是由于IO导致。
in 中断数 , 数字越大,说明操作系统越活跃,操作系统在充分发挥它该做的事情。
cs 上下文切换数, 数字越大,说明操作系统越活跃,操作系统在充分发挥它该做的事情。
 
us 用户进程使用的cpu百分比
sy 内核进程使用的cpu百分比
us : sy = 7 : 3
id 空间的cpu百分比
wa cpu花在等待的时间的备份比。一般产生wa时间都是由于某个进程被阻塞了,在慢设备上读写数据,然后让cpu被迫停止等待指令执行完毕。
一直都出现wa时间,就应该注意。
 
 
 
# mpstat -p ALL <---分别查看每个核心的使用情况
 
# yum install sysstat -y
# sar -u <---输出过去24小时内sar -u每10分钟捕获一次历史数据。
# sar -u 2
# sar -q 2
 
案例数据分析
 
===================================================================
@内存子系统调优
虚拟内存
内存页 page
作为系统分配内存空间的最基本的单位。内核默认的内存也多大?
内存分页(paging)--内核就必须经常扫描内存空间并且收回其中未被使用的内存页,把数据同步到硬盘
kswapd
pdflush 进程负责将内存中的内容和文件系统进行同步操
 
# free -m
total used free shared buffers cached
Mem: 3925 2114 1810 0 109 1461
-/+ buffers/cache: 543 3381
Swap: 4095 0 4095
 
buffers
缓冲区,用于缓存块设备的数据。这些数据是不经过文件系统的数据。
 
cached
高速缓存,用于缓存文件系统的数据。默认情况下,Linux在内存空间允许的情况下,把曾经打开过的文件都缓存到这里。
 
如果放在内存中的数据被修改过了,这些数据叫做“脏数据”。pdflush 进程定期扫描内存中的脏数据,把脏数据同步到磁盘上。同步之后的数据,叫做干净数据。
如果出现内存空间不足的时候,就会把脏数据马上同步到磁盘,把同步后的数据,从内存中删除掉。
如果内存中的数据并不是来自磁盘,而是进程在运行过程中产生,如果遇到内存空间不足的时候,而这部分数据当前不许要马上使用,就会被置换到swap分区。
 
 
# vmstat 2 3
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 0 1849492 112304 1500544 0 0 48 18 644 751 23 2 75 0 0
0 0 0 1848020 112312 1501792 0 0 0 12 1248 813 3 1 95 1 0
0 0 0 1851492 112312 1498248 0 0 0 0 707 392 1 0 99 0 0
 
si 有多少数据从swap加载到内存中 (page in)
so 有多少数据从内存中保存到swap中 (page out)
swpd 使用了多少的swap分区空间
 
 
 
 
如果发现某个系统是存在内存瓶颈,想知道哪个进程使用了大量的内存,使用top指令查看最活跃的进程,或者按内存使用百分比输出结果。
找到这个进程之后,就可以使用strace去跟踪进程,看一下进程到底在调用什么的系统函数,结合进程本身作用,推断出进程背后在做些什么?
 
例如: 如果现在系统出现性能瓶颈,top指令发现mysqld进程最活跃》
# strace -F -p 2234 <--- 2234是mysqld进程pid
 
 
 
主页面故障:
程序运行的时候所需要的数据并不在内存中,就会产生主页面故障。
次页面故障
程序运行的时候所需要的数据已经在内存中,就会产生次页面故障。
vmstat , time , sar -B ,free
 
 
 
# /usr/bin/time -v date
2012年 12月 28日 星期五 16:29:28 CST
...
Major (requiring I/O) page faults: 0 主页面故障
Minor (reclaiming a frame) page faults: 234 次页面故障
...
Page size (bytes): 4096
 
# sar -B 2
 
 
=================================================================
@IO子系统调优
IO 输入输出(写读硬盘)
IOPS计算
IOPS 每秒钟完成多少个IO指令。越多就越好。
在最好发挥硬盘性能的条件下去测试的:
10K 120-150 IOPS
15K 150-200 IOPS
IO吞吐量计算
每个IO指令完成了多少的数据。 数字越大,代表设备性能越好。
 
IO分类
随机IO
顺序IO
iostat
 
# iostat -d sda2 2
Linux 2.6.32-220.el6.x86_64 (www.upl.com) 2012年12月28日 _x86_64_ (2 CPU)
 
Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn
sda2 3.21 127.32 22.67 3079282 548200 <---开机到现在的平均数据
 
Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn
sda2 0.00 0.00 0.00 0 0 <--过去2秒钟的平均数据
 
 
tps 平均每秒钟发生的事务数(IO) = IOPS
Blk_read/s 平均每秒中读取了多少块数据。 1块=1扇区=512字节
Blk_read 过去2秒钟中读取了多少块的数据
 
 
# iostat -d sda2 2 -k
Linux 2.6.32-220.el6.x86_64 (www.upl.com) 2012年12月28日 _x86_64_ (2 CPU)
 
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda2 3.20 63.13 11.39 1539641 277756
 
-k 以KB 单位输出
-m 以MB 为单位输出
 
 
测试设备的读写性能,以下是顺序IO的测试结果:
# mount -o remount,sync /dev/sda6
# dd if=/dev/zero of=/rhci/1Gb bs=1M count=1024
 
记录了1024+0 的读入
记录了1024+0 的写出
1073741824字节(1.1 GB)已复制,55.7702 秒,19.3 MB/秒
 
 
# iostat -m -d sda6 2
Linux 2.6.32-220.el6.x86_64 (www.upl.com) 2012年12月28日 _x86_64_ (2 CPU)
 
Device: tps MB_read/s MB_wrtn/s MB_read MB_wrtn
sda6 0.94 0.00 0.06 3 1397
 
Device: tps MB_read/s MB_wrtn/s MB_read MB_wrtn
sda6 326.00 0.12 18.36 0 36
 
Device: tps MB_read/s MB_wrtn/s MB_read MB_wrtn
sda6 360.00 0.00 20.47 0 40
 
Device: tps MB_read/s MB_wrtn/s MB_read MB_wrtn
sda6 306.00 0.00 17.40 0 34
 
Device: tps MB_read/s MB_wrtn/s MB_read MB_wrtn
sda6 332.50 0.00 18.93 0 37
 
 
数据 ---ext4驱动---> sda6
 
 
 
不经过文件系统,直接写快设备:非常危险,注意!!
以下实验中的/dev/sda7是没有重要数据,里面的数据测试后会被彻底损坏。
# dd if=/dev/zero of=/dev/sda7 bs=1M count=1024
记录了1024+0 的读入
记录了1024+0 的写出
1073741824字节(1.1 GB)已复制,12.0819 秒,88.9 MB/秒
 
数据---> sda7
 
 
优化手段:分区、文件系统挂载,块大小,高级文件系统e4fsprogs,IO算法
 
 
分区
 
1k 80KB 就需要分配80次
4k 80KB 只需要分配20次
 
# mkfs.ext4 -b 4096 /dev/sda8
 
rhel5 为了实用性能更好的文件系统ext4,可以安装软件包:e4fsprogs
 
noatime 不更新文件的访问时间。
# mount -o noatime /dev/sda8 /webroot
noacl 不许要的功能可以去掉。
 
1eb --1024PB ---> -- 1024*1024TB ---> 1024*1024*1024GB
16TB
 
 
IO算法
 
# cat /sys/block/sda/queue/scheduler
noop anticipatory deadline [cfq]
 
临时生效:
# echo "deadline" > /sys/block/sda/queue/scheduler
 
cfq 完全公平的算法。内核会给每个程序单独维护一个IO队列,每次都从每个队列中取出部分IO指令执行。
 
deadline 在更多的时间内完成更多的IO。有可能出现不公平的现象。主要用于功能单一的环境。例如: 这是一台仅仅作为web服务用的服务器,那么就可以使用deadline.
 
anticipatory 预读算法。主要适合那种老式的非常慢的设备。是旧内核的默认算法。每执行玩一个IO或者一系列IO后,会花2ms的时间去预测下次IO到底会发生在哪个地方,事先把猜测的数据读取了。
 
noop 不使用任何算法。 所有进程共用一个IO队列,IO执行的顺序使用的是产生IO的顺序
 
 
 
 
开机就生效:
title Red Hat Enterprise Linux (2.6.32-220.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-220.el6.x86_64 ro root=UUID=627d0a6a-e81e-4750-891e-1466c6932e9f ... ... elevator=deadline
 
 
 
@网络子系统调优
工具:
mii-tool
# mii-tool eth0
# ethtool eth0
 
如果网卡的工作模式自动识别出错,可以手工更改
# ethtool -s eth0 speed 100 duplex full autoneg off
 
iptraf 测试当前网卡的实时流量情况
iptraf-3.0.0.tar.gz
# iptraf -d eth0
 
 
netperf 测试两个服务器时间的实际最大传输速度。
netperf-2.5.0.tar.bz2
基于c/s模式测试
 
建议:
局域网之前的测试,一般测试的速度都比较理想
跨机房的测试。
netserver
netperf -H 192.168.0.10 -l 30
 
 
# netperf -H 10.1.1.140 -l 30
MIGRATED TCP STREAM TEST from 0.0.0.0 (0.0.0.0) port 0 AF_INET to 10.1.1.140 (10.1.1.140) port 0 AF_INET
Recv Send Send
Socket Socket Message Elapsed
Size Size Size Time Throughput
bytes bytes bytes secs. 10^6bits/sec
 
87380 16384 16384 30.31 93.04
 
 
 
多网卡绑定
多个网卡使用软件绑定,就像是一个网卡一样使用,网卡的传输带宽就累计。
 
eth0 ----\
抽象成bond0 swtich
eth1 ----/
 
 
 
前提: 绑定的网卡强烈建议是同一型号。
 
 
临时绑定的:
 
rhel6:
 
1、加载对应的绑定网卡使用的驱动模块。
 
# modprob bonding mode=0 miimon=100
 
mode设定工作模式
0 负载均衡模式 (两个网卡同时使用,理论上带宽翻倍)
1 高可用模式 (其中一个网卡工作,另外的网卡备用)
 
miimon=100 每100毫秒对网卡进行检测。
 
 
 
 
2、激活虚拟网卡(绑定后的逻辑网卡,以后配置IP的就是该网卡,物理网卡不需要配置IP)
 
rhel6必须先把NetworkManager关闭:
# service NetworkManager stop
# chkconfig NetworkManager off
 
# ifconfig bond0 10.1.1.28 netmask 255.255.255.0 up
 
# ifenslave bond0 eth0 eth1 <---把物理网卡eth0,eth1绑定到bond0
 
表面上看,配置了IP和发送数据都是使用bond0,实际上是bonding软件驱动,使用物理网卡在底层发送数据。
 
# ifconfig
bond0 Link encap:Ethernet HWaddr 52:54:00:C3:B8:57
inet addr:10.1.1.28 Bcast:10.1.1.255 Mask:255.255.255.0
inet6 addr: fe80::5054:ff:fec3:b857/64 Scope:Link
UP BROADCAST RUNNING MASTER MULTICAST MTU:1500 Metric:1
。。。。
 
eth0 Link encap:Ethernet HWaddr 52:54:00:C3:B8:57
UP BROADCAST RUNNING SLAVE MULTICAST MTU:1500 Metric:1
。。。
 
eth1 Link encap:Ethernet HWaddr 52:54:00:C3:B8:57
UP BROADCAST RUNNING SLAVE MULTICAST MTU:1500 Metric:1
。。。
 
 
 
 
临时取消绑定:
 
# ifenslave -d bond0 eth0 eth1
 
# ifconfig bond0 down
 
# rmmod bonding <--卸载模块
 
 
 
方法2: 编写配置文件
 
# vim /etc/modprobe.d/dist.conf
 
alias bond0 bonding
options bonding miimon=100 mode=0
 
# vim /etc/sysconfig/network-scripts/ifcfg-bond0
DEVICE=bond0
ONBOOT=yes
BOOTPROTO=static
IPADDR=1.1.1.138
NETMASK=255.255.255.0
NM_CONTROLLED="no" 《——————
MASTER=yes
 
# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
BOOTPROTO="none"
HWADDR="52:54:00:C3:B8:57"
NM_CONTROLLED="no" 《---
ONBOOT="yes"
MASTER=bond0
SLAVE=yes
 
# vim /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE="eth1"
BOOTPROTO="none"
HWADDR="52:54:00:42:C1:8A"
NM_CONTROLLED="no" 《----
ONBOOT="yes"
MASTER=bond0
SLAVE=yes
 
# service network restart
 
 
测试
在绑定的机器上看网卡流量:
# watch "ifconfig |grep 'RX packets' | head -3 |tail -2"
Every 2.0s: ifconfig |grep 'RX packets' | ... Wed Jan 2 03:22:42 2013
 
RX packets:9532 errors:0 dropped:0 overruns:0 frame:0
RX packets:9470 errors:0 dropped:0 overruns:0 frame:0
 
 
从另外一台机器发送大文件到绑定的机器
 
 
 
 

Guess you like

Origin www.cnblogs.com/steven9898/p/11331151.html