19

I can pretty quickly monitor the running time of a process with time:

x@y ~ $ time foo

real        0m14.299s
user        0m4.770s
sys         0m0.440s

Is there a way I can get the same data for I/O and CPU usage of an argument, recorded to STDOUT? A simple command or utility like time would be ideal, where I just pass the argument of the thing I want to run:

x@y ~ $ stats foo

wallclock runtime     0m14.299s
I/O reads             290,420 KB
I/O writes            239,429 KB
peak CPU usage        18.62%
mean CPU usage        1.44%
# etc.
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
John Feminella
  • 557
  • 4
  • 10

2 Answers2

25

look at the time man page on your system, some implementations have format options to include I/O, CPU and Memory stats (-f).

For instance, GNU time, with -v will show all available info (here on Linux):

/usr/bin/time -v ls

Command being timed: "ls"
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: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): 3664
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 273
Voluntary context switches: 2
Involuntary context switches: 2
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

On BSDs, use -l instead.

Note that this is the actual /usr/bin/time program, not the keyword that some shells like bash provide which you invoke with time pipeline.

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
MaQleod
  • 2,594
  • 4
  • 21
  • 18
  • 4
    `zsh`'s `time` keyword can also be configured (with `$TIMEFMT`) to provide with that information. – Stéphane Chazelas Mar 19 '14 at 21:02
  • 1
    This is exactly what I wanted. Perfect! Also, thank you to the editors of this post who clarified that you'll need to run it with `command time -v ...` since `bash` is hijacking `time`. – John Feminella Mar 19 '14 at 23:19
4

The command strace can be useful, you can limit the trace to only count -c or a subset of system calls,

-e trace=set
      Trace  only  the  specified set of system calls.  The -c option is
      useful for determining which  system  calls  might  be  useful  to
      trace.   For  example,  trace=open,close,read,write  means to only
      trace those four system calls.  Be careful when making  inferences
      about  the  user/kernel  boundary if only a subset of system calls
      are being monitored.  The default is trace=all.

In this answer, I use it to count the number of system calls. I don't know if anything will give you a summary breakdown of throughput, however strace can produce the figures for something like awk to summarise.

X Tian
  • 10,413
  • 2
  • 33
  • 48
  • `strace` can slow things down significantly, so it's useful to measure relative times of different syscalls, but I would not use it as a general purpose timer. `perf` is far faster/less intrusive, if you play with sampling frequency options. – Marcin Jan 11 '19 at 22:20