scope_timer.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2013 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 */
28 
29 #pragma once
30 
31 #if defined(_MSC_VER)
32 #include <intrin.h>
33 #endif
34 
35 namespace clan
36 {
37 
38 #if defined(_MSC_VER)
39 
40 static inline unsigned long long rdtsc()
41 {
42  return __rdtsc();
43 }
44 
45 #elif defined(__i386__)
46 
47 static __inline__ unsigned long long rdtsc()
48 {
49  unsigned long long int x;
50  __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
51  return x;
52 }
53 
54 #elif defined(__x86_64__)
55 
56 static __inline__ unsigned long long rdtsc()
57 {
58  unsigned hi, lo;
59  __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
60  return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
61 }
62 
63 #else
64 
65 static __inline__ unsigned long long rdtsc()
66 {
67  return 0;
68 }
69 
70 #endif
71 
73 {
74 public:
75  ScopeTimerResult(const char *name, unsigned long long ticks) : name(name), ticks(ticks) { }
76 
77  const char *name;
78  unsigned long long ticks;
79 };
80 
82 {
83 public:
84  static void start();
85  static void add_result(const ScopeTimerResult &result);
86  static void end();
87  static int percentage(const char *name);
88  static std::string timer_results();
89 
90 private:
92 
93  unsigned long long start_time;
94  std::vector<ScopeTimerResult> results;
95  unsigned long long end_time;
96 
97  unsigned long long current_start_time;
98  std::vector<ScopeTimerResult> current_results;
99  unsigned long long current_end_time;
100 
101  static ScopeTimerResults instance;
102 };
103 
105 {
106 public:
107  ScopeTimer() : _name(), _start()
108  {
109  }
110 
111  ScopeTimer(const char *name) : _name(), _start()
112  {
113  start(name);
114  }
115 
117  {
118  end();
119  }
120 
121  void start(const char *name)
122  {
123  end();
124  _name = name;
125  _start = rdtsc();
126  }
127 
128  void end()
129  {
130  if (_name)
131  {
132  ScopeTimerResults::add_result(ScopeTimerResult(_name, rdtsc() - _start));
133  _name = 0;
134  }
135  }
136 
137 private:
138  ScopeTimer(const ScopeTimer &that);
139  ScopeTimer &operator =(const ScopeTimer &that);
140 
141  const char *_name;
142  unsigned long long _start;
143 };
144 
145 #define ScopeTimeFunction() ScopeTimer scope_timer(__FUNCTION__);
146 
147 }
unsigned long long ticks
Definition: scope_timer.h:78
const char * name
Definition: scope_timer.h:77
Definition: scope_timer.h:104
ScopeTimer()
Definition: scope_timer.h:107
ScopeTimerResult(const char *name, unsigned long long ticks)
Definition: scope_timer.h:75
void start(const char *name)
Definition: scope_timer.h:121
static int percentage(const char *name)
ScopeTimer(const char *name)
Definition: scope_timer.h:111
static void add_result(const ScopeTimerResult &result)
Definition: scope_timer.h:81
void end()
Definition: scope_timer.h:128
Definition: scope_timer.h:72
~ScopeTimer()
Definition: scope_timer.h:116
static std::string timer_results()