English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Linux time command

Linux Command大全

The purpose of the Linux time command is to measure the time and system resources required for the execution of specific commands.

For example, CPU time, memory, input/output, etc. It should be noted that some information may not be displayed on Linux. This is because some resource allocation functions in Linux are not the same as the default method of the time command, so the time command cannot obtain this information.

Syntax

time [options] COMMAND [arguments]

parameters:

  • -o or --output=FILE: Sets the output file for the result. This option will write the output of time to the specified file. If the file already exists, the system will overwrite its content.
  • -a or --append:配合 -o Using it, the result will be written to the end of the file without overwriting the original content.
  • -f FORMAT or --format=FORMAT:Set the display method with the FORMAT string. When this option is not set, the system default format will be used. However, you can use the environment variable time to set this format, so that you do not have to set it every time you log in to the system.

time The resources that the command can display include four major items, namely:

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

The detailed content is as follows:

1、Time Resources

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

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

S The time spent in kernel mode (kernel mode) when executing instructions, in seconds.

U The time spent in user mode (user mode) when executing instructions, in seconds.

P The CPU usage ratio when executing instructions. In fact, this number is the CPU time of the core mode plus the user mode divided by the total time.

2、Memory Resources

M The maximum amount of physical memory used during execution. The unit is KB

t The average amount of physical memory used during execution, in KB

K The total memory used by the executing program (stack+data+text) in KB

D The average size of the executing program's own data area (unshared data area), in KB

p The average size of the executing program's own stack (unshared stack), in KB

X The average shared content (shared text) between executing programs, in KB

Z The size of the system memory page, in bytes. This is a constant for the same system

3、IO Resources

F The number of primary memory page faults for this program. A primary memory page fault refers to a memory page that has been swapped to the swap file and has been allocated to another program. At this time, the content of the page must be read out from the swap file again.

R The number of secondary memory page faults for this program. A secondary memory page fault refers to a memory page that has been swapped to the swap file but has not yet been allocated to another program. At this time, the content of the page has not been destroyed and does not need to be read out from the swap file

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

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

w This program voluntarily terminates (as if waiting for some I/O Number of times the program is executed after completion, such as disk reading, etc.

I Number of files input by this program

O Number of files output by this program

r Socket Message received by this program

s Socket Message sent by this program

k Number of signals (Signal) received by this program

4、Command Info

C Execution parameters and command name

x Command's exit code (Exit Status)

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

real %e user %Usys %S: The purpose of this is to be compatible with the POSIX specification.

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

Online Example

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 the1line).

The system first executes the command 'date', the2The line is the execution result of the command 'date'.

The3-6The line is the time statistics result of executing the command 'date', where the4The line 'real' is the actual time, the5The line 'user' is the user CPU time, the6The line 'sys' is the system CPU time.

The display format of the above three types of time is all MMmNN[.FFF]s.

Using the following command

time -v ps -aux

We can get the execution of 'ps -The results of 'aux' and the system resources used are as follows:

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 Command大全