PLearn 0.1
Profiler.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PLearn (A C++ Machine Learning Library)
00004 // Copyright (C) 2001 Yoshua Bengio and University of Montreal
00005 //
00006 
00007 // Redistribution and use in source and binary forms, with or without
00008 // modification, are permitted provided that the following conditions are met:
00009 // 
00010 //  1. Redistributions of source code must retain the above copyright
00011 //     notice, this list of conditions and the following disclaimer.
00012 // 
00013 //  2. Redistributions in binary form must reproduce the above copyright
00014 //     notice, this list of conditions and the following disclaimer in the
00015 //     documentation and/or other materials provided with the distribution.
00016 // 
00017 //  3. The name of the authors may not be used to endorse or promote
00018 //     products derived from this software without specific prior written
00019 //     permission.
00020 // 
00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 // 
00032 // This file is part of the PLearn library. For more information on the PLearn
00033 // library, go to the PLearn Web site at www.plearn.org
00034 
00035 /* *******************************************************      
00036  * $Id: Profiler.h 9519 2008-10-03 18:39:38Z nouiz $
00037  * This file is part of the PLearn library.
00038  ******************************************************* */
00039 
00042 #ifndef PROFILER_INC
00043 #define PROFILER_INC
00044 
00045 
00046 #ifndef WIN32
00047 #define PROFILE
00048 #include <sys/times.h>
00049 #endif
00050 
00051 #include <plearn/base/general.h>
00052 #include <plearn/base/plerror.h>
00053 #include <plearn/base/stringutils.h>
00054 #include <plearn/io/PStream.h>
00055 
00056 namespace PLearn {
00057 using namespace std;
00058 
00100 class Profiler {
00101 public:
00102     class Stats {
00103     public:
00104         long frequency_of_occurence;         
00105         long wall_duration;                  
00106         long user_duration;                  
00107         long system_duration;                
00108         clock_t wall_last_start;             
00109         clock_t user_last_start;             
00110         clock_t system_last_start;           
00111         int nb_going;                       
00112       
00113         Stats()
00114             : frequency_of_occurence(0),
00115               wall_duration(0),
00116               user_duration(0),
00117               system_duration(0),
00118               wall_last_start(0),
00119               user_last_start(0),
00120               system_last_start(0),
00121               nb_going(0)
00122         { }
00123     };
00124 
00125 public:
00126 
00128     static void activate()
00129     {
00130 #ifdef WIN32
00131         PLERROR("In Profiler::activate - Profiling is not currently supported "
00132                 "under Windows");
00133 #endif
00134         active=true;
00135     }
00136 
00138     static void deactivate() { active=false; }
00139 
00141     static bool isActive() { return active; }
00142     
00144 #ifdef PROFILE
00145     static void start(const string& name_of_piece_of_code, const int max_nb_going=1);
00146 #else
00147     static inline void start(const string& name_of_piece_of_code, const int max_nb_going=1) { }
00148 #endif
00149 
00152 #ifdef PROFILE
00153     static void end(const string& name_of_piece_of_code);
00154 #else
00155     static inline void end(const string& name_of_piece_of_code) { } 
00156 #endif
00157 
00159 #if defined(PROFILE) && defined(PL_PROFILE)
00160     static void pl_profile_start(const string& name_of_piece_of_code, const int max_nb_going=1);
00161 #else
00162     static inline void pl_profile_start(const string& name_of_piece_of_code, const int max_nb_going=1) {}
00163 #endif
00164 
00166 #if defined(PROFILE) && defined(PL_PROFILE)
00167     static void pl_profile_end(const string& name_of_piece_of_code);
00168 #else
00169     static inline void pl_profile_end(const string& name_of_piece_of_code) { } 
00170 #endif
00171 
00173 #if defined(PROFILE) && defined(PL_PROFILE)
00174     static void pl_profile_activate();
00175 #else
00176     static inline void pl_profile_activate() {}
00177 #endif
00178 
00180 #if defined(PROFILE) && defined(PL_PROFILE)
00181     static void pl_profile_deactivate();
00182 #else
00183     static inline void pl_profile_deactivate() {}
00184 #endif
00185 
00187 #if defined(PROFILE) && defined(PL_PROFILE)
00188     static void pl_profile_report(ostream& out);
00189     static void pl_profile_report(PStream out);
00190 #else
00191     static inline void pl_profile_report(ostream& out) {}
00192     static inline void pl_profile_report(PStream out) {}
00193 #endif
00194 
00196 #if defined(PROFILE) && defined(PL_PROFILE)
00197     static void pl_profile_reportwall(ostream& out);
00198     static void pl_profile_reportwall(PStream out);
00199 #else
00200     static inline void pl_profile_reportwall(ostream& out) {}
00201     static inline void pl_profile_reportwall(PStream out) {}
00202 #endif
00203 
00205 #ifdef PROFILE
00206     static long ticksPerSecond() { return sysconf(_SC_CLK_TCK); }
00207 #else
00208     static long ticksPerSecond() { return 0; }
00209 #endif
00210 
00211 
00214     static const Stats& getStats(const string& name_of_piece_of_code);
00215 
00218     static void reset(const string& name_of_piece_of_code);
00219     
00222     static void report(ostream& out);
00223     static void report(PStream out);
00224 
00227     static void reportwall(ostream& out);
00228     static void reportwall(PStream out);
00229 
00230 protected:
00231     static map<string,Stats> codes_statistics;
00232     static struct tms t;
00233     static bool active;
00234     //return a thread save name. usefull for openmp code.
00235     static string get_omp_save_name(const string name_of_piece_of_code);
00236 };
00237 
00238 } // end of namespace PLearn
00239 
00240 #endif
00241 
00242 
00243 /*
00244   Local Variables:
00245   mode:c++
00246   c-basic-offset:4
00247   c-file-style:"stroustrup"
00248   c-file-offsets:((innamespace . 0)(inline-open . 0))
00249   indent-tabs-mode:nil
00250   fill-column:79
00251   End:
00252 */
00253 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines