[084729c] | 1 | /*
|
---|
| 2 | * Chronos.hpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Mar 14, 2011
|
---|
| 5 | * Author: heber
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #ifndef CHRONOS_HPP_
|
---|
| 9 | #define CHRONOS_HPP_
|
---|
| 10 |
|
---|
| 11 | // include config.h
|
---|
| 12 | #ifdef HAVE_CONFIG_H
|
---|
| 13 | #include <config.h>
|
---|
| 14 | #endif
|
---|
| 15 |
|
---|
| 16 | #include <iosfwd>
|
---|
| 17 | #include <map>
|
---|
| 18 | #include <string>
|
---|
| 19 |
|
---|
| 20 | #include "CodePatterns/Singleton.hpp"
|
---|
| 21 |
|
---|
| 22 | class ChronosTest;
|
---|
| 23 |
|
---|
| 24 | class Chronos : public Singleton<Chronos>{
|
---|
| 25 | //!> Singleton pattern needs private access
|
---|
| 26 | friend class Singleton<Chronos>;
|
---|
| 27 | //!> unit tests needs access to private members
|
---|
| 28 | friend class ChronosTest;
|
---|
| 29 | //!> operator needs to access some internals
|
---|
| 30 | friend std::ostream& operator<<(std::ostream &ost, const Chronos &_time);
|
---|
| 31 | public :
|
---|
| 32 | //!> typedef for the map storing times per token
|
---|
| 33 | typedef std::map<const std::string, double> TimekeepingMap;
|
---|
| 34 |
|
---|
| 35 | /** Returns current kept time of function \a _name.
|
---|
| 36 | *
|
---|
| 37 | * @param _name name of function
|
---|
| 38 | * @return current amount of time passed for this function, 0 if unknown, -1 if currently running
|
---|
| 39 | */
|
---|
| 40 | double getTime(const std::string &_name) const;
|
---|
| 41 |
|
---|
| 42 | /** Resets time counter for this function \a _name to zero.
|
---|
| 43 | *
|
---|
| 44 | * @param _name name of function
|
---|
| 45 | */
|
---|
| 46 | void resetTime(const std::string &_name);
|
---|
| 47 |
|
---|
| 48 | /** Starts Timing for this function \a _name.
|
---|
| 49 | *
|
---|
| 50 | * @param _name name of function
|
---|
| 51 | */
|
---|
| 52 | void startTiming(const std::string &_name);
|
---|
| 53 |
|
---|
| 54 | /** Finishes Timing for this function \a _name.
|
---|
| 55 | *
|
---|
| 56 | * @param _name name of function
|
---|
| 57 | */
|
---|
| 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;
|
---|
| 65 |
|
---|
| 66 | /** Sums up total time accounted for.
|
---|
| 67 | *
|
---|
| 68 | * @return total time in seconds
|
---|
| 69 | */
|
---|
| 70 | double SumUpTotalTime() const;
|
---|
| 71 |
|
---|
| 72 | /** Sums up all functions accounted.
|
---|
| 73 | *
|
---|
| 74 | * @return number of functions
|
---|
| 75 | */
|
---|
| 76 | size_t SumUpTotalFunctions() const;
|
---|
| 77 |
|
---|
| 78 | protected:
|
---|
| 79 | /** Do not call this constructor directly, use getInstance() instead. */
|
---|
| 80 | Chronos();
|
---|
| 81 | /** Do not call this destructor directly, use purgeInstance() instead. */
|
---|
| 82 | ~Chronos();
|
---|
| 83 |
|
---|
| 84 | private:
|
---|
| 85 | /** Returns the current timer in seconds.
|
---|
| 86 | *
|
---|
| 87 | * This function is present to allow for changing of time measurements
|
---|
| 88 | * without the other functions noticing.
|
---|
| 89 | *
|
---|
| 90 | * \note We convert clock() by CLOCKS_PER_SEC.
|
---|
| 91 | * \note We convert struct tms::tms_utime by _SC_CLK_TCK via sysconf().
|
---|
| 92 | *
|
---|
| 93 | * @return current time in unit of seconds
|
---|
| 94 | */
|
---|
| 95 | double getCurrentTime() const;
|
---|
| 96 |
|
---|
| 97 | typedef std::pair<size_t, size_t> sec_ncsec_t;
|
---|
| 98 |
|
---|
| 99 | /** Returns the correct difference between split tuples (sec, nsec).
|
---|
| 100 | *
|
---|
| 101 | * \param _time1 first tuple
|
---|
| 102 | * \param _time2 second tuple
|
---|
| 103 | */
|
---|
| 104 | static
|
---|
| 105 | double calculateCorrectTimeDifference(
|
---|
| 106 | const sec_ncsec_t &_time1,
|
---|
| 107 | const sec_ncsec_t &_time2);
|
---|
| 108 |
|
---|
| 109 | //!> typedef for the map storing status of time keeping per token
|
---|
| 110 | typedef std::map<const std::string, double> TimerMap;
|
---|
| 111 |
|
---|
| 112 | //!> typedef for the map storing number of recursive calls to this token
|
---|
| 113 | typedef std::map<const std::string, size_t> TimerRecursionMap;
|
---|
| 114 |
|
---|
| 115 | //!> map storing times per token
|
---|
| 116 | TimekeepingMap AccountedTime;
|
---|
| 117 | //!> map storing time keeping status per token
|
---|
| 118 | TimerMap StartingTime;
|
---|
| 119 | //!> map storing level of recursion per token
|
---|
| 120 | TimerRecursionMap RecursionMap;
|
---|
| 121 |
|
---|
| 122 | #ifdef HAVE_TIME_H
|
---|
| 123 | timespec basetime;
|
---|
| 124 | #else
|
---|
| 125 | #ifdef HAVE_SYS_TIME_H
|
---|
| 126 | struct timezone basetime;
|
---|
| 127 | #else
|
---|
| 128 | #ifdef HAVE_SYS_TIMES_H
|
---|
| 129 | struct tms *basetime;
|
---|
| 130 | #endif
|
---|
| 131 | #endif
|
---|
| 132 | #endif
|
---|
| 133 | };
|
---|
| 134 |
|
---|
| 135 | /** Output for Chronos.
|
---|
| 136 | *
|
---|
| 137 | * @param ost output stream
|
---|
| 138 | * @param _time Chronos to print
|
---|
| 139 | * @return reference to given output stream for concatenation
|
---|
| 140 | */
|
---|
| 141 | std::ostream& operator<<(std::ostream &ost, const Chronos &_time);
|
---|
| 142 |
|
---|
| 143 | // inline functions
|
---|
| 144 |
|
---|
| 145 | inline
|
---|
| 146 | const Chronos::TimekeepingMap& Chronos::getTimekeepingMap() const
|
---|
| 147 | {
|
---|
| 148 | return AccountedTime;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | #endif /* CHRONOS_HPP_ */
|
---|