nshlib: ps supports noheap parameter

Obtaining the heap usage size requires traversing all memory.
When the number of threads is large, it will become very slow.

ps -heap
PID GROUP PRI POLICY   TYPE    NPX STATE    EVENT     SIGMASK              HEAP  STACK   USED  FILLED COMMAND
  0     0   0 FIFO     Kthread   - Ready              0000000000000000  4338432 2162672 0005196   0.2%  Idle_Task

Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
This commit is contained in:
yinshengkai
2024-05-16 13:00:44 +08:00
committed by GUIDINGLI
parent a655e067ef
commit b90899b1d5
2 changed files with 88 additions and 68 deletions

View File

@@ -475,7 +475,7 @@ static const struct cmdmap_s g_cmdmap[] =
#ifndef CONFIG_NSH_DISABLE_PS #ifndef CONFIG_NSH_DISABLE_PS
CMD_MAP("ps", cmd_ps, 1, CONFIG_NSH_MAXARGUMENTS, CMD_MAP("ps", cmd_ps, 1, CONFIG_NSH_MAXARGUMENTS,
"<pid1 pid2 ...>"), "<-heap> <pid1 pid2 ...>"),
#endif #endif
#ifdef CONFIG_NET_UDP #ifdef CONFIG_NET_UDP

View File

@@ -115,7 +115,7 @@ static const char g_priority[] = "Priority:";
static const char g_scheduler[] = "Scheduler:"; static const char g_scheduler[] = "Scheduler:";
static const char g_sigmask[] = "SigMask:"; static const char g_sigmask[] = "SigMask:";
#if CONFIG_MM_BACKTRACE >= 0 && !defined(CONFIG_NSH_DISABLE_PSHEAPUSAGE) #ifdef PS_SHOW_HEAPSIZE
static const char g_heapsize[] = "AllocSize:"; static const char g_heapsize[] = "AllocSize:";
#endif /* CONFIG_DEBUG _MM && !CONFIG_NSH_DISABLE_PSHEAPUSAGE */ #endif /* CONFIG_DEBUG _MM && !CONFIG_NSH_DISABLE_PSHEAPUSAGE */
@@ -249,16 +249,15 @@ static void nsh_parse_statusline(FAR char *line,
static int ps_callback(FAR struct nsh_vtbl_s *vtbl, FAR const char *dirpath, static int ps_callback(FAR struct nsh_vtbl_s *vtbl, FAR const char *dirpath,
FAR struct dirent *entryp, FAR void *pvarg) FAR struct dirent *entryp, FAR void *pvarg)
{ {
UNUSED(pvarg);
struct nsh_taskstatus_s status; struct nsh_taskstatus_s status;
FAR char *filepath; FAR char *filepath;
FAR char *line; FAR char *line;
FAR char *nextline; FAR char *nextline;
int ret; int ret;
int i; int i;
#if CONFIG_MM_BACKTRACE >= 0 && !defined(CONFIG_NSH_DISABLE_PSHEAPUSAGE) #ifdef PS_SHOW_HEAPSIZE
unsigned long heap_size = 0; bool heap = *(FAR bool *)pvarg;
char heapsize[10] = "";
#endif #endif
#if !defined(CONFIG_NSH_DISABLE_PSSTACKUSAGE) #if !defined(CONFIG_NSH_DISABLE_PSSTACKUSAGE)
unsigned long stack_size = 0; unsigned long stack_size = 0;
@@ -368,7 +367,9 @@ static int ps_callback(FAR struct nsh_vtbl_s *vtbl, FAR const char *dirpath,
status.td_flags, status.td_state, status.td_event, status.td_flags, status.td_state, status.td_event,
status.td_sigmask); status.td_sigmask);
#if CONFIG_MM_BACKTRACE >= 0 && !defined(CONFIG_NSH_DISABLE_PSHEAPUSAGE) #ifdef PS_SHOW_HEAPSIZE
if (heap)
{
/* Get the Heap AllocSize */ /* Get the Heap AllocSize */
filepath = NULL; filepath = NULL;
@@ -389,8 +390,8 @@ static int ps_callback(FAR struct nsh_vtbl_s *vtbl, FAR const char *dirpath,
nextline = vtbl->iobuffer; nextline = vtbl->iobuffer;
do do
{ {
/* Find the beginning of the next line and NUL-terminate the /* Find the beginning of the next line and NUL-terminate
* current line. * the current line.
*/ */
line = nextline; line = nextline;
@@ -419,13 +420,15 @@ static int ps_callback(FAR struct nsh_vtbl_s *vtbl, FAR const char *dirpath,
if (strncmp(line, g_heapsize, strlen(g_heapsize)) == 0) if (strncmp(line, g_heapsize, strlen(g_heapsize)) == 0)
{ {
heap_size = strtoul(&line[12], NULL, 0); snprintf(heapsize, sizeof(heapsize), "%8s ",
&line[12]);
break; break;
} }
} }
while (nextline != NULL); while (nextline != NULL);
} }
} }
}
#endif #endif
@@ -531,7 +534,7 @@ static int ps_callback(FAR struct nsh_vtbl_s *vtbl, FAR const char *dirpath,
defined (PS_SHOW_STACKUSAGE) || defined (NSH_HAVE_CPULOAD) defined (PS_SHOW_STACKUSAGE) || defined (NSH_HAVE_CPULOAD)
nsh_output(vtbl, nsh_output(vtbl,
#ifdef PS_SHOW_HEAPSIZE #ifdef PS_SHOW_HEAPSIZE
"%08lu " "%s"
#endif #endif
#ifdef PS_SHOW_STACKSIZE #ifdef PS_SHOW_STACKSIZE
"%07lu " "%07lu "
@@ -543,8 +546,8 @@ static int ps_callback(FAR struct nsh_vtbl_s *vtbl, FAR const char *dirpath,
#ifdef NSH_HAVE_CPULOAD #ifdef NSH_HAVE_CPULOAD
"%6s " "%6s "
#endif #endif
#if CONFIG_MM_BACKTRACE >= 0 && !defined(CONFIG_NSH_DISABLE_PSHEAPUSAGE) #ifdef PS_SHOW_HEAPSIZE
, heap_size , heapsize
#endif #endif
#if !defined(CONFIG_NSH_DISABLE_PSSTACKUSAGE) #if !defined(CONFIG_NSH_DISABLE_PSSTACKUSAGE)
, stack_size , stack_size
@@ -620,15 +623,28 @@ int cmd_exec(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
#ifndef CONFIG_NSH_DISABLE_PS #ifndef CONFIG_NSH_DISABLE_PS
int cmd_ps(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv) int cmd_ps(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
{ {
FAR const char *heapprompt = "";
bool heap = false;
int i;
#ifdef PS_SHOW_HEAPSIZE
for (i = 1; i < argc; i++)
{
if (strcmp(argv[i], "-heap") == 0)
{
heap = true;
heapprompt = " HEAP ";
}
}
#endif
nsh_output(vtbl, "%5s %5s " nsh_output(vtbl, "%5s %5s "
#ifdef CONFIG_SMP #ifdef CONFIG_SMP
"%3s " "%3s "
#endif #endif
"%3s %-8s %-7s %3s %-8s %-9s " "%3s %-8s %-7s %3s %-8s %-9s "
"%-16s " "%-16s "
#if CONFIG_MM_BACKTRACE >= 0 && !defined(CONFIG_NSH_DISABLE_PSHEAPUSAGE) "%s"
"%8s "
#endif
#if !defined(CONFIG_NSH_DISABLE_PSSTACKUSAGE) #if !defined(CONFIG_NSH_DISABLE_PSSTACKUSAGE)
"%6s " "%6s "
#ifdef CONFIG_STACK_COLORATION #ifdef CONFIG_STACK_COLORATION
@@ -646,9 +662,7 @@ int cmd_ps(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
#endif #endif
"PRI", "POLICY", "TYPE", "NPX", "STATE", "EVENT", "PRI", "POLICY", "TYPE", "NPX", "STATE", "EVENT",
"SIGMASK", "SIGMASK",
#if CONFIG_MM_BACKTRACE >= 0 && !defined(CONFIG_NSH_DISABLE_PSHEAPUSAGE) heapprompt,
"HEAP",
#endif
#if !defined(CONFIG_NSH_DISABLE_PSSTACKUSAGE) #if !defined(CONFIG_NSH_DISABLE_PSSTACKUSAGE)
"STACK", "STACK",
#ifdef CONFIG_STACK_COLORATION #ifdef CONFIG_STACK_COLORATION
@@ -662,22 +676,28 @@ int cmd_ps(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
"COMMAND" "COMMAND"
); );
if (argc > 1) if (argc - heap > 1)
{ {
int i;
for (i = 1; i < argc; i++) for (i = 1; i < argc; i++)
{ {
struct dirent entry; struct dirent entry;
if (!isdigit(*argv[i]))
{
continue;
}
entry.d_type = DT_DIR; entry.d_type = DT_DIR;
strcpy(entry.d_name, argv[i]); strcpy(entry.d_name, argv[i]);
ps_callback(vtbl, CONFIG_NSH_PROC_MOUNTPOINT, &entry, NULL); ps_callback(vtbl, CONFIG_NSH_PROC_MOUNTPOINT, &entry, &heap);
} }
return 0; return 0;
} }
else
{
return nsh_foreach_direntry(vtbl, "ps", CONFIG_NSH_PROC_MOUNTPOINT, return nsh_foreach_direntry(vtbl, "ps", CONFIG_NSH_PROC_MOUNTPOINT,
ps_callback, NULL); ps_callback, &heap);
}
} }
#endif #endif