Java程序員常用Linux性能分析命令


性能分析

vmstat

虛擬內存統計

用法

Usage:
 vmstat [options] [delay [count]]

Options:
 -a, --active           active/inactive memory
 -f, --forks            number of forks since boot
 -m, --slabs            slabinfo
 -n, --one-header       do not redisplay header
 -s, --stats            event counter statistics
 -d, --disk             disk statistics
 -D, --disk-sum         summarize disk statistics
 -p, --partition <dev>  partition specific statistics
 -S, --unit <char>      define display unit
 -w, --wide             wide output
 -t, --timestamp        show timestamp

 -h, --help     display this help and exit
 -V, --version  output version information and exit

 

示例

[root@localhost ~]# vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0  71256  92720      0 565120    0    0     1   115   22   36  1  0 99  0  0

 

其中

  • r : 運行和等待 CPU 時間片的進程數。若該值大於系統 CPU 個數,則說明 CPU 不足
  • b : 表示等待資源的進程數
  • swpd : 切換到內存交換區的內存大小(單位 KB)
  • free: 空閑的物理內存的大小,我的機器內存總共2G,剩余92M。
  • buff: Linux/Unix系統是用來存儲,目錄里面有什么內容,權限等的緩存
  • cache:直接用來記憶我們打開的文件,給文件做緩沖,把空閑的物理內存的一部分拿來做文件和目錄的緩存,是為了提高 程序執行的性能,當程序使用內存時,buffer/cached會很快地被使用
  • si:每秒從磁盤讀入虛擬內存的大小,如果這個值大於0,表示物理內存不夠用或者內存泄露了,要查找耗內存進程解決掉。
  • so:每秒虛擬內存寫入磁盤的大小,如果這個值大於0,同上。
  • bi:塊設備每秒接收的塊數量(讀磁盤)(kb/s)
  • bo:塊設備每秒發送的塊數量(寫磁盤)(kb/s)
  • in:每秒CPU的中斷次數,包括時間中斷
  • cs:每秒上下文切換次數。
  • us:用戶進程消耗 CPU 時間百分比
  • sy:內核進程消耗 CPU 時間百分比,sy 的值較高時,則說明內核消耗的 CPU 資源很多
  • id:空閑 CPU 時間百分比
  • wt:IO 等待 CPU 時間百分比

free

內存使用狀況

[root@localhost ~]# free
              total        used        free      shared  buff/cache   available
Mem:        1849336     1193336       90344        1752      565656      460000
Swap:       2097148       71248     2025900
[root@localhost ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           1.8G        1.1G         88M        1.7M        552M        449M
Swap:          2.0G         69M        1.9G
[root@localhost ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1805        1165          88           1         552         449
Swap:          2047          69        1978
[root@localhost ~]# free -h 2
              total        used        free      shared  buff/cache   available
Mem:           1.8G        1.1G         88M        1.7M        552M        449M
Swap:          2.0G         69M        1.9G
[root@localhost ~]# free -h -s 2
              total        used        free      shared  buff/cache   available
Mem:           1.8G        1.1G         88M        1.7M        552M        449M
Swap:          2.0G         69M        1.9G

              total        used        free      shared  buff/cache   available
Mem:           1.8G        1.1G         88M        1.7M        552M        449M
Swap:          2.0G         69M        1.9G

              total        used        free      shared  buff/cache   available
Mem:           1.8G        1.1G         88M        1.7M        552M        449M
Swap:          2.0G         69M        1.9G

 

uptime

統計當前系統的運行情況

[root@localhost ~]# uptime
 09:59:55 up 2 days, 7 min,  1 user,  load average: 0.00, 0.01, 0.05

 

load average: 系統 1分鍾,5分鍾,15分鍾的平均負荷

netstat

當前網絡信息

用法

usage: netstat [-vWeenNcCF] [<Af>] -r         netstat {-V|--version|-h|--help}
       netstat [-vWnNcaeol] [<Socket> ...]
       netstat { [-vWeenNac] -I[<Iface>] | [-veenNac] -i | [-cnNe] -M | -s [-6tuw] } [delay]

        -r, --route              display routing table
        -I, --interfaces=<Iface> display interface table for <Iface>
        -i, --interfaces         display interface table
        -g, --groups             display multicast group memberships
        -s, --statistics         display networking statistics (like SNMP)
        -M, --masquerade         display masqueraded connections

        -v, --verbose            be verbose
        -W, --wide               don't truncate IP addresses
        -n, --numeric            don't resolve names
        --numeric-hosts          don't resolve host names
        --numeric-ports          don't resolve port names
        --numeric-users          don't resolve user names
        -N, --symbolic           resolve hardware names
        -e, --extend             display other/more information
        -p, --programs           display PID/Program name for sockets
        -o, --timers             display timers
        -c, --continuous         continuous listing

        -l, --listening          display listening server sockets
        -a, --all                display all sockets (default: connected)
        -F, --fib                display Forwarding Information Base (default)
        -C, --cache              display routing cache instead of FIB
        -Z, --context            display SELinux security context for sockets

  <Socket>={-t|--tcp} {-u|--udp} {-U|--udplite} {-w|--raw} {-x|--unix}
           --ax25 --ipx --netrom
  <AF>=Use '-6|-4' or '-A <af>' or '--<af>'; default: inet
  List of possible address families (which support routing):
    inet (DARPA Internet) inet6 (IPv6) ax25 (AMPR AX.25)
    netrom (AMPR NET/ROM) ipx (Novell IPX) ddp (Appletalk DDP)
    x25 (CCITT X.25)

 

其中

  • -a (all)顯示所有選項,默認不顯示LISTEN相關
  • -t (tcp)僅顯示tcp相關選項
  • -u (udp)僅顯示udp相關選項
  • -n 拒絕顯示別名,能顯示數字的全部轉化成數字
  • -l 僅列出有在 Listen (監聽) 的服務狀態
  • -p 顯示建立相關鏈接的程序名
  • -r 顯示路由信息,路由表
  • -e 顯示擴展信息,例如uid等
  • -s 按各個協議進行統計
  • -c 每隔一個固定時間,執行該netstat命令。

提示:LISTEN和LISTENING的狀態只有用-a或者-l才能看到

top

 監控linux的系統狀況,比如cpu、內存的使用

[root@localhost java_demos]# top
top - 14:36:14 up 2 days,  4:43,  1 user,  load average: 0.00, 0.01, 0.05
Tasks: 110 total,   2 running, 108 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.3 us,  0.2 sy,  0.0 ni, 99.3 id,  0.2 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1849336 total,   963348 free,   542372 used,   343616 buff/cache
KiB Swap:  2097148 total,  2011496 free,    85652 used.  1126908 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 1858 mysql     20   0 1465504 200800   6212 S   1.0 10.9  34:56.46 mysqld
    1 root      20   0   56636   4308   2808 S   0.0  0.2   0:02.05 systemd
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.04 kthreadd
    3 root      20   0       0      0      0 S   0.0  0.0   0:01.53 ksoftirqd/0
    5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H
    7 root      rt   0       0      0      0 S   0.0  0.0   0:00.07 migration/0
    8 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_bh
    9 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcuob/0
   10 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcuob/1
   11 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcuob/2
   12 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcuob/3
   13 root      20   0       0      0      0 S   0.0  0.0   0:14.63 rcu_sched
   14 root      20   0       0      0      0 S   0.0  0.0   0:10.43 rcuos/0
   15 root      20   0       0      0      0 S   0.0  0.0   0:04.67 rcuos/1

 

其中

  • 第一行:
    • 14:36:14 當前系統時間
    • days, 4:43 系統已經運行了2天4小時43分鍾(在這期間沒有重啟過)
    • 1 users 當前有1個用戶登錄系統
    • load average: 0.00, 0.01, 0.05 ,后面的三個數分別是1分鍾、5分鍾、15分鍾的負載情況。load average數據是每隔5秒鍾檢查一次活躍的進程數,然后按特定算法計算出的數值。如果這個數除以邏輯CPU的數量,結果高於5的時候就表明系統在超負荷運轉了。
  • 第二行:Tasks 任務(進程),
    • 系統現在共有10個進程,其中處於運行中的有2個,108個在休眠(sleep),stoped狀態的有0個,zombie狀態(僵屍)的有0個。
  • 第三行:cpu狀態
    • us 用戶空間占用CPU的百分比
    • sy 內核空間占用CPU的百分比
    • ni 改變過優先級的進程占用CPU的百分比
    • id 空閑CPU百分比
    • wa IO等待占用CPU的百分比
    • hi 硬中斷(Hardware IRQ)占用CPU的百分比
    • si 軟中斷(Software Interrupts)占用CPU的百分比
  • 第四行:內存狀態
    • total 物理內存總量
    • free 空閑內存總量
    • used 使用中的內存總量
    • buff/cache 緩存的內存量
  • 第五行:swap交換分區
    • total 交換區總量
    • free 空閑交換區總量
    • used 使用的交換區總量
    • avail Mem 緩沖的交換區總量
  • 第六行:各進程(任務)的狀態監控
    • PID 進程id
    • USER 進程所有者
    • PR 進程優先級
    • NI nice值。負值表示高優先級,正值表示低優先級
    • VIRT 進程使用的虛擬內存總量,單位kb。VIRT=SWAP+RES
    • RES 進程使用的、未被換出的物理內存大小,單位kb。RES=CODE+DATA
    • SHR 共享內存大小,單位kb
    • S 進程狀態。D=不可中斷的睡眠狀態 R=運行 S=睡眠 T=跟蹤/停止 Z=僵屍進程
    • %CPU 上次更新到現在的CPU時間占用百分比
    • %MEM 進程使用的物理內存百分比
    • TIME+ 進程使用的CPU時間總計,單位1/100秒
    • COMMAND 進程名稱(命令名/命令行)

監控java線程數:

ps -eLf	grep java wc -l

 

監控網絡客戶連接數:

netstat -n	grep tcp	grep 偵聽端口	wc -l

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM