All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
Profiler.h
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2008, Willow Garage, Inc.
00005 *  All rights reserved.
00006 *
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 *
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the Willow Garage nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 *
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 *********************************************************************/
00034 
00035 
00036 /* Author Ioan Sucan */
00037 
00038 #ifndef OMPL_UTIL_PROFILER_
00039 #define OMPL_UTIL_PROFILER_
00040 
00041 #define ENABLE_PROFILING 1
00042 
00043 #ifndef ENABLE_PROFILING
00044 
00048 #  ifdef NDEBUG
00049 #    define ENABLE_PROFILING 0
00050 #  else
00051 #    define ENABLE_PROFILING 1
00052 #  endif
00053 
00054 #endif
00055 
00056 #if ENABLE_PROFILING
00057 
00058 #include <map>
00059 #include <string>
00060 #include <iostream>
00061 #include <boost/thread.hpp>
00062 
00063 #include "ompl/util/Time.h"
00064 
00065 namespace ompl
00066 {
00067 
00074     class Profiler
00075     {
00076     public:
00077 
00079         class BeginBlock
00080         {
00081         public:
00082 
00083             BeginBlock(const std::string &name, Profiler *prof = Profiler::Instance()) : name_(name), prof_(prof)
00084             {
00085                 prof->begin(name);
00086             }
00087 
00088             ~BeginBlock(void)
00089             {
00090                 prof_->end(name_);
00091             }
00092 
00093         private:
00094 
00095             std::string  name_;
00096             Profiler    *prof_;
00097         };
00098 
00100         static Profiler* Instance(void);
00101 
00104         Profiler(bool printOnDestroy = false, bool autoStart = false) : running_(false), printOnDestroy_(printOnDestroy)
00105         {
00106             if (autoStart)
00107                 start();
00108         }
00109 
00111         ~Profiler(void)
00112         {
00113             if (printOnDestroy_ && !data_.empty())
00114                 status();
00115         }
00116 
00118         static void Start(void)
00119         {
00120             Instance()->start();
00121         }
00122 
00124         static void Stop(void)
00125         {
00126             Instance()->stop();
00127         }
00128 
00130         static void Clear(void)
00131         {
00132             Instance()->clear();
00133         }
00134 
00136         void start(void);
00137 
00139         void stop(void);
00140 
00142         void clear(void);
00143 
00145         static void Event(const std::string& name, const unsigned int times = 1)
00146         {
00147             Instance()->event(name, times);
00148         }
00149 
00151         void event(const std::string &name, const unsigned int times = 1);
00152 
00154         static void Begin(const std::string &name)
00155         {
00156             Instance()->begin(name);
00157         }
00158 
00160         static void End(const std::string &name)
00161         {
00162             Instance()->end(name);
00163         }
00164 
00166         void begin(const std::string &name);
00167 
00169         void end(const std::string &name);
00170 
00174         static void Status(std::ostream &out = std::cout, bool merge = true)
00175         {
00176             Instance()->status(out, merge);
00177         }
00178 
00182         void status(std::ostream &out = std::cout, bool merge = true);
00183 
00184     private:
00185 
00187         struct TimeInfo
00188         {
00189             TimeInfo(void) : total(0, 0, 0, 0), shortest(boost::posix_time::pos_infin), longest(boost::posix_time::neg_infin), parts(0)
00190             {
00191             }
00192 
00194             time::duration    total;
00195 
00197             time::duration    shortest;
00198 
00200             time::duration    longest;
00201 
00203             unsigned long int parts;
00204 
00206             time::point       start;
00207 
00209             void set(void)
00210             {
00211                 start = time::now();
00212             }
00213 
00215             void update(void)
00216             {
00217                 const time::duration &dt = time::now() - start;
00218                 if (dt > longest)
00219                     longest = dt;
00220                 if (dt < shortest)
00221                     shortest = dt;
00222                 total = total + dt;
00223                 ++parts;
00224             }
00225         };
00226 
00228         struct PerThread
00229         {
00231             std::map<std::string, unsigned long int> events;
00232 
00234             std::map<std::string, TimeInfo>          time;
00235         };
00236 
00237         void printThreadInfo(std::ostream &out, const PerThread &data);
00238 
00239         boost::mutex                           lock_;
00240         std::map<boost::thread::id, PerThread> data_;
00241         TimeInfo                               tinfo_;
00242         bool                                   running_;
00243         bool                                   printOnDestroy_;
00244 
00245     };
00246 }
00247 
00248 #else
00249 
00250 #include <string>
00251 #include <iostream>
00252 
00253 /* If profiling is disabled, provide empty implementations for the
00254    public functions */
00255 namespace ompl
00256 {
00257 
00258     class Profiler
00259     {
00260     public:
00261 
00262         class BeginBlock
00263         {
00264         public:
00265 
00266             BeginBlock(const std::string &, Profiler *dummy = NULL)
00267             {
00268             }
00269 
00270             ~BeginBlock(void)
00271             {
00272             }
00273         };
00274 
00275         static Profiler* Instance(void);
00276 
00277         Profiler(bool dummy1 = true, bool dummy2 = true)
00278         {
00279         }
00280 
00281         ~Profiler(void)
00282         {
00283         }
00284 
00285         static void Start(void)
00286         {
00287         }
00288 
00289         static void Stop(void)
00290         {
00291         }
00292 
00293         static void Clear(void)
00294         {
00295         }
00296 
00297         void start(void)
00298         {
00299         }
00300 
00301         void stop(void)
00302         {
00303         }
00304 
00305         void clear(void)
00306         {
00307         }
00308 
00309         static void Event(const std::string&, const unsigned int = 1)
00310         {
00311         }
00312 
00313         void event(const std::string &, const unsigned int = 1)
00314         {
00315         }
00316 
00317         static void Begin(const std::string &)
00318         {
00319         }
00320 
00321         static void End(const std::string &)
00322         {
00323         }
00324 
00325         void begin(const std::string &)
00326         {
00327         }
00328 
00329         void end(const std::string &)
00330         {
00331         }
00332 
00333         static void Status(std::ostream & = std::cout, bool = true)
00334         {
00335         }
00336 
00337         void status(std::ostream & = std::cout, bool = true)
00338         {
00339         }
00340     };
00341 }
00342 
00343 #endif
00344 
00345 
00346 #endif
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator