Linux time Command
The purpose of the Linux time command is to measure the time and system resources required for the execution of a specific command.
For example, CPU time, memory, input/output, etc. It is important to note that some information may not be displayed on Linux. This is because some resource allocation functions on Linux are different from the default methods used by the time command, preventing it from obtaining this data.
Syntax
time [options] COMMAND [arguments]
Parameters:
-o or --output=FILE: Set the result output file. This option writes the time output to the specified file. If the file already exists, its content will be overwritten.
-a or --append: Used with -o, appends the result to the end of the file instead of overwriting the original content.
-f FORMAT or --format=FORMAT: Set the display format using the FORMAT string. If this option is not set, the system default format is used. You can also set this format using the environment variable time, so you don't have to set it every time you log in.
The time command can display four types of resources:
Time resources
Memory resources
IO resources
Command info
Detailed content is as follows:
- Time Resources
E: The time taken to execute the command, in the format: [hour]:minute:second. Note that this number does not represent the actual CPU time.
e: The time taken to execute the command, in seconds. Note that this number does not represent the actual CPU time.
S: The time spent in kernel mode while executing the command, in seconds.
U: The time spent in user mode while executing the command, in seconds.
P: The CPU usage percentage while executing the command. This number is the sum of kernel mode and user mode CPU time divided by the total time.
- Memory Resources
M: The maximum amount of physical memory used while executing, in KB.
t: The average amount of physical memory used while executing, in KB.
K: The average total memory size (stack+data+text) used by the process, in KB.
D: The average size of the unshared data area of the process, in KB.
p: The average size of the unshared stack of the process, in KB.
X: The average size of shared text of the process, in KB.
Z: The size of the system memory page, in bytes. This is a constant for the same system.
- IO Resources
F: The number of major memory page faults that occurred for this process. A major memory page fault occurs when a memory page has been swapped to a swap file and allocated to another process. The content of this page must be read back from the swap file.
R: The number of minor memory page faults that occurred for this process. A minor memory page fault occurs when a memory page has been swapped to a swap file but not yet allocated to another process. The content of this page is not damaged and does not need to be read from the swap file.
W: The number of times this process was swapped to a swap file.
c: The number of times this process was forcibly interrupted (e.g., when the allocated CPU time was exhausted).
w: The number of times this process voluntarily interrupted (e.g., while waiting for an I/O operation to complete, such as disk read).
I: The number of input files for this process.
O: The number of output files for this process.
r: The number of Socket Messages received by this process.
s: The number of Socket Messages sent by this process.
k: The number of signals received by this process.
- Command Info
C: The parameters and command name during execution.
x: The exit status of the command.
-p or --portability: This option automatically sets the display format to:
real %e user %U sys %S: This is to comply with the POSIX specification.
-v or --verbose: This option lists all resources used by the program, not only in general English sentences but also with explanations. This is useful for those who do not want to spend time learning the format settings or are new to this command.
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, the command "time date" is executed (see line 1).
The system first executes the command "date", and line 2 shows the result of the "date" command.
Lines 3-6 show the time statistics for executing the "date" command, where 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 these three times is MMmNN[.FFF]s.
Using the following command:
We can obtain the results of executing `ps -aux` and the system resources it consumed. The data is 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 ```