From 7f9e974a8b770f76244ad662da9134574ac515c3 Mon Sep 17 00:00:00 2001 From: Hugo Locurcio Date: Tue, 15 Feb 2022 22:25:51 +0100 Subject: [PATCH] Add an `OS.get_processor_name()` method This method can be used to get the CPU model name. It can be used in conjunction with `VisualServer.get_video_adapter_name()` and `VisualServer.get_video_adapter_vendor()` for annotating benchmarks and automatic graphics quality configuration. --- core/bind/core_bind.cpp | 5 +++++ core/bind/core_bind.h | 1 + core/os/os.cpp | 4 ++++ core/os/os.h | 1 + doc/classes/OS.xml | 9 ++++++++- platform/iphone/os_iphone.h | 1 + platform/iphone/os_iphone.mm | 10 ++++++++++ platform/osx/os_osx.h | 1 + platform/osx/os_osx.mm | 14 +++++++++++--- platform/windows/os_windows.cpp | 20 ++++++++++++++++++++ platform/windows/os_windows.h | 1 + platform/x11/os_x11.cpp | 14 ++++++++++++++ platform/x11/os_x11.h | 1 + 13 files changed, 78 insertions(+), 4 deletions(-) diff --git a/core/bind/core_bind.cpp b/core/bind/core_bind.cpp index 24f1d67e570e..e20988c9349a 100644 --- a/core/bind/core_bind.cpp +++ b/core/bind/core_bind.cpp @@ -960,6 +960,10 @@ int _OS::get_processor_count() const { return OS::get_singleton()->get_processor_count(); } +String _OS::get_processor_name() const { + return OS::get_singleton()->get_processor_name(); +} + bool _OS::is_stdout_verbose() const { return OS::get_singleton()->is_stdout_verbose(); } @@ -1319,6 +1323,7 @@ void _OS::_bind_methods() { ClassDB::bind_method(D_METHOD("get_low_processor_usage_mode_sleep_usec"), &_OS::get_low_processor_usage_mode_sleep_usec); ClassDB::bind_method(D_METHOD("get_processor_count"), &_OS::get_processor_count); + ClassDB::bind_method(D_METHOD("get_processor_name"), &_OS::get_processor_name); ClassDB::bind_method(D_METHOD("get_executable_path"), &_OS::get_executable_path); ClassDB::bind_method(D_METHOD("execute", "path", "arguments", "blocking", "output", "read_stderr", "open_console"), &_OS::execute, DEFVAL(true), DEFVAL(Array()), DEFVAL(false), DEFVAL(false)); diff --git a/core/bind/core_bind.h b/core/bind/core_bind.h index 7e9b70be900f..2f1c9e32506f 100644 --- a/core/bind/core_bind.h +++ b/core/bind/core_bind.h @@ -327,6 +327,7 @@ class _OS : public Object { bool is_stdout_verbose() const; int get_processor_count() const; + String get_processor_name() const; enum SystemDir { SYSTEM_DIR_DESKTOP, diff --git a/core/os/os.cpp b/core/os/os.cpp index aecd3d2dc910..51040a993f88 100644 --- a/core/os/os.cpp +++ b/core/os/os.cpp @@ -512,6 +512,10 @@ int OS::get_processor_count() const { return 1; } +String OS::get_processor_name() const { + return ""; +} + Error OS::native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track) { return FAILED; } diff --git a/core/os/os.h b/core/os/os.h index a61cf414c951..27c29ccfebb2 100644 --- a/core/os/os.h +++ b/core/os/os.h @@ -519,6 +519,7 @@ class OS { virtual bool is_custom_exit_code(); virtual int get_processor_count() const; + virtual String get_processor_name() const; virtual String get_unique_id() const; diff --git a/doc/classes/OS.xml b/doc/classes/OS.xml index 85801f4af94c..910eaf6ca8ce 100644 --- a/doc/classes/OS.xml +++ b/doc/classes/OS.xml @@ -345,7 +345,14 @@ - Returns the number of threads available on the host machine. + Returns the number of [i]logical[/i] CPU cores available on the host machine. On CPUs with HyperThreading enabled, this number will be greater than the number of [i]physical[/i] CPU cores. + + + + + + Returns the name of the CPU model on the host machine (e.g. "Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz"). + [b]Note:[/b] This method is only implemented on Windows, macOS, Linux and iOS. On Android, HTML5 and UWP, [method get_processor_name] returns an empty string. diff --git a/platform/iphone/os_iphone.h b/platform/iphone/os_iphone.h index fd115cadc525..ac6165e9598e 100644 --- a/platform/iphone/os_iphone.h +++ b/platform/iphone/os_iphone.h @@ -123,6 +123,7 @@ class OSIPhone : public OS_Unix { String get_locale() const; String get_unique_id() const; + virtual String get_processor_name() const; virtual void vibrate_handheld(int p_duration_ms = 500); diff --git a/platform/iphone/os_iphone.mm b/platform/iphone/os_iphone.mm index 0598b3d78cdb..7c27aebcb5e8 100644 --- a/platform/iphone/os_iphone.mm +++ b/platform/iphone/os_iphone.mm @@ -54,6 +54,7 @@ #import #include +#include #import extern int gl_view_base_fb; // from gl_view.mm @@ -665,6 +666,15 @@ void register_dynamic_symbol(char *name, void *address) { } } +String OSIPhone::get_processor_name() const { + char buffer[256]; + size_t buffer_len = 256; + if (sysctlbyname("machdep.cpu.brand_string", &buffer, &buffer_len, NULL, 0) == 0) { + return String::utf8(buffer, buffer_len); + } + ERR_FAIL_V_MSG("", String("Couldn't get the CPU model name. Returning an empty string.")); +} + void OSIPhone::vibrate_handheld(int p_duration_ms) { // iOS does not support duration for vibration AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); diff --git a/platform/osx/os_osx.h b/platform/osx/os_osx.h index f31234a25277..e3bd7b937941 100644 --- a/platform/osx/os_osx.h +++ b/platform/osx/os_osx.h @@ -310,6 +310,7 @@ class OS_OSX : public OS_Unix { virtual String get_ime_text() const; virtual String get_unique_id() const; + virtual String get_processor_name() const; virtual OS::PowerState get_power_state(); virtual int get_power_seconds_left(); diff --git a/platform/osx/os_osx.mm b/platform/osx/os_osx.mm index cad7dccdde49..a662ba81a6e1 100644 --- a/platform/osx/os_osx.mm +++ b/platform/osx/os_osx.mm @@ -41,6 +41,8 @@ #include "servers/visual/visual_server_raster.h" #include +#include +#include #include #import @@ -48,9 +50,6 @@ #include #include #include -#if MAC_OS_X_VERSION_MAX_ALLOWED >= 101200 -#include -#endif #include #include @@ -1510,6 +1509,15 @@ - (BOOL)canBecomeKeyWindow { im_position = p_pos; } +String OS_OSX::get_processor_name() const { + char buffer[256]; + size_t buffer_len = 256; + if (sysctlbyname("machdep.cpu.brand_string", &buffer, &buffer_len, NULL, 0) == 0) { + return String::utf8(buffer, buffer_len); + } + ERR_FAIL_V_MSG("", String("Couldn't get the CPU model name. Returning an empty string.")); +} + void OS_OSX::initialize_core() { crash_handler.initialize(); diff --git a/platform/windows/os_windows.cpp b/platform/windows/os_windows.cpp index 77009c49c95a..07acf5ee27ed 100644 --- a/platform/windows/os_windows.cpp +++ b/platform/windows/os_windows.cpp @@ -3130,6 +3130,26 @@ int OS_Windows::get_processor_count() const { return sysinfo.dwNumberOfProcessors; } +String OS_Windows::get_processor_name() const { + const String id = "Hardware\\Description\\System\\CentralProcessor\\0"; + + HKEY hkey; + if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, (LPCWSTR)(id.c_str()), 0, KEY_QUERY_VALUE, &hkey) != ERROR_SUCCESS) { + ERR_FAIL_V_MSG("", String("Couldn't get the CPU model name. Returning an empty string.")); + } + + WCHAR buffer[256]; + DWORD buffer_len = 256; + DWORD vtype = REG_SZ; + if (RegQueryValueExW(hkey, L"ProcessorNameString", NULL, &vtype, (LPBYTE)buffer, &buffer_len) == ERROR_SUCCESS) { + RegCloseKey(hkey); + return String((const wchar_t *)buffer, buffer_len).strip_edges(); + } else { + RegCloseKey(hkey); + ERR_FAIL_V_MSG("", String("Couldn't get the CPU model name. Returning an empty string.")); + } +} + OS::LatinKeyboardVariant OS_Windows::get_latin_keyboard_variant() const { unsigned long azerty[] = { 0x00020401, // Arabic (102) AZERTY diff --git a/platform/windows/os_windows.h b/platform/windows/os_windows.h index 6dea669f50d2..e05fc06292c5 100644 --- a/platform/windows/os_windows.h +++ b/platform/windows/os_windows.h @@ -520,6 +520,7 @@ class OS_Windows : public OS { virtual String get_locale() const; virtual int get_processor_count() const; + virtual String get_processor_name() const; virtual LatinKeyboardVariant get_latin_keyboard_variant() const; virtual int keyboard_get_layout_count() const; diff --git a/platform/x11/os_x11.cpp b/platform/x11/os_x11.cpp index eb4ecba942a3..2abbbd022ba5 100644 --- a/platform/x11/os_x11.cpp +++ b/platform/x11/os_x11.cpp @@ -806,6 +806,20 @@ String OS_X11::get_unique_id() const { return machine_id; } +String OS_X11::get_processor_name() const { + FileAccessRef f = FileAccess::open("/proc/cpuinfo", FileAccess::READ); + ERR_FAIL_COND_V_MSG(!f, "", String("Couldn't open `/proc/cpuinfo` to get the CPU model name. Returning an empty string.")); + + while (!f->eof_reached()) { + const String line = f->get_line(); + if (line.find("model name") != -1) { + return line.split(":")[1].strip_edges(); + } + } + + ERR_FAIL_V_MSG("", String("Couldn't get the CPU model name from `/proc/cpuinfo`. Returning an empty string.")); +} + void OS_X11::finalize() { events_thread_done = true; events_thread.wait_to_finish(); diff --git a/platform/x11/os_x11.h b/platform/x11/os_x11.h index 6b9d32516eef..908e5a61f697 100644 --- a/platform/x11/os_x11.h +++ b/platform/x11/os_x11.h @@ -329,6 +329,7 @@ class OS_X11 : public OS_Unix { virtual void set_ime_position(const Point2 &p_pos); virtual String get_unique_id() const; + virtual String get_processor_name() const; virtual void move_window_to_foreground(); virtual void alert(const String &p_alert, const String &p_title = "ALERT!");