From 792bbbb6d88d82130d30f35040eb09fb699cc1e7 Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Thu, 20 Aug 2015 17:58:03 +0300 Subject: [PATCH] net: Refactor the stack analyzer functions Generalize the stack analyze functions so that they can be used by other fibers. Change-Id: If1fae51db45010b75f6ac58cd8d874b31ca336e2 Signed-off-by: Jukka Rissanen --- include/net/net_buf.h | 74 +++++++++++++++++++++++++++++++++++++++++++ net/ip/net_init.c | 73 +++--------------------------------------- 2 files changed, 78 insertions(+), 69 deletions(-) diff --git a/include/net/net_buf.h b/include/net/net_buf.h index 230abce8130..5235b5c2454 100644 --- a/include/net/net_buf.h +++ b/include/net/net_buf.h @@ -372,4 +372,78 @@ void net_mbuf_put(struct net_mbuf *buf); #define uip_pkt_packetbuf_addrs(buf) ((buf)->pkt_packetbuf_addrs) /* @endcond */ +/** @cond ignore */ +#if defined(CONFIG_INIT_STACKS) && defined(CONFIG_PRINTK) +#include +#include + +enum { + STACK_DIRECTION_UP, + STACK_DIRECTION_DOWN, +}; + +static inline unsigned net_calculate_unused(const char *stack, unsigned size, + int stack_growth) +{ + unsigned i, unused = 0; + + if (stack_growth == STACK_DIRECTION_DOWN) { + for (i = __tTCS_SIZEOF; i < size; i++) { + if ((unsigned char)stack[i] == 0xaa) { + unused++; + } else { + break; + } + } + } else { + for (i = size - 1; i >= __tTCS_SIZEOF; i--) { + if ((unsigned char)stack[i] == 0xaa) { + unused++; + } else { + break; + } + } + } + + return unused; +} + +static inline unsigned net_get_stack_dir(struct net_buf *buf, + struct net_buf **ref) +{ + if (buf > *ref) { + return 1; + } else { + return 0; + } +} + +static inline void net_analyze_stack(const char *name, + unsigned char *stack, + size_t size) +{ + unsigned unused; + int stack_growth; + char *dir; + struct net_buf *buf = NULL; + + if (net_get_stack_dir(buf, &buf)) { + dir = "up"; + stack_growth = STACK_DIRECTION_UP; + } else { + dir = "down"; + stack_growth = STACK_DIRECTION_DOWN; + } + + unused = net_calculate_unused(stack, size, stack_growth); + + printk("net: ip: %s stack grows %s, " + "stack(%p/%u): unused %u bytes\n", + name, dir, stack, size, unused); +} +#else +#define net_analyze_stack(...) +#endif +/* @endcond */ + #endif /* __NET_BUF_H */ diff --git a/net/ip/net_init.c b/net/ip/net_init.c index 0288e16ac12..6425c804d38 100644 --- a/net/ip/net_init.c +++ b/net/ip/net_init.c @@ -87,73 +87,6 @@ static struct net_dev { struct net_driver *drv; } netdev; -#if defined(CONFIG_INIT_STACKS) && defined(CONFIG_PRINTK) -#include -#include - -enum { - STACK_DIRECTION_UP, - STACK_DIRECTION_DOWN, -}; - -static unsigned calculate_unused(const char *stack, unsigned size, - int stack_growth) -{ - unsigned i, unused = 0; - - if (stack_growth == STACK_DIRECTION_DOWN) { - for (i = __tTCS_SIZEOF; i < size; i++) { - if ((unsigned char)stack[i] == 0xaa) { - unused++; - } else { - break; - } - } - } else { - for (i = size - 1; i >= __tTCS_SIZEOF; i--) { - if ((unsigned char)stack[i] == 0xaa) { - unused++; - } else { - break; - } - } - } - - return unused; -} - -static void analyze_stacks(struct net_buf *buf, struct net_buf **ref) -{ - unsigned unused_rx, unused_tx; - int stack_growth; - char *dir; - - if (buf > *ref) { - dir = "up"; - stack_growth = STACK_DIRECTION_UP; - } else { - dir = "down"; - stack_growth = STACK_DIRECTION_DOWN; - } - - unused_rx = calculate_unused(rx_fiber_stack, sizeof(rx_fiber_stack), - stack_growth); - unused_tx = calculate_unused(tx_fiber_stack, sizeof(tx_fiber_stack), - stack_growth); - - printk("net: ip: stack grows %s, sizeof(tTCS): %u " - "rx stack(%p/%u): unused %u/%u " - "tx stack(%p/%u): unused %u/%u\n", - dir, __tTCS_SIZEOF, - rx_fiber_stack, sizeof(rx_fiber_stack), - unused_rx, sizeof(rx_fiber_stack), - tx_fiber_stack, sizeof(tx_fiber_stack), - unused_tx, sizeof(tx_fiber_stack)); -} -#else -#define analyze_stacks(...) -#endif - /* Called by application to send a packet */ int net_send(struct net_buf *buf) { @@ -528,7 +461,8 @@ static void net_tx_fiber(void) } while (ret > 0); /* Check stack usage (no-op if not enabled) */ - analyze_stacks(buf, &buf); + net_analyze_stack("TX fiber", tx_fiber_stack, + sizeof(tx_fiber_stack)); net_buf_put(buf); } @@ -544,7 +478,8 @@ static void net_rx_fiber(void) buf = nano_fifo_get_wait(&netdev.rx_queue); /* Check stack usage (no-op if not enabled) */ - analyze_stacks(buf, &buf); + net_analyze_stack("RX fiber", rx_fiber_stack, + sizeof(rx_fiber_stack)); NET_DBG("Received buf %p\n", buf);