Pages

2010-09-08

Linux: Measuring Boot Time

// Purpose of this utility is to timestamp each line coming over stdin
// USAGES:
//    tstamp.exe < /dev/ttyS0
//    <commands> | tstamp.exe
// 

#include <stdio.h>
#include <time.h>
#include <sys/time.h>

char buf[10*1024];

main(int argc)
{
  struct timeval tv;
  double time_start,time_now,time_prev;
  int first=1;

  if(argc!=1) // as of now no arguments are allowed. print usage
  {
     printf("Timestamps each line coming over stdin\n"
             "\tUSAGES:\n"
             "\t\ttstamp.exe < /dev/ttyS0\n"
             "\t\t<commands> | tstamp.exe\n"
             "\t\tetc..\n");
     printf("Output is printed in the following format\n"
               "\tcolumn1 is elapsed time since first message"
               "\tcolumn2 is elapsed time since previous message\n"
             "\tcolumn3 is the message\n");
 
     exit(0);
  }

  printf("\tcolumn1 is elapsed time since first message\n"
           "\tcolumn2 is elapsed time since previous message\n"
         "\tcolumn3 is the message\n");

  while(1)
  {
    if(gets(buf))
    {
       gettimeofday(&tv, NULL); // get system time
       time_now = tv.tv_sec + (tv.tv_usec*(1.0/1000000.0)); // convert to double
       if(first) // if first time, notedown the time_start
         first = 0,  time_start = time_prev = time_now;

       printf("%03.3f %02.3f: ",(float)(time_now-time_start),
                      (float)(time_now-time_prev)); // print the column1 and 2

       puts(buf); // now print the message as column3
       time_prev = time_now;
    }
  }
}

No comments:

Post a Comment