29 #ifndef PIRANHA_RUNTIME_INFO_HPP 30 #define PIRANHA_RUNTIME_INFO_HPP 32 #if defined(__linux__) 34 #include <boost/lexical_cast.hpp> 40 #include <sys/sysinfo.h> 44 #elif defined(__FreeBSD__) 47 #include <sys/sysctl.h> 48 #include <sys/types.h> 61 #elif defined(__APPLE_CC__) 66 #include <sys/sysctl.h> 75 #include <piranha/config.hpp> 77 #include <piranha/runtime_info.hpp> 78 #include <piranha/safe_cast.hpp> 98 #if defined(__linux__) 99 int candidate = ::get_nprocs();
100 return (candidate <= 0) ? 0u :
static_cast<unsigned>(candidate);
101 #elif defined(__FreeBSD__) 103 std::size_t size =
sizeof(count);
104 return ::sysctlbyname(
"hw.ncpu", &count, &size, NULL, 0) ? 0u :
static_cast<unsigned>(count);
105 #elif defined(_WIN32) 106 ::SYSTEM_INFO info = ::SYSTEM_INFO();
107 ::GetSystemInfo(&info);
110 unsigned retval = 0u;
112 retval =
safe_cast<
unsigned>(info.dwNumberOfProcessors);
117 #elif defined(__APPLE_CC__) 119 return safe_cast<
unsigned>(::sysconf(_SC_NPROCESSORS_ONLN));
126 return std::thread::hardware_concurrency();
135 #if defined(__linux__) 136 auto ls = ::sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
140 std::ifstream sys_file(
"/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size");
141 if (sys_file.is_open() && sys_file.good()) {
144 std::getline(sys_file, line);
145 ls = boost::lexical_cast<decltype(ls)>(line);
151 unsigned retval = 0u;
160 #elif defined(_WIN32) && defined(PIRANHA_HAVE_SYSTEM_LOGICAL_PROCESSOR_INFORMATION) 163 std::size_t line_size = 0u;
164 ::DWORD buffer_size = 0u;
165 ::SYSTEM_LOGICAL_PROCESSOR_INFORMATION *buffer = 0;
167 const auto retval = ::GetLogicalProcessorInformation(0, &buffer_size);
168 if (retval || ::GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
181 buffer = (::SYSTEM_LOGICAL_PROCESSOR_INFORMATION *)std::malloc(safe_cast<std::size_t>(buffer_size));
182 if (unlikely(!buffer)) {
185 if (!::GetLogicalProcessorInformation(&buffer[0u], &buffer_size)) {
189 for (::DWORD i = 0u; i != buffer_size /
sizeof(::SYSTEM_LOGICAL_PROCESSOR_INFORMATION); ++i) {
190 if (buffer[i].Relationship == ::_LOGICAL_PROCESSOR_RELATIONSHIP::RelationCache
191 && buffer[i].Cache.Level == 1) {
192 line_size = buffer[i].Cache.LineSize;
201 #elif defined(__APPLE_CC__) 205 std::size_t ls, size =
sizeof(ls);
207 return ::sysctlbyname(
"hw.cachelinesize", &ls, &size, NULL, 0) ? 0u :
safe_cast<
unsigned>(ls);
static unsigned get_cache_line_size()
Size of the data cache line.
#define piranha_throw(exception_type,...)
Exception-throwing macro.
static unsigned get_hardware_concurrency()
Hardware concurrency.
safe_cast_type< To, From > safe_cast(const From &x)
Safe cast.