Skip to content

Commit f426265

Browse files
chenzihan0416xiaoxiang781216
authored andcommitted
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>
1 parent 4b8375e commit f426265

File tree

1 file changed

+81
-36
lines changed

1 file changed

+81
-36
lines changed

testing/mm/mm_main.c

+81-36
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,17 @@ static struct mallinfo g_alloc_info;
124124
* Private Functions
125125
****************************************************************************/
126126

127+
static bool is_oversize(int size)
128+
{
129+
if (size < 0)
130+
{
131+
return false;
132+
}
133+
134+
unsigned long threshold = g_alloc_info.mxordblk * 3 / 4;
135+
return size > threshold;
136+
}
137+
127138
static void mm_showmallinfo(void)
128139
{
129140
g_alloc_info = mallinfo();
@@ -149,17 +160,22 @@ static void do_mallocs(FAR void **mem, FAR const int *size,
149160
for (i = 0; i < n; i++)
150161
{
151162
j = seq[i];
163+
int allocsize = MM_ALIGN_UP(size[j] + MM_SIZEOF_ALLOCNODE);
152164
if (!mem[j])
153165
{
154-
printf("(%d)Allocating %d bytes\n", i, size[j]);
166+
printf("(%d)Allocating %d bytes\n", i, allocsize);
167+
if (is_oversize(allocsize))
168+
{
169+
printf("(%d)The allocated memory exceeds the threshold, "
170+
"skipping\n", i);
171+
continue;
172+
}
155173

156174
mem[j] = malloc(size[j]);
157175
printf("(%d)Memory allocated at %p\n", i, mem[j]);
158176

159177
if (mem[j] == NULL)
160178
{
161-
int allocsize = MM_ALIGN_UP(size[j] + MM_SIZEOF_ALLOCNODE);
162-
163179
fprintf(stderr, "(%d)malloc failed for allocsize=%d\n",
164180
i, allocsize);
165181

@@ -196,6 +212,15 @@ static void do_reallocs(FAR void **mem, FAR const int *oldsize,
196212
for (i = 0; i < n; i++)
197213
{
198214
j = seq[i];
215+
int allocsize = MM_ALIGN_UP(newsize[j] + MM_SIZEOF_ALLOCNODE) -
216+
MM_ALIGN_UP(oldsize[j] + MM_SIZEOF_ALLOCNODE);
217+
if (is_oversize(allocsize))
218+
{
219+
printf("(%d)The reallocs memory exceeds the threshold, "
220+
"skipping\n", i);
221+
continue;
222+
}
223+
199224
printf("(%d)Re-allocating at %p from %d to %d bytes\n",
200225
i, mem[j], oldsize[j], newsize[j]);
201226

@@ -213,8 +238,6 @@ static void do_reallocs(FAR void **mem, FAR const int *oldsize,
213238

214239
if (ptr == NULL)
215240
{
216-
int allocsize = MM_ALIGN_UP(newsize[j] + MM_SIZEOF_ALLOCNODE);
217-
218241
fprintf(stderr,
219242
"(%d)realloc failed for allocsize=%d\n", i, allocsize);
220243
if (allocsize > g_alloc_info.mxordblk)
@@ -249,17 +272,22 @@ static void do_memaligns(FAR void **mem,
249272
for (i = 0; i < n; i++)
250273
{
251274
j = seq[i];
275+
int allocsize = MM_ALIGN_UP(size[j] + MM_SIZEOF_ALLOCNODE) +
276+
2 * align[i];
252277
printf("(%d)Allocating %d bytes aligned to 0x%08x\n",
253278
i, size[j], align[i]);
279+
if (is_oversize(allocsize))
280+
{
281+
printf("(%d)The reallocs memory exceeds the threshold, "
282+
"skipping\n", i);
283+
continue;
284+
}
254285

255286
mem[j] = memalign(align[i], size[j]);
256287
printf("(%d)Memory allocated at %p\n", i, mem[j]);
257288

258289
if (mem[j] == NULL)
259290
{
260-
int allocsize = MM_ALIGN_UP(size[j] + MM_SIZEOF_ALLOCNODE) +
261-
2 * align[i];
262-
263291
fprintf(stderr,
264292
"(%d)memalign failed for allocsize=%d\n", i, allocsize);
265293
if (allocsize > g_alloc_info.mxordblk)
@@ -311,36 +339,11 @@ static void do_frees(FAR void **mem, FAR const int *size,
311339
}
312340
}
313341

314-
static int mm_stress_test(int argc, FAR char *argv[])
342+
static int mm_stress_test(int delay, int prio, int maxsize)
315343
{
316344
FAR unsigned char *tmp;
317-
int delay = 1;
318-
int prio = 0;
319345
int size;
320346
int i;
321-
int maxsize = 1024;
322-
323-
while ((i = getopt(argc, argv, "d:p:s:")) != ERROR)
324-
{
325-
if (i == 'd')
326-
{
327-
delay = atoi(optarg);
328-
}
329-
else if (i == 'p')
330-
{
331-
prio = atoi(optarg);
332-
}
333-
else if (i == 's')
334-
{
335-
maxsize = atoi(optarg);
336-
}
337-
else
338-
{
339-
printf("Unrecognized option: '%c'\n", i);
340-
return -EINVAL;
341-
}
342-
}
343-
344347
if (prio != 0)
345348
{
346349
struct sched_param param;
@@ -380,9 +383,51 @@ static int mm_stress_test(int argc, FAR char *argv[])
380383

381384
int main(int argc, FAR char *argv[])
382385
{
383-
if (argc > 1)
386+
int stress_test_mode = 0;
387+
int delay = 1;
388+
int prio = 0;
389+
int i;
390+
int maxsize = 1024;
391+
392+
while ((i = getopt(argc, argv, "mfd:p:s:")) != ERROR)
393+
{
394+
switch (i)
395+
{
396+
case 'm':
397+
{
398+
stress_test_mode = 1;
399+
break;
400+
}
401+
402+
case 'd':
403+
{
404+
delay = atoi(optarg);
405+
break;
406+
}
407+
408+
case 'p':
409+
{
410+
prio = atoi(optarg);
411+
break;
412+
}
413+
414+
case 's':
415+
{
416+
maxsize = atoi(optarg);
417+
break;
418+
}
419+
420+
default:
421+
{
422+
printf("Unrecognized option: '%c'\n", i);
423+
return -EINVAL;
424+
}
425+
}
426+
}
427+
428+
if (stress_test_mode)
384429
{
385-
return mm_stress_test(argc, argv);
430+
return mm_stress_test(delay, prio, maxsize);
386431
}
387432

388433
mm_showmallinfo();

0 commit comments

Comments
 (0)