| [93abe8] | 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> | 
|---|
| [3f06bb] | 18 | #include <string> | 
|---|
| [93abe8] | 19 |  | 
|---|
| [9b8fa4] | 20 | #include "CodePatterns/Singleton.hpp" | 
|---|
| [93abe8] | 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; | 
|---|
| [8e24ef] | 29 | //!> operator needs to access some internals | 
|---|
|  | 30 | friend std::ostream& operator<<(std::ostream &ost, const Chronos &_time); | 
|---|
| [93abe8] | 31 | public : | 
|---|
| [3f06bb] | 32 | //!> typedef for the map storing times per token | 
|---|
|  | 33 | typedef std::map<const std::string, double> TimekeepingMap; | 
|---|
| [93abe8] | 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 | */ | 
|---|
| [3f06bb] | 40 | double getTime(const std::string &_name) const; | 
|---|
| [93abe8] | 41 |  | 
|---|
|  | 42 | /** Resets time counter for this function \a _name to zero. | 
|---|
|  | 43 | * | 
|---|
|  | 44 | * @param _name name of function | 
|---|
|  | 45 | */ | 
|---|
| [3f06bb] | 46 | void resetTime(const std::string &_name); | 
|---|
| [93abe8] | 47 |  | 
|---|
|  | 48 | /** Starts Timing for this function \a _name. | 
|---|
|  | 49 | * | 
|---|
|  | 50 | * @param _name name of function | 
|---|
|  | 51 | */ | 
|---|
| [3f06bb] | 52 | void startTiming(const std::string &_name); | 
|---|
| [93abe8] | 53 |  | 
|---|
|  | 54 | /** Finishes Timing for this function \a _name. | 
|---|
|  | 55 | * | 
|---|
|  | 56 | * @param _name name of function | 
|---|
|  | 57 | */ | 
|---|
| [3f06bb] | 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; | 
|---|
| [93abe8] | 65 |  | 
|---|
|  | 66 | /** Sums up total time accounted for. | 
|---|
|  | 67 | * | 
|---|
| [8e24ef] | 68 | * @return total time in seconds | 
|---|
| [93abe8] | 69 | */ | 
|---|
| [8e24ef] | 70 | double SumUpTotalTime() const; | 
|---|
| [93abe8] | 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: | 
|---|
| [8e24ef] | 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. | 
|---|
| [760f97c] | 91 | * \note We convert struct tms::tms_utime by _SC_CLK_TCK via sysconf(). | 
|---|
| [8e24ef] | 92 | * | 
|---|
|  | 93 | * @return current time in unit of seconds | 
|---|
|  | 94 | */ | 
|---|
|  | 95 | double getCurrentTime() const; | 
|---|
|  | 96 |  | 
|---|
| [3f06bb] | 97 | //!> typedef for the map storing status of time keeping per token | 
|---|
|  | 98 | typedef std::map<const std::string, double> TimerMap; | 
|---|
| [93abe8] | 99 |  | 
|---|
| [3f06bb] | 100 | //!> typedef for the map storing number of recursive calls to this token | 
|---|
|  | 101 | typedef std::map<const std::string, size_t> TimerRecursionMap; | 
|---|
| [93abe8] | 102 |  | 
|---|
| [3f06bb] | 103 | //!> map storing times per token | 
|---|
| [93abe8] | 104 | TimekeepingMap AccountedTime; | 
|---|
| [3f06bb] | 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 | 
|---|
| [93abe8] | 121 | }; | 
|---|
|  | 122 |  | 
|---|
|  | 123 | /** Output for Chronos. | 
|---|
|  | 124 | * | 
|---|
|  | 125 | * @param ost output stream | 
|---|
|  | 126 | * @param _time Chronos to print | 
|---|
|  | 127 | * @return reference to given output stream for concatenation | 
|---|
|  | 128 | */ | 
|---|
|  | 129 | std::ostream& operator<<(std::ostream &ost, const Chronos &_time); | 
|---|
|  | 130 |  | 
|---|
| [3f06bb] | 131 | // inline functions | 
|---|
|  | 132 |  | 
|---|
|  | 133 | inline | 
|---|
|  | 134 | const Chronos::TimekeepingMap& Chronos::getTimekeepingMap() const | 
|---|
|  | 135 | { | 
|---|
|  | 136 | return AccountedTime; | 
|---|
|  | 137 | } | 
|---|
|  | 138 |  | 
|---|
| [93abe8] | 139 | #endif /* CHRONOS_HPP_ */ | 
|---|