Академический Документы
Профессиональный Документы
Культура Документы
Home
Free eBook
Start Here
Contact
About
Like
Tw eet
16
There are times when you might want to profile your program on parameters like:
Time taken by program in user mode
Time taken by program in kernel mode
Average memory usage by the program
etc
On Linux we have a utility time that is designed specifically for this purpose. The utility time takes a program name as
an input and displays information about the resources used by the program. Also, if the command exists with non-zero
status, this utility displays a warning message and exit status.
The syntax of time is :
/usr/bin/time [options] program [arguments]
In the above syntax, options refer to set of optional flag/values that can be passed to time utility to set or unset a
particular functionality. The following are the available time command options:
-v, verbose : This option is passed when a detailed description of the output is required.
quite : This option prevents the time utility to report the status of the program.
-f, format : This option lets the user to control the format of output of the time utility.
-p, portability : This option sets the following output format to make the output in conformance with POSIX
real %e
http://www.thegeekstuff.com/2012/01/time-command-examples/
1/8
2/8/2014
user %U
sys %S
-o FILE, output=FILE : This option lets the user to redirect the output of time utility to a file. This option lets
the time utility to overwrite the file FILE.
-a, append : This option lets the time utility to append the information to file FILE rather than overwriting it.
When the time command is run, following is the kind of output it gives :
# /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, apart from executing the command, the last two lines of the output are the resource information
that time command outputs.
Note: In the above example, the command time was run without any options. So this is a default output generated by
the time command, which is not formatted properly.
As we can see from the output, the default format of the output generated is :
2/8
2/8/2014
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 that in the above example, we tried to change the output format by using a different output format.
Resources
Since we discussed above that time utility displays information about the resource usage by a program, In this section
lets list the resources that can be tracked by this utility and the corresponding specifiers.
From the man page :
C Name and command line arguments of the command being timed.
D Average size of the processs unshared data area, in Kilobytes.
E Elapsed real (wall clock) time used by the process, in [hours:]minutes:seconds.
F Number of major, or I/O-requiring, page faults that occurred while the process was running. These are faults
where the page has actually migrated out of primary memory.
I Number of file system inputs by the process.
K - Average total (data+stack+text) memory use of the process, in Kilobytes.
M - Maximum resident set size of the process during its lifetime, in Kilobytes.
O - Number of file system outputs by the process.
P - Percentage of the CPU that this job got. This is just user + system times divided by the total running time. It
also prints a percentage sign.
R - Number of minor, or recoverable, page faults. These are pages that are not valid (so they fault) 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.
S - Total number of CPU-seconds used by the system on behalf of the process (in kernel mode), in seconds.
U - Total number of CPU-seconds that the process used directly (in user mode), in seconds.
W - Number of times the process was swapped out of main memory.
X - Average amount of shared text in the process, in Kilobytes.
Z - Systems page size, in bytes. This is a per-system constant, but varies between systems.
c - Number of times the process was context-switched involuntarily (because the time slice expired).
e - Elapsed real (wall clock) time used by the process, in seconds.
k - Number of signals delivered to the process.
p - Average unshared stack size of the process, in Kilobytes.
r - Number of socket messages received by the process.
s - Number of socket messages sent by the process.
t - Average resident set size of the process, in Kilobytes.
w - Number of times that the program was context-switched voluntarily, for instance while waiting for an I/O
operation to complete.
x - Exit status of the command.
So we can see that there is a long list of resources whose usage can be tracked by the time utility.
3/8
2/8/2014
0m0.255s
0m0.230s
0m0.030s
As seen from the output above, the time command when used without the complete path (/usr/bin/time) spits out an
error regarding the -f flag. Also the format of output is neither the one specified by us in the command nor the default
format we discussed earlier. This lead to a confusion over how this output got generated.
When time command is executed without the complete path (/usr/bin/time), then its the built-in time command of the
bash shell that is executed.
Use man time to view the man page of /usr/bin/time
Use help time to view the information about the bash time built-in.
12
Tw eet
16
Like
Linux provides several powerful administrative tools and utilities which will help you to
manage your systems effectively. If you dont know what these tools are and how to use them, you could be spending
lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand
system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!
http://www.thegeekstuff.com/2012/01/time-command-examples/
4/8
2/8/2014
Previous post: How to Setup Rsyslog Remote Logging on Linux (Central Log Server)
Next post: C Pointer to Pointer, Pointer to Functions, Array of Pointers Explained with Examples
http://www.thegeekstuff.com/2012/01/time-command-examples/
5/8
2/8/2014
COURSE
Linux Sysadmin CentOS 6 Course - Master the Tools, Configure it Right, and be Lazy
EBOOKS
Linux 101 Hacks 2nd Edition eBook - Practical Examples to Build a Strong Foundation in Linux
Bash 101 Hacks eBook - Take Control of Your Bash Command Line and Shell Scripting
Sed and Awk 101 Hacks eBook - Enhance Your UNIX / Linux Life with Sed and Awk
Vim 101 Hacks eBook - Practical Examples for Becoming Fast and Productive in Vim Editor
Nagios Core 3 eBook - Monitor Everything, Be Proactive, and Sleep Well
POPULAR POSTS
12 Amazing and Essential Linux Books To Enrich Your Brain and Library
50 UNIX / Linux Sysadmin Tutorials
50 Most Frequently Used UNIX / Linux Commands (With Examples)
How To Be Productive and Get Things Done Using GTD
30 Things To Do When you are Bored and have a Computer
Linux Directory Structure (File System Structure) Explained with Examples
Linux Crontab: 15 Awesome Cron Job Examples
Get a Grip on the Grep! 15 Practical Grep Command Examples
Unix LS Command: 15 Practical Examples
15 Examples To Master Linux Command Line History
Top 10 Open Source Bug Tracking System
Vi and Vim Macro Tutorial: How To Record and Play
Mommy, I found it! -- 15 Practical Linux Find Command Examples
15 Awesome Gmail Tips and Tricks
15 Awesome Google Search Tips and Tricks
RAID 0, RAID 1, RAID 5, RAID 10 Explained with Diagrams
http://www.thegeekstuff.com/2012/01/time-command-examples/
6/8
2/8/2014
CATEGORIES
Linux Tutorials
Vim Editor
Sed Scripting
Awk Scripting
Bash Shell Scripting
Nagios Monitoring
OpenSSH
IPTables Firewall
Apache Web Server
MySQL Database
Perl Programming
Google Tutorials
Ubuntu Tutorials
PostgreSQL DB
Hello World Examples
C Programming
C++ Programming
DELL Server Tutorials
Oracle Database
VMware Tutorials
Ramesh Natarajan
Follow
http://www.thegeekstuff.com/2012/01/time-command-examples/
7/8
2/8/2014
Support Us
Support this blog by purchasing one of my ebooks.
Bash 101 Hacks eBook
Sed and Awk 101 Hacks eBook
Vim 101 Hacks eBook
Nagios Core 3 eBook
Contact Us
Email Me : Use this Contact Form to get in touch me with your comments, questions or suggestions about this
site. You can also simply drop me a line to say hello!.
Follow us on Google+
Follow us on Twitter
Become a fan on Facebook
Copyright 20082014 Ramesh Natarajan. All rights reserved | Terms of Service
http://www.thegeekstuff.com/2012/01/time-command-examples/
8/8