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 <chenzihan1@xiaomi.com>
This commit is contained in:
chenzihan1 2024-06-11 09:49:34 +08:00 committed by Xiang Xiao
parent 4b8375ea95
commit f426265ab2

View file

@ -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)
{
return mm_stress_test(argc, argv);
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(delay, prio, maxsize);
}
mm_showmallinfo();