- Timestamp:
- Mar 2, 2013, 10:45:46 PM (13 years ago)
- Children:
- b9273a
- Parents:
- 8f60da
- git-author:
- Frederik Heber <heber@…> (03/01/13 13:41:05)
- git-committer:
- Frederik Heber <heber@…> (03/02/13 22:45:46)
- Location:
- src
- Files:
-
- 8 edited
-
CodePatterns/Chronos.hpp (modified) (8 diffs)
-
Helpers/Chronos.cpp (modified) (5 diffs)
-
Helpers/Makefile.am (modified) (1 diff)
-
Helpers/unittests/ChronosUnitTest.cpp (modified) (4 diffs)
-
Helpers/unittests/Makefile.am (modified) (1 diff)
-
Helpers/unittests/stubs/ChronosStub.cpp (modified) (2 diffs)
-
Makefile.am (modified) (1 diff)
-
unittests/Makefile.am (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
src/CodePatterns/Chronos.hpp
r8f60da r3f06bb 16 16 #include <iosfwd> 17 17 #include <map> 18 #include <string> 18 19 19 20 #include "CodePatterns/Singleton.hpp" … … 29 30 friend std::ostream& operator<<(std::ostream &ost, const Chronos &_time); 30 31 public : 32 //!> typedef for the map storing times per token 33 typedef std::map<const std::string, double> TimekeepingMap; 31 34 32 35 /** Returns current kept time of function \a _name. … … 35 38 * @return current amount of time passed for this function, 0 if unknown, -1 if currently running 36 39 */ 37 double getTime(const std::string _name) const;40 double getTime(const std::string &_name) const; 38 41 39 42 /** Resets time counter for this function \a _name to zero. … … 41 44 * @param _name name of function 42 45 */ 43 void resetTime(const std::string _name);46 void resetTime(const std::string &_name); 44 47 45 48 /** Starts Timing for this function \a _name. … … 47 50 * @param _name name of function 48 51 */ 49 void startTiming(const std::string _name);52 void startTiming(const std::string &_name); 50 53 51 54 /** Finishes Timing for this function \a _name. … … 53 56 * @param _name name of function 54 57 */ 55 void endTiming(const std::string _name); 58 void endTiming(const std::string &_name); 59 60 /** Returns const reference to time keeping map. 61 * 62 * \return const ref to timekeeping map 63 */ 64 const TimekeepingMap& getTimekeepingMap() const; 56 65 57 66 /** Sums up total time accounted for. … … 86 95 double getCurrentTime() const; 87 96 97 //!> typedef for the map storing status of time keeping per token 98 typedef std::map<const std::string, double> TimerMap; 88 99 89 typedef std::map<const std::string, double> TimekeepingMap;90 typedef std::map<const std::string, double> TimerStatusMap;100 //!> typedef for the map storing number of recursive calls to this token 101 typedef std::map<const std::string, size_t> TimerRecursionMap; 91 102 103 //!> map storing times per token 92 104 TimekeepingMap AccountedTime; 93 TimerStatusMap TimeRunning; 105 //!> map storing time keeping status per token 106 TimerMap StartingTime; 107 //!> map storing level of recursion per token 108 TimerRecursionMap RecursionMap; 109 110 #ifdef HAVE_TIME_H 111 timespec basetime; 112 #else 113 #ifdef HAVE_SYS_TIME_H 114 struct timezone basetime; 115 #else 116 #ifdef HAVE_SYS_TIMES_H 117 struct tms *basetime; 118 #endif 119 #endif 120 #endif 94 121 }; 95 122 … … 102 129 std::ostream& operator<<(std::ostream &ost, const Chronos &_time); 103 130 131 // inline functions 132 133 inline 134 const Chronos::TimekeepingMap& Chronos::getTimekeepingMap() const 135 { 136 return AccountedTime; 137 } 138 104 139 #endif /* CHRONOS_HPP_ */ -
src/Helpers/Chronos.cpp
r8f60da r3f06bb 39 39 40 40 Chronos::Chronos() 41 {} 41 { 42 // get time and store it internally as base time 43 #ifdef HAVE_TIME_H 44 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &basetime); 45 #else 46 #ifdef HAVE_SYS_TIME_H 47 struct timezone timezone1; 48 gettimeofday(&basetime, &timezone1); 49 #else 50 #ifdef HAVE_SYS_TIMES_H 51 struct tms *basetime = new tms; 52 times(basetime); 53 #endif 54 #endif 55 #endif 56 } 42 57 43 58 Chronos::~Chronos() 44 {} 45 46 double Chronos::getTime(const std::string _name) const 59 { 60 #ifndef HAVE_TIME_H 61 #ifndef HAVE_SYS_TIME_H 62 #ifdef HAVE_SYS_TIMES_H 63 delete basetime; 64 #endif 65 #endif 66 #endif 67 } 68 69 double Chronos::getTime(const std::string &_name) const 47 70 { 48 71 // only those functions have a time that have run already 49 if ( TimeRunning.count(_name)) {72 if (AccountedTime.count(_name) != 0) { 50 73 // return -1 if function is currently running 51 if ( TimeRunning.count(_name) != 0.)74 if (StartingTime.count(_name) == 0.) 52 75 return AccountedTime.at(_name); 53 76 else … … 57 80 } 58 81 59 void Chronos::resetTime(const std::string _name) 60 { 61 if (TimeRunning.count(_name)) { 82 void Chronos::resetTime(const std::string &_name) 83 { 84 // set accounted time to zero 85 if (AccountedTime.count(_name) != 0) { 62 86 AccountedTime[_name] = 0.; 63 87 } 64 } 65 66 void Chronos::startTiming(const std::string _name) 88 // and end if it's currently running 89 StartingTime.erase(_name); 90 RecursionMap.erase(_name); 91 } 92 93 void Chronos::startTiming(const std::string &_name) 67 94 { 68 95 // start time keeping 69 TimeRunning[_name] = getCurrentTime(); 96 if ((RecursionMap.count(_name) == 0) || (RecursionMap[_name] == 0)) { 97 StartingTime[_name] = getCurrentTime(); 98 RecursionMap[_name] = 1; 99 } else { 100 ++RecursionMap[_name]; 101 } 70 102 } 71 103 72 104 double Chronos::getCurrentTime() const 73 105 { 74 #ifdef HAVE_SYS_TIMES_H 106 #ifdef HAVE_TIME_H 107 // clock_gettime gives nanoseconds accuracy 108 timespec time1; 109 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1); 110 double currenttime; 111 if (time1.tv_nsec < basetime.tv_nsec) 112 currenttime = (time1.tv_sec - basetime.tv_sec - 1) 113 + (basetime.tv_nsec - time1.tv_nsec) * 1.e-9; 114 else 115 currenttime = (time1.tv_sec - basetime.tv_sec) 116 + (time1.tv_nsec - basetime.tv_nsec) * 1.e-9; 117 #else 118 #ifdef HAVE_SYS_TIME_H 119 struct timezone timezone1; 120 timeval time1; 121 // gettimeofday gives microseconds accuracy 122 gettimeofday(&time1, &timezone1); 123 double currenttime; 124 if (time1.tv_usec < basetime.tv_usec) 125 currenttime = (time1.tv_sec - basetime.tv_sec - 1) 126 + (basetime.tv_usec - time1.tv_usec) * 1.e-6; 127 else 128 currenttime = (time1.tv_sec - basetime.tv_sec) 129 + (time1.tv_usec - basetime.tv_usec) * 1.e-6; 130 #else 131 #ifdef HAVE_SYS_TIMES_H 132 // clock is only accurate up to milliseconds 75 133 struct tms *buffer = new tms; 76 134 double currenttime; 77 135 if (times(buffer) != (clock_t)(-1)) 78 currenttime = ((double)buffer->tms_utime/(double)sysconf(_SC_CLK_TCK)); 136 currenttime = 137 (double)(buffer->tms_utime - basetime->tms_utime)/(double)sysconf(_SC_CLK_TCK); 79 138 else 80 139 currenttime = 0.; 81 140 delete buffer; 82 141 #else 83 const double currenttime = (clock()/(double)CLOCKS_PER_SEC); 142 // no time keeping possible 143 const double currenttime = 0.; 144 #endif 145 #endif 84 146 #endif 85 147 //std::cout << "Current time is " << currenttime << std::endl; … … 87 149 } 88 150 89 void Chronos::endTiming(const std::string _name) 90 { 151 void Chronos::endTiming(const std::string &_name) 152 { 153 // check whether we are the topmost function, return if not 154 if (--RecursionMap[_name] != 0) 155 return; 156 157 // if present 158 ASSERT(StartingTime.count(_name), "Chronos::endTiming() - no timer under " 159 +_name+" running."); 160 ASSERT(RecursionMap.count(_name), "Chronos::endTiming() - negative recursion level for " 161 +_name+"."); 162 163 // finish time keeping 91 164 const double endtime = getCurrentTime(); 92 const double starttime = TimeRunning[_name]; 93 94 // if present 95 ASSERT(TimeRunning.count(_name), "Chronos::endTiming() - no timer under "+_name+" running."); 96 // finish time keeping 165 const double starttime = StartingTime[_name]; 97 166 const double RunTime = ((double)endtime - starttime); 98 TimekeepingMap::iterator iter = AccountedTime.find(_name); 99 if (iter != AccountedTime.end()) 167 if (AccountedTime.count(_name) != 0) 100 168 AccountedTime[_name] += RunTime; 101 169 else … … 103 171 104 172 // and zero for next run 105 TimeRunning[_name] = 0.;173 StartingTime.erase(_name); 106 174 } 107 175 … … 119 187 size_t Chronos::SumUpTotalFunctions() const 120 188 { 121 return TimeRunning.size();189 return AccountedTime.size(); 122 190 } 123 191 -
src/Helpers/Makefile.am
r8f60da r3f06bb 44 44 $(BOOST_THREAD_LDFLAGS) 45 45 libCodePatterns_Helpers_la_LIBADD = \ 46 $(LIBRT) \ 46 47 $(BOOST_THREAD_LIBS) 47 48 libCodePatterns_Helpers_debug_la_LIBADD = \ 49 $(LIBRT) \ 48 50 $(BOOST_THREAD_LIBS) 49 51 -
src/Helpers/unittests/ChronosUnitTest.cpp
r8f60da r3f06bb 54 54 } 55 55 56 static size_t level = 0; 57 58 void dummyRecursion() 59 { 60 if (level > 2) 61 return; 62 ++level; 63 Chronos::getInstance().startTiming(__func__); 64 for (int i=0;i<10;++i) { 65 std::cout << ""; 66 dummyRecursion(); 67 } 68 Chronos::getInstance().endTiming(__func__); 69 --level; 70 } 71 56 72 57 73 void ChronosTest::setUp() … … 78 94 void ChronosTest::dummyTest() 79 95 { 80 double timings[ 4];96 double timings[5]; 81 97 // first dummy 82 98 dummy(); 83 CPPUNIT_ASSERT( Chronos::getInstance().TimeRunning.find(std::string("dummy")) 84 != Chronos::getInstance().TimeRunning.end() ); 99 CPPUNIT_ASSERT( Chronos::getInstance().StartingTime.find(std::string("dummy")) 100 == Chronos::getInstance().StartingTime.end() ); 101 CPPUNIT_ASSERT( Chronos::getInstance().AccountedTime.find(std::string("dummy")) 102 != Chronos::getInstance().AccountedTime.end() ); 85 103 CPPUNIT_ASSERT_EQUAL( (size_t) 1, Chronos::getInstance().SumUpTotalFunctions() ); 86 104 timings[0] = Chronos::getInstance().AccountedTime[std::string("dummy")]; 87 105 std::cout << "Timing[0]: " << timings[0] << std::endl; 88 106 CPPUNIT_ASSERT(timings[0] > 0.); 107 108 // second call goes to same entry 89 109 dummy(); 90 // second call goes to same entry91 110 CPPUNIT_ASSERT_EQUAL( (size_t) 1, Chronos::getInstance().SumUpTotalFunctions() ); 92 111 timings[1] = Chronos::getInstance().AccountedTime[std::string("dummy")] - timings[0]; … … 94 113 CPPUNIT_ASSERT(timings[1] > 0.); 95 114 96 97 115 // second dummy 98 116 dummy_two(); 99 CPPUNIT_ASSERT( Chronos::getInstance(). TimeRunning.find(std::string("dummy_two"))100 != Chronos::getInstance(). TimeRunning.end() );117 CPPUNIT_ASSERT( Chronos::getInstance().AccountedTime.find(std::string("dummy_two")) 118 != Chronos::getInstance().AccountedTime.end() ); 101 119 CPPUNIT_ASSERT_EQUAL( (size_t) 2, Chronos::getInstance().SumUpTotalFunctions() ); 102 120 timings[2] = Chronos::getInstance().AccountedTime[std::string("dummy_two")]; 103 121 std::cout << "Timing[2]: " << timings[2] << std::endl; 104 122 CPPUNIT_ASSERT(timings[2] > 0.); 123 124 // recursive dummy 125 dummyRecursion(); 126 CPPUNIT_ASSERT( Chronos::getInstance().RecursionMap.find(std::string("dummyRecursion")) 127 != Chronos::getInstance().RecursionMap.end() ); 128 CPPUNIT_ASSERT( Chronos::getInstance().AccountedTime.find(std::string("dummyRecursion")) 129 != Chronos::getInstance().AccountedTime.end() ); 130 CPPUNIT_ASSERT_EQUAL( (size_t) 3, Chronos::getInstance().SumUpTotalFunctions() ); 131 timings[3] = Chronos::getInstance().AccountedTime[std::string("dummyRecursion")]; 132 std::cout << "Timing[3]: " << timings[3] << std::endl; 133 CPPUNIT_ASSERT(timings[3] > 0.); 105 134 106 135 // "inline" dummy … … 111 140 Chronos::getInstance().endTiming("dummy_three"); 112 141 } 113 CPPUNIT_ASSERT( Chronos::getInstance(). TimeRunning.find(std::string("dummy_three"))114 != Chronos::getInstance(). TimeRunning.end() );115 CPPUNIT_ASSERT_EQUAL( (size_t) 3, Chronos::getInstance().SumUpTotalFunctions() );116 timings[ 3] = Chronos::getInstance().AccountedTime[std::string("dummy_three")];117 std::cout << "Timing[ 3]: " << timings[3] << std::endl;118 CPPUNIT_ASSERT(timings[ 3] > 0.);142 CPPUNIT_ASSERT( Chronos::getInstance().AccountedTime.find(std::string("dummy_three")) 143 != Chronos::getInstance().AccountedTime.end() ); 144 CPPUNIT_ASSERT_EQUAL( (size_t) 4, Chronos::getInstance().SumUpTotalFunctions() ); 145 timings[4] = Chronos::getInstance().AccountedTime[std::string("dummy_three")]; 146 std::cout << "Timing[4]: " << timings[4] << std::endl; 147 CPPUNIT_ASSERT(timings[4] > 0.); 119 148 120 149 // check summing of times 121 CPPUNIT_ASSERT( fabs(timings[0] + timings[1] + timings[2] + timings[3] - Chronos::getInstance().SumUpTotalTime()) < numeric_limits<double>::epsilon());150 CPPUNIT_ASSERT( fabs(timings[0] + timings[1] + timings[2] + timings[3] + timings[4]- Chronos::getInstance().SumUpTotalTime()) < numeric_limits<double>::epsilon()); 122 151 123 152 std::cout << Chronos::getInstance() << std::endl; -
src/Helpers/unittests/Makefile.am
r8f60da r3f06bb 46 46 $(top_srcdir)/src/CodePatterns/Info.hpp 47 47 ChronosUnitTest_LDADD = \ 48 ../Helpers/libCodePatterns-Helpers-debug.la 48 ../Helpers/libCodePatterns-Helpers-debug.la \ 49 $(LIBRT) 49 50 50 51 InfoUnitTest_SOURCES = UnitTestMain.cpp \ -
src/Helpers/unittests/stubs/ChronosStub.cpp
r8f60da r3f06bb 28 28 {} 29 29 30 double Chronos::getTime(const std::string _name) const30 double Chronos::getTime(const std::string &_name) const 31 31 { 32 32 return 0.; 33 33 } 34 34 35 void Chronos::resetTime(const std::string _name)35 void Chronos::resetTime(const std::string &_name) 36 36 {} 37 37 38 void Chronos::startTiming(const std::string _name)38 void Chronos::startTiming(const std::string &_name) 39 39 {} 40 40 … … 44 44 } 45 45 46 void Chronos::endTiming(const std::string _name)46 void Chronos::endTiming(const std::string &_name) 47 47 {} 48 48 -
src/Makefile.am
r8f60da r3f06bb 58 58 Patterns/libCodePatterns-Patterns.la \ 59 59 Observer/libCodePatterns-Observer.la \ 60 Helpers/libCodePatterns-Helpers.la 60 Helpers/libCodePatterns-Helpers.la \ 61 $(LIBRT) 61 62 libCodePatterns_debug_la_LIBADD = \ 62 63 Patterns/libCodePatterns-Patterns-debug.la \ 63 64 Observer/libCodePatterns-Observer-debug.la \ 64 Helpers/libCodePatterns-Helpers-debug.la 65 Helpers/libCodePatterns-Helpers-debug.la \ 66 $(LIBRT) 65 67 66 68 libCodePatterns_la_CPPFLAGS = -DNDEBUG -DNO_MEMDEBUG $(AM_CPPFLAGS) -
src/unittests/Makefile.am
r8f60da r3f06bb 29 29 ../Patterns/libCodePatterns-Patterns-debug.la \ 30 30 ../Observer/libCodePatterns-Observer-debug.la \ 31 ../Helpers/libCodePatterns-Helpers-debug.la 31 ../Helpers/libCodePatterns-Helpers-debug.la \ 32 $(LIBRT) 32 33 33 34 #AUTOMAKE_OPTIONS = parallel-tests
Note:
See TracChangeset
for help on using the changeset viewer.
