This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
The following commit(s) were added to refs/heads/master by this push:
new 2b7dcf34b testing/sched/exitmem: Add a test for the memory of an ended
process.
2b7dcf34b is described below
commit 2b7dcf34baa23a04f9c591ab8dc5403c0743b706
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Nov 12 12:34:04 2026 +0100
testing/sched/exitmem: Add a test for the memory of an ended process.
exitmem forks children one after the other. Each child takes a part of
the free memory (40 % by default, less when its heap is smaller), writes
every page and ends. After each waitpid() the free memory
(sysconf(_SC_AVPHYS_PAGES)) must come back within 3 seconds. A child
that cannot take its memory is a failure too.
With SMP, each child runs on another CPU than the parent, and a thread of
the child runs on a third CPU first. Both CPUs then run only the idle
task. The test checks that such a CPU does not keep the address
environment of the ended child.
The test needs fork() (ARCH_HAVE_FORK: kernel builds).
Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
---
testing/sched/exitmem/CMakeLists.txt | 33 +++++
testing/sched/exitmem/Kconfig | 33 +++++
testing/sched/exitmem/Make.defs | 25 ++++
testing/sched/exitmem/Makefile | 36 +++++
testing/sched/exitmem/exitmem_main.c | 248 +++++++++++++++++++++++++++++++++++
5 files changed, 375 insertions(+)
diff --git a/testing/sched/exitmem/CMakeLists.txt
b/testing/sched/exitmem/CMakeLists.txt
new file mode 100644
index 000000000..9930811d8
--- /dev/null
+++ b/testing/sched/exitmem/CMakeLists.txt
@@ -0,0 +1,33 @@
+#
##############################################################################
+# apps/testing/sched/exitmem/CMakeLists.txt
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
contributor
+# license agreements. See the NOTICE file distributed with this work for
+# additional information regarding copyright ownership. The ASF licenses this
+# file to you under the Apache License, Version 2.0 (the "License"); you may
not
+# use this file except in compliance with the License. You may obtain a copy
of
+# the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations under
+# the License.
+#
+#
##############################################################################
+
+if(CONFIG_TESTING_EXITMEM)
+ nuttx_add_application(
+ NAME
+ ${CONFIG_TESTING_EXITMEM_PROGNAME}
+ SRCS
+ exitmem_main.c
+ STACKSIZE
+ ${CONFIG_TESTING_EXITMEM_STACKSIZE}
+ PRIORITY
+ ${CONFIG_TESTING_EXITMEM_PRIORITY})
+endif()
diff --git a/testing/sched/exitmem/Kconfig b/testing/sched/exitmem/Kconfig
new file mode 100644
index 000000000..15f310822
--- /dev/null
+++ b/testing/sched/exitmem/Kconfig
@@ -0,0 +1,33 @@
+#
+# For a description of the syntax of this configuration file,
+# see the file kconfig-language.txt in the NuttX tools repository.
+#
+
+config TESTING_EXITMEM
+ tristate "Memory of an ended process test"
+ default n
+ depends on ARCH_HAVE_FORK
+ ---help---
+ Fork children that take a large part of the free memory and end.
+ After each child the free memory must come back. With SMP, each
+ child runs on another CPU, and a thread of the child runs on a
+ third CPU first.
+
+if TESTING_EXITMEM
+
+config TESTING_EXITMEM_PROGNAME
+ string "Program name"
+ default "exitmem"
+ ---help---
+ This is the name of the program that will be used when the NSH
ELF
+ program is installed.
+
+config TESTING_EXITMEM_PRIORITY
+ int "exitmem task priority"
+ default 100
+
+config TESTING_EXITMEM_STACKSIZE
+ int "exitmem stack size"
+ default DEFAULT_TASK_STACKSIZE
+
+endif
diff --git a/testing/sched/exitmem/Make.defs b/testing/sched/exitmem/Make.defs
new file mode 100644
index 000000000..720838cd0
--- /dev/null
+++ b/testing/sched/exitmem/Make.defs
@@ -0,0 +1,25 @@
+############################################################################
+# apps/testing/sched/exitmem/Make.defs
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership. The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+############################################################################
+
+ifneq ($(CONFIG_TESTING_EXITMEM),)
+CONFIGURED_APPS += $(APPDIR)/testing/sched/exitmem
+endif
diff --git a/testing/sched/exitmem/Makefile b/testing/sched/exitmem/Makefile
new file mode 100644
index 000000000..f6eae0e4b
--- /dev/null
+++ b/testing/sched/exitmem/Makefile
@@ -0,0 +1,36 @@
+############################################################################
+# apps/testing/sched/exitmem/Makefile
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership. The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+############################################################################
+
+include $(APPDIR)/Make.defs
+
+# exitmem built-in application info
+
+PROGNAME = $(CONFIG_TESTING_EXITMEM_PROGNAME)
+PRIORITY = $(CONFIG_TESTING_EXITMEM_PRIORITY)
+STACKSIZE = $(CONFIG_TESTING_EXITMEM_STACKSIZE)
+MODULE = $(CONFIG_TESTING_EXITMEM)
+
+# exitmem main source
+
+MAINSRC = exitmem_main.c
+
+include $(APPDIR)/Application.mk
diff --git a/testing/sched/exitmem/exitmem_main.c
b/testing/sched/exitmem/exitmem_main.c
new file mode 100644
index 000000000..96b46d8a1
--- /dev/null
+++ b/testing/sched/exitmem/exitmem_main.c
@@ -0,0 +1,248 @@
+/****************************************************************************
+ * apps/testing/sched/exitmem/exitmem_main.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <errno.h>
+#include <pthread.h>
+#include <sched.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define EXITMEM_ROUNDS 8 /* Default number of children */
+#define EXITMEM_PERCENT 40 /* Default part of the free memory */
+#define EXITMEM_WAIT 30 /* Free memory checks, 100 ms apart */
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: free_kib
+ *
+ * Description:
+ * Return the free memory in KiB, or -1.
+ *
+ ****************************************************************************/
+
+static long free_kib(void)
+{
+ long npages = sysconf(_SC_AVPHYS_PAGES);
+ long pgsize = sysconf(_SC_PAGESIZE);
+
+ if (npages < 0 || pgsize < 1024)
+ {
+ return -1;
+ }
+
+ return npages * (pgsize / 1024);
+}
+
+/****************************************************************************
+ * Name: pin
+ *
+ * Description:
+ * Run the calling thread on one CPU only.
+ *
+ ****************************************************************************/
+
+static void pin(int cpu)
+{
+#ifdef CONFIG_SMP
+ cpu_set_t cpuset;
+
+ CPU_ZERO(&cpuset);
+ CPU_SET(cpu, &cpuset);
+ if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0)
+ {
+ printf("exitmem: sched_setaffinity(%d) failed: %d\n", cpu, errno);
+ }
+
+ sched_yield();
+#endif
+}
+
+/****************************************************************************
+ * Name: side_thread
+ *
+ * Description:
+ * Run on another CPU and end: that CPU then idles after the child.
+ *
+ ****************************************************************************/
+
+static FAR void *side_thread(FAR void *arg)
+{
+ pin((int)(intptr_t)arg);
+ return NULL;
+}
+
+/****************************************************************************
+ * Name: child
+ *
+ * Description:
+ * Run on cpu (and a thread on third), take size KiB, write every page
+ * and end.
+ *
+ ****************************************************************************/
+
+static void child(int cpu, int third, long size)
+{
+ long pgsize = sysconf(_SC_PAGESIZE);
+ pthread_t thread;
+ FAR char *buf;
+ long off;
+
+ pin(cpu);
+ if (pthread_create(&thread, NULL, side_thread,
+ (FAR void *)(intptr_t)third) == 0)
+ {
+ pthread_join(thread, NULL);
+ }
+
+ /* A heap of fixed size can be smaller than the free memory */
+
+ while ((buf = malloc(size * 1024)) == NULL && size > 64)
+ {
+ size /= 2;
+ }
+
+ if (buf == NULL)
+ {
+ _exit(2);
+ }
+
+ for (off = 0; off < size * 1024; off += pgsize)
+ {
+ buf[off] = (char)off;
+ }
+
+ _exit(0);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * exitmem_main
+ ****************************************************************************/
+
+int main(int argc, FAR char *argv[])
+{
+ int rounds = argc > 1 ? atoi(argv[1]) : EXITMEM_ROUNDS;
+ int percent = argc > 2 ? atoi(argv[2]) : EXITMEM_PERCENT;
+ int ncpus = 1;
+ long start;
+ int fail = 0;
+ int round;
+
+#ifdef CONFIG_SMP
+ ncpus = CONFIG_SMP_NCPUS;
+#endif
+
+ start = free_kib();
+ if (start < 0 || rounds < 1 || percent < 1 || percent > 90)
+ {
+ printf("Usage: %s [rounds [percent]]\n", argv[0]);
+ return EXIT_FAILURE;
+ }
+
+ printf("exitmem: %d CPUs, %d rounds, %d%% of the free memory\n",
+ ncpus, rounds, percent);
+
+ pin(0);
+ for (round = 1; round <= rounds; round++)
+ {
+ int cpu = ncpus > 1 ? 1 + (round - 1) % (ncpus - 1) : 0;
+ int third = ncpus > 2 ? 1 + cpu % (ncpus - 1) : cpu;
+ long before = free_kib();
+ long size = before / 100 * percent;
+ long after = -1;
+ int status = -1;
+ pid_t pid;
+ int i;
+
+ fflush(stdout);
+ pid = fork();
+ if (pid == 0)
+ {
+ child(cpu, third, size);
+ }
+
+ if (pid < 0 || waitpid(pid, &status, 0) != pid)
+ {
+ printf("exitmem: round %d: fork or waitpid failed: %d\n",
+ round, errno);
+ fail++;
+ continue;
+ }
+
+ /* A work queue frees the address environment: give it some time */
+
+ for (i = 0; i < EXITMEM_WAIT; i++)
+ {
+ after = free_kib();
+ if (after >= before - before / 16)
+ {
+ break;
+ }
+
+ usleep(100 * 1000);
+ }
+
+ if (status != 0 || after < before - before / 16)
+ {
+ printf("exitmem: round %d CPU %d: %ld KiB, status %d, "
+ "free %ld KiB before, %ld KiB after: FAIL\n",
+ round, cpu, size, status, before, after);
+ fail++;
+ }
+ else
+ {
+ printf("exitmem: round %d CPU %d: %ld KiB, "
+ "free %ld KiB before, %ld KiB after\n",
+ round, cpu, size, before, after);
+ }
+ }
+
+ if (free_kib() < start - start / 16)
+ {
+ printf("exitmem: free %ld KiB at the start, %ld KiB at the end: "
+ "FAIL\n", start, free_kib());
+ fail++;
+ }
+
+ printf("exitmem: %s\n", fail > 0 ? "FAIL" : "PASS");
+ return fail > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
+}