    /***********************************************************************
                   Copyright (c) 2007 SKY Computers, Inc.
    
     Redistribution and use in source and binary forms are permitted
     provided that this notice is preserved and that due credit is given to
     SKY Computers, Inc. The name of SKY Computers, Inc. may not be used to
     endorse or promote products derived from this software without
     specific prior written permission. This software is provided ``as is''
     without express or implied warranty.
     ************************************************************************/

#include "sky_ex_inc.h"

/* Define some TimeTrac variables. */
TTHandle trace_log;
TTEventHandle tt_hello_bgn, tt_hello_end;

/* ---------------------------------------------------------------------- */ 

/*
 >++
 *
 *   void tt_init (): Set up the TimeTrac file (open the file, define the 
 *       range events) for the main processing thread.
 *
 *       Returns: Nothing
 >--
 */

void tt_init ()
{
  unsigned group_mask = 0xffffffff;
  unsigned group_id = 0x1;
  int use_thread_safe = 0;
  int num_events = 10000;
  int context_depth = 1;
  int status;
  char filename[256];
  
  /* Create the trace file name */
  sprintf (filename, "hello_world");

  /* Create all neccessary TimeTrac handles */
  status = time_trac_open (filename, use_thread_safe, num_events, group_mask,
			   TT_NO_AUTO_SAVE, TT_CREATE_FILE, context_depth, &trace_log);
  VERIFY_TIMETRAC_STATUS (status);
  
  /* This example has a single TimeTrac range event. */
  status = time_trac_reg_range_event (trace_log, "print_hello", "Blue",
				      group_id, &tt_hello_bgn, &tt_hello_end);
  VERIFY_TIMETRAC_STATUS (status);
}

/* ---------------------------------------------------------------------- */ 

/*
 >++
 *
 *   void tt_term (): Save all of the TimeTrac events and then close the 
 *       TimeTrac file.
 *
 *       Returns: Nothing
 >--
 */

void tt_term ()
{
  int status;

  /* Save the events by overwritting any existing file, close the TimeTrac file. */
  status = time_trac_save (trace_log, TT_OVERWRITE_FILE);
  VERIFY_TIMETRAC_STATUS (status);
  status = time_trac_close (trace_log);
  VERIFY_TIMETRAC_STATUS (status);
}

/* ---------------------------------------------------------------------- */ 

/*
 >++
 *
 *   int main (int argc, char **argv, char **arge): This is the main program 
 *   that does a printf, then waits a while. Repeats 10 times.
 *   
 *       Returns: Exits when done.
 >--
 */

int main (int argc, char **argv, char **arge)
{
  int i = -1;

  /* Set up the TimeTrac parameters. */
  tt_init ();
  
  /* Time, via TimeTrac, the printed message */
  for (i = 0; i < 10; i++)
    {
      /* Time the call to a system function. */
      time_trac_record (trace_log, tt_hello_bgn, (float)i);
      printf ("Hello World -- %d\n", i);
      time_trac_record (trace_log, tt_hello_end, (float)i);

      /* Spread out the above times when viewed w/ TimeTrac. */
      ex_spin_some (500);
    }

  /* Save the TimeTrac events and exit */
  tt_term ();
  return 0;
}

/* --------------------------- End of Module ---------------------------- */
 

