LINUX TIPS AND TRICKS --- September 01, 2000

Published by ITworld.com, the IT problem-solving network
http://www.itworld.com/newsletters

*********************************************************************

High-Resolution Timers
by Danny Kalev

The ANSI time measurement functions time() and clock() are suitable for 
most purposes; however, multimedia apps, real time environments and 
hardware interfaces usually need more fine-grained time measurement. 
This week, I will focus on POSIX time measuring APIs that provide time 
resolution of a microsecond (one millionth of second) and even 
nanosecond (one billionth of a second).

A timer's accuracy and resolution depends on the underlying hardware's 
clock. Alpha and SPARC architectures usually provide high-resolution 
timers. Intel-based PCs can also provide time resolution of a 
microsecond. Other architectures, however, offer a very coarse time 
resolution of 100hz. Therefore, even if the APIs presented here are 
supported on your machine, check whether they're actually capable of 
measuring small time units accurately.

The first system call is gettimeofday(), which has the following
prototype:

    #include  sys/time.h>
    #include  unistd.h>
    int gettimeofday(struct timeval * val, struct timezone *zone);

Where the timeval and timezone structs are defined as follows: 

    struct timeval
    {
     int tv_sec; /*seconds*/
     int tv_usec /*microseconds*/
    };

    struct timezone
    {
     int tz_minuteswest; /*minutes west of Greenwich*/
     int tz_dsttime; /*type of Daylight Saving Time correction*/
    };
    
Note:  The use of struct timezone is obsolete. The tz_dsttime field is 
not supported by libc or glibc and there's no plan to do so in the 
future. Hence, we always pass a NULL pointer instead of a timezone 
pointer. Our interest is in the timeval struct. The first field of 
timeval holds the number of seconds that have elapsed since the current 
day started while the second field contains the number of trailing 
microseconds. By calling gettimeofday() before and after a certain 
operation, you can calculate the time that this operation took in a 
time-resolution of a microsecond. For example:

    int main()
    { 
     struct timeval tv1, tv2; 
     gettimeofday(&tv1, NULL); 
     int * p = (int *) malloc(sizeof(int));
     gettimeofday(&tv2, NULL); 
     
     printf("took %d seconds and %d microseconds\n", 
    tv1.tv_sec-tv2.tv_sec, tv1.tv_usec-tv2.tv_usec);
    }

The sys/time.h header defines three macros for operating on timeval
structs:

    timerclear(tv) - Clears a timeval struct
    
    timerisset(tv) - Checks whether tv has been filled (i.e., returns   
    true if either elements is non-zero)
    
    timercmp(tv1, tv2, operator) - Compares two timeval structs. This 
    macro takes two structs and an operator, e.g., >, , etc., and 
    returns the result accordingly.

The POSIX.1b real-time processing standard supports even higher 
resolution than a microsecond. The struct timespec uses nanoseconds:

    struct timespec
    {
     time_t tv_sec; /*seconds*/
     long int tv_nsec; /*nanoseconds*/
    };

For complete documentation and a list of functions that operate on 
timespec, see 
http://www.interlog.com/~calex/software/libgpl/ts_util.html

Resources

Communicating the POSIX way 
Implementing POSIX IPC interfaces into the Solaris kernel
http://www.sunworld.com/sunworldonline/swol-12-1999/swol-12-insidesolaris.html

Threads libraries in Solaris 
New features for developing multi-threaded code
http://www.sunworld.com/sunworldonline/swol-06-2000/swol-06-insidesolaris.html

************************************************************************

About the author
----------------
Danny Kalev is a system analyst and software engineer with more
than 10 years of experience, specializing in C++ and
object-oriented analysis and design on various platforms including
VMS, DOS, Windows, Unix, and Linux. His technical interests involve
code optimization, networking, and distributed computing. He is
also a member of the ANSI C++ standardization committee and the
author of ANSI/ISO C++ Professional Programmer's Handbook (Que,
1999). Contact him at linuxnl@excite.com.
 
*********************************************************************

CUSTOMER SERVICE

You can subscribe or unsubscribe to any of your e-mail newsletters by 
updating your form at: 
http://www.itworld.com/cgi-bin/w3-msql/newsletters/subcontent12.html?

For subscription changes that cannot be handled via the web, please send 
an email to our customer service dept: support@itworld.com

*********************************************************************

CONTACTS

* For editorial comments, write Andrew Santosusso, Associate Editor, 
Newsletters at: andrew_santosusso@itworld.com
* For advertising information, write Dan Chupka, Account Executive at:
dan_chupka@itworld.com
* For recruitment advertising information, write Jamie Swartz, Eastern
Regional Sales Manager at: jamie_swartz@itworld.com or Paul Duthie,
Western Regional Sales Manager at: paul_duthie@itworld.com
* For all other inquiries, write Jodie Naze, Product Manager,
Newsletters at: jodie_naze@itworld.com

*********************************************************************

Copyright 2000 ITworld.com, Inc., All Rights Reserved. 

http://www.itworld.com
