From 212eacd9d60cbba6486130075fffe8920d4f4884 Mon Sep 17 00:00:00 2001 From: Dirk Ziegelmeier Date: Thu, 7 Jul 2016 21:55:51 +0200 Subject: [PATCH] Fix bug #48356: private memp pools have no statistic counters Implement struct stats_mem instance for each pool, let lwip_stats.mem[] point to these instances --- src/core/memp.c | 60 ++++++++++++++++++++----------- src/core/stats.c | 15 -------- src/include/lwip/memp.h | 12 +++++++ src/include/lwip/priv/memp_priv.h | 4 +++ src/include/lwip/stats.h | 14 ++------ 5 files changed, 57 insertions(+), 48 deletions(-) diff --git a/src/core/memp.c b/src/core/memp.c index 13e34777..a7505f6e 100644 --- a/src/core/memp.c +++ b/src/core/memp.c @@ -211,7 +211,8 @@ memp_init_pool(const struct memp_desc *desc) for (i = 0; i < desc->num; ++i) { memp->next = *desc->tab; *desc->tab = memp; - memp = (struct memp *)(void *)((u8_t *)memp + MEMP_SIZE + desc->size + /* cast through void* to get rid of alignment warnings */ + memp = (struct memp *)(void *)((u8_t *)memp + MEMP_SIZE + desc->size #if MEMP_OVERFLOW_CHECK + MEMP_SANITY_REGION_AFTER_ALIGNED #endif @@ -233,16 +234,25 @@ memp_init(void) { u16_t i; - for (i = 0; i < MEMP_MAX; ++i) { - MEMP_STATS_AVAIL(used, i, 0); - MEMP_STATS_AVAIL(max, i, 0); - MEMP_STATS_AVAIL(err, i, 0); - MEMP_STATS_AVAIL(avail, i, memp_pools[i]->num); - } - /* for every pool: */ - for (i = 0; i < MEMP_MAX; ++i) { + for (i = 0; i < LWIP_ARRAYSIZE(memp_pools); i++) { memp_init_pool(memp_pools[i]); + +#if MEMP_STATS + memp_pools[i]->stats->used = 0; + memp_pools[i]->stats->max = 0; + memp_pools[i]->stats->err = 0; + memp_pools[i]->stats->illegal = 0; + memp_pools[i]->stats->avail = memp_pools[i]->num; + +#ifdef LWIP_DEBUG + memp_pools[i]->stats->name = memp_pools[i]->desc; +#endif /* LWIP_DEBUG */ + +#if LWIP_STATS + lwip_stats.memp[i] = memp_pools[i]->stats; +#endif +#endif /* MEMP_STATS */ } #if MEMP_OVERFLOW_CHECK @@ -279,9 +289,24 @@ memp_malloc_pool_fn(const struct memp_desc *desc, const char* file, const int li #endif /* MEMP_OVERFLOW_CHECK */ LWIP_ASSERT("memp_malloc: memp properly aligned", ((mem_ptr_t)memp % MEM_ALIGNMENT) == 0); + /* cast through void* to get rid of alignment warnings */ memp = (struct memp*)(void *)((u8_t*)memp + MEMP_SIZE); } + if (memp != NULL) { +#if MEMP_STATS + desc->stats->used++; + if (desc->stats->used > desc->stats->max) { + desc->stats->max = desc->stats->used; + } +#endif + } else { + LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("memp_malloc: out of memory in pool %s\n", desc->desc)); +#if MEMP_STATS + desc->stats->err++; +#endif + } + SYS_ARCH_UNPROTECT(old_level); return memp; @@ -322,16 +347,6 @@ memp_malloc_fn(memp_t type, const char* file, const int line) memp = memp_malloc_pool_fn(memp_pools[type], file, line); #endif - if (memp != NULL) { - MEMP_STATS_INC(used, type); - if (MEMP_STATS_GET(used, type) > MEMP_STATS_GET(max, type)) { - MEMP_STATS_AVAIL(max, type, MEMP_STATS_GET(used, type)); - } - } else { - LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("memp_malloc: out of memory in pool %s\n", memp_pools[type]->desc)); - MEMP_STATS_INC(err, type); - } - SYS_ARCH_UNPROTECT(old_level); return memp; @@ -350,6 +365,7 @@ do_memp_free_pool(const struct memp_desc* desc, void *mem) LWIP_ASSERT("memp_free: mem properly aligned", ((mem_ptr_t)mem % MEM_ALIGNMENT) == 0); + /* cast through void* to get rid of alignment warnings */ memp = (struct memp *)(void *)((u8_t*)mem - MEMP_SIZE); SYS_ARCH_PROTECT(old_level); @@ -359,6 +375,10 @@ do_memp_free_pool(const struct memp_desc* desc, void *mem) memp_overflow_check_element_underflow(memp, desc); #endif /* MEMP_OVERFLOW_CHECK */ +#if MEMP_STATS + desc->stats->used--; +#endif + memp->next = *desc->tab; #ifdef LWIP_HOOK_MEMP_AVAILABLE @@ -411,8 +431,6 @@ memp_free(memp_t type, void *mem) memp_overflow_check_all(); #endif /* MEMP_OVERFLOW_CHECK >= 2 */ - MEMP_STATS_DEC(used, type); - #ifdef LWIP_HOOK_MEMP_AVAILABLE do_memp_free_pool(memp_pools[type], mem, &old_first); #else diff --git a/src/core/stats.c b/src/core/stats.c index b47ab0b7..c3b24177 100644 --- a/src/core/stats.c +++ b/src/core/stats.c @@ -49,25 +49,10 @@ struct stats_ lwip_stats; -#if defined(LWIP_DEBUG) || LWIP_STATS_DISPLAY -#if MEMP_STATS -static const char * memp_names[] = { -#define LWIP_MEMPOOL(name,num,size,desc) desc, -#include "lwip/priv/memp_std.h" -}; -#endif /* MEMP_STATS */ -#endif /* LWIP_DEBUG || LWIP_STATS_DISPLAY */ - void stats_init(void) { #ifdef LWIP_DEBUG -#if MEMP_STATS - int i; - for (i = 0; i < MEMP_MAX; i++) { - lwip_stats.memp[i].name = memp_names[i]; - } -#endif /* MEMP_STATS */ #if MEM_STATS lwip_stats.mem.name = "MEM"; #endif /* MEM_STATS */ diff --git a/src/include/lwip/memp.h b/src/include/lwip/memp.h index 285d3b8b..ce52b7fd 100644 --- a/src/include/lwip/memp.h +++ b/src/include/lwip/memp.h @@ -54,6 +54,7 @@ typedef enum { } memp_t; #include "lwip/priv/memp_priv.h" +#include "lwip/stats.h" /* Private mempools example: * .h: only when pool is used in multiple .c files: LWIP_MEMPOOL_PROTOTYPE(my_private_pool); @@ -71,6 +72,14 @@ extern const struct memp_desc* const memp_pools[MEMP_MAX]; #define LWIP_MEMPOOL_PROTOTYPE(name) extern const struct memp_desc memp_ ## name +#if MEMP_STATS +#define LWIP_MEMPOOL_DECLARE_STATS_INSTANCE(name) static struct stats_mem name; +#define LWIP_MEMPOOL_DECLARE_STATS_REFERENCE(name) &name, +#else +#define LWIP_MEMPOOL_DECLARE_STATS_INSTANCE(name) +#define LWIP_MEMPOOL_DECLARE_STATS_REFERENCE(name) +#endif + #if MEMP_MEM_MALLOC #include "lwip/mem.h" @@ -93,10 +102,13 @@ extern const struct memp_desc* const memp_pools[MEMP_MAX]; #define LWIP_MEMPOOL_DECLARE(name,num,size,desc) \ LWIP_DECLARE_MEMORY_ALIGNED(memp_memory_ ## name ## _base, ((num) * (MEMP_SIZE + MEMP_ALIGN_SIZE(size)))); \ \ + LWIP_MEMPOOL_DECLARE_STATS_INSTANCE(memp_stats_ ## name) \ + \ static struct memp *memp_tab_ ## name; \ \ const struct memp_desc memp_ ## name = { \ LWIP_MEM_ALIGN_SIZE(size), \ + LWIP_MEMPOOL_DECLARE_STATS_REFERENCE(memp_stats_ ## name) \ (num), \ DECLARE_LWIP_MEMPOOL_DESC(desc) \ memp_memory_ ## name ## _base, \ diff --git a/src/include/lwip/priv/memp_priv.h b/src/include/lwip/priv/memp_priv.h index d6f63b06..c77c08ca 100644 --- a/src/include/lwip/priv/memp_priv.h +++ b/src/include/lwip/priv/memp_priv.h @@ -128,6 +128,10 @@ struct memp_desc { /** Element size */ u16_t size; +#if MEMP_STATS + struct stats_mem *stats; +#endif + #if !MEMP_MEM_MALLOC /** Number of elements */ u16_t num; diff --git a/src/include/lwip/stats.h b/src/include/lwip/stats.h index f483cc7e..628d9969 100644 --- a/src/include/lwip/stats.h +++ b/src/include/lwip/stats.h @@ -247,7 +247,7 @@ struct stats_ { struct stats_mem mem; #endif #if MEMP_STATS - struct stats_mem memp[MEMP_MAX]; + struct stats_mem *memp[MEMP_MAX]; #endif #if SYS_STATS struct stats_sys sys; @@ -370,19 +370,9 @@ void stats_init(void); #endif #if MEMP_STATS -#define MEMP_STATS_AVAIL(x, i, y) lwip_stats.memp[i].x = y -#define MEMP_STATS_INC(x, i) STATS_INC(memp[i].x) -#define MEMP_STATS_DEC(x, i) STATS_DEC(memp[i].x) -#define MEMP_STATS_INC_USED(x, i) STATS_INC_USED(memp[i], 1) -#define MEMP_STATS_DISPLAY(i) stats_display_memp(&lwip_stats.memp[i], i) -#define MEMP_STATS_GET(x, i) STATS_GET(memp[i].x) +#define MEMP_STATS_DEC(x, i) STATS_DEC(memp[i]->x) #else -#define MEMP_STATS_AVAIL(x, i, y) -#define MEMP_STATS_INC(x, i) #define MEMP_STATS_DEC(x, i) -#define MEMP_STATS_INC_USED(x, i) -#define MEMP_STATS_DISPLAY(i) -#define MEMP_STATS_GET(x, i) 0 #endif #if SYS_STATS