From 0cda4cec159343c5783545d19f91540eb6baa012 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Mon, 13 Nov 2017 12:55:03 -0600 Subject: [PATCH] apps/nshlib: The free commands no longer used mallinfo() to get the state of the use heap. Two reasons: That is not useful information in the kernel build. And (2) there are other memory resources of interest in other configurations such as the Kernel heap in PROTECTED and KERNEL builds, and the prog mem uses when FLASH is used to hold modifiable data. The free command has been extended to just dump the content of procfs entries and to include all of these other memory resources of the procfs entries are available. --- nshlib/nsh.h | 20 ++++++++++ nshlib/nsh_mmcmds.c | 94 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 96 insertions(+), 18 deletions(-) diff --git a/nshlib/nsh.h b/nshlib/nsh.h index 77229abce..40625b2bb 100644 --- a/nshlib/nsh.h +++ b/nshlib/nsh.h @@ -664,6 +664,26 @@ # define HAVE_MOUNT_LIST 1 #endif +#undef HAVE_PROC_KMEM +#undef HAVE_PROC_UMEM +#undef HAVE_PROC_PROGMEM + +#ifdef CONFIG_FS_PROCFS +# if defined(CONFIG_MM_KERNEL_HEAP) && !defined(CONFIG_FS_PROCFS_EXCLUDE_KMM) +# define HAVE_PROC_KMEM 1 +# endif +# if !defined(CONFIG_FS_PROCFS_EXCLUDE_UMM) && !defined(CONFIG_BUILD_KERNEL) +# define HAVE_PROC_UMEM 1 +# endif +# if defined(CONFIG_ARCH_HAVE_PROGMEM) && !defined(CONFIG_FS_PROCFS_EXCLUDE_PROGMEM) +# define HAVE_PROC_PROGMEM 1 +# endif +# if !defined(HAVE_PROC_KMEM) && !defined(HAVE_PROC_UMEM) && !defined(HAVE_PROC_PROGMEM) +# undef CONFIG_NSH_DISABLE_FREE +# define CONFIG_NSH_DISABLE_FREE 1 +# endif +#endif + /* Suppress unused file utilities */ #define NSH_HAVE_CATFILE 1 diff --git a/nshlib/nsh_mmcmds.c b/nshlib/nsh_mmcmds.c index bc6a8bc31..e025787fc 100644 --- a/nshlib/nsh_mmcmds.c +++ b/nshlib/nsh_mmcmds.c @@ -1,7 +1,7 @@ /**************************************************************************** * apps/nshlib/nsh_mmcmds.c * - * Copyright (C) 2011-2012 Gregory Nutt. All rights reserved. + * Copyright (C) 2011-2012, 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -39,11 +39,64 @@ #include -#include +#include +#include +#include +#include #include "nsh.h" #include "nsh_console.h" +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#undef NEED_CATLINE2 +#ifndef CONFIG_NSH_DISABLE_FREE +# if defined(HAVE_PROC_KMEM) && (defined(HAVE_PROC_UMEM) || \ + defined(HAVE_PROC_PROGMEM)) +# define NEED_CATLINE2 1 +# elif defined(HAVE_PROC_UMEM) && defined(HAVE_PROC_PROGMEM) +# define NEED_CATLINE2 1 +# endif +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: cat_free + ****************************************************************************/ + +#ifdef NEED_CATLINE2 +static void nsh_catline2(FAR struct nsh_vtbl_s *vtbl, FAR const char *filepath) +{ + int fd; + + /* Open the file for reading */ + + fd = open(filepath, O_RDONLY); + if (fd >= 0) + { + /* Skip the first line which contains the header */ + + if (fgets(vtbl->iobuffer, IOBUFFERSIZE, INSTREAM(pstate)) != NULL) + { + /* The second line contains the info to send to stdout */ + + if (fgets(vtbl->iobuffer, IOBUFFERSIZE, INSTREAM(pstate)) != NULL) + { + size_t linesize = strnlen(vtbl->iobuffer, IOBUFFERSIZE); + (void)nsh_write(vtbl, vtbl->iobuffer, linesize); + } + } + + (void)close(fd); + } +} +#endif + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -55,24 +108,29 @@ #ifndef CONFIG_NSH_DISABLE_FREE int cmd_free(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) { - struct mallinfo mem; +#if defined(HAVE_PROC_KMEM) + (void)nsh_catfile(vtbl, argv[0], CONFIG_NSH_PROC_MOUNTPOINT "/kmm"); +# ifdef HAVE_PROC_UMEM + nsh_catline2(vtbl, CONFIG_NSH_PROC_MOUNTPOINT "/umm"); +# endif +# ifdef HAVE_PROC_PROGMEM + nsh_catline2(vtbl, CONFIG_NSH_PROC_MOUNTPOINT "/progmem"); +# endif + return OK; + +#elif defined(HAVE_PROC_UMEM) + (void)nsh_catfile(vtbl, argv[0], CONFIG_NSH_PROC_MOUNTPOINT "/umm"); +# ifdef HAVE_PROC_PROGMEM + nsh_catline2(vtbl, CONFIG_NSH_PROC_MOUNTPOINT "/progmem"); +# endif + return OK; -#ifdef CONFIG_CAN_PASS_STRUCTS - mem = mallinfo(); #else - (void)mallinfo(&mem); +# ifdef HAVE_PROC_PROGMEM + return nsh_catfile(vtbl, argv[0], CONFIG_NSH_PROC_MOUNTPOINT "/progmem"); +# else + return ERROR; +# endif #endif - -#ifdef CONFIG_NOPRINTF_FIELDWIDTH - nsh_output(vtbl, "\ttotal\tused\tfree\tlargest\n"); - nsh_output(vtbl, "Mem:\t%d\t%d\t%d\t%d\n", - mem.arena, mem.uordblks, mem.fordblks, mem.mxordblk); -#else - nsh_output(vtbl, " total used free largest\n"); - nsh_output(vtbl, "Mem: %11d%11d%11d%11d\n", - mem.arena, mem.uordblks, mem.fordblks, mem.mxordblk); -#endif - - return OK; } #endif /* !CONFIG_NSH_DISABLE_FREE */