有沒有像時間這樣的命令,但用於記憶體使用?

有沒有像時間這樣的命令,但用於記憶體使用?

是否有類似 的命令time,但可以報告更多統計資料?如果我能做這樣的事情那就太好了:

$ statistics some_command
time:
    real    0m3.002s
    user    0m0.000s
    sys     0m0.000s
memory:
    min     41K
    peak    2.5M
    mean    1.1M
. . .

如果能更進一步,那就太好了。現在,為了調試,我要么專注地盯著top(實際上glances),要么在我的程式碼中散佈語句。

如果我可以向某些東西傳遞命令,那就太好了。

編輯

我可能已經找到了一個解決方案:perf在軟體包中linux-toolslinux-tools-commonUbuntu 12.04 上。

$ perf stat ./someprocess
Performance counter stats for './someprocess':

      12007.384578 task-clock                #    0.996 CPUs utilized          
             1,092 context-switches          #    0.000 M/sec                  
                16 CPU-migrations            #    0.000 M/sec                  
           295,102 page-faults               #    0.025 M/sec                  
    40,553,682,299 cycles                    #    3.377 GHz                     [83.33%]
    18,400,458,723 stalled-cycles-frontend   #   45.37% frontend cycles idle    [83.35%]
     8,356,832,355 stalled-cycles-backend    #   20.61% backend  cycles idle    [66.64%]
    56,930,684,595 instructions              #    1.40  insns per cycle        
                                             #    0.32  stalled cycles per insn [83.34%]
     9,083,443,825 branches                  #  756.488 M/sec                   [83.35%]
         3,431,737 branch-misses             #    0.04% of all branches         [83.33%]

      12.051963969 seconds time elapsed

有幫助的頁面。

答案1

zshtime有比has更強大的內建命令bash,並且該zsh版本可以報告記憶體統計資料。

即使您不經常將其用作zsh日常 shell,也可以在需要收集此類統計資料時運行它。

設定TIMEFMT環境變數以指示您想要的輸出。這是我的.zshrc文件中的內容(可能有點太花哨,但我喜歡它):

if [[ `uname` == Darwin ]]; then
    MAX_MEMORY_UNITS=KB
else
    MAX_MEMORY_UNITS=MB
fi

TIMEFMT='%J   %U  user %S system %P cpu %*E total'$'\n'\
'avg shared (code):         %X KB'$'\n'\
'avg unshared (data/stack): %D KB'$'\n'\
'total (sum):               %K KB'$'\n'\
'max memory:                %M '$MAX_MEMORY_UNITS''$'\n'\
'page faults from disk:     %F'$'\n'\
'other page faults:         %R'

(一個複雜的細節:在 Linux 上,最大記憶體以兆位元組為單位;在 macOS 上以千位元組為單位。要取得 的值%M,zsh 調用getrusage(),然後使用ru_maxrss / 1024。但在 Linux 上ru_maxrss以千位元組為單位,在 Mac 上以位元組為單位。請在兩個平台上查看man getrusage

範例輸出:

% time ls
[... the output of ls, followed by:]
ls -G   0.00s  user 0.00s system 91% cpu 0.004 total
avg shared (code):         0 KB
avg unshared (data/stack): 0 KB
total (sum):               0 KB
max memory:                3 MB
page faults from disk:     0
other page faults:         337

答案2

GNU時間可以比 Bash 內建的版本報告更多的資訊;使用command time而不僅僅是time調用它,並查看手冊頁或資訊以獲取詳細資訊。

答案3

根據 Richard 的回答,您可以建立一個別名來使用 GNU 時間並提供平均和最大記憶體資訊:

alias time="$(which time) -f '\t%E real,\t%U user,\t%S sys,\t%K amem,\t%M mmem'"

或調整您的環境:

export TIME='\t%E real,\t%U user,\t%S sys,\t%K amem,\t%M mmem'

但請注意,這僅適用/usr/bin/time於預設情況下通常不調用的情況。

從手冊頁:

K 進程的平均總記憶體使用量(資料+堆疊+文字),以千位元組為單位。

M 進程在其生命週期內的最大駐留集合大小(以千位元組為單位)。

答案4

您可以使用/usr/bin/time與 不同的time

使用它-v,您可能會得到您想要的東西,而無需任何額外的安裝。

例子:

$ /usr/bin/time -v cat xxx.txt > /dev/null
    Command being timed: "cat xxx.txt"
    User time (seconds): 0.00
    System time (seconds): 0.00
    Percent of CPU this job got: 100%
    Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.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): 2024
    Average resident set size (kbytes): 0
    Major (requiring I/O) page faults: 0
    Minor (reclaiming a frame) page faults: 121
    Voluntary context switches: 0
    Involuntary context switches: 0
    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

相關內容