commit f72b297d7728f4f2ef623a49352223faec45a30b parent dd78272a5e3183e02f847254b8fca97c2c546885 Author: William Casarin <jb55@jb55.com> Date: Sat, 2 Dec 2023 13:47:17 -0800 nostrdb: add cpu helper Diffstat:
A | nostrdb/cpu.h | | | 34 | ++++++++++++++++++++++++++++++++++ |
1 file changed, 34 insertions(+), 0 deletions(-)
diff --git a/nostrdb/cpu.h b/nostrdb/cpu.h @@ -0,0 +1,34 @@ + +#if defined(_WIN32) || defined(_WIN64) + #include <windows.h> +#elif defined(__linux__) + #include <unistd.h> +#elif defined(__APPLE__) + #include <sys/types.h> + #include <sys/sysctl.h> +#else + #error "Unsupported platform" +#endif + +static inline int get_cpu_cores() { + int num_cores = 0; + + // Windows + #if defined(_WIN32) || defined(_WIN64) + SYSTEM_INFO sysinfo; + GetSystemInfo(&sysinfo); + num_cores = sysinfo.dwNumberOfProcessors; // This returns logical processors + // Further use GetLogicalProcessorInformation for physical cores... + // Linux + #elif defined(__linux__) + num_cores = sysconf(_SC_NPROCESSORS_ONLN); // This returns logical processors + // macOS + #elif defined(__APPLE__) + size_t size = sizeof(num_cores); + sysctlbyname("hw.physicalcpu", &num_cores, &size, NULL, 0); + #else + num_cores = -1; // Unsupported platform + #endif + + return num_cores; +}