hobbes
a language, embedded compiler, and runtime for efficient dynamic expression evaluation, data storage and analysis
perf.H
Go to the documentation of this file.
1 
2 #ifndef HOBBES_UTIL_PERF_HPP_INCLUDED
3 #define HOBBES_UTIL_PERF_HPP_INCLUDED
4 
5 #include <string>
6 #include <sstream>
7 #include <iostream>
8 #include <time.h>
9 
10 namespace hobbes {
11 
12 // access a nanosecond timer to do performance-testing on expressions
13 inline long tick() {
14  timespec ts;
15  if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
16  return ts.tv_sec * 1000000000L + ts.tv_nsec;
17  } else {
18  return 0;
19  }
20 }
21 
22 // access a nanosecond clock
23 inline long time() {
24  timespec ts;
25  if (clock_gettime(CLOCK_REALTIME, &ts) == 0) {
26  return ts.tv_sec * 1000000000L + ts.tv_nsec;
27  } else {
28  return 0;
29  }
30 }
31 
32 inline std::string describeNanoTime(long x) {
33  static const double micros = 1000;
34  static const double millis = 1000*micros;
35  static const double seconds = 1000*millis;
36  static const double minutes = 60*seconds;
37 
38  double t = (double)x;
39  std::ostringstream ss;
40 
41  if ((t / minutes) >= 1) {
42  ss << (t / minutes) << "m";
43  } else if ((t / seconds) >= 1) {
44  ss << (t / seconds) << "s";
45  } else if ((t / millis) >= 1) {
46  ss << (t / millis) << "ms";
47  } else if ((t / micros) >= 1) {
48  ss << (t / micros) << "us";
49  } else {
50  ss << t << "ns";
51  }
52 
53  return ss.str();
54 }
55 
56 struct probe {
57  std::string msg;
58  long t0;
59  probe(const std::string& msg) : msg(msg) {
60  t0 = tick();
61  }
62 
63  void report() const {
64  long dt = tick() - this->t0;
65  static const double micros = 1000;
66  static const double millis = 1000*micros;
67  static const double seconds = 1000*millis;
68 
69  if (dt > (1*seconds)) {
70  std::cout << msg << ": " << std::flush;
71  std::cout << describeNanoTime(tick() - this->t0) << std::endl;
72  }
73  }
74 };
75 
76 template <long* globalTC>
77  struct timed_region {
78  long t0;
79 
80  static bool activelyTiming() {
81  return (*globalTC % 2) == 1;
82  }
83 
84  static void activelyTiming(bool f) {
85  if (f) {
86  if ((*globalTC % 2) == 0) { *globalTC += 1; }
87  } else {
88  if ((*globalTC % 2) == 1) { *globalTC += 1; }
89  }
90  }
91 
93  if (activelyTiming()) {
94  t0 = -1;
95  } else {
96  t0 = hobbes::tick();
97  activelyTiming(true);
98  }
99  }
100 
102  if (t0 != -1) {
103  *globalTC += hobbes::tick() - t0;
104  activelyTiming(false);
105  }
106  }
107  };
108 
109 #if defined(__i386__)
110 inline unsigned long long rdtsc(void) {
111  unsigned long long int x;
112  __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
113  return x;
114 }
115 #elif defined(__x86_64__)
116 inline unsigned long long rdtsc(void) {
117  unsigned hi, lo;
118  __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
119  return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
120 }
121 #endif
122 
123 }
124 
125 #endif
static bool activelyTiming()
Definition: perf.H:80
Definition: perf.H:77
timed_region()
Definition: perf.H:92
Definition: boot.H:7
long t0
Definition: perf.H:78
Definition: cio.H:7
static void activelyTiming(bool f)
Definition: perf.H:84
long tick()
Definition: perf.H:13
~timed_region()
Definition: perf.H:101
long time()
Definition: perf.H:23
probe(const std::string &msg)
Definition: perf.H:59
std::string describeNanoTime(long x)
Definition: perf.H:32
void report() const
Definition: perf.H:63
long t0
Definition: perf.H:58
Definition: perf.H:56
std::string msg
Definition: perf.H:57