#include #include #include #include #include int main (int argc, char *argv[]) { if(argc!=2) { printf("Error: ./tlb \n"); return -1; } int NUMPAGES = atoi(argv[1]); //number of pages to load int PAGESIZE=sysconf(_SC_PAGESIZE); //page size in the system /* We keep the number of iterations constant so that the program does not run for too long, but it is long enough for the time measures to be relevant. */ long unsigned NUM_ITER = (unsigned)(1024*1024*256)/NUMPAGES; long unsigned i,j; // iterators long unsigned msz; // matrix size clock_t a,b; //time measurements double avg=0.0; // time calc //Jump size to access different pages in each load int jump = PAGESIZE/sizeof(int); msz=NUMPAGES*jump; int *array; array = malloc(msz*sizeof(int)); if (NUMPAGES < 0) { printf("Error, the number of pages should be positive\n"); return -1; } srand((int)clock()); int n=0; //Measure 4 times to improve the results quality (although not enough) for(n=0; n<4; n++) { a=clock(); /* The process is executed several times to reduce the error of the clock measurement (it would lack precision otherwise). First lap will always miss in the TLB. If the number of pages is low, we may have some hits in the TLB in the later laps. */ for(i=0; i