/
profile.hh
204 lines (190 loc) · 4.8 KB
/
profile.hh
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
// C++ PAPI library
// Copyright (c) 2008, 2009 Timothy Middelkoop. All rights reserved.
// Licensed under the GPL 2.0 or later.
// WARNING: Turn off frequency scaling.
#ifndef _mtim_profile_papi_hh_
#define _mtim_profile_papi_hh_
#include <iostream>
#include <stdexcept>
using namespace std;
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <papi.h>
#include <tbb/enumerable_thread_specific.h>
namespace mtim {
namespace profile {
namespace debug {
const int DEBUG=true;
const int TRACE=false;
}
inline void papi_ok(int retval) {
if(retval==PAPI_OK)
return;
else if(retval==PAPI_ESYS)
throw std::runtime_error(strerror(errno));
else{
char error[PAPI_MAX_STR_LEN];
PAPI_perror(retval,error,PAPI_MAX_STR_LEN);
throw std::runtime_error(error);
}
}
class Profile {
public:
static const int max_events=5; //PAPI_MAX_HWCTRS;
static const int static_default_eventlist[];
static int const * default_eventlist;
static int default_events;
typedef long long values_t[max_events];
private:
int eventset;
int events;
int event[max_events];
long long usec_start;
public:
// Called once to initialize the profile system.
static void init() {
if(debug::TRACE) std::cout << "Profile::init>" << std::endl;
if(PAPI_library_init(PAPI_VER_CURRENT) != PAPI_VER_CURRENT)
throw std::runtime_error("PAPI_library_init");
if(PAPI_thread_init(pthread_self) != PAPI_OK)
throw std::runtime_error("PAPI_thread_init");
}
static void init(int* eventlist, int events){
default_eventlist=eventlist;
default_events=events;
init();
}
Profile(const int* eventlist=default_eventlist, int events=0)
: eventset(PAPI_NULL), events(0), usec_start(0) {
if(eventlist==default_eventlist)
events=default_events;
papi_ok(PAPI_create_eventset(&eventset));
for(int i=0;i<events;i++)
add(eventlist[i]);
}
Profile(const Profile& p)
: eventset(PAPI_NULL), events(p.events), usec_start(0) {
papi_ok(PAPI_create_eventset(&eventset));
for(int i=0;i<p.events;i++)
add(p.event[i]);
}
~Profile(){
papi_ok(PAPI_cleanup_eventset(eventset));
papi_ok(PAPI_destroy_eventset(&eventset));
}
public:
void add(int event){
papi_ok(PAPI_add_event(eventset,event));
this->event[events++]=event;
}
void start(){
// Start the counters from zero
papi_ok(!(usec_start=PAPI_get_virt_usec()));
if(events)
papi_ok(PAPI_start(eventset));
}
long long stop(values_t& value){
long long usec_stop;
papi_ok(!(usec_stop=PAPI_get_virt_usec()));
// stops the counters (reset to zero) adding the result to value.
if(events){
long long _value[events];
papi_ok(PAPI_stop(eventset,_value)!=PAPI_OK);
for(int i=0;i<events;i++)
value[i]+=_value[i];
}
return (usec_stop-usec_start);
}
long long mem(){
PAPI_dmem_info_t dmem;
papi_ok(PAPI_get_dmem_info(&dmem));
return dmem.size;
}
void print(const values_t& value) const {
std::cout << "Profile.print>";
char event_string[PAPI_MAX_STR_LEN];
for(int i=0;i<events;i++){
PAPI_event_code_to_name(event[i],event_string);
std::cout << " " << event_string << ": " << value[i];
}
std::cout << std::endl;
}
/***
* Get the value of a specific event
* @param event event code
* @param values Values array
* @return return value of event code
*/
long long get(const int event,const Profile::values_t& value) const {
for(int i=0;i<events;i++)
if(event==this->event[i])
return value[i];
throw std::runtime_error("Event not found");
}
};
class Timer {
private:
long long usec_start;
long long usec_stop;
long long usec_value;
long count;
public:
Timer() : usec_value(0), count(0) {};
~Timer() {}
public:
void start(){
// Start the counters from zero
count++;
papi_ok(!(usec_start=PAPI_get_virt_usec()));
}
void stop(){
papi_ok(!(usec_stop=PAPI_get_virt_usec()));
usec_value+=(usec_stop-usec_start);
}
void reset(){
usec_value=0;
}
inline long long usec() const {
return usec_value;
}
inline long long interval() const {
return usec_stop-usec_start;
}
long long mem(){
PAPI_dmem_info_t dmem;
papi_ok(PAPI_get_dmem_info(&dmem));
return dmem.size;
}
void join(const Timer& p){
count+=p.count;
usec_value+=p.usec_value;
}
void print() const {
std::cout << "Timer.print>";
std::cout << " msec:" << (usec_value)/1000.0;
std::cout << " count:" << count;
std::cout << std::endl;
}
private: // Hidden default methods
Timer(const Timer& p);
};
class Profilers {
private:
typedef tbb::enumerable_thread_specific<Profile,tbb::cache_aligned_allocator<Profile>,tbb::ets_key_per_instance> ProfileLocal;
ProfileLocal profilers;
public:
static Profilers* global;
public:
Profilers(){
if(debug::TRACE) cout << "Profilers>" << endl;
}
~Profilers(){
if(debug::TRACE) cout << "Profilers~>" << endl;
}
inline Profile& profile(){
return profilers.local();
}
};
}}//mtim::profile
#endif//_mtim_profile_papi_hh_