NASD Programmer's Documentation
Timing

Because the NASD tree is a research prototype, it is often desirable to measure time elapsed between two points. NASD provides a portable interface for doing so. Use of the timer mechanism requires initialization of the threads module. The basic type of timer for measuring elapsed time between two points is nasd_timer_t. To start such a timer running, call NASD_TM_START() with a pointer to the nasd_timer_t. To stop it, call NASD_TM_STOP() with a pointer to the nasd_timer_t. To determine how much time elapsed between start and stop, call NASD_TM_ELAPSED_TS() with two arguments: the first, a pointer to the nasd_timer_t, and the second, a pointer to a nasd_timespec_t, which will be initialized to the time elapsed between start and stop.

Example:


nasd_timespec_t ts;
nasd_timer_t tm;

/* ... */

NASD_TM_START(&tm);

/* code to measure elapsed execution time of here */

NASD_TM_STOP(&tm);
NASD_TM_ELAPSED_TS(&tm, &ts);
printf("Elapsed time: %d:%09d\n", ts.ts_sec, ts.ts_nsec);

Accumulating elapsed time

Sometimes, it is useful to maintain a running total of the amount of time between two points. For this purpose, NASD_TM_STOP_ACCUM_TS() is also provided. This takes two arguments: the first, a pointer to the nasd_timer_t, and the second, a pointer to a nasd_timespec_t. NASD_TM_STOP_ACCUM_TS() stops a running timer, evaluates how much time elapsed between start and stop, and adds that amount of time to the timespec pointed to by the second argument.

Example:


nasd_timespec_t ts;
nasd_timer_t tm;
int i;

/* ... */

ts.ts_sec = 0;
ts.ts_nsec = 0;

for(i=0;i<NITERS;i++) {
  /* ... */
  NASD_TM_START(&tm);
  /* code to measure elapsed execution time of here */
  NASD_TM_STOP_ACCUM_TS(&tm, &ts);
  /* ... */
}

printf("Total elapsed time: %d:%09d\n", ts.ts_sec, ts.ts_nsec);

Checkpointing

When timers are used to profile code, it may be useful to understand how much time elapses between each of several points. To do this, use a nasd_multitimer_t in place of a nasd_timer_t. The same operations that work on a nasd_timer_t work on a nasd_multitimer_t, but two additional operations are defined as well. NASD_TM_CKPT() takes as its sole argument a pointer to a nasd_multitimer_t, and records a timestamp as well as the source filename and line number where the checkpoint occurred. NASD_TM_REPORT() takes a pointer to a nasd_multitimer_t, and prints the filename and line number of the start, stop, and each checkpoint, as well as the time elapsed between each of these points.

Example:


nasd_multitimer_t mtm;

NASD_TM_START(&mtm);

do_something();

NASD_TM_CKPT(&mtm);

do_something_else(0);

NASD_TM_CKPT(&mtm);

do_something_else(1);

NASD_TM_CKPT(&mtm);

do_something();

NASD_TM_STOP(&mtm);
printf("Timing results\n");
NASD_TM_REPORT(&mtm);


<--- ---> ^<br>|<br>|
Time Delays NASD Programmer's Documentation