diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2020-11-23 17:50:41 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-23 17:50:41 +0100 |
commit | 66445f857b81b051c1572409ce19d032cb03024c (patch) | |
tree | c64f7eb471229e0843c623c37cbf8e3fc69e8849 /vespalib | |
parent | bb001b8e430b0d85ec8a463d92940c843f0facea (diff) | |
parent | 7d53ab1b5a5e607b0e3023755b5ae4048cbb2c09 (diff) |
Merge pull request #15385 from vespa-engine/balder/reorder-for-smaller-footprint
Balder/reorder for smaller footprint
Diffstat (limited to 'vespalib')
-rw-r--r-- | vespalib/src/tests/trace/trace.cpp | 24 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/trace/trace.cpp | 72 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/trace/trace.h | 90 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/trace/tracenode.cpp | 69 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/trace/tracenode.h | 8 |
5 files changed, 139 insertions, 124 deletions
diff --git a/vespalib/src/tests/trace/trace.cpp b/vespalib/src/tests/trace/trace.cpp index 92bee3231b0..992317b0289 100644 --- a/vespalib/src/tests/trace/trace.cpp +++ b/vespalib/src/tests/trace/trace.cpp @@ -146,25 +146,25 @@ TEST("testTraceLevel") t.setLevel(4); EXPECT_EQUAL(4u, t.getLevel()); t.trace(9, "no"); - EXPECT_EQUAL(0u, t.getRoot().getNumChildren()); + EXPECT_EQUAL(0u, t.getNumChildren()); t.trace(8, "no"); - EXPECT_EQUAL(0u, t.getRoot().getNumChildren()); + EXPECT_EQUAL(0u, t.getNumChildren()); t.trace(7, "no"); - EXPECT_EQUAL(0u, t.getRoot().getNumChildren()); + EXPECT_EQUAL(0u, t.getNumChildren()); t.trace(6, "no"); - EXPECT_EQUAL(0u, t.getRoot().getNumChildren()); + EXPECT_EQUAL(0u, t.getNumChildren()); t.trace(5, "no"); - EXPECT_EQUAL(0u, t.getRoot().getNumChildren()); + EXPECT_EQUAL(0u, t.getNumChildren()); t.trace(4, "yes"); - EXPECT_EQUAL(1u, t.getRoot().getNumChildren()); + EXPECT_EQUAL(1u, t.getNumChildren()); t.trace(3, "yes"); - EXPECT_EQUAL(2u, t.getRoot().getNumChildren()); + EXPECT_EQUAL(2u, t.getNumChildren()); t.trace(2, "yes"); - EXPECT_EQUAL(3u, t.getRoot().getNumChildren()); + EXPECT_EQUAL(3u, t.getNumChildren()); t.trace(1, "yes"); - EXPECT_EQUAL(4u, t.getRoot().getNumChildren()); + EXPECT_EQUAL(4u, t.getNumChildren()); t.trace(0, "yes"); - EXPECT_EQUAL(5u, t.getRoot().getNumChildren()); + EXPECT_EQUAL(5u, t.getNumChildren()); } TEST("testCompact") @@ -261,10 +261,10 @@ TEST("testTraceDump") b1.addChild(b2); } for (int i = 0; i < 10; ++i) { - big.getRoot().addChild(b1); + big.addChild(TraceNode(b1)); } string normal = big.toString(); - string full = big.getRoot().toString(); + string full = big.toString(100000); EXPECT_GREATER(normal.size(), 30000u); EXPECT_LESS(normal.size(), 32000u); EXPECT_GREATER(full.size(), 50000u); diff --git a/vespalib/src/vespa/vespalib/trace/trace.cpp b/vespalib/src/vespa/vespalib/trace/trace.cpp index 8ca2ef6561c..be370aebbd2 100644 --- a/vespalib/src/vespa/vespalib/trace/trace.cpp +++ b/vespalib/src/vespa/vespalib/trace/trace.cpp @@ -6,43 +6,13 @@ namespace vespalib { -Trace::Trace() : - _level(0), - _root() +Trace::Trace(const Trace &rhs) + : _root(), + _level(rhs._level) { - // empty -} - -Trace::Trace(uint32_t level) : - _level(level), - _root() -{ - // empty -} - -Trace::~Trace() = default; - -Trace & -Trace::clear() -{ - _level = 0; - _root.clear(); - return *this; -} - -Trace & -Trace::swap(Trace &other) -{ - std::swap(_level, other._level); - _root.swap(other._root); - return *this; -} - -Trace & -Trace::setLevel(uint32_t level) -{ - _level = std::min(level, 9u); - return *this; + if (!rhs.isEmpty()) { + _root = std::make_unique<TraceNode>(rhs.getRoot()); + } } bool @@ -53,12 +23,36 @@ Trace::trace(uint32_t level, const string ¬e, bool addTime) } if (addTime) { struct timeval tv; - gettimeofday(&tv, NULL); - _root.addChild(make_string("[%ld.%06ld] %s", tv.tv_sec, static_cast<long>(tv.tv_usec), note.c_str())); + gettimeofday(&tv, nullptr); + ensureRoot().addChild(make_string("[%ld.%06ld] %s", tv.tv_sec, static_cast<long>(tv.tv_usec), note.c_str())); } else { - _root.addChild(note); + ensureRoot().addChild(note); } return true; } +string +Trace::toString(size_t limit) const { + return _root ? _root->toString(limit) : ""; +} + +string +Trace::encode() const { + return isEmpty() ? "" : getRoot().encode(); +} + +void +Trace::clear() { + _level = 0; + _root.reset(); +} + +TraceNode & +Trace::ensureRoot() { + if (!_root) { + _root = std::make_unique<TraceNode>(); + } + return *_root; +} + } // namespace vespalib diff --git a/vespalib/src/vespa/vespalib/trace/trace.h b/vespalib/src/vespa/vespalib/trace/trace.h index d1ca5c5d7e7..2f70785d77d 100644 --- a/vespalib/src/vespa/vespalib/trace/trace.h +++ b/vespalib/src/vespa/vespalib/trace/trace.h @@ -18,31 +18,23 @@ namespace vespalib { * information will be traced. */ class Trace { -private: - uint32_t _level; - TraceNode _root; - public: /** * Create an empty Trace with level set to 0 (no tracing) */ - Trace(); - ~Trace(); - - /** - * Create an empty trace with given level. - * - * @param level Level to set. - */ - explicit Trace(uint32_t level); + Trace() : Trace(0) {} + explicit Trace(uint32_t level) : _root(), _level(level) { } + Trace & operator = (Trace &&) = default; + Trace(Trace &&) = default; + Trace(const Trace &); + Trace & operator = (const Trace &) = delete; + ~Trace() = default; /** * Remove all trace information and set the trace level to 0. - * - * @return This, to allow chaining. */ - Trace &clear(); + void clear(); /** * Swap the internals of this with another. @@ -50,21 +42,16 @@ public: * @param other The trace to swap internals with. * @return This, to allow chaining. */ - Trace &swap(Trace &other); + Trace &swap(Trace &other) { + std::swap(_level, other._level); + _root.swap(other._root); + return *this; + } - /** - * Set the trace level. 0 means no tracing, 9 means enable all tracing. - * - * @param level The level to set. - * @return This, to allow chaining. - */ - Trace &setLevel(uint32_t level); + void setLevel(uint32_t level) { + _level = std::min(level, 9u); + } - /** - * Returns the trace level. - * - * @return The trace level. - */ uint32_t getLevel() const { return _level; } /** @@ -91,19 +78,29 @@ public: */ bool trace(uint32_t level, const string ¬e, bool addTime = true); - /** - * Returns the root of the trace tree. - * - * @return The root. - */ - TraceNode &getRoot() { return _root; } + void normalize() { + if (_root) { + _root->normalize(); + } + } - /** - * Returns a const reference to the root of the trace tree. - * - * @return The root. - */ - const TraceNode &getRoot() const { return _root; } + void setStrict(bool strict) { + ensureRoot().setStrict(strict); + } + void addChild(TraceNode && child) { + ensureRoot().addChild(std::move(child)); + } + void addChild(Trace && child) { + if (!child.isEmpty()) { + addChild(std::move(*child._root)); + } + } + + bool isEmpty() const { return !_root || _root->isEmpty(); } + + uint32_t getNumChildren() const { return _root ? _root->getNumChildren() : 0; } + const TraceNode & getChild(uint32_t child) const { return getRoot().getChild(child); } + string encode() const; /** * Returns a string representation of the contained trace tree. This is a @@ -111,7 +108,16 @@ public: * * @return Readable trace string. */ - string toString() const { return _root.toString(31337); } + string toString(size_t limit=31337) const; + size_t computeMemoryUsage() const { + return _root ? _root->computeMemoryUsage() : 0; + } +private: + const TraceNode &getRoot() const { return *_root; } + TraceNode &ensureRoot(); + + std::unique_ptr<TraceNode> _root; + uint32_t _level; }; #define VESPALIB_TRACE2(ttrace, level, note, addTime) \ diff --git a/vespalib/src/vespa/vespalib/trace/tracenode.cpp b/vespalib/src/vespa/vespalib/trace/tracenode.cpp index d34b45025d3..12dd51ac677 100644 --- a/vespalib/src/vespa/vespalib/trace/tracenode.cpp +++ b/vespalib/src/vespa/vespalib/trace/tracenode.cpp @@ -40,22 +40,22 @@ struct Cmp { } // namespace <unnamed> -TraceNode::TraceNode() : - _parent(nullptr), - _strict(true), - _hasNote(false), - _note(""), - _children(), - _timestamp() +TraceNode::TraceNode() + : _note(""), + _children(), + _parent(nullptr), + _timestamp(), + _strict(true), + _hasNote(false) { } -TraceNode::TraceNode(const TraceNode &rhs) : - _parent(nullptr), - _strict(rhs._strict), - _hasNote(rhs._hasNote), - _note(rhs._note), - _children(), - _timestamp(rhs._timestamp) +TraceNode::TraceNode(const TraceNode &rhs) + : _note(rhs._note), + _children(), + _parent(nullptr), + _timestamp(rhs._timestamp), + _strict(rhs._strict), + _hasNote(rhs._hasNote) { addChildren(rhs._children); } @@ -65,22 +65,22 @@ TraceNode & TraceNode::operator =(const TraceNode &) = default; TraceNode::~TraceNode() = default; -TraceNode::TraceNode(const string ¬e, system_time timestamp) : - _parent(nullptr), - _strict(true), - _hasNote(true), - _note(note), - _children(), - _timestamp(timestamp) +TraceNode::TraceNode(const string ¬e, system_time timestamp) + : _note(note), + _children(), + _parent(nullptr), + _timestamp(timestamp), + _strict(true), + _hasNote(true) { } -TraceNode::TraceNode(system_time timestamp) : - _parent(nullptr), - _strict(true), - _hasNote(false), - _note(""), - _children(), - _timestamp(timestamp) +TraceNode::TraceNode(system_time timestamp) + : _note(""), + _children(), + _parent(nullptr), + _timestamp(timestamp), + _strict(true), + _hasNote(false) { } TraceNode & @@ -342,4 +342,17 @@ TraceNode::accept(TraceVisitor & visitor) const return visitor; } +size_t +TraceNode::computeMemoryUsage() const +{ + if (isLeaf()) { + return getNote().size(); + } + size_t childSum = 0; + for (const TraceNode & child : _children) { + childSum += child.computeMemoryUsage(); + } + return childSum; +} + } // namespace vespalib diff --git a/vespalib/src/vespa/vespalib/trace/tracenode.h b/vespalib/src/vespa/vespalib/trace/tracenode.h index 63e7bcd6dc0..7a7cdb89c69 100644 --- a/vespalib/src/vespa/vespalib/trace/tracenode.h +++ b/vespalib/src/vespa/vespalib/trace/tracenode.h @@ -23,12 +23,12 @@ struct TraceVisitor; */ class TraceNode { private: - TraceNode *_parent; - bool _strict; - bool _hasNote; string _note; std::vector<TraceNode> _children; + TraceNode *_parent; system_time _timestamp; + bool _strict; + bool _hasNote; public: /** @@ -253,6 +253,8 @@ public: */ TraceVisitor & accept(TraceVisitor & visitor) const; + size_t computeMemoryUsage() const; + }; } // namespace vespalib |