MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/Essentials/MOOSUtilityLib/MOOSThreadedTimeJournal.h
Go to the documentation of this file.
00001 #ifndef __MOOSTHREADEDTIMEJOURNAL_H
00002 #define __MOOSTHREADEDTIMEJOURNAL_H
00003 
00004 #include <MOOSLIB/MOOSLib.h>
00005 #include <MOOSGenLib/MOOSGenLib.h>
00006 
00007 #include <fstream>
00008 #include <map>
00009 #include <list>
00010 
00011 /* The following usage info is from the old
00012  * CMOOSTimeJournal class.  You now need
00013  * to specify a thread handle provided by
00014  * GetNextHandle.  And there's a straight
00015  * Log(handle, string) method if you don't
00016  * want to time anything.
00017  * The levels thing is temporarily disabled.
00018  */
00019 
00020 //this is a utility class to do time stamping. 
00021 //
00022 //Tick("LABEL");
00023 //  DO YOUR CODE
00024 //        TICK("LABEL2")
00025 //            MORE CODE
00026 //        Tock("LABEL2")
00027 //Tock("LABEL")
00028 //Dump();
00029 //
00030 //this will produce a pretty file of timings
00031 //you can call NewLevel() to print an iteration seperator
00032 //in the dump file. This an exerpt from a dump file
00033 /*Level:4754
00034 Iterate              0.000 seconds
00035 Level:4755
00036 Iterate              0.430 seconds
00037 ConstraintApplication 0.422 seconds
00038 EKF                  0.012 seconds
00039 EKF_UPD_1            0.006 seconds
00040 EKF_OBS              0.006 seconds    
00041 ConstraintSearch     0.003 seconds
00042 Level:4756
00043 Iterate              0.041 seconds
00044 Level:4757
00045 Iterate              0.000 seconds
00046 */
00047 
00048 
00049 /* This could desparately do with some error checking! */
00050 
00051 
00052 
00053 class CMOOSThreadedTimeJournal
00054 {
00055 public:
00056     
00057     typedef long H_THREADLOG;
00058     
00059     
00060     CMOOSThreadedTimeJournal()
00061     {        
00062         m_dfStartTime = HPMOOSTime();
00063         m_nextHandle = 0;
00064     }
00065     
00066     ~CMOOSThreadedTimeJournal()
00067     {
00068         Dump();
00069         m_file.close();
00070     }
00071     
00072     void Open(const std::string & sFile)
00073     {
00074         m_file.open(sFile.c_str());
00075     }
00076     
00077     void SetStartTime(double startTime)
00078     {
00079         m_dfStartTime = startTime;
00080     }
00081 
00082     H_THREADLOG GetNextHandle(std::string sName)
00083     {
00084         THREADINFO threadinfo;
00085         
00086         threadinfo.name = sName;
00087         threadinfo.level = 0;
00088         
00089         m_threadInfoMap[m_nextHandle] = threadinfo;
00090         
00091         return static_cast<H_THREADLOG> (m_nextHandle++);
00092     }
00093     
00094     bool Tick(const H_THREADLOG handle, const std::string &sTimerName)
00095     {       
00096         m_lock.Lock();
00097         
00098         bool bSuccess = false;
00099         THREADMAP::iterator p = m_threadInfoMap.find(handle);
00100         if (p != m_threadInfoMap.end()) {
00101             m_threadInfoMap[handle].timermap[sTimerName] = HPMOOSTime();
00102                         
00103             bSuccess = true;
00104         }
00105         
00106         m_lock.UnLock();
00107         
00108         return bSuccess;
00109         
00110     }
00111     
00112     bool Tock(const H_THREADLOG handle, const std::string &sTimerName)
00113     {
00114         m_lock.Lock();
00115         
00116         bool bSuccess = false;
00117         THREADMAP::iterator pThreadInfo = m_threadInfoMap.find(handle);
00118         if (pThreadInfo != m_threadInfoMap.end()) {
00119             THREADINFO &threadinfo = m_threadInfoMap[handle];
00120             
00121             TIMERMAP::iterator pTimer = threadinfo.timermap.find(sTimerName);
00122             if (pTimer != threadinfo.timermap.end()) {
00123                 std::string msg = MOOSFormat("\t\t%-15s : %-20s %.3f seconds", threadinfo.name.c_str(), sTimerName.c_str(), HPMOOSTime()-threadinfo.timermap[sTimerName]);
00124                 m_logList.push_back(msg);
00125                 
00126                 threadinfo.timermap.erase(pTimer);
00127                 bSuccess = true;
00128             } else {
00129                 MOOSTrace("No such timer %s : \"%s\"\n", threadinfo.name, sTimerName.c_str());
00130             }
00131             
00132         } else {
00133             
00134             
00135         }
00136         
00137         m_lock.UnLock();
00138         return bSuccess;
00139         
00140     }
00141     
00142     void Log(const H_THREADLOG handle, const std::string &str)
00143     {
00144         m_lock.Lock();
00145         
00146         THREADMAP::iterator pThreadInfo = m_threadInfoMap.find(handle);
00147         if (pThreadInfo != m_threadInfoMap.end()) {
00148             THREADINFO &threadinfo = m_threadInfoMap[handle];
00149             
00150             std::string msg = MOOSFormat("\t%.3f %-15s : %s", HPMOOSTime()-m_dfStartTime, threadinfo.name.c_str(), str.c_str());
00151             m_logList.push_back(msg);
00152         }   
00153         
00154         m_lock.UnLock();
00155     }
00156     
00157     void NewLevel(const H_THREADLOG handle, int nL=-1)
00158     {
00159         m_lock.Lock();
00160         
00161         THREADMAP::iterator pThreadInfo = m_threadInfoMap.find(handle);
00162         if (pThreadInfo != m_threadInfoMap.end()) {
00163             THREADINFO &threadinfo = m_threadInfoMap[handle];
00164             
00165             if (nL == -1) {
00166                 threadinfo.level++;
00167             } else {
00168                 threadinfo.level = nL;
00169             }
00170             
00171         }
00172         
00173         m_lock.UnLock();
00174     }
00175     
00176     void Dump()
00177     {        
00178         //m_File<<"Level:"<<m_nLevel<<std::endl;
00179         
00180         std::copy(m_logList.begin(),m_logList.end(),std::ostream_iterator<std::string>(m_file,"\n"));
00181         m_file.flush();
00182 
00183         m_logList.clear();
00184     }
00185     
00186 protected:    
00187     
00188     
00189 
00190     typedef std::map<std::string, double> TIMERMAP;
00191 
00192     typedef struct {
00193         std::string name;
00194         int level;
00195         TIMERMAP timermap;
00196     } THREADINFO;
00197     
00198 
00199     typedef std::map<H_THREADLOG, THREADINFO> THREADMAP;
00200     
00201     
00202     THREADMAP m_threadInfoMap;
00203  
00204     CMOOSLock m_lock;
00205    
00206     double m_dfStartTime;
00207 
00208     // This is just a buffer of log entries which are yet to be dumped to file
00209     std::list<std::string> m_logList;
00210     
00211     // The log file
00212     std::ofstream m_file;
00213     
00214     long m_nextHandle;
00215     
00216 private:
00217     CMOOSThreadedTimeJournal(const CMOOSThreadedTimeJournal &);  // Deliberately not implemented
00218     void operator=(const CMOOSThreadedTimeJournal &);  // Deliberately not implemented
00219     
00220 };
00221 
00222 #endif // __MOOSTHREADEDTIMEJOURNAL_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines