linux狀態及原理全剖析

Table of Contents

1 linux

 

1.1 proc filesystem

 

1.1.1 /proc

 
1.1.1.1 /proc/meminfo

系統中關於當前內存的利用情況等的信息,常由free命令使用;可使用文件查看命令直接讀取此文件,其內容顯示爲兩列,前者爲統計屬性,後者爲對應的值;php

[dirlt@umeng-ubuntu-pc] > cat /proc/meminfo
MemTotal:        3960316 kB
MemFree:          202908 kB
Buffers:           53632 kB
Cached:           764004 kB
SwapCached:       131884 kB
Active:          2484752 kB
Inactive:        1016724 kB
Active(anon):    2280636 kB
Inactive(anon):   814792 kB
Active(file):     204116 kB
Inactive(file):   201932 kB
Unevictable:          32 kB
Mlocked:              32 kB
SwapTotal:       4103164 kB
SwapFree:        2678984 kB
Dirty:              1492 kB
Writeback:             0 kB
AnonPages:       2629544 kB
Mapped:           122244 kB
Shmem:            411588 kB
Slab:             125096 kB
SReclaimable:      83480 kB
SUnreclaim:        41616 kB
KernelStack:        5904 kB
PageTables:        50164 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     6083320 kB
Committed_AS:    7203968 kB
VmallocTotal:   34359738367 kB
VmallocUsed:      362948 kB
VmallocChunk:   34359276724 kB
HardwareCorrupted:     0 kB
AnonHugePages:         0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:       57344 kB
DirectMap2M:     4052992 kB
1.1.1.2 /proc/stat

實時追蹤自系統上次啓動以來的多種統計信息;以下所示,其中,css

  • 「cpu」行後的八個值分別表示以1/100(jiffies)秒爲單位的統計值(包括系統運行於用戶模式、低優先級用戶模式,運系統模式、空閒模式、I/O等待模式的時間等);
  • 「intr」行給出中斷的信息,第一個爲自系統啓動以來,發生的全部的中斷的次數;而後每一個數對應一個特定的中斷自系統啓動以來所發生的次數;
  • 「ctxt」給出了自系統啓動以來CPU發生的上下文交換的次數。
  • 「btime」給出了從系統啓動到如今爲止的時間,單位爲秒;
  • 「processes (total_forks) 自系統啓動以來所建立的任務的個數目;
  • 「procs_running」:當前運行隊列的任務的數目;
  • 「procs_blocked」:當前被阻塞的任務的數目;
[dirlt@umeng-ubuntu-pc] > cat /proc/stat
cpu  25175536 13184 10088010 208990815 1165440 37 29476 0 0 0
cpu0 7597990 7286 3089511 49810301 706702 37 23258 0 0 0
cpu1 7914370 3065 2901460 50300065 188223 0 2403 0 0 0
cpu2 4922712 1228 2039477 54300492 171151 0 1921 0 0 0
cpu3 4740463 1604 2057560 54579955 99363 0 1892 0 0 0
intr 8881387947 2112 3 0 0 0 0 0 0 1 0 0 0 3175452 0 0 0 0 0 0 0 3554144 0 0 224677 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6681668 12 51519086 12401 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ctxt 12584201613
btime 1345525951
processes 234445
procs_running 3
procs_blocked 0
softirq 304218476 0 151026247 103572 14071723 3554590 0 262575 55846497 864372 78488900
1.1.1.3 /proc/swaps

當前系統上的交換分區及其空間利用信息,若是有多個交換分區的話,則會每一個交換分區的信息分別存儲於/proc/swap目錄中的單獨文件中,而其優先級數字越低,被使用到的可能性越大;html

[dirlt@umeng-ubuntu-pc] > cat /proc/swaps
Filename                                Type            Size    Used    Priority
/dev/sda5                               partition       4103164 1423572 -1
1.1.1.4 /proc/cmdline

在啓動時傳遞至內核的相關參數信息,這些信息一般由lilo或grub等啓動管理工具進行傳遞;java

[dirlt@umeng-ubuntu-pc] > cat /proc/cmdline
BOOT_IMAGE=/boot/vmlinuz-3.2.0-27-generic root=UUID=b25db1c7-e694-4eed-8d11-ea33047338cb ro quiet splash vt.handoff=7
1.1.1.5 /proc/uptime

系統上次啓動以來的運行時間,以下所示,其第一個數字表示系統運行時間,第二個數字表示系統空閒時間,單位是秒;系統上次啓動以來的運行時間,以下所示,其第一個數字表示系統運行時間,第二個數字表示系統空閒時間,單位是秒;node

[dirlt@umeng-ubuntu-pc] > cat /proc/uptime
621137.39 2116303.76
1.1.1.6 /proc/version

當前系統運行的內核版本號python

[dirlt@umeng-ubuntu-pc] > cat /proc/version
Linux version 3.2.0-27-generic (buildd@allspice) (gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) ) #43-Ubuntu SMP Fri Jul 6 14:25:57 UTC 2012
1.1.1.7 /proc/mounts

系統當前掛載的全部文件系統.第一列表示掛載的設備,第二列表示在當前目錄樹中的掛載點,第三點表示當前文件系統的類型,第四列表示掛載屬性(ro或者rw),第五列和第六列用來匹配/etc/mtab文件中的轉儲(dump)屬性;linux

[dirlt@umeng-ubuntu-pc] > cat /proc/mounts
rootfs / rootfs rw 0 0
sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0
proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
udev /dev devtmpfs rw,relatime,size=1971356k,nr_inodes=492839,mode=755 0 0
devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0
tmpfs /run tmpfs rw,nosuid,relatime,size=792064k,mode=755 0 0
/dev/disk/by-uuid/b25db1c7-e694-4eed-8d11-ea33047338cb / ext4 rw,relatime,errors=remount-ro,user_xattr,barrier=1,data=ordered 0 0
none /sys/fs/fuse/connections fusectl rw,relatime 0 0
none /sys/kernel/debug debugfs rw,relatime 0 0
none /sys/kernel/security securityfs rw,relatime 0 0
none /run/lock tmpfs rw,nosuid,nodev,noexec,relatime,size=5120k 0 0
none /run/shm tmpfs rw,nosuid,nodev,relatime 0 0
/dev/sda3 /home ext4 rw,relatime,user_xattr,barrier=1,data=ordered 0 0
binfmt_misc /proc/sys/fs/binfmt_misc binfmt_misc rw,nosuid,nodev,noexec,relatime 0 0
gvfs-fuse-daemon /home/dirlt/.gvfs fuse.gvfs-fuse-daemon rw,nosuid,nodev,relatime,user_id=1000,group_id=1000 0 0
1.1.1.8 /proc/modules

當前裝入內核的全部模塊名稱列表,能夠由lsmod命令使用,也能夠直接查看;以下所示,其中第一列表示模塊名,第二列表示此模塊佔用內存空間大小,第三列表示此模塊有多少實例被裝入,第四列表示此模塊依賴於其它哪些模塊,第五列表示此模塊的裝載狀態(Live:已經裝入;Loading:正在裝入;Unloading:正在卸載),第六列表示此模塊在內核內存(kernel memory)中的偏移量;ios

[dirlt@umeng-ubuntu-pc] > cat /proc/modules
pci_stub 12622 1 - Live 0x0000000000000000
vboxpci 23200 0 - Live 0x0000000000000000 (O)
vboxnetadp 13382 0 - Live 0x0000000000000000 (O)
vboxnetflt 23441 0 - Live 0x0000000000000000 (O)
vboxdrv 287082 3 vboxpci,vboxnetadp,vboxnetflt, Live 0x0000000000000000 (O)
bnep 18281 2 - Live 0x0000000000000000
rfcomm 47604 0 - Live 0x0000000000000000
bluetooth 180104 10 bnep,rfcomm, Live 0x0000000000000000
binfmt_misc 17540 1 - Live 0x0000000000000000
snd_hda_codec_realtek 223962 1 - Live 0x0000000000000000
ppdev 17113 0 - Live 0x0000000000000000
eeepc_wmi 13109 0 - Live 0x0000000000000000
asus_wmi 24456 1 eeepc_wmi, Live 0x0000000000000000
sparse_keymap 13890 1 asus_wmi, Live 0x0000000000000000
snd_hda_intel 33773 5 - Live 0x0000000000000000
snd_hda_codec 127706 2 snd_hda_codec_realtek,snd_hda_intel, Live 0x0000000000000000
snd_hwdep 13668 1 snd_hda_codec, Live 0x0000000000000000
snd_pcm 97188 3 snd_hda_intel,snd_hda_codec, Live 0x0000000000000000
snd_seq_midi 13324 0 - Live 0x0000000000000000
snd_rawmidi 30748 1 snd_seq_midi, Live 0x0000000000000000
snd_seq_midi_event 14899 1 snd_seq_midi, Live 0x0000000000000000
snd_seq 61896 2 snd_seq_midi,snd_seq_midi_event, Live 0x0000000000000000
snd_timer 29990 2 snd_pcm,snd_seq, Live 0x0000000000000000
snd_seq_device 14540 3 snd_seq_midi,snd_rawmidi,snd_seq, Live 0x0000000000000000
parport_pc 32866 1 - Live 0x0000000000000000
snd 78855 18 snd_hda_codec_realtek,snd_hda_intel,snd_hda_codec,snd_hwdep,snd_pcm,snd_rawmidi,snd_seq,snd_timer,snd_seq_device, Live 0
x0000000000000000
psmouse 87692 0 - Live 0x0000000000000000
serio_raw 13211 0 - Live 0x0000000000000000
soundcore 15091 1 snd, Live 0x0000000000000000
snd_page_alloc 18529 2 snd_hda_intel,snd_pcm, Live 0x0000000000000000
mac_hid 13253 0 - Live 0x0000000000000000
i915 472897 3 - Live 0x0000000000000000
wmi 19256 1 asus_wmi, Live 0x0000000000000000
drm_kms_helper 46978 1 i915, Live 0x0000000000000000
drm 242038 4 i915,drm_kms_helper, Live 0x0000000000000000
i2c_algo_bit 13423 1 i915, Live 0x0000000000000000
mei 41616 0 - Live 0x0000000000000000 (C)
video 19596 1 i915, Live 0x0000000000000000
lp 17799 0 - Live 0x0000000000000000
parport 46562 3 ppdev,parport_pc,lp, Live 0x0000000000000000
usbhid 47199 0 - Live 0x0000000000000000
hid 99559 1 usbhid, Live 0x0000000000000000
r8169 62099 0 - Live 0x0000000000000000
1.1.1.9 /proc/diskstats

每塊磁盤設備的磁盤I/O統計信息列表nginx

1.1.1.10 /proc/cpuinfo
 
1.1.1.11 /proc/crypto

系統上已安裝的內核使用的密碼算法及每一個算法的詳細信息列表git

[dirlt@umeng-ubuntu-pc] > cat /proc/crypto
name         : cbc(aes)
driver       : cbc(aes-generic)
module       : kernel
priority     : 100
refcnt       : 1
selftest     : passed
type         : blkcipher
blocksize    : 16
min keysize  : 16
max keysize  : 32
ivsize       : 16
geniv        : <default>

name         : hmac(sha256)
driver       : hmac(sha256-generic)
module       : kernel
priority     : 0
refcnt       : 2
selftest     : passed
type         : shash
blocksize    : 64
digestsize   : 32

name         : hmac(sha1)
driver       : hmac(sha1-generic)
module       : kernel
priority     : 0
refcnt       : 2
selftest     : passed
type         : shash
blocksize    : 64
digestsize   : 20

name         : stdrng
driver       : krng
module       : kernel
priority     : 200
refcnt       : 1
selftest     : passed
type         : rng
seedsize     : 0

name         : crc32c
driver       : crc32c-generic
module       : kernel
priority     : 100
refcnt       : 1
selftest     : passed
type         : shash
blocksize    : 1
digestsize   : 4

name         : aes
driver       : aes-generic
module       : kernel
priority     : 100
refcnt       : 1
selftest     : passed
type         : cipher
blocksize    : 16
min keysize  : 16
max keysize  : 32

name         : sha256
driver       : sha256-generic
module       : kernel
priority     : 0
refcnt       : 3
selftest     : passed
type         : shash
blocksize    : 64
digestsize   : 32

name         : sha224
driver       : sha224-generic
module       : kernel
priority     : 0
refcnt       : 1
selftest     : passed
type         : shash
blocksize    : 64
digestsize   : 28

name         : sha1
driver       : sha1-generic
module       : kernel
priority     : 0
refcnt       : 3
selftest     : passed
type         : shash
blocksize    : 64
digestsize   : 20

name         : md5
driver       : md5-generic
module       : kernel
priority     : 0
refcnt       : 1
selftest     : passed
type         : shash
blocksize    : 64
digestsize   : 16

name         : crc32c
driver       : crc32c-intel
module       : kernel
priority     : 200
refcnt       : 1
selftest     : passed
type         : shash
blocksize    : 1
digestsize   : 4
1.1.1.12 /proc/loadavg

保存關於CPU和磁盤I/O的負載平均值,其前三列分別表示每1秒鐘、每5秒鐘及每15秒的負載平均值,相似於uptime命令輸出的相關信息;第四列是由斜線隔開的兩個數值,前者表示當前正由內核調度的實體(進程和線程)的數目,後者表示系統當前存活的內核調度實體的數目;第五列表示此文件被查看前最近一個由內核建立的進程的PID.

[dirlt@umeng-ubuntu-pc] > cat /proc/loadavg
2.46 2.14 2.16 15/736 11012
1.1.1.13 /proc/locks

保存當前由內核鎖定的文件的相關信息,包含內核內部的調試數據;每一個鎖定佔據一行,且具備一個唯一的編號;以下輸出信息中每行的第二列表示當前鎖定使用的鎖定類別,POSIX表示目前較新類型的文件鎖,由lockf系統調用產生,FLOCK是傳統的UNIX文件鎖,由flock系統調用產生;第三列也一般由兩種類型,ADVISORY表示不容許其餘用戶鎖定此文件,但容許讀取,MANDATORY表示此文件鎖按期間不容許其餘用戶任何形式的訪問;

[dirlt@umeng-ubuntu-pc] > cat /proc/locks
1: POSIX  ADVISORY  WRITE 18536 08:01:1573063 0 9223372036854775806
2: POSIX  ADVISORY  WRITE 3187 08:03:1970738 0 0
3: POSIX  ADVISORY  WRITE 3187 08:03:1971423 0 0
4: POSIX  ADVISORY  WRITE 3187 08:03:1971422 0 0
5: POSIX  ADVISORY  WRITE 3187 08:03:1971421 0 0
6: POSIX  ADVISORY  READ  647 08:01:3702340 1073741826 1073742335
7: FLOCK  ADVISORY  WRITE 28279 08:01:394731 0 EOF
8: POSIX  ADVISORY  WRITE 25505 08:03:1442453 1073741824 1073742335
9: POSIX  ADVISORY  READ  25505 08:03:1442317 128 128
10: POSIX  ADVISORY  READ  25505 08:03:1443015 1073741826 1073742335
11: POSIX  ADVISORY  READ  25505 08:03:1441997 128 128
12: POSIX  ADVISORY  READ  25505 08:03:1443023 1073741826 1073742335
13: POSIX  ADVISORY  READ  25505 08:03:1443008 1073741826 1073742335
14: POSIX  ADVISORY  WRITE 25505 08:03:1443000 0 EOF
15: POSIX  ADVISORY  WRITE 18249 08:03:1971018 0 9223372036854775806
16: POSIX  ADVISORY  WRITE 17989 08:03:1970815 0 9223372036854775806
17: POSIX  ADVISORY  WRITE 4378 08:03:2492158 1073741824 1073742335
18: POSIX  ADVISORY  READ  4378 08:03:2491544 1073741826 1073742335
19: POSIX  ADVISORY  READ  4378 08:03:2492160 128 128
20: POSIX  ADVISORY  READ  4378 08:03:2491555 1073741826 1073742335
21: POSIX  ADVISORY  READ  4378 08:03:2491887 128 128
22: POSIX  ADVISORY  READ  4378 08:03:2491558 1073741826 1073742335
23: POSIX  ADVISORY  WRITE 4378 08:03:2491537 0 EOF
24: POSIX  ADVISORY  READ  1945 08:03:1179790 124 124
25: POSIX  ADVISORY  WRITE 1953 08:03:1179792 0 0
26: POSIX  ADVISORY  READ  1951 08:03:1179790 128 128
27: POSIX  ADVISORY  READ  1951 08:03:1179788 1073741826 1073742335
28: POSIX  ADVISORY  READ  1945 08:03:1179790 128 128
29: POSIX  ADVISORY  READ  1945 08:03:1179788 1073741826 1073742335
30: POSIX  ADVISORY  WRITE 997 00:0f:1714 0 EOF
31: FLOCK  ADVISORY  WRITE 958 00:0f:8496 0 EOF
1.1.1.14 /proc/slabinfo

在內核中頻繁使用的對象(如inode、dentry等)都有本身的cache,即slab pool,而/proc/slabinfo文件列出了這些對象相關slap的信息;詳情能夠參見內核文檔中slapinfo的手冊頁;

[dirlt@umeng-ubuntu-pc] > sudo cat /proc/slabinfo
[sudo] password for dirlt:
slabinfo - version: 2.1
# name            <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab> : tunables <limit> <batchcount> <sharedfactor> : sla
bdata <active_slabs> <num_slabs> <sharedavail>
ip6_dst_cache        100    100    320   25    2 : tunables    0    0    0 : slabdata      4      4      0
UDPLITEv6              0      0   1024   16    4 : tunables    0    0    0 : slabdata      0      0      0
UDPv6                 64     64   1024   16    4 : tunables    0    0    0 : slabdata      4      4      0
tw_sock_TCPv6        425    450    320   25    2 : tunables    0    0    0 : slabdata     18     18      0
TCPv6                221    221   1920   17    8 : tunables    0    0    0 : slabdata     13     13      0
flow_cache             0      0    104   39    1 : tunables    0    0    0 : slabdata      0      0      0
kcopyd_job             0      0   3240   10    8 : tunables    0    0    0 : slabdata      0      0      0
dm_uevent              0      0   2608   12    8 : tunables    0    0    0 : slabdata      0      0      0
dm_rq_target_io        0      0    408   20    2 : tunables    0    0    0 : slabdata      0      0      0
cfq_queue            510    510    232   17    1 : tunables    0    0    0 : slabdata     30     30      0
bsg_cmd                0      0    312   26    2 : tunables    0    0    0 : slabdata      0      0      0
mqueue_inode_cache     18     18    896   18    4 : tunables    0    0    0 : slabdata      1      1      0
fuse_request         104    104    608   26    4 : tunables    0    0    0 : slabdata      4      4      0
fuse_inode            46     46    704   23    4 : tunables    0    0    0 : slabdata      2      2      0
ecryptfs_key_record_cache      0      0    576   28    4 : tunables    0    0    0 : slabdata      0      0      0
ecryptfs_inode_cache      0      0    960   17    4 : tunables    0    0    0 : slabdata      0      0      0
hugetlbfs_inode_cache     28     28    576   28    4 : tunables    0    0    0 : slabdata      1      1      0
journal_handle       680    680     24  170    1 : tunables    0    0    0 : slabdata      4      4      0
journal_head         720    720    112   36    1 : tunables    0    0    0 : slabdata     20     20      0
revoke_record       4992   4992     32  128    1 : tunables    0    0    0 : slabdata     39     39      0
ext4_inode_cache   53478  53478    880   18    4 : tunables    0    0    0 : slabdata   2971   2971      0
ext4_free_data      3650   4015     56   73    1 : tunables    0    0    0 : slabdata     55     55      0
ext4_allocation_context   3810   3810    136   30    1 : tunables    0    0    0 : slabdata    127    127      0
ext4_io_end          261    406   1128   29    8 : tunables    0    0    0 : slabdata     14     14      0
ext4_io_page        1284   2816     16  256    1 : tunables    0    0    0 : slabdata     11     11      0
ext3_inode_cache       0      0    776   21    4 : tunables    0    0    0 : slabdata      0      0      0
ext3_xattr             0      0     88   46    1 : tunables    0    0    0 : slabdata      0      0      0
dquot                  0      0    256   16    1 : tunables    0    0    0 : slabdata      0      0      0
dnotify_mark        4230   4230    136   30    1 : tunables    0    0    0 : slabdata    141    141      0
dio                  100    100    640   25    4 : tunables    0    0    0 : slabdata      4      4      0
pid_namespace          0      0   2128   15    8 : tunables    0    0    0 : slabdata      0      0      0
user_namespace         0      0   1072   30    8 : tunables    0    0    0 : slabdata      0      0      0
UDP-Lite               0      0    832   19    4 : tunables    0    0    0 : slabdata      0      0      0
ip_fib_trie          292    292     56   73    1 : tunables    0    0    0 : slabdata      4      4      0
arp_cache             72     72    448   18    2 : tunables    0    0    0 : slabdata      4      4      0
UDP                   76     76    832   19    4 : tunables    0    0    0 : slabdata      4      4      0
tw_sock_TCP          240    240    256   16    1 : tunables    0    0    0 : slabdata     15     15      0
TCP                  140    252   1728   18    8 : tunables    0    0    0 : slabdata     14     14      0
blkdev_queue          51     51   1880   17    8 : tunables    0    0    0 : slabdata      3      3      0
blkdev_requests      374    374    368   22    2 : tunables    0    0    0 : slabdata     17     17      0
fsnotify_event      1632   1632    120   34    1 : tunables    0    0    0 : slabdata     48     48      0
bip-256                7      7   4224    7    8 : tunables    0    0    0 : slabdata      1      1      0
bip-128                0      0   2176   15    8 : tunables    0    0    0 : slabdata      0      0      0
bip-64                 0      0   1152   28    8 : tunables    0    0    0 : slabdata      0      0      0
bip-16                21     21    384   21    2 : tunables    0    0    0 : slabdata      1      1      0
sock_inode_cache    1149   1300    640   25    4 : tunables    0    0    0 : slabdata     52     52      0
net_namespace          0      0   2560   12    8 : tunables    0    0    0 : slabdata      0      0      0
shmem_inode_cache   2929   3375    640   25    4 : tunables    0    0    0 : slabdata    135    135      0
Acpi-ParseExt       3304   3304     72   56    1 : tunables    0    0    0 : slabdata     59     59      0
Acpi-State           204    204     80   51    1 : tunables    0    0    0 : slabdata      4      4      0
Acpi-Namespace      1428   1428     40  102    1 : tunables    0    0    0 : slabdata     14     14      0
task_delay_info     1764   1764    112   36    1 : tunables    0    0    0 : slabdata     49     49      0
taskstats             96     96    328   24    2 : tunables    0    0    0 : slabdata      4      4      0
proc_inode_cache    3001   3302    624   26    4 : tunables    0    0    0 : slabdata    127    127      0
sigqueue             150    150    160   25    1 : tunables    0    0    0 : slabdata      6      6      0
bdev_cache            76     76    832   19    4 : tunables    0    0    0 : slabdata      4      4      0
sysfs_dir_cache    18480  18480    144   28    1 : tunables    0    0    0 : slabdata    660    660      0
inode_cache         7515   8120    560   29    4 : tunables    0    0    0 : slabdata    280    280      0
dentry             63000  63000    192   21    1 : tunables    0    0    0 : slabdata   3000   3000      0
iint_cache             0      0    112   36    1 : tunables    0    0    0 : slabdata      0      0      0
buffer_head        54221  60684    104   39    1 : tunables    0    0    0 : slabdata   1556   1556      0
vm_area_struct     44863  48047    176   23    1 : tunables    0    0    0 : slabdata   2089   2089      0
mm_struct            252    252    896   18    4 : tunables    0    0    0 : slabdata     14     14      0
files_cache          465    506    704   23    4 : tunables    0    0    0 : slabdata     22     22      0
signal_cache         456    600   1088   30    8 : tunables    0    0    0 : slabdata     20     20      0
sighand_cache        285    315   2112   15    8 : tunables    0    0    0 : slabdata     21     21      0
task_xstate         1631   1824    832   19    4 : tunables    0    0    0 : slabdata     96     96      0
task_struct          758    820   5872    5    8 : tunables    0    0    0 : slabdata    164    164      0
anon_vma           12228  13328     72   56    1 : tunables    0    0    0 : slabdata    238    238      0
shared_policy_node  39239  42670     48   85    1 : tunables    0    0    0 : slabdata    502    502      0
numa_policy          680    680     24  170    1 : tunables    0    0    0 : slabdata      4      4      0
radix_tree_node    11597  15792    568   28    4 : tunables    0    0    0 : slabdata    564    564      0
idr_layer_cache     1320   1320    544   30    4 : tunables    0    0    0 : slabdata     44     44      0
dma-kmalloc-8192       0      0   8192    4    8 : tunables    0    0    0 : slabdata      0      0      0
dma-kmalloc-4096       0      0   4096    8    8 : tunables    0    0    0 : slabdata      0      0      0
dma-kmalloc-2048       0      0   2048   16    8 : tunables    0    0    0 : slabdata      0      0      0
dma-kmalloc-1024       0      0   1024   16    4 : tunables    0    0    0 : slabdata      0      0      0
dma-kmalloc-512        0      0    512   16    2 : tunables    0    0    0 : slabdata      0      0      0
dma-kmalloc-256        0      0    256   16    1 : tunables    0    0    0 : slabdata      0      0      0
dma-kmalloc-128        0      0    128   32    1 : tunables    0    0    0 : slabdata      0      0      0
dma-kmalloc-64         0      0     64   64    1 : tunables    0    0    0 : slabdata      0      0      0
dma-kmalloc-32         0      0     32  128    1 : tunables    0    0    0 : slabdata      0      0      0
dma-kmalloc-16         0      0     16  256    1 : tunables    0    0    0 : slabdata      0      0      0
dma-kmalloc-8          0      0      8  512    1 : tunables    0    0    0 : slabdata      0      0      0
dma-kmalloc-192        0      0    192   21    1 : tunables    0    0    0 : slabdata      0      0      0
dma-kmalloc-96         0      0     96   42    1 : tunables    0    0    0 : slabdata      0      0      0
kmalloc-8192          87     92   8192    4    8 : tunables    0    0    0 : slabdata     23     23      0
kmalloc-4096         200    240   4096    8    8 : tunables    0    0    0 : slabdata     30     30      0
kmalloc-2048         422    544   2048   16    8 : tunables    0    0    0 : slabdata     34     34      0
kmalloc-1024        1154   1280   1024   16    4 : tunables    0    0    0 : slabdata     80     80      0
kmalloc-512         2848   3328    512   16    2 : tunables    0    0    0 : slabdata    208    208      0
kmalloc-256        14079  14704    256   16    1 : tunables    0    0    0 : slabdata    919    919      0
kmalloc-128         3467   6816    128   32    1 : tunables    0    0    0 : slabdata    213    213      0
kmalloc-64         10415  12928     64   64    1 : tunables    0    0    0 : slabdata    202    202      0
kmalloc-32         10224  11648     32  128    1 : tunables    0    0    0 : slabdata     91     91      0
kmalloc-16          4608   4608     16  256    1 : tunables    0    0    0 : slabdata     18     18      0
kmalloc-8          31744  31744      8  512    1 : tunables    0    0    0 : slabdata     62     62      0
kmalloc-192         3055   4368    192   21    1 : tunables    0    0    0 : slabdata    208    208      0
kmalloc-96          4031   4116     96   42    1 : tunables    0    0    0 : slabdata     98     98      0
kmem_cache            32     32    256   16    1 : tunables    0    0    0 : slabdata      2      2      0
kmem_cache_node      128    128     64   64    1 : tunables    0    0    0 : slabdata      2      2      0
1.1.1.15 /proc/vmstat

當前系統虛擬內存的多種統計數據,信息量可能會比較大,這因系統而有所不一樣,可讀性較好;

[dirlt@umeng-ubuntu-pc] > cat /proc/vmstat
nr_free_pages 39606
nr_inactive_anon 207905
nr_active_anon 575539
nr_inactive_file 51308
nr_active_file 51193
nr_unevictable 8
nr_mlock 8
nr_anon_pages 662833
nr_mapped 30562
nr_file_pages 242500
nr_dirty 288
nr_writeback 0
nr_slab_reclaimable 20951
nr_slab_unreclaimable 10401
nr_page_table_pages 12576
nr_kernel_stack 739
nr_unstable 0
nr_bounce 0
nr_vmscan_write 1043356
nr_vmscan_immediate_reclaim 310904
nr_writeback_temp 0
nr_isolated_anon 0
nr_isolated_file 0
nr_shmem 106998
nr_dirtied 14956613
nr_written 13377394
numa_hit 6181305909
numa_miss 0
numa_foreign 0
numa_interleave 5518
numa_local 6181305909
numa_other 0
nr_anon_transparent_hugepages 0
nr_dirty_threshold 92555
nr_dirty_background_threshold 46277
pgpgin 17601663
pgpgout 64310804
pswpin 491621
pswpout 789986
pgalloc_dma 0
pgalloc_dma32 5030620457
pgalloc_normal 1192146375
pgalloc_movable 0
pgfree 6222807408
pgactivate 4825471
pgdeactivate 4561700
pgfault 5884815503
pgmajfault 159176
pgrefill_dma 0
pgrefill_dma32 2727143
pgrefill_normal 3119709
pgrefill_movable 0
pgsteal_dma 0
pgsteal_dma32 4612169
pgsteal_normal 2661815
pgsteal_movable 0
pgscan_kswapd_dma 0
pgscan_kswapd_dma32 6468262
pgscan_kswapd_normal 4573712
pgscan_kswapd_movable 0
pgscan_direct_dma 0
pgscan_direct_dma32 275766
pgscan_direct_normal 246885
pgscan_direct_movable 0
zone_reclaim_failed 0
pginodesteal 0
slabs_scanned 1904768
kswapd_steal 7105516
kswapd_inodesteal 41781
kswapd_low_wmark_hit_quickly 10
kswapd_high_wmark_hit_quickly 980
kswapd_skip_congestion_wait 4130
pageoutrun 147240
allocstall 3159
pgrotated 884823
compact_blocks_moved 0
compact_pages_moved 0
compact_pagemigrate_failed 0
compact_stall 5
compact_fail 3
compact_success 2
htlb_buddy_alloc_success 0
htlb_buddy_alloc_fail 0
unevictable_pgs_culled 0
unevictable_pgs_scanned 0
unevictable_pgs_rescued 74
unevictable_pgs_mlocked 82
unevictable_pgs_munlocked 74
unevictable_pgs_cleared 0
unevictable_pgs_stranded 0
unevictable_pgs_mlockfreed 0
thp_fault_alloc 0
thp_fault_fallback 0
thp_collapse_alloc 0
thp_collapse_alloc_failed 0
thp_split 0
1.1.1.16 /proc/zoneinfo

內存區域(zone)的詳細信息列表

[dirlt@umeng-ubuntu-pc] > cat /proc/zoneinfo
Node 0, zone      DMA
  pages free     3977
        min      65
        low      81
        high     97
        scanned  0
        spanned  4080
        present  3913
    nr_free_pages 3977
    nr_inactive_anon 0
    nr_active_anon 0
    nr_inactive_file 0
    nr_active_file 0
    nr_unevictable 0
    nr_mlock     0
    nr_anon_pages 0
    nr_mapped    0
    nr_file_pages 0
    nr_dirty     0
    nr_writeback 0
    nr_slab_reclaimable 0
    nr_slab_unreclaimable 0
    nr_page_table_pages 0
    nr_kernel_stack 0
    nr_unstable  0
    nr_bounce    0
    nr_vmscan_write 0
    nr_vmscan_immediate_reclaim 0
    nr_writeback_temp 0
    nr_isolated_anon 0
    nr_isolated_file 0
    nr_shmem     0
    nr_dirtied   0
    nr_written   0
    numa_hit     0
    numa_miss    0
    numa_foreign 0
    numa_interleave 0
    numa_local   0
    numa_other   0
    nr_anon_transparent_hugepages 0
        protection: (0, 2906, 3912, 3912)
  pagesets
    cpu: 0
              count: 0
              high:  0
              batch: 1
  vm stats threshold: 6
    cpu: 1
              count: 0
              high:  0
              batch: 1
  vm stats threshold: 6
    cpu: 2
              count: 0
              high:  0
              batch: 1
  vm stats threshold: 6
    cpu: 3
              count: 0
              high:  0
              batch: 1
  vm stats threshold: 6
  all_unreclaimable: 1
  start_pfn:         16
  inactive_ratio:    1
Node 0, zone    DMA32
  pages free     36397
        min      12503
        low      15628
        high     18754
        scanned  0
        spanned  1044480
        present  744178
    nr_free_pages 36397
    nr_inactive_anon 122972
    nr_active_anon 492944
    nr_inactive_file 32614
    nr_active_file 31141
    nr_unevictable 0
    nr_mlock     0
    nr_anon_pages 528611
    nr_mapped    16731
    nr_file_pages 167055
    nr_dirty     202
    nr_writeback 0
    nr_slab_reclaimable 14391
    nr_slab_unreclaimable 3764
    nr_page_table_pages 5393
    nr_kernel_stack 217
    nr_unstable  0
    nr_bounce    0
    nr_vmscan_write 573794
    nr_vmscan_immediate_reclaim 248788
    nr_writeback_temp 0
    nr_isolated_anon 0
    nr_isolated_file 0
    nr_shmem     74754
    nr_dirtied   8249855
    nr_written   6854644
    numa_hit     5001386683
    numa_miss    0
    numa_foreign 0
    numa_interleave 0
    numa_local   5001386683
    numa_other   0
    nr_anon_transparent_hugepages 0
        protection: (0, 0, 1006, 1006)
  pagesets
    cpu: 0
              count: 62
              high:  186
              batch: 31
  vm stats threshold: 36
    cpu: 1
              count: 171
              high:  186
              batch: 31
  vm stats threshold: 36
    cpu: 2
              count: 62
              high:  186
              batch: 31
  vm stats threshold: 36
    cpu: 3
              count: 125
              high:  186
              batch: 31
  vm stats threshold: 36
  all_unreclaimable: 0
  start_pfn:         4096
  inactive_ratio:    4
Node 0, zone   Normal
  pages free     5426
        min      4327
        low      5408
        high     6490
        scanned  0
        spanned  261632
        present  257544
    nr_free_pages 5426
    nr_inactive_anon 81011
    nr_active_anon 80301
    nr_inactive_file 18852
    nr_active_file 20072
    nr_unevictable 8
    nr_mlock     8
    nr_anon_pages 131848
    nr_mapped    13831
    nr_file_pages 71702
    nr_dirty     172
    nr_writeback 0
    nr_slab_reclaimable 6568
    nr_slab_unreclaimable 6641
    nr_page_table_pages 7176
    nr_kernel_stack 523
    nr_unstable  0
    nr_bounce    0
    nr_vmscan_write 469562
    nr_vmscan_immediate_reclaim 62116
    nr_writeback_temp 0
    nr_isolated_anon 0
    nr_isolated_file 0
    nr_shmem     28316
    nr_dirtied   6708910
    nr_written   6523845
    numa_hit     1183081020
    numa_miss    0
    numa_foreign 0
    numa_interleave 5518
    numa_local   1183081020
    numa_other   0
    nr_anon_transparent_hugepages 0
        protection: (0, 0, 0, 0)
  pagesets
    cpu: 0
              count: 41
              high:  186
              batch: 31
  vm stats threshold: 24
    cpu: 1
              count: 170
              high:  186
              batch: 31
  vm stats threshold: 24
    cpu: 2
              count: 132
              high:  186
              batch: 31
  vm stats threshold: 24
    cpu: 3
              count: 185
              high:  186
              batch: 31
  vm stats threshold: 24
  all_unreclaimable: 0
  start_pfn:         1048576
  inactive_ratio:    1

1.1.2 proc/<pid>

其中pid爲對應的進程號,目錄下面就是這個進程對應的信息。

  • attr ???
  • auxv ???
  • cgroup ???
  • clear_refs ???
  • cmdline 進程啓動運行參數
  • coredump_filter ???
  • cpuset ???
  • cwd 當前目錄
  • environ 環境變量
  • exe 映像文件
  • fd 打開fd
  • fdinfo 打開fd對應信息(很是少).
  • io 讀寫字節數目以及讀寫系統調用次數。
  • limits 運行環境limits
  • loginuid 運行程序用戶登陸的uid(不是euid).
  • maps 進程運行內存鏡像圖示(文本)
  • mountinfo 文件系統掛載信息
  • mounts 文件系統掛載信息
  • mountstatus 文件系統掛載信息
  • net 網絡信息
  • numa_maps NUMA的內存映射 ???
  • oom_adj 出現OOM時進程被kill的權值。範圍從[-17,15],越小意味着越不容易被kill。
  • oom_score 出現OOM時進程被kill的分值,就是每一個進程計算出來的badness。badness越高越容易被kill。
  • pagemap 內存映像(二進制),相似於core
  • personality ??
  • root 根文件系統路徑
  • sched 進程調度信息
  • sessionid ???
  • smaps 進程運行內存鏡像圖示(文本)各個段大小
  • stat 當前進程的狀態信息
  • statm 當前進程佔用內存的狀態信息
  • status 進程運行系統狀態
  • syscall ???
  • task 若是是多線程的話每一個線程對應信息
  • wchan ???
1.1.2.1 fd

todo(zhangyan04):

1.1.2.2 io

TODO:

1.1.2.3 limits

TODO:

1.1.2.4 maps

當前進程關聯到的每一個可執行文件和庫文件在內存中的映射區域及其訪問權限所組成的列表

00400000-004e0000 r-xp 00000000 08:01 4456452                            /bin/bash
006df000-006e0000 r--p 000df000 08:01 4456452                            /bin/bash
006e0000-006e9000 rw-p 000e0000 08:01 4456452                            /bin/bash
006e9000-006ef000 rw-p 00000000 00:00 0
02411000-024ac000 rw-p 00000000 00:00 0                                  [heap]
7f81aa605000-7f81aa611000 r-xp 00000000 08:01 5770912                    /lib/x86_64-linux-gnu/libnss_files-2.15.so
7f81aa611000-7f81aa810000 ---p 0000c000 08:01 5770912                    /lib/x86_64-linux-gnu/libnss_files-2.15.so
7f81aa810000-7f81aa811000 r--p 0000b000 08:01 5770912                    /lib/x86_64-linux-gnu/libnss_files-2.15.so
7f81aa811000-7f81aa812000 rw-p 0000c000 08:01 5770912                    /lib/x86_64-linux-gnu/libnss_files-2.15.so
7f81aa812000-7f81aa81c000 r-xp 00000000 08:01 5770916                    /lib/x86_64-linux-gnu/libnss_nis-2.15.so
7f81aa81c000-7f81aaa1c000 ---p 0000a000 08:01 5770916                    /lib/x86_64-linux-gnu/libnss_nis-2.15.so
7f81aaa1c000-7f81aaa1d000 r--p 0000a000 08:01 5770916                    /lib/x86_64-linux-gnu/libnss_nis-2.15.so
7f81aaa1d000-7f81aaa1e000 rw-p 0000b000 08:01 5770916                    /lib/x86_64-linux-gnu/libnss_nis-2.15.so
7f81aaa1e000-7f81aaa35000 r-xp 00000000 08:01 5770906                    /lib/x86_64-linux-gnu/libnsl-2.15.so
7f81aaa35000-7f81aac34000 ---p 00017000 08:01 5770906                    /lib/x86_64-linux-gnu/libnsl-2.15.so
7f81aac34000-7f81aac35000 r--p 00016000 08:01 5770906                    /lib/x86_64-linux-gnu/libnsl-2.15.so
7f81aac35000-7f81aac36000 rw-p 00017000 08:01 5770906                    /lib/x86_64-linux-gnu/libnsl-2.15.so
7f81aac36000-7f81aac38000 rw-p 00000000 00:00 0
7f81aac38000-7f81aac40000 r-xp 00000000 08:01 5770908                    /lib/x86_64-linux-gnu/libnss_compat-2.15.so
7f81aac40000-7f81aae3f000 ---p 00008000 08:01 5770908                    /lib/x86_64-linux-gnu/libnss_compat-2.15.so
7f81aae3f000-7f81aae40000 r--p 00007000 08:01 5770908                    /lib/x86_64-linux-gnu/libnss_compat-2.15.so
7f81aae40000-7f81aae41000 rw-p 00008000 08:01 5770908                    /lib/x86_64-linux-gnu/libnss_compat-2.15.so
7f81aae41000-7f81ab524000 r--p 00000000 08:01 2628153                    /usr/lib/locale/locale-archive
7f81ab524000-7f81ab6d7000 r-xp 00000000 08:01 5770861                    /lib/x86_64-linux-gnu/libc-2.15.so
7f81ab6d7000-7f81ab8d6000 ---p 001b3000 08:01 5770861                    /lib/x86_64-linux-gnu/libc-2.15.so
7f81ab8d6000-7f81ab8da000 r--p 001b2000 08:01 5770861                    /lib/x86_64-linux-gnu/libc-2.15.so
7f81ab8da000-7f81ab8dc000 rw-p 001b6000 08:01 5770861                    /lib/x86_64-linux-gnu/libc-2.15.so
7f81ab8dc000-7f81ab8e1000 rw-p 00000000 00:00 0
7f81ab8e1000-7f81ab8e3000 r-xp 00000000 08:01 5770874                    /lib/x86_64-linux-gnu/libdl-2.15.so
7f81ab8e3000-7f81abae3000 ---p 00002000 08:01 5770874                    /lib/x86_64-linux-gnu/libdl-2.15.so
7f81abae3000-7f81abae4000 r--p 00002000 08:01 5770874                    /lib/x86_64-linux-gnu/libdl-2.15.so
7f81abae4000-7f81abae5000 rw-p 00003000 08:01 5770874                    /lib/x86_64-linux-gnu/libdl-2.15.so
7f81abae5000-7f81abb07000 r-xp 00000000 08:01 5770958                    /lib/x86_64-linux-gnu/libtinfo.so.5.9
7f81abb07000-7f81abd07000 ---p 00022000 08:01 5770958                    /lib/x86_64-linux-gnu/libtinfo.so.5.9
7f81abd07000-7f81abd0b000 r--p 00022000 08:01 5770958                    /lib/x86_64-linux-gnu/libtinfo.so.5.9
7f81abd0b000-7f81abd0c000 rw-p 00026000 08:01 5770958                    /lib/x86_64-linux-gnu/libtinfo.so.5.9
7f81abd0c000-7f81abd2e000 r-xp 00000000 08:01 5770841                    /lib/x86_64-linux-gnu/ld-2.15.so
7f81abf12000-7f81abf15000 rw-p 00000000 00:00 0
7f81abf25000-7f81abf2c000 r--s 00000000 08:01 2884820                    /usr/lib/x86_64-linux-gnu/gconv/gconv-modules.cache
7f81abf2c000-7f81abf2e000 rw-p 00000000 00:00 0
7f81abf2e000-7f81abf2f000 r--p 00022000 08:01 5770841                    /lib/x86_64-linux-gnu/ld-2.15.so
7f81abf2f000-7f81abf31000 rw-p 00023000 08:01 5770841                    /lib/x86_64-linux-gnu/ld-2.15.so
7fffccd69000-7fffccd8a000 rw-p 00000000 00:00 0                          [stack]
7fffccdff000-7fffcce00000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
1.1.2.5 mount

TODO:

1.1.2.6 net

todo(zhangyan04):

1.1.2.7 sched

todo(zhangyan04):

1.1.2.8 status

TODO:

1.1.2.9 statm

Provides information about memory usage, measured in pages. The columns are:

  • size total program size (same as VmSize in /proc/[pid]/status)
  • resident resident set size (same as VmRSS in /proc/[pid]/status)
  • share shared pages (from shared mappings)
  • text text (code)
  • lib library (unused in Linux 2.6)
  • data data + stack
  • dt dirty pages (unused in Linux 2.6)

1.1.3 /proc/sys

在/proc/sys下面有一些能夠動態修改的內核參數,有兩種方式能夠修改這些參數。

首先可使用sysctl工具來進行修改。好比若是想修改sys/vm/swappiness==0的話,那麼能夠

dirlt@dirlt-virtual-machine:~/github/sperm/essay$ sudo sysctl vm.swappiness=0
[sudo] password for dirlt:
vm.swappiness = 0

上面修改方式是臨時的,若是想進行永久修改的話能夠修改/etc/sysctl.conf文件

vm.swappiness = 0

而後重啓那麼這個設置就會永久生效。

1.1.4 /proc/sys/vm

 
1.1.4.1 /proc/sys/vm/overcommit_memory

所謂的overcommit是過量使用的意思。

  • OVERCOMMIT_GUESS=0 用戶申請內存的時候,系統會判斷剩餘的內存有多少。若是不夠的話那麼就會失敗。這種方式是比較保守的,由於有時候好比用戶申請1G內存可是可能只是會使用其中1K.
    • Heuristic overcommit handling. Obvious overcommits of address space are refused. Used for a typical system. It ensures a seriously wild allocation fails while allowing overcommit to reduce swap usage. root is allowed to allocate slighly more memory in this mode. This is the default.
  • OVERCOMMIT_ALWAYS=1 用戶申請內存的時候,系統不進行任何檢查認爲內存足夠使用,直到使用內存超過可用內存。
    • Always overcommit. Appropriate for some scientific applications.
  • OVERCOMMIT_NEVER=2 用戶一次申請內存的大小不容許超過<可用內存>的大小。關於這個<可用內存>的大小計算能夠看下面overcommit_ration這個參數,能夠上面兩種所說的可用內存不太同樣。
    • Don't overcommit. The total address space commit for the system is not permitted to exceed swap + a configurable percentage (default is 50) of physical RAM. Depending on the percentage you use, in most situations this means a process will not be killed while accessing pages but will receive errors on memory allocation as appropriate.

下午將dp3的overcommit_memory參數修改爲爲2以後,首先出現的問題就是不可以再執行任何shell命令了,錯誤是fork can't allocate enough memory,就是fork沒有那麼多的內存可用。而後推出會話以後沒有辦法再登錄dp3了。這個主要是由於jvm應該基本上佔用滿了物理內存,而overcommit_ration=0.5,而且沒有swap空間,因此沒有辦法allocate更多的memory了。

從/var/log/syslog裏面能夠看到,修改了這個參數以後,不少程序受到影響(ganglia掛掉了,cron不可以fork出進程了,init也不可以分配出更多的tty,致使咱們沒有辦法登錄上去)在ganglia裏面看到內存以及CPU使用都是一條直線,不是由於系統穩定而是由於gmond掛掉了。

Nov  8 18:07:04 dp3 /usr/sbin/gmond[1664]: [PYTHON] Can't call the metric handler function for [diskstat_sdd_reads] in the python module [diskstat].#012
Nov  8 18:07:04 dp3 /usr/sbin/gmond[1664]: [PYTHON] Can't call the metric handler function for [diskstat_sdd_writes] in the python module [diskstat].#012
Nov  8 18:07:28 dp3 console-kit-daemon[1760]: WARNING: Error writing state file: No space left on device
Nov  8 18:07:28 dp3 console-kit-daemon[1760]: WARNING: Cannot write to file /var/run/ConsoleKit/database~
Nov  8 18:07:28 dp3 console-kit-daemon[1760]: WARNING: Unable to spawn /usr/lib/ConsoleKit/run-session.d/pam-foreground-compat.ck: Failed to fork (Cannot allocate memory)
Nov  8 18:07:28 dp3 console-kit-daemon[1760]: WARNING: Error writing state file: No space left on device
Nov  8 18:07:28 dp3 console-kit-daemon[1760]: WARNING: Cannot write to file /var/run/ConsoleKit/database~
Nov  8 18:07:28 dp3 console-kit-daemon[1760]: WARNING: Cannot unlink /var/run/ConsoleKit/database: No such file or directory
Nov  8 18:08:12 dp3 /usr/sbin/gmond[1664]: slurpfile() open() error on file /proc/stat: Too many open files
Nov  8 18:08:12 dp3 /usr/sbin/gmond[1664]: update_file() got an error from slurpfile() reading /proc/stat
Nov  8 18:08:12 dp3 /usr/sbin/gmond[1664]: slurpfile() open() error on file /proc/stat: Too many open files
Nov  8 18:08:12 dp3 /usr/sbin/gmond[1664]: update_file() got an error from slurpfile() reading /proc/stat
Nov  8 18:08:12 dp3 /usr/sbin/gmond[1664]: slurpfile() open() error on file /proc/stat: Too many open files
Nov  8 18:08:12 dp3 /usr/sbin/gmond[1664]: update_file() got an error from slurpfile() reading /proc/stat
Nov  8 18:08:12 dp3 /usr/sbin/gmond[1664]: slurpfile() open() error on file /proc/stat: Too many open files
Nov  8 18:08:12 dp3 /usr/sbin/gmond[1664]: update_file() got an error from slurpfile() reading /proc/stat
Nov  8 18:08:12 dp3 /usr/sbin/gmond[1664]: slurpfile() open() error on file /proc/stat: Too many open files
Nov  8 18:08:12 dp3 /usr/sbin/gmond[1664]: update_file() got an error from slurpfile() reading /proc/stat
Nov  8 18:08:12 dp3 kernel: [4319715.969327] gmond[1664]: segfault at ffffffffffffffff ip 00007f52e0066f34 sp 00007fff4e428620 error 4 in libganglia-3.1.2.so.0.0.0[7f52e0060000+13000]
Nov  8 18:10:01 dp3 cron[1637]: (CRON) error (can't fork)
Nov  8 18:13:53 dp3 init: tty1 main process (2341) terminated with status 1
Nov  8 18:13:53 dp3 init: tty1 main process ended, respawning
Nov  8 18:13:53 dp3 init: Temporary process spawn error: Cannot allocate memory

而在hadoop的datanode日誌裏面,有下面這些錯誤(只是給出部分exception):

2012-11-08 18:07:01,283 ERROR org.apache.hadoop.hdfs.server.datanode.DataNode: DatanodeRegistration(10.18.10.56:50010, storageID=DS-1599419066-10.18.10.47-50010-1329122718923, infoPort=50075, ipcPort=50020):DataXceiver
java.io.EOFException: while trying to read 65557 bytes
        at org.apache.hadoop.hdfs.server.datanode.BlockReceiver.readToBuf(BlockReceiver.java:290)
        at org.apache.hadoop.hdfs.server.datanode.BlockReceiver.readNextPacket(BlockReceiver.java:334)
        at org.apache.hadoop.hdfs.server.datanode.BlockReceiver.receivePacket(BlockReceiver.java:398)
        at org.apache.hadoop.hdfs.server.datanode.BlockReceiver.receiveBlock(BlockReceiver.java:577)

        at org.apache.hadoop.hdfs.server.datanode.DataXceiver.writeBlock(DataXceiver.java:480)
        at org.apache.hadoop.hdfs.server.datanode.DataXceiver.run(DataXceiver.java:171)
2012-11-08 18:07:02,163 ERROR org.apache.hadoop.hdfs.server.datanode.DataNode: DatanodeRegistration(10.18.10.56:50010, storageID=DS-1599419066-10.18.10.47-50010-1329122718923, infoPort=50075, ipcPort=50020):DataXceiverServer: Exiting due to:java.lang.OutOfMemoryError: unable to create new native thread
        at java.lang.Thread.start0(Native Method)
        at java.lang.Thread.start(Thread.java:640)
        at org.apache.hadoop.hdfs.server.datanode.DataXceiverServer.run(DataXceiverServer.java:131)
        at java.lang.Thread.run(Thread.java:662)


2012-11-08 18:07:04,964 ERROR org.apache.hadoop.hdfs.server.datanode.DataNode: DatanodeRegistration(10.18.10.56:50010, storageID=DS-1599419066-10.18.10.47-50010-1329122718923, infoPort=50075, ipcPort=50020):DataXceiver
java.io.InterruptedIOException: Interruped while waiting for IO on channel java.nio.channels.SocketChannel[closed]. 0 millis timeout left.
        at org.apache.hadoop.net.SocketIOWithTimeout$SelectorPool.select(SocketIOWithTimeout.java:349)
        at org.apache.hadoop.net.SocketIOWithTimeout.doIO(SocketIOWithTimeout.java:157)
        at org.apache.hadoop.net.SocketInputStream.read(SocketInputStream.java:155)
        at org.apache.hadoop.net.SocketInputStream.read(SocketInputStream.java:128)
        at java.io.BufferedInputStream.read1(BufferedInputStream.java:256)
        at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
        at java.io.DataInputStream.read(DataInputStream.java:132)
        at org.apache.hadoop.hdfs.server.datanode.BlockReceiver.readToBuf(BlockReceiver.java:287)
        at org.apache.hadoop.hdfs.server.datanode.BlockReceiver.readNextPacket(BlockReceiver.java:334)
        at org.apache.hadoop.hdfs.server.datanode.BlockReceiver.receivePacket(BlockReceiver.java:398)
        at org.apache.hadoop.hdfs.server.datanode.BlockReceiver.receiveBlock(BlockReceiver.java:577)

        at org.apache.hadoop.hdfs.server.datanode.DataXceiver.writeBlock(DataXceiver.java:480)
        at org.apache.hadoop.hdfs.server.datanode.DataXceiver.run(DataXceiver.java:171)

2012-11-08 18:07:04,965 INFO org.apache.hadoop.hdfs.server.datanode.DataNode: PacketResponder blk_-1079258682690587867_32990729 1 Exception java.io.EOFException
        at java.io.DataInputStream.readFully(DataInputStream.java:180)
        at java.io.DataInputStream.readLong(DataInputStream.java:399)
        at org.apache.hadoop.hdfs.protocol.DataTransferProtocol$PipelineAck.readFields(DataTransferProtocol.java:120)
        at org.apache.hadoop.hdfs.server.datanode.BlockReceiver$PacketResponder.run(BlockReceiver.java:937)
        at java.lang.Thread.run(Thread.java:662)

2012-11-08 18:07:05,057 INFO org.apache.hadoop.hdfs.server.datanode.DataNode: PacketResponder blk_1523791863488769175_32972264 1 Exception java.nio.channels.ClosedChannelException
        at sun.nio.ch.SocketChannelImpl.ensureWriteOpen(SocketChannelImpl.java:133)
        at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:324)
        at org.apache.hadoop.net.SocketOutputStream$Writer.performIO(SocketOutputStream.java:55)
        at org.apache.hadoop.net.SocketIOWithTimeout.doIO(SocketIOWithTimeout.java:142)
        at org.apache.hadoop.net.SocketOutputStream.write(SocketOutputStream.java:146)
        at org.apache.hadoop.net.SocketOutputStream.write(SocketOutputStream.java:107)
        at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
        at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
        at java.io.DataOutputStream.flush(DataOutputStream.java:106)
        at org.apache.hadoop.hdfs.server.datanode.BlockReceiver$PacketResponder.run(BlockReceiver.java:1047)
        at java.lang.Thread.run(Thread.java:662)

2012-11-08 18:07:04,972 ERROR org.apache.hadoop.hdfs.server.datanode.DataNode: DatanodeRegistration(10.18.10.56:50010, storageID=DS-1599419066-10.18.10.47-50010-1329122718923, infoPort=5
0075, ipcPort=50020):DataXceiver
java.io.IOException: Interrupted receiveBlock
        at org.apache.hadoop.hdfs.server.datanode.BlockReceiver.receiveBlock(BlockReceiver.java:622)
        at org.apache.hadoop.hdfs.server.datanode.DataXceiver.writeBlock(DataXceiver.java:480)
        at org.apache.hadoop.hdfs.server.datanode.DataXceiver.run(DataXceiver.java:171)

2012-11-08 18:08:02,003 INFO org.apache.hadoop.hdfs.server.datanode.DataNode: Waiting for threadgroup to exit, active threads is 1
2012-11-08 18:08:02,025 WARN org.apache.hadoop.util.Shell: Could not get disk usage information
java.io.IOException: Cannot run program "du": java.io.IOException: error=12, Cannot allocate memory
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
        at org.apache.hadoop.util.Shell.runCommand(Shell.java:200)
        at org.apache.hadoop.util.Shell.run(Shell.java:182)
        at org.apache.hadoop.fs.DU.access$200(DU.java:29)
        at org.apache.hadoop.fs.DU$DURefreshThread.run(DU.java:84)
        at java.lang.Thread.run(Thread.java:662)
Caused by: java.io.IOException: java.io.IOException: error=12, Cannot allocate memory
        at java.lang.UNIXProcess.<init>(UNIXProcess.java:148)
        at java.lang.ProcessImpl.start(ProcessImpl.java:65)
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:453)

接着以後就一直打印下面日誌hang住了

2012-11-08 18:08:52,015 INFO org.apache.hadoop.hdfs.server.datanode.DataNode: Waiting for threadgroup to exit, active threads is 1

hdfs web頁面上面顯示dead node,可是實際上這個datanode進程還存活。緣由估計也是由於不可以分配足夠的內存出現這些問題的吧。

最後能夠登錄上去的緣由,我猜測應該是datanode掛掉了,上面的regionserver暫時沒有分配內存因此有足夠的內存空間,init能夠開闢tty。

如今已經將這個值調整成爲原來的值,也就是0。索性的是,在這個期間,這個修改對於線上的任務執行沒有什麼影響。

1.1.4.2 /proc/sys/vm/overcommit_ratio

若是overcommit_memory值爲2的話,那麼這個參數決定了系統的<可用內存>的大小。計算方式是 (Physical-RAM-Size) * ratio / 100 + (Swap-Size).

dirlt@dirlt-virtual-machine:~/github/sperm/essay$ free -m
             total       used       free     shared    buffers     cached
Mem:           491        484          6          0         43        296
-/+ buffers/cache:        145        346
Swap:          509          0        509
dirlt@dirlt-virtual-machine:~/github/sperm/essay$ cat /proc/sys/vm/overcommit_ratio
50

因此對於我這個系統來講,可用的虛擬內存在(491*50/100)+509=754M. note(dirlt):這個僅僅是在overcommit_memory=2的時候估算的<可用內存>大小, 實際上對於其餘狀況來講可用內存大小仍是(Physical-RAM-Size) + (Swap-Size).

1.1.4.3 /proc/sys/vm/swappiness

這個參數決定系統使用swap的程度。可是這個參數並無禁止使用swap分區,而只是一個依賴於swap分區的程度。 若是這個值設置成爲0的話那麼,那麼系統會盡量地將減小page swap in/out操做,將更多的內存操做於物理內存上面。

1.1.4.4 /proc/sys/vm/dirty_*

這幾個參數主要是用來控制髒頁刷回磁盤策略。關於髒頁刷回磁盤的過程能夠參看"文件IO/write"一節。

note(dirlt)@2013-05-25: 我copy了一分內容過來

對於這些髒頁的寫回策略是:

  • 首先判斷髒頁比例是否超過dirty_ratio.若是沒有的話那麼直接退出
  • 而後開始將髒頁刷到磁盤直到比率小於dirty_ratio.(此時write會阻塞)
  • 判斷髒頁比例是否超過dirty_background_ratio或者是超過dirty_background_bytes.若是沒有那麼退出。
  • 若是超過的話那麼就會啓動pdflush daemon後臺進程刷新髒頁。(此時write不會阻塞)

注意到這裏可能啓動pdflush daemon在後臺刷新髒頁。另外系統每隔dirty_writeback_centisecs時間會啓動pdflush daemon將髒頁刷到磁盤上面。而pdflush daemon工做方式是這樣的,檢查髒頁是否存在超過dirty_expire_centisecs時間的,若是超過的話那麼就會在後臺刷新這些髒頁。

1.1.4.5 /proc/sys/vm/drop_caches

能夠用來釋放kernel保存的buffers和cached memory,buffers保存的是目錄以及文件的inode,cached memory保存的是操做文件時候使用的pagecache

  • 0 // 釋放cached memory
  • 1 // 釋放buffers
  • 2 // 釋放二者

爲了防止數據丟失,能夠在修改這個文件以前先調用sync強制寫盤

1.1.4.6 /proc/sys/vm/panic_on_oom

This enables or disables panic on out-of-memory feature.

If this is set to 0, the kernel will kill some rogue process, called oom_killer.  Usually, oom_killer can kill rogue processes and system will survive.

If this is set to 1, the kernel panics when out-of-memory happens. However, if a process limits using nodes by mempolicy/cpusets, and those nodes become memory exhaustion status, one process may be killed by oom-killer. No panic occurs in this case. Because other nodes' memory may be free. This means system total status may be not fatal yet.

If this is set to 2, the kernel panics compulsorily even on the above-mentioned.

The default value is 0. 1 and 2 are for failover of clustering. Please select either according to your policy of failover.

note(dirlt):對於1,2不是很理解,多是用於分佈式集羣Linux系統上面的策略

1.1.5 /proc/sys/net

 
1.1.5.1 /proc/sys/net/ipv4/ip_local_port_range

本地port分配範圍.

1.1.5.2 /proc/sys/net/ipv4/tcp_tw_reuse

重複使用處於TIME_WAIT的socket.

Allow  to  reuse  TIME_WAIT sockets for new connections when it is safe from protocol viewpoint.

1.1.5.3 /proc/sys/net/ipv4/tcp_tw_recycle

快速回收處理TIME_WAIT的socket.

Enable fast recycling of TIME_WAIT sockets.

1.1.5.4 /proc/sys/net/ipv4/tcp_max_syn_backlog

等待client作ack的鏈接數目上限

1.1.5.5 /proc/sys/net/core/somaxconn

每一個端口監聽隊列的最大長度。

1.1.5.6 /proc/sys/net/core/netdev_max_backlog

網絡設備接收數據包的速率比內核處理這些包的速率快時,容許送到隊列的數據包的最大數目。

1.1.6 /proc/sys/fs

 
1.1.6.1 /proc/sys/fs/file-max

全部進程容許打開文件的最大數量 note(dirlt):這個應該是和文件描述符有區別的

1.1.6.2 /proc/sys/fs/epoll/max_user_instances

單個用戶使用epoll的文件描述符上限。若是超過上限會返回EMFILE錯誤。 note(dirlt):不過在個人文件系統下面彷佛沒有這個選項

1.1.6.3 /proc/sys/fs/epoll/max_user_watches

單個用戶使用epoll進行watch的文件描述符上限。 note(dirlt):對於服務器應該特別有用,能夠限制內存使用量

This specifies a limit on the total number of file descriptors that a user can register across all epoll  instances
on  the system.  The limit is per real user ID.  Each registered file descriptor costs roughly 90 bytes on a 32-bit
kernel, and roughly 160 bytes on a 64-bit kernel.  Currently, the default value for max_user_watches is  1/25  (4%)
of the available low memory, divided by the registration cost in bytes.

1.1.7 /proc/sys/kernel

 
1.1.7.1 /proc/sys/kernel/hung_task_timeout_secs

Detecting hung tasks in Linux

Sometimes tasks under Linux are blocked forever (essentially hung). Recent Linux kernels have an infrastructure to detect hung tasks. When this infrastructure is active it will periodically get activated to find out hung tasks and present a stack dump of those hung tasks (and maybe locks held). Additionally we can choose to panic the system when we detect atleast one hung task in the system. I will try to explain how khungtaskd works.

The infrastructure is based on a single kernel thread named as 「khungtaskd」. So if you do a ps in your system and see that there is entry like [khungtaskd] you know it is there. I have one in my system: "136 root SW [khungtaskd]"

The loop of the khungtaskd daemon is a call to the scheduler for waking it up after ever 120 seconds (default value). The core algorithm is like this:

  1. Iterate over all the tasks in the system which are marked as TASK_UNINTERRUPTIBLE (additionally it does not consider UNINTERRUPTIBLE frozen tasks & UNINTERRUPTIBLE tasks that are newly created and never been scheduled out).
  2. If a task has not been switched out by the scheduler atleast once in the last 120 seconds it is considered as a hung task and its stack dump is displayed. If CONFIG_LOCKDEP is defined then it will also show all the locks the hung task is holding.

One can change the sampling interval of khungtaskd through the sysctl interface /proc/sys/kernel/hung_task_timeout_secs.

以前在hdfs一個datanode上面出現了磁盤損壞問題,而後在syslog裏面發現了下面日誌

May 14 00:02:50 dp46 kernel: INFO: task jbd2/sde1-8:3411 blocked for more than 120 seconds.
May 14 00:02:50 dp46 kernel: "echo 0 > /proc/sys/kernel/hung_task_timeout_secsmahung_task_timeout_secs" disables this message.
May 14 00:02:50 dp46 kernel: jbd2/sde1-8   D 0000000000000000     0  3411      2 0x00000000
May 14 00:02:50 dp46 kernel: ffff880817a71a80 0000000000000046 ffff880096d12f00 0000000000000441
May 14 00:02:50 dp46 kernel: ffff880818052938 ffff880818052848 ffff88081805c3b8 ffff88081805c3b8
May 14 00:02:50 dp46 kernel: ffff88081b22e6b8 ffff880817a71fd8 000000000000f4e8 ffff88081b22e6b8
May 14 00:02:50 dp46 kernel: Call Trace:
May 14 00:02:50 dp46 kernel: [<ffffffff8109b809>] ? ktime_get_ts+0xa9/0xe0
May 14 00:02:50 dp46 kernel: [<ffffffff81110b10>] ? sync_page+0x0/0x50
May 14 00:02:50 dp46 kernel: [<ffffffff814ed1e3>] io_schedule+0x73/0xc0
May 14 00:02:50 dp46 kernel: [<ffffffff81110b4d>] sync_page+0x3d/0x50
May 14 00:02:50 dp46 kernel: [<ffffffff814eda4a>] __wait_on_bit_lock+0x5a/0xc0
May 14 00:02:50 dp46 kernel: [<ffffffff81110ae7>] __lock_page+0x67/0x70
May 14 00:02:50 dp46 kernel: [<ffffffff81090c30>] ? wake_bit_function+0x0/0x50
May 14 00:02:50 dp46 kernel: [<ffffffff811271a5>] ? pagevec_lookup_tag+0x25/0x40
May 14 00:02:50 dp46 kernel: [<ffffffff811261f2>] write_cache_pages+0x392/0x4a0
May 14 00:02:50 dp46 kernel: [<ffffffff81124c80>] ? __writepage+0x0/0x40
May 14 00:02:50 dp46 kernel: [<ffffffff81126324>] generic_writepages+0x24/0x30
May 14 00:02:50 dp46 kernel: [<ffffffffa00774d7>] journal_submit_inode_data_buffers+0x47/0x50 [jbd2]
May 14 00:02:50 dp46 kernel: [<ffffffffa00779e5>] jbd2_journal_commit_transaction+0x375/0x14b0 [jbd2]
May 14 00:02:50 dp46 kernel: [<ffffffff8100975d>] ? __switch_to+0x13d/0x320
May 14 00:02:50 dp46 kernel: [<ffffffff8107c0ec>] ? lock_timer_base+0x3c/0x70
May 14 00:02:50 dp46 kernel: [<ffffffff81090bf0>] ? autoremove_wake_function+0x0/0x40
May 14 00:02:50 dp46 kernel: [<ffffffffa007d928>] kjournald2+0xb8/0x220 [jbd2]
May 14 00:02:50 dp46 kernel: [<ffffffff81090bf0>] ? autoremove_wake_function+0x0/0x40
May 14 00:02:50 dp46 kernel: [<ffffffffa007d870>] ? kjournald2+0x0/0x220 [jbd2]
May 14 00:02:50 dp46 kernel: [<ffffffff81090886>] kthread+0x96/0xa0
May 14 00:02:50 dp46 kernel: [<ffffffff8100c14a>] child_rip+0xa/0x20
May 14 00:02:50 dp46 kernel: [<ffffffff810907f0>] ? kthread+0x0/0xa0
May 14 00:02:50 dp46 kernel: [<ffffffff8100c140>] ? child_rip+0x0/0x20

The JBD is the journaling block device that sits between the file system and the block device driver. The jbd2 version is for ext4.

1.1.8 /proc/net

 
1.1.8.1 /proc/net/tcp

記錄全部tcp鏈接,netstat以及lsof都會讀取這個文件. 咱們遇到過一個問題就是netstat/lsof速度很是慢,經過strace發現是在讀取這個文件時候很是耗時,下面兩個連接給出了一些相關信息

todo(dirlt):

1.2 system utility

./images/linux-analysis-and-tools.jpg

1.2.1 SYS DEV

 

1.2.2 mpstat

mpstat - Report processors related statistics.

mpstat -P [cpu|ALL] -V internal count
  • -P 可以選擇特定的CPU來察看,或者是察看全部的CPU
  • internal 表示多少秒顯示一次
  • count 表示顯示多少次

一般使用就是"mpstat -P ALL 1"

[zhangyan@tc-cm-et18.tc.baidu.com]$ mpstat -P ALL 1
Linux 2.6.9_5-10-0-1 (tc-cm-et18.tc.baidu.com)  2011年09月20日

08時39分58秒  CPU   %user   %nice %system %iowait    %irq   %soft   %idle    intr/s
08時39分59秒  all   37.55    0.00    0.00    0.00    0.00    0.00   62.45   1009.00
08時39分59秒    0    0.00    0.00    0.00    0.00    0.00    0.00  100.00   1002.00
08時39分59秒    1  100.00    0.00    0.00    0.00    0.00    0.00    0.00      0.00
08時39分59秒    2    0.00    0.00    0.00    0.00    0.00    0.00  100.00      7.00
08時39分59秒    3  100.00    0.00    0.00    0.00    0.00    0.00    0.00      0.00
08時39分59秒    4  100.00    0.00    0.00    0.00    0.00    0.00    0.00      0.00
08時39分59秒    5    0.00    0.00    0.00    0.00    0.00    0.00  100.00      0.00
08時39分59秒    6    0.00    0.00    0.00    0.00    0.00    0.00   99.00      0.00
08時39分59秒    7    0.00    0.00    0.00    0.00    0.00    0.00  100.00      0.00

其中每一個字段的意思分別是:

  • CPU 處理器編號
  • %user 處理user狀態佔用率
  • %nice 處理user狀態而且是nice priority佔用率
  • %system 處理system狀態佔用率(不包括硬中斷和軟中斷)
  • %iowait 在進行io操做時候有cpu idle時間比率
  • %irq CPU耗費在硬中斷上時間比率
  • %soft CPU耗費在軟中斷上時間比率(軟中斷能夠被任何一個CPU處理)
  • %idle 在沒有io操做時候的cpu idle時間比率
  • intr/s 每秒CPU中斷次數(包括硬中斷和軟終端)

1.2.3 vmstat

vmstat [-a] [-n] [-S unit] [delay [ count]] // 查看全局內存開銷
vmstat [-f] // 查看自從boot依賴forks的次數(fork,vfork,clone),能夠認爲就是建立的task數目(包括線程)
vmstat [-s] // 系統事件統計次數以及內存統計
vmstat [-d] // 磁盤使用統計數據
vmstat [-m] // 內核開闢對象slabinfo
[dirlt@localhost.localdomain]$ vmstat 1
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      4  45752  33460  99324    0    0     1     1    1    9  0  0 99  0  0
 0  0      4  45752  33460  99324    0    0     0     0    1    8  0  0 100  0  0
  • r 多少個進程正在等待運行
  • b 多少個進程block住了
  • swpd 虛擬內存(swap)
  • free 空閒內存
  • buff 緩衝內存(buffers,用戶緩存文件inode)
  • cache 緩衝內存(cache,用戶做爲文件系統page-cache)
  • 前面三個參數相加纔是可用內存大小
  • si 讀取disk使用的虛擬內存
  • so 寫disk使用的虛擬內存
  • bi[block in] 塊設備讀取速率,blocks/s
  • bo[block out] 塊設備寫速度,blocks/s
  • in[interrupt] 每秒中斷個數
  • cs[context switch] 每秒上下文切換次數
  • us 用戶態時間[user]
  • sy 系統時間[system]
  • id 空閒時間[idle]
  • wa 等待io時間[wait IO]
  • st 虛擬機使用時間[stolen from VM]
[zhangyan04@tc-hpc-dev.tc.baidu.com]$ vmstat -d
disk- ------------reads------------ ------------writes----------- -----IO------
       total merged sectors      ms  total merged sectors      ms    cur    sec
sda    13873   1504  633974   37617 1366407 89873356 108823150 37820617      0    323
sda1      46     88    1072      34      0      0       0       0      0      0
sda2   10274    242  595386   24867  34050  35092 2474128 1078425      0    239
sda3    3515   1125   36820   12653 1332349 89838264 106349006 36742192      0    292
  • total 總共發起讀寫次數
  • merged 聚合讀寫次數
  • sectors 讀寫多少扇區
  • ms 耗時毫秒
  • cur 當前是否存在IO操做
  • sec 耗費在IO的時間(秒)
[zhangyan04@tc-hpc-dev.tc.baidu.com]$ vmstat -m
Cache                       Num  Total   Size  Pages
nfs_direct_cache              0      0    168     24
nfs_write_data               69     69    704     23
  • Num 當前多少個對象正在被使用
  • Total 總共有多少個對象能夠被使用
  • Size 每一個對象大小
  • Pages 佔用了多少個Page(這個Page上面至少包含一個正在被使用的對象)
[zhangyan04@tc-hpc-dev.tc.baidu.com]$ vmstat -s
      8191996  total memory
      4519256  used memory
      1760044  active memory
      2327204  inactive memory
      3672740  free memory
        76200  buffer memory
      3935788  swap cache
      1020088  total swap
            0  used swap
      1020088  free swap
       423476 non-nice user cpu ticks
           91 nice user cpu ticks
       295803 system cpu ticks
     70621941 idle cpu ticks
        39354 IO-wait cpu ticks
          800 IRQ cpu ticks
        52009 softirq cpu ticks
       317179 pages paged in
     54413375 pages paged out
            0 pages swapped in
            0 pages swapped out
    754373489 interrupts
    500998741 CPU context switches
   1323083318 boot time
       418742 forks

1.2.4 free

[zhangyan04@tc-hpc-dev.tc.baidu.com]$ free -t
             total       used       free     shared    buffers     cached
Mem:       8191996    4519868    3672128          0      76204    3935864
-/+ buffers/cache:     507800    7684196
Swap:      1020088          0    1020088
Total:     9212084    4519868    4692216
  • total 總內存大小[實際內存大小]
  • used 已經使用大小[buffers+shared+cached+應用程序使用]
  • free 未使用大小
  • shared 共享內存大小
  • buffers 緩存大小包括目錄inode
  • cached 用於操做文件使用緩衝[所謂cached的內容仍是能夠被使用的]
  • - buffers/cache 表示used-buffers/cached的大小,就是應用程序已經使用內存大小
  • + buffers/cache 表示free+buffers/cached的大小,就是全部可供應用程序使用的大小
  • Swap:total 交換分區大小
  • Swap:used 交換分區使用大小
  • Swap:free 可使用的分區大小

1.2.5 taskset

能夠用來獲取和修改進程的CPU親和性。

  • taskset -c 1,2,3 cmd (指定在CPU #1,#2,#3上運行cmd)
  • taskset -cp 1,2,3 pid (指定在CPU #1,#2,#3上運行進程pid)
  • taskset -c mask cmd|pid 經過mask格式進行設置,好比0x3就是#0和#1親和性

若是不指定-c的話那麼就是獲取親和性。程序上的話可使用sched_setaffinity/sched_getaffinity調用來修改和獲取某個進程和CPU的親和性。

taskset  is used to set or retrieve the CPU affinity of a running pro-
cess given its PID or to launch a new COMMAND with a given CPU  affin-
ity.  CPU affinity is a scheduler property that "bonds" a process to a
given set of CPUs on the system.  The Linux scheduler will  honor  the
given  CPU  affinity  and  the process will not run on any other CPUs.
Note that the Linux scheduler also supports natural CPU affinity:  the
scheduler  attempts to keep processes on the same CPU as long as prac-
tical for performance reasons.   Therefore,  forcing  a  specific  CPU
affinity is useful only in certain applications.

1.2.6 lsof

todo(dirlt):

1.2.7 hdparm

hdparm - get/set hard disk parameters

下面是使用的用法

/sbin/hdparm [ flags ] [device] ..

對於device的話能夠經過mount來查看

[dirlt@localhost.localdomain]$ mount
/dev/mapper/VolGroup00-LogVol00 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)

咱們關注本身讀寫目錄,好比一般在/home下面,這裏就是使用的device就是/dev/mapper/VolGroup00-LogVol00

todo(dirlt):好多選項都不太清楚是什麼意思.

1.2.8 pmap

todo(dirlt):

1.2.9 strace

todo(dirlt):

1.2.10 iostat

iostat主要用來觀察io設備的負載狀況的。首先咱們看看iostat的樣例輸出

dirlt@dirlt-virtual-machine:~$ iostat
Linux 3.2.0-23-generic (dirlt-virtual-machine)  2012年06月18日  _x86_64_        (1 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.91    0.05    4.05    5.08    0.00   89.91

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
scd0              0.01         0.04         0.00         48          0
sda              31.09       203.67      9862.91     260487   12614468

第一行顯示了CPU平均負載狀況,而後給出的信息是自從上一次reboot起來今的iostat平均信息。若是咱們使用iostat採用interval輸出的話,那麼下一次的數值是相對於上一次的數值而言的。這裏解釋一下CPU的各個狀態:

  • %user 用戶態比率
  • %nice 用戶態使用nice priority比率
  • %system 系統態比率
  • %iowait CPU在等待IO操做時候idle比率
  • %steal todo(dirlt)
  • %idle idle比率可是不包括在等待IO操做idle比率

而後在來看看iostat的命令行參數

iostat [ -c ] [ -d ] [ -h ] [ -N ] [ -k | -m ] [ -t ] [ -V ] [ -x ] [ -z ] [ device [...] | ALL  ]  [  -p  [
device [,...] | ALL ] ] [ interval [ count ] ]

其中interval表示每隔x時間刷新一次輸出,而count表示但願輸出多少次.下面解釋一下每隔參數的含義:

  • -c 產生CPU輸出(只是生成CPU輸出)
  • -d 生成device使用狀況輸出
  • -h 和-d同樣只不過human readable一些(不過其實差很少)
  • -k 統計數據按照kilobytes爲單位
  • -m 統計數據按照megabytes爲單位
  • -N todo(dirlt)
  • -p 選擇block device以及其partitions.若是使用ALL的話那麼選擇全部block devices.
  • -t 每次輸出都是打印當前時間
  • -V version
  • -x 顯示擴展數據(extended)
  • -z 忽略輸出那些在period期間沒有任何活動的device.

iostat也能夠指定選擇輸出哪些block device.

一般命令也就是iostat -d -k -x 1.咱們來看看樣例輸出

dirlt@dirlt-virtual-machine:~$ iostat -d -x -k 1
Linux 3.2.0-23-generic (dirlt-virtual-machine)  2012年06月18日  _x86_64_        (1 CPU)

xsDevice:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util
scd0              0.00     0.00    0.00    0.00     0.02     0.00     8.00     0.00   29.00   29.00    0.00  29.00   0.01
sda               6.34     5.72   15.95   12.60   192.62  5675.85   411.10     2.48   87.41   18.33  174.88   2.74   7.83

Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util
scd0              0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00
sda               0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00

Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util
scd0              0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00
sda               0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00

而後分析其中字段:

  • Device 設備名
  • tps number of transfers per second.對於IO device多個請求可能會合併成爲一個transfer.不過彷佛沒有太大意義。
  • Blk_read/s(kB_read/s, MB_read/s). 讀速度,這裏Block至關於一個扇區佔據512bytes.
  • Blk_wrtn/s(kB_wrtn/s, MB_wrtn/s). 寫速度。
  • Blk_read(kb_read, MB_read). 讀總量
  • Blk_wrtn(kB_wrtn, MB_wrtn). 寫總量
  • rrqm/s 每秒多少個讀請求被merge.當系統調用須要讀取數據的時候,VFS將請求發到各個FS,若是FS發現不一樣的讀取請求讀取的是相同Block的數據,FS會將這個請求合併Merge
  • wrqm/s 每秒多少個些請求被merge
  • r/s 每秒多少個讀請求(after merged)
  • w/s 每秒多少個寫請求(after merged)
  • rsec/s(rkB/s, rMB/s) 讀速度。sec表示sector.
  • wsec/s(wkB/s, wMB/s) 寫速度
  • avgrq-sz 提交到這個device queue上面請求的平均大小(in sectors)
  • avgqu-sz 提交到這個device queue上面請求的平均長度
  • await 提交到這個device上面請求處理的平均時間(ms)
  • r_await 提交到這個device上面讀請求處理的平均時間
  • w_await 提交到這個device上面寫請求處理的平均時間
  • svctm todo(dirlt):don't use it.
  • %util CPU在處理IO請求時的百分比.能夠認爲是IO設備使用率.

1.2.11 vmtouch

https://github.com/hoytech/vmtouch

note(dirlt):能夠用來warmup數據,使用參數彷佛也比較簡單

裏面有一些系統調用比較值得注意和學習:

  • mlock/munlock 可以將一部分的內存lock住而不會切換到swap分區上面。
  • mlockall/munlockall 將進程全部的內存都lock住而不會切換到swap分區上面。
  • posix_fadvise 針對fd作一些訪問模式的預測

1.2.12 latencytop

todo(dirlt): https://latencytop.org/

1.2.13 iotop

能夠用來觀察單獨進程的IO使用情況

todo(dirlt):

1.2.15 slurm

Simple Linux Utility for Resource Management todo(dirlt): https://computing.llnl.gov/linux/slurm/

1.2.16 sar

sar - Collect, report, or save system activity information.

下面是全部的選項

  • -A 至關於指定 -bBcdqrRuvwWy -I SUM  -n FULL -P ALL.
  • -b 這個選項只是針對2.5如下的內核.統計磁盤的IO運行狀況
    • tps // 對磁盤總共有多少個請求.
    • rtps // 對磁盤總共有多少個讀請求.
    • wtps // 對磁盤總共有多少個寫請求.
    • bread/s // 磁盤每秒多少個read block.(512bytes/block)
    • bwrtn/s // 磁盤每秒多少個write block.(512bytes/block)
  • -B 內存頁面狀況
    • pgpgin/s // page in from disk
    • pgpgout/s // page out to disk.
    • fault/s // (major+mintor) page fault
    • majflt/s // major page fault
  • -c 進程建立狀況
    • proc/s 每秒建立進程個數
  • -d 塊設備運行狀況.打印出的dev m-n(major-mior device number)
    • tps 對這個設備總共有多少個請求.若是多個邏輯請求的話可能會結合成爲一個請求.
    • rd_sec/s 每秒讀取多少個sector.一個sector 512bytes.
    • wr_sec/s 每秒寫入多少個sector.一個sector 512bytes.
  • -e hh:mm:ss 設置彙報的終止時間
  • -f filename 從文件中讀入
  • -h -H 從文件讀入時轉換稱爲分析方便的格式
  • -i interval 從文件中讀入的話選取interval seconds的間隔的記錄
  • -I <irq> | SUM | ALL | XALL 中斷統計
    • <irq> interrupt number
    • SUM 全部中斷次數之和/s
    • ALL 前16箇中斷次數/s
    • XALL 全部中斷次數/s.包括APIC
  • -n DEV | EDEV | SOCK | FULL 網絡統計數據
    • DEV 全部網絡接口正常數據
    • EDEV 全部網絡接口異常數據
    • SOCK socket數據統計
    • FULL DEV + EDEV + SOCK
    • 關於網絡接口顯示數據在後面會仔細討論
  • -o filename 能夠將sar輸出重定位
  • -P <cpu> | ALL 處理器統計數據
  • -q 處理器請求隊列長度以及平均負載
    • runq-sz 處理器上有多少個進程在等待執行
    • plist-sz 當前有多少個進程運行
    • ldavg-1 過去1分鐘系統負載
    • ldavg-5 過去5分鐘系統負載
    • ldavg-15 過去15分鐘系統負載
  • -r 內存和swap空間利用率數據
    • kbmemfree 多少free內存(KB)
    • kbmemused 多少使用內存(KB)(不包括kernel使用)
    • %memused 使用內存率
    • kbbuffers kenel內部使用buffer(KB)
    • kbcached kernel內部使用cache data(KB)
    • kbswpfree swap可用大小(KB)
    • kbswpused swap使用大小(KB)
    • %swpused swap使用比率
    • kbswpcad cached swap內存大小(KB)(曾經被換出如今換入,可是依然佔用swap分區)
  • -R 內存使用數據
    • frmpg/s free出來的page數目/s
    • bufpg/s 當作buffer使用的page數目/s
    • campg/s 當作cache使用的page數目/s
  • -s hh:mm:ss 設置彙報起始時間
  • -t 從文件載入時候使用使用原有locale time表示而不是當前locale
  • -u CPU使用狀況
    • %user 用戶態CPU佔用率
    • %nice 用戶態使用nice的CPU佔用率
    • %system 內核態CPU佔用率
    • %iowait CPU由於等待io的idle比率
    • %idle CPU idle比率
  • -v inode,file以及kernel tables狀況
    • dentunusd 在directory cache裏面沒有使用的cache entries個數
    • file-sz 使用文件句柄數
    • inode-sz 使用inode句柄數
    • super-sz 內存使用的超級塊句柄數
    • %super-sz 當前超級塊句柄數使用比率
    • dquot-sz 磁盤配額分配的entries個數
    • %dquot-sz 磁盤配額分配的entries比率
    • rtsig-sz 排隊的RT signals數量
    • %rtsig-sz 排隊的RT signals比率
  • -V 版本號
  • -w 系統切換狀況
    • cswch/s 上下文切換次數/s
  • -W swap統計數據
    • pswpin/s 每秒換入swap pages
    • pswpout/s 每秒換出swap pages
  • -x pid | SELF | ALL  進程統計數據
    • minflt/s minor faults/s
    • majflt/s major faults/s
    • %user 用戶態CPU利用率
    • %system 內核態CPU利用率
    • nswap/s 每秒換出pages
    • CPU 處理器編號
  • -X pid | SELF | ALL 統計子進程數據
    • cminflt/s
    • cmajflt/s
    • %cuser
    • %csystem
    • cnswaps/s
  • -y TTY設備狀況
    • rcvin/s 每一個serial line接收中斷數目/s
    • xmtin/s 每一個serial line發出中斷數目/s

關於網絡接口數據顯示的話,下面是使用DEV能夠查看的字段

  • IFACE network interface
  • rxpck/s rcv packets/s
  • txpck/s snd packets/s
  • rxbyt/s rcv bytes/s
  • txbyt/s snd bytes/s
  • rxcmp/s rcv compressed packets/s
  • txcmp/s snd compressed packets/s
  • rxmcst/s 接收多播packets/s

下面是使用EDEV能夠查看的字段

  • rxerr/s 接收bad packets/s
  • txerr/s 發送bad packets/s
  • coll/s 每秒發送packets出現collisions數目
  • rxdrop/s 每秒由於linux缺乏buffer而丟棄packets
  • txcarr/s 每秒發送packets出現carrier-errors數目
  • rxfram/s 每秒接收packets出現frame alignment error數目
  • rxfifo/s 每秒接收packets出現FIFO overrun error數目
  • txfifo/s 每秒發送packets出現FIFO overrun error數目

下面是使用SOCK能夠查看的字段

  • totsock 總共使用socket數目
  • tcpsck 正在使用tcp socket數目
  • udpsck 正在使用udp socket數目
  • rawsck 正在使用RAW socket數目
  • ip-frag 當前使用IP fragments數目

選項很是多,可是不少選項沒有必要打開。對於網絡程序來講的話,一般咱們使用到的選項會包括

  • -n DEV 網卡流量
  • -n EDEV 網卡丟包以及kerner buffer問題
  • -n SOCK 多少鏈接
  • -r 內存使用。不經常使用。不少時候只關心大概使用多少內存,是否持續增加)
  • -u CPU使用 -P ALL查看全部CPU
  • -w 上下文切換。相對來講也不經常使用。

一般咱們使用的命令就應該是sar -n DEV -P ALL -u 1 0(1表示1秒刷新,0表示持續顯示)

01時56分18秒       CPU     %user     %nice   %system   %iowait     %idle
01時56分19秒       all      7.52      0.00     78.95      0.25     13.28
01時56分19秒         0      7.00      0.00     81.00      0.00     12.00
01時56分19秒         1      7.92      0.00     79.21      0.00     12.87
01時56分19秒         2      9.00      0.00     78.00      1.00     12.00
01時56分19秒         3      7.07      0.00     77.78      0.00     15.15

01時56分18秒     IFACE   rxpck/s   txpck/s   rxbyt/s   txbyt/s   rxcmp/s   txcmp/s  rxmcst/s
01時56分19秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
01時56分19秒      eth0      0.00      0.00      0.00      0.00      0.00      0.00      0.00
01時56分19秒      eth1 159997.00 320006.00 175029796.00 186239160.00      0.00      0.00      0.00

1.2.17 netstat

netstat - Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships

netstat能夠查看不少信息,包括網絡連接,路由表,網卡信息,假裝連接以及多播成員關係。可是從文檔上看,一部分工做能夠在/sbin/ip裏面完成

NOTE
       This program is obsolete.  Replacement for netstat is ss.  Replacement
       for netstat -r is ip route.  Replacement for netstat -i is ip -s link.
       Replacement for netstat -g is ip maddr.

咱們這裏打算對netstat使用限制在查看網絡鏈接,以及各類協議的統計數據上.


首先咱們看看如何查看各類協議的統計數據.

netstat {--statistics|-s} [--tcp|-t] [--udp|-u] [--raw|-w] [delay]

咱們能夠查看和tcp,udp以及raw socket相關的數據,delay表示刷新時間。

[zhangyan04@tc-hpc-dev.tc.baidu.com]$ netstat -s
Ip:
    322405625 total packets received
    0 forwarded
    0 incoming packets discarded
    322405625 incoming packets delivered
    369134846 requests sent out
    33 dropped because of missing route
Icmp:
    30255 ICMP messages received
    0 input ICMP message failed.
    ICMP input histogram:
        echo requests: 30170
        echo replies: 83
        timestamp request: 2
    30265 ICMP messages sent
    0 ICMP messages failed
    ICMP output histogram:
        destination unreachable: 10
        echo request: 83
        echo replies: 30170
        timestamp replies: 2
IcmpMsg:
        InType0: 83
        InType8: 30170
        InType13: 2
        OutType0: 30170
        OutType3: 10
        OutType8: 83
        OutType14: 2
Tcp:
    860322 active connections openings
    199165 passive connection openings
    824990 failed connection attempts
    43268 connection resets received
    17 connections established
    322306693 segments received
    368937621 segments send out
    56075 segments retransmited
    0 bad segments received.
    423873 resets sent
Udp:
    68643 packets received
    10 packets to unknown port received.
    0 packet receive errors
    110838 packets sent
UdpLite:
TcpExt:
    1999 invalid SYN cookies received
    5143 resets received for embryonic SYN_RECV sockets
    2925 packets pruned from receive queue because of socket buffer overrun
    73337 TCP sockets finished time wait in fast timer
    85 time wait sockets recycled by time stamp
    4 delayed acks further delayed because of locked socket
    Quick ack mode was activated 7106 times
    5141 times the listen queue of a socket overflowed
    5141 SYNs to LISTEN sockets ignored
    81288 packets directly queued to recvmsg prequeue.
    297394763 packets directly received from backlog
    65102525 packets directly received from prequeue
    180740292 packets header predicted
    257396 packets header predicted and directly queued to user
    5983677 acknowledgments not containing data received
    176944382 predicted acknowledgments
    2988 times recovered from packet loss due to SACK data
    Detected reordering 9 times using FACK
    Detected reordering 15 times using SACK
    Detected reordering 179 times using time stamp
    835 congestion windows fully recovered
    1883 congestion windows partially recovered using Hoe heuristic
    TCPDSACKUndo: 1806
    1093 congestion windows recovered after partial ack
    655 TCP data loss events
    TCPLostRetransmit: 6
    458 timeouts after SACK recovery
    7 timeouts in loss state
    3586 fast retransmits
    178 forward retransmits
    425 retransmits in slow start
    51048 other TCP timeouts
    37 sack retransmits failed
    1610293 packets collapsed in receive queue due to low socket buffer
    7094 DSACKs sent for old packets
    14430 DSACKs received
    4358 connections reset due to unexpected data
    12564 connections reset due to early user close
    29 connections aborted due to timeout
    TCPDSACKIgnoredOld: 12177
    TCPDSACKIgnoredNoUndo: 347
    TCPSackShifted: 6421
    TCPSackMerged: 5600
    TCPSackShiftFallback: 119131
IpExt:
    InBcastPkts: 22
    InOctets: 167720101517
    OutOctets: 169409102263
    InBcastOctets: 8810

內容很是多這裏也不詳細分析了。


而後看看鏈接這個部分的功能

netstat  [address_family_options]  [--tcp|-t]  [--udp|-u]   [--raw|-w]
[--listening|-l]       [--all|-a]      [--numeric|-n]      [--numeric-
hosts][--numeric-ports][--numeric-ports]               [--symbolic|-N]
[--extend|-e[--extend|-e]] [--timers|-o] [--program|-p] [--verbose|-v]
[--continuous|-c] [delay]

對於address_family容許指定協議族,一般來講咱們可能會使用

  • –unix -x unix域協議族
  • –inet –ip ip協議族

而後剩下的選項

  • –tcp -t tcp socket
  • –udp -u udp socket
  • –raw -w raw socket
  • –listening -l listen socket
  • –all -a listen socket和connected socket.
  • –numeric -n
  • –numeric-hosts
  • –numeric-ports
  • –numeric-users
  • –symbolic -N ???
  • –extend -e 擴展信息
  • –timers -o 網絡計時器信息(???)
  • –program -p socket所屬program
  • –verbose -v
  • –continuous -c 1s刷新
  • delay 刷新時間(sec)

咱們看看一個使用的例子

[zhangyan04@tc-hpc-dev.tc.baidu.com]$ netstat --ip --tcp -a -e -p
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       User       Inode      PID/Program name
tcp        0      0 tc-hpc-dev.tc.baidu.c:19870 *:*                         LISTEN      zhangyan04 30549010   28965/echo_server
tcp     1024      0 tc-hpc-dev.tc.baidu.c:19870 tc-com-test00.tc.baid:60746 ESTABLISHED zhangyan04 30549012   28965/echo_server
tcp        0   1024 tc-hpc-dev.tc.baidu.c:19870 tc-com-test00.tc.baid:60745 ESTABLISHED zhangyan04 30549011   28965/echo_server

下面是對於tcp socket的字段解釋.對於unix domain socket字段不一樣可是這裏不寫出來了.

  • Proto socket類型
  • Recv-Q 在這個socket上還有多少bytes沒有copy到用戶態
  • Send-Q 在這個socket還有多少bytes沒有ack
  • Local Address 本地地址
  • Foreign Address 遠程地址
  • State 鏈接狀態
    • ESTABLISHED
    • SYN_SENT
    • SYN_RECV
    • FIN_WAIT1
    • FIN_WAIT2
    • TIME_WAIT
    • CLOSED
    • CLOSE_WAIT 大量狀態表示服務器句柄泄露
    • LAST_ACK
    • LISTEN
    • CLOSING
    • UNKNOWN
  • User
  • PID/Program name

1.2.18 tcpdump

todo(zhangyan04):

1.2.19 iftop

1.2.20 iftraf

todo(dirlt): http://iptraf.seul.org/

1.2.21 rsync

經常使用選項:

  • -a –archive
  • -r –recursive
  • -v –verbose
  • -z –compress

經常使用命令:

  • 同步目錄 rsync -avrz dir user@host:/dir/ note(dirlt):注意目錄後面須要加上/
  • 同步文件 rysnc -vz /dir/file1 user@host:/dir/file

1.2.22 iodump

 

1.2.23 iopp

 

1.2.24 nethogs

todo(dirlt):

1.2.25 slabtop

slabtop - display kernel slab cache information in real time

1.2.26 nmon

nmon - systems administrator, tuner, benchmark tool.

http://nmon.sourceforge.net/pmwiki.php Nigel's performance Monitor for Linux

1.2.27 collectl

collectl http://collectl.sourceforge.net/ todo(dirlt):彷佛至關不錯,將不少關鍵信息都作了收集和整合

1.2.28 numactl

todo(dirlt):

1.2.29 jnettop

todo(dirlt):

1.2.30 glances

1.2.31 ifconfig

ifconfig - configure a network interface

/sbin/ifconfig能夠用來配置和查看network interface.不過從文檔上看的話,更加推薦使用/sbin/ip這個工具

NOTE
       This  program is obsolete!  For replacement check ip addr and ip link.
       For statistics use ip -s link.

這裏咱們不打算學習如何配置network interface只是想查看一下網卡的信息。使用/sbin/ifconfig -a能夠查看全部的網卡信息,即便這個網卡關閉。

[zhangyan04@tc-hpc-dev.tc.baidu.com]$ /sbin/ifconfig -a
eth0      Link encap:Ethernet  HWaddr 00:1D:09:22:C9:A9
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
          Interrupt:16 Memory:f4000000-f4012800

eth1      Link encap:Ethernet  HWaddr 00:1D:09:22:C9:A7
          inet addr:10.26.140.39  Bcast:10.26.140.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:90671796 errors:0 dropped:14456 overruns:0 frame:0
          TX packets:143541932 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:71169282564 (66.2 GiB)  TX bytes:74096812979 (69.0 GiB)
          Interrupt:16 Memory:f8000000-f8012800

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:231762640 errors:0 dropped:0 overruns:0 frame:0
          TX packets:231762640 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:98089257363 (91.3 GiB)  TX bytes:98089257363 (91.3 GiB)

咱們這裏稍微仔細看看eht1的網卡信息

  • encap 網卡鏈接網絡類型Ethernet(以太網)
  • HWaddr 網卡物理地址
  • inet_addr 綁定IP地址
  • Bcast 這個IP對應的廣播地址
  • Mask 子網掩碼
  • UP(打開) BROADCAST(廣播) MULTICAST(多播) RUNNING(運行)
  • MTU (Maximum Transport Unit)最大傳輸單元,以太網通常就是1500字節
  • Metric ???
  • RX packets 接受packets數目
  • TX packets 傳輸packets數目
  • errors 錯誤packets數目
  • dropped 丟棄packets數目
  • overruns frame carrier ???
  • collisions ???
  • txqueuelen 發送packets的queue長度上限.0應該是無限
  • RX bytes 接收字節
  • TX bytes 發送字節

使用ifconfig可以建立虛擬網卡綁定IP

  • sudo ifconfig <name> <ip>
  • sudo ifconfig <name> down # 銷燬虛擬網卡

1.2.32 ps(process snapshot)

進程狀態有下面幾種:

  • D    uninterruptible sleep (usually IO)
  • R    running or runnable (on run queue)
  • S    interruptible sleep (waiting for an event to complete)
  • T    stopped, either by a job control signal or because it is being traced.
  • W    paging (not valid since the 2.6.xx kernel)
  • X    dead (should never be seen)
  • Z    defunct ("zombie") process, terminated but not reaped by its parent.
  • <    high-priority (not nice to other users)
  • N    low-priority (nice to other users)
  • L    has pages locked into memory (for real-time and custom IO)
  • s    is a session leader
  • l    is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
  • +    is in the foreground process group.

在使用ubuntu的apt-get時候,可能會出現一些異常的情況,咱們直接終止了apt-get。可是這個時候apt-get軟件自己出於一個不正常的狀態, 致使以後不可以啓動apt-get。若是觀察進程的話會出現下面一些可疑的進程

dp@dp1:~$ ps aux | grep "apt"
root      3587  0.0  0.0  36148 22800 ?        Ds   Oct08   0:00 /usr/bin/dpkg --status-fd 50 --unpack --auto-deconfigure /var/cache/apt/archives/sgml-data_2.0.4_all.deb
root      9579  0.0  0.0  35992 22744 ?        Ds   Oct19   0:00 /usr/bin/dpkg --status-fd 50 --unpack --auto-deconfigure /var/cache/apt/archives/iftop_0.17-16_amd64.deb
root     25957  0.0  0.0  36120 22796 ?        Ds   Nov05   0:00 /usr/bin/dpkg --status-fd 50 --unpack --auto-deconfigure /var/cache/apt/archives/iftop_0.17-16_amd64.deb /var/cache/apt/archives/iotop_0.4-1_all.deb
dp       30586  0.0  0.0   7628  1020 pts/2    S+   08:59   0:00 grep --color=auto apt

這些進程的父進程都是init進程,而且狀態是uninterruptible sleep,給kill -9也沒有辦法終止,惟一的辦法只能reboot機器來解決這個問題。關於這個問題能夠看stackoverflow上面的解答 How to stop 'uninterruptible' process on Linux? - Stack Overflow http://stackoverflow.com/questions/767551/how-to-stop-uninterruptible-process-on-linux

  • Simple answer: you cannot. Longer answer: the uninterruptable sleep means the process will not be woken up by signals. It can be only woken up by what it's waiting for. When I get such situations eg. with CD-ROM, I usually reset the computer by using suspend-to-disk and resuming.
  • The D state basically means that the process is waiting for disk I/O, or other block I/O that can't be interrupted. Sometimes this means the kernel or device is feverishly trying to read a bad block (especially from an optical disk). Sometimes it means there's something else. The process cannot be killed until it gets out of the D state. Find out what it is waiting for and fix that. The easy way is to reboot. Sometimes removing the disk in question helps, but that can be rather dangerous: unfixable catastrophic hardware failure if you don't know what you're doing (read: smoke coming out).

1.2.33 ulimit

todo(dirlt)

1.2.34 sysprof

Sysprof - Statistical, system-wide Profiler for Linux : http://sysprof.com/

1.2.35 ss

 

1.2.36 SYS ADMIN

 

1.2.37 uptime

 

1.2.38 top

 

1.2.39 htop

 

1.2.40 ttyload

 

1.2.41 dmesg

可以察看開機時啓動信息(啓動信息保存在/var/log/dmesg文件裏)

[dirlt@localhost.localdomain]$ dmesg | less
Linux version 2.6.23.1-42.fc8 (kojibuilder@xenbuilder4.fedora.phx.redhat.com) (gcc version 4.1.2 20070925 (Red Hat 4.1.2-33)) #1 SMP Tue Oct 30 13:55:12 EDT 2007
BIOS-provided physical RAM map:
 BIOS-e820: 0000000000000000 - 000000000009f800 (usable)
 BIOS-e820: 000000000009f800 - 00000000000a0000 (reserved)
 BIOS-e820: 00000000000ca000 - 00000000000cc000 (reserved)
 BIOS-e820: 00000000000dc000 - 0000000000100000 (reserved)
 BIOS-e820: 0000000000100000 - 000000000fef0000 (usable)
 BIOS-e820: 000000000fef0000 - 000000000feff000 (ACPI data)
 BIOS-e820: 000000000feff000 - 000000000ff00000 (ACPI NVS)
 BIOS-e820: 000000000ff00000 - 0000000010000000 (usable)
 BIOS-e820: 00000000fec00000 - 00000000fec10000 (reserved)
 BIOS-e820: 00000000fee00000 - 00000000fee01000 (reserved)
 BIOS-e820: 00000000fffe0000 - 0000000100000000 (reserved)

1.2.42 quota

http://blog.itpub.net/post/7184/488931

quota用來爲用戶編輯磁盤配額。

  • 初始化磁盤配額功能

修改/etc/fstab增長usrquota以及grpquota

/dev/hdb1 /home/extra ext3 defaults,noatime,usrquota,grpquota 1 2
  • 創建磁盤配額文件
# cd /home/extra
# touch aquota.user
# touch aquota.group
# chmod 600 aquota.user
# chmod 600 aquota.group
  • 從新掛載磁盤。mount -o remount /home/extra
  • 掃描磁盤,初始化磁盤配額表。修改以前創建的aquota文件。/sbin/quotacheck -uvg /home/extra
  • 編輯用戶磁盤配額

若是爲用戶設定可使用/usr/sbin/edquota -u testuser,若是須要爲羣組設定的話/usr/sbin/edquota -g testgrp.

Disk quotas for user testuser (uid 517):
Filesystem blocks soft hard inodes soft hard
/dev/hdb1 0 0 0 0 0 0

各個字段含義以下:

  • blocks:已經使用的磁盤塊(1k bytes)
  • soft:軟限額大小,若是用戶超出了這個限額,系統會發出警告,若是在設定的時間內尚未回到該限額如下,系統將拒絕該用戶使用額外的磁盤空間。
  • hard:硬限額,用戶不容許超出的磁盤限制
  • inodes:用戶已經使用的inodes數
  • soft和hard含義同上,不過,這裏是限制用戶建立的文件/目錄總數。

可使用/usr/sbin/edquota -t來修改軟限額期限。

  • 啓動磁盤配額可使用/sbin/quotaon -av,關閉磁盤配額可使用/sbin/quotaoff -av.
  • 查看總體磁盤配額可使用/usr/sbin/repquota -a,查看用戶配額狀況可使用quota -vu test

能夠對WWW空間,FTP空間,Email空間進行磁盤配額限制。Quota只能基於磁盤分區進行配額管理,不能基於目錄進行配額管理,所以只能把數據存放在有配額限制的分區,再用符號連接到實際應用的目錄。

1.2.43 crontab

crontab就是爲了可以使得工做自動化在特定的時間或者是時間間隔執行特定的程序。crontab -e就能夠編輯crontab配置文件,默認是vim編輯器。crontab配置文件裏面能夠像shell同樣定義變量,以後就是任務描述,每個任務分爲6個字段: minute hour day month week command

  1. minute範圍是0-59
  2. hour範圍是0-23
  3. day範圍是1-31
  4. month範圍是1-12
  5. week範圍是0-7[0/7表示Sun]

對於每一個字段能夠有3種表示方式

  1. - 表示匹配全部時間
  2. num 表示徹底匹配
  3. num1-num2 表示匹配範圍
  4. num1,num2,num3… 進行枚舉匹配
  5. num1-num2/interval 表示從num1-num2時間段內每interval間隔

對於系統級別的crontab配置文件在/etc/crontab貌似裏面還多了一個用戶字段.下面是幾個配置的例子:

0 6 * * * /home/dirlt/platform/apache/bin/apachectl restart 天天早上6點重啓apache
0,30 18-23 * * * /home/dirlt/platform/apache/bin/apachectl restart 天天18-23時候每隔半小時重啓apache
\* 23-7/1 * * * /* 23-7/1 * * * /usr/local/apache/bin/apachectl restart 天天從23到第二天7點每隔一小時重啓apache

1.2.44 ntp

  • ntp.org: Home of the Network Time Protocol http://www.ntp.org/ note(dirlt):這個項目應該是很是複雜的

ntp(network time protocol)是用來作機器時間同步的,包含下面幾個組件:

  • ntpd(ntp daemon) 後臺進程能夠用來從其餘機器上面同步時間,也能夠爲其餘機器提供時間同步。配置文件是/etc/ntpd.conf.
  • ntpq(ntp query) 查詢當前時間同步的狀況。
  • ntpdate(ntp date) 和其餘機器進行同步,不能夠和ntpd共同使用。
[dirlt@umeng-ubuntu-pc] > sudo /etc/init.d/ntp start
 * Starting NTP server ntpd
   ...done.
[dirlt@umeng-ubuntu-pc] > sudo /usr/sbin/ntpdate cn.pool.ntp.org
12 Nov 17:08:07 ntpdate[30044]: the NTP socket is in use, exiting
[dirlt@umeng-ubuntu-pc] > sudo /etc/init.d/ntp stop
 * Stopping NTP server ntpd
   ...done.
[dirlt@umeng-ubuntu-pc] > sudo /usr/sbin/ntpdate cn.pool.ntp.org
12 Nov 17:08:22 ntpdate[30061]: adjust time server 202.112.31.197 offset -0.071953 sec
[dirlt@umeng-ubuntu-pc] > ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 netmon2.dcs1.bi 121.182.147.191  2 u    7   64    1  405.743   10.731   0.000
 202-150-213-154 133.243.238.163  2 u    8   64    1  171.722  -131.73   0.000
 dns1.synet.edu. 223.255.185.2    2 u    7   64    1  166.558  -39.375   0.000
 Hshh.org        66.220.9.122     2 u    6   64    1   41.734  -109.29   0.000
 europium.canoni 193.79.237.14    2 u    4   64    1  673.181  -240.24   0.000

一個最重要的問題就是,daemon以什麼時間間隔來和指定的server進行同步。

ntp是能夠在minpoll和maxpoll指定的時間間隔內來選擇同步間隔的,默認使用minpoll也就是64seconds.

其實若是不考慮爲其餘機器提供服務的話,徹底能夠在cron裏面使用ntpdate來進行同步。

1.2.45 cssh

todo(dirlt):

1.2.46 iptables

查看當前過濾規則 iptables -L/-S

➜  ~GALERA_SRC git:(yan) sudo iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

➜  ~GALERA_SRC git:(yan) sudo iptables -S
-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT

可使用 iptables -A [chain] [chain-specification]來添加規則

sudo iptables -A INPUT -s "192.168.100.104" -d "192.168.100.105" -j DROP

其中chain指INPUT, 以後部分都是chain-specification. 其中s表示過濾源地址,d表示目的地址,而-j而表示動做。

可使用 iptables -D 來刪除規則。其中規則既可使用rule-num來引用,也可使用chain-specification來指定

➜  ~GALERA_SRC git:(yan) sudo iptables -S
-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT
-A INPUT -s 192.168.100.104/32 -d 192.168.100.105/32 -j DROP
-A INPUT -s 192.168.100.105/32 -d 192.168.100.104/32 -j DROP
➜  ~GALERA_SRC git:(yan) sudo iptables -D INPUT 1
➜  ~GALERA_SRC git:(yan) sudo iptables -D INPUT -s 192.168.100.105/32 -d 192.168.100.104/32 -j DROP
➜  ~GALERA_SRC git:(yan) sudo iptables -S
-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT

1.2.47 HTTP BENCH TOOL

 

1.2.48 httperf

download http://www.hpl.hp.com/research/linux/httperf/

paper http://www.hpl.hp.com/research/linux/httperf/wisp98/httperf.pdf

httperf是用來測試HTTP server性能的工具,支持HTTP1.0和1.1.下面是這個工具命令行參數

httperf  [--add-header S] [--burst-length N] [--client I/N] [--close-with-reset] [-d|--debug N] [--failure-
status N] [-h|--help] [--hog] [--http-version S] [--max-connections N] [--max-piped-calls N]  [--method  S]
[--no-host-hdr]  [--num-calls  N]  [--num-conns  N]  [--period  [d|u|e]T1[,T2]]  [--port  N] [--print-reply
[header|body]] [--print-request [header|body]] [--rate X] [--recv-buffer N]  [--retry-on-failure]  [--send-
buffer  N]  [--server  S]  [--server-name  S] [--session-cookie] [--ssl] [--ssl-ciphers L] [--ssl-no-reuse]
[--think-timeout X] [--timeout X] [--uri S] [-v|--verbose] [-V|--version] [--wlog  y|n,F]  [--wsess  N,N,X]
[--wsesslog N,X,F] [--wset N,X]

httperf有幾種不一樣的workload方式:

  • request-oriented(RO)
  • session-oriented(SO)

note(dirlt):關於session-oriented這個概念,是後來看了論文裏面才清楚的。主要解決的就是實際中browse的場景。 一般咱們請求一個頁面裏面都會嵌入不少objects包括js或者是css等。咱們一次瀏覽稱爲session,而session裏面會有不少請求。 這些請求一般是,首先等待第一個請求處理完成(瀏覽器解析頁面),而後同時發起其餘請求。

httperf --num-calls=10 --num-conns=100 --http-version=1.1 --method=GET --rate=10 --timeout=2.0 --server=dp5 --
port=12347 --uri="/tell?reqid=3&account=dirlt&timeout=1000&reqtype=geographic&imei=123"

經常使用選項

  • –add-header=S 在http裏面添加header.好比–add-header="Referer: foo\nAuth: Secret\n".容許使用轉義字符。
  • –burst-length=N 每一個burst包含N個calls. 這個參數的意思在不一樣的workload下面含義不一樣。
  • –no-host-hdr 在header不帶上Host字段
  • –client=I/N 指定咱們模擬實際有N個clients而咱們做爲第I個clients狀況。由於一些參數(–wset)會根據clients的特徵不一樣生成不一樣的workload.若是是單機狀況的話那麼就是0/1
  • –close-with-reset 不經過正常關閉流程而是直接RST分節
  • -d/–debug=N 調試級別
  • –failure-status=N 若是http coder=N的話那麼就認爲失敗
  • -h/–help 幫助信息
  • –hog 儘量使用更多的TCP端口。在壓力測試下一般開啓
  • –http-version=S 指定http協議版本(1.0/1.1)
  • –max-conections=N SO模式下有效。在一個session上最多創建N個鏈接,配合–wsess和–wsesslog使用
  • –max-piped-calls=N SO模式下有效。在一個鏈接上最多發起N個pipelined calls.(非阻塞上write屢次而後read屢次) 配合–wsess和–wsesslog使用
  • –method=S 指定http請求方法,GET, HEAD, PUT, POST
  • –num-calls=N RO模式下有效。每一個connection上發起N次calls.若是指定burst-length=B的話那麼,一次發起B個pipelined calls.總共發起N/B個bursts.
  • –num-conns=N RO模式下有效。指定建立多少個連接。對於一個測試來講的話,必須等待到N個connections完成或者是失敗。失敗定義是在鏈接上沒有任何活動超過–timeout或者是–think-timeout.
  • –period=[D]T1[,T2] 各個鏈接或者是session之間建立的時間間隔,包括固定間隔,泊松分佈以及一致分佈。–rate只是–period的簡化用法,做爲固定間隔使用。默認的話是建立連接,除非使用選項–wsess或者是–wsesslog
  • –port=N webserver服務端口,默認是80
  • –print-reply[=[header|body]] 打印響應
  • –print-request[=[header|body]] 打印請求
  • –rate=X 每次建立多少個連接或者是session,內部都是均勻建立的。若是==0的話表示全部鏈接/session都是順序建立(等待前面一個連接/session完成以後才建立下一個). 默認的話是建立連接,除非使用選項–wsess或者是–wsesslog
  • –recv-buffer=N 接收緩衝區大小,默認是16KB
  • –retry-on-failure 失敗的話會繼續重試。這個選項只是下SO工做方式下適用。
  • –send-buffer=N 發送緩衝區大小,默認是4KB
  • –server=S webserver服務地址,默認是localhost
  • –server=name=S 設置header的Host字段,默認是httperf
  • –session-cookie SO模式下有效。請求服務器若是返回cookie的話,那麼這個session上面後續鏈接都會帶上相同的cookie. 若是server下次返回不一樣的cookie的話,那麼原來的cookie會覆蓋而且打印debug信息。也就是說後續請求帶上的第二個cookie而不是第一個cookie.
  • –ssl 使用SSL進行通訊
  • –ssl-ciphers=L SSL相關 todo(dirlt):
  • –ssl-no-reuse SSL相關 todo(dirlt):
  • –think-timeout=X 這個超時時間會疊加到–timeout選項指定的超時時間上。選項主要是爲了解決動態頁面。若是是靜態頁面能夠直接=0.
  • –timeout=X 超時時間,單位是second,能夠爲浮點數。超時時間包括鏈接創建和讀寫時間。若是超時的話那麼關閉鏈接或者是session,client-timeo+=1
  • –uri=S 請求URI.好比–uri=/foreground/all_stars 能夠經過–wset來以這個參數內容爲前綴拼接出uri.
  • -V/–verbose
  • –wlog=B,F 容許從文件中讀取訪問的URI.其中B表示是否重複讀取,F表示要讀取的文件。
  • –wsess=N1,N2,X 是用session模式測試。發起N1個session,一個session內部會調用N2次calls.calls被聚合稱爲burst.各個burst之間等待X秒(這裏稱爲user-think time.仍是很貼切的。這個就比如用戶瀏覽一次頁面以後,看一段時間,而後訪問下一個頁面).經過burst-length來控制一次burst中間存在多少個calls.工做場景是首先發起一個calls,而後將其他calls發送。定義session成功或者是失敗,是要求任意一個calls不能超過超時時間。
  • –wsesslog=N,X,F 相似於–wess選項,可是關於請求以及各類參數能夠從文件讀取。N表示發起多少個session,X表示user-think time.F就是提供其餘內容的文件。這個選項和–wlog很是相似。
  • –wset=N,X 生成URI用來訪問。其中X表示多少機率會訪問新的URI,好比X=0.25的話表示每次生成的URI平均都會訪問4次。N表示生成多少個頁面。假設N=100的話,那麼從[0,100],對於每一個數字補齊到N-1的數字位,好比9就是09.而後根據每一個數字分隔造成0/9.若是URI爲x做爲前綴,那麼就是x/0/9.html.

note(dirlt):不過httperf採用select模型,致使最大鏈接數存在上限。

結果分析

Total: connections 30000 requests 29997 replies 29997 test-duration 299.992 s

Connection rate: 100.0 conn/s (10.0 ms/conn, <=14 concurrent connections)
Connection time [ms]: min 1.4 avg 3.0 max 163.4 median 1.5 stddev 7.3
Connection time [ms]: connect 0.6
Connection length [replies/conn]: 1.000

Request rate: 100.0 req/s (10.0 ms/req)
Request size [B]: 75.0

Reply rate [replies/s]: min 98.8 avg 100.0 max 101.2 stddev 0.3 (60 samples)
Reply time [ms]: response 2.4 transfer 0.0
Reply size [B]: header 242.0 content 1010.0 footer 0.0 (total 1252.0)
Reply status: 1xx=0 2xx=29997 3xx=0 4xx=0 5xx=0

CPU time [s]: user 94.31 system 205.26 (user 31.4% system 68.4% total 99.9%)
Net I/O: 129.6 KB/s (1.1*10^6 bps)

Errors: total 3 client-timo 0 socket-timo 0 connrefused 3 connreset 0
Errors: fd-unavail 0 addrunavail 0 ftab-full 0 other 0

Connection部分

  • connection rate表示發起connection頻率
  • concurrent connections表示併發數最高達到14個鏈接
  • onnection time表示從connection發起到斷開間隔
  • connection connect表示創建鏈接時間
  • connection length表示reply和connection數目比

Request部分

  • rate表示在 單個鏈接上每一個請求 處理速率和時間
  • size表示每一個request平均大小

Reply部分

  • repsonse表示從發出第一個請求字節到接收到第一個響應字節時間
  • transfer表示接收響應的傳輸時間

若是使用session模式的話,那麼結果會有

Session rate [sess/s]: min 0.00 avg 0.59 max 2.40 stddev 0.37 (240/450)
Session: avg 6.45 connections/session
Session lifetime [s]: 123.9
Session failtime [s]: 58.5
Session length histogram: 4 7 4... 3 3 240
  • session rate表示session完成頻率。240/250表示發起250個可是完成240個
  • 表示每一個session包含多少個鏈接
  • lifetime表示平均每一個成功session完成佔用時間
  • failtime表示平均每一個失敗session在失敗以前佔用時間
  • length historagm表示每一個session獲得的reply個數,4個沒有接收到任何reply,7個接受到1個。

1.2.49 ab

ab(apache benchmarking)是apache httpd自帶的一個HTTP Server個的工具。下面是這個工具命令行參數

ab  [ -A auth-username:password ] [ -b windowsize ] [ -c concurrency ] [ -C cookie-name=value ] [ -d ] [ -e
csv-file ] [ -f protocol ] [ -g gnuplot-file ] [ -h ] [ -H custom-header ] [ -i ] [ -k ] [ -n requests ]  [
-p  POST-file  ] [ -P proxy-auth-username:password ] [ -q ] [ -r ] [ -s ] [ -S ] [ -t timelimit ] [ -T con‐
tent-type ] [ -u PUT-file ] [ -v verbosity] [ -V ] [ -w ] [ -x <table>-attributes ] [ -X proxy[:port]  ]  [
-y <tr>-attributes ] [ -z <td>-attributes ] [ -Z ciphersuite ] [http[s]://]hostname[:port]/path

功能上沒有httperf多可是以爲應該大部分時候足夠使用的。

note(dirlt):ab和httperf工做模型不一樣。httperf是指定創建多少個連接,每一個連接上發起多少個calls。而ab指定一共發送多少個請求, 每批發送多少個請求,而後計算每批時間統計。ab必須等待這批請求所有返回或者是失敗或者是超時。能夠做爲對方的互補。nice!!!

下面看看每一個參數的含義:

  • -A auth-username:password 提供Basic Auth選項
  • -b windowsize TCP的讀寫buffer
  • -c concurrency 每次發起請求數目(認爲這是一批請求,必須等待這批請求徹底返回)這個和httperf模型還不太相同。
  • -C cookie-name=value 提供cookie參數
  • -d
  • -e csv-file 提供CSV文件包含處理請求時間在各個級別上的百分比,一般結合gnuplot使用。
  • -f protocol 指定SSL/TSL協議
  • -g gnuplot-file 將測量參數寫成gnuplot格式或者是TSV(Tab separate values)文件能夠導入Gnuplot或者是Excel中
  • -h 幫助信息
  • -H custom-header 提供自定義頭
  • -i 發送HEAD而不是GET請求
  • -k 使用keepalive選項
  • -n requests 總共發起請求數目
  • -p POST-file 指定文件提供POST數據。一般須要設置content-type.
  • -P proxy-auth-username:password 爲proxy提供Basic Auth驗證。
  • -q 安靜模式
  • -r socket處理錯誤不退出
  • -s
  • -S
  • -t timelimit 做爲benchmark整體時間,內部就使用-n 50000.默認沒有超時
  • -T content-type 設置content-type.
  • -u PUT-file 指定文件提供PUT數據。一般須要設置content-type.
  • -v
  • -V
  • -w 將結果輸出成爲HTML文件
  • -x <table>-attributes 設置HTML文件的table屬性
  • -X proxy[:port] 設置proxy服務器
  • -y <tr>-attributes 設置HTML文件的tr屬性
  • -z <td>-attributes 設置HTML文件的td屬性
  • -Z ciphersuite 指定SSL/TLS cipher suite

咱們能夠這樣使用ab -c 100 -n 10000 -r localhost/ 輸出仍是很好理解的。對於最後面百分比時間,注意是包含100個concurrency的結果。

Server Software:        nginx/1.2.1
Server Hostname:        localhost
Server Port:            80

Document Path:          /
Document Length:        1439 bytes

Concurrency Level:      100
Time taken for tests:   0.760 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      16500000 bytes
HTML transferred:       14390000 bytes
Requests per second:    13150.09 [#/sec] (mean)
Time per request:       7.605 [ms] (mean)
Time per request:       0.076 [ms] (mean, across all concurrent requests)
Transfer rate:          21189.11 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   1.4      0      18
Processing:     2    7   1.8      7      20
Waiting:        1    7   1.8      7      20
Total:          5    7   2.0      7      20

Percentage of the requests served within a certain time (ms)
  50%      7
  66%      7
  75%      8
  80%      8
  90%      9
  95%     10
  98%     14
  99%     19
 100%     20 (longest request)

1.2.50 autobench

http://www.xenoclast.org/autobench/

autobench做爲httperf包裝,也提供了分佈式壓力測試的工具。

這裏先介紹一下單機使用狀況。autobench的manpage提供了很是清晰的說明 http://www.xenoclast.org/autobench/man/autobench.html. 能夠看到autobench提供了比較兩個站點的性能。

  • –file 性能測試結果
  • –high_rate 指定最高rate
  • –low_rate 指定最低rate(所謂rate和httperf相同)
  • –rate_step rate的步進
  • –host1
  • –host2
  • –port1
  • –port2
  • –num_conn 總鏈接數
  • –num_call 單個鏈接的調用次數
  • –output_fmt 測試結果格式有TSV和CSV
  • –quiet
  • –single_host 只是測量單個host
  • –timeout
  • –uri1
  • –uri2
  • –version

默認配置文件是~/.autobench.conf,方便常用。經常使用命令方式就是

autobench --single_host --host1=localhost --port1=19880 --low_rate=10 --high_rate=1000 --rate_step=100 --num_conn=10000 --num_call=1 --file=bench.tsv
autobench --host1=www.sina.com --host2=www.sohu.com  --low_rate=10 --high_rate=1000 --rate_step=100 --num_conn=10000 --num_call=1 --file=bench.tsv

獲得tsv文件以後可使用bench2graph轉換成爲png格式。bench2graph須要作一些修改

#echo set terminal postscript color > gnuplot.cmd
echo set terminal png xffffff > gnuplot.cmd

#echo set data style linespoints >> gnuplot.cmd
echo set style data linespoints >> gnuplot.cmd

使用bench2graph bench.tsv bench.png,而後會提示輸入title便可生成比較圖。

todo(dirlt):後續可能須要學習如何使用autobench分佈式測試,由於httperf該死的select模型。

1.3 kernel

 

1.3.1 vmlinuz

vmlinuz是可引導的、壓縮的內核。「vm」表明「Virtual Memory」。Linux 支持虛擬內存,不像老的操做系統好比DOS有640KB內存的限制。Linux可以使用硬盤空間做爲虛擬內存,所以得名「vm」。vmlinuz是可執行的Linux內核,它位於/boot/vmlinuz,它通常是一個軟連接。vmlinux是未壓縮的內核,vmlinuz是vmlinux的壓縮文件。

vmlinuz的創建有兩種方式。一是編譯內核時經過「make zImage」建立,而後經過:「cp /usr/src/linux-2.4/arch/i386/linux/boot/zImage /boot/vmlinuz」產生。zImage適用於小內核的狀況,它的存在是爲了向後的兼容性。二是內核編譯時經過命令make bzImage建立,而後經過:「cp /usr/src/linux-2.4/arch/i386/linux/boot/bzImage /boot/vmlinuz」產生。bzImage是壓縮的內核映像,須要注意,bzImage不是用bzip2壓縮的,bzImage中的bz容易引發誤解,bz表示「big zImage」。 bzImage中的b是「big」意思。

zImage(vmlinuz)和bzImage(vmlinuz)都是用gzip壓縮的。它們不只是一個壓縮文件,並且在這兩個文件的開頭部份內嵌有gzip解壓縮代碼。因此你不能用gunzip 或 gzip –dc解包vmlinuz。內核文件中包含一個微型的gzip用於解壓縮內核並引導它。二者的不一樣之處在於,老的zImage解壓縮內核到低端內存(第一個640K),bzImage解壓縮內核到高端內存(1M以上)。若是內核比較小,那麼能夠採用zImage或bzImage之一,兩種方式引導的系統運行時是相同的。大的內核採用bzImage,不能採用zImage。

1.3.2 tcp IO

http://www.ece.virginia.edu/cheetah/documents/papers/TCPlinux.pdf

note(dirlt):後面影響擁塞部分沒有看


packet reception

./images/tcp-packet-reception.png

整個流程大體以下:

  • linux裏面使用sk_buff數據結構來描述packet.
  • NIC檢測到packet到達,從Kernel Memory(sk_buffs)分配sk_buff數據結構,調用DMA Engine將packet放到sk_buff數據結構裏面 note(dirlt):NIC檢測有packet到達和有packet發送,都不是觸發而是主動poll的方式來完成的
  • 將sk_buff而且加入rx_ring這個ring_buffer裏面。若是rx_ring滿了的話那麼將packet丟棄。
  • 當DMA Engine完成處理以後, NIC經過向CPU發起中斷 通知kernel進行處理。
  • kernel將這個packet傳遞給IP層進行處理。IP層須要將信息組裝成爲ip packet。若是ip packet是tcp的話那麼放到socket backlog裏面。若是socket backlog滿了的話那麼將ip packet丟棄。 copy packet data to ip buffer to form ip packet
  • note(dirlt):這個步驟完成以後IP layer就能夠釋放sk_buffer結構
  • tcp layer從socket backlog裏面取出tcp packet, copy ip packet tcp recv buffer to form tcp packet
  • tcp recv buffer交給application layer處理, copy tcp recv buffer to app buffer to form app packet

其中內核參數有

  • /proc/sys/net/ipv4/tcp_rmem tcp recv buffer大小
  • /proc/sys/net/core/netdev_max_backlog socket backlog大小

packet transmission

./images/tcp-packet-transmission.png

整個流程大體以下:

  • application layer將數據copy到tcp send buffer裏面,若是空間不夠的話那麼就會出現阻塞。 copy app buffer to tcp send buffer as app packet
  • tcp layer等待tcp send buffer存在數據或者是須要作ack的時候,組裝ip packet推送到IP layer copy tcp send buffer to ip send buffer as tcp packet
  • IP layer從kernel memory申請sk_buffer,將ip data包裝成爲packet data,而後塞到qdisc(txqueuelen控制隊列長度)裏面。若是隊列滿的話那麼就會出現阻塞,反饋到tcp layer阻塞。 copy ip send buffer to packet data as ip packet
  • NIC driver若是檢測到qdisc有數據的話,那麼會將packet data從qdisc放置到ring buffer裏面,而後調用NIC DMA Engine將packet發送出去 todo(dirlt):可能理解有誤
  • 發送完成以後NIC向CPU發起中斷釋放packet data內存到Kernel Memory

其中內核參數有:

  • /proc/sys/net/ipv4/tcp_wmem 這個和rmem很是相似
  • note(dirlt):qdisc文章裏面說是txqueuelen控制的,可是彷佛找不到這個參數!

note(dirlt):在wangyx的幫助下這個配置在ifconfig下面找到了

➜  ~  ifconfig
eth0      Link encap:Ethernet  HWaddr 12:31:40:00:49:d1
          inet addr:10.170.78.31  Bcast:10.170.79.255  Mask:255.255.254.0
          inet6 addr: fe80::1031:40ff:fe00:49d1/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:13028359 errors:0 dropped:0 overruns:0 frame:0
          TX packets:9504902 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:2464083770 (2.4 GB)  TX bytes:20165782073 (20.1 GB)
          Interrupt:25

txqueuelen = 1000

1.3.3 tcp congestion control

./images/tcp-congestion-control.png

  • 初始狀態是slow start
  • cwnd(congestion window) 擁塞窗口,表示一次最多發送的數據包多少。
  • ssthresh(slow start threshold) 慢速啓動閾值。
  • MSS(maximum segment size) 最大分節大小,和傳輸網絡的MTU相關。

1.3.4 kernel panic

todo(dirlt):

./images/kernel-panic-not-syncing-attempted-to-kill-init.png

1.4 application

 

1.4.1 返回值問題


首先看下面一段Java程序

/* coding:utf-8
 * Copyright (C) dirlt
 */

public class X{
  public static void main(String[] args) {
    System.exit(1);
  }
}

而後這個Java程序被Python調用,判斷這個打印值

#!/usr/bin/env python
#coding:utf-8
#Copyright (C) dirlt

import os
print os.system('java X')

返回值不爲1而是256,對此解釋是這樣的

a 16-bit number, whose low byte is the signal number that killed the process, and whose high byte is the exit status (if the signal number is zero); the high bit of the low byte is set if a core file was produced.

而後下面這段Python程序,使用echo $?判斷返回值爲0而不是256

#!/usr/bin/env python
#coding:utf-8
#Copyright (C) dirlt

code=256
exit(code)

1.4.2 dp8網卡問題

當時dp8的網絡流量從一個很是大的值變爲很是小的值,檢查/proc/net/netstat,如下幾個統計數值dp8和其餘機器差距較大(相差1-2個數量級):

  • TCPDirectCopyFromPrequeue
  • TCPHPHitsToUser
  • TCPDSACKUndo
  • TCPLossUndo
  • TCPLostRetransmit
  • TCPFastRetrans
  • TCPSlowStartRetrans
  • TCPSackShiftFallback

以後在dmesg上面發現以下線索:

dp@dp8:~$ dmesg | grep eth0
[ 15.635160] eth0: Broadcom NetXtreme II BCM5716 1000Base-T (C0) PCI Express f
[ 15.736389] bnx2: eth0: using MSIX
[ 15.738263] ADDRCONF(NETDEV_UP): eth0: link is not ready
[ 37.848755] bnx2: eth0 NIC Copper Link is Up, 100 Mbps full duplex
[ 37.850623] ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready
[ 1933.934668] bnx2: eth0: using MSIX
[ 1933.936960] ADDRCONF(NETDEV_UP): eth0: link is not ready
[ 1956.130773] bnx2: eth0 NIC Copper Link is Up, 100 Mbps full duplex
[ 1956.132625] ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready
[4804526.542976] bnx2: eth0 NIC Copper Link is Down
[4804552.008858] bnx2: eth0 NIC Copper Link is Up, 100 Mbps full duplex

[4804552.008858] bnx2: eth0 NIC Copper Link is Up, 100 Mbps full duplex  dp8上的網卡速度被識別成100 Mbps了。,可能的緣由以下:

  1. 網線、水晶頭質量太差或老化、水晶頭沒壓好,從而致使網線接觸不良或短路等,能夠從新壓水晶頭或更換網線,建議用質量可靠的六類網線六類水晶頭
  2. 本地鏈接―右鍵―屬性―配置―高級―速度和雙工,這裏設置錯誤,改成自動感應或1000Mbps全雙工便可
  3. 網卡所接的交換機或路由器等硬件設備出現故障,或者這些設備是百兆的(千和百連在一塊兒,千變百向下兼容)
  4. 電磁場干擾有時也會變百兆,因此說網線儘可能別與電線一塊兒穿管(論壇會員tchack友情提供)

咱們的網線都是由 世xx聯 提供的,質量應該不錯,有兩種狀況須要優先排除。

  1. 網線問題(測試方法:換根網線試試)
  2. 交換機dp8鏈接的口壞了(測試方法:把dp8的網線換一個交換機的口)

1.4.3 修改資源限制

臨時的修改方式能夠經過ulimit來進行修改,也能夠經過修改文件/etc/security/limits.conf來永久修改

hadoop - nofile 102400
hadoop - nproc 40960

1.4.4 CPU溫度太高

這個問題是我在Ubuntu PC上面遇到的,明顯的感受就是運行速度變慢。而後在syslog裏面出現以下日誌:

May  2 18:24:21 umeng-ubuntu-pc kernel: [ 1188.717609] CPU1: Core temperature/speed normal
May  2 18:24:21 umeng-ubuntu-pc kernel: [ 1188.717612] CPU0: Package temperature above threshold, cpu clock throttled (total events = 137902)
May  2 18:24:21 umeng-ubuntu-pc kernel: [ 1188.717615] CPU2: Package temperature above threshold, cpu clock throttled (total events = 137902)
May  2 18:24:21 umeng-ubuntu-pc kernel: [ 1188.717619] CPU1: Package temperature above threshold, cpu clock throttled (total events = 137902)
May  2 18:24:21 umeng-ubuntu-pc kernel: [ 1188.717622] CPU3: Package temperature above threshold, cpu clock throttled (total events = 137902)

1.4.5 sync hangup

1.4.6 更換glibc

@2013-05-23 https://docs.google.com/a/umeng.com/document/d/12dzJ3OhVlrEax3yIdz0k08F8tM8DDQva1wdrD3K49PI/edit 懷疑glibc版本存在問題,在dp45上操做可是出現問題。

個人操做順序計劃是這樣的:

  1. 將dp20的glibc copy到本身的目錄下面/home/dp/dirlt/libc-2.11.so
  2. 將dp45的glibc backup. mv /lib64/libc-2.12.so /lib64/libc-2.12.bak.so(補充一點,就是在lib64下面還有軟連接 libc.so.6 -> libc-2.12.so,這個文件應該是被程序查找使用的)
  3. cp /home/dp/dirlt/libc-2.11.so /lib64/libc-2.12.so

可是進行到2以後就發現cp不可用了,而且ls等命令也不可以使用了。緣由很是簡單,就是由於2以後libc.so.6沒有對應的文件了,而cp,ls這些基本的命令依賴於這個動態連接庫。

~ $ ldd /bin/cp
        linux-vdso.so.1 =>  (0x00007fff9717f000)
        libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f5efb804000)
        librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f5efb5fc000)
        libacl.so.1 => /lib/x86_64-linux-gnu/libacl.so.1 (0x00007f5efb3f3000)
        libattr.so.1 => /lib/x86_64-linux-gnu/libattr.so.1 (0x00007f5efb1ee000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f5efae2f000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f5efac2a000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f5efba2d000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f5efaa0d000)

todo(dirlt): what is the correct way to do it(change glibc)


@2013-08-03

A copy of the C library was found in an unexpected directory | Blog : http://blog.i-al.net/2013/03/a-copy-of-the-c-library-was-found-in-an-unexpected-directory/

上面的連接給出了升級glibc的方法

  • sudo su - root # 首先切換到root帳號下面
  • mv libc.so librt.so  /root # 將glibc等相關的so移動到root帳號下面,主要不要移動軟鏈接文件。
  • LD_PRELOAD=/root/libc.so:/root/librt.so bash # 這個時候若是執行bash是找不到glibc等so了,因此須要使用LD_PRELOAD來預先加載
  • apt-get install # 在這個bash下面使用apt-get來安裝和升級glibc.

1.4.7 容許不從tty執行sudo

修改/etc/sudoers文件,註釋掉

Defaults requiretty

1.4.8 ssh proxy

http://serverfault.com/questions/37629/how-do-i-do-multihop-scp-transfers

  • 目的機器是D,端口是16021,用戶是x
  • 跳板機器是T,端口是18021,用戶是y
  • client須要和x@D以及y@T創建信任關係
  • 方法A
    • 從T上和D創建連接而且配置轉發端口p, 全部和T:p的數據交互都會轉發到D:16021
    • 在T上執行 ssh -L "*:5502:D:16021" x@D # 轉發端口是5502
      • -o ServerAliveInterval=60 # 我纔想單位應該是s。這樣每隔60s能夠和server作一些keepalive的通訊,確保長時間沒有數據通訊的狀況下,鏈接不會斷開。
    • ssh -p 5502 x@T 或者 scp -P 5502 <file> x@T:<path-at-D>
  • 方法B
    • scp能夠指定proxyCommand配合D上nc命令完成
    • scp -o ProxyCommand="ssh -p 18021 y@T 'nc D 16021'" <file> x@D:<path-at-D>

Date: 2014-06-17T10:30+0800

Org version 7.9.3f with Emacs version 24

Validate XHTML 1.0
Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a>

Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a>

 

https://blog.csdn.net/chinaclock/article/details/48780625

相關文章
相關標籤/搜索