libobs: Add functions to get logical/physical cores
parent
829ec5be2d
commit
6fc74d69a9
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -22,6 +22,8 @@
|
|||
#include <dlfcn.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/sysctl.h>
|
||||
|
||||
#include <CoreServices/CoreServices.h>
|
||||
#include <mach/mach.h>
|
||||
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue