linux一些优化相关的东西
<Linux内核设计与实现>>
<<深入理解Linux内核原理>>
<<高级编程Unix环境>>
平衡cpu、内存、I/O、及网络
硬件体系结构:控制器、运算器、存储器、输入和输出设备
CPU:cisc(complex instruction set computing复杂指令集)(低端)(如x86,ia32)与risc(reduced instruction set computing精简指令集)(趋势)(高端)(如ia64)
总线:系统总线和I/O总线
性能优化原则:一次只调一项,使用多种工具
系统性能指标:响应时间(从用户的角度看),吞吐量(从系统管理员的角度看)
进程与线程:
进程(process)是资源管理的最小单位
线程(thread)是程序执行的最小单位
32位与64位:
64位操作系统支持大于4GB的内存寻址
性能优化就是找到系统处理中的瓶颈以及去除这些的过程。
性能优化其实是对OS 各子系统达到一种平衡的定义,这些子系统包括了:
CPU
Memory
IO
Network
这些子系统之间关系是相互彼此依赖的,任何一个高负载都会导致其他子系统出现问题.比如:
大量的页调入请求导致内存队列的拥塞
网卡的大吞吐量可能导致更多的 CPU 开销
大量的 CPU 开销又会尝试更多的内存使用请求
大量来自内存的磁盘写请求可能导致更多的 CPU 以及 IO 问题
所以要对一个系统进行优化,查找瓶颈来自哪个方面是关键,虽然看似是某一个子系统出现问题,其实有可能是别的子系统导致的.
要调优得分析,OS是什么?是什么应用?要调什么?
1、先看一些工具
uptime
10:35:38 up 1:31, 3 users, load average: 0.46, 0.36, 0.28
hostname
uname -a
id
who
last
w
free (
buffer/cache
used=used-buffer-cache
swap 如果交换分区占用了,则当前内存可能会成为瓶颈
)
------ free将/proc/meminfo读出,格式化输出了
[root@xen ~]# cat /proc/meminfo
MemTotal: 1720320 kB
MemFree: 909000 kB
Buffers: 41500 kB
Cached: 385016 kB
SwapCached: 0 kB
Active: 347340 kB 活跃内存,指进程一直读写的内存空间
Inactive: 346992 kB 非活跃内存
kernel总是把不活跃的内存交换到swap空间。如果inactive内存多,加swap空间可以解决问题,而active多,则考虑加内存
HighTotal: 981512 kB
HighFree: 318568 kB
LowTotal: 738808 kB
LowFree: 590432 kB
SwapTotal: 4096532 kB
SwapFree: 4096532 kB
cat /proc/cpuinfo
processor 0,1,2,3.....
cpu MHZ
cache size
flags
[root@xen ~]# cat /proc/cmdline
ro root=LABEL=/ rhgb quiet
cat /proc/devices
cat /proc/diskstates (fdisk -l)
cat /proc/filesystem (nodev 表示支持但不存在,没使用)
cat /proc/interrupts
cat /proc/mdstat
cat /proc/modules(lsmod)
cat /proc/mounts(mount)
cat /proc/partitions
cat /proc/swaps
cat /proc/uptime (启动时间计时 s)
1, 从内核启动时间算 2,从应用程序启动时间算
/proc/sys下一般可改变
ls /proc/numberdir
man 5 proc
2,
ps
ps a
ps au
ps axu
pstree -p
man ps
stat : S R Z
D
(
中断
硬件中断
软件中断
)
L(内存锁,防止交换--解决swap信息泄漏,锁住内存,防止交换)
s session header 父子/父子 ----> 父
+ 前台进程
l
<
N
虚拟内存空间
实际内存空间
每个进程有2的32次方的空间可用,但实际只用了几K或者几M,由内核映射到直接的物理地址上。
其差别在于:很多进程要占用同一段内存空间,如库文件,PAM组件等
共享库 省内存,有竞争,加锁
静态库 多读少写
编译apache模块的动静态哪个更合适?
(调优时,一定要看是什么应用)
VSZ RSS当使用动态库时,实际 地址空间,一定会比虚拟地址空间要低
pmap $$ 看当前的PID有哪些库
10489: bash
00110000 36K r-x-- /lib/libnss_files-2.5.so
00119000 4K r---- /lib/libnss_files-2.5.so
0011a000 4K rw--- /lib/libnss_files-2.5.so 有 w就是 静态 没有的是共享库
00583000 4K r-x-- [ anon ]
00838000 104K r-x-- /lib/ld-2.5.so
00852000 4K r---- /lib/ld-2.5.so
00853000 4K rw--- /lib/ld-2.5.so
00856000 1288K r-x-- /lib/i686/nosegneg/libc-2.5.so
00998000 4K ----- /lib/i686/nosegneg/libc-2.5.so
00999000 8K r---- /lib/i686/nosegneg/libc-2.5.so
0099b000 4K rw--- /lib/i686/nosegneg/libc-2.5.so
0099c000 12K rw--- [ anon ]
009ca000 8K r-x-- /lib/libdl-2.5.so
009cc000 4K r---- /lib/libdl-2.5.so
009cd000 4K rw--- /lib/libdl-2.5.so
009d0000 12K r-x-- /lib/libtermcap.so.2.0.8
009d3000 4K rw--- /lib/libtermcap.so.2.0.8
08047000 696K r-x-- /bin/bash
080f5000 20K rw--- /bin/bash
080fa000 20K rw--- [ anon ]
092c5000 264K rw--- [ anon ]
b7c97000 8K rw--- [ anon ]
b7c99000 28K r--s- /usr/lib/gconv/gconv-modules.cache
b7ca0000 4K r---- /usr/lib/locale/locale-archive
b7ca1000 240K r---- /usr/lib/locale/locale-archive
b7cdd000 864K r---- /usr/lib/locale/locale-archive
b7db5000 2048K r---- /usr/lib/locale/locale-archive
b7fb5000 8K rw--- [ anon ]
b7fcc000 4K rw--- [ anon ]
bfc95000 84K rw--- [ stack ]
total 5796K
ps -l
ps -le
ps -p PID
ps -p PID -o %cpu,%mem,rss,vsz,cmd
kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL
5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE
9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2
13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT
17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU
25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH
29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN
35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4
39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12
47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14
51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10
55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6
59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
软中断 (信号来实现)
ctrl +C (3)
[root@xen ~]# ps -p $$ -o %cpu,%mem,rss,vsz,cmd
%CPU %MEM RSS VSZ CMD
0.0 0.0 1556 4932 bash
ps 查看线程的参数
ps-p PID -L
[root@xen ~]# ps -p 3208 -L
PID LWP TTY TIME CMD (LWP 轻量级线程ID)
3208 3208 ? 00:00:00 tgtd
3208 3216 ? 00:00:00 tgtd
3208 3217 ? 00:00:00 tgtd
3208 3218 ? 00:00:00 tgtd
3208 3219 ? 00:00:00 tgtd
3208 3220 ? 00:00:00 tgtd
watch -n 1 ps
top
<
>
k
r
q
top -d 0.1
top -d 1
top -p PID
top -p PID1 PID2 ...
top -p PID -n 3 刷新3次
top----> f ----> *的列显示
top -----> o ----> p
-------------------------------------------------------------------------------------
vmstat 对内存进程临近
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 513504 218524 474192 0 0 65 13 239 288 2 0 95 2 0
man vmstat
[root@xen ~]#
r 运行状态的进程个数
b 阻塞状态的进程个数 进程读取外设上的数据,等待时
si swapin swap换入到内存
so swapout 内存换出到swap 换出的越多,内存越不够用
bi blockin 从硬盘往内存读
bo blockout 从内存拿出到硬盘 (周期性的有值)
判断是读多还是写多,是否有i/o瓶颈
Procs
r: The number of processes waiting for run time.
b: The number of processes in uninterruptible sleep.
Memory
swpd: 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)
Swap
si: Amount of memory swapped in from disk (/s).
so: Amount of memory swapped to disk (/s).
IO
bi: Blocks received from a block device (blocks/s).
bo: Blocks sent to 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: 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.
vmstat 1 5
1刷新间隔 5 刷新次数
CPU专用的
[root@xen ~]# rpm -qf `which mpstat`
sysstat-7.0.2-3.el5
mpstat
mpstat -P 0
mpstat -P 1
mpstat 1 5
I/O专用的
iostat
net专用的
netstat
netstat -i
netstat -r
sar 默认显示每10分钟统计一次状态信息 (从装sysstat包开始)
sar 1
sar 1 10
sar -P 0 1 10
sar最大特点是可以监控所有状态
sar -r 1
sar -n ALL
sar -b 1 10
sar -I irq#
sar -I ALL 1 0
sar -s 15:00:00 -e 15:30:00
sar -s 15:00:00 -e 15:30:20 -f /var/log/sa/sa01
sar -r -s 15:00:00 -e 15:30:20 -f /var/log/sa/sa01
vmstat
mpstat
iostat
netstat
sar
ps
pmap
ld
ldconfig
cat /etc/ld.so.conf
ldconfig -f /etc/ld.so.conf
nm
top -b -n 1