-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPapiTracer.h
More file actions
141 lines (103 loc) · 3.29 KB
/
PapiTracer.h
File metadata and controls
141 lines (103 loc) · 3.29 KB
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
#ifndef HELPER_PAPI_TRACER_H
#define HELPER_PAPI_TRACER_H
#include <stdio.h>
#include <vector>
#include <map>
#include <string>
#include <sys/time.h>
using namespace std;
#ifdef USE_PAPI_TRACE
#include <papi.h>
#include <pthread.h>
#include <stdexcept>
#endif
struct PapiTracer
{
typedef long long ll_t;
typedef std::pair<ll_t, ll_t> result_t;
static int start(std::string eventName = "PAPI_TOT_INS")
{
if (eventName.compare("NO_PAPI") == 0)
return -1;
#ifdef USE_PAPI_TRACE
//ovverride event settings
//eventName = std::string(getenv("PAPI_EVENT"));
static bool initialized = false;
if(!initialized)
{
if (PAPI_library_init(PAPI_VER_CURRENT) != PAPI_VER_CURRENT)
throw runtime_error("PAPI could not be initialized");
initialized = true;
}
if (PAPI_thread_init(pthread_self) != PAPI_OK)
throw runtime_error("PAPI could not initialize thread");
int retval;
// Create the event set
int eventSet = PAPI_NULL;
if ((retval = PAPI_create_eventset(&eventSet)) != PAPI_OK)
throw runtime_error(PAPI_strerror(retval));
// Add two events
if ((retval = PAPI_add_event(eventSet, PAPI_TOT_CYC)) != PAPI_OK)
throw runtime_error(PAPI_strerror(retval));
// Add the configurable event
int eventCode;
if (PAPI_event_name_to_code((char*) eventName.c_str(), &eventCode) != PAPI_OK)
throw runtime_error("Could not create event from name " + eventName);
if ((retval = PAPI_add_event(eventSet, eventCode)) != PAPI_OK)
throw runtime_error("Could not add events to event set, maybe conflicting");
// Start the measurement
if ((retval = PAPI_start(eventSet)) != PAPI_OK)
throw runtime_error(PAPI_strerror(retval));
return eventSet;
#else
return clock();
#endif
}
static int reset(int eventSet)
{
if (eventSet == -1)
return -1;
#ifdef USE_PAPI_TRACE
ll_t tmp[2] = {0,0};
int retval;
if ((retval = PAPI_stop(eventSet, tmp)) != PAPI_OK)
throw runtime_error(PAPI_strerror(retval));
if ((retval = PAPI_start(eventSet)) != PAPI_OK)
throw runtime_error(PAPI_strerror(retval));
return eventSet;
#else
return clock();
#endif
}
static result_t stop(int eventSet)
{
if (eventSet == -1)
{
result_t result;
result.first = 0;
result.second = 0;
return result;
}
#ifdef USE_PAPI_TRACE
ll_t tmp[2] = {0,0};
int retval;
if ((retval = PAPI_stop(eventSet, tmp)) != PAPI_OK)
throw runtime_error(PAPI_strerror(retval));
if ((retval = PAPI_cleanup_eventset(eventSet)) != PAPI_OK)
throw runtime_error(PAPI_strerror(retval));
if ((retval = PAPI_destroy_eventset(&eventSet)) != PAPI_OK)
throw runtime_error(PAPI_strerror(retval));
result_t result;
result.first = tmp[0];
result.second = tmp[1];
return result;
#else
clock_t end = clock();
result_t result;
result.first = end - eventSet;
result.second = 0;
return result;
#endif
}
};
#endif // HELPER_PAPI_TRACER_H