/************************************************************************************ * * Micro Benchmark mbtlb * -------------------------------- * * Filename : mbtlb.c * Author : Yuanhua Yu * Date : 20/12/1999 *----------------------------------------------------------------------------- * * Purpose : This micro-benchmark is used to measure the key parameters of * the TLB such as size, associativity using a sequential access * sequence of pages without repeatition. * * Methods : 1. determined_indexing: * the offset inside the accessed pages is sequential from 0. * the offset is computed by: * i*stride_block+mod(subblock_size*i, stride_block)] * where mod(x,y) = x - y*int(x/y) * * 2. random_offset: * the offset inside the accessed pages is a random number between * 0 and stride_block. * the offset is determined by: * (long)stride_block*((double)rand()/RAND_MAX)) * * 3. random_index but determinated offset * * 4. random_index and offset * * History :First version date to 10/08/1999 * Second version date to 12/10/1999 * Third version date to 10/12/1999 * * Parameters:arr_size : size of the accessed array * num_pages : number of the pages in the accessed array * page_size : size of the virtual memory page (assummed) * stride_block : size of the block in which only one item is accessed * stride is in terms of pages * * Functions : * * 1. determined_indexing(): build up a accessed addresses sequence of pages * with sequential offset in the accessed pages. * * 2. random_offset(): build up a accessed addresses sequence of pages * with random offset in the accessed block. * * 3. random_indexing(): build up a random accessing addresses sequence of pages * with sequential offset in the accessed pages. * * 4. random_index_offset(): build up a random accessing addresses sequence of pages * with random offset in the accessed block. * * 5. set_parameters(): config parameters and store them in TLB_Config.dat * * 6. get_parameters(): get parameters from TLB_Config.dat * * 7. write_file_head(): write some given data , test date and time into * the given file. * * 8. random_sequence(): build up a random sequence * ***************************************************************************************/ #include #include #include #include #include #include #define PAGESIZE_START 1024 // bytes #define PAGESIZE_END (1024*16) // bytes #define SUBBLOCK_SIZE 32 // bytes #define ARR_SIZE (1024*16834*2) // bytes #define TOTAL_ACCESSED (4096*4096*8) // items in the accessed array /*-------------------------------------------------------------------------------*/ long mod(long x, long y); void write_file_head(FILE *fp,char mname[15],long min_page,long max_page, long num_pages,long step,long subblock_size, int option); void test(FILE *fp,long *array,long num_pages,long step,long stride_block); void determined_index(long *array,long num_pages,long stride_block,long subblock_size); void random_offset(long *array,long num_pages,long stride_block); void random_index(long *array,long arr_size,long num_pages, long stride_block,long subblock_size); void random_index_offset(long *array,long arr_size, long num_pages,long stride_block); void random_sequence(long *array, long max_index); void set_parameters(char mname[15],long *min_page,long *max_page,long *num_pages, long *step,long *subbock_size); void get_parameters(char mname[15],char fname[25],long *min_page,long *max_page, long *num_pages,long *step,long *subbock_size); /*------------------------------------------------------------------------------*/ int main(char **av, int ac) { long *array, arr_size, page_size, stride_block; long min_page=1024, max_page=4096, num_pages=128, step=2, subblock_size=32; int option; char file_name[25], machine_name[15]; FILE *fp; arr_size = ARR_SIZE/(sizeof(long)); /* number of elements in array */ array = (long *)calloc(arr_size, sizeof(long)); option = 1; while(option >=0 && option <6) { printf("\n---------- Measuring Parameters of TLB ------------------\n\n"); printf(" 0: All of the test\n"); printf(" 1: Sequential indexing with incremented offset\n"); printf(" 2: Sequential indexing with random offset\n"); printf(" 3: Random indexing with incremented offset\n"); printf(" 4: Random indexing with random offset\n"); printf(" 5: Set parameters\n"); printf(" 6: Exit\n"); printf("\n---------------------------------------------------------\n"); printf(" Option (0-6): "); scanf("%d",&option); if(option ==5) { set_parameters(machine_name,&min_page,&max_page, &num_pages,&step, &subblock_size); continue; } get_parameters(machine_name,file_name, &min_page,&max_page, &num_pages, &step, &subblock_size); fp = fopen(file_name,"a+"); if(fp == NULL) { perror("Error: Can't open file !"); exit(1); } subblock_size = subblock_size / sizeof(long); if (option ==1 || option == 0) { write_file_head(fp,machine_name, min_page,max_page, num_pages, step, subblock_size*sizeof(long), 1); for(page_size=min_page; page_size<=max_page; page_size*=2) { stride_block = page_size/sizeof(long); determined_index(array, num_pages, stride_block, subblock_size); test(fp, array, num_pages, step, stride_block); } } if (option ==2 || option == 0) { write_file_head(fp,machine_name, min_page,max_page, num_pages, step, subblock_size*sizeof(long), 2); for(page_size=min_page; page_size<=max_page; page_size*=2) { stride_block = page_size/sizeof(long); random_offset(array, num_pages, stride_block); test(fp, array, num_pages, step, stride_block); } } if (option ==3 || option == 0) { write_file_head(fp,machine_name, min_page,max_page, num_pages, step, subblock_size*sizeof(long), 3); for(page_size=min_page; page_size<=max_page; page_size*=2) { stride_block = page_size/sizeof(long); random_index(array, arr_size, num_pages,stride_block,subblock_size); test(fp, array, num_pages, step, stride_block); } } if (option ==4 || option == 0) { write_file_head(fp,machine_name, min_page,max_page, num_pages, step, subblock_size*sizeof(long), 4); for(page_size=min_page; page_size<=max_page; page_size*=2) { stride_block = page_size/sizeof(long); random_index_offset(array, arr_size, num_pages, stride_block); test(fp, array, num_pages, step, stride_block); } } } free(array); fclose(fp); return 1; } long mod(long x, long y) { return (x - y*(int)(x/y)); } void test(FILE *fp,long *array, long num_pages, long step, long stride_block) /*---------------------------------------------------------------------------*/ /* Function: measuring the TLB using the given accessing sequence */ /*---------------------------------------------------------------------------*/ { long i, j, k; double time_cost_total, time_cost_per_op; unsigned long start_time, stop_time; printf("Pages size(bytes) = : %d\n",stride_block*sizeof(long)); fprintf(fp,"Assummed pages size(bytes)= : %d\n",stride_block*sizeof(long)); /*--------------------------------------------------------Test Start---*/ for (k = num_pages; k>0; k = k-step) { i=array[0]; for(j=1; j< k-1; j++) /* Build the accessed-page circle */ i=array[i]; array[i]=0; start_time=clock(); /* -------------------Start the clock tick */ i=array[0]; for (j=0;j0 ) { continue; } else { array[index] = tmp; index = tmp; break; } } } return; } void random_index_offset(long *array, long arr_size, long num_pages, long stride_block) /*-----------------------------------------------------------------------------------*/ /* Function: * build up a randm accessing addresses sequence of pages */ /* with random offset in the accessed block. */ /* * subblock_size is not needed in this case. */ /*-----------------------------------------------------------------------------------*/ { long i, index, block_no, offset, tmp, *flag; FILE *fp; fp = fopen("sss","w"); flag = (long *) calloc(num_pages, sizeof(long)); for(i=0; i 0 ) { continue; } else { fprintf(fp,"%d\n", tmp); array[index] = tmp; flag[block_no] = 1; index = tmp; break; } } } fclose(fp); return; } void random_sequence(long *array, long max_index) /*-----------------------------------------------------------------------------*/ /* Function: * build up a random accessing addresses sequence */ /*-----------------------------------------------------------------------------*/ { long i, index, block_no; FILE *fp; fp = fopen("sss","w"); for(i=0; i0 || block_no >= max_index) { continue; } else { fprintf(fp,"%d\n", block_no); array[index] = block_no; index = block_no; break; } } } fclose(fp); return; }