summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArne Juul <arnej@verizonmedia.com>2020-09-08 11:58:29 +0000
committerArne Juul <arnej@verizonmedia.com>2020-09-09 10:32:38 +0000
commit30777d5b3d2f6f0fe921341ba04c7f68650e68f2 (patch)
tree25669a3eefda513ed05472ea465c1473d466acb0
parentcd0e5fae5156294354ad589b0148853266c79500 (diff)
add get_memory_usage for Stash
-rw-r--r--vespalib/src/vespa/vespalib/util/stash.cpp23
-rw-r--r--vespalib/src/vespa/vespalib/util/stash.h5
2 files changed, 25 insertions, 3 deletions
diff --git a/vespalib/src/vespa/vespalib/util/stash.cpp b/vespalib/src/vespa/vespalib/util/stash.cpp
index 836654c2fb2..31580e871db 100644
--- a/vespalib/src/vespa/vespalib/util/stash.cpp
+++ b/vespalib/src/vespa/vespalib/util/stash.cpp
@@ -53,8 +53,9 @@ Stash::do_alloc(size_t size)
_chunks = new (chunk_mem) stash::Chunk(_chunks);
return _chunks->alloc(size, _chunk_size);
} else {
- char *mem = static_cast<char*>(malloc(sizeof(stash::DeleteMemory) + size));
- _cleanup = new (mem) stash::DeleteMemory(_cleanup);
+ size_t allocate = sizeof(stash::DeleteMemory) + size;
+ char *mem = static_cast<char*>(malloc(allocate));
+ _cleanup = new (mem) stash::DeleteMemory(allocate, _cleanup);
return (mem + sizeof(stash::DeleteMemory));
}
}
@@ -121,4 +122,22 @@ Stash::count_used() const
return used;
}
+MemoryUsage
+Stash::get_memory_usage() const
+{
+ size_t allocated = 0;
+ size_t used = 0;
+ for (stash::Chunk *chunk = _chunks; chunk != nullptr; chunk = chunk->next) {
+ allocated += _chunk_size;
+ used += chunk->used;
+ }
+ for (auto cleanup = _cleanup; cleanup; cleanup = cleanup->next) {
+ if (auto memory = dynamic_cast<stash::DeleteMemory *>(cleanup)) {
+ allocated += memory->allocated;
+ used += memory->allocated;
+ }
+ }
+ return MemoryUsage(allocated, used, 0, 0);
+};
+
} // namespace vespalib
diff --git a/vespalib/src/vespa/vespalib/util/stash.h b/vespalib/src/vespa/vespalib/util/stash.h
index c5e8631ca9e..48ac16080c0 100644
--- a/vespalib/src/vespa/vespalib/util/stash.h
+++ b/vespalib/src/vespa/vespalib/util/stash.h
@@ -4,6 +4,7 @@
#include "traits.h"
#include "arrayref.h"
+#include "memoryusage.h"
#include <cstdlib>
namespace vespalib {
@@ -19,8 +20,9 @@ protected:
// used as header for memory allocated outside the stash
struct DeleteMemory : public Cleanup {
- explicit DeleteMemory(Cleanup *next_in) noexcept : Cleanup(next_in) {}
+ explicit DeleteMemory(size_t sz, Cleanup *next_in) noexcept : Cleanup(next_in), allocated(sz) {}
void cleanup() override { free((void*)this); }
+ size_t allocated;
};
// used as prefix for objects to be destructed
@@ -140,6 +142,7 @@ public:
size_t count_used() const;
size_t get_chunk_size() const { return _chunk_size; }
+ MemoryUsage get_memory_usage() const;
char *alloc(size_t size) {
char *ret = __builtin_expect(is_small(size) && _chunks != nullptr, true)