27#include <boost/version.hpp>
28#include <ql/version.hpp>
30#if defined(_WIN32) || defined(_WIN64)
38#include <sys/sysctl.h>
42#include <boost/algorithm/string/predicate.hpp>
43#include <boost/algorithm/string/trim.hpp>
45#include <sys/resource.h>
46#include <sys/utsname.h>
50#if defined(_WIN32) || defined(_WIN64)
51#include <boost/version.hpp>
52#if BOOST_VERSION > 106500
53#include <boost/stacktrace.hpp>
63string memoryString(
unsigned long long m) {
65 oss << fixed << setprecision(2);
68 else if (m < 1024 * 1024)
69 oss << m / (double)1024 <<
"kB";
70 else if (m < 1024 * 1024 * 1024)
71 oss << m / (double)(1024 * 1024) <<
"MB";
73 oss << m / (double)(1024 * 1024 * 1024) <<
"GB";
84 oss <<
"System Details:" << endl;
94 oss <<
" QuantLib Version : " << QL_VERSION << endl;
95 oss <<
" Boost Version : " << BOOST_LIB_VERSION << endl;
106#if defined(_WIN32) || defined(_WIN64)
110 return "Windows 64-bit";
112 return "Windows 32-bit";
118#pragma warning(disable : 4996)
122 ZeroMemory(&osvi,
sizeof(OSVERSIONINFO));
123 osvi.dwOSVersionInfoSize =
sizeof(OSVERSIONINFO);
128 oss << osvi.dwMajorVersion <<
"." << osvi.dwMinorVersion <<
"." << osvi.dwBuildNumber <<
" " << osvi.szCSDVersion;
136 int CPUInfo[4] = {-1};
137 char CPUBrandString[0x40];
138 __cpuid(CPUInfo, 0x80000000);
139 int nExIds = CPUInfo[0];
141 memset(CPUBrandString, 0,
sizeof(CPUBrandString));
144 for (
int i = 0x80000002; i <= nExIds; ++i) {
148 memcpy(CPUBrandString, CPUInfo,
sizeof(CPUInfo));
149 else if (i == 0x80000003)
150 memcpy(CPUBrandString + 16, CPUInfo,
sizeof(CPUInfo));
151 else if (i == 0x80000004)
152 memcpy(CPUBrandString + 32, CPUInfo,
sizeof(CPUInfo));
154 return CPUBrandString;
159 GetSystemInfo(&sysinfo);
160 return sysinfo.dwNumberOfProcessors;
164 unsigned long long mem = 0;
165 if (!GetPhysicallyInstalledSystemMemory(&mem))
167 return memoryString(mem * 1024);
171 PROCESS_MEMORY_COUNTERS info;
172 if (!GetProcessMemoryInfo(GetCurrentProcess(), &info,
sizeof(info)))
174 return info.WorkingSetSize;
178 PROCESS_MEMORY_COUNTERS info;
179 if (!GetProcessMemoryInfo(GetCurrentProcess(), &info,
sizeof(info)))
181 return info.PeakWorkingSetSize;
185 char acUserName[256 + 1];
186 DWORD nUserName =
sizeof(acUserName);
187 if (!GetUserName(acUserName, &nUserName))
189 acUserName[nUserName] =
'\0';
194 char buf[MAX_COMPUTERNAME_LENGTH + 1];
195 DWORD buflen =
sizeof(buf);
196 if (!GetComputerName(buf, &buflen))
209 getrusage(RUSAGE_SELF, &ru);
211 return (
size_t)ru.ru_maxrss;
213 return (
size_t)(ru.ru_maxrss * 1024L);
218 char* login = getlogin();
221 login = cuserid(NULL);
229 gethostname(buf, buflen);
230 buf[buflen - 1] =
'\0';
244 sysctlbyname(
"kern.osversion", &buf, &buflen, NULL, 0);
251 sysctlbyname(
"machdep.cpu.brand_string", &buf, &buflen, NULL, 0);
258 size_t len =
sizeof(ncpus);
259 sysctlbyname(
"hw.physicalcpu_max", &ncpus, &len, NULL, 0);
264 struct mach_task_basic_info info;
265 mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
266 if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info, &infoCount) != KERN_SUCCESS)
268 return (
unsigned long long)info.resident_size;
273 size_t len =
sizeof(mem);
274 sysctlbyname(
"hw.memsize", &mem, &len, NULL, 0);
275 return memoryString(mem);
285#if defined(__unix) || defined(__unix__)
297 struct utsname unameData;
298 if (uname(&unameData))
301 return unameData.release;
304static string parseProcFile(
const char* filename,
const string& nodename) {
305 ifstream ifile(filename);
306 if (ifile.is_open()) {
308 while (!ifile.eof()) {
309 getline(ifile, line);
310 if (boost::starts_with(line, nodename)) {
311 string result = line.substr(nodename.size() + 1);
313 if (result.size() > 0 && result.at(0) ==
':') {
315 result = result.substr(1);
327string getCpuName() {
return parseProcFile(
"/proc/cpuinfo",
"model name"); }
330 long n = sysconf(_SC_NPROCESSORS_ONLN);
338 unsigned long long rss = 0ULL;
340 if ((fp = fopen(
"/proc/self/statm",
"r")) == NULL)
342 if (fscanf(fp,
"%*s%llu", &rss) != 1) {
347 return (
unsigned long long)rss * (
unsigned long long)sysconf(_SC_PAGESIZE);
350string getMemoryRAM() {
return parseProcFile(
"/proc/meminfo",
"MemTotal"); }
364#if defined(_WIN32) || defined(_WIN64)
365#if BOOST_VERSION > 106500
366 boost::stacktrace::stacktrace st = boost::stacktrace::stacktrace();
367 unsigned st_size = st.size();
370 unsigned st_size = 0;
374 unsigned st_size = 0;
375 void* callstack[1024];
376 st_size = backtrace(callstack, 1024);
377 st = backtrace_symbols(callstack, st_size);
381 std::cerr <<
"Stacktrace " << st_size <<
" frames:" << std::endl;
382 ALOG(
"Stacktrace " << st_size <<
" frames:")
383 for (
unsigned int i = 0; i < st_size; i++) {
384 std::cerr <<
"#" << i <<
" " << st[i] << std::endl;
385 ALOG(
"#" << i <<
" " << st[i]);
388#if defined(_WIN32) || defined(_WIN64)
395#if defined(_WIN32) || defined(_WIN64)
396void setAssertHandler() {
LOG(
"oreplus::data::setAssertHandler() not defined for Windows") }
400static void _oreplus_handler(
int sig) {
401 ALOG(
"Received Signal " << sig)
406 ALOG(
"Setting ORE+ SigAction handler to intercept SIGABRT signals");
409 static bool sigaction_is_set =
false;
410 if (!sigaction_is_set) {
411 struct sigaction psa;
412 psa.sa_handler = _oreplus_handler;
413 sigaction(SIGABRT, &psa, NULL);
414 sigaction(SIGSEGV, &psa, NULL);
415 sigaction_is_set =
true;
string getPeakMemoryUsage()
Returns the current process peak memory usage.
string getCpuName()
Returns the CPU name (e.g. "Intel(R) Core(TM) i7-3720QM CPU @ 2.60GHz".
string getMemoryUsage()
Returns the current process memory usage.
string getHostname()
Returns the machine name.
string getUsername()
Returns the current username.
string getOsName()
Returns the OS Name.
void setAssertHandler()
Set an assert handler that logs the stacktrace.
string getMemoryRAM()
Returns the total amount of memory available (installed RAM)
void dumpStacktrace()
Write the current stacktrace to stderr and LOG() with level = ALERT.
string getOsVersion()
Returns the OS Version.
string getSystemDetails()
Returns all the above system details in a single string.
unsigned long long getPeakMemoryUsageBytes()
Returns the current process peak memory usage in bytes.
unsigned long long getMemoryUsageBytes()
unsigned int getNumberCores()
Returns the number of Cores available to the OS.
Classes and functions for log message handling.
#define LOG(text)
Logging Macro (Level = Notice)
#define ALOG(text)
Logging Macro (Level = Alert)
Serializable Credit Default Swap.
Various OS specific utilities.
ORE version as defined in QuantExt.
#define OPEN_SOURCE_RISK_VERSION