70

Is there any command like time, but that reports more statistics? It would be great if I could do something like:

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

If it could go even further, that would be great. Right now, for debugging, I either end up staring intently at top (actually glances), or sprinkling statements all through my code.

If there was something that I could pass a command to, that would be fantastic.

EDIT

I might have found a solution: perf in the package linux-tools and linux-tools-common on 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

(The page that helped.)

3
  • 7
    There is no memory statistics in your perf results.
    – BatchyX
    Sep 28, 2012 at 20:25
  • "like time but for memory" doesn't really make sense. What exactly do you want to know? Memory is not a measurement. Sep 29, 2012 at 10:34
  • 1
    Are you looking to audit an application that you're making? If so, in what language?
    – dset0x
    Oct 17, 2012 at 12:11

4 Answers 4

50

zsh has a more powerful built-in time command than bash has, and the zsh version can report memory statistics.

Even if you don't regularly use zsh as your day-to-day shell, you can just run it when you need to gather these kinds of statistics.

Set the TIMEFMT environment variable to indicate the output you want. Here is what I have in my .zshrc file (perhaps a bit too fancy, but I like it):

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'

(A complicated detail: On Linux, max memory is megabytes; on macOS it's in kilobytes. To get the value for %M, zsh calls getrusage(), and then uses ru_maxrss / 1024. but on Linux, ru_maxrss is in kilobytes, and on Mac it's in bytes. See man getrusage on both platforms.)

Sample output:

% 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
6
  • Your answer helped me A LOT but I have a question: Is 'max memory' in MB? I believe it's KB or even B.
    – Andres
    Oct 26, 2015 at 14:29
  • Glad it helped, @Andres. In my tests , 'max memory' is in MB, but you can test it yourself by compiling gist.github.com/mmorearty/fa34c0f29abe454fd14b and running it in zsh with e.g. time malloc-bytes 10000000. That will malloc 10 megabytes, so try it and then see what zsh reports. Oct 26, 2015 at 17:09
  • According to the zsh docs %M is the maximum memory in megabytes.
    – gerrard00
    Jul 20, 2016 at 17:44
  • 2
    %M reports in kilobytes Mar 26, 2019 at 7:58
  • 2
    The Zsh docs were wrong about the unit of %M until 2018: zsh.org/mla/workers/2018/msg00170.html
    – Miles
    Jul 1, 2020 at 6:18
29

GNU time can report a bit more information than the version built into Bash; use command time rather than just time to invoke it, and see the man page or info for details.

1
  • 5
    Or call /usr/bin/time -v ./my_command.sh
    – ostrokach
    Jun 14, 2017 at 11:43
8

Based on Richard's answer, you can create an alias to use GNU time and provide average and maximum memory information:

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

or adjust your environment:

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

But be aware that this only works for /usr/bin/time which is often not called by default.

From the man page:

K Average total (data+stack+text) memory use of the process, in Kilobytes.

M Maximum resident set size of the process during its lifetime, in Kilobytes.

1

You can use /usr/bin/time which is different from time.

Use it with -v and you may have what you want without any additional install.

Example:

$ /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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .