prof.h

Go to the documentation of this file.
00001 /** @file rutz/prof.h class for accumulating profiling information */
00002 
00003 ///////////////////////////////////////////////////////////////////////
00004 //
00005 // Copyright (c) 1999-2004 California Institute of Technology
00006 // Copyright (c) 2004-2007 University of Southern California
00007 // Rob Peters <rjpeters at klab dot caltech dot edu>
00008 //
00009 // created: Thu Jun 30 14:47:11 2005
00010 // commit: $Id: prof.h 8249 2007-04-12 06:03:40Z rjpeters $
00011 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/rutz/prof.h $
00012 //
00013 // --------------------------------------------------------------------
00014 //
00015 // This file is part of GroovX.
00016 //   [http://www.klab.caltech.edu/rjpeters/groovx/]
00017 //
00018 // GroovX is free software; you can redistribute it and/or modify it
00019 // under the terms of the GNU General Public License as published by
00020 // the Free Software Foundation; either version 2 of the License, or
00021 // (at your option) any later version.
00022 //
00023 // GroovX is distributed in the hope that it will be useful, but
00024 // WITHOUT ANY WARRANTY; without even the implied warranty of
00025 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00026 // General Public License for more details.
00027 //
00028 // You should have received a copy of the GNU General Public License
00029 // along with GroovX; if not, write to the Free Software Foundation,
00030 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00031 //
00032 ///////////////////////////////////////////////////////////////////////
00033 
00034 #ifndef GROOVX_RUTZ_PROF_H_UTC20050630214711_DEFINED
00035 #define GROOVX_RUTZ_PROF_H_UTC20050630214711_DEFINED
00036 
00037 #include "rutz/time.h"
00038 
00039 #include <iosfwd>
00040 
00041 namespace rutz
00042 {
00043   class prof;
00044 }
00045 
00046 /// Accumulates profiling information for a given execution context.
00047 /** rutz::prof, along with rutz::trace and associated macros
00048     (e.g. GVX_TRACE()) form a basic profiling facility.
00049 
00050     * GVX_TRACE() statements collect timing information at function
00051       entry and exit (or, more precisely, at construction and
00052       destruction of the local object that is introduced by the
00053       GVX_TRACE() macro call). The overhead for this profiling
00054       information is non-zero, but is completely neglible in
00055       proportion to the amount of execution time taken by any of the
00056       functions in FilterOps.
00057 
00058     * To turn on profiling in a given source file, just #include
00059       "rutz/trace.h" and insert a GVX_TRACE() wherever you like. To
00060       force profiling to be off, #define GVX_NO_PROF before you
00061       #include "rutz/trace.h".
00062 
00063     * In order to actually see the profiling information, you can
00064       either call rutz::prof::print_all_prof_data() directly, or you
00065       can call rutz::prof::print_at_exit(true). If you do the latter,
00066       then when the program exits, the profiling information will be
00067       dumped to a file named 'prof.out' in the current
00068       directory. Currently, the only program that generates a prof.out
00069       in this way is bin/invt (the tcl script interpreter).
00070 
00071     * Profiling information in prof.out looks like the following,
00072       where the first column is the average total usec per call, the
00073       second column is the number of calls, the third column is the
00074       total self usec (i.e. excluding time attributed to nested
00075       GVX_TRACE statements), the fourth column is the total
00076       self+children usec, and the remainder of the line is the
00077       function name or whatever string was passed to GVX_TRACE():
00078 
00079          4   4500      18999      18999 xFilterClean
00080          5   4500      23998      23998 yFilterClean
00081        305  11600      34998    3548467 lowPassX
00082          8   4900      42997      42997 lowPass3y
00083        297  11600      44994    3454495 lowPassY
00084         15   4900      77989      77989 lowPass3x
00085        508  14700    7458831    7477830 lowPass9x
00086        582  14700    8534732    8558730 lowPass9y
00087       8749   4000   25762090   34996666 orientedFilter
00088  */
00089 class rutz::prof
00090 {
00091 public:
00092   prof(const char* s, const char* fname, int lineno) throw();
00093   ~prof() throw();
00094 
00095   /// Different types of timing.
00096   enum timing_mode
00097     {
00098       WALLCLOCK, ///< Track elapsed wall-clock time.
00099       RUSAGE     ///< Track elpased user+sys rusage.
00100     };
00101 
00102   /// Get the current timing_mode.
00103   static timing_mode get_timing_mode() throw()
00104   { return s_timing_mode; }
00105 
00106   /// Set the current timing_mode.
00107   static void set_timing_mode(timing_mode mode) throw()
00108   { s_timing_mode = mode; }
00109 
00110   /// Get the current time according to the given timing_mode.
00111   static rutz::time get_now_time(timing_mode mode) throw()
00112   {
00113     switch (mode)
00114       {
00115       case rutz::prof::RUSAGE:
00116         return (rutz::time::user_rusage() + rutz::time::sys_rusage());
00117 
00118       case rutz::prof::WALLCLOCK:
00119       default:
00120         return rutz::time::wall_clock_now();
00121       }
00122   }
00123 
00124   /// Reset the call count and elapsed time to zero.
00125   void reset() throw();
00126 
00127   /// Returns the number of calls since the last reset().
00128   unsigned int count() const throw();
00129 
00130   void add_time(const rutz::time& t) throw();
00131 
00132   void add_child_time(const rutz::time& t) throw();
00133 
00134   const char* context_name() const throw();
00135 
00136   const char* src_file_name() const throw();
00137 
00138   int src_line_no() const throw();
00139 
00140   /// Get the total elapsed time in microsecs since the last reset().
00141   double total_time() const throw();
00142 
00143   /// Get the total self time in microsecs since the last reset().
00144   double self_time() const throw();
00145 
00146   /// Get the per-call average self time in microsecs since the last reset().
00147   double avg_self_time() const throw();
00148 
00149   /// Print this object's info to the given file.
00150   void print_prof_data(FILE* f) const throw();
00151 
00152   /// Print this object's info to the given stream.
00153   void print_prof_data(std::ostream& os) const throw();
00154 
00155   /// Whether to write a profiling summary file when the program exits.
00156   static void print_at_exit(bool yes_or_no) throw();
00157 
00158   /// Specify the filename for the profiling summary (default is "prof.out").
00159   static void prof_summary_file_name(const char* fname);
00160 
00161   /// Reset all call counts and elapsed times to zero.
00162   static void reset_all_prof_data() throw();
00163 
00164   /// Print all profile data to the given file.
00165   static void print_all_prof_data(FILE* f) throw();
00166 
00167   /// Print all profile data to the given stream.
00168   static void print_all_prof_data(std::ostream& os) throw();
00169 
00170 private:
00171   prof(const prof&) throw();
00172   prof& operator=(const prof&) throw();
00173 
00174   const char*  const m_context_name;
00175   const char*  const m_src_file_name;
00176   int          const m_src_line_no;
00177   unsigned int       m_call_count;
00178   rutz::time         m_total_time;
00179   rutz::time         m_children_time;
00180 
00181   static timing_mode s_timing_mode;
00182 };
00183 
00184 static const char __attribute__((used)) vcid_groovx_rutz_prof_h_utc20050630214711[] = "$Id: prof.h 8249 2007-04-12 06:03:40Z rjpeters $ $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/rutz/prof.h $";
00185 #endif // !GROOVX_RUTZ_PROF_H_UTC20050630214711DEFINED
Generated on Sun May 8 08:42:12 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3