diff --git a/src/ContextSwitchEvent.cc b/src/ContextSwitchEvent.cc index 4eb45e69a12..94aecab83af 100644 --- a/src/ContextSwitchEvent.cc +++ b/src/ContextSwitchEvent.cc @@ -20,19 +20,6 @@ using namespace std; namespace rr { -static optional read_perf_event_paranoid() { - ScopedFd fd("/proc/sys/kernel/perf_event_paranoid", O_RDONLY); - if (fd.is_open()) { - char buf[100]; - ssize_t size = read(fd, buf, sizeof(buf) - 1); - if (size >= 0) { - buf[size] = 0; - return atoi(buf); - } - } - return nullopt; -} - static volatile int sigio_count; static void sigio_handler(int, siginfo_t*, void*) { diff --git a/src/util.cc b/src/util.cc index ec5294214c4..f55765d9494 100644 --- a/src/util.cc +++ b/src/util.cc @@ -2600,4 +2600,23 @@ void base_name(string& s) { } } +static optional init_read_perf_event_paranoid() { + ScopedFd fd("/proc/sys/kernel/perf_event_paranoid", O_RDONLY); + if (fd.is_open()) { + char buf[100]; + ssize_t size = read(fd, buf, sizeof(buf) - 1); + if (size >= 0) { + buf[size] = 0; + return atoi(buf); + } + } + + return nullopt; +} + +optional read_perf_event_paranoid() { + static optional value = init_read_perf_event_paranoid(); + return value; +} + } // namespace rr diff --git a/src/util.h b/src/util.h index e45c5a8b68a..9a8459dd549 100644 --- a/src/util.h +++ b/src/util.h @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -666,6 +667,8 @@ void replace_in_buffer(MemoryRange src, const uint8_t* src_data, // Strip any directory part from the filename `s` void base_name(std::string& s); +std::optional read_perf_event_paranoid(); + } // namespace rr #endif /* RR_UTIL_H_ */