NASD Programmer's Documentation
Time

Representing time

The basic representation of time in NASD is the nasd_timespec_t. This is a structure with two 32-bit fields, representing seconds and nanoseconds.
typedef struct {
  nasd_int32  ts_sec;
  nasd_int32  ts_nsec;
} nasd_timespec_t;


Clock

To read the current time, use nasd_gettime(), which takes as its sole argument a pointer to a nasd_timespec_t. This function assigns to that timespec the current time since the epoch of midnight January 1, 1970 Greenwich Mean Time (GMT). Calls to nasd_gettime() always return unique timestamps.


Manipulating nasd_timespec_t structures

There are several useful macros defined in <nasd/nasd_general.h>. These may prove useful in simplifying code that deals with nasd_timespec_t structures. They are:
MacroMeaning
NASD_NSEC_PER_SEC The number of nanoseconds in a second
NASD_TIMESPEC_VALID(ts) Evaluates nonzero if and only if ts is valid. The seconds field must be non-negative, and the nanosecond field must be non-negative and less than NASD_NSEC_PER_SEC.
NASD_TIMESPEC_VALID_NZ(ts) Evaluates nonzero if the timespec is valid (NASD_TIMESPEC_VALID(ts)) and is non-zero
NASD_TIMESPEC_EQ(ts1, ts2) ts1 == ts2
NASD_TIMESPEC_GT(ts1, ts2) ts1 > ts2
NASD_TIMESPEC_LT(ts1, ts2) ts1 < ts2
NASD_TIMESPEC_GE(ts1, ts2) ts1 >= ts2
NASD_TIMESPEC_LE(ts1, ts2) ts1 <= ts2
NASD_TIMESPEC_ADD(result, addend) result += addend
NASD_TIMESPEC_SUB(result, subtrahend) result -= subtrahend
NASD_TIMESPEC_ZERO(ts) ts := 0
NASD_TIMESPEC_EQ_ZERO(ts) ts == 0


Idle time

Some platforms provide facilities for determining the amount of time the processor has spent idle. On these platforms, NASD_IDLE_SUPPORT will be defined nonzero. On platforms which lack this support, NASD_IDLE_SUPPORT will be zero. To determine this idle time, call

nasd_status_t nasd_get_total_idle_time(nasd_timespec_t *idle_ts);
The time value that this returns does not necessarily have any particular base or epoch. It is useful for determining the delta between two points in time, however.

The following code fragment illustrates the use of several of the interfaces described herein.


{
  nasd_timespec_t idle0, idle1, idle_diff, t0, t1, tdiff
  nasd_delaycounter_t delayer;
  nasd_status_t rc;

#if NASD_IDLE_SUPPORT > 0
  rc = nasd_get_total_idle_time(&idle0);
#endif /* NASD_IDLE_SUPPORT > 0 */
  nasd_gettime(&t0);

  NASD_BEGIN_DELAYCNT(&delayer);
  NASD_DELAY_FROM(&delayer, 10000); /* 10ms */

  nasd_gettime(&t1);
#if NASD_IDLE_SUPPORT > 0
  rc = nasd_get_total_idle_time(&idle1);
  if (rc == NASD_SUCCESS) {
    idle_diff = idle1;
    NASD_TIMESPEC_SUB(idle_diff, idle0);
  }
#else /* NASD_IDLE_SUPPORT > 0 */
  rc = NASD_OP_NOT_SUPPORTED;
#endif /* NASD_IDLE_SUPPORT > 0 */

  tdiff = t1;
  NASD_TIMESPEC_SUB(tdiff, t0);

  printf("%d:%09d elapsed\n", tdiff.ts_sec, tdiff.ts_nsec);
  if (rc == NASD_SUCCESS) {
    printf("%d:%09d idle\n", idle_diff.ts_sec, idle_diff.ts_nsec);
  }
  else if (rc == NASD_OP_NOT_SUPPORTED) {
    printf("Idle time not supported on this platform\n");
  }
  else {
    printf("Got 0x%x (%s) from nasd_get_total_idle_time()\n",
      rc, nasd_error_string(rc));
  }
}


<--- ---> ^<br>|<br>|
Freelists Timing NASD Programmer's Documentation