Skip to content

Commit

Permalink
Create wrappers around ioctl for more concise code
Browse files Browse the repository at this point in the history
  • Loading branch information
rocallahan committed Oct 17, 2023
1 parent 563bb88 commit 4f24986
Showing 1 changed file with 39 additions and 40 deletions.
79 changes: 39 additions & 40 deletions src/PerfCounters.cc
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,30 @@ static void make_counter_async(ScopedFd& fd, int signal) {
}
}

static void infallible_perf_event_reset_if_open(ScopedFd& fd) {
if (fd.is_open()) {
if (ioctl(fd, PERF_EVENT_IOC_RESET, 0)) {
FATAL() << "ioctl(PERF_EVENT_IOC_RESET) failed";
}
}
}

static void infallible_perf_event_enable_if_open(ScopedFd& fd) {
if (fd.is_open()) {
if (ioctl(fd, PERF_EVENT_IOC_ENABLE, 0)) {
FATAL() << "ioctl(PERF_EVENT_IOC_ENABLE) failed";
}
}
}

static void infallible_perf_event_disable_if_open(ScopedFd& fd) {
if (fd.is_open()) {
if (ioctl(fd, PERF_EVENT_IOC_ENABLE, 0)) {
FATAL() << "ioctl(PERF_EVENT_IOC_ENABLE) failed";
}
}
}

void PerfCounters::reset(Ticks ticks_period) {
if (!enable) {
return;
Expand Down Expand Up @@ -668,40 +692,21 @@ void PerfCounters::reset(Ticks ticks_period) {
} else {
LOG(debug) << "Resetting counters with period " << ticks_period;

if (ioctl(fd_ticks_interrupt, PERF_EVENT_IOC_RESET, 0)) {
FATAL() << "ioctl(PERF_EVENT_IOC_RESET) failed";
}
infallible_perf_event_reset_if_open(fd_ticks_interrupt);
if (ioctl(fd_ticks_interrupt, PERF_EVENT_IOC_PERIOD, &ticks_period)) {
FATAL() << "ioctl(PERF_EVENT_IOC_PERIOD) failed with period "
<< ticks_period;
}
if (ioctl(fd_ticks_interrupt, PERF_EVENT_IOC_ENABLE, 0)) {
FATAL() << "ioctl(PERF_EVENT_IOC_ENABLE) failed";
}
if (fd_minus_ticks_measure.is_open()) {
if (ioctl(fd_minus_ticks_measure, PERF_EVENT_IOC_RESET, 0)) {
FATAL() << "ioctl(PERF_EVENT_IOC_RESET) failed";
}
if (ioctl(fd_minus_ticks_measure, PERF_EVENT_IOC_ENABLE, 0)) {
FATAL() << "ioctl(PERF_EVENT_IOC_ENABLE) failed";
}
}
if (fd_ticks_measure.is_open()) {
if (ioctl(fd_ticks_measure, PERF_EVENT_IOC_RESET, 0)) {
FATAL() << "ioctl(PERF_EVENT_IOC_RESET) failed";
}
if (ioctl(fd_ticks_measure, PERF_EVENT_IOC_ENABLE, 0)) {
FATAL() << "ioctl(PERF_EVENT_IOC_ENABLE) failed";
}
}
if (fd_ticks_in_transaction.is_open()) {
if (ioctl(fd_ticks_in_transaction, PERF_EVENT_IOC_RESET, 0)) {
FATAL() << "ioctl(PERF_EVENT_IOC_RESET) failed";
}
if (ioctl(fd_ticks_in_transaction, PERF_EVENT_IOC_ENABLE, 0)) {
FATAL() << "ioctl(PERF_EVENT_IOC_ENABLE) failed";
}
}
infallible_perf_event_enable_if_open(fd_ticks_interrupt);

infallible_perf_event_reset_if_open(fd_minus_ticks_measure);
infallible_perf_event_enable_if_open(fd_minus_ticks_measure);

infallible_perf_event_reset_if_open(fd_ticks_measure);
infallible_perf_event_enable_if_open(fd_ticks_measure);

infallible_perf_event_reset_if_open(fd_ticks_in_transaction);
infallible_perf_event_enable_if_open(fd_ticks_in_transaction);
}

started = true;
Expand Down Expand Up @@ -735,16 +740,10 @@ void PerfCounters::stop_counting() {
if (always_recreate_counters(perf_attrs[pmu_index])) {
stop();
} else {
ioctl(fd_ticks_interrupt, PERF_EVENT_IOC_DISABLE, 0);
if (fd_minus_ticks_measure.is_open()) {
ioctl(fd_minus_ticks_measure, PERF_EVENT_IOC_DISABLE, 0);
}
if (fd_ticks_measure.is_open()) {
ioctl(fd_ticks_measure, PERF_EVENT_IOC_DISABLE, 0);
}
if (fd_ticks_in_transaction.is_open()) {
ioctl(fd_ticks_in_transaction, PERF_EVENT_IOC_DISABLE, 0);
}
infallible_perf_event_disable_if_open(fd_ticks_interrupt);
infallible_perf_event_disable_if_open(fd_minus_ticks_measure);
infallible_perf_event_disable_if_open(fd_ticks_measure);
infallible_perf_event_disable_if_open(fd_ticks_in_transaction);
}
}

Expand Down

0 comments on commit 4f24986

Please sign in to comment.