Linux time command

Linux 命令大全Linux Command Reference

The purpose of the Linux time command is to measure the time consumed by executing a specific command, as well as system resources and other information.

For example, CPU time, memory, input/output, and so on. It is important to note that some information cannot be displayed on Linux. This is because the allocation functions for some resources on Linux differ from the default method used by the time command, so the time command cannot obtain this data.

Syntax

time [options] COMMAND [arguments]

Parameters:

  • -o or --output=FILE: Set the result output file. This option writes the output of time to the specified file. If the file already exists, the system will overwrite its contents.
  • -a or --append: Used together with -o, it appends the result to the end of the file without overwriting the original contents.
  • -f FORMAT or --format=FORMAT: Set the display method using the FORMAT string. When this option is not set, the system default format is used. However, you can set this format using the environment variable time, so you don't have to set it every time you log into the system.

There are four major categories of resources that the time command can display::

  • Time resources
  • Memory resources
  • IO resources
  • Command info

The detailed contents are as follows:

1、Time Resources

E The time spent executing the command, in the format: [hour]:minute:second. Please note that this number does not represent actual CPU time.

e The time spent executing the command, in seconds. Please note that this number does not represent actual CPU time.

S The time spent in kernel mode while the command is executing, in seconds.

U The time spent in user mode while the command is executing, in seconds.

P The CPU usage percentage while executing the command. In fact, this number is the CPU time of kernel mode plus user mode divided by the total time.

2、Memory Resources

M The maximum amount of physical memory occupied during execution. Unit is KB.

t The average amount of physical memory occupied during execution. Unit is KB.

K The average size of the total memory (stack+data+text) occupied by the executing program. Unit is KB.

D The average size of the program's unshared data area. Unit is KB.

p The average size of the program's unshared stack. Unit is KB.

X The average size of shared content (shared text) between executing programs. Unit is KB.

Z The size of the system memory page. Unit is byte. This is a constant for the same system.

3、IO Resources

F The number of major page faults of this program. A major page fault means that a memory page has been swapped to the swap file and has already been allocated to another program. At this time, the contents of that page must be read back from the swap file.

R The number of minor page faults of this program. A minor page fault means that a memory page has been swapped to the swap file but has not yet been allocated to another program. At this time, the contents of that page are not corrupted and do not need to be read from the swap file.

W The number of times this program was swapped to the swap file.

c The number of times this program was forcibly interrupted (such as when the allocated CPU time is exhausted).

w The number of times this program voluntarily interrupted (such as when waiting for an I/O operation to complete, like disk reads, etc.).

I The number of files input by this program.

O The number of files output by this program.

r The number of Socket Messages received by this program.

s The number of Socket Messages sent by this program.

k The number of signals received by this program.

4、Command Info

C The command name and arguments when executing.

x The exit status of the command.

-p or --portability: This option automatically sets the display format to:

real %e user %U sys %S: The purpose is to be compatible with the POSIX specification.

-v or --verbose: This option lists all the resources used by the program, not only in plain English sentences but also with explanations. It is quite useful for those who do not want to spend time familiarizing themselves with format settings, or for those who are just starting to use this command.

Examples

1. # time date
2. Sun Mar 26 22:45:34 GMT-8 2006
3. 
4. real    0m0.136s
5. user    0m0.010s
6. sys     0m0.070s
7. #

In the above example, execute the command "time date" (see line 1).

The system first executes the command "date", and line 2 is the execution result of the command "date".

Lines 3-6 are the time statistics of executing the command "date". Among them, line 4 "real" is the actual time, line 5 "user" is the user CPU time, and line 6 "sys" is the system CPU time.

The display format for all three times above is MMmNN[.FFF]s.

Use the following command

time -v ps -aux

We can obtain the results of executing ps -aux and the system resources consumed. As listed below:

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.4 1096 472 ? S Apr19 0:04 init
root 2 0.0 0.0 0 0 ? SW Apr19 0:00 [kflushd]
root 3 0.0 0.0 0 0 ? SW Apr19 0:00 [kpiod]
......
root 24269 0.0 1.0 2692 996 pts/3 R 12:16 0:00 ps -aux
Command being timed: "ps -aux"
User time (seconds): 0.05
System time (seconds): 0.06
Percent of CPU this job got: 68%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.16
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): 0
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 238
Minor (reclaiming a frame) page faults: 46
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

Linux 命令大全Linux Command Reference

Other Extensions