daveb@rtech.UUCP (02/17/87)
> A while back I asked the net if anyone had a malloc/free test program that > would allocate and free randomly sized pieces of memory with random > lifetimes... [ and one follows ] Here is a program I've been using recently. It is a lot more intensive than Neil Webber's, and shows you what the process size is doing. I am usually comfortable when I can run an allocator through 10 million or so loops with out a problem. A useful addition would be computation of kilo-core-seconds. As usual, no documentation and best wishes. -dB ---------------- cut here ---------------- #! /bin/sh # This is a shell archive, meaning: # 1. Remove everything above the #! /bin/sh line. # 2. Save the resulting text in a file. # 3. Execute the file with /bin/sh (not csh) to create the files: # looptest.c # This archive created: Tue Feb 17 10:54:15 1987 export PATH; PATH=/bin:$PATH if test -f 'looptest.c' then echo shar: will not over-write existing file "'looptest.c'" else sed 's/^X//' << \SHAR_EOF > 'looptest.c' X/* X** looptest.c -- intensive allocator tester X** X** Usage: looptest X** X** History: X** 4-Feb-1987 rtech!daveb X*/ X X X# ifdef SYS5 X# define random rand X# else X# include <sys/vadvise.h> X# endif X X# include <stdio.h> X# include <signal.h> X# include <setjmp.h> X X# define MAXITER 1000000 /* main loop iterations */ X# define MAXOBJS 1000 /* objects in pool */ X# define BIGOBJ 90000 /* max size of a big object */ X# define TINYOBJ 80 /* max size of a small object */ X# define BIGMOD 100 /* 1 in BIGMOD is a BIGOBJ */ X# define STATMOD 10000 /* interation interval for status */ X Xmain( argc, argv ) Xint argc; Xchar **argv; X{ X register int **objs; /* array of objects */ X register int *sizes; /* array of object sizes */ X register int n; /* iteration counter */ X register int i; /* object index */ X register int size; /* object size */ X register int r; /* random number */ X X int objmax; /* max size this iteration */ X int cnt; /* number of allocated objects */ X int nm = 0; /* number of mallocs */ X int nre = 0; /* number of reallocs */ X int nal; /* number of allocated objects */ X int nfre; /* number of free list objects */ X long alm; /* memory in allocated objects */ X long frem; /* memory in free list */ X long startsize; /* size at loop start */ X long endsize; /* size at loop exit */ X X extern char end; /* memory before heap */ X char *calloc(); X char *malloc(); X char *sbrk(); X X# ifndef SYS5 X /* your milage may vary... */ X vadvise( VA_ANOM ); X# endif X X printf("MAXITER %d MAXOBJS %d ", MAXITER, MAXOBJS ); X printf("BIGOBJ %d, TINYOBJ %d, nbig/ntiny 1/%d\n", X BIGOBJ, TINYOBJ, BIGMOD ); X fflush( stdout ); X X if( NULL == (objs = (int **)calloc( MAXOBJS, sizeof( *objs ) ) ) ) X { X fprintf(stderr, "Can't allocate memory for objs array\n"); X exit(1); X } X X if( NULL == ( sizes = (int *)calloc( MAXOBJS, sizeof( *sizes ) ) ) ) X { X fprintf(stderr, "Can't allocate memory for sizes array\n"); X exit(1); X } X X /* as per recent discussion on net.lang.c, calloc does not X ** necessarily fill in NULL pointers... X */ X for( i = 0; i < MAXOBJS; i++ ) X objs[ i ] = NULL; X X startsize = sbrk(0) - &end; X printf( "Memory use at start: %d bytes\n", startsize ); X fflush(stdout); X X printf("Starting the test...\n"); X fflush(stdout); X corrupt = 0; X for( n = 0; n < MAXITER ; n++ ) X { X if( !(n % STATMOD) ) X { X printf("%d iterations\n", n); X fflush(stdout); X } X X /* determine object of interst and it's size */ X X r = random(); X objmax = ( r % BIGMOD ) ? TINYOBJ : BIGOBJ; X size = r % objmax; X i = r % (MAXOBJS - 1); X X /* either replace the object of get a new one */ X X if( objs[ i ] == NULL ) X { X objs[ i ] = (int *)malloc( size ); X nm++; X } X else X { X /* don't keep bigger objects around */ X if( size > sizes[ i ] ) X { X objs[ i ] = (int *)realloc( objs[ i ], size ); X nre++; X } X else X { X free( objs[ i ] ); X objs[ i ] = (int *)malloc( size ); X nm++; X } X } X X sizes[ i ] = size; X if( objs[ i ] == NULL ) X { X printf("\nCouldn't allocate %d byte object!\n", X size ); X break; X } X } /* for() */ X X printf( "\n" ); X cnt = 0; X for( i = 0; i < MAXOBJS; i++ ) X if( objs[ i ] ) X cnt++; X X printf( "Did %d iterations, %d objects, %d mallocs, %d reallocs\n", X n, cnt, nm, nre ); X printf( "Memory use at end: %d bytes\n", sbrk(0) - &end ); X fflush( stdout ); X X /* free all the objects */ X for( i = 0; i < MAXOBJS; i++ ) X if( objs[ i ] != NULL ) X free( objs[ i ] ); X X endsize = sbrk(0) - &end; X printf( "Memory use after free: %d bytes\n", endsize ); X fflush( stdout ); X X if( startsize != endsize ) X printf("startsize %d != endsize %d\n", startsize, endsize ); X X free( objs ); X free( sizes ); X X exit( 0 ); X} X SHAR_EOF fi # end of overwriting check # End of shell archive exit 0 -- If it worked, we wouldn't call it high tech. {amdahl, sun, mtxinu, cbosgd}!rtech!daveb
michael@orcisi.UUCP (02/20/87)
There seems to be a meaningless corrupt = 0; in this program and corrupt is not delcared anywhere.