Operating System - HP-UX
1825769 Members
2128 Online
109687 Solutions
New Discussion

little program to stress test memory

 
SOLVED
Go to solution
Anthony deRito
Respected Contributor

little program to stress test memory


I have this program that I got from this forum but I don't recall from who. I want to keep allocating chunks of memory to monitor memory alarms. It not doing what I thought it would do. Can someone who is familiar with C take a look at this? Here is the short code:

#include
#define CHUNK (1024 * 1024)

#define assign_errno(x) ((errno != 0) ? errno : (x))
extern int errno;

int main(argc,argv)
int argc;
char *argv[];
{
int cc = 0,stop = -1;
long tot = 0L;
char *p = NULL;

if (argc > 1) stop = atoi(argv[1]);
if (stop > 0)
{
int i = 0;
while (i < stop && cc == 0)
{
p = (char *) malloc((size_t) CHUNK);
if (p != NULL)
{
tot += CHUNK;
++i;
(void) printf("%4d. %9ld\n",i,tot);
}
else
{
cc = assign_errno(-1);
}
}
}
else
{
while (cc == 0)
{
p = (char *) malloc((size_t) CHUNK);
if (p != NULL)
{
tot += CHUNK;
(void) printf("%9ld\n",tot);
}
else cc = assign_errno(-1);
}
}
return(cc);
} /* main */


Any help would be appreciated.

Also, any other suggestions to allocate memory on a server to bring it to its knees would be great.

Thank you!!

Tony
1 REPLY 1
Bill Hassell
Honored Contributor
Solution

Re: little program to stress test memory

It look a little bit like a program I've posted in the past which was probably a sample program in an HP tech note. However, there are a *lot* of pieces of information missing, namely, how to compile the program for large amounts of RAM. The first is to change your kernel parameters maxdsiz and maxdsiz_64. You want maxdsiz to be as large as possible for a 32bit program but this is a moving target depending on the version of HP-UX you have and the patches you have added.

A straight compile will produce a program that simply grows to the size of maxdsiz (60 megs default or 250 megs for new versions of 11i). So in SAM, change maxdsiz to 2000megs and now the program will hit 900 megs. Make sure that maxdsiz_64 is LARGER than maxdsiz.

Now recompile the program with the flags -Ae -Wl -N as in:

cc -Ae -Wl -N -o mallocbig mallocbig.c

This changes the program to connect two 1Gb quadrants together and now the prgram will get about 1700 megs. But wait, there's more!!! Let's start with the opsystem: if it is only a 32bit OS (like 10.20 or 11.xx in 32bit), this is about as far as you can go. Use this to see what size the OS is running:

getconf KERNEL_BITS

If it says 64, keep going, it can get a *LOT* bigger. First, change maxdsiz (and maxdsia_64 if necessary) to 3Gb.

Now change the type of program to q3p (details are in the white papers and release notes):

chatr +q3p enable mallocbig

This changes the program to connect 3 quadrants together and you'll get about 2700megs. Change maxdsiz to Now change the program once again to use all 4 quadrants with:

chatr +q4p enable mallocbig

and you'll get about 3600 megs. But wait, let's bring out the big guns (the reason that 64bits is needed). Change maxdsiz_64 to 50Gb or maybe 200Gb. Then, recompile the program again like this:

cc -Ae -DD64 -o mallocbig mallocbig.c

and now the program will allocate as much memory as you have swap space, dozens to hundreds of Gb.

Now as I mentioned, success depends on patches, and you'll need the ANSI compiler to compile the program.

Now, VERY IMPORTANT: this program simply reserves the space and writes nothing into the space. Depending on how you measure memory usage, this large program may not be seen as most of it is bookeeping (virtual). I have attached a program that repeatedly asks you how many additional megs of RAM you would like, but it also writes a pattern into the RAM, thius forcing swapping and memory re-mapping. Use the exact same options as I mentioned above (they're also in the code).


Bill Hassell, sysadmin