Repository: google/benchmark
File: src/perf_counters.cc (C++)

Code before:
  static const bool success = []() {
    return pfm_initialize() == PFM_SUCCESS;
  }();
  return success;
}

bool PerfCounters::IsCounterSupported(const std::string& name) {
  Initialize();
  perf_event_attr attr;
  std::memset(&attr, 0, sizeof(attr));
  pfm_perf_encode_arg_t arg;
  std::memset(&arg, 0, sizeof(arg));
  arg.attr = &attr;
  const int mode = PFM_PLM3;  // user mode only
  int ret = pfm_get_os_event_encoding(name.c_str(), mode, PFM_OS_PERF_EVENT_EXT,
                                      &arg);
  return (ret == PFM_SUCCESS);
}

static std::vector<uint64_t> GetPMUTypesForEvent(const perf_event_attr& attr) {
  // Replicate generic hardware events on all CPU PMUs.
  if (attr.type == PERF_TYPE_HARDWARE && attr.config < PERF_COUNT_HW_MAX) {
    std::vector<uint64_t> types;
    DIR* dir = opendir("/sys/bus/event_source/devices");
    if (!dir) {
      return {0};
    }
    while (dirent* ent = readdir(dir)) {
      std::string_view name_str = ent->d_name;
      auto node_path = [&](const char* node) {
        return std::string("/sys/bus/event_source/devices/") + ent->d_name +
               "/" + node;
      };
      struct stat st;
      if (name_str == "cpu" || name_str == "cpum_cf" ||
          stat(node_path("cpus").c_str(), &st) == 0 || errno != ENOENT) {
        int type_fd = open(node_path("type").c_str(), O_RDONLY);
        if (type_fd < 0) {
          closedir(dir);
          return {0};
        }
        char type_str[32] = {};
        ssize_t res = read(type_fd, type_str, sizeof(type_str) - 1);
        close(type_fd);
        if (res < 0) {
          closedir(dir);
          return {0};
        }
        uint64_t type;
        if (sscanf(type_str, "%" PRIu64, &type) != 1) {
          closedir(dir);

Review comment (bug):
Nitpick: i think it would be cleaner to have `QueryPMUTypesForEvent()`
that would return `None` when failing to read from the `/sys`,
and have `GetPMUTypesForEvent()` return the fall-back `{0}`.

Code after:
  // Function-scope static gets initialized only once on first call.
  static const bool success = []() {
    return pfm_initialize() == PFM_SUCCESS;
  }();
  return success;
}

bool PerfCounters::IsCounterSupported(const std::string& name) {
  Initialize();
  perf_event_attr attr;
  std::memset(&attr, 0, sizeof(attr));
  pfm_perf_encode_arg_t arg;
  std::memset(&arg, 0, sizeof(arg));
  arg.attr = &attr;
  const int mode = PFM_PLM3;  // user mode only
  int ret = pfm_get_os_event_encoding(name.c_str(), mode, PFM_OS_PERF_EVENT_EXT,
                                      &arg);
  return (ret == PFM_SUCCESS);
}

static std::optional<std::vector<uint64_t>> QueryCPUPMUTypes() {
  std::vector<uint64_t> types;
  DIR* dir = opendir("/sys/bus/event_source/devices");
  if (!dir) {
    return std::nullopt;
  }
  while (dirent* ent = readdir(dir)) {
    std::string_view name_str = ent->d_name;
    auto node_path = [&](const char* node) {
      return std::string("/sys/bus/event_source/devices/") + ent->d_name + "/" +
             node;
    };
    struct stat st;
    if (name_str == "cpu" || name_str == "cpum_cf" ||
        stat(node_path("cpus").c_str(), &st) == 0 || errno != ENOENT) {
      int type_fd = open(node_path("type").c_str(), O_RDONLY);
      if (type_fd < 0) {
        closedir(dir);
        return std::nullopt;
      }
      char type_str[32] = {};
      ssize_t res = read(type_fd, type_str, sizeof(type_str) - 1);
      close(type_fd);
      if (res < 0) {
        closedir(dir);
        return std::nullopt;
      }
      uint64_t type;
      if (sscanf(type_str, "%" PRIu64, &type) != 1) {
        closedir(dir);
        return std::nullopt;