Rhel

執行了這麼長時間的程序之後的執行時間和資源

  • October 15, 2013

這可能是一個基本問題,但我很難找到答案。我正在使用 RHEL6。在執行任何需要大量 cpu 時間的程序後,我會得到一條自動告訴(我猜)執行時間和 IO 使用情況的行。內容如下:

77410.101u 124.968s 1:42:43.49 657.9%    0+0k 0+1353384io 0pf+0w

我有以下問題:

  1. 如何解釋此消息中的每個欄位?我可以猜測一些時間,IO 使用情況,可能是 CPU 使用情況……但我不確定。
  2. 什麼實際列印這條線?是殼嗎?是終端模擬器嗎?是否有一個正在執行的守護程序來處理這個問題?此功能/服務的名稱是什麼/無論它是什麼。
  3. 是否可以控制此消息?喜歡設置列印的cpu使用門檻值嗎?
  4. 我可以添加額外的資訊嗎?比如程序的絕對路徑、記憶體使用峰值、磁碟使用峰值等等……

調試正在發生的事情的提示

假設您使用的是 Bash,我建議您打開 shell 的調試工具。

$ set -x

當您執行產生此輸出的命令時,此輸出將向您展示幕後發生的事情。

輸出

該輸出來自/usr/bin/time您執行的每個命令的前綴 time 命令。為了獲得該輸出,我猜您使用的是 C-shell (csh) 或 Turbo C-shell (tcsh)。

例子

$ tcsh
$ time sleep 2
0.000u 0.000s 0:02.00 0.0%  0+0k 0+0io 0pf+0w

我懷疑這是一個tcshshell 的原因是,當我/usr/bin/time在 Bash shell 中執行命令時,輸出如下所示:

$ /usr/bin/time sleep 2
0.00user 0.00system 0:02.02elapsed 0%CPU (0avgtext+0avgdata 580maxresident)k
0inputs+0outputs (0major+180minor)pagefaults 0swaps

可以使用-for--format開關控制輸出,因此您看到的輸出也可以在 Bash 中實現,但必須有意完成。

輸出的含義

如果您/usr/bin/time在詳細模式下執行命令, ( -v) 您將獲得每個欄位的所有詳細資訊,如下所示:

$ /usr/bin/time -v sleep 2
   Command being timed: "sleep 2"
   User time (seconds): 0.00
   System time (seconds): 0.00
   Percent of CPU this job got: 0%
   Elapsed (wall clock) time (h:mm:ss or m:ss): 0:02.00
   Average shared text size (kbytes): 0
   Average unshared data size (kbytes): 0
   Average stack size (kbytes): 0
   Average total size (kbytes): 0
   Maximum resident set size (kbytes): 584
   Average resident set size (kbytes): 0
   Major (requiring I/O) page faults: 0
   Minor (reclaiming a frame) page faults: 184
   Voluntary context switches: 2
   Involuntary context switches: 4
   Swaps: 0
   File system inputs: 0
   File system outputs: 0
   Socket messages sent: 0
   Socket messages received: 0
   Signals delivered: 0
   Page size (bytes): 4096
   Exit status: 0

如果您排列原始輸出:

77410.101u 124.968s 1:42:43.49 657.9%    0+0k 0+1353384io 0pf+0w
^^^^^^^^^^ ^^^^^^^^ ^^^^^^^^^^ ^^^^^^    ^^^^ ^^^^^^^^^^^ ^^^^^^
    1        2         3       4         5        6        7
  1. 使用者時間(秒)
  2. 系統時間(秒)
  3. 經過(掛鐘)時間(h:mm:ss 或 m:ss)
  4. 此作業獲得的 CPU 百分比
  5. 平均共享文本大小 (kbytes) + 平均非共享數據大小 (kbytes)
  6. 程序的文件系統輸入數 + 程序的文件系統輸出數
  7. 程序執行時發生的主要頁面錯誤數。這些是必須從磁碟讀取頁面的錯誤 + 程序從主記憶體換出的次數

您可以手動執行相同的格式,如下所示:

$ /usr/bin/time -f '%Uu %Ss %E %P %X+%Dk %I+%Oio %Fpf+%Ww' sleep 2
0.00u 0.00s 0:02.00 0% 0+0k 0+0io 0pf+0w

自定義輸出

一旦您能夠確定呼叫的/usr/bin/time位置,您就可以通過在time. 此輸出中可以包含許多選項。

$ man time

摘抄

  Time
  %E     Elapsed real time (in [hours:]minutes:seconds).
  %e     (Not in tcsh.) Elapsed real time (in seconds).
  %S     Total number of CPU-seconds that the process spent in kernel mode.
  %U     Total number of CPU-seconds that the process spent in user mode.
  %P     Percentage of the CPU that this job got, computed as (%U + %S) / %E.

  Memory
  %M     Maximum resident set size of the process during its lifetime, in Kbytes.
  %t     (Not in tcsh.) Average resident set size of the process, in Kbytes.
  %K     Average total (data+stack+text) memory use of the process, in Kbytes.
  %D     Average size of the process's unshared data area, in Kbytes.
  %p     (Not in tcsh.) Average size of the process's unshared stack space, in Kbytes.
  %X     Average size of the process's shared text space, in Kbytes.
  %Z     (Not in tcsh.) System's page size, in bytes.  This is a per-system constant, but varies between systems.
  %F     Number  of major page faults that occurred while the process was running.  These are faults where the page has to be read
         in from disk.
  %R     Number of minor, or recoverable, page faults.  These are faults for pages that are not valid but which have not yet  been
         claimed by other virtual pages.  Thus the data in the page is still valid but the system tables must be updated.
  %W     Number of times the process was swapped out of main memory.
  %c     Number of times the process was context-switched involuntarily (because the time slice expired).
  %w     Number of waits: times that the program was context-switched voluntarily, for instance while waiting for an I/O operation
         to complete.

  I/O
  %I     Number of file system inputs by the process.
  %O     Number of file system outputs by the process.
  %r     Number of socket messages received by the process.
  %s     Number of socket messages sent by the process.
  %k     Number of signals delivered to the process.
  %C     (Not in tcsh.) Name and command-line arguments of the command being timed.
  %x     (Not in tcsh.) Exit status of the command.

有關更多詳細資訊,請參見手冊頁。

編輯#1:你的問題

原來您關於自動顯示輸出的問題是由 csh/tcsh 中此環境變數的設置引起的。

從 tcsh 手冊頁

   The time shell variable can be set to execute the time builtin command 
     after the completion of any process that takes more  than a given number 
     of CPU seconds.

例子

將時間設置為 5 秒。

$ set time=5

確認:

$ set|grep time
time    5

測試一下:

$ bash -c "while [ 1 ];do echo hi; done"
hi
hi
...
...waited ~5 seconds, then Ctrl-C to stop it

5.650u 1.471s 0:09.68 73.5% 0+0k 0+0io 0pf+0w

僅當您正在執行的任務消耗的 CPU 時間的秒數超過變數設置的秒數時,才會顯示輸出$time

參考

引用自:https://unix.stackexchange.com/questions/95937