From 773319228a89fff503b2cc4bf9a7f950d7933bc1 Mon Sep 17 00:00:00 2001 From: "zhou.xu" Date: Fri, 9 May 2025 09:52:04 +0800 Subject: [PATCH] FIX:Less than 5 GB of free memory will not apply for lod jira: STUDIO-12063 Change-Id: Id55f609cc62371ae8660e0487d2894dff6bcf836 --- src/slic3r/CMakeLists.txt | 2 + src/slic3r/GUI/GCodeViewer.cpp | 2 +- src/slic3r/GUI/GLCanvas3D.cpp | 11 +++-- src/slic3r/GUI/GLCanvas3D.hpp | 2 +- src/slic3r/Utils/CpuMemory.cpp | 76 ++++++++++++++++++++++++++++++++++ src/slic3r/Utils/CpuMemory.hpp | 12 ++++++ 6 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 src/slic3r/Utils/CpuMemory.cpp create mode 100644 src/slic3r/Utils/CpuMemory.hpp diff --git a/src/slic3r/CMakeLists.txt b/src/slic3r/CMakeLists.txt index 602f5c1ed..31dc131d0 100644 --- a/src/slic3r/CMakeLists.txt +++ b/src/slic3r/CMakeLists.txt @@ -539,6 +539,8 @@ set(SLIC3R_GUI_SOURCES Utils/NetworkAgent.hpp Utils/MKS.hpp Utils/MKS.cpp + Utils/CpuMemory.cpp + Utils/CpuMemory.hpp Utils/Duet.cpp Utils/Duet.hpp Utils/FlashAir.cpp diff --git a/src/slic3r/GUI/GCodeViewer.cpp b/src/slic3r/GUI/GCodeViewer.cpp index 90fef3f84..a814be5ba 100644 --- a/src/slic3r/GUI/GCodeViewer.cpp +++ b/src/slic3r/GUI/GCodeViewer.cpp @@ -3219,7 +3219,7 @@ void GCodeViewer::load_shells(const Print& print, bool initialized, bool force_p // BBS: fix the issue that object_idx is not assigned as index of Model.objects array int object_count = 0; const ModelObjectPtrs& model_objs = wxGetApp().model().objects; - bool enable_lod = GUI::wxGetApp().app_config->get("enable_lod") == "true"; + bool enable_lod = false; for (const PrintObject* obj : print.objects()) { const ModelObject* model_obj = obj->model_object(); diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index 9ec358eed..23c51354f 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -38,7 +38,7 @@ #include "format.hpp" #include "DailyTips.hpp" #include "FilamentMapDialog.hpp" - +#include "../Utils/CpuMemory.hpp" #if ENABLE_RETINA_GL #include "slic3r/Utils/RetinaHelper.hpp" #endif @@ -1363,7 +1363,7 @@ GLCanvas3D::GLCanvas3D(wxGLCanvas* canvas, Bed3D &bed) GLCanvas3D::~GLCanvas3D() { - reset_volumes(); + reset_volumes(false); m_sel_plate_toolbar.del_all_item(); m_sel_plate_toolbar.del_stats_item(); @@ -1529,7 +1529,7 @@ unsigned int GLCanvas3D::get_volumes_count() const return (unsigned int)m_volumes.volumes.size(); } -void GLCanvas3D::reset_volumes() +void GLCanvas3D::reset_volumes(bool set_notice) { if (!m_initialized) return; @@ -1542,8 +1542,7 @@ void GLCanvas3D::reset_volumes() m_selection.clear(); m_volumes.clear(); m_dirty = true; - - _set_warning_notification(EWarning::ObjectOutside, false); + if (set_notice) { _set_warning_notification(EWarning::ObjectOutside, false); } } //BBS: get current plater's bounding box @@ -2989,7 +2988,7 @@ void GLCanvas3D::reload_scene(bool refresh_immediately, bool force_full_scene_re } } m_volumes.volumes = std::move(glvolumes_new); - bool enable_lod = GUI::wxGetApp().app_config->get("enable_lod") == "true"; + bool enable_lod = GUI::wxGetApp().app_config->get_bool("enable_lod") && CpuMemory::cur_free_memory_less_than_specify_size_gb(LOD_FREE_MEMORY_SIZE); for (unsigned int obj_idx = 0; obj_idx < (unsigned int)m_model->objects.size(); ++ obj_idx) { const ModelObject &model_object = *m_model->objects[obj_idx]; for (int volume_idx = 0; volume_idx < (int)model_object.volumes.size(); ++ volume_idx) { diff --git a/src/slic3r/GUI/GLCanvas3D.hpp b/src/slic3r/GUI/GLCanvas3D.hpp index 06bf41c2e..0fc5176df 100644 --- a/src/slic3r/GUI/GLCanvas3D.hpp +++ b/src/slic3r/GUI/GLCanvas3D.hpp @@ -804,7 +804,7 @@ public: unsigned int get_volumes_count() const; const GLVolumeCollection& get_volumes() const { return m_volumes; } - void reset_volumes(); + void reset_volumes(bool set_notice = true); ModelInstanceEPrintVolumeState check_volumes_outside_state(ObjectFilamentResults* object_results = nullptr) const; bool is_all_plates_selected() { return m_sel_plate_toolbar.m_all_plates_stats_item && m_sel_plate_toolbar.m_all_plates_stats_item->selected; } const float get_scale() const; diff --git a/src/slic3r/Utils/CpuMemory.cpp b/src/slic3r/Utils/CpuMemory.cpp new file mode 100644 index 000000000..b027faebd --- /dev/null +++ b/src/slic3r/Utils/CpuMemory.cpp @@ -0,0 +1,76 @@ +#include "CpuMemory.hpp" + +#include +namespace Slic3r { +#ifdef _WIN32 +#include +unsigned long long get_free_memory_win() +{ + MEMORYSTATUSEX status; + status.dwLength = sizeof(status); + GlobalMemoryStatusEx(&status); + return status.ullAvailPhys; +} +#endif + +#ifdef __linux__ +#include +#include +#elif __APPLE__ +#include +#include +#endif +#if defined(__linux__) || defined(__APPLE__) +unsigned long long get_free_memory_unix() +{ +#ifdef __linux__ + struct sysinfo info; + if (sysinfo(&info) == 0) { + return info.freeram * info.mem_unit; + } +#elif __APPLE__ + int mib[2] = {CTL_HW, HW_MEMSIZE}; + uint64_t memsize; + size_t len = sizeof(memsize); + if (sysctl(mib, 2, &memsize, &len, NULL, 0) == 0) { + vm_size_t page_size; + mach_port_t mach_port; + mach_msg_type_number_t count; + vm_statistics64_data_t vm_stats; + + mach_port = mach_host_self(); + count = sizeof(vm_stats) / sizeof(natural_t); + if (host_page_size(mach_port, &page_size) == KERN_SUCCESS && host_statistics64(mach_port, HOST_VM_INFO, (host_info64_t) &vm_stats, &count) == KERN_SUCCESS) { + return (vm_stats.free_count + vm_stats.inactive_count) * page_size; + } + } +#endif + return 0; +} +#endif +unsigned long long get_free_memory() +{ +#ifdef _WIN32 + return get_free_memory_win(); +#elif defined(__linux__) || defined(__APPLE__) + return get_free_memory_unix(); +#else + return 0; +#endif +} +bool CpuMemory::cur_free_memory_less_than_specify_size_gb(int size) +{ + unsigned long long free_mem = get_free_memory(); + auto cur_size = free_mem / (1024.0 * 1024.0 * 1024.0); + static bool first_debug_free_memory = true; + if (first_debug_free_memory) { + first_debug_free_memory = false; + BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << " cur_size = " << cur_size << "GB"; + } + if (cur_size > size) { + return true; + } + return false; +} + +} // namespace Slic3r diff --git a/src/slic3r/Utils/CpuMemory.hpp b/src/slic3r/Utils/CpuMemory.hpp new file mode 100644 index 000000000..3b46ab344 --- /dev/null +++ b/src/slic3r/Utils/CpuMemory.hpp @@ -0,0 +1,12 @@ +#ifndef slic3r_CpuMemory_hpp_ +#define slic3r_CpuMemory_hpp_ + +namespace Slic3r { +#define LOD_FREE_MEMORY_SIZE 5 +class CpuMemory +{ +public: + static bool cur_free_memory_less_than_specify_size_gb(int size); +}; +} // namespace Slic3r +#endif