code_profiler
Profiler.h
Go to the documentation of this file.
1 #ifndef PROFILER_H
2 #define PROFILER_H
3 #include <string>
4 #include <map>
5 #include <vector>
6 #include <boost/thread.hpp>
7 #include <boost/utility.hpp>
8 #include "Timer.h"
9 #include "profiling/ProfileLog.h"
10 
11 
12 #ifdef PROFILEAPP
13 
14 static std::string LOGGING_LOCATION = "~/profile_logs";
15 
16 struct Statistics
17 {
18  Timer timer;
19  double msec;
20  int callAmount;
21 
22  std::string parent;
23  bool running;
24 
25  Statistics() : msec(0), callAmount(0), parent(""), running(true) {}
26  Statistics(std::string p) : msec(0), callAmount(0), parent(p), running(true){}
27 };
28 
29 typedef std::map< std::string, Statistics > FunctionStats;
30 typedef std::map< std::string, Statistics >* FunctionStatsPtr;
31 
32 class ThreadProfiler
33 {
34 
35 public:
37  {
38  threadId = boost::this_thread::get_id();
39  currentlyProfiling = "";
40  stats = new FunctionStats();
41  }
42  virtual ~ThreadProfiler();
43 
44  static void Start(const std::string &name);
45  static void Stop(const std::string& name);
46  static FunctionStatsPtr ReturnCurrentStatistics();
47 
48 
49  FunctionStatsPtr stats;
50 
51 private:
52  boost::thread::id threadId;
53  std::string currentlyProfiling;
54 };
55 
56 class ThreadProfilerManager
57 {
58 public:
59 
60  ThreadProfilerManager();
61 
62  inline ThreadProfiler& Profiler()
63  {
64  if(profiler.get() == NULL)
65  {
66  profiler.reset(new ThreadProfiler());
67  boost::lock_guard<boost::mutex> lock(mutex);
68  threadStats.push_back(profiler.get()->stats);
69  }
70 
71  return *profiler.get();
72  }
73 
74  ~ThreadProfilerManager();
75 private:
76  boost::thread_specific_ptr<ThreadProfiler> profiler;
77  boost::mutex mutex;
79  Timer timer;
80 
81 };
82 
83 //Global threadProfiler
84 static ThreadProfilerManager manager;
85 
86 //Implementation of ThreadProfiler methods
87 inline void ThreadProfiler::Start(const std::string& name)
88 {
89  ThreadProfiler& instance = manager.Profiler();
90  if(instance.stats->find(name) == instance.stats->end() && !instance.stats->find(name)->second.running)
91  {
92  (*instance.stats)[name] = Statistics(instance.currentlyProfiling);
93  }
94  (*instance.stats)[name].timer.start();
95  (*instance.stats)[name].running = true;
96  instance.currentlyProfiling = name;
97 }
98 
99 inline void ThreadProfiler::Stop(const std::string &name)
100 {
101  ThreadProfiler& instance = manager.Profiler();
102  FunctionStats::iterator mapIt = instance.stats->find(name);
103 
104  if(mapIt == instance.stats->end())
105  return;
106 
107  mapIt->second.msec += mapIt->second.timer.getElapsedTimeInMilliSec();
108  mapIt->second.callAmount++;
109  mapIt->second.running = false;
110 }
111 
112 inline FunctionStatsPtr ThreadProfiler::ReturnCurrentStatistics()
113 {
114  ThreadProfiler& instance = manager.Profiler();
115  return instance.stats;
116 }
117 
118 namespace
119 {
120  template<typename T>
121  void deleteElement(T t)
122  {
123  delete t;
124  }
125 
126  template<typename _Element, typename T>
127  void deleteAll(T &t)
128  {
129  std::for_each(t.begin(), t.end(), deleteElement<_Element>);
130  }
131 }
132 
133 inline ThreadProfiler::~ThreadProfiler(){ }
134 
135 inline ThreadProfilerManager::ThreadProfilerManager()
136 {
137  timer.start();
138 }
139 
140 inline ThreadProfilerManager::~ThreadProfilerManager()
141 {
142  timer.stop();
143  ProfileLog::PrintLog(threadStats, timer.getElapsedTimeInMilliSec());
144  deleteAll<FunctionStatsPtr>(threadStats);
145 }
146 
147 
148 #else
150 {
151 public:
152  static void Start(std::string name);
153  static void Stop(std::string name);
154 };
155 
158 #endif
159 
160 #endif
std::lock
T lock(T... args)
std::for_each
T for_each(T... args)
std::string
t
Timer t
Definition: main.cpp:6
vector
ThreadProfiler::Start
static void Start(std::string name)
Definition: Profiler.h:156
ProfileLog.h
Timer
Definition: Timer.h:28
ThreadProfiler::Stop
static void Stop(std::string name)
Definition: Profiler.h:157
Timer.h
profiler
tue::Profiler profiler
Definition: test_profiler.cpp:4
map
ThreadProfiler
Definition: Profiler.h:149
string