Line Code
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
#include <string>
#include <boost/predef/os.h>

#if BOOST_OS_LINUX
// Linux
#include <fstream>
#include <regex>
#endif  // BOOST_OS_LINUX

#if BOOST_OS_BSD || BOOST_OS_MACOS
// OSX or BSD
#include <sys/types.h>
#include <sys/sysctl.h>
#endif  // BOOST_OS_BSD || BOOST_OS_MACOS

#include "HLTrigger/Timer/interface/processor_model.h"

std::string read_processor_model() {
  std::string result = "unknown";

#if BOOST_OS_LINUX
  // on Linux, read the processor model from /proc/cpuinfo,
  // and check the status of SMT from /sys/devices/system/cpu/smt/active
  static const std::regex pattern("^model name\\s*:\\s*(.*)", std::regex::optimize);
  std::smatch match;

  std::ifstream cpuinfo("/proc/cpuinfo", std::ios::in);
  std::string line;
  while (cpuinfo.good()) {
    std::getline(cpuinfo, line);
    if (std::regex_match(line, match, pattern)) {
      result = match[1];
      break;
    }
  }

  std::ifstream smtinfo("/sys/devices/system/cpu/smt/active", std::ios::in);
  if (smtinfo) {
    int status;
    smtinfo >> status;
    result += status ? " with SMT enabled" : " with SMT disabled";
  }
#endif  // BOOST_OS_LINUX

#if BOOST_OS_BSD || BOOST_OS_MACOS
  // on BSD and OS X, read the processor  model via sysctlbyname("machdep.cpu.brand_string", ...)
  std::string result;
  size_t len;
  sysctlbyname("machdep.cpu.brand_string", nullptr, &len, NULL, 0);
  result.resize(len);
  sysctlbyname("machdep.cpu.brand_string", result.data(), &len, NULL, 0);
#endif  // BOOST_OS_BSD || BOOST_OS_MACOS

  return result;
}

const std::string processor_model = read_processor_model();