Linux Time Command Tutorial With Examples To Get Programs Resource Usage – POFTUT

Linux Time Command Tutorial With Examples To Get Programs Resource Usage


Linux time command provides commands and programs usage statistics especially as CPU load. Time command gets real command as parameter like below.

  • Time taken in user mode
  • Time taken in kernel mode
  • Time taken in real mode
  • Memory usage

Syntax

We will use following syntax for time command.

time [options] [command]

Run

If we run the time command without any option  we will see following output. In this example we want to run command mkdir test in order to create a new directory named test.

$ time mkdir test
Run
Run

We get performance of the mkdir command.

Time Command Path

There is more than one time program in most of the Linux distributions. So running time may call other than we want. So while using time command we will specify and provide the full path like below.

/usr/bin/time  date
Time Command Path
Time Command Path

Format Output

Simply issuing time command provides very simple output. Time command supports a lot of metrics where they can be used with format option with-f . In the example we will list user, system load and status information.

$ /usr/bin/time -f "\t%U user,\t%S system,\t%x status" date
Format Output
Format Output

Format Options

Here we will list available formatting options of the time command.

  • %      A literal `%’.
  • C      Name and command line arguments of the command being timed.
  • D      Average size of the process’s unshared data area, in Kilobytes.
  • E      Elapsed real (wall clock) time used by the process, in [hours:]minutes:seconds.
  • F      Number of major, or I/O-requiring, page faults that occurred while the process was running. These are faults where the page has actually migrated out of primary memory.
  •  I      Number of file system inputs by the process.
  • 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.
  • O      Number of file system outputs by the process.
  • P      Percentage of the CPU that this job got.  This is just user + system times divided by the total running time.  It also prints a percentage sign.
  • R      Number of minor, or recoverable, page faults.  These are pages that are not valid (so they fault) but which have not yet been claimed by other virtual pages.  Thus the data in the page is still valid but the system tables must be updated.
  • S      Total number of CPU-seconds used by the system on behalf of the process (in kernel mode), in seconds.
  • U      Total number of CPU-seconds that the process used directly (in user mode), in seconds.
  • W      Number of times the process was swapped out of main memory.
  • X      Average amount of shared text in the process, in Kilobytes.
  • Z      System’s page size, in bytes.  This is a per-system constant, but varies between systems.
  • c      Number of times the process was context-switched involuntarily (because the time slice expired).
  • e      Elapsed real (wall clock) time used by the process, in seconds.
  • k      Number of signals delivered to the process.
  • p      Average unshared stack size of the process, in Kilobytes.
  • r      Number of socket messages received by the process.
  • s      Number of socket messages sent by the process.
  • t      Average resident set size of the process, in Kilobytes.
  • w      Number of times that the program was context-switched voluntarily, for instance while waiting for an I/O operation to complete.
  • x      Exit status of the command.
LEARN MORE  Virt Install Tool For Virtualization With KVM and Qemu For Linux

Write Output To A File

By default the output of the time command is redirected to the standard output. The output can be redirected to a file with -o option. In the example we redirect the output to the file named time_output.txt

$ /usr/bin/time -o time_output.txt date

More Details With Verbose Option

More detailed information about program execution can be printed with the verbose option. We will enable verbose mode with -v option.

$ /usr/bin/time -v date
More Details With Verbose Option
More Details With Verbose Option

 

With the verbose output following information and stats will be listed;

  • user time
  • system time
  • elapsed time
  • exit status

Leave a Comment