Skip to content

Commit

Permalink
sys/shell: cleanup the heap command approach
Browse files Browse the repository at this point in the history
Replaces the special heap command approach of the lpc_common module with a more general heap command approach. Module lpc_common was already removed with PR RIOT-OS#2118. PR RIOT-OS#2118 integrated cpu/lpc_common code in cpu/lpc2387. With PR RIOT-OS#3530 special heap handling for cpu/lpc2387 was replaced by newlib memory management which uses _sbrk_r to allocate chunks from the heap. _sbrk_r uses _sheap and _eheap symbols that are defined in lpc2387.ld and can be used together with mallinfo function for heap statistics.
  • Loading branch information
gschorcht authored and gdoffe committed Dec 16, 2019
1 parent a812ae2 commit 1485010
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
19 changes: 19 additions & 0 deletions sys/newlib_syscalls_default/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <unistd.h>
#include <reent.h>
#include <errno.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -122,6 +123,24 @@ void *_sbrk_r(struct _reent *r, ptrdiff_t incr)
return res;
}

/**
* @brief Print heap statistics
*
* If the CPU does not provide its own heap handling and heap_stats function,
* but instead uses the newlib_syscall_default function, this function outputs
* the heap statistics. If the CPU provides its own heap_stats function, it
* should define HAVE_HEAP_STATS in its cpu_conf.h file.
*/
#ifndef HAVE_HEAP_STATS
__attribute__((weak)) void heap_stats(void)
{
struct mallinfo minfo = mallinfo();
long int heap_size = &_eheap - &_sheap;
printf("heap: %ld (used %d, free %ld) [bytes]\n",
heap_size, minfo.uordblks, heap_size - minfo.uordblks);
}
#endif /* HAVE_HEAP_STATS */

#endif /*__mips__*/

/**
Expand Down
3 changes: 3 additions & 0 deletions sys/shell/commands/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ endif
ifneq (,$(filter ps,$(USEMODULE)))
SRC += sc_ps.c
endif
ifneq (,$(filter heap_cmd,$(USEMODULE)))
SRC += sc_heap.c
endif
ifneq (,$(filter sht1x,$(USEMODULE)))
SRC += sc_sht1x.c
endif
Expand Down
12 changes: 11 additions & 1 deletion sys/shell/commands/sc_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,24 @@
* @}
*/

#include "cpu_conf.h"

#if defined(MODULE_NEWLIB_SYSCALLS_DEFAULT) || defined (HAVE_HEAP_STATS)
extern void heap_stats(void);
#else
#include <stdio.h>
#endif

int _heap_handler(int argc, char **argv)
{
(void) argc;
(void) argv;

#if defined(MODULE_NEWLIB_SYSCALLS_DEFAULT) || defined (HAVE_HEAP_STATS)
heap_stats();

return 0;
#else
printf("heap statistics are not supported for %s cpu\n", RIOT_CPU);
return 1;
#endif
}
6 changes: 3 additions & 3 deletions sys/shell/commands/shell_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extern int _reboot_handler(int argc, char **argv);
extern int _id_handler(int argc, char **argv);
#endif

#ifdef MODULE_LPC_COMMON
#ifdef MODULE_HEAP_CMD
extern int _heap_handler(int argc, char **argv);
#endif

Expand Down Expand Up @@ -168,8 +168,8 @@ const shell_command_t _shell_command_list[] = {
#ifdef MODULE_CONFIG
{"id", "Gets or sets the node's id.", _id_handler},
#endif
#ifdef MODULE_LPC_COMMON
{"heap", "Shows the heap state for the LPC2387 on the command shell.", _heap_handler},
#ifdef MODULE_HEAP_CMD
{"heap", "Prints heap statistics.", _heap_handler},
#endif
#ifdef MODULE_PS
{"ps", "Prints information about running threads.", _ps_handler},
Expand Down

0 comments on commit 1485010

Please sign in to comment.