【Summary】 Sometimes you may want to analyze your program based on the following parameters: the time spent by the program in user mode, the time spent by the program in kernel mode, the average memory usage of the program, etc. On Linux, we have a utility specifically designed for this purpose called “time”. The “time” utility takes the program name as input and displays information about the resources used by the program. Additionally, if the command exits with a non-zero status, this utility will display a warning message and the exit status. The syntax for “time” is: /usr/bin/time [options] …
Sometimes you may want to analyze your program based on the following parameters:
-
The time spent by the program in user mode
-
The time spent by the program in kernel mode
-
The average memory usage of the program
-
etc.
On Linux, we have a utility specifically designed for this purpose called “time”. The “time” utility takes the program name as input and displays information about the resources used by the program. Additionally, if the command exits with a non-zero status, this utility will display a warning message and the exit status.The syntax for “time” is:
/usr/bin/time [options] program [arguments]
In the above syntax, “options” refers to a set of optional flags/values that can be passed to the “time” utility to set or unset specific features. Here are the available options for the time command:
-
-v, –verbose: Pass this option when a detailed description of the output is required.
-
–quiet: This option prevents the “time” utility from reporting the status of the program.
-
-f, –format: This option allows the user to control the output format of the “time” utility.
-
-p, –portability: This option sets the output format to comply with POSIX.
real %e
user %U
sys %S
-
-o FILE, –output=FILE: This option allows the user to redirect the output of the “time” utility to a file. This option allows the “time” utility to overwrite the file FILE.
-
-a, –append: This option allows the “time” utility to append information to the file FILE instead of overwriting it.
When running the “time” command, the output type it provides is as follows:
# /usr/bin/time ls
anaconda-ks.cfg bin install.log install.log.syslog mbox
0.00user 0.00system 0:00.00elapsed 0%CPU (0avgtext+0avgdata 3888maxresident)k
0inputs+0outputs (0major+304minor)pagefaults 0swaps
As we can see above, in addition to executing the command, the last two lines of the output are the resource information output by the ‘time’ command.
Note: In the example above, the “time” command was run without any options. So this is the default output generated by the “time” command, which is not formatted correctly.
From the output, we can see that the default format of the generated output is:
%Uuser %Ssystem %Eelapsed %PCPU (%Xtext+%Ddata %Mmax)k
%Iinputs+%Ooutputs (%Fmajor+%Rminor)pagefaults %Wswaps
Format Options
This option allows the user to decide the output generated by the “time” command. In the previous section, we discussed the default format used in the output. In this section, we will learn how to specify a custom format.
Format strings typically consist of “resource specifiers” scattered throughout plain text. A percent sign (`%’) in the format string causes the following character to be interpreted as a resource specifier.
A backslash (`\’) introduces a “backslash escape”, converting it to a single printable character during output. `\t’ outputs a tab, `\n’ outputs a newline, and `\\’ outputs a backslash. A backslash followed by any other character will output a question mark (`?’) followed by a backslash, indicating an invalid backslash escape was given.
Other text in the format string is copied verbatim to the output. The time command always prints a newline after printing resource usage information, so format strings typically do not end with a newline (or `0`).
For example:
$ /usr/bin/time -f "\t%U user,\t%S system,\t%x status" date
Sun Jan 22 17:46:58 IST 2012
0.00 user,0.00 system,0 status
So we see in the example above that we attempted to change the output format by using a different output format.
Resources
Since we discussed above that the “time” utility displays information about the program’s resource usage, in this section, we list the resources that this utility can track and their corresponding specifiers.
From the man page:
-
C – The name and command line arguments of the command being timed.
-
D – The average size of the process’s non-shared data area, in kilobytes.
-
E – The actual (wall clock) time used by the process, in [hours:] minutes: seconds.
-
F – The number of major or I/O page faults that occurred during the process’s runtime. These are faults where pages were actually migrated out of main memory.
-
I – The number of file system inputs for the process.
-
K – The average total (data + stack + text) memory usage of the process, in kilobytes.
-
M – The maximum resident set size of the process during its lifetime, in kilobytes.
-
O – The number of file system outputs for the process.
-
P – The percentage of CPU time that the job received. This is just user + system time divided by total run time. It also prints a percent sign.
-
R – The number of minor or recoverable page faults. These pages are invalid (thus they fault) but have not yet been claimed by other virtual pages. Therefore, the data in the pages is still valid but must update the system tables.
-
S – The total number of CPU seconds used by the process (in kernel mode).
-
U – The total number of CPU seconds used directly by the process (in user mode).
-
W – The number of times the process was swapped out of main memory.
-
X – The average number of shared text in the process, in kilobytes.
-
Z – The page size of the system, in bytes. This is a constant for each system but varies from system to system.
-
c – The number of involuntary context switches of the process (because the time slice expired).
-
e – The actual (wall clock) time used by the process, in seconds.
-
k – The number of signals sent to the process.
-
p – The average non-shared stack size of the process, in kilobytes.
-
r – The number of socket messages received by the process.
-
s – The number of socket messages sent by the process.
-
t – The average resident set size of the process, in kilobytes.
-
w – The number of voluntary context switches of the program, such as when waiting for I/O operations to complete.
-
x – The exit status of the command.
So we can see there is a long list of resources that can be tracked for usage by the “time” utility.
Why /usr/bin/time? (And not just time)
Let’s not use /usr/bin/time but rather just “time”.
$ time -f "\t%U user,\t%S system,\t%x status" date
-f: command not found
real0m0.255s
user0m0.230s
sys0m0.030s
From the output above, it can be seen that using the “time” command without the full path (/usr/bin/time) results in an error regarding the “-f” flag. Additionally, the output format is neither the format we specified in the command nor the default format we discussed earlier. This leads to confusion about how this output was generated.
If the “time” command is executed without the full path (/usr/bin/time), it executes the built-in “time” command of the bash shell.
-
Use ‘man time’ to view the man page for /usr/bin/time
-
Use ‘help time’ to view information about the built-in bash time.
Link: https://bbs.huaweicloud.com/blogs/355148
(Copyright belongs to the original author, please delete if infringed)
WeChat group
WeChat group
To facilitate better communication on operation and maintenance and related technical issues, a WeChat group has been created. Friends who need to join the group can scan the QR code below to add me as a friend (note: add group).
Blog
Guest
Blog
CSDN Blog: https://blog.csdn.net/qq_25599925
Juejin Blog: https://juejin.cn/user/4262187909781751
Long press to recognize the QR code to visit the blog website and see more high-quality original content.