From 9b6453675a112d7067b6ebec5f47eafe8c5ede49 Mon Sep 17 00:00:00 2001 From: David Rose Date: Tue, 24 Feb 2009 20:41:20 +0000 Subject: [PATCH] report number of cpu's --- panda/src/configfiles/panda.prc.pp | 3 ++ panda/src/display/displayInformation.cxx | 27 ++++++++++ panda/src/display/displayInformation.h | 6 +++ panda/src/windisplay/winGraphicsPipe.cxx | 68 ++++++++++++++++++++++++ 4 files changed, 104 insertions(+) diff --git a/panda/src/configfiles/panda.prc.pp b/panda/src/configfiles/panda.prc.pp index 147b0a6adb..4ae9760667 100644 --- a/panda/src/configfiles/panda.prc.pp +++ b/panda/src/configfiles/panda.prc.pp @@ -28,6 +28,9 @@ aux-display pandagl #if $[HAVE_DX8] aux-display pandadx8 #endif +#if $[HAVE_TINYDISPLAY] +aux-display tinydisplay +#endif # The egg loader is handy to have available by default. This allows # clients to load egg files. (The bam loader is built-in so bam files diff --git a/panda/src/display/displayInformation.cxx b/panda/src/display/displayInformation.cxx index 6284538084..3c77112ec8 100644 --- a/panda/src/display/displayInformation.cxx +++ b/panda/src/display/displayInformation.cxx @@ -119,6 +119,9 @@ DisplayInformation() { _maximum_cpu_frequency = 0; _current_cpu_frequency = 0; + _num_cpu_cores = 0; + _num_logical_cpus = 0; + _get_memory_information_function = 0; _cpu_time_function = 0; _update_cpu_frequency_function = 0; @@ -667,6 +670,30 @@ update_cpu_frequency(int processor_number) { } } +//////////////////////////////////////////////////////////////////// +// Function: DisplayInformation::get_num_cpu_cores +// Access: Published +// Description: Returns the number of individual CPU cores in the +// system, or 0 if this number is not available. A +// hyperthreaded CPU counts once here. +//////////////////////////////////////////////////////////////////// +int DisplayInformation:: +get_num_cpu_cores() { + return _num_cpu_cores; +} + +//////////////////////////////////////////////////////////////////// +// Function: DisplayInformation::get_num_logical_cpus +// Access: Published +// Description: Returns the number of logical CPU's in the +// system, or 0 if this number is not available. A +// hyperthreaded CPU counts as two or more here. +//////////////////////////////////////////////////////////////////// +int DisplayInformation:: +get_num_logical_cpus() { + return _num_logical_cpus; +} + //////////////////////////////////////////////////////////////////// // Function: DisplayInformation::get_os_version_major // Access: Published diff --git a/panda/src/display/displayInformation.h b/panda/src/display/displayInformation.h index 9e7a487878..b9cc20470f 100644 --- a/panda/src/display/displayInformation.h +++ b/panda/src/display/displayInformation.h @@ -106,6 +106,9 @@ PUBLISHED: PN_uint64 get_current_cpu_frequency(); void update_cpu_frequency(int processor_number); + int get_num_cpu_cores(); + int get_num_logical_cpus(); + int get_os_version_major(); int get_os_version_minor(); int get_os_version_build(); @@ -164,6 +167,9 @@ public: PN_uint64 _maximum_cpu_frequency; PN_uint64 _current_cpu_frequency; + + int _num_cpu_cores; + int _num_logical_cpus; void (*_get_memory_information_function) (DisplayInformation *display_information); PN_uint64 (*_cpu_time_function) (void); diff --git a/panda/src/windisplay/winGraphicsPipe.cxx b/panda/src/windisplay/winGraphicsPipe.cxx index 3484e2aa4e..e53ee73d22 100644 --- a/panda/src/windisplay/winGraphicsPipe.cxx +++ b/panda/src/windisplay/winGraphicsPipe.cxx @@ -16,6 +16,7 @@ #include "config_windisplay.h" #include "displaySearchParameters.h" #include "dtool_config.h" +#include "pbitops.h" #include "psapi.h" #include "powrprof.h" @@ -710,6 +711,70 @@ int update_cpu_frequency_function (int processor_number, DisplayInformation *dis return update; } +void +count_number_of_cpus(DisplayInformation *display_information) { + int num_cpu_cores = 0; + int num_logical_cpus = 0; + + // Get a pointer to the GetLogicalProcessorInformation function. + typedef BOOL (WINAPI *LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, + PDWORD); + LPFN_GLPI glpi; + glpi = (LPFN_GLPI)GetProcAddress(GetModuleHandle(TEXT("kernel32")), + "GetLogicalProcessorInformation"); + if (glpi == NULL) { + windisplay_cat.info() + << "GetLogicalProcessorInformation is not supported.\n"; + return; + } + + // Allocate a buffer to hold the result of the + // GetLogicalProcessorInformation call. + PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL; + DWORD buffer_length = 0; + DWORD rc = glpi(buffer, &buffer_length); + while (!rc) { + if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { + if (buffer != NULL) { + PANDA_FREE_ARRAY(buffer); + } + + buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)PANDA_MALLOC_ARRAY(buffer_length); + nassertv(buffer != NULL); + } else { + windisplay_cat.info() + << "GetLogicalProcessorInformation failed: " << GetLastError() + << "\n"; + return; + } + rc = glpi(buffer, &buffer_length); + } + + // Now get the results. + PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = buffer; + PSYSTEM_LOGICAL_PROCESSOR_INFORMATION end = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)((char *)buffer + buffer_length); + + while (ptr < end) { + if (ptr->Relationship == RelationProcessorCore) { + num_cpu_cores++; + + // A hyperthreaded core supplies more than one logical processor. + num_logical_cpus += count_bits_in_word((PN_uint64)(ptr->ProcessorMask)); + } + ++ptr; + } + + PANDA_FREE_ARRAY(buffer); + + windisplay_cat.info() + << num_cpu_cores << " CPU cores, with " + << num_logical_cpus << " logical processors.\n"; + + display_information->_num_cpu_cores = num_cpu_cores; + display_information->_num_logical_cpus = num_logical_cpus; +} + + //////////////////////////////////////////////////////////////////// // Function: WinGraphicsPipe::Constructor // Access: Public @@ -851,6 +916,9 @@ WinGraphicsPipe() { windisplay_cat.info() << "end CPU ID\n"; + // Number of CPU's + count_number_of_cpus(_display_information); + OSVERSIONINFO version_info; version_info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);