CpuTimer.H

Go to the documentation of this file.
00001 /*! @file Util/CpuTimer.H Track user/system CPU usage and wall-clock time */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00005 // University of Southern California (USC) and the iLab at USC.         //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; filed July 23, 2001, following provisional applications     //
00013 // No. 60/274,674 filed March 8, 2001 and 60/288,724 filed May 4, 2001).//
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Rob Peters <rjpeters@klab.caltech.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Util/CpuTimer.H $
00035 // $Id: CpuTimer.H 5375 2005-08-23 15:19:13Z rjpeters $
00036 //
00037 
00038 #ifndef CPUTIMER_H_DEFINED
00039 #define CPUTIMER_H_DEFINED
00040 
00041 #include "Util/log.H"
00042 
00043 #include <sys/resource.h>
00044 #include <sys/time.h>
00045 
00046 //! Utility class for monitoring user/system cpu usage plus wall-clock time.
00047 class CpuTimer
00048 {
00049 public:
00050   //! Constructor; immediately start timing
00051   inline CpuTimer();
00052 
00053   //! Reset the timer and immediately start timing again
00054   inline void reset();
00055 
00056   //! Mark the current time for access via user_secs()/sys_secs()/real_secs()
00057   inline void mark();
00058 
00059   //! Get the user CPU seconds between reset() and mark()
00060   inline double user_secs() const;
00061 
00062   //! Get the system CPU seconds between reset() and mark()
00063   inline double sys_secs() const;
00064 
00065   //! Get the real (wall-clock) CPU seconds between reset() and mark()
00066   inline double real_secs() const;
00067 
00068   //! Print a report based on the most recent mark()
00069   inline void report(const char* where = "") const;
00070 
00071 private:
00072   timeval t1, t2;
00073   rusage r1, r2;
00074 };
00075 
00076 // ######################################################################
00077 // ##### INLINE FUNCTIONS:
00078 // ######################################################################
00079 
00080 inline CpuTimer::CpuTimer()
00081 { reset(); }
00082 
00083 inline void CpuTimer::reset()
00084 {
00085   gettimeofday(&t1, (struct timezone*)0);
00086   getrusage(RUSAGE_SELF, &r1);
00087 
00088   r2 = r1;
00089   t2 = t1;
00090 }
00091 
00092 inline void CpuTimer::mark()
00093 {
00094   gettimeofday(&t2, (struct timezone*)0);
00095   getrusage(RUSAGE_SELF, &r2);
00096 }
00097 
00098 inline double CpuTimer::user_secs() const
00099 {
00100   return
00101     double(r2.ru_utime.tv_sec - r1.ru_utime.tv_sec) +
00102     double(r2.ru_utime.tv_usec - r1.ru_utime.tv_usec) / 1000000.0;
00103 }
00104 
00105 inline double CpuTimer::sys_secs() const
00106 {
00107   return
00108     double(r2.ru_stime.tv_sec - r1.ru_stime.tv_sec) +
00109     double(r2.ru_stime.tv_usec - r1.ru_stime.tv_usec) / 1000000.0;
00110 }
00111 
00112 inline double CpuTimer::real_secs() const
00113 {
00114   return
00115     double(t2.tv_sec - t1.tv_sec) +
00116     double(t2.tv_usec - t1.tv_usec) / 1000000.0;
00117 }
00118 
00119 inline void CpuTimer::report(const char* where) const
00120 {
00121   const double u = user_secs();
00122   const double s = sys_secs();
00123   const double r = real_secs();
00124   if (where != 0 && where[0] != '\0')
00125     LINFO("%s: %.3fu, %.3fs, %.3fr, %.1f%%",
00126           where, u, s, r, 100.0 * (u+s) / r);
00127   else
00128     LINFO("%.3fu, %.3fs, %.3fr, %.1f%%",
00129           u, s, r, 100.0 * (u+s) / r);
00130 }
00131 
00132 #endif // !CPUTIMER_H_DEFINED
Generated on Sun May 8 08:42:25 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3