Search code examples
c++ccontikicooja

Memory overflow in Contiki


I am working on an application which requires to msp430 math functions. On using such functions i.e. powf, sqrt etc. memory overflow (ROM) occurs. One such case is, my code works when i use this float i variable without using static. enter image description here

#include "contiki.h"

#include <stdio.h> /* For printf() */
#include <math.h>
#define DEBUG DEBUG_NONE
/*---------------------------------------------------------------------------*/
PROCESS(hello_world_process, "Hello world process");
AUTOSTART_PROCESSES(&hello_world_process);
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(hello_world_process, ev, data)
{
  PROCESS_BEGIN();
  float i;
  i = 2.1;
  printf("Hello, world\n");
  printf("%i\n", (int)powf(10,i));
  PROCESS_END();
}
/*---------------------------------------------------------------------------*/

However in second case it doesn't work...

#include "contiki.h"

#include <stdio.h> /* For printf() */
#include <math.h>
#define DEBUG DEBUG_NONE
static float i;
/*---------------------------------------------------------------------------*/
PROCESS(hello_world_process, "Hello world process");
AUTOSTART_PROCESSES(&hello_world_process);
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(hello_world_process, ev, data)
{
  PROCESS_BEGIN();
  i = 2.1;
  printf("Hello, world\n");
  printf("%i\n", (int)powf(10,i));
  PROCESS_END();
}
/*---------------------------------------------------------------------------*/

Suggested answer is to upgrade msp430-gcc, but this may lead to system instability. Any other suggestions to efficiently handle memory overflows ?

What methodology can be followed for efficiently managing memory in embedded systems.


Solution

  • If you need to save RAM, you might consider reducing:

    QUEUEBUF_CONF_NUM: the number of packets in the link-layer queue. 4 is probably a lower bound for reasonable operation. As the traffic load increases, e.g. more frequent traffic or larger datagrams, you will need to increase this parameter.

    NBR_TABLE_CONF_MAX_NEIGHBORS: the number of entries in the neighbor table. A value greater than the maximum network density is safe. A value lower than that will also work, as the neighbor table will automatically focus on relevant neighbors. But too low values will result in degraded performance.

    NETSTACK_MAX_ROUTE_ENTRIES: the number of routing entries, i.e., in RPL non-storing mode, the number of links in the routing graph, and in storing mode, the number of routing table elements. At the network root, this must be set to the maximum network size. In non-storing mode, other nodes can set this parameter to 0. In storing mode, it is recommended for all nodes to also provision enough entries for each node in the network. UIP_CONF_BUFFER_SIZE: the size of the IPv6 buffer. The minimum value for interoperability is 1280. In closed systems, where no large datagrams are used, lowering this to e.g. 140 may be sensible.

    SICSLOWPAN_CONF_FRAG: Enables/disables 6LoWPAN fragmentation. Disable this if all your traffic fits a single link-layer packet. Note that this will also save some significant ROM. If you need to save ROM, you can consider the following:

    UIP_CONF_TCP: Enables/disables TCP. Make sure this is disabled when TCP is unused.

    UIP_CONF_UDP: Enables/disables UDP. Make sure this is disabled when UDP is unused.

    SICSLOWPAN_CONF_FRAG: As mentioned above. Disable if no fragmentation is needed.

    LOG_CONF_LEVEL_*: Logs consume a large amount of ROM. Reduce log levels to save some more.

    There are many other parameters that affect RAM/ROM usage. You can inspect os/contiki-default-conf.h as well as platform-specific contiki-conf.h files for inspiration. Or use .flashprof and .ramprof to identify the hotspots.

    *Answered on Contiki wiki in Tutorial: RAM and ROM usage by George Oikonomou