diff --git a/core/core_bind.cpp b/core/core_bind.cpp index b31cc18b7ad..810284d5d07 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 21a1fc20774..1ed243206b1 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 0032e8e4bc9..9558a6978ef 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 188900a0705..808d704b3da 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 13a19206b30..bc9bfc96761 100644 --- a/doc/classes/OS.xml +++ b/doc/classes/OS.xml @@ -343,7 +343,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 aca6f5fe2be..3281ff0cdb7 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 8350365d888..9fb6426b210 100644 --- a/platform/iphone/os_iphone.mm +++ b/platform/iphone/os_iphone.mm @@ -45,6 +45,7 @@ #import #import #import +#include #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 e95a8656367..5e8656c79f6 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 d97a528ece3..d3857e85f8f 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 5bb5b3320e4..53c5c8bd90e 100644 --- a/platform/osx/os_osx.h +++ b/platform/osx/os_osx.h @@ -102,6 +102,7 @@ public: virtual Error create_instance(const List &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 9288e658cf8..868721e8755 100644 --- a/platform/osx/os_osx.mm +++ b/platform/osx/os_osx.mm @@ -42,6 +42,8 @@ #include #include #include +#include +#include _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 59f55b5dd2e..99c97128345 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 bde663a27be..5bfd24327ec 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;