This is an automated email from the ASF dual-hosted git repository.

xiaoxiang 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 f426265ab testing: mm test will be skipped to prevent memory overflow
f426265ab is described below

commit f426265ab2b2dfd2efad5345ad5c8835170d8b18
Author: chenzihan1 <[email protected]>
AuthorDate: Tue Jun 11 09:49:34 2024 +0800

    testing: mm test will be skipped to prevent memory overflow
    
    In the mm test, if the memory requested this time exceeds 3/4 of the 
remaining memory,
    the request will be skipped to avoid insufficient memory.
    
    Signed-off-by: chenzihan1 <[email protected]>
---
 testing/mm/mm_main.c | 117 +++++++++++++++++++++++++++++++++++----------------
 1 file changed, 81 insertions(+), 36 deletions(-)

diff --git a/testing/mm/mm_main.c b/testing/mm/mm_main.c
index 40fbb6bf9..0b9b53dfa 100644
--- a/testing/mm/mm_main.c
+++ b/testing/mm/mm_main.c
@@ -124,6 +124,17 @@ static struct mallinfo g_alloc_info;
  * Private Functions
  ****************************************************************************/
 
+static bool is_oversize(int size)
+{
+  if (size < 0)
+    {
+      return false;
+    }
+
+  unsigned long threshold = g_alloc_info.mxordblk * 3 / 4;
+  return size > threshold;
+}
+
 static void mm_showmallinfo(void)
 {
   g_alloc_info = mallinfo();
@@ -149,17 +160,22 @@ static void do_mallocs(FAR void **mem, FAR const int 
*size,
   for (i = 0; i < n; i++)
     {
       j = seq[i];
+      int allocsize = MM_ALIGN_UP(size[j] + MM_SIZEOF_ALLOCNODE);
       if (!mem[j])
         {
-          printf("(%d)Allocating %d bytes\n", i,  size[j]);
+          printf("(%d)Allocating %d bytes\n", i, allocsize);
+          if (is_oversize(allocsize))
+            {
+              printf("(%d)The allocated memory exceeds the threshold, "
+                       "skipping\n", i);
+              continue;
+            }
 
           mem[j] = malloc(size[j]);
           printf("(%d)Memory allocated at %p\n", i, mem[j]);
 
           if (mem[j] == NULL)
             {
-              int allocsize = MM_ALIGN_UP(size[j] + MM_SIZEOF_ALLOCNODE);
-
               fprintf(stderr, "(%d)malloc failed for allocsize=%d\n",
                       i, allocsize);
 
@@ -196,6 +212,15 @@ static void do_reallocs(FAR void **mem, FAR const int 
*oldsize,
   for (i = 0; i < n; i++)
     {
       j = seq[i];
+      int allocsize = MM_ALIGN_UP(newsize[j] + MM_SIZEOF_ALLOCNODE) -
+                      MM_ALIGN_UP(oldsize[j] + MM_SIZEOF_ALLOCNODE);
+      if (is_oversize(allocsize))
+        {
+          printf("(%d)The reallocs memory exceeds the threshold, "
+                    "skipping\n", i);
+          continue;
+        }
+
       printf("(%d)Re-allocating at %p from %d to %d bytes\n",
              i, mem[j], oldsize[j], newsize[j]);
 
@@ -213,8 +238,6 @@ static void do_reallocs(FAR void **mem, FAR const int 
*oldsize,
 
       if (ptr == NULL)
         {
-          int allocsize = MM_ALIGN_UP(newsize[j] + MM_SIZEOF_ALLOCNODE);
-
           fprintf(stderr,
                   "(%d)realloc failed for allocsize=%d\n", i, allocsize);
           if (allocsize > g_alloc_info.mxordblk)
@@ -249,17 +272,22 @@ static void do_memaligns(FAR void **mem,
   for (i = 0; i < n; i++)
     {
       j = seq[i];
+      int allocsize = MM_ALIGN_UP(size[j] + MM_SIZEOF_ALLOCNODE) +
+                                      2 * align[i];
       printf("(%d)Allocating %d bytes aligned to 0x%08x\n",
              i,  size[j], align[i]);
+      if (is_oversize(allocsize))
+        {
+          printf("(%d)The reallocs memory exceeds the threshold, "
+                    "skipping\n", i);
+          continue;
+        }
 
       mem[j] = memalign(align[i], size[j]);
       printf("(%d)Memory allocated at %p\n", i, mem[j]);
 
       if (mem[j] == NULL)
         {
-          int allocsize = MM_ALIGN_UP(size[j] + MM_SIZEOF_ALLOCNODE) +
-                                      2 * align[i];
-
           fprintf(stderr,
                   "(%d)memalign failed for allocsize=%d\n", i, allocsize);
           if (allocsize > g_alloc_info.mxordblk)
@@ -311,36 +339,11 @@ static void do_frees(FAR void **mem, FAR const int *size,
     }
 }
 
-static int mm_stress_test(int argc, FAR char *argv[])
+static int mm_stress_test(int delay, int prio, int maxsize)
 {
   FAR unsigned char *tmp;
-  int delay = 1;
-  int prio = 0;
   int size;
   int i;
-  int maxsize = 1024;
-
-  while ((i = getopt(argc, argv, "d:p:s:")) != ERROR)
-    {
-      if (i == 'd')
-        {
-          delay = atoi(optarg);
-        }
-      else if (i == 'p')
-        {
-          prio = atoi(optarg);
-        }
-      else if (i == 's')
-        {
-          maxsize = atoi(optarg);
-        }
-      else
-        {
-          printf("Unrecognized option: '%c'\n", i);
-          return -EINVAL;
-        }
-    }
-
   if (prio != 0)
     {
       struct sched_param param;
@@ -380,9 +383,51 @@ static int mm_stress_test(int argc, FAR char *argv[])
 
 int main(int argc, FAR char *argv[])
 {
-  if (argc > 1)
+  int stress_test_mode = 0;
+  int delay = 1;
+  int prio = 0;
+  int i;
+  int maxsize = 1024;
+
+  while ((i = getopt(argc, argv, "mfd:p:s:")) != ERROR)
+    {
+      switch (i)
+        {
+          case 'm':
+            {
+              stress_test_mode = 1;
+              break;
+            }
+
+          case 'd':
+            {
+              delay = atoi(optarg);
+              break;
+            }
+
+          case 'p':
+            {
+              prio = atoi(optarg);
+              break;
+            }
+
+          case 's':
+            {
+              maxsize = atoi(optarg);
+              break;
+            }
+
+          default:
+            {
+              printf("Unrecognized option: '%c'\n", i);
+              return -EINVAL;
+            }
+        }
+    }
+
+  if (stress_test_mode)
     {
-      return mm_stress_test(argc, argv);
+      return mm_stress_test(delay, prio, maxsize);
     }
 
   mm_showmallinfo();

Reply via email to