/* * thrash.c * * A standalone Unix command-line program to * make the machine thrash, ie go into permanent swapping, * by using VM >= RAM size and accessing all pages repeatedly * * Usage: thrash size * where "size" is the number of megabytes to thrash * A good choice for N is the number of megabytes of physical RAM * that the machine has. * * Reason: * to force a machine to use its swap space, * to flush all unused pages out to swap and so free RAM for other purposes * or to see how a system behaves under extreme duress. * * It currently *writes* to all pages, but could be made to read them * as an alternative, or as well. * * Martin Guy, 9 November 2006 */ #include /* for exit() */ #include #include /* for system calls */ main(int argc, char **argv) { int megabytes = 0; /* MB of VM to thrash, from command-line argument. * 0 means uninitialised */ char *buf; /* Huge VM buffer */ intptr_t bufsize; /* Size of buffer in bytes */ long pagesize; /* size of VM page */ int i; /* index into argv */ int verbose = 0; /* Print a dot for every pass through VM? */ pagesize = getpagesize(); for (i=1; i0; p+=pagesize, i--) *p=(char) i; if (verbose) { putchar('.'); fflush(stdout); } } /* NOTREACHED */ }