/
timer.h
210 lines (171 loc) · 5.94 KB
/
timer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#ifndef LVV_TIMER_H
#define LVV_TIMER_H
// this is for uint64_t on gcc-4.4
//#define _GLIBCXX_USE_C99_STDINT_TR1
#include <sys/time.h>
#include <sys/resource.h>
#include <stdlib.h>
#include <stdint.h>
#include <iostream>
#include <iomanip>
namespace lvv {
/* OpenMP thread time?
double omp_time = 0;
double last_omp_time;
last_omp_time = omp_time;
omp_time = omp_get_wtime();
int static cnt = 0;
MSG("(%d/%.1fs ? :%.1fs elapsed)%8t") % cnt++ %timer() %(omp_time-last_omp_time);
*/
///////////////////////////////////////////////////////////////////////////////////// READ_TICK
#if defined(__x86_64) || defined (__i386)
uint64_t read_tick() { // tested with with x86_64 only.
uint64_t now_tick;
asm volatile ( "subl %%eax,%%eax;"
"cpuid;"
"rdtsc;"
#if defined(__x86_64)
"shlq $32, %%rdx;"
"orq %%rdx, %%rax;" // combine into 64 bit register
"movq %%rax, %[now_tick];"
#endif
#if defined(__i386)
"movl %%eax, %[now_tick];"
"lea %0, %%eax;"
"movl %%edx, 4(%%eax);"
#endif
"cpuid;"
:[now_tick] "=m"(now_tick) // output
:
#if defined(__x86_64)
:"rax", "rdx", "rbx","rcx", "rdx" // clobbered register
#else
:"ebx","ecx", "edx" // clobbered register
#endif
);
return now_tick;
}
#endif
class Timer { //=========================================== TIMER
// too late, I've found almost the same impl at http://www.boost.org/doc/libs/1_35_0/libs/timer/timer.htm
// article about hi-res timers: http://www.devx.com/cplus/Article/35375/0/page/2
// see also OpenMP timer: http://gcc.gnu.org/onlinedocs/libgomp/omp_005fget_005fwtime.html#omp_005fget_005fwtime
private:
bool verbose_dtor;
// Tick (cpu cycle)
#if defined(__x86_64) || defined (__i386)
uint64_t ctor_tick;
uint64_t interval_start_tick;
uint64_t now_tick;
uint64_t overhead;
#endif
// TV - wall clock time
timeval ctor_tv;
timeval interval_start_tv;
timeval now_tv;
// RU - CPU time
struct rusage ctor_ru;
struct rusage interval_start_ru;
struct rusage now_ru;
double wall_time_at(timeval tv) { return tv.tv_sec + tv.tv_usec / 1000000.; };
double cpu_time_at(struct rusage ru) {
return
ru.ru_utime.tv_sec + ru.ru_utime.tv_usec / 1000000. +
ru.ru_stime.tv_sec + ru.ru_stime.tv_usec / 1000000.;
};
public:
Timer(bool dtor=false) : verbose_dtor(dtor) {
#if defined(__x86_64) || defined (__i386)
ctor_tick = interval_start_tick = read_tick();
overhead = interval_ticks();
#endif
gettimeofday(&now_tv, NULL); ctor_tv = interval_start_tv = now_tv;
getrusage(RUSAGE_SELF, &now_ru); ctor_ru = interval_start_ru = now_ru;
};
~Timer() {
if (verbose_dtor)
cout << "(*) on exit: cpu time: " << total_cpu() << "s " << "wall time: " << total_wall() << "s " << endl;
// memory report
//system("egrep -r '^Vm(Peak|Size|RSS|Data|Stk|Exe|Lib)' /proc/$PPID/status |sed 's/^Vm//; s/ kB/k/; s/ *//'|tr ' \n' ' '");
// amount of unrequested memory - CommitLimit - Committed_AS
};
void reset() {
#if defined(__x86_64) || defined (__i386)
interval_ticks();
#endif
interval_cpu();
}
#if defined(__x86_64) || defined (__i386)
uint64_t
interval_ticks() {
uint64_t now_tick = read_tick();
uint64_t ticks = now_tick - interval_start_tick;
interval_start_tick = now_tick;
return ticks;
}
#endif
double
interval_wall() {
gettimeofday(&now_tv, NULL);
double sec = wall_time_at(now_tv) - wall_time_at(interval_start_tv);
interval_start_tv = now_tv;
return sec;
}
double
interval_cpu() {
getrusage(RUSAGE_SELF, &now_ru);
double sec = cpu_time_at(now_ru) - cpu_time_at(interval_start_ru);
interval_start_ru = now_ru;
return sec;
}
#if defined(__x86_64) || defined (__i386)
double total_ticks () { return read_tick() - ctor_tick; }
#endif
double total_wall () { gettimeofday(&now_tv, NULL); return wall_time_at(now_tv) - wall_time_at(ctor_tv); }
double total_cpu () { getrusage(RUSAGE_SELF, &now_ru); return cpu_time_at (now_ru) - cpu_time_at (ctor_ru); }
double operator() () { return interval_wall(); }
void
print(string msg="") {
if (msg=="")
std::cout << std::setw(13) << "(timer) ";
else {
std::cout <<" ⌛ " << msg << " ";
};
std::cout << std::setprecision(4);
#if defined(__x86_64) || defined (__i386)
std::cout << "overhead:" << overhead << "t " ;
std::cout << "interval:" << interval_ticks() << "t " ;
std::cout << "total: " << total_ticks( ) << "t " ;
#endif
std::cout << "interval-wall:" << interval_wall() << "s " ;
std::cout << "interval-cpu: " << interval_cpu( ) << "s " ;
std::cout << "total-wall: " << total_wall( ) << "s " ;
std::cout << "total-cpu: " << total_cpu( ) << "s " ;
std::cout << std::endl;
};
friend ostream& operator<< (ostream& os, Timer& t);
};
ostream& operator<< (ostream& os, Timer& t) {
os << setprecision(5) << " " << t() << "s ";
return os;
}
void
progress_dots(long var, long first, long last, string msg="" ) { //========================================================= progress_dots()
static bool first_time = true;
static int columns = 0;
static int width;
if (first_time) {
first_time=false;
char* columns_str=getenv("COLUMNS");
if (columns_str)
columns=atoi(columns_str);
width = columns - msg.length();
}
if (!columns) return;
if ( var==first) cout << msg;
int div=(last-first)/width;
if ( (var-first) % (div+1) == 0 ) cout << "." << flush;
if ( var == last ) cout << endl;
}
} // namespace lvv
#endif // LVV_TIMER_H