Friday, January 19, 2007

Inducing Low Memory Conditions

Testing under stress and load conditions are important for every application. Having empirical knowledge of what the breaking point is for any computer hosting an application helps you to plan for such an occaision, as well as giving you an idea of what sort of load the machine can handle before it buckles. If you're aware that having 1000 simultaneous users on a web application you've built is the down-hill tipping point for failure, and you're seeing peak usage at 1200 users a day, you know that it's time to either get another machine or tweak the code to allow for more users on the machine.

Recently I did some searching for a program that would induce low memory conditions on my linux box. I wanted to do some stress testing on a web app while the machine was low in memory. Unfortunately I couldn't find a decent program, so I wrote one! Since I've found that "tutorial" sites are very popular (I visit them myself daily), I thought posting this might be good for the blog. And what's good for the blog, is good for Ryan.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MEGABYTE 1024*1024
#define LOWMEMORY 30000

/*
 * SUMMARY: Induce low memory conditions by allocating memory in 1 MB chunks and checking free memory until there is only LOWMEMORY KB of free memory left
 * REQUIREMENTS: Must be run on a Linux machine, and the LOWMEMORY number should be in KB
 * AUTHOR: Ryan Gerard, ryan dot gerard at gmail dot com
 */

int main(int argc, char *argv[]) {
     void *myblock = NULL;
     int count = 0;
     int freeMem = 0;

     //Allocate memory in 1 MB chunks as long as the free mem can be found and the free mem is greater than LOWMEMORY KB
     while( (freeMem = getFreeMem()) != -1 && freeMem > LOWMEMORY)
     {
          myblock = (void *) malloc(MEGABYTE);
          if (!myblock) break;
          memset(myblock,1, MEGABYTE);
          printf("Currently allocating %d MB\n",++count);
          printf("Free mem is %d KB\n", freeMem);
     }

     //Print out amount of free memory left
     printf("%d free memory left\n", getFreeMem());
     exit(0);
}

/*
 * Parse the output of 'vmstat' to determine the amount of free memory left on the system
 */
int getFreeMem()
{
     FILE *readFile;
     char string[256];
     int cnt = 0;
     int freeMem = -1;

     //Output memory information to file tmpfile
     system("vmstat -a 1 1 > tmpfile");

     //Open the file for reading
     if ( (readFile = fopen("tmpfile", "r")) != NULL )
     {
          //Get the third line of text from the file
          while( fgets(string, 256, readFile) && cnt < 2 )
               cnt++

          cnt = 0;
          char* token = strtok(string, " ");
          while(token != NULL && cnt < 3)
          {
               token = strtok(NULL, " ");
               cnt++;
          }

          //Convert to an integer to return
          freeMem = atoi(token);

          //Close the file
          fclose(readFile);
     }

     return freeMem;
}

25 comments:

Jack Diederich said...

Check the python-dev lists. There is a C malloc replacement (failloc? I don't remember offhand) that will return allocation errors with configurable frequency. As I recall just one or two guys found lots of impossible or unlikely failures by running it with the python test suite. Just keep running the suite and lowering the frequency of failure until things even out ("just" - ha!)

Seriously though, it did catch bugs in low system memory conditions. The unit tests that flex memory exhaution are focused on gigs and not megs. The random failure of malloc exposed the small allocations during VM startup that failed. That sounds like what you need.

Anonymous said...

free viagra in the uk cheapest place to buy viagra online viagra england viagra 34434 viagra stories viagra price comparison try viagra for free viagra england viagra online stores viagra from canada 18 takes viagra where to buy viagra viagra and cocaine viagra paypal

Anonymous said...

[url=http://www.xbox360achievements.org/forum/member.php?u=259462]mexitil discount[/url]

Anonymous said...

miley cyrus nude [url=http://www.ipetitions.com/petition/mileycyrus]miley cyrus nude[/url] paris hilton nude [url=http://www.ipetitions.com/petition/parishilt]paris hilton nude[/url] kim kardashian nude [url=http://www.ipetitions.com/petition/kimkardashian45]kim kardashian nude[/url] kim kardashian nude [url=http://www.ipetitions.com/petition/celebst]kim kardashian nude[/url]

Anonymous said...

Hi!


Mansour Engineering always considers adding high performance features in large capital projects that maximize operational energy savings; improve comfort, health, and safety of occupants and visitors; and limit detrimental effects on the environment.

[url=http://www.mansour.ca] click here to go to Mansour Engineering[/url]


http://www.mansour.ca

Anonymous said...

Free ebay finally


The new kid on the block is http://www.BuySellDirect.net their internet selling free service is the Future of E-commerce. If you will not use such wonderful FREE service to make extra money at home, then tell your friends and family members about it.


http://www.BuySellDirect.net is 100% FREE

Anonymous said...

Lovingly done is better than well said.

Anonymous said...

Artistically done is well-advised b wealthier than spectacularly said.

Anonymous said...

Artistically done is well-advised b wealthier than well said.

Anonymous said...

Well done is better than well said.

Anonymous said...

Splendidly done is sick than well said.

Anonymous said...

A human beings who dares to barrens anyone hour of age has not discovered the value of life.

[url=http://www.swvatoday.com/forums/member/3120/]Jane[/url]


Gilda

Anonymous said...

A man who dares to waste anyone hour of age has not discovered the value of life.

[url=http://www.kitabureadinggroup.com/apps/profile/profilePage?id=54280822]Linda[/url]


Ana

Anonymous said...

We should be chary and fussy in all the par‘nesis we give. We should be extraordinarily aware in giving opinion that we would not about of following ourselves. Most of all, we ought to escape giving counsel which we don't tag along when it damages those who depreciate us at our word.

ryobi

[url=http://ryobi-69.webs.com/apps/blog/]ryobi[/url]

Anonymous said...

A human beings begins cutting his discernment teeth the earliest time he bites out more than he can chew.

Anonymous said...

To be a adroit charitable being is to be enduring a amiable of openness to the far-out, an gift to trust undeterminable things beyond your own control, that can front you to be shattered in unequivocally outermost circumstances for which you were not to blame. That says something remarkably outstanding about the prerequisite of the ethical passion: that it is based on a conviction in the up in the air and on a willingness to be exposed; it's based on being more like a plant than like a treasure, something somewhat tenuous, but whose very particular attraction is inseparable from that fragility.

Anonymous said...

To be a good benign being is to from a make of openness to the in the seventh heaven, an skill to group unsure things beyond your own control, that can front you to be shattered in very extreme circumstances on which you were not to blame. That says something exceedingly important thither the prerequisite of the honest compulsion: that it is based on a corporation in the unpredictable and on a willingness to be exposed; it's based on being more like a weed than like a sparkler, something kind of feeble, but whose mere particular beauty is inseparable from that fragility.

Anonymous said...

Vex ferments the humors, casts them into their right channels, throws substandard redundancies, and helps species in those secretive distributions, without which the fuselage cannot subsist in its vigor, nor the typification role of with cheerfulness.

Anonymous said...

Work out ferments the humors, casts them into their right channels, throws off redundancies, and helps feather in those confidential distributions, without which the fuselage cannot subsist in its vigor, nor the incarnation act with cheerfulness.

Anonymous said...

While you set up your property for your first open house event, try to envision it from the point of view of a likely buyer. Make sure that the whole home is orderly and that it appears inviting. Turn on all the main lights in the house so that each and every area feels welcoming and pleasant. Where you can, do a little staging. This will likely include placing some reading material on a end table, or placing a vase of fresh flowers on the [url=http://www.handfans123.com]hand fans[/url] kitchen table. A lot of real-estate agents even go so far as to make some cookies or bread to give the house a [url=http://www.handfans123.com]custom hand fans[/url] inviting and cozy atmosphere.

Vinny said...

Well done. I m certainly impressed with the calibre of the advice supplied. I expect that you continue with the excellent work achieved.
irvine locksmith

penisenlargement4male said...

It is best to participate in a contest for among the best blogs on the web. I'll recommend this website!
Vimax Pills Enhance VigRX Plus metropathies Male Extra Amp Do Vimax pills really work mastoidal
More Info:
Penis Enlargement|Penis Enlargement Pills|Vimax Pills|Male Extra Pills |VigRX Plus

Anonymous said...

buy facebook likes
facebook likes

http://bradpaisley.com/tour/nashville-tn-cma-fest-2011-06-09 http://www.provokator.org/polls/you-take-public-bus-30min-free-seats-spiked-hair-hiphopper-granny-roma-girl-bus-driver-where-d
1000 facebook likes get facebook likes buy facebook likes
At work the man I worked with got a virus opening a picture and when I am not there he tends to use my computer because its faster, I have important information about payrolls and financial info about the company and I can't afford to lose it. Thanks

facebook likes buy facebook likes [url=http://1000fbfans.info]get facebook likes [/url] buy facebook likes

Anonymous said...

A encyclopaedic fitness program tailored to an person resolve unquestionably nave on one or more delineated skills, and on age-[3] or health-related needs such as bone health.[4] Innumerable sources[citation needed] also cite disposition, social and heated constitution as an superior part of all-inclusive fitness. This is regularly presented in textbooks as a triangle made up of three points, which represent solid, emotional, and mental fitness. Bones seemliness can also hamper or act towards multifarious persistent salubrity conditions brought on next to insalubrious lifestyle or aging.[5] Working out can also help people saw wood better. To delay robust it is important to agree in actual activity.
Training

Unique to or task-oriented [url=http://www.pella.pl]fitness[/url] is a living soul's talents to depict in a definite enterprise with a sober efficiency: for pattern, sports or military service. Specific training prepares athletes to put on well in their sports.

Examples are:

400 m sprint: in a sprint the athlete should be trained to line anaerobically throughout the race.
Marathon: in this specimen the athlete have to be trained to being done aerobically and their tenacity requisite be built-up to a maximum.
Many run a risk fighters and patrol officers subject oneself to unvarying good physical condition testing to end if they are capable of the physically demanding tasks required of the job.
Members of the Partnership States Army and Army National Guard have to be able to pass the Army Tangible Fitness Test (APFT).

Anonymous said...

Like a huge candle which shanghai escort is self-assured to craze such as pitch pitch Do not pace stock between plates wheeled accentuation