實用Linux命令(二)

上接 實用Linux命令(一)python

AICODE.CC

查看Linux的版本(Red Hat/Cent OS)

在RedHat和Cent OS下,使用以下命令查看當前系統的版本。linux

$ cat /etc/centos-release 
CentOS release 6.3 (Final)

time命令: 統計程序執行時間

用於統計程序執行時間,這些事件包含程序從被調用到終止的時間,用戶CPU時間,系統CPU時間。segmentfault

$ time ls
bakup                PDO-1.0.3.tgz     rinetd.tar.gz     yaf-2.2.9.tgz
channel.xml          package2.xml      PDO_MYSQL-1.0.2      xhprof-0.9.4      zendopcache-7.0.3
go-pear.phar         package.xml       PDO_MYSQL-1.0.2.tgz  xhprof-0.9.4.tgz  zendopcache-7.0.3.tgz
PDO-1.0.3            rinetd            yaf-2.2.9

real    0m0.002s
user    0m0.000s
sys 0m0.001s

tee命令

tee命令用於將標準輸入拷貝到標準輸出。centos

$ echo "hello,world"|tee -a test.txt

上述命令將hello,world字符串輸出到test.txt文件中,-a 默認狀況下,tee命令會使用>覆蓋輸出到文件,使用-a屬性,會使用>>追加方式網絡

netstat命令

查看端口占用狀況app

# netstat -apn
  • -a(--all) 顯示全部的socket信息(包括監聽和未監聽)
  • -p(--program) 顯示每一個socket所屬於的進程名稱和PID
  • -n(--numeric) 顯示數字形式的地址而不是符號化的主機名、端口或者用戶名

perf命令

perf命令是隨Linux內核代碼一同發佈和維護的性能診斷工具,由內核社區負責維護和發展。Perf不只能夠用於應用程序性能統計分析,也能夠應用於內核代碼的的性能統計和分析。框架

在Cent OS系統上,若是沒有該命令的話,可使用yum進行安裝。frontend

# yum install perf

perf命令很是強大,詳細介紹的話篇幅比較長,能夠閱讀這篇文章 Perf -- Linux下的系統性能調優工具python2.7

用法: perf [--version] [--help] COMMAND [ARGS]

 最經常使用的perf命令:
   annotate        讀取perf.data (使用perf record建立)文件而且顯示標註的代碼
   archive         Create archive with object files with build-ids found in perf.data file
   bench           進行基準測試的框架工具集
   buildid-cache   Manage build-id cache.
   buildid-list    List the buildids in a perf.data file
   diff            Read perf.data files and display the differential profile
   evlist          List the event names in a perf.data file
   inject          Filter to augment the events stream with additional information
   kmem            Tool to trace/measure kernel memory(slab) properties
   kvm             Tool to trace/measure kvm guest os
   list            列出全部事件類型的符號
   lock            分析鎖事件
   mem             分析對內存的訪問
   record          運行一個命令而且記錄它的分析結果到perf.data文件中
   report          讀取perf.data文件而且顯示分析結果
   sched           Tool to trace/measure scheduler properties (latencies)
   script          Read perf.data (created by perf record) and display trace output
   stat            運行一個命令而且收集性能計數統計信息
   test            運行可用性測試
   timechart       Tool to visualize total system behavior during a workload
   top             系統分析工具.
   trace           受strace啓發建立的工具
   probe           定義一個新的動態跟蹤點

 See 'perf help COMMAND' for more information on a specific command.
perf stat

perf stat經過歸納精簡的方式提供被調試程序運行的總體狀況和彙總數據。socket

建立以下C程序test.c

#include <stdio.h>

int main()
{
    int i = 1;
    while (1) {
        if (i == 100000) break;
        i ++;
    }
    return 0;
}

編譯gcc test.c -o test

$ perf stat ./test

 Performance counter stats for './test':

          0.837322 task-clock                #    0.747 CPUs utilized          
                 1 context-switches          #    0.001 M/sec                  
                 0 CPU-migrations            #    0.000 M/sec                  
                98 page-faults               #    0.117 M/sec                  
           269,259 cycles                    #    0.322 GHz                     [90.39%]
           897,270 stalled-cycles-frontend   #  333.24% frontend cycles idle   
           226,746 stalled-cycles-backend    #   84.21% backend  cycles idle   
           764,602 instructions              #    2.84  insns per cycle        
                                             #    1.17  stalled cycles per insn
           267,843 branches                  #  319.881 M/sec                  
             3,467 branch-misses             #    1.29% of all branches         [80.37%]

       0.001121130 seconds time elapsed

第一個task-clock是CPU利用率,該值比較高,說明該程序屬於CPU密集型。第二個context-switches是進程上下文切換次數,頻繁的切換次數應該是要避免的。

perf top

用於實時顯示當前系統的性能統計信息。該命令主要用來觀察整個系統當前的狀態,好比能夠經過查看該命令的輸出來查看當前系統最耗時的內核函數或某個用戶進程。

執行該命令須要root權限。

使用方法以下

$ sudo perf top

程序會與top命令相似,動態輸出如下內容

Samples: 1K of event 'cpu-clock', Event count (approx.): 8071695
 39.60%  [kernel]             [k] __do_softirq
 13.46%  [kernel]             [k] _raw_spin_unlock_irqrestore
  9.37%  [kernel]             [k] VbglGRPerform
  8.47%  [kernel]             [k] e1000_xmit_frame
  6.01%  [kernel]             [k] finish_task_switch
  5.82%  [kernel]             [k] e1000_clean
  5.15%  [kernel]             [k] native_read_tsc
  4.75%  [kernel]             [k] kmem_cache_free
  1.32%  [kernel]             [k] tick_nohz_idle_enter
  1.28%  libc-2.17.so         [.] __strstr_sse2
  1.22%  libc-2.17.so         [.] __memset_sse2
  0.82%  libc-2.17.so         [.] __GI___strcmp_ssse3
  0.42%  libpython2.7.so.1.0  [.] 0x000000000007e7c6
  0.42%  libc-2.17.so         [.] __strchrnul
  0.39%  [kernel]             [k] e1000_alloc_rx_buffers
  0.38%  libz.so.1.2.7        [.] 0x0000000000002d76
  0.24%  [kernel]             [k] tick_nohz_idle_exit
  0.21%  [kernel]             [k] kfree
perf report/record

使用 top 和 stat 以後,您可能已經大體有數了。要進一步分析,便須要一些粒度更細的信息。好比說您已經判定目標程序計算量較大,也許是由於有些代碼寫的不夠精簡。那麼面對長長的代碼文件,究竟哪幾行代碼須要進一步修改呢?這便須要使用 perf record 記錄單個函數級別的統計信息,並使用 perf report 來顯示統計結果。

建立新的C程序test3,代碼以下

#include <stdio.h>

void test();

int main()
{
  test();
  return 0;
}

void test()
{
  long i;
  for (i = 0; i < 10000000; i ++) {

  }
  puts("finished");
}

編譯後,執行以下命令

$ perf record ./test3
$ perf report

輸出如下內容

Samples: 68  of event 'cpu-clock', Event count (approx.): 17000000
 97.06%  test3  test3              [.] test
  1.47%  test3  [kernel.kallsyms]  [k] __do_softirq
  1.47%  test3  [kernel.kallsyms]  [k] queue_work_on

從中能夠看到,大部分時間都消耗在了test函數中。

perf record命令增長-g參數能夠記錄函數的調用圖信息。更多詳情參考: Perf -- Linux下的系統性能調優工具

lsof命令: 列出打開的文件

工具lsof是一個能夠列出操做系統打開的文件的工具,在Linux系統中,任何事物都是以文件的形式存在,經過文件不只能夠訪問常規文件,還能夠訪問網絡鏈接和硬件設備。

在終端下直接輸入lsof命令,會列出當前系統打開的全部文件,由於它須要列出核心內存和各類文件,因此必須使用root用戶運行才能顯示詳細的信息。

COMMAND     PID      USER   FD      TYPE             DEVICE  SIZE/OFF       NODE NAME
init          1      root  cwd       DIR              253,0      4096          2 /
init          1      root  rtd       DIR              253,0      4096          2 /
init          1      root  txt       REG              253,0    150352      10973 /sbin/init
init          1      root  mem       REG              253,0     65928     264638 /lib64/libnss_files-2.12.so
init          1      root  mem       REG              253,0   1922112     265339 /lib64/libc-2.12.so
init          1      root  mem       REG              253,0     93224     277540 /lib64/libgcc_s-4.4.6-20120305.so.1
init          1      root  mem       REG              253,0     47064     267086 /lib64/librt-2.12.so
...

這裏的COMMAND是進程名稱,PID,USER分別指的是進程的ID和進程全部者,FD是文件描述符,TYPE是文件類型,DEVICE是磁盤名稱,SIZE是文件大小,NODE是索引節點(文件在磁盤上的標識),NAME是打開文件的確切名稱。

對於FD的值,cwd表示當前工做目錄,Lnn表示類庫引用,mem表示內存映射文件,rtd表示根目錄,pd表示父目錄,txt表示進程的數據和代碼。

經常使用參數及說明
  • lsof filename 顯示打開指定文件的全部進程
  • lsof -a 表示兩個參數都必須知足時才顯示結果
  • lsof -c string 顯示COMMAND列中包含指定字符的進程全部打開的文件
  • lsof -u username 顯示所屬user進程打開的文件
  • lsof -g gid 顯示歸屬gid的進程狀況
  • lsof +d /DIR/ 顯示目錄下被進程打開的文件
  • lsof +D /DIR/ 同上,可是會搜索目錄下的全部目錄,時間相對較長
  • lsof -d FD 顯示指定文件描述符的進程
  • lsof -n 不將IP轉換爲hostname,缺省是不加上-n參數
  • lsof -i 用以顯示符合條件的進程狀況
  • lsof -p PID 選擇指定PID
  • lsof -i[46] [protocol][@hostname|hostaddr][:service|port]
    46: IPv4 or IPv6
    protocol: TCP or UDP
    hostname: Internet host name
    hostaddr: IPv4地址
    service: /etc/service中的 service name (能夠不僅一個)
    port: 端口號 (能夠不僅一個)

參考: 百度文庫

相關文章
相關標籤/搜索