1 /* Copyright (C) 2014 InfiniDB, Inc. 2 3 This program is free software; you can redistribute it and/or 4 modify it under the terms of the GNU General Public License 5 as published by the Free Software Foundation; version 2 of 6 the License. 7 8 This program is distributed in the hope that it will be useful, 9 but WITHOUT ANY WARRANTY; without even the implied warranty of 10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License 14 along with this program; if not, write to the Free Software 15 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 MA 02110-1301, USA. */ 17 18 // 19 // $Id$ 20 // 21 // Timer class used to spit out times and percentages. Temporarily lives in joblist. 22 // This is strictly a debugging utility class that accumulates times between .start() and 23 // .stop() calls for a particular string. It can be useful in tdrivers. 24 // 25 // It outputs a report when finish() is called with 26 // the total times by string and their percentage of the total. The total is tracked from 27 // the first time start() is called until finish() is called. You should always match your 28 // start and stop calls up. 29 // 30 // This works fine as long as you don't use it with huge numbers of calls. If you call start 31 // and stop too many times the overhead of the StopWatch class will start factoring in to your 32 // results. 33 // 34 // How to use: 35 // 36 // StopWatch timer; 37 // timer.start("Loop only"); 38 // for(int i = 0; i < 6999075; i++) 39 // { 40 // } 41 // timer.stop("Loop only"); 42 // 43 // timer.start("Loop Plus"); 44 // for(int i = 0; i < 100000; i++) 45 // { 46 // timer.start("Inside loop"); 47 // timer.stop("Inside loop"); 48 // } 49 // timer.stop("Loop Plus"); 50 // timer.finish(); 51 // 52 // Produces this: 53 // 54 // Seconds Percentage Calls Description 55 // 0.02865 9.377% 1 Loop only 56 // 0.27680 90.59% 1 Loop Plus 57 // 0.12138 39.72% 100000 Inside loop 58 // 59 // 0.30553 100 % 1 Total 60 // 61 // Note that you can have overlapping timers which will make your percentages add up to more than 100%. 62 #ifndef LOGGING_STOPWATCH_H 63 #define LOGGING_STOPWATCH_H 64 #include <iostream> 65 #include <fstream> 66 #include <list> 67 #include <sstream> 68 #include <time.h> 69 #include <sys/time.h> 70 #include <vector> 71 #include <stdexcept> 72 #include <map> 73 #include <unistd.h> 74 75 namespace logging 76 { 77 78 class StopWatch 79 { 80 public: 81 void start(const std::string& message); 82 bool stop(const std::string& message, const int limit); 83 void stop(const std::string& message); 84 void finish(); 85 isActive()86 bool isActive() 87 { 88 return fOpenCalls > 0; 89 } StopWatch()90 StopWatch() : fStarted(false), fId(-1), fOpenCalls(0), fOutputToFile(false), fLogFile("") {}; StopWatch(int id)91 StopWatch(int id) : fStarted(false), fId(id), fOpenCalls(0), fOutputToFile(false), fLogFile("") {}; StopWatch(const std::string & fileName)92 StopWatch(const std::string& fileName) : fStarted(false), fId(-1), fOpenCalls(0), fOutputToFile(true), fLogFile(fileName) {} 93 struct ::timeval fTvLast; getId()94 int getId() 95 { 96 return fId; 97 } 98 99 private: 100 class ProcessStats 101 { 102 public: 103 104 std::string fProcess; 105 struct timeval fTvProcessStarted; 106 double fTotalSeconds; 107 int64_t fStartCount; 108 int64_t fStopCount; 109 ProcessStats()110 ProcessStats() : fProcess(""), fTotalSeconds(0.0), fStartCount(0), fStopCount(0) {}; 111 processStart()112 void processStart() 113 { 114 gettimeofday(&fTvProcessStarted, 0); 115 fStartCount++; 116 } 117 processStop()118 void processStop() 119 { 120 struct timeval tvStop; 121 gettimeofday(&tvStop, 0); 122 fStopCount++; 123 fTotalSeconds += 124 (tvStop.tv_sec + (tvStop.tv_usec / 1000000.0)) - 125 (fTvProcessStarted.tv_sec + (fTvProcessStarted.tv_usec / 1000000.0)); 126 127 } 128 }; 129 130 struct timeval fTvStart; 131 std::vector <ProcessStats> fProcessStats; 132 bool fStarted; 133 int fId; 134 int fOpenCalls; 135 bool fOutputToFile; 136 std::string fLogFile; 137 }; 138 139 } // end of logging namespace 140 141 #endif // STOPWATCH_H 142