aboutsummaryrefslogtreecommitdiffstats
path: root/vespamalloc
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2021-06-28 11:21:20 +0000
committerHenning Baldersheim <balder@yahoo-inc.com>2021-06-28 11:21:20 +0000
commit78153ab3b636f1b0642590cfd296ed8e19415c88 (patch)
treea2f69801527086e04b3d4b4eada7f7c64671853c /vespamalloc
parent21f1ed5bceb790564918ccb010d60fd85a87497c (diff)
Expose a way to access vespamalloc internals.
Diffstat (limited to 'vespamalloc')
-rw-r--r--vespamalloc/src/tests/stacktrace/stacktrace.cpp18
-rw-r--r--vespamalloc/src/tests/test1/new_test.cpp14
-rw-r--r--vespamalloc/src/vespamalloc/malloc/malloc.cpp4
-rw-r--r--vespamalloc/src/vespamalloc/malloc/malloc.h1
-rw-r--r--vespamalloc/src/vespamalloc/malloc/overload.h5
-rw-r--r--vespamalloc/src/vespamalloc/malloc/threadproxy.cpp3
-rw-r--r--vespamalloc/src/vespamalloc/malloc/vespamalloc.h20
7 files changed, 49 insertions, 16 deletions
diff --git a/vespamalloc/src/tests/stacktrace/stacktrace.cpp b/vespamalloc/src/tests/stacktrace/stacktrace.cpp
index d13fb6bb59b..e7c53fa5c3f 100644
--- a/vespamalloc/src/tests/stacktrace/stacktrace.cpp
+++ b/vespamalloc/src/tests/stacktrace/stacktrace.cpp
@@ -1,7 +1,9 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-#include <stdlib.h>
-#include <stdio.h>
+#include <cstdlib>
+#include <cstdio>
#include <pthread.h>
+#include <dlfcn.h>
+#include <cassert>
void * run(void * arg)
{
@@ -10,7 +12,11 @@ void * run(void * arg)
char * b = new char [1]; // but b should as it not deleted.
(void) b;
delete [] a;
- return NULL;
+ return nullptr;
+}
+
+void verify_that_vespamalloc_datasegment_size_exists() {
+ assert(dlsym(RTLD_NEXT, "vespamalloc_datasegment_size") != nullptr);
}
int main(int argc, char *argv[])
@@ -22,14 +28,16 @@ int main(int argc, char *argv[])
(void) b;
delete [] a;
pthread_t tid;
- int retval = pthread_create(&tid, NULL, run, NULL);
+ int retval = pthread_create(&tid, nullptr, run, nullptr);
if (retval != 0) {
perror("pthread_create failed");
abort();
}
- retval = pthread_join(tid, NULL);
+ retval = pthread_join(tid, nullptr);
if (retval != 0) {
perror("pthread_join failed");
abort();
}
+
+ verify_that_vespamalloc_datasegment_size_exists();
}
diff --git a/vespamalloc/src/tests/test1/new_test.cpp b/vespamalloc/src/tests/test1/new_test.cpp
index 2400e41a1d9..a8392363f6f 100644
--- a/vespamalloc/src/tests/test1/new_test.cpp
+++ b/vespamalloc/src/tests/test1/new_test.cpp
@@ -12,7 +12,7 @@ void cmp(const void *base, size_t offset, const void *p) {
}
template <typename S>
-void veryfy_aligned(S * p) {
+void verify_aligned(S * p) {
EXPECT_TRUE((uintptr_t(p) % alignof(S)) == 0);
memset(p, 0, sizeof(S));
}
@@ -26,7 +26,7 @@ TEST("verify new with normal alignment") {
static_assert(sizeof(S) == 24);
static_assert(alignof(S) == 8);
auto s = std::make_unique<S>();
- veryfy_aligned(s.get());
+ verify_aligned(s.get());
cmp(s.get(), &s->a);
cmp(s.get(), 8, &s->b);
cmp(s.get(), 16, &s->c);
@@ -42,7 +42,7 @@ TEST("verify new with alignment = 16") {
static_assert(sizeof(S) == 32);
static_assert(alignof(S) == 16);
auto s = std::make_unique<S>();
- veryfy_aligned(s.get());
+ verify_aligned(s.get());
cmp(s.get(), &s->a);
cmp(s.get(), 16, &s->b);
cmp(s.get(), 24, &s->c);
@@ -58,7 +58,7 @@ TEST("verify new with alignment = 32") {
static_assert(sizeof(S) == 64);
static_assert(alignof(S) == 32);
auto s = std::make_unique<S>();
- veryfy_aligned(s.get());
+ verify_aligned(s.get());
cmp(s.get(), &s->a);
cmp(s.get(), 32, &s->b);
cmp(s.get(), 40, &s->c);
@@ -74,7 +74,7 @@ TEST("verify new with alignment = 64") {
static_assert(sizeof(S) == 128);
static_assert(alignof(S) == 64);
auto s = std::make_unique<S>();
- veryfy_aligned(s.get());
+ verify_aligned(s.get());
cmp(s.get(), &s->a);
cmp(s.get(), 64, &s->b);
cmp(s.get(), 72, &s->c);
@@ -88,7 +88,7 @@ TEST("verify new with alignment = 64 with single element") {
static_assert(sizeof(S) == 64);
static_assert(alignof(S) == 64);
auto s = std::make_unique<S>();
- veryfy_aligned(s.get());
+ verify_aligned(s.get());
cmp(s.get(), &s->a);
LOG(info, "&s=%p", s.get());
}
@@ -100,7 +100,7 @@ TEST("verify new with alignment = 64 with single element") {
static_assert(sizeof(S) == 64);
static_assert(alignof(S) == 64);
auto s = std::make_unique<S>();
- veryfy_aligned(s.get());
+ verify_aligned(s.get());
cmp(s.get(), &s->a);
LOG(info, "&s=%p", s.get());
}
diff --git a/vespamalloc/src/vespamalloc/malloc/malloc.cpp b/vespamalloc/src/vespamalloc/malloc/malloc.cpp
index 380e2844f42..4a810c1ac5f 100644
--- a/vespamalloc/src/vespamalloc/malloc/malloc.cpp
+++ b/vespamalloc/src/vespamalloc/malloc/malloc.cpp
@@ -11,11 +11,11 @@ typedef ThreadListT<MemBlock, NoStat> ThreadList;
typedef MemoryWatcher<MemBlock, ThreadList> Allocator;
static char _Gmem[sizeof(Allocator)];
-static Allocator *_GmemP = NULL;
+static Allocator *_GmemP = nullptr;
static Allocator * createAllocator()
{
- if (_GmemP == NULL) {
+ if (_GmemP == nullptr) {
_GmemP = (Allocator *)1;
_GmemP = new (_Gmem) Allocator(-1, 0x7fffffffffffffffl);
}
diff --git a/vespamalloc/src/vespamalloc/malloc/malloc.h b/vespamalloc/src/vespamalloc/malloc/malloc.h
index df40197bbbd..49a7b765cc8 100644
--- a/vespamalloc/src/vespamalloc/malloc/malloc.h
+++ b/vespamalloc/src/vespamalloc/malloc/malloc.h
@@ -67,6 +67,7 @@ public:
_threadList.setParams(alwayReuseLimit, threadCacheLimit);
_allocPool.setParams(alwayReuseLimit, threadCacheLimit);
}
+ const DataSegment<MemBlockPtrT> & dataSegment() const { return _segment; }
private:
void freeSC(void *ptr, SizeClassT sc);
void crash() __attribute__((noinline));;
diff --git a/vespamalloc/src/vespamalloc/malloc/overload.h b/vespamalloc/src/vespamalloc/malloc/overload.h
index 56cd8101731..f9c51582fb9 100644
--- a/vespamalloc/src/vespamalloc/malloc/overload.h
+++ b/vespamalloc/src/vespamalloc/malloc/overload.h
@@ -105,6 +105,11 @@ void operator delete[](void* ptr, std::size_t sz, std::align_val_t alignment) no
extern "C" {
+unsigned long vespamalloc_datasegment_size() __attribute__((visibility ("default")));
+unsigned long vespamalloc_datasegment_size() {
+ return vespamalloc::_GmemP->dataSegment().dataSize();
+}
+
void * malloc(size_t sz) {
return vespamalloc::createAllocator()->malloc(sz);
}
diff --git a/vespamalloc/src/vespamalloc/malloc/threadproxy.cpp b/vespamalloc/src/vespamalloc/malloc/threadproxy.cpp
index fd3a1748b92..1fb2a92c71d 100644
--- a/vespamalloc/src/vespamalloc/malloc/threadproxy.cpp
+++ b/vespamalloc/src/vespamalloc/malloc/threadproxy.cpp
@@ -3,8 +3,7 @@
#include "threadproxy.h"
#include <dlfcn.h>
#include <pthread.h>
-#include <stdio.h>
-#include <errno.h>
+#include <cstdio>
namespace vespamalloc {
diff --git a/vespamalloc/src/vespamalloc/malloc/vespamalloc.h b/vespamalloc/src/vespamalloc/malloc/vespamalloc.h
new file mode 100644
index 00000000000..98ee34da676
--- /dev/null
+++ b/vespamalloc/src/vespamalloc/malloc/vespamalloc.h
@@ -0,0 +1,20 @@
+// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+#pragma once
+
+/**
+ * Contains the interfaces that vespamalloc offers in addition to the standard
+ * allocation interfaces like malloc, aclloc, free, new, delete, etc.
+ * Use dlsym(RTLD_NEXT, "function_name") to ensure the existence of the interface.
+ **/
+
+extern "C" {
+
+/**
+ * Reports the amount of memory vespamalloc uses. It is actually the size of the datasegment.
+ * This is the peak memory usage during process lifetime, and does not differentiate between malloced or freed memory.
+ * This is cheap to call
+ **/
+unsigned long vespamalloc_datasegment_size();
+
+} \ No newline at end of file