#include #include #include #include #include /* memstress - Illustrate the effect of locality on memory performance. Usage: memstress <#bits of address space> <# of repetitions> Copyright (c) 1999 Nathan Meyers $Id: memstress.c,v 1.2 1999/11/09 20:30:33 nathanm Exp $ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ int main(int argc, char **argv) { int nbits, repetitions; size_t stride, memsize, memsizemask; char *memory; if (argc != 3) { fprintf(stderr, "Usage: %s <#bits of address space> <#repetitions>\n", argv[0]); exit(1); } nbits = atoi(argv[1]); repetitions = atoi(argv[2]); /* Allocate our memory */ memsize = (size_t)1 << nbits; memsizemask = memsize - 1; memory = (char *)malloc(memsize); if (!memory) { fprintf(stderr, "Insufficient memory to allocate %ld bytes\n", (long)memsize); exit(1); } /* Touch the memory before we start */ memset(memory, 255, memsize); /* Start our output */ printf("Memory size = %ld bytes (1 << %d)\n", (long)memsize, nbits); printf("Number of repetitions = %d\n", repetitions); printf("\n Stride Time\n"); printf(" ------ ----\n"); /* Stress the memory, with different stride values */ for (stride = 1; stride < memsize; stride = stride * 2 + 1) { struct timeval time1, time2; struct timezone tz; double delta, rate; register size_t offset = 0; register long count; volatile register char *mem = memory, chr; gettimeofday(&time1, &tz); printf("%12ld ", stride); fflush(stdout); /* Start accessing memory */ for (count = (long)memsize * (long)repetitions; count--; offset = (offset + stride) & memsizemask) { chr = mem[offset]; } gettimeofday(&time2, &tz); delta = time2.tv_sec + time2.tv_usec / 1.0e6 - time1.tv_sec - time1.tv_usec / 1.0e6; rate = (double)memsize * repetitions / delta; printf("%lf sec (%lf bytes/sec)\n", delta, rate); } exit(0); }