Kaben123 opened a new pull request, #4066:
URL: https://github.com/apache/nuttx-apps/pull/4066
## Summary
Reduce heap usage of the NSH `top` and `ps` commands and fix a leak in `top`.
All changes are in `nshlib/nsh_proccmds.c` (plus one Kconfig option), so they
are kept in one PR:
1. **nshlib/nsh_proccmds: fix pre-existing nxstyle errors** - whitespace only
(`git diff -w` is empty). The CI style check runs on the whole touched
file, and this file had about 25 existing errors (case label indentation
in
`cmd_kill()`/`cmd_pkill()`, missing blank lines after declarations).
2. **nshlib/top: shrink per-task I/O buffer to reduce peak heap** - `top`
keeps one buffer per task until all tasks are sorted and printed, so peak
heap is about (number of tasks) * `IOBUFFERSIZE`. The per-task buffer only
holds one task's status + loadavg + cmdline. A new
`CONFIG_NSH_TOP_TASKBUF_SIZE` (default 512) sets its size. Longer output
is truncated by `nsh_readfile()`, never overflowed.
3. **nshlib/top: fix memory leak on realloc failure** - `p = realloc(p, ...)`
lost the status array and all its buffers when `realloc()` failed.
4. **nshlib/ps: replace asprintf with a stack buffer in ps_readprocfs()** -
removes 4-5 malloc/free pairs per process for each `ps`. The path is built
with `snprintf()` into a `PATH_MAX` buffer; truncation returns
`-ENAMETOOLONG`.
## Impact
- New Kconfig option `CONFIG_NSH_TOP_TASKBUF_SIZE` (default 512, was
effectively `CONFIG_NSH_FILEIOSIZE`, 1024 by default). Documented in
apache/nuttx#DOC_PR.
- `top` and `ps` output is unchanged.
## Testing
Host: Ubuntu 22.04 x86_64, GCC 13.1.0. Base: apache/nuttx master
5c1746778e8, apache/nuttx-apps master 7f4e5382d.
`sim:nsh` + `CONFIG_SCHED_CPULOAD_SYSCLK=y`, builds with CMake and Make
without warnings.
`top -n 10 -d 1`:
```
top - up 00:00:01
Tasks: 5 total, 2 running, 3 sleeping
%Cpu(s): 0.0 busy, 100.0 idle
Mem : 67108864 total, 1860784 used, 65248080 free
TID PID PPID PRI POLICY TYPE NPX STATE EVENT SIGMASK
STACK CPU COMMAND
0 0 0 0 FIFO Kthread - Ready
0000000000000000 0069584 100.0% Idle_Task
1 0 0 224 FIFO Kthread - Waiting Semaphore
0000000000000000 0067456 0.0% sim_loop_wq 0x75ae9ea003f0 0x75ae9ea00478
2 0 0 224 FIFO Kthread - Waiting Semaphore
0000000000000000 0067472 0.0% hpwork 0x40162660 0x401626e8
4 4 0 100 FIFO Task - Waiting Semaphore
0000000000000000 0067504 0.0% init_main
7 7 4 100 FIFO Task - Running
0000000000000000 0067496 0.0% sh
```
`ps`:
```
nsh> ps
TID PID PPID PRI POLICY TYPE NPX STATE EVENT SIGMASK
STACK CPU COMMAND
0 0 0 0 FIFO Kthread - Ready
0000000000000000 0069584 98.6% Idle_Task
1 0 0 224 FIFO Kthread - Waiting Semaphore
0000000000000000 0067456 0.0% sim_loop_wq 0x75ae9ea003f0 0x75ae9ea00478
2 0 0 224 FIFO Kthread - Waiting Semaphore
0000000000000000 0067472 1.3% hpwork 0x40162660 0x401626e8
4 4 0 100 FIFO Task - Waiting Semaphore
0000000000000000 0067504 0.0% init_main
7 7 4 100 FIFO Task - Running
0000000000000000 0067496 0.0% sh
```
`tools/checkpatch.sh -c -u -m -g` passes on the whole branch.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]