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 <jukka.rissanen@linux.intel.com>
This commit is contained in:
Jukka Rissanen 2015-08-20 17:58:03 +03:00 committed by Anas Nashif
parent 649a525329
commit 792bbbb6d8
2 changed files with 78 additions and 69 deletions

View File

@ -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 <offsets.h>
#include <misc/printk.h>
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 */

View File

@ -87,73 +87,6 @@ static struct net_dev {
struct net_driver *drv;
} netdev;
#if defined(CONFIG_INIT_STACKS) && defined(CONFIG_PRINTK)
#include <offsets.h>
#include <misc/printk.h>
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);