Pet*_*ter 49 memory linux time profiling
有没有类似的命令time,但它会报告更多的统计信息?如果我能做这样的事情,那就太好了:
$ statistics some_command
time:
real 0m3.002s
user 0m0.000s
sys 0m0.000s
memory:
min 41K
peak 2.5M
mean 1.1M
. . .
Run Code Online (Sandbox Code Playgroud)
如果能走得更远,那就太好了。现在,为了调试,我要么全神贯注地盯着top(实际上glances),要么在我的代码中散布语句。
如果有什么我可以传递命令的东西,那就太棒了。
编辑
我可能已经找到了一个解决方案:perf在包linux-tools和linux-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
Run Code Online (Sandbox Code Playgroud)
(帮助的页面。)
Mik*_*rty 37
zsh有time比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'
Run Code Online (Sandbox Code Playgroud)
(一个复杂的细节:在 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
Run Code Online (Sandbox Code Playgroud)
根据理查德的回答,您可以创建一个别名来使用 GNU 时间并提供平均和最大内存信息:
alias time="$(which time) -f '\t%E real,\t%U user,\t%S sys,\t%K amem,\t%M mmem'"
Run Code Online (Sandbox Code Playgroud)
或调整您的环境:
export TIME='\t%E real,\t%U user,\t%S sys,\t%K amem,\t%M mmem'
Run Code Online (Sandbox Code Playgroud)
但请注意,这仅适用于/usr/bin/time默认情况下通常不会调用的情况。
从手册页:
K 进程的平均总(数据+堆栈+文本)内存使用量,以千字节为单位。
M 进程在其生命周期内的最大驻留集大小,以千字节为单位。
| 归档时间: |
|
| 查看次数: |
17752 次 |
| 最近记录: |