"I am a person who works hard and plays hard."

Yuan Wei
Second Year Graduate Student Department of Computer Science
University of Virginia Charlottesville, VA 22903
Email: yw3f@cs.virginia.edu


Source Code Analysis

Main Page   Compound List   File List   Compound Members   File Members  

dmvl.c

Go to the documentation of this file.
00001 /************************************************************************
00002 * Copyright (C) 1989, 1990, 1991, 1992, 1993                            *
00003 *                   Rabin A. Sugumar and Santosh G. Abraham             *
00004 *                                                                       *
00005 * This software is distributed absolutely without warranty. You are     *
00006 * free to use and modify the software as you wish.  You are also free   *
00007 * to distribute the software as long as it is not for commercial gain,  *
00008 * you retain the above copyright notice, and you make clear what your   *
00009 * modifications were.                                                   *
00010 *                                                                       *
00011 * Send comments and bug reports to rabin@eecs.umich.edu                 *
00012 *                                                                       *
00013 ************************************************************************/
00014 
00015 /* Algorithm for simulating direct mapped caches of a range of line     *
00016  * sizes, all caches being of the same size (same tag length)           */
00017 
00018 
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 
00022 #include "../host.h"
00023 #include "../misc.h"
00024 #include "../machine.h"
00025 #include "util.h"
00026 #include "libcheetah.h"
00027 
00028 #define ONE 1
00029 #define INVALID 0
00030 #define SUB_ZERO 0xffffffff       /* substitute for zero tag */
00031 
00032 extern int A,
00033            B,
00034            C,
00035            T,           /* No. of entries processed (may be set by user) */
00036            SAVE_INTERVAL,       /* Interval at which results are saved */
00037            P_INTERVAL;  /* Intervals at which progress output is done */
00038 
00039 static unsigned CACHE_SZ,       /* Size of the caches */
00040                 MIN_LINE_SZ,    /* Min line size (field width) (may be set by user) */
00041                 MAX_LINE_SZ,    /* Maximum line size (fw) (may be set by user) */
00042 
00043                 NO_LINES,       /* No. of line sizes processed */
00044                 MAX_SET_SZ,     /* Maximum set size (fw) */
00045                 MIN_SET_SZ,     /* Minimum set size (fw) */
00046 
00047                 TAG_MASK,       /* Masks */
00048                 TAG_SET1_MASK,
00049 
00050 
00051                 **tag_arr,      /* Tag store array */
00052                 **set_arr,      /* Set field store array */
00053                 **hits_arr,     /* Two-dimensional hit count array */
00054                 *lm_arr,        /* Left-match array */
00055                 *hits;          /* Used in output processing */
00056 
00057 
00058 static int t_entries;           /* Count of no. of entries seen */
00059 
00060 #ifdef PERF
00061 int compares, skips;
00062 #endif
00063 
00064 /********************************************************************
00065 Simulation routine which does most of the work. Reads in trace addresses
00066 and does the binomial tree lookup and update for each.
00067 
00068 Input: None
00069 Output: None
00070 Side effects: Changes the binomial tree arrays to reflect current state
00071               of caches. Also updates the hit array.
00072 ********************************************************************/
00073 
00074 static unsigned **rtag_arr, **rset_arr, **rhits_arr;
00075 static unsigned RTAG_MASK, RMAX_LINE_SZ;
00076 static unsigned RCACHE_SZ;
00077 static unsigned RTAG_SET1_MASK;
00078 static unsigned RMIN_LINE_SZ;
00079 static unsigned RNO_LINES;
00080 static int next_save_time;
00081 
00082 
00083 /****************************************************************
00084 Output routine
00085 
00086 Input: None
00087 Output: None
00088 Side effects: None
00089 ****************************************************************/
00090 
00091 void
00092 outpr_dmvl(FILE *fd)
00093 {
00094   unsigned i, j, k;
00095 
00096   fprintf(fd, "Direct Mapped Caches\n");
00097   fprintf(fd, "Addresses processed %d\n", t_entries);
00098   fprintf(fd, "Cache size: %d bytes\n", (ONE << CACHE_SZ));
00099 #ifdef PERF
00100   fprintf(fd, "compares %d\n", compares);
00101 #endif
00102   fprintf(fd, "Line size (bytes)\tMiss ratio\n");
00103   for (i= 0; i <= NO_LINES-1; i++)
00104     {
00105       hits[i+MIN_LINE_SZ] = 0;
00106       for (j= 0; j <= (NO_LINES- i - 1); j++)
00107         {
00108           for (k=(NO_LINES-i-1); k <= (NO_LINES-1); k++)
00109             hits[i+MIN_LINE_SZ] += hits_arr[j][k];
00110         }
00111       fprintf(fd, "%d\t\t\t%f\n", (ONE << (i+MIN_LINE_SZ)), 
00112               (1.0 - ((double) hits[i+MIN_LINE_SZ]/(double) t_entries)));
00113     }
00114   fprintf(fd, "\n");
00115 }
00116 
00117 
00118 void
00119 dmvl(md_addr_t addr)
00120 {
00121   unsigned orig_tag,            /* Tag field of address */
00122         tag;            /* Tag removed from prev level */
00123         
00124   /* addr = tt111112222llll . t-tag 1-set;fld 1;2-set fld 2;l-ln fld */
00125   unsigned set1,                /* Set field 1 of addr. For selecting BT */
00126         orig_set2,              /* Set field 2 of addr. Stored in BT */
00127         set2;           /* Set field 2 removed from prev level */
00128   unsigned st, tg;              /* Temps used ot reduce array refns */
00129   int entry,                    /* Scratch */
00130         pos;                    /* Array posn. being examined */
00131   int depth,                    /* Level being examined */
00132         set2_match;             /* Scratch */
00133   unsigned *rtag_arr_set1, *rset_arr_set1;
00134 
00135   t_entries++;
00136   if ((t_entries % P_INTERVAL) == 0)
00137     fprintf(stderr, "libcheetah: addresses processed  %d\n", t_entries);
00138 
00139   set1 = (addr & RTAG_MASK) >> RMAX_LINE_SZ;
00140   orig_tag = addr >> RCACHE_SZ;
00141   orig_set2 = (addr & RTAG_SET1_MASK) >> RMIN_LINE_SZ;
00142 #ifdef PERF
00143   ++compares;
00144 #endif
00145   if (rset_arr[set1][0] == orig_set2)
00146     {
00147       if (rtag_arr[set1][0] == orig_tag)
00148         {
00149           /* Level 0 hit */
00150           ++rhits_arr[0][RNO_LINES-1];
00151         } 
00152       else
00153         rtag_arr[set1][0] = orig_tag;
00154     }
00155   else
00156     {
00157       rtag_arr_set1 = *(rtag_arr + set1);
00158       rset_arr_set1 = *(rset_arr + set1);
00159       tg = rtag_arr_set1[0];
00160       st = rset_arr_set1[0];
00161       depth = 0;
00162       set2_match = lm_arr[(st ^ orig_set2)];
00163       if (tg == orig_tag)
00164         ++rhits_arr[0][set2_match];
00165       depth = set2_match + 1;
00166       tag = tg;
00167       rtag_arr_set1[0] = orig_tag;
00168       set2 = st;
00169       rset_arr_set1[0] = orig_set2;
00170       entry = set2 >> (RNO_LINES - depth );
00171       pos = entry + (ONE << (depth - 1));
00172 
00173       while ((unsigned)depth < RNO_LINES)
00174         {
00175 #ifdef PERF
00176           ++compares;
00177 #endif
00178           tg = rtag_arr_set1[pos];
00179           st = rset_arr_set1[pos];
00180           if (st == orig_set2)
00181             {
00182               if (tg == orig_tag)
00183                 { 
00184                   /* Hit at level 1 or greater */
00185                   ++rhits_arr[depth][RNO_LINES-1];
00186                 }
00187               rtag_arr_set1[pos] = tag;
00188               rset_arr_set1[pos] = set2;
00189               break;
00190             }
00191           set2_match = lm_arr[(st ^ orig_set2)];
00192           if (tg == orig_tag)
00193             ++rhits_arr[depth][set2_match];
00194           depth = set2_match + 1;
00195           rtag_arr_set1[pos] = tag;
00196           tag = tg;
00197           rset_arr_set1[pos] = set2;
00198           set2 = st;
00199           entry = set2 >> (RNO_LINES - depth );
00200           pos = entry + (ONE << (depth - 1));
00201         }
00202     }
00203   if (t_entries > next_save_time)
00204     {
00205       outpr_dmvl(stderr);
00206       next_save_time += SAVE_INTERVAL;
00207     }
00208 }
00209 
00210 
00211 /*********************************************************************
00212 Initialization routine.
00213 
00214 Input: None
00215 Output: None
00216 Side effects: Allocates space for the various arrays and initializes them.
00217 *********************************************************************/
00218 void
00219 init_dmvl(void)
00220 {
00221   int i;
00222   unsigned depth, k;
00223   unsigned st, t;
00224 
00225   MIN_LINE_SZ = A;
00226   MAX_LINE_SZ = B;
00227   CACHE_SZ = C;
00228 
00229   NO_LINES = MAX_LINE_SZ - MIN_LINE_SZ + 1;
00230   MAX_SET_SZ =  (CACHE_SZ - MIN_LINE_SZ);
00231   MIN_SET_SZ =  (CACHE_SZ - MAX_LINE_SZ);
00232 
00233   TAG_MASK = (( ONE << CACHE_SZ) - ONE );
00234   TAG_SET1_MASK = ((ONE << MAX_LINE_SZ) - ONE);
00235 
00236 
00237   tag_arr = idim2((ONE << MIN_SET_SZ), (ONE << (NO_LINES-1)));
00238   set_arr = idim2((ONE << MIN_SET_SZ), (ONE << (NO_LINES-1)));
00239   hits_arr = idim2((NO_LINES), (NO_LINES));
00240 
00241   lm_arr = calloc((ONE << (NO_LINES - 1)), sizeof(int));
00242   if (!lm_arr)
00243     fatal("out of virtual memory");
00244 
00245   hits = calloc((MAX_LINE_SZ+1), sizeof(int));
00246   if (!hits)
00247     fatal("out of virtual memory");
00248   
00249   for (i=0; i < (ONE<<MIN_SET_SZ); i++)
00250     {
00251       set_arr[i][0] = 0;
00252       tag_arr[i][0] = SUB_ZERO;
00253       for (depth= 1; depth < NO_LINES; depth++)
00254         {
00255           st = ONE << (depth-1);
00256           t = ONE << (NO_LINES - depth);
00257           set_arr[i][st] = ONE << (NO_LINES-depth-1);
00258           tag_arr[i][st] = SUB_ZERO;
00259           for (k=1; k<st; k++)
00260             {
00261               set_arr[i][st+k] = set_arr[i][st+k-1] + t;
00262               tag_arr[i][st+k] = SUB_ZERO;
00263             }
00264         }
00265     }
00266 
00267   t = ONE;
00268   st = NO_LINES - 1;
00269   for (i=0; i < (ONE << (NO_LINES - 1)); i++)
00270     {
00271       if (t > (unsigned)i)
00272         lm_arr[i] = st;
00273       else
00274         {
00275           t <<= ONE;
00276           st -= 1;
00277           lm_arr[i] = st;
00278         }
00279     }
00280 
00281   rtag_arr = tag_arr;
00282   rset_arr = set_arr;
00283   rhits_arr = hits_arr;
00284   next_save_time = SAVE_INTERVAL;
00285 
00286   RTAG_MASK=TAG_MASK;
00287   RMAX_LINE_SZ=MAX_LINE_SZ;
00288   RCACHE_SZ=CACHE_SZ;
00289   RTAG_SET1_MASK=TAG_SET1_MASK;
00290   RMIN_LINE_SZ=MIN_LINE_SZ;
00291   RNO_LINES=NO_LINES;
00292 }


UVa CS Department of Computer Science
School of Engineering, University of Virginia
151 Engineer's Way, P.O. Box 400740
Charlottesville, Virginia 22904-4740

(434) 982-2200  Fax: (434) 982-2214