commit 3617535: [Minor] Track memory usage in mempool

Vsevolod Stakhov vsevolod at highsecure.ru
Thu Dec 12 20:28:07 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-12-12 19:50:20 +0000
URL: https://github.com/rspamd/rspamd/commit/3617535f341e5ea4df04b94e10f78c1f9ee5e814

[Minor] Track memory usage in mempool

---
 src/libutil/mem_pool.c | 6 ++++++
 src/libutil/mem_pool.h | 2 ++
 2 files changed, 8 insertions(+)

diff --git a/src/libutil/mem_pool.c b/src/libutil/mem_pool.c
index c01ce0c2c..ed3a6b73d 100644
--- a/src/libutil/mem_pool.c
+++ b/src/libutil/mem_pool.c
@@ -393,6 +393,8 @@ memory_pool_alloc_common (rspamd_mempool_t * pool, gsize size,
 
 	if (pool) {
 		POOL_MTX_LOCK ();
+		pool->used_memory += size;
+
 		if (always_malloc && pool_type != RSPAMD_MEMPOOL_SHARED) {
 			void *ptr;
 
@@ -416,6 +418,10 @@ memory_pool_alloc_common (rspamd_mempool_t * pool, gsize size,
 		}
 
 		if (cur == NULL || free < size) {
+			if (free < size) {
+				pool->wasted_memory += free;
+			}
+
 			/* Allocate new chain element */
 			if (pool->elt_len >= size + MIN_MEM_ALIGNMENT) {
 				pool->entry->elts[pool->entry->cur_elts].fragmentation += size;
diff --git a/src/libutil/mem_pool.h b/src/libutil/mem_pool.h
index 5a663e35f..a32aa0597 100644
--- a/src/libutil/mem_pool.h
+++ b/src/libutil/mem_pool.h
@@ -127,6 +127,8 @@ typedef struct memory_pool_s {
 	GPtrArray *trash_stack;
 	GHashTable *variables;                  /**< private memory pool variables			*/
 	gsize elt_len;                            /**< size of an element						*/
+	gsize used_memory;
+	gsize wasted_memory;
 	struct rspamd_mempool_entry_point *entry;
 	struct rspamd_mempool_tag tag;          /**< memory pool tag						*/
 } rspamd_mempool_t;


More information about the Commits mailing list