From bffcaeff4130079d31f9f8997962b1379b5b9bde Mon Sep 17 00:00:00 2001 From: bubnikv Date: Tue, 18 Dec 2018 14:10:31 +0100 Subject: [PATCH] Time estimator: Added consumed memory tracing, replaced std::map with std::vector for lower memory consumption. --- src/libslic3r/GCode.cpp | 4 +++- src/libslic3r/GCodeTimeEstimator.cpp | 26 +++++++++++++++++++++----- src/libslic3r/GCodeTimeEstimator.hpp | 6 +++++- src/libslic3r/Utils.hpp | 2 ++ 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index 894e623aa..ef143a3e8 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -1231,7 +1231,7 @@ void GCode::process_layer( const Print &print, // Set of object & print layers of the same PrintObject and with the same print_z. const std::vector &layers, - const LayerTools &layer_tools, + const LayerTools &layer_tools, // If set to size_t(-1), then print all copies of all objects. // Otherwise print a single copy of a single object. const size_t single_object_idx) @@ -1644,6 +1644,8 @@ void GCode::process_layer( // printf("G-code after filter:\n%s\n", out.c_str()); _write(file, gcode); + BOOST_LOG_TRIVIAL(trace) << "Exported layer " << layer.id() << " print_z " << print_z << ", time estimator memory: " + + format_memsize_MB(m_normal_time_estimator.memory_used() + m_silent_time_estimator_enabled ? m_silent_time_estimator.memory_used() : 0); } void GCode::apply_print_config(const PrintConfig &print_config) diff --git a/src/libslic3r/GCodeTimeEstimator.cpp b/src/libslic3r/GCodeTimeEstimator.cpp index 5c8cc2659..0ab49a345 100644 --- a/src/libslic3r/GCodeTimeEstimator.cpp +++ b/src/libslic3r/GCodeTimeEstimator.cpp @@ -309,8 +309,9 @@ namespace Slic3r { gcode_line += "\n"; // add remaining time lines where needed + G1LineIdToBlockIdMap::const_iterator it_line_id = _g1_line_ids.begin(); _parser.parse_line(gcode_line, - [this, &g1_lines_count, &last_recorded_time, &time_line, &gcode_line, time_mask, interval](GCodeReader& reader, const GCodeReader::GCodeLine& line) + [this, &it_line_id, &g1_lines_count, &last_recorded_time, &time_line, &gcode_line, time_mask, interval](GCodeReader& reader, const GCodeReader::GCodeLine& line) { if (line.cmd_is("G1")) { @@ -319,10 +320,10 @@ namespace Slic3r { if (!line.has_e()) return; - G1LineIdToBlockIdMap::const_iterator it = _g1_line_ids.find(g1_lines_count); - if ((it != _g1_line_ids.end()) && (it->second < (unsigned int)_blocks.size())) + if ((it_line_id != _g1_line_ids.end()) && (it_line_id->first == g1_lines_count) && (it_line_id->second < (unsigned int)_blocks.size())) { - const Block& block = _blocks[it->second]; + const Block& block = _blocks[it_line_id->second]; + ++ it_line_id; if (block.elapsed_time != -1.0f) { float block_remaining_time = _time - block.elapsed_time; @@ -667,6 +668,21 @@ namespace Slic3r { return _get_time_minutes(get_time()); } + // Return an estimate of the memory consumed by the time estimator. + size_t GCodeTimeEstimator::memory_used() const + { + size_t out = sizeof(*this); +#if WIN32 + #define STDVEC_MEMSIZE(NAME, TYPE) NAME.capacity() * ((sizeof(TYPE) + __alignof(TYPE) - 1) / __alignof(TYPE)) * __alignof(TYPE) +#else + #define STDVEC_MEMSIZE(NAME, TYPE) NAME.capacity() * ((sizeof(TYPE) + alignof(TYPE) - 1) / alignof(TYPE)) * alignof(TYPE) +#endif + out += STDVEC_MEMSIZE(this->_blocks, Block); + out += STDVEC_MEMSIZE(this->_g1_line_ids, G1LineIdToBlockId); +#undef STDVEC_MEMSIZE + return out; + } + void GCodeTimeEstimator::_reset() { _curr.reset(); @@ -1072,7 +1088,7 @@ namespace Slic3r { // adds block to blocks list _blocks.emplace_back(block); - _g1_line_ids.insert(G1LineIdToBlockIdMap::value_type(get_g1_line_id(), (unsigned int)_blocks.size() - 1)); + _g1_line_ids.emplace_back(G1LineIdToBlockIdMap::value_type(get_g1_line_id(), (unsigned int)_blocks.size() - 1)); } void GCodeTimeEstimator::_processG4(const GCodeReader::GCodeLine& line) diff --git a/src/libslic3r/GCodeTimeEstimator.hpp b/src/libslic3r/GCodeTimeEstimator.hpp index e9da584c3..ef91d5ff1 100644 --- a/src/libslic3r/GCodeTimeEstimator.hpp +++ b/src/libslic3r/GCodeTimeEstimator.hpp @@ -209,7 +209,8 @@ namespace Slic3r { typedef std::map MovesStatsMap; #endif // ENABLE_MOVE_STATS - typedef std::map G1LineIdToBlockIdMap; + typedef std::pair G1LineIdToBlockId; + typedef std::vector G1LineIdToBlockIdMap; private: EMode _mode; @@ -338,6 +339,9 @@ namespace Slic3r { // Returns the estimated time, in minutes (integer) std::string get_time_minutes() const; + // Return an estimate of the memory consumed by the time estimator. + size_t memory_used() const; + private: void _reset(); void _reset_time(); diff --git a/src/libslic3r/Utils.hpp b/src/libslic3r/Utils.hpp index e34476d4c..f4f05ae66 100644 --- a/src/libslic3r/Utils.hpp +++ b/src/libslic3r/Utils.hpp @@ -12,6 +12,8 @@ namespace Slic3r { extern void set_logging_level(unsigned int level); extern void trace(unsigned int level, const char *message); +// Format memory allocated, separate thousands by comma. +extern std::string format_memsize_MB(size_t n); // Return string to be added to the boost::log output to inform about the current process memory allocation. // The string is non-empty only if the loglevel >= info (3). extern std::string log_memory_info();