#include #include #include #include #include #include #include #define CACHE_MIN (8*128) /* smallest cache */ #define CACHE_MAX (8*1024*1024) /* largest cache */ #define SAMPLE 20 /* to get larger time sample */ //XXX #define SAMPLE 10 /* to get larger time sample */ int x[CACHE_MAX]; /* stride thru this array */ double get_seconds () { /* routine to read time */ struct tms rusage; times(&rusage); /* time in clock ticks */ return (double) (rusage.tms_utime)/sysconf(_SC_CLK_TCK); } int main () { int register i, index, stride, limit, temp; long steps, tsteps; int csize; double sec0, sec; /* timing variables */ for (csize=CACHE_MIN; csize <= CACHE_MAX; csize=csize*2) { for (stride=1; stride <= csize/2; stride=stride*2) { sec = 0; /* init timer */ limit = csize - stride + 1; /* cache size this loop */ steps = 0; do { /* repeat until collected 1 sec */ sec0 = get_seconds (); /* start timer */ for (i=SAMPLE*stride; i != 0; i=i-1) { /* larger sample */ for (index=0; index < limit; index=index+stride) { x[index] = x[index] + 1; } /* cache access */ } steps = steps + 1; /* count while loop iterations */ sec = sec + (get_seconds() - sec0); /* end timer */ } while (sec < 1.0); /* repeat empty loop to subtract loop overhead */ tsteps = 0; /* used to match # while iterations */ do { /* repeat until same # iterations as above */ sec0 = get_seconds (); /* start timer */ for (i=SAMPLE*stride; i != 0; i=i-1) { /* larger sample */ for (index=0; index < limit; index=index+stride) { temp = temp + index; } /* dummy code */ } tsteps = tsteps + 1; /* count while loop iterations */ sec = sec - (get_seconds() - sec0); /* end timer */ } while (tsteps < steps); printf ("Size:%7d Stride:%7d read+write:%10.3f ns, sec = %6.3f, steps = %6.0f\n", csize*sizeof(int), stride*sizeof(int), (double) sec*1e9/(steps*SAMPLE* stride*((limit-1)/stride+1)), sec, (double) steps); fflush(stdout); } printf ("\n\n"); } return 0; }