Capturing Performance of a Command in Linux
We are interested in checking the time taken to complete a command, is there any way to get to know that in linux.
yes, /usr/bin/time will help to do that.
time command
$/usr/bin/time ls
output will be
a.txt b.txt 0.000u 0.004s 0:00.79 0.0% 0+0k 0+0io 0pf+0w
Hope, this result is confusing, please refe man time
.
The default format string is
%Uuser %Ssystem %Eelapsed %PCPU (%Xtext+%Ddata %Mmax)k %Iinputs+%Ooutputs (%Fmajor+%Rminor)pagefaults %Wswaps
When the -p option is given the (portable) output format
real %e user %U sys %S
is used.
Format
we can format the result as we wanted
- %E Elapsed real time (in [hours:]minutes:seconds).
- %e (Not in tcsh.) Elapsed real time (in seconds).
- %S Total number of CPU-seconds that the process spent in kernel mode.
- %U Total number of CPU-seconds that the process spent in user mode.
- %P Percentage of the CPU that this job got, computed as (%U + %S) / %E.
- %M Maximum resident set size of the process during its lifetime, in Kbytes.
- %t (Not in tcsh.) Average resident set size of the process, in Kbytes.
- %K Average total (data+stack+text) memory use of the process, in Kbytes.
- %D Average size of the process\u2019s unshared data area, in Kbytes.
- %p (Not in tcsh.) Average size of the process\u2019s unshared stack space, in Kbytes.
- %X Average size of the process\u2019s shared text space, in Kbytes.
- %Z (Not in tcsh.) System\u2019s page size, in bytes. This is a per-sys-tem constant, but varies between systems.
- %F Number of major page faults that occurred while the process was running. These are faults where the page has to be read in from disk.
- %R Number of minor, or recoverable, page faults. These are faults for pages that are not valid 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.
- %W Number of times the process was swapped out of main memory.
- %I Number of file system inputs by the process.
- %O Number of file system outputs by the process.
- %r Number of socket messages received by the process.
- %s Number of socket messages sent by the process.
- %k Number of signals delivered to the process.
- %C (Not in tcsh.) Name and command line arguments of the command being timed.
- %x (Not in tcsh.) Exit status of the command.
Time
Memory
I/O
Routing result to file
time command has argument option to route the result to a file system
/usr/bin/time -o /tmp/timeresult.txt ls
output will be like below in file /tmp/timeresult.txt
0.00user 0.00system 0:00.00elapsed 0%CPU (0avgtext+0avgdata 3440maxresident)k
0inputs+0outputs (0major+272minor)pagefaults 0swaps
Route only elapsed time to file
/usr/bin/time -o -f /tmp/timeresult.txt "ls command elapsed time: %e" ls
output in file looks like below, elapsed time calculated in seconds
ls command elapsed time: 0.91
time command output not able to capture in crontab
crontab is the default scheduler to start the process in partiular time and number of reoccurencess.
Above mentioned option to route may not work in crontab, or inconsistently work, due to crontab works with separate shell, and/or if we try to gather result from different shell.
sh -> sh -c -> time ls
at command
This command helps to start the command in particular time and day. Unfortunately, results the same as crontab.
sleep command
This command is old and golden days one, which helps to run every scheduled time.
sleep [<umber>suffix
suffix
- s seconds
- m minutes
- h hours
- d days
conclusion : time with sleep
If we give a try using these commands like below
for i in {0..20}
do
/usr/bin/time -a -o /tmp/timeresult.txt -f "ls command elapsed time: %e" ls
sleep 1h
done
Every hour onece, this taks wake up and run. No new shell get started every wake up call.