diff options
author | Hugo Locurcio <hugo.locurcio@hugo.pro> | 2020-12-27 01:50:21 +0100 |
---|---|---|
committer | Hugo Locurcio <hugo.locurcio@hugo.pro> | 2022-02-15 20:55:53 +0100 |
commit | ee7cd9a3a14415722c1086a3bde02c2e4d0fd0eb (patch) | |
tree | d9d653e44615c17cc3b33d47997ee0cc4ff7009a | |
parent | 171021145d49ffdda9110869d35ee63a946af9f8 (diff) |
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
`RenderingServer.get_video_adapter_name()` and
`RenderingServer.get_video_adapter_vendor()` for annotating benchmarks
and automatic graphics quality configuration.
-rw-r--r-- | core/core_bind.cpp | 5 | ||||
-rw-r--r-- | core/core_bind.h | 1 | ||||
-rw-r--r-- | core/os/os.cpp | 4 | ||||
-rw-r--r-- | core/os/os.h | 1 | ||||
-rw-r--r-- | doc/classes/OS.xml | 9 | ||||
-rw-r--r-- | platform/iphone/os_iphone.h | 1 | ||||
-rw-r--r-- | platform/iphone/os_iphone.mm | 10 | ||||
-rw-r--r-- | platform/linuxbsd/os_linuxbsd.cpp | 14 | ||||
-rw-r--r-- | platform/linuxbsd/os_linuxbsd.h | 1 | ||||
-rw-r--r-- | platform/osx/os_osx.h | 1 | ||||
-rw-r--r-- | platform/osx/os_osx.mm | 11 | ||||
-rw-r--r-- | platform/windows/os_windows.cpp | 20 | ||||
-rw-r--r-- | platform/windows/os_windows.h | 1 |
13 files changed, 78 insertions, 1 deletions
diff --git a/core/core_bind.cpp b/core/core_bind.cpp index b31cc18b7a..810284d5d0 100644 --- a/core/core_bind.cpp +++ b/core/core_bind.cpp @@ -362,6 +362,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(); } @@ -554,6 +558,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", "output", "read_stderr", "open_console"), &OS::execute, DEFVAL(Array()), DEFVAL(false), DEFVAL(false)); diff --git a/core/core_bind.h b/core/core_bind.h index 21a1fc2077..1ed243206b 100644 --- a/core/core_bind.h +++ b/core/core_bind.h @@ -218,6 +218,7 @@ public: 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 0032e8e4bc..9558a6978e 100644 --- a/core/os/os.cpp +++ b/core/os/os.cpp @@ -358,6 +358,10 @@ int OS::get_processor_count() const { return 1; } +String OS::get_processor_name() const { + return ""; +} + bool OS::can_use_threads() const { #ifdef NO_THREADS return false; diff --git a/core/os/os.h b/core/os/os.h index 188900a070..808d704b3d 100644 --- a/core/os/os.h +++ b/core/os/os.h @@ -302,6 +302,7 @@ public: virtual void set_exit_code(int p_code); virtual int get_processor_count() const; + virtual String get_processor_name() const; virtual int get_default_thread_pool_size() const { return get_processor_count(); } virtual String get_unique_id() const; diff --git a/doc/classes/OS.xml b/doc/classes/OS.xml index 13a19206b3..bc9bfc9676 100644 --- a/doc/classes/OS.xml +++ b/doc/classes/OS.xml @@ -343,7 +343,14 @@ <method name="get_processor_count" qualifiers="const"> <return type="int" /> <description> - 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. + </description> + </method> + <method name="get_processor_name" qualifiers="const"> + <return type="String" /> + <description> + 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. </description> </method> <method name="get_static_memory_peak_usage" qualifiers="const"> diff --git a/platform/iphone/os_iphone.h b/platform/iphone/os_iphone.h index aca6f5fe2b..3281ff0cdb 100644 --- a/platform/iphone/os_iphone.h +++ b/platform/iphone/os_iphone.h @@ -109,6 +109,7 @@ public: virtual String get_locale() const override; virtual String get_unique_id() const override; + virtual String get_processor_name() const override; virtual void vibrate_handheld(int p_duration_ms = 500) override; diff --git a/platform/iphone/os_iphone.mm b/platform/iphone/os_iphone.mm index 8350365d88..9fb6426b21 100644 --- a/platform/iphone/os_iphone.mm +++ b/platform/iphone/os_iphone.mm @@ -45,6 +45,7 @@ #import <AudioToolbox/AudioServices.h> #import <UIKit/UIKit.h> #import <dlfcn.h> +#include <sys/sysctl.h> #if defined(VULKAN_ENABLED) #include "servers/rendering/renderer_rd/renderer_compositor_rd.h" @@ -287,6 +288,15 @@ String OSIPhone::get_unique_id() const { return String::utf8([uuid UTF8String]); } +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/linuxbsd/os_linuxbsd.cpp b/platform/linuxbsd/os_linuxbsd.cpp index e95a865636..5e8656c79f 100644 --- a/platform/linuxbsd/os_linuxbsd.cpp +++ b/platform/linuxbsd/os_linuxbsd.cpp @@ -141,6 +141,20 @@ String OS_LinuxBSD::get_unique_id() const { return machine_id; } +String OS_LinuxBSD::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_LinuxBSD::finalize() { if (main_loop) { memdelete(main_loop); diff --git a/platform/linuxbsd/os_linuxbsd.h b/platform/linuxbsd/os_linuxbsd.h index d97a528ece..d3857e85f8 100644 --- a/platform/linuxbsd/os_linuxbsd.h +++ b/platform/linuxbsd/os_linuxbsd.h @@ -87,6 +87,7 @@ public: virtual Error shell_open(String p_uri) override; virtual String get_unique_id() const override; + virtual String get_processor_name() const override; virtual void alert(const String &p_alert, const String &p_title = "ALERT!") override; diff --git a/platform/osx/os_osx.h b/platform/osx/os_osx.h index 5bb5b3320e..53c5c8bd90 100644 --- a/platform/osx/os_osx.h +++ b/platform/osx/os_osx.h @@ -102,6 +102,7 @@ public: virtual Error create_instance(const List<String> &p_arguments, ProcessID *r_child_id = nullptr) override; virtual String get_unique_id() const override; + virtual String get_processor_name() const override; virtual bool _check_internal_feature_support(const String &p_feature) override; diff --git a/platform/osx/os_osx.mm b/platform/osx/os_osx.mm index 9288e658cf..868721e875 100644 --- a/platform/osx/os_osx.mm +++ b/platform/osx/os_osx.mm @@ -42,6 +42,8 @@ #include <dlfcn.h> #include <libproc.h> #include <mach-o/dyld.h> +#include <os/log.h> +#include <sys/sysctl.h> _FORCE_INLINE_ String OS_OSX::get_framework_executable(const String &p_path) { // Append framework executable name, or return as is if p_path is not a framework. @@ -72,6 +74,15 @@ void OS_OSX::initialize() { initialize_core(); } +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() { OS_Unix::initialize_core(); diff --git a/platform/windows/os_windows.cpp b/platform/windows/os_windows.cpp index 59f55b5dd2..99c9712834 100644 --- a/platform/windows/os_windows.cpp +++ b/platform/windows/os_windows.cpp @@ -625,6 +625,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.utf16().get_data()), 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::utf16((const char16_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.")); + } +} + void OS_Windows::run() { if (!main_loop) return; diff --git a/platform/windows/os_windows.h b/platform/windows/os_windows.h index bde663a27b..5bfd24327e 100644 --- a/platform/windows/os_windows.h +++ b/platform/windows/os_windows.h @@ -142,6 +142,7 @@ public: virtual String get_locale() const override; virtual int get_processor_count() const override; + virtual String get_processor_name() const override; virtual String get_config_path() const override; virtual String get_data_path() const override; |