| 214 | |
| 215 | |
| 216 | void System::get_hardware( |
| 217 | int& core_count, |
| 218 | unsigned long long& kilobytes_free) const |
| 219 | { |
| 220 | kilobytes_free = 0; |
| 221 | core_count = System::get_cores(); |
| 222 | |
| 223 | #if defined(_WIN32) || defined(__CYGWIN__) |
| 224 | // Using GlobalMemoryStatusEx instead of GlobalMemoryStatus |
| 225 | // was suggested by Lorne Anderson. |
| 226 | MEMORYSTATUSEX statex; |
| 227 | statex.dwLength = sizeof(statex); |
| 228 | GlobalMemoryStatusEx(&statex); |
| 229 | kilobytes_free = static_cast<unsigned long long>( |
| 230 | statex.ullTotalPhys / 1024); |
| 231 | |
| 232 | SYSTEM_INFO sysinfo; |
| 233 | GetSystemInfo(&sysinfo); |
| 234 | core_count = static_cast<int>(sysinfo.dwNumberOfProcessors); |
| 235 | return; |
| 236 | #endif |
| 237 | |
| 238 | #ifdef __APPLE__ |
| 239 | // The code for Mac OS X was suggested by Matthew Kidd. |
| 240 | |
| 241 | // This is physical memory, rather than "free" memory as below |
| 242 | // for Linux. Always leave 0.5 GB for the OS and other stuff. |
| 243 | // It would be better to find free memory (how?) but in practice |
| 244 | // the number of cores rather than free memory is almost certainly |
| 245 | // the limit for Macs which have standardized hardware (whereas |
| 246 | // say a 32 core Linux server is hardly unusual). |
| 247 | FILE * fifo = popen("sysctl -n hw.memsize", "r"); |
| 248 | fscanf(fifo, "%lld", &kilobytes_free); |
| 249 | fclose(fifo); |
| 250 | |
| 251 | kilobytes_free /= 1024; |
| 252 | if (kilobytes_free > 500000) |
| 253 | { |
| 254 | kilobytes_free -= 500000; |
| 255 | } |
| 256 | |
| 257 | core_count = sysconf(_SC_NPROCESSORS_ONLN); |
| 258 | return; |
| 259 | #endif |
| 260 | |
| 261 | #ifdef __linux__ |
| 262 | // Use half of the physical memory |
| 263 | long pages = sysconf (_SC_PHYS_PAGES); |
| 264 | long pagesize = sysconf (_SC_PAGESIZE); |
| 265 | if (pages > 0 && pagesize > 0) |
| 266 | kilobytes_free = static_cast<unsigned long long>(pages * pagesize / 1024 / 2); |
| 267 | else |
| 268 | kilobytes_free = 1024 * 1024; // guess 1GB |
| 269 | |
| 270 | core_count = sysconf(_SC_NPROCESSORS_ONLN); |
| 271 | return; |
| 272 | #endif |
| 273 | } |