Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

phTime.cpp

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002     Phission :
00003         Realtime Vision Processing System
00004 
00005     Copyright (C) 2003-2006 Philip D.S. Thoren (pthoren@cs.uml.edu)
00006     University of Massachusetts at Lowell,
00007     Laboratory for Artificial Intelligence and Robotics
00008 
00009     This file is part of Phission.
00010 
00011     Phission is free software; you can redistribute it and/or modify
00012     it under the terms of the GNU Lesser General Public License as published by
00013     the Free Software Foundation; either version 2 of the License, or
00014     (at your option) any later version.
00015 
00016     Phission is distributed in the hope that it will be useful,
00017     but WITHOUT ANY WARRANTY; without even the implied warranty of
00018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019     GNU Lesser General Public License for more details.
00020 
00021     You should have received a copy of the GNU Lesser General Public License
00022     along with Phission; if not, write to the Free Software
00023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024 
00025  ---------------------------------------------------------------------------*/
00026 #ifdef HAVE_CONFIG_H
00027     #include <phissionconfig.h>
00028 #endif
00029 
00030 #include <phStandard.h>
00031 
00032 #if defined(WIN32)
00033     #include <windows.h>
00034     #define USE_CLOCK() 0
00035     #define USE_TIMEVAL() 0
00036 #elif defined(__ADSPBLACKFIN__)
00037     #include <PhissionVDK.h>
00038     #define USE_CLOCK() 0
00039     #define USE_TIMEVAL() 0
00040 #else
00041     #define USE_CLOCK() 0
00042     #define USE_TIMEVAL() 1
00043     #if defined(HAVE_SYS_TIME_H) && USE_TIMEVAL()
00044         #include <sys/time.h>
00045     #endif
00046 
00047     #if defined(HAVE_TIME_H) && USE_CLOCK()
00048         #include <time.h>
00049     #endif
00050 #endif
00051 
00052 #include <phTime.h>
00053 
00054 #include <phError.h>
00055 #include <phMemory.h>
00056 #include <phPrint.h>
00057 
00058 #ifdef __cplusplus
00059 extern "C" 
00060 {
00061 #endif
00062  
00063 /* ---------------------------------------------------------------------- */
00064 #if USE_TIMEVAL()
00065     typedef struct timeval ph_time_type;
00066 #else
00067     typedef struct ph_time_t
00068     {
00069         double  value;
00070         #if defined(WIN32)
00071         int64_t priv;
00072         #endif
00073     } ph_time_type;
00074 #endif
00075 
00076 /* ---------------------------------------------------------------------- */
00077 struct ph_time_entry_t 
00078 {
00079     ph_time_type    time_start; /* start */
00080     ph_time_type    time_stop; /* stop */
00081     
00082     char            *label;
00083 
00084     uint32_t        stopped;
00085     
00086     struct ph_time_entry_t *next;
00087     struct ph_time_entry_t *previous;
00088 
00089 };
00090     
00091 /* ---------------------------------------------------------------------- */
00092 struct ph_timedb_t 
00093 {
00094     char *label;
00095 
00096     uint32_t count;
00097     struct ph_time_entry_t *list;
00098     struct ph_time_entry_t *list_end;
00099     
00100 };
00101     
00102 /* ---------------------------------------------------------------------- */
00103 extern double ph_time_diff_us(ph_time_type time_start,
00104                               ph_time_type time_stop,
00105                               ph_time_type *tdiff );
00106 
00107 #if 0
00108 extern void ph_time_report( char *label,
00109                             ph_time_type *tstart,
00110                             ph_time_type *tstop,
00111                             char **tags,
00112                             unsigned int n,
00113                             unsigned long flag);
00114 #endif
00115 
00116 /* ---------------------------------------------------------------------- */
00117 double ph_time_diff_us( ph_time_type time_start,
00118                         ph_time_type time_stop,
00119                         ph_time_type *tdiff)
00120 {
00121     double fdiff = 0.0;
00122 #if defined(WIN32)
00123     if (time_start.priv)
00124     {
00125         tdiff->value = (time_stop.value - time_start.value) / 
00126                                         (double)time_start.priv;
00127         fdiff = tdiff->value * 1000000000.0;
00128     }
00129     else
00130     {
00131         tdiff->value = (time_stop.value - time_start.value);
00132         fdiff = tdiff->value * 1000000.0;
00133         tdiff->value /= 1000.0;
00134     }
00135 #elif defined(__ADSPBLACKFIN__)
00136     tdiff->value = (time_stop.value - time_start.value);
00137     fdiff = tdiff->value * 1000.0;
00138     tdiff->value /= 1000.0;
00139 #else
00140     #if USE_TIMEVAL()
00141     double usec = 0.0;
00142     double sec = 0.0;
00143 
00144     if (((time_start.tv_sec != 0) && (time_start.tv_usec != 0)) ||
00145         ((time_stop.tv_sec != 0) && (time_stop.tv_usec != 0)))
00146     {
00147         sec = time_stop.tv_sec - time_start.tv_sec;
00148         sec *= ((sec < 0) ? -1.0 : 1.0);
00149         if (tdiff != NULL) tdiff->tv_sec =  (long)sec;
00150         
00151         usec = time_stop.tv_usec - time_start.tv_usec;
00152         usec *= ((usec < 0) ? -1.0 : 1.0);
00153         if (tdiff != NULL) tdiff->tv_usec = (long)usec;
00154         
00155         fdiff = (sec * 1000000) + usec;
00156     }
00157     #endif
00158 
00159     #if USE_CLOCK()
00160     /* *tdiff is in s */
00161     tdiff-.value = (((double)(time_stop - time_start)) / CLOCKS_PER_SEC);
00162     /* fdiff is in us */
00163     fdiff = tdiff->value * 1000000.0; 
00164     #endif
00165 #endif
00166     
00167     return fdiff;
00168 }
00169 
00170 /* ---------------------------------------------------------------------- */
00171 #if 0
00172 void ph_time_report(char *label,
00173                  ph_time_type *tstart,
00174                  ph_time_type *tstop,
00175                  char **tags,
00176                  unsigned int n,
00177                  unsigned long flag )
00178 {
00179     phFUNCTION("report_time")
00180 
00181     unsigned int i = 0;
00182     ph_time_type tdiff = {0,0};
00183     ph_time_type total_tdiff = {0,0};
00184     double fdiff = 0.0;
00185     double total_fdiff = 0.0;
00186 
00187     for (i = 0; i < n; i++)
00188     {
00189         fdiff = ph_time_diff_us(tstart[i],tstop[i],&tdiff);
00190         if ((flag & phTIME_REPORT_ALL) == phTIME_REPORT_ALL)
00191         {
00192             printf("(%17s):Time[%3d]:%10.0f \tsec:%3ld usec:%10ld : [%s]\n",
00193                      label,
00194                      i,
00195                      fdiff,
00196                      tdiff.tv_sec,
00197                      tdiff.tv_usec,
00198                      tags[i]
00199                      );
00200             fflush(stdout);
00201         }
00202         total_fdiff += fdiff;
00203     }
00204 
00205     total_tdiff.tv_sec = (long)(total_fdiff / 1000000);
00206     total_tdiff.tv_usec = ((long)total_fdiff) - (1000000 * total_tdiff.tv_sec);
00207     
00208     if ((flag & (phTIME_REPORT_SUM)) == phTIME_REPORT_SUM)
00209     {
00210         printf("(%17s)Total     :%10.0f \tsec:%3ld usec:%10ld\n",
00211                  label,
00212                  total_fdiff,
00213                  total_tdiff.tv_sec,total_tdiff.tv_usec);
00214         fflush(stdout);
00215     }
00216 }
00217 #endif
00218 
00219 /* ---------------------------------------------------------------------- */
00220 int ph_timedb_alloc(struct ph_timedb_t **timedb,
00221                     const char *label )
00222 {
00223     phFUNCTION("ph_timedb_alloc")
00224     uint32_t length = 0;
00225 
00226     if (timedb == NULL) return phFAIL;
00227     if (*timedb != NULL) return phFAIL;
00228    
00229     (*timedb) = (struct ph_timedb_t *)phCalloc(1,sizeof(struct ph_timedb_t));
00230     if ((*timedb) == NULL) return phFAIL;
00231 
00232     (*timedb)->count    = 0;
00233     (*timedb)->list     = NULL;
00234     (*timedb)->list_end = NULL;
00235     (*timedb)->label    = NULL;
00236     
00237     if (label != NULL)
00238     {
00239         length = strlen(label);
00240         if (length != 0)
00241         {
00242             (*timedb)->label = (char *)phCalloc(length + 1,sizeof(char));
00243             if ((*timedb)->label != NULL)
00244             {
00245                 sprintf((*timedb)->label,"%s",label);
00246             }
00247         }
00248     }
00249     
00250     return phSUCCESS;
00251 }
00252 
00253 /* ---------------------------------------------------------------------- */
00254 int ph_timedb_free( struct ph_timedb_t **timedb )
00255 {
00256     phFUNCTION("ph_timedb_free")
00257 
00258     struct ph_time_entry_t *cur = NULL;
00259     struct ph_time_entry_t *temp = NULL;
00260     
00261     if (timedb == NULL) return phFAIL;
00262     if (*timedb == NULL) return phFAIL;
00263     
00264     cur = (*timedb)->list;
00265     
00266     while (cur != NULL)
00267     {
00268         temp = cur->next;
00269 
00270         phFree(cur->label);
00271         phFree(cur);
00272         
00273         cur = temp;
00274     }
00275 
00276     phFree((*timedb)->label);
00277     phFree(*timedb);
00278     
00279     return phSUCCESS;
00280 }
00281 
00282 /* ---------------------------------------------------------------------- */
00283 int ph_time_start( struct ph_timedb_t *timedb, char *label )
00284 {
00285     phFUNCTION("ph_time_start")
00286     struct ph_time_entry_t *node = NULL;
00287     uint32_t length = 0;
00288     
00289     if (timedb == NULL) return phFAIL;
00290 
00291     /* Make sure there is an end timestamp on the last node */
00292     if (timedb->list_end != NULL)
00293     {
00294         if (timedb->list_end->stopped != 1)
00295         {
00296             rc = ph_time_stop(timedb);
00297             if (rc != phSUCCESS) return phFAIL;
00298         }        
00299     }
00300             
00301     /* Allocate a new node */
00302     node = (struct ph_time_entry_t *)phCalloc(1,sizeof(struct ph_time_entry_t));
00303     if (node == NULL) return phFAIL;
00304 
00305     /* Init the time entry's pointers */
00306     node->previous = timedb->list_end;
00307     node->next = NULL;
00308  
00309     /* Set the time entry's label */
00310     if (label != NULL)
00311     {
00312         length = strlen(label);
00313         if (length != 0)
00314         {
00315             node->label = (char *)phCalloc(length + 1,sizeof(char));
00316             if (node->label != NULL)
00317             {
00318                 sprintf(node->label,"%s",label);
00319             }
00320         }
00321     }
00322     
00323     /* Initialize the timedb's pointers */
00324     if (timedb->list == NULL)
00325     {
00326         timedb->list_end = timedb->list = node;
00327     }
00328     /* Add the new node to the end of the list */
00329     else
00330     {
00331         timedb->list_end->next = node;
00332         timedb->list_end = node;
00333     }
00334 
00335     /* Start the timestamp */
00336     node->stopped = 0;
00337     
00338 #if defined(WIN32)
00339     {
00340         LARGE_INTEGER li;
00341         if (QueryPerformanceFrequency(&li))
00342         {
00343             node->time_start.priv = li.QuadPart;
00344             if (QueryPerformanceCounter(&li))
00345             {
00346                 node->time_start.value = li.QuadPart;                    
00347             }
00348             else
00349             {
00350                 node->time_start.priv = 0;
00351                 node->time_start.value = GetTickCount();
00352             }
00353 
00354         }
00355         else
00356         {
00357             node->time_start.priv = 0;
00358             node->time_start.value = GetTickCount();
00359         }
00360         
00361     }
00362 #elif defined(__ADSPBLACKFIN__)
00363     node->time_start.value = ((double)VDK::GetUptime()) * 
00364                              ((double)VDK::GetTickPeriod());
00365 #else
00366     #if USE_TIMEVAL()
00367     gettimeofday(&(node->time_start),NULL);
00368     #endif
00369     #if USE_CLOCK()
00370     node->time_start.value = clock();
00371     #endif
00372 #endif
00373 
00374     return phSUCCESS;
00375 }
00376 
00377 /* ---------------------------------------------------------------------- */
00378 int ph_time_stop( struct ph_timedb_t *timedb )
00379 {
00380     phFUNCTION("ph_time_stop")
00381     struct ph_time_entry_t *node = NULL;
00382     
00383     if (timedb == NULL) return phFAIL;
00384     if ((node = timedb->list_end) == NULL) return phFAIL;
00385 #if defined(WIN32)
00386     {
00387         if (node->time_start.priv)
00388         {
00389             LARGE_INTEGER li;
00390             if (QueryPerformanceCounter(&li))
00391             {
00392                 node->time_stop.value = li.QuadPart;
00393                 node->time_stop.priv = node->time_start.priv;
00394             }
00395             else
00396             {
00397                 node->time_start.priv = node->time_stop.priv = 0;
00398                 node->time_stop.value = GetTickCount();
00399             }
00400         }
00401         else
00402         {
00403             node->time_stop.priv = 0;
00404             node->time_stop.value = GetTickCount();
00405         }
00406     }
00407 #elif defined(__ADSPBLACKFIN__)
00408     node->time_stop.value = ((double)VDK::GetUptime()) * 
00409                             ((double)VDK::GetTickPeriod());
00410 #else
00411     #if USE_TIMEVAL()
00412     gettimeofday(&(node->time_stop), NULL);
00413     #endif
00414     #if USE_CLOCK()
00415     node->time_stop.value = clock();
00416     #endif
00417 #endif
00418     node->stopped = 1;
00419 
00420     return phSUCCESS;
00421 }
00422 
00423 /* ---------------------------------------------------------------------- */
00424 int ph_timedb_report(   struct ph_timedb_t *timedb,
00425                         uint32_t flag )
00426 {
00427     phFUNCTION("ph_timedb_report")
00428 
00429     uint32_t                i = 0;
00430     struct ph_time_entry_t  *cur = NULL;
00431     
00432     ph_time_type            tdiff;
00433     double                  fdiff = 0;
00434     
00435     long                    total_tdiff_sec;
00436     long                    total_tdiff_usec;
00437     double                  total_fdiff = 0.0;
00438     
00439     if (timedb == NULL) return phFAIL;
00440     cur = timedb->list;
00441  
00442     while (cur != NULL)
00443     {
00444         fdiff = ph_time_diff_us(cur->time_start,cur->time_stop,&tdiff);
00445 
00446         if ((flag & phTIME_REPORT_ALL) == phTIME_REPORT_ALL)
00447         {
00448 #if USE_TIMEVAL()
00449             printf("(%17s):Time[%3lu]:%10.0f \tsec:%3ld usec:%10ld : [%s]\n",
00450                      ((timedb->label == NULL) ? "" : timedb->label),
00451                      (unsigned long int)i,
00452                      fdiff,
00453                      tdiff.tv_sec,
00454                      tdiff.tv_usec,
00455                      ((cur->label == NULL) ? "" : cur->label)
00456                      );
00457 #else
00458             printf("(%17s):Time[%3lu]:%10.0f \tsec:%3ld usec:%10ld : [%s]\n",
00459                      ((timedb->label == NULL) ? "" : timedb->label),
00460                      (unsigned long int)i,
00461                      fdiff,
00462                      (long int)tdiff.value, /* TODO: fix this to be real us, not us + s */
00463                      (long int)fdiff,
00464                      ((cur->label == NULL) ? "" : cur->label)
00465                      );
00466 #endif
00467             fflush(stdout);
00468             i++;
00469         }
00470         total_fdiff += fdiff;
00471         cur = cur->next;
00472     }
00473 
00474     total_tdiff_sec = (long)(total_fdiff / 1000000);
00475     total_tdiff_usec = ((long)total_fdiff) - (1000000 * total_tdiff_sec);
00476 
00477     if ((flag & (phTIME_REPORT_SUM)) == phTIME_REPORT_SUM)
00478     {
00479         printf("(%17s)Total     :%10.0f \tsec:%3ld usec:%10ld\n",
00480                  ((timedb->label == NULL) ? "" : timedb->label),
00481                  total_fdiff,
00482                  total_tdiff_sec,
00483                  total_tdiff_usec
00484                  );
00485         fflush(stdout);
00486     }
00487    
00488     return phSUCCESS;
00489 }
00490 
00491 
00492 #ifdef __cplusplus
00493 } /* extern "C" { */
00494 #endif
00495 
00496 
00497 




Copyright (C) 2002 - 2007 Philip D.S. Thoren ( pthoren@users.sourceforge.net )
University Of Massachusetts at Lowell
Robotics Lab
SourceForge.net Logo

Generated on Sat Jun 16 02:44:03 2007 for phission by  doxygen 1.4.4