-
-
Notifications
You must be signed in to change notification settings - Fork 542
Expand file tree
/
Copy pathpapi.cpp
More file actions
359 lines (311 loc) · 12.9 KB
/
papi.cpp
File metadata and controls
359 lines (311 loc) · 12.9 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright (c) 2007-2011 Hartmut Kaiser
// Copyright (c) 2011-2012 Maciej Brodowicz
//
// SPDX-License-Identifier: BSL-1.0
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <hpx/config.hpp>
#if defined(HPX_HAVE_PAPI)
#include <hpx/components/performance_counters/papi/server/papi.hpp>
#include <hpx/components/performance_counters/papi/util/papi.hpp>
#include <hpx/components_base/server/component.hpp>
#include <hpx/modules/actions_base.hpp>
#include <hpx/modules/errors.hpp>
#include <hpx/modules/runtime_local.hpp>
#include <hpx/modules/timing.hpp>
#include <hpx/modules/type_support.hpp>
#include <hpx/runtime_components/derived_component_factory.hpp>
#include <boost/version.hpp>
#include <cstdint>
#include <functional>
#include <mutex>
#include <string>
#include <vector>
#if defined(__linux__) && !defined(__ANDROID) && !defined(ANDROID)
#include <sys/syscall.h>
#else
#error "The PAPI performance counter component can only be used on Linux"
#endif
///////////////////////////////////////////////////////////////////////////////
namespace papi_ns = hpx::performance_counters::papi;
typedef hpx::components::component<papi_ns::server::papi_counter>
papi_counter_type;
HPX_REGISTER_DERIVED_COMPONENT_FACTORY_DYNAMIC(
papi_counter_type, papi_counter, "base_performance_counter")
///////////////////////////////////////////////////////////////////////////////
namespace hpx { namespace performance_counters { namespace papi {
namespace server {
#define NS_STR "hpx::performance_counters::papi::server::"
///////////////////////////////////////////////////////////////////////////
// static members
papi_counter_base::mutex_type papi_counter_base::base_mtx_;
papi_counter_base::ttable_type papi_counter_base::thread_state_;
using hpx::performance_counters::papi::util::papi_call;
///////////////////////////////////////////////////////////////////////////
// methods
thread_counters::thread_counters(std::uint32_t tix)
: thread_index_(tix)
, evset_(PAPI_NULL)
{
char const* locstr =
"hpx::performance_counters::papi::server::thread_counters()";
hpx::util::thread_mapper& tm =
hpx::get_runtime().get_thread_mapper();
papi_call(PAPI_create_eventset(&evset_),
"could not create PAPI event set", locstr);
papi_call(PAPI_assign_eventset_component(evset_, 0),
"cannot assign component index to event set", locstr);
pid_t tid = tm.get_linux_thread_id(tix);
if (tid == -1)
HPX_THROW_EXCEPTION(hpx::error::bad_parameter,
NS_STR "thread_counters::thread_counters()",
"unable to retrieve correct OS thread ID for profiling "
"(perhaps thread was not registered)");
papi_call(PAPI_attach(evset_, tid),
"failed to attach thread to PAPI event set", locstr);
tm.register_callback(
tix, [&](std::uint32_t i) { return this->terminate(i); });
}
thread_counters::~thread_counters()
{
{
std::lock_guard<mutex_type> m(mtx_);
finalize();
}
// callback cancellation moved outside the mutex to avoid potential deadlock
hpx::get_runtime().get_thread_mapper().revoke_callback(
thread_index_);
}
bool thread_counters::add_event(papi_counter* cnt)
{
stop_all();
if (PAPI_add_event(evset_, cnt->get_event()) != PAPI_OK)
return false;
counts_.push_back(cnt->get_value());
cnttab_.push_back(cnt);
cnt->update_index(counts_.size() - 1);
return start_all();
}
bool thread_counters::remove_event(papi_counter* cnt)
{
if (!stop_all() || PAPI_cleanup_eventset(evset_) != PAPI_OK)
return false;
// store the most recent value
int rmix = cnt->get_counter_index();
cnt->update_state(timestamp_, counts_[rmix], PAPI_COUNTER_STOPPED);
// erase entries corresponding to removed event
counts_.erase(counts_.begin() + rmix);
cnttab_.erase(cnttab_.begin() + rmix);
// For the lack of better strategy the remaining events are added in the
// same order as before. This avoids reordering of remaining counter
// values and at least some surprises on architectures with asymmetric
// functionality of counting registers.
unsigned i = 0;
while (i < counts_.size())
{
papi_counter* c = cnttab_[i];
if (PAPI_add_event(evset_, c->get_event()) != PAPI_OK)
{ // failed to add event
c->update_state(timestamp_, counts_[c->get_counter_index()],
PAPI_COUNTER_SUSPENDED);
counts_.erase(counts_.begin() + i);
cnttab_.erase(cnttab_.begin() + i);
continue;
}
// update indices of remaining counters
c->update_index(i++);
}
return start_all();
}
bool thread_counters::read_value(papi_counter* cnt, bool reset)
{
{
std::lock_guard<papi_counter_base::mutex_type> lk(
cnt->get_global_mtx());
if (PAPI_accum(evset_, &counts_[0]) != PAPI_OK)
return false;
}
timestamp_ = static_cast<std::int64_t>(hpx::get_system_uptime());
cnt->update_state(timestamp_, counts_[cnt->get_counter_index()]);
if (reset)
counts_[cnt->get_counter_index()] = 0;
return true;
}
bool thread_counters::terminate(std::uint32_t)
{
std::lock_guard<mutex_type> m(mtx_);
return finalize();
}
bool thread_counters::start_all()
{
if (counts_.empty())
return true; // nothing to count
return PAPI_start(evset_) == PAPI_OK;
}
bool thread_counters::stop_all()
{
int stat;
if (PAPI_state(evset_, &stat) != PAPI_OK)
return false;
if ((stat & PAPI_RUNNING) != 0)
{
std::vector<long long> tmp(counts_.size());
if (PAPI_stop(evset_, &tmp[0]) != PAPI_OK)
return false;
// accumulate existing counts before modifying event set
if (PAPI_accum(evset_, &counts_[0]) != PAPI_OK)
return false;
timestamp_ =
static_cast<std::int64_t>(hpx::get_system_uptime());
}
return true;
}
bool thread_counters::finalize()
{
if (evset_ != PAPI_NULL)
{
if (stop_all())
{
for (std::uint32_t i = 0; i < cnttab_.size(); ++i)
{ // retrieve the most recent values for the still active counters
papi_counter* c = cnttab_[i];
if (c->get_status() == PAPI_COUNTER_ACTIVE)
c->update_state(timestamp_, counts_[i],
PAPI_COUNTER_TERMINATED);
else
c->update_status(PAPI_COUNTER_TERMINATED);
}
}
// release event set resources
return PAPI_destroy_eventset(&evset_) == PAPI_OK;
}
return true;
}
///////////////////////////////////////////////////////////////////////////
thread_counters* papi_counter_base::get_thread_counters(
std::uint32_t tix)
{
std::lock_guard<mutex_type> m(base_mtx_);
// create entry for the thread associated with the counter if it doesn't exist
ttable_type::iterator it = thread_state_.find(tix);
if (it == thread_state_.end())
{
hpx::util::ignore_all_while_checking i;
HPX_UNUSED(i);
return thread_state_[tix] = new thread_counters(tix);
}
else
return it->second;
}
///////////////////////////////////////////////////////////////////////////
papi_counter::papi_counter(
hpx::performance_counters::counter_info const& info)
: base_type_holder(info)
, event_(PAPI_NULL)
, index_(-1)
, value_(0)
, timestamp_(-1)
, status_(PAPI_COUNTER_STOPPED)
{
char const* locstr = NS_STR "papi_counter()";
// extract path elements from counter name
counter_path_elements cpe;
get_counter_path_elements(info.fullname_, cpe);
// convert event name to code and check availability
{
std::lock_guard<papi_counter_base::mutex_type> lk(
this->get_global_mtx());
// papi_call might throw, leading to potential "lock held while
// suspending thread" errors. At this point, it is safe to ignore
// the lock.
hpx::util::ignore_all_while_checking il;
HPX_UNUSED(il);
papi_call(PAPI_event_name_to_code(
const_cast<char*>(cpe.countername_.c_str()),
const_cast<int*>(&event_)),
cpe.countername_ +
" does not seem to be a valid event name",
locstr);
papi_call(PAPI_query_event(event_),
"event " + cpe.countername_ +
" is not available on this platform",
locstr);
}
// find OS thread associated with the counter
std::string label;
std::uint32_t tix = util::get_counter_thread(cpe, label);
if (tix == hpx::util::thread_mapper::invalid_index)
{
HPX_THROW_EXCEPTION(hpx::error::no_success, locstr,
"could not find thread " + label);
}
// associate low level counters object
counters_ = get_thread_counters(tix);
if (!counters_)
{
HPX_THROW_EXCEPTION(hpx::error::no_success, locstr,
"failed to find low level counters for thread " + label);
}
// counting is not enabled here; it has to be started explicitly
}
hpx::performance_counters::counter_value
papi_counter::get_counter_value(bool reset)
{
std::lock_guard<thread_counters::mutex_type> m(
counters_->get_lock());
if (status_ == PAPI_COUNTER_ACTIVE)
counters_->read_value(this, reset);
hpx::performance_counters::counter_value value;
if (timestamp_ != -1)
copy_value(value);
else
value.status_ =
hpx::performance_counters::counter_status::invalid_data;
// clear local copy
if (reset)
value_ = 0;
value.count_ = ++invocation_count_;
return value;
}
bool papi_counter::start()
{
std::lock_guard<papi_counter_base::mutex_type> lk(get_global_mtx());
if (status_ == PAPI_COUNTER_ACTIVE)
return true;
if (counters_->add_event(this))
{
status_ = PAPI_COUNTER_ACTIVE;
return true;
}
status_ = PAPI_COUNTER_SUSPENDED;
return false;
}
bool papi_counter::stop()
{
std::lock_guard<thread_counters::mutex_type> m(
counters_->get_lock());
return stop_counter();
}
void papi_counter::reset()
{
std::lock_guard<thread_counters::mutex_type> m(
counters_->get_lock());
reset_counter();
}
void papi_counter::finalize()
{
std::lock_guard<thread_counters::mutex_type> m(
counters_->get_lock());
stop_counter();
base_type_holder::finalize();
base_type::finalize();
}
naming::address papi_counter::get_current_address() const
{
return naming::address(
naming::get_gid_from_locality_id(hpx::get_locality_id()),
components::get_component_type<papi_counter>(),
const_cast<papi_counter*>(this));
}
}}}} // namespace hpx::performance_counters::papi::server
#endif