Skip to content

Commit 001f4c7

Browse files
committed
Proper closing EventCouner and Sampler when opening failed.
1 parent aecef08 commit 001f4c7

3 files changed

Lines changed: 17 additions & 18 deletions

File tree

include/perfcpp/sampler.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1323,4 +1323,4 @@ class CounterComparator
13231323
private:
13241324
const Counter& _counter;
13251325
};
1326-
}
1326+
}

src/event_counter.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -466,16 +466,16 @@ perf::EventCounter::stop()
466466
void
467467
perf::EventCounter::close() noexcept
468468
{
469-
if (const auto is_open = std::exchange(this->_is_opened, false); is_open) {
470-
/// Close all groups.
471-
for (auto& [group, _] : this->_hardware_event_groups) {
472-
group.close();
473-
}
469+
this->_is_opened = false;
474470

475-
/// Close all live counters.
476-
for (auto& live_counter : this->_hardware_live_counters) {
477-
live_counter.close();
478-
}
471+
/// Close all groups, including groups that may have been partially opened before open() threw.
472+
for (auto& [group, _] : this->_hardware_event_groups) {
473+
group.close();
474+
}
475+
476+
/// Close all live counters, including counters that may have been partially opened before open() threw.
477+
for (auto& live_counter : this->_hardware_live_counters) {
478+
live_counter.close();
479479
}
480480
}
481481

@@ -849,4 +849,4 @@ perf::MultiCoreEventCounter::result_of_core(const std::uint16_t core_id, const s
849849
}
850850

851851
return std::nullopt;
852-
}
852+
}

src/sampler.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,13 @@ perf::Sampler::stop()
212212
void
213213
perf::Sampler::close() noexcept
214214
{
215-
if (std::exchange(this->_is_opened, false)) {
216-
/// Clear all buffers, groups, and event names
217-
/// in order to enable opening again.
218-
this->_sample_counter.clear();
215+
this->_is_opened = false;
219216

220-
/// Clear the sample data.
221-
this->_sample_data.clear();
222-
}
217+
/// Clear all buffers and groups, including groups that may have been partially opened before open() threw.
218+
this->_sample_counter.clear();
219+
220+
/// Clear the sample data.
221+
this->_sample_data.clear();
223222
}
224223

225224
perf::Sampler::SampleCounter

0 commit comments

Comments
 (0)