2016-11-10 41 views
2

在Linux中有perf命令-linux實用程序來訪問硬件性能監視計數器,它使用perf_events內核子系統工作。使用linux perf實用程序每秒鐘報告計數器,如vmstat

perf本身基本上具有兩種模式:perf record/perf top記錄採樣輪廓(該樣品是例如每第十萬CPU時鐘週期或執行的命令),以及perf stat模式來報告週期/執行的命令的總計數爲應用程序(或整個系統)。

是否有的perf模式打印系統範圍或每CPU摘要上總計數每秒(每3,5,10秒),就像它是在vmstat和SYSTAT家族工具(iostatmpstat印刷, sar -n DEV ...就像在http://techblog.netflix.com/2015/11/linux-performance-analysis-in-60s.html中列出的那樣)?例如,對於週期和指令計數器,我會在系統(或每個CPU)的每一秒獲得平均IPC。

是否有任何非perf工具(https://perf.wiki.kernel.org/index.php/Tutorialhttp://www.brendangregg.com/perf.html)可以通過perf_events內核子系統得到這樣的統計信息?那麼系統範圍內的每個進程IPC計算的分辨率爲幾秒?

回答

1

還有就是-I Nperf stat選項 「間隔印刷」,其中N是毫秒間隔做間隔計數器打印每隔N毫秒(N> = 10):http://man7.org/linux/man-pages/man1/perf-stat.1.html

-I msecs, --interval-print msecs 
     Print count deltas every N milliseconds (minimum: 10ms) The 
     overhead percentage could be high in some cases, for instance 
     with small, sub 100ms intervals. Use with caution. example: perf 
     stat -I 1000 -e cycles -a sleep 5 

    For best results it is usually a good idea to use it with interval 
    mode like -I 1000, as the bottleneck of workloads can change often. 

還有以機器可讀形式導入結果,並使用-I第一個字段爲日期時間:

使用-x,perf stat能夠輸出not-q uite,CSV格式輸出......在第二分數可選微秒時間戳(以-I XXX)

vmstat,SYSTAT家庭工具iostatmpstat等定期打印PERF的統計(每秒)的-I 1000 ,例如系統範圍內(添加-A以分開cpu計數器):

perf stat -a -I 1000 

該選項在builtin-stat中實現。Çhttp://lxr.free-electrons.com/source/tools/perf/builtin-stat.c?v=4.8__run_perf_stat功能

531 static int __run_perf_stat(int argc, const char **argv) 
532 { 
533   int interval = stat_config.interval; 

對於perf stat -I 1000一些程序參數(forks=1),例如perf stat -I 1000 sleep 10有間隔環(ts是毫秒間隔轉換爲struct timespec):

639     enable_counters(); 
641     if (interval) { 
642       while (!waitpid(child_pid, &status, WNOHANG)) { 
643         nanosleep(&ts, NULL); 
644         process_interval(); 
645       } 
646     } 
666   disable_counters(); 

對於系統的變型 - 寬硬件性能監視器計數和forks=0有其他間隔環

658     enable_counters(); 
659     while (!done) { 
660       nanosleep(&ts, NULL); 
661       if (interval) 
662         process_interval(); 
663     } 
666   disable_counters(); 

process_interval()http://lxr.free-electrons.com/source/tools/perf/builtin-stat.c?v=4.8#L347從同一個文件使用read_counters();它遍歷事件列表中,並read_counter()loops over所有已知線程,所有的CPU,並開始實際的閱讀功能:

306   for (thread = 0; thread < nthreads; thread++) { 
307     for (cpu = 0; cpu < ncpus; cpu++) { 
... 
310       count = perf_counts(counter->counts, cpu, thread); 
311       if (perf_evsel__read(counter, cpu, thread, count)) 
312         return -1; 

perf_evsel__read是真正的計數器讀出程序在仍在運行:

1207 int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread, 
1208      struct perf_counts_values *count) 
1209 { 
1210   memset(count, 0, sizeof(*count)); 
1211 
1212   if (FD(evsel, cpu, thread) < 0) 
1213     return -EINVAL; 
1214 
1215   if (readn(FD(evsel, cpu, thread), count, sizeof(*count)) < 0) 
1216     return -errno; 
1217 
1218   return 0; 
1219 }