read_real_time, read_wall_time,time_base_to_time or mread_real time Subroutine

Purpose

Read the processor real-time clock or time base registers to obtain high-resolution elapsed time.

Library

Standard C Library (libc.a)

Syntax

#include <sys/time.h>
#include <sys/systemcfg.h>
int read_real_time(timebasestruct_t *t,
                 size_t size_of_timebasestruct_t);
int read_wall_time(timebasestruct_t *t,
                 size_t size_of_timebasestruct_t);
int time_base_to_time(timebasestruct_t *t,
                 size_t size_of_timebasestruct_t);
int mread_real_time(timebasestruct_t *t,
                 size_t size_of_timebasestruct_t);

Description

These subroutines are used for making high-resolution measurement of elapsed time, by using the processor real-time clock or time base registers. The read_real_time subroutine reads the value of the appropriate registers and stores them in a structure. The read_wall_time subroutine returns the monotonically increasing time base value. The time_base_to_time subroutine converts time base data to real time, if necessary. This process is divided into two steps because the process of reading the time is usually part of the timed code. The conversion from time base to real time can be moved out of the timed code.

The read_real_time subroutine reads the time base register. The t argument is a pointer to a timebasestruct_t, where the time values are recorded.

After the system calls the read_real_time subroutine, if it is running on a processor with a real-time clock, t->tb_high and t->tb_low contain the current clock values (seconds and nanoseconds), and t->flag contains the RTC_POWER.

If it is running on a processor with a time base register, t->tb_high and t-tb_low contain the current values of the time base register, and t->flag contains RTC_POWER_PC.

Note: The read_real_time subroutine occasionally provides negative timing results for MPI calls. Use the mread_real_time subroutine to monotonically increase timing values.

The time_base_to_time subroutine converts time base information to real time, if necessary. It is suggested that applications unconditionally call the time_base_to_time subroutine rather than conducting a check to see whether it is necessary.

If t->flag is RTC_POWER, the subroutine returns (the data is already in real-time format).

If t->flag is RTC_POWER_PC, the time base information in t->tb_high and t->tb_low is converted to seconds and nanoseconds; t->tb_high is replaced by the seconds; t->tb_low is replaced by the nanoseconds; and t->flag is changed to RTC_POWER.

Parameters

Item Description
t Points to a timebasestruct_t.

Return Values

The read_real_time subroutine returns RTC_POWER if the contents of the real-time clock are recorded in the timebasestruct, or returns RTC_POWER_PC if the content of the time base registers is recorded in the timebasestruct.

The read_wall_time subroutine always returns RTC_POWER_PC.

The time_base_to_time subroutine returns 0 if the conversion to real time is successful (or not necessary), otherwise -1 is returned.

Examples

This example shows the time that it takes for print_f to print the comment between the begin and end time codes:

#include <stdio.h>
#include <sys/time.h>
 
int
main(void)
{
   timebasestruct_t start, finish;
   int val = 3;
   int secs, n_secs;
 
   /* get the time before the operation begins */
   read_real_time(&start, TIMEBASE_SZ);
 
   /* begin code to be timed */
   (void) printf("This is a sample line %d \n", val);
   /* end code to be timed   */
 
   /* get the time after the operation is complete */
   read_real_time(&finish, TIMEBASE_SZ);
 
   /*
    * Call the conversion routines unconditionally, to ensure
    * that both values are in seconds and nanoseconds regardless
    * of the hardware platform. 
    */
   time_base_to_time(&start, TIMEBASE_SZ);
   time_base_to_time(&finish, TIMEBASE_SZ);
 
   /* subtract the starting time from the ending time */
   secs = finish.tb_high - start.tb_high;
   n_secs = finish.tb_low - start.tb_low;
 
  /*
   * If there was a carry from low-order to high-order during 
   * the measurement, we may have to undo it. 
   */
   if (n_secs < 0)  {
      secs--;
      n_secs += 1000000000;
      }
 
   (void) printf("Sample time was %d seconds %d nanoseconds\n",
                secs, n_secs);
 
   exit(0);
}