22 #if defined(HW_NCPU) || defined(__APPLE__)
23 #include <sys/sysctl.h>
28 #elif defined __APPLE__
29 #include <CoreServices/CoreServices.h>
37 MEMORYSTATUSEX status;
38 status.dwLength =
sizeof(status);
39 GlobalMemoryStatusEx(&status);
43 return std::min<unsigned long long>(status.ullTotalPhys, 1500*1024*1024);
45 return status.ullTotalPhys;
48 #elif defined __APPLE__
52 if(Gestalt(gestaltPhysicalRAMSizeInMegabytes, &ramSize)==noErr)
54 unsigned long long _ramSize = ramSize;
55 return _ramSize * 1024 * 1024;
61 return 1024*1024*1024;
67 long pages = sysconf(_SC_PHYS_PAGES);
68 long page_size = sysconf(_SC_PAGE_SIZE);
69 return pages * page_size;
77 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX procInfoTotal = NULL;
81 if (GetLogicalProcessorInformationEx(RelationProcessorCore, procInfoTotal, &length))
85 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
89 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX tmpprocInfoTotal = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX)realloc(procInfoTotal, length);
90 if (!tmpprocInfoTotal)
95 procInfoTotal = tmpprocInfoTotal;
98 unsigned int cpuCount = 0;
99 for (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX procInfo = procInfoTotal;
100 (
void*)procInfo < (
void*)((uintptr_t)procInfoTotal + length);
101 procInfo = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX)((uintptr_t)procInfo + procInfo->Size))
103 if (procInfo->Relationship == RelationProcessorCore)
109 return cpuCount > 0 ? cpuCount : 1;
111 #elif defined(__APPLE__) || defined(__FreeBSD__)
112 #include <sys/sysctl.h>
116 uint32_t num_cores = 0;
117 size_t num_cores_len =
sizeof(num_cores);
118 sysctlbyname(
"hw.physicalcpu", &num_cores, &num_cores_len, 0, 0);
131 if ((fp = fopen(
"/proc/cpuinfo",
"r")))
133 while (fgets(str,
sizeof(str), fp))
135 if (memcmp(str,
"core id", 7) == 0)
142 return cpuCount > 0 ? cpuCount : 1;
unsigned long long getTotalMemory()
returns the total memory in byte
unsigned int GetPhysicalCPUCount()
return the number of physical cpu cores