zephyr/net/bluetooth/stack.h
Szymon Janc edf9cc3065 Bluetooth: Add new code for stack usage analysis
This introduces simple API for analysing stack usage. Stack growth
direction is determined on build instead of runtime.

Change-Id: Iacb160d088cc0af57e2e9dedc72cbc5e31fd22f4
Signed-off-by: Szymon Janc <ext.szymon.janc@tieto.com>
2016-02-05 20:24:25 -05:00

88 lines
3.0 KiB
C

/**
* @file stack.h
* Stack usage analysis helpers
*/
/*
* Copyright (c) 2015 Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1) Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2) Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3) Neither the name of Intel Corporation nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#if defined(CONFIG_INIT_STACKS) && defined(CONFIG_PRINTK)
#include <offsets.h>
#include <misc/printk.h>
static inline void stack_analyze(const char *name, const char *stack,
unsigned size)
{
unsigned i, stack_offset, pcnt, unused = 0;
/* The TCS is always placed on a 4-byte aligned boundary - if
* the stack beginning doesn't match that there will be some
* unused bytes in the beginning.
*/
stack_offset = __tTCS_SIZEOF + ((4 - ((unsigned)stack % 4)) % 4);
/* TODO
* Currently all supported platforms have stack growth down and there is no
* Kconfig option to configure it so this always build "else" branch.
* When support for platform with stack direction up (or configurable direction)
* is added this check should be confirmed that correct Kconfig option is used.
*/
#if defined(CONFIG_STACK_GROWS_UP)
for (i = size - 1; i >= stack_offset; i--) {
if ((unsigned char)stack[i] == 0xaa) {
unused++;
} else {
break;
}
}
#else
for (i = stack_offset; i < size; i++) {
if ((unsigned char)stack[i] == 0xaa) {
unused++;
} else {
break;
}
}
#endif
/* Calculate the real size reserved for the stack */
size -= stack_offset;
pcnt = ((size - unused) * 100) / size;
printk("%s (real size %u):\tunused %u\tusage %u / %u (%u %%)\n", name,
size + stack_offset, unused, size - unused, size, pcnt);
}
#else
static inline void stack_analyze(const char *name, const char *stack,
unsigned size)
{
}
#endif