diff --git a/libobs/obs-cocoa.c b/libobs/obs-cocoa.c index a1755c32e..33a2110e1 100644 --- a/libobs/obs-cocoa.c +++ b/libobs/obs-cocoa.c @@ -94,23 +94,8 @@ static void log_processor_speed(void) static void log_processor_cores(void) { - size_t size; - int physical_cores = 0, logical_cores = 0; - int ret; - - size = sizeof(physical_cores); - ret = sysctlbyname("machdep.cpu.core_count", &physical_cores, - &size, NULL, 0); - if (ret != 0) - return; - - ret = sysctlbyname("machdep.cpu.thread_count", &logical_cores, - &size, NULL, 0); - if (ret != 0) - return; - blog(LOG_INFO, "Physical Cores: %d, Logical Cores: %d", - physical_cores, logical_cores); + os_get_physical_cores(), os_get_logical_cores()); } static void log_available_memory(void) diff --git a/libobs/obs-windows.c b/libobs/obs-windows.c index e2c19c8eb..b8a139ec2 100644 --- a/libobs/obs-windows.c +++ b/libobs/obs-windows.c @@ -105,54 +105,10 @@ static void log_processor_info(void) RegCloseKey(key); } -static DWORD num_logical_cores(ULONG_PTR mask) -{ - DWORD left_shift = sizeof(ULONG_PTR) * 8 - 1; - DWORD bit_set_count = 0; - ULONG_PTR bit_test = (ULONG_PTR)1 << left_shift; - - for (DWORD i = 0; i <= left_shift; ++i) { - bit_set_count += ((mask & bit_test) ? 1 : 0); - bit_test /= 2; - } - - return bit_set_count; -} - static void log_processor_cores(void) { - PSYSTEM_LOGICAL_PROCESSOR_INFORMATION info = NULL, temp = NULL; - DWORD len = 0; - - GetLogicalProcessorInformation(info, &len); - if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) - return; - - info = malloc(len); - - if (GetLogicalProcessorInformation(info, &len)) { - DWORD num = len / sizeof(*info); - int physical_cores = 0; - int logical_cores = 0; - - temp = info; - - for (DWORD i = 0; i < num; i++) { - if (temp->Relationship == RelationProcessorCore) { - ULONG_PTR mask = temp->ProcessorMask; - - physical_cores++; - logical_cores += num_logical_cores(mask); - } - - temp++; - } - - blog(LOG_INFO, "Physical Cores: %d, Logical Cores: %d", - physical_cores, logical_cores); - } - - free(info); + blog(LOG_INFO, "Physical Cores: %d, Logical Cores: %d", + os_get_physical_cores(), os_get_logical_cores()); } static void log_available_memory(void) diff --git a/libobs/util/platform-cocoa.m b/libobs/util/platform-cocoa.m index 29076d6aa..b9f392c9c 100644 --- a/libobs/util/platform-cocoa.m +++ b/libobs/util/platform-cocoa.m @@ -22,6 +22,8 @@ #include #include #include +#include +#include #include #include @@ -312,3 +314,41 @@ void os_inhibit_sleep_destroy(os_inhibit_t *info) bfree(info); } } + +static int physical_cores = 0; +static int logical_cores = 0; +static bool core_count_initialized = false; + +static void os_get_cores_internal(void) +{ + if (core_count_initialized) + return; + + core_count_initialized = true; + + size_t size; + int ret; + + size = sizeof(physical_cores); + ret = sysctlbyname("machdep.cpu.core_count", &physical_cores, + &size, NULL, 0); + if (ret != 0) + return; + + ret = sysctlbyname("machdep.cpu.thread_count", &logical_cores, + &size, NULL, 0); +} + +int os_get_physical_cores(void) +{ + if (!core_count_initialized) + os_get_cores_internal(); + return physical_cores; +} + +int os_get_logical_cores(void) +{ + if (!core_count_initialized) + os_get_cores_internal(); + return logical_cores; +} diff --git a/libobs/util/platform-nix.c b/libobs/util/platform-nix.c index 5787c5484..20855455e 100644 --- a/libobs/util/platform-nix.c +++ b/libobs/util/platform-nix.c @@ -622,3 +622,60 @@ void os_breakpoint() { raise(SIGTRAP); } + +#ifndef __APPLE__ +static int physical_cores = 0; +static int logical_cores = 0; +static bool core_count_initialized = false; + +/* return sysconf(_SC_NPROCESSORS_ONLN); */ + +static void os_get_cores_internal(void) +{ + if (core_count_initialized) + return; + + core_count_initialized = true; + + logical_cores = sysconf(_SC_NPROCESSORS_ONLN); + +#ifndef __linux__ + physical_cores = logical_cores; +#else + char *text = os_quick_read_utf8_file("/proc/cpuinfo"); + char *core_id = text; + + if (!text || !*text) { + physical_cores = logical_cores; + return; + } + + for (;;) { + core_id = strstr(core_id, "\ncore id"); + if (!core_id) + break; + physical_cores++; + core_id++; + } + + if (physical_cores == 0) + physical_cores = logical_cores; + + bfree(text); +#endif +} + +int os_get_physical_cores(void) +{ + if (!core_count_initialized) + os_get_cores_internal(); + return physical_cores; +} + +int os_get_logical_cores(void) +{ + if (!core_count_initialized) + os_get_cores_internal(); + return logical_cores; +} +#endif diff --git a/libobs/util/platform-windows.c b/libobs/util/platform-windows.c index 022930dae..f5931a3af 100644 --- a/libobs/util/platform-windows.c +++ b/libobs/util/platform-windows.c @@ -879,3 +879,70 @@ void os_breakpoint(void) { __debugbreak(); } + +DWORD num_logical_cores(ULONG_PTR mask) +{ + DWORD left_shift = sizeof(ULONG_PTR) * 8 - 1; + DWORD bit_set_count = 0; + ULONG_PTR bit_test = (ULONG_PTR)1 << left_shift; + + for (DWORD i = 0; i <= left_shift; ++i) { + bit_set_count += ((mask & bit_test) ? 1 : 0); + bit_test /= 2; + } + + return bit_set_count; +} + +static int physical_cores = 0; +static int logical_cores = 0; +static bool core_count_initialized = false; + +static void os_get_cores_internal(void) +{ + PSYSTEM_LOGICAL_PROCESSOR_INFORMATION info = NULL, temp = NULL; + DWORD len = 0; + + if (core_count_initialized) + return; + + core_count_initialized = true; + + GetLogicalProcessorInformation(info, &len); + if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) + return; + + info = malloc(len); + + if (GetLogicalProcessorInformation(info, &len)) { + DWORD num = len / sizeof(*info); + temp = info; + + for (DWORD i = 0; i < num; i++) { + if (temp->Relationship == RelationProcessorCore) { + ULONG_PTR mask = temp->ProcessorMask; + + physical_cores++; + logical_cores += num_logical_cores(mask); + } + + temp++; + } + } + + free(info); +} + +int os_get_physical_cores(void) +{ + if (!core_count_initialized) + os_get_cores_internal(); + return physical_cores; +} + +int os_get_logical_cores(void) +{ + if (!core_count_initialized) + os_get_cores_internal(); + return logical_cores; +} diff --git a/libobs/util/platform.h b/libobs/util/platform.h index f4be48419..c552c7e2e 100644 --- a/libobs/util/platform.h +++ b/libobs/util/platform.h @@ -176,6 +176,9 @@ EXPORT void os_inhibit_sleep_destroy(os_inhibit_t *info); EXPORT void os_breakpoint(void); +EXPORT int os_get_physical_cores(void); +EXPORT int os_get_logical_cores(void); + #ifdef _MSC_VER #define strtoll _strtoi64 #if _MSC_VER < 1900