|
Just following up my other message with some code you can use to measure memory access performance. It takes one required parameter - the amount of memory to manipulate, and one optional parameter - a flag indicating whether to print out the 'touch rate' for every megabyte (by default this is 'on'; specify '0' as the second parm to turn it off.) Anyway, the program measures three things: 1. How long it takes to perform the malloc(). 2. The 'touch rate' per million bytes (optional; on by default). 3. The total time it takes to touch all allocated bytes. The program uses 'gettimeofday()' in order to get microsecond resolution. Summarizing the results I get on my V5R2 machine, for 24,000,000 bytes: malloc() time: 27,392 microseconds Typical time to touch 1 million bytes BELOW 16MB: around 225,000 microseconds Typical time to touch 1 million bytes ABOVE 16MB: around 3 seconds + 300,000 microseconds By the way, this affects Java applications as well (try to use byte[] arrays >16MB.) Following is the code. Note that you have to specify the 'teraspace' options on the C compiler in order to allocate >16MB of contiguous memory. /*** BEGIN PROGRAM ***/ #include <stdio.h> #include <stdlib.h> #include <sys/time.h> void computeTimeDiff(struct timeval *start, struct timeval *finish, struct timeval *difference) { difference->tv_sec = finish->tv_sec - start->tv_sec; difference->tv_usec = finish->tv_usec - start->tv_usec; while(difference->tv_usec < 0) { difference->tv_sec--; difference->tv_usec += 1000000L; } return; } int main(int argc, char *argv[]) { int mem = 0, i = 0, showRate = 1; char *p = NULL; struct timeval beforeMalloc, afterMalloc, beforeLoop, afterLoop; struct timeval mallocDiff, loopDiff; struct timeval mbStart, mbCurrent, mbDiff; if(argc < 2) { printf("Usage: %s <memory size>\n", argv[0]); exit(1); } if(argc > 2) /* Check 'showRate' flag */ { if((argv[2][0] == 'n') || (argv[2][0] == 'N') || (argv[2][0] == '0')) { showRate = 0; } } mem = atoi(argv[1]); if(mem < 1) { printf("%s: must specify memory size > 0\n", argv[0]); exit(1); } gettimeofday(&beforeMalloc, NULL); p = malloc(mem); gettimeofday(&afterMalloc, NULL); computeTimeDiff(&beforeMalloc, &afterMalloc, &mallocDiff); printf("Attempt to malloc '%d' bytes took: %ld sec and %ld us\n", mem, mallocDiff.tv_sec, mallocDiff.tv_usec); if(p == NULL) { printf("%s: failed to malloc '%d' byts of memory\n", argv[0], mem); exit(1); } gettimeofday(&beforeLoop, NULL); gettimeofday(&mbStart, NULL); for(i = 0; i < mem; i++) { if(showRate && (i >= 1000000) && ((i % 1000000) == 0)) { gettimeofday(&mbCurrent, NULL); computeTimeDiff(&mbStart, &mbCurrent, &mbDiff); printf("Current offset: %d, rate: %ld s and %ld us\n", i, mbDiff.tv_sec, mbDiff.tv_usec); gettimeofday(&mbStart, NULL); } p[i] = 'x'; } gettimeofday(&afterLoop, NULL); computeTimeDiff(&beforeLoop, &afterLoop, &loopDiff); printf("Touching '%d' bytes took: %ld sec and %ld us\n", mem, loopDiff.tv_sec, loopDiff.tv_usec); printf("\nFinished!\n\n"); return(0); } /*** END PROGRAM ***/ -- Mike Mills mikem@xxxxxxxx ConsulTech Information Systems, Inc.
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.