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

phTimeList.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 #include <phObject.h>
00033 #include <phMutex.h>
00034 #include <phList.h>
00035 #include <phTime.h>
00036 #include <phTimeStamp.h>
00037 #include <phTimeInterval.h>
00038 
00039 #include <phTimeList.h>
00040 
00041 #include <phError.h>
00042 #include <phMemory.h>
00043 #include <phPrint.h>
00044 
00045 /* ------------------------------------------------------------------------ */
00046 phTimeList::phTimeList(char *label,FILE *file_desc)
00047 {
00048     phFUNCTION("phTimeList::phTimeList")
00049     int locked = 0;
00050     
00051     phTHIS_LOOSE_LOCK(locked);
00052 
00053     this->setName("phTimeList");
00054  
00055     /* Set the file descriptor for the report */
00056     if (file_desc == NULL) 
00057     {
00058         file_desc = stdout;
00059     }
00060     
00061     this->m_file = file_desc;
00062 
00063     /* allocate the DB */
00064     this->m_list_end = NULL;
00065 
00066     /* set the label */
00067     this->m_label = NULL;
00068     this->m_length = 0;
00069     this->setLabel(label);
00070 
00071     this->m_stopped = 1;
00072 
00073     this->m_elapsed.clear();
00074     
00075     phTHIS_LOOSE_UNLOCK(locked);
00076 }
00077 
00078 /* ------------------------------------------------------------------------ */
00079 phTimeList::~phTimeList()
00080 {
00081     phFUNCTION("phTimeList::~phTimeList")
00082     int locked = 0;
00083     
00084     phTHIS_LOOSE_LOCK(locked);
00085    
00086     phFree(this->m_label);
00087 
00088     /* Free the list */
00089 }
00090 
00091 /* ------------------------------------------------------------------------ */
00092 int phTimeList::start(char *label)
00093 {
00094     phFUNCTION("phTimeList::start")
00095     int locked = 0;
00096     
00097     const int temp_label_size = 1024;
00098     char    temp_label[temp_label_size];
00099     char    *use_label = NULL;
00100     int     retrc = phSUCCESS;
00101     
00102     phTHIS_LOOSE_LOCK(locked);
00103 
00104     if (label == NULL)
00105     {
00106         sprintf(temp_label,"undefined-stamp(%lu)",
00107                 (unsigned long int )this->m_list.length());
00108         use_label = temp_label;
00109     }
00110     else
00111     {
00112         use_label = label;
00113     }
00114    
00115     if (this->m_stopped == 0)
00116     {
00117         rc = this->stop();
00118         phPRINT_RC(rc,NULL,"this->stop()");
00119     }
00120     
00121     phTimeInterval *interval = new phTimeInterval(use_label);
00122     phCHECK_NULLPTR(interval,"new","phTimeInterval allocation failed");
00123 
00124     /* Add the new stamp to the end of the list */
00125     rc = this->m_list.push(interval);
00126     phPRINT_RC(rc,NULL,"this->m_list.push(stamp)");
00127     
00128     this->m_list_end = interval;
00129     
00130     if (use_label == temp_label)
00131     {
00132         phMemset(temp_label,0,sizeof(char)*temp_label_size);
00133     }
00134 
00135     /* start the interval here */
00136     interval->start();
00137     this->m_stopped = 0;
00138     
00139     phTHIS_LOOSE_UNLOCK(locked);
00140 
00141     return retrc;
00142     
00143 error:
00144     if (use_label == temp_label)
00145     {
00146         phMemset(temp_label,0,sizeof(char)*temp_label_size);
00147     }
00148 
00149     phTHIS_LOOSE_UNLOCK(locked);
00150 
00151     return phFAIL;
00152 }
00153 
00154 /* ------------------------------------------------------------------------ */
00155 int phTimeList::stop()
00156 {
00157     phFUNCTION("phTimeList::stop")
00158     int locked = 0;
00159     
00160     int retrc = phSUCCESS;
00161     
00162     phTHIS_LOOSE_LOCK(locked);
00163 
00164     if (this->m_stopped == 0)
00165     {
00166         this->m_list_end->stop();
00167         this->m_stopped = 1;
00168         
00169         phTimeStamp e = this->m_list_end->elapsed();
00170         this->m_elapsed += e;
00171     }
00172     
00173     phTHIS_LOOSE_UNLOCK(locked);
00174 
00175     return retrc;
00176 }
00177  
00178 /* ------------------------------------------------------------------------ */
00179 int phTimeList::empty()
00180 {
00181     phFUNCTION("phTimeList::empty")
00182     int locked = 0;
00183     
00184     int retrc = phSUCCESS;
00185     
00186     phTHIS_LOOSE_LOCK(locked);
00187 
00188     rc = this->m_list.empty();
00189     phPRINT_RC(rc,NULL,"this->m_list.empty()");
00190 
00191     this->m_elapsed.clear();
00192 
00193     this->m_list_end = NULL;
00194     this->m_stopped = 1;
00195     
00196     phTHIS_LOOSE_UNLOCK(locked);
00197 
00198     return retrc;
00199 }
00200 
00201 /* ------------------------------------------------------------------------ */
00202 int phTimeList::setLabel(char *label)
00203 {
00204     phFUNCTION("phTimeList::setLabel")
00205     int locked = 0;
00206      
00207     const int   temp_label_size = 1024;
00208     char        temp_label[temp_label_size];
00209     char        *use_label = NULL;
00210   
00211     phTHIS_LOOSE_LOCK(locked);
00212  
00213     uint32_t length = 0;
00214     
00215     if (label == NULL)
00216     {
00217 #if phPRINT_THREAD_ID 
00218         sprintf(temp_label,"phTimeList(thread:%d,this:%p)",(int)phGetCurrentThreadId(), this);
00219 #else
00220         sprintf(temp_label,"phTimeList(this:%p)", this);
00221 #endif
00222                 
00223         use_label = temp_label;
00224     }
00225     else
00226     {
00227         use_label = label;
00228     }
00229         
00230     length = strlen(use_label);
00231     
00232     if (this->m_label == NULL)
00233     {
00234         this->m_label = (char *)phCalloc(length+1,sizeof(char));
00235         phCHECK_NULLPTR(this->m_label,"phCalloc","phCalloc failed");
00236         
00237         this->m_length = length;
00238     }
00239     else if (length > this->m_length)
00240     {
00241         this->m_label = (char *)phRealloc(this->m_label,(length+1) * sizeof(char));
00242         phCHECK_NULLPTR(this->m_label,"phRealloc","phRealloc failed");        
00243         
00244         this->m_length = length;
00245     }
00246     
00247     sprintf(this->m_label,"%s", use_label );
00248     
00249     if (use_label == temp_label)
00250     {
00251         phMemset(temp_label,0,sizeof(char)*temp_label_size);
00252     }
00253     
00254     phTHIS_LOOSE_UNLOCK(locked);
00255 
00256     return phSUCCESS;
00257 error:
00258     return phFAIL;
00259 }
00260 
00261 /* ------------------------------------------------------------------------ */
00262 phTimeStamp phTimeList::elapsed()
00263 {
00264     phFUNCTION("phTimeList::elapsed")
00265     int locked = 0;
00266     phTimeStamp temp;
00267     
00268     phTHIS_LOOSE_LOCK(locked);
00269     
00270     temp = this->m_elapsed;
00271     
00272     phTHIS_LOOSE_UNLOCK(locked);
00273 
00274     return temp;
00275 }
00276    
00277 /* ------------------------------------------------------------------------ */
00278 int phTimeList::report(int flag, FILE *file_desc )
00279 {
00280     phFUNCTION("phTimeList::report")
00281     int locked = 0;
00282 
00283     phTimeInterval  *cur    = NULL;
00284     uintmax_t       sec     = 0;
00285     uintmax_t       usec    = 0;
00286     double          ftotal  = 0.0;
00287     
00288     phTHIS_LOOSE_LOCK(locked);
00289    
00290     cur = (phTimeInterval *)this->m_list.getHead();
00291     
00292     /* if a file descriptor was supplied, use it otherwise (-1) use the 
00293      * default file descriptor set in the constructor (stdout or use defined) 
00294      */
00295     if (file_desc == NULL)
00296     {
00297         file_desc = this->m_file;
00298     }
00299     
00300     while (cur != NULL)
00301     {
00302         if ((flag & phTIME_REPORT_ALL) == phTIME_REPORT_ALL)
00303         {
00304             cur->report(this->m_label,file_desc);
00305         }
00306         
00307         /* Add elapsed time onto total */
00308         cur = cur->getNextInterval();
00309     }
00310 
00311     sec = this->m_elapsed.getSeconds();
00312     usec = this->m_elapsed.getMicroseconds();
00313     ftotal = (double)sec + (((double)usec) / 1000000.0);
00314     
00315     if ((flag & (phTIME_REPORT_SUM)) == phTIME_REPORT_SUM)
00316     {
00317         fprintf(file_desc,
00318                 "(%17s)Total - %10.0f\ts:%3lu us:%10lu\n",
00319                 ((this->m_label == NULL) ? "" : this->m_label),
00320                 ftotal,
00321                 sec,usec);
00322         fflush(file_desc);
00323     }
00324    
00325     phTHIS_LOOSE_UNLOCK(locked);
00326     
00327     return phSUCCESS;
00328 }
00329 




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