-
Notifications
You must be signed in to change notification settings - Fork 2
/
perf.h
104 lines (84 loc) · 1.63 KB
/
perf.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
#ifndef _perf_h_
#define _perf_h_
#include <valgrind/callgrind.h>
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <iomanip>
#include "callgrind-wrapper/callgrind-wrapper.h"
//using namespace callgrind_wrapper;
struct Startup
{
Startup()
{
if (!callgrind_wrapper::is_working())
{
std::cout << "ERROR: please run within callgrind (using 'make run' should work)!" << std::endl;
//std::abort();
}
}
} startup;
class Instrument
{
public:
Instrument(const char *name)
:
name (name),
stopped(false)
{
callgrind_wrapper::reset();
}
void stop()
{
long cycles = callgrind_wrapper::current();
stopped = true;
std::cout << "> " << name << " " << std::fixed << std::setprecision(6)
<< static_cast<double>(cycles)/1000000.0 << std::endl;
}
~Instrument()
{
if (!stopped)
stop();
}
private:
const char *name;
bool stopped;
};
/*
class Instrument
{
public:
Instrument(const char* name)
:
name (name),
stopped(false)
{
CALLGRIND_START_INSTRUMENTATION;
CALLGRIND_ZERO_STATS;
}
void stop()
{
if (stopped)
return;
stopped = true;
int ret;
ret = system("rm -f log*");
CALLGRIND_DUMP_STATS_AT(name);
ret = system("cat log* | grep summary >log.summary");
std::ifstream f("log.summary");
std::string bla;
long cycles;
f >> bla >> cycles;
std::cout << "> " << name << " " << cycles << std::endl;
ret = system("rm -f log*");
}
~Instrument()
{
stop();
}
private:
const char* name;
bool stopped;
};
*/
#endif