Leviathan  0.8.0.0
Leviathan game engine
TimingMonitor.cpp
Go to the documentation of this file.
1 // ------------------------------------ //
2 #include "TimingMonitor.h"
3 
4 #include "Define.h"
5 #include "../TimeIncludes.h"
6 #include "../Utility/Convert.h"
7 using namespace Leviathan;
8 using namespace std;
9 // ------------------------------------ //
10 void Leviathan::TimingMonitor::StartTiming(const std::string& name,
11  int style /*= TIMINGMONITOR_STYLE_RESULT_DEFAULT*/)
12 {
13  Timers.push_back(shared_ptr<TimingMonitorClock>(new TimingMonitorClock(name, style)));
14 }
15 
16 int Leviathan::TimingMonitor::GetCurrentElapsed(const std::string& name){
17  // loop through timers and get right based on name //
18  for(unsigned int i = 0; i < Timers.size(); i++){
19  if(Timers[i]->Name == name){
20  // "end" the timer and return it's result which is the elapsed time //
21  return Timers[i]->EndMonitoring();
22  }
23  }
24  return -1;
25 }
26 
27 int Leviathan::TimingMonitor::StopTiming(const std::string& name, bool printoutput /*= true*/){
28 
29  // loop through timers and get right based on name //
30  for(size_t i = 0; i < Timers.size(); i++){
31  if(Timers[i]->Name == name){
32  // "end" the timer and return it's result which is the elapsed time //
33  // if specified print to log //
34  int time = Timers[i]->EndMonitoring();
35 
36  if(printoutput){
37  Logger::Get()->Info("TimingMonitor: Timer \""+name+
38  "\" stopped elapsed "+Convert::ToString(time/1000000.f)+" s ("+
39  Convert::ToString(time)+" micro seconds)");
40  }
41 
42  // remove the timer because memory might be filled with stuff //
43  // smart pointer //
44  Timers.erase(Timers.begin()+i);
45 
46  return time;
47  }
48  }
49 
50  return -1;
51 }
52 // ------------------------------------ //
54  return Timers.size();
55 }
56 // ------------------------------------ //
57 Leviathan::TimingMonitor::TimingMonitor(){
58 
59 }
60 
61 Leviathan::TimingMonitor::~TimingMonitor(){
62 
63 }
64 
66 
67  for(size_t i = 0; i < Timers.size(); i++){
68  if(i == 0)
69  Logger::Get()->Info("TimingMonitor: leaked timers! names:");
70  Logger::Get()->Write(Timers[i]->Name);
71  }
72  // just clear all timers vector and they will delete automatically //
73  Timers.clear();
74 }
75 
76 // ------------------------------------ //
77 vector<shared_ptr<TimingMonitorClock>> Leviathan::TimingMonitor::Timers;
78 // ---------------- TimingMonitorClock -------------------- //
80  // get end time and calculate duration //
81  EndTime = Time::GetTimeMicro64();
82  CurrentElapsed = (int)(EndTime-StartTime);
83  return CurrentElapsed;
84 }
85 
86 Leviathan::TimingMonitorClock::TimingMonitorClock(const std::string& name, int style){
87  EndTime = -1;
88  Name = name;
89 
90  CurrentElapsed = 0;
91  StartTime = Time::GetTimeMicro64();
92  Style = style;
93 }
94 // ---------------- ScopeTimer -------------------- //
95 DLLEXPORT Leviathan::ScopeTimer::ScopeTimer(const std::string& source){
96  // create unique name for this timer //
97  CurID++;
99  Source = source;
100  // start timer for this object //
102 }
103 
105  // kill this timer //
106  int ElapsedTime = TimingMonitor::StopTiming(TimerName, false);
107 
108  // print data //
109  Logger::Get()->Info("ScopeTimer: "+Source+" Stopped elapsed: "+
110  Convert::ToString(ElapsedTime/1000000.f)+" s ("+
111  Convert::ToString(ElapsedTime)+" micro seconds)");
112 }
113 
DLLEXPORT void Write(const std::string &data) override
Definition: Logger.cpp:113
DLLEXPORT void Info(const std::string &data) override
Definition: Logger.cpp:164
static DLLEXPORT void ClearTimers()
static DLLEXPORT int GetCurrentElapsed(const std::string &name)
static DLLEXPORT void StartTiming(const std::string &name, int style=TIMINGMONITOR_STYLE_RESULT_DEFAULT)
static DLLEXPORT int64_t GetTimeMicro64()
static DLLEXPORT int StopTiming(const std::string &name, bool printoutput=true)
static std::string ToString(const T &val)
Definition: Convert.h:72
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
#define DLLEXPORT
Definition: Include.h:84
#define TIMINGMONITOR_STYLE_RESULT_NONE
Definition: TimingMonitor.h:10
DLLEXPORT ScopeTimer(const std::string &source)
TimingMonitorClock(const std::string &name, int style)
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
#define BASETIMERNAME_FOR_SCROPE_TIMER
Definition: TimingMonitor.h:22
static DLLEXPORT size_t GetCurrentTimerCount()