All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
perfmon.h
Go to the documentation of this file.
1 #ifndef MISC_PERFMON_H
2 #define MISC_PERFMON_H
3 #if defined(__i386__) || defined(__x86_64__) || defined(_MSC_VER)
4 # define HAVE_TSC 1
5 #endif
6 #ifndef _MSC_VER
7 # include <sys/time.h>
8 # ifndef HAVE_TSC
9 # include <sys/resource.h>
10 # endif
11 #endif
12 #include <iosfwd>
13 #include <string>
14 #include <cassert>
15 namespace osl
16 {
17  namespace misc
18  {
19  class PerfMon
20  {
21 #ifdef HAVE_TSC
22  unsigned long long start_time;
23 #else
24  rusage start_time;
25 #endif
26  public:
27  void restart()
28  {
29 #ifdef HAVE_TSC
30 # ifndef _MSC_VER
31  unsigned int ax,dx;
32  asm volatile("rdtsc\nmovl %%eax,%0\nmovl %%edx,%1":"=g"(ax),"=g"(dx): :"eax","edx");
33  start_time =
34  (static_cast<unsigned long long>(dx)<<32)
35  + static_cast<unsigned long long>(ax);
36 # else
37  start_time = 0;
38 # endif
39 #else
40 #ifdef NDEBUG
41  getrusage(RUSAGE_SELF, &start_time);
42 #else
43  int ret=getrusage(RUSAGE_SELF, &start_time);
44  assert(ret==0);
45 #endif
46 #endif
47  }
48  PerfMon() {
49  restart();
50  }
51  unsigned long long stop(){
52 #ifdef HAVE_TSC
53 # ifndef _MSC_VER
54  unsigned int ax,dx;
55  asm volatile("rdtsc\nmovl %%eax,%0\nmovl %%edx,%1":"=g"(ax),"=g"(dx): :"eax","edx");
56  const unsigned long long end_time
57  = ((static_cast<unsigned long long>(dx)<<32)
58  + static_cast<unsigned long long>(ax));
59  return (end_time - PerfMon::start_time);
60 # else
61  return 0;
62 # endif
63 #else
64  rusage end_time;
65 #ifdef NDEBUG
66  getrusage(RUSAGE_SELF,&end_time);
67 #else
68  int ret=getrusage(RUSAGE_SELF,&end_time);
69  assert(ret==0);
70 #endif
71  return (end_time.ru_utime.tv_sec - start_time.ru_utime.tv_sec)*1000000
72  +(end_time.ru_utime.tv_usec - start_time.ru_utime.tv_usec);
73 #endif
74  }
75  void stop(const char *message,int loop){
76  const unsigned long long cycles=stop();
77  PerfMon::message(cycles, message, loop);
78  }
79  static void message(unsigned long long cycles,
80  const char *message,long long int loop);
81  };
82 
83  class TSC
84  {
85  unsigned long long start_time;
86  unsigned long long sum_time;
87  long long int counter;
88  std::string message;
89  public:
90  TSC(const char *m) :start_time(0ll),sum_time(0ll),counter(0ll),message(m) {}
91  void start()
92  {
93 #ifdef HAVE_TSC
94 # ifndef _MSC_VER
95  unsigned int ax,dx;
96  asm volatile("rdtsc\nmovl %%eax,%0\nmovl %%edx,%1":"=g"(ax),"=g"(dx): :"eax","edx");
97  start_time =
98  (static_cast<unsigned long long>(dx)<<32)
99  + static_cast<unsigned long long>(ax);
100 # else
101  start_time = 0;
102 # endif
103 #endif
104  counter++;
105  }
106  void stop(){
107 #ifdef HAVE_TSC
108 # ifndef _MSC_VER
109  unsigned int ax,dx;
110  asm volatile("rdtsc\nmovl %%eax,%0\nmovl %%edx,%1":"=g"(ax),"=g"(dx): :"eax","edx");
111  const unsigned long long end_time
112  = ((static_cast<unsigned long long>(dx)<<32)
113  + static_cast<unsigned long long>(ax));
114  sum_time+=end_time - start_time;
115 # else
116  sum_time = 0;
117 # endif
118 #endif
119  }
121  {
123  }
124  };
125  class Counter
126  {
127  unsigned long long int counter;
128  std::string message;
129  public:
130  Counter(const char *m) :counter(0ll),message(m) {}
131  Counter(std::string const& m) :counter(0ll),message(m) {}
132  void count()
133  {
134  counter++;
135  }
137  {
138  PerfMon::message(0ll,message.c_str(),counter);
139  }
140  };
142  {
143  unsigned long long int counter1;
144  unsigned long long int counter2;
145  std::string message;
146  public:
147  CounterPair(std::string const& m) :counter1(0ll),counter2(0ll),message(m) {}
148  CounterPair(const char *file, const char *function, int line);
149  void count1()
150  {
151  counter1++;
152  }
153  void count2()
154  {
155  counter2++;
156  }
157  ~CounterPair();
158  };
159 #ifndef _MSC_VER
161  {
162  timeval start;
163  std::ostream& os;
164  char const* message;
165  public:
166  MeasureTimeLock (std::ostream& os, char const* message)
167  : os (os), message (message)
168  {
169 #ifndef NDEBUG
170  int ret =
171 #endif
172  gettimeofday(&start, NULL);
173  assert(ret == 0);
174  }
175 
177  };
178 #endif
179  } // namespace misc
180 } // namespace osl
181 
182 
183 #endif /* MISC_PERFMON_H */
184 // ;;; Local Variables:
185 // ;;; mode:c++
186 // ;;; c-basic-offset:2
187 // ;;; End: