00001 //###################################################################### 00002 // 00003 // GraspIt! 00004 // Copyright (C) 2002-2009 Columbia University in the City of New York. 00005 // All rights reserved. 00006 // 00007 // GraspIt! is free software: you can redistribute it and/or modify 00008 // it under the terms of the GNU General Public License as published by 00009 // the Free Software Foundation, either version 3 of the License, or 00010 // (at your option) any later version. 00011 // 00012 // GraspIt! is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 // 00017 // You should have received a copy of the GNU General Public License 00018 // along with GraspIt!. If not, see <http://www.gnu.org/licenses/>. 00019 // 00020 // Author(s): Matei T. Ciocarlie 00021 // 00022 // $Id: profiling.cpp,v 1.6.4.1 2009/07/23 21:18:02 cmatei Exp $ 00023 // 00024 //###################################################################### 00025 00026 #ifndef _profiling_h_ 00027 #define _profiling_h_ 00028 00107 #include <iostream> 00108 #include <vector> 00109 #include <string> 00110 #include "assert.h" 00111 00112 namespace Profiling { 00113 class Profiler; 00114 inline Profiler &getProfiler(); 00115 } 00116 00117 #ifdef PROF_ENABLED 00118 00119 //declarations 00121 #define PROF_DECLARE(STR) namespace Profiling{extern const int STR = getProfiler().getNewIndex(#STR);} 00122 00123 #define PROF_EXTERN(STR) namespace Profiling{extern const int STR;} 00124 00125 //resetting 00127 #define PROF_RESET(STR) Profiling::getProfiler().reset(Profiling::STR); 00128 00129 #define PROF_RESET_ALL Profiling::getProfiler().resetAll(); 00130 00131 //counting 00133 #define PROF_COUNT(STR) Profiling::getProfiler().count(Profiling::STR); 00134 00135 //timing 00137 #define PROF_START_TIMER(STR) Profiling::getProfiler().startTimer(Profiling::STR); 00138 00139 #define PROF_STOP_TIMER(STR) Profiling::getProfiler().stopTimer(Profiling::STR); 00140 00141 #define PROF_TIMER_FUNC(STR) Profiling::FunctionTimer fctnTimer__##STR##__(Profiling::getProfiler(),Profiling::STR); 00142 00143 //printing 00145 #define PROF_PRINT(STR) Profiling::getProfiler().print(Profiling::STR); 00146 00147 #define PROF_PRINT_ALL Profiling::getProfiler().printAll(); 00148 00149 #else 00150 00151 #define PROF_DECLARE(STR) ; 00152 #define PROF_EXTERN(STR) ; 00153 #define PROF_RESET(STR) ; 00154 #define PROF_RESET_ALL ; 00155 #define PROF_COUNT(STR) ; 00156 #define PROF_START_TIMER(STR) ; 00157 #define PROF_STOP_TIMER(STR) ; 00158 #define PROF_TIMER_FUNC(STR) ; 00159 #define PROF_PRINT(STR) ; 00160 #define PROF_PRINT_ALL ; 00161 00162 #endif 00163 00164 //contains all the low-level calls for getting and processing system time 00165 #include "timer_calls.h" 00166 00167 namespace Profiling { 00168 00169 class ProfileInstance 00170 { 00171 private: 00172 int mCount; 00173 bool mRunning; 00174 std::string mName; 00176 00177 PROF_TIME_UNIT mStartTime; 00178 PROF_DURATION_UNIT mElapsedTime; 00179 public: 00180 ProfileInstance(); 00181 void setName(const char *name){mName = name;} 00182 00183 void count(){mCount++;} 00184 int getCount(){return mCount;} 00185 void reset() 00186 { 00187 mCount=0; 00188 PROF_RESET_DURATION(mElapsedTime); 00189 if (mRunning) 00190 { 00191 PROF_GET_TIME(mStartTime); 00192 } 00193 } 00194 void startTimer() 00195 { 00196 if (!mRunning) 00197 { 00198 PROF_GET_TIME(mStartTime); 00199 mRunning = true; 00200 } 00201 else 00202 { 00203 std::cerr << "Timer " << mName << " already running.\n"; 00204 } 00205 } 00206 void stopTimer() 00207 { 00208 if (mRunning) 00209 { 00210 PROF_TIME_UNIT currentTime; 00211 PROF_GET_TIME(currentTime); 00212 PROF_ADD_DURATION( mElapsedTime, mStartTime, currentTime); 00213 mRunning = false; 00214 } 00215 else 00216 { 00217 std::cerr << "Timer " << mName << " is not running.\n"; 00218 } 00219 } 00227 double getTotalTimeMicroseconds(); 00228 void print(); 00229 }; 00230 00231 class Profiler 00232 { 00233 private: 00234 int mNextIndex; 00235 int mSize; 00236 std::vector<ProfileInstance> mPI; 00237 #ifdef WIN32 00238 UINT64 COUNTS_PER_SEC; 00239 #endif 00240 public: 00241 Profiler(); 00242 ~Profiler(); 00243 00244 void resize(int size); 00245 int getNewIndex(const char *name); 00246 00247 void count(int index){mPI[index].count();} 00248 int getCount(int index){return mPI[index].getCount();} 00249 void reset(int index){mPI[index].reset();} 00250 void startTimer(int index){mPI[index].startTimer();} 00251 void stopTimer(int index){mPI[index].stopTimer();} 00252 void print(int index){mPI[index].print();} 00253 00254 void resetAll(); 00255 void printAll(); 00256 #ifdef WIN32 00257 UINT64 getCountsPerSec(){return COUNTS_PER_SEC;} 00258 #endif 00259 }; 00260 00261 Profiler& getProfiler() 00262 { 00263 //the one and only instance of the profiler 00264 static Profiler profInstance; 00265 return profInstance; 00266 } 00267 00268 class FunctionTimer 00269 { 00270 private: 00271 Profiler &mProfiler; 00272 int mIndex; 00273 public: 00274 FunctionTimer(Profiler &prof, int index) : mProfiler(prof), mIndex(index) 00275 { 00276 mProfiler.count(mIndex); 00277 mProfiler.startTimer(mIndex); 00278 } 00279 ~FunctionTimer(){mProfiler.stopTimer(mIndex);} 00280 }; 00281 00282 } 00283 00284 #endif