fast_mpool add stat fields: alloc_count, alloc_bytes and reset_count

vote_node
YuQing 2022-06-04 22:05:09 +08:00
parent 0b539bbba2
commit 7e5acf144b
2 changed files with 45 additions and 16 deletions

View File

@ -48,6 +48,10 @@ int fast_mpool_init(struct fast_mpool_man *mpool,
mpool->malloc_chain_head = NULL;
mpool->free_chain_head = NULL;
mpool->alloc_count = 0;
mpool->alloc_bytes = 0;
mpool->reset.count = 0;
mpool->reset.last_alloc_count = 0;
return 0;
}
@ -138,6 +142,8 @@ static inline void *fast_mpool_do_alloc(struct fast_mpool_man *mpool,
fast_mpool_remove_free_node(mpool, pMallocNode);
}
mpool->alloc_count++;
mpool->alloc_bytes += size;
return ptr;
}
return NULL;
@ -198,6 +204,13 @@ void fast_mpool_reset(struct fast_mpool_man *mpool)
{
struct fast_mpool_malloc *pMallocNode;
mpool->reset.count++;
if (mpool->reset.last_alloc_count == mpool->alloc_count)
{
return;
}
mpool->reset.last_alloc_count = mpool->alloc_count;
mpool->free_chain_head = NULL;
pMallocNode = mpool->malloc_chain_head;
while (pMallocNode != NULL)
@ -243,12 +256,22 @@ void fast_mpool_stats(struct fast_mpool_man *mpool,
void fast_mpool_log_stats(struct fast_mpool_man *mpool)
{
struct fast_mpool_stats stats;
char sz_total_bytes[32];
char sz_free_bytes[32];
char sz_alloc_bytes[32];
fast_mpool_stats(mpool, &stats);
long_to_comma_str(stats.total_bytes, sz_total_bytes);
long_to_comma_str(stats.free_bytes, sz_free_bytes);
long_to_comma_str(mpool->alloc_bytes, sz_alloc_bytes);
logInfo("alloc_size_once: %d, discard_size: %d, "
"bytes: {total: %"PRId64", free: %"PRId64"}, "
"trunk_count: {total: %d, free: %d}",
mpool->alloc_size_once, mpool->discard_size,
stats.total_bytes, stats.free_bytes,
stats.total_trunk_count, stats.free_trunk_count);
"bytes: {total: %s, free: %s}, "
"trunk_count: {total: %d, free: %d}, "
"alloc_count: %"PRId64", alloc_bytes: %s, "
"reset_count: %"PRId64, mpool->alloc_size_once,
mpool->discard_size, sz_total_bytes,
sz_free_bytes, stats.total_trunk_count,
stats.free_trunk_count, mpool->alloc_count,
sz_alloc_bytes, mpool->reset.count);
}

View File

@ -41,6 +41,12 @@ struct fast_mpool_man
struct fast_mpool_malloc *free_chain_head; //free node chain
int alloc_size_once; //alloc size once, default: 1MB
int discard_size; //discard size, default: 64 bytes
int64_t alloc_count;
int64_t alloc_bytes;
struct {
int64_t count;
int64_t last_alloc_count;
} reset;
};
struct fast_mpool_stats