のようなコマンド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-tools
とlinux-tools-common
Ubuntu 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
zsh
には、よりも強力な組み込みtime
コマンドがありbash
、このzsh
バージョンではメモリ統計を報告できます。
zsh
日常的なシェルとして定期的に使用しない場合でも、このような統計情報を収集する必要があるときに実行することができます。
必要な出力を示す環境変数を設定します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
、詳細についてはマニュアル ページまたは info を参照してください。
答え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
ただし、これはデフォルトでは呼び出されない場合にのみ機能することに注意してください。
man ページから:
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