"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  

libcheetah.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 /* Processes command line arguments and calls routines as appropriate. */
00016 
00017 #include <stdio.h>
00018 #include <stdlib.h>
00019 
00020 #include "../host.h"
00021 #include "../misc.h"
00022 #include "../machine.h"
00023 #include "util.h"
00024 #include "libcheetah.h"
00025 
00026 #define NOT !
00027 #define ONE 1U
00028 #define MAX_PHYSICAL_MEM 8388608
00029 
00030 #define LRU 0
00031 #define OPT 1
00032 #define SA 2
00033 #define FA 3
00034 #define DM 4
00035 #define BASIC 5
00036 #define PIXIE 6
00037 #define DIN 7
00038 
00039 #define BYTES_PER_LINE 35  /* Storage requirement per line */
00040   
00041 int N;          /* Degree of associativity = 2^N */
00042 int B;          /* Max set field width */
00043 int A;          /* Min set field width */
00044 int L;          /* Line field width */
00045 int C;          /* Cache size for variable line size Dm simulation */
00046 int T;          /* Max no of addresses to be processed */
00047 int P_INTERVAL; /* Intervals at which progress output is done */
00048 
00049 unsigned MAX_CACHE_SIZE;        /* Maximum cache size of interest */
00050 unsigned MAX_LINES;     /* Calculated from max cache size and line size */
00051 int MISS_RATIO_INTERVAL;        /* Output interval; fully-associative cache */
00052 int SAVE_INTERVAL;     /* Intervals at which output is saved */
00053 
00054 
00055 char *outstrings[] = {  "LRU",
00056                         "OPT",
00057                         "Set-associative",
00058                         "Fully-associative",
00059                         "Direct-mapped",
00060                         "Basic",
00061                         "Pixie",
00062                         "DIN",
00063                         "an instruction",
00064                         "a load",
00065                         " ",
00066                         "a store",
00067                         " ",
00068                         "a data",
00069                         "a unified"
00070                      };
00071 
00072 
00073 /**************************************************************
00074 Main routine. Reads in command line parameters and set variable values.
00075 
00076 Input: None
00077 Output: None
00078 Side effects: Allocates space for arrays and sets parameter values.
00079 **************************************************************/
00080 
00081 /* cache configuration */
00082 int trace_config;
00083 
00084 /* replacement strategy */
00085 short repl;
00086 
00087 /* non-zero after cheetah_init() has been called */
00088 static int initialized = 0;
00089 
00090 /* initialize libcheetah */
00091 void            
00092 cheetah_init(int argc, char **argv)     /* cheetah arguments */
00093 {
00094   short i;
00095 
00096   if (initialized)
00097     {
00098       fprintf(stderr, "libcheetah: already initialized\n");
00099       exit(1);
00100     }
00101   initialized = 1;
00102 
00103   /* Default Settings */
00104   A = 7;
00105   B = 14;
00106   L = 4;
00107   N = 1;
00108   C = 16;
00109   T = 0x7fffffff;
00110   MAX_CACHE_SIZE = 524288;
00111   MISS_RATIO_INTERVAL = 512;
00112   P_INTERVAL = 0x7fffffff;
00113   SAVE_INTERVAL = 0x7fffffff;
00114   trace_config = SA;
00115   repl = LRU;
00116 
00117   /* Command line settings */
00118   for (i=0; i < argc; i++)
00119     {
00120       if (argv[i][0] != '-')
00121         {
00122           fprintf(stderr, "libcheetah: illegal argument `%s'\n", argv[i]);
00123           exit(1);
00124         }
00125       else
00126         {
00127           switch(argv[i][1])
00128             {
00129             /* -R<repl> replacement policy */
00130             case 'R':
00131               if ((NOT strcmp ("lru", &argv[i][2]))
00132                   || (NOT strcmp ("LRU", &argv[i][2])))
00133                 repl = LRU;
00134               else if ((NOT strcmp ("opt", &argv[i][2]))
00135                        || (NOT strcmp ("OPT", &argv[i][2])))
00136                 {
00137                   repl = OPT;
00138                 }
00139               else
00140                 {
00141                   fprintf (stderr,
00142                            "libcheetah: replacement policy `%s' "
00143                            "not supported\n",
00144                            &argv[i][2]);
00145                   exit (1);
00146                 }
00147               break;
00148 
00149             /* -C<config> cache configuration */
00150             case 'C':
00151               if ((NOT strcmp ("fa", &argv[i][2]))
00152                   || (NOT strcmp ("FA", &argv[i][2])))
00153                 trace_config = FA;
00154               else if ((NOT strcmp ("sa", &argv[i][2]))
00155                        || (NOT strcmp ("SA", &argv[i][2])))
00156                 trace_config = SA;
00157               else if ((NOT strcmp ("dm", &argv[i][2]))
00158                        || (NOT strcmp ("DM", &argv[i][2])))
00159                 trace_config = DM;
00160               else
00161                 {
00162                   fprintf (stderr,
00163                            "libcheetah: configuration `%s' not supported\n",
00164                            &argv[i][2]);
00165                   exit (1);
00166                 }
00167               break;
00168 
00169             /* -a<num> minimum number of sets */
00170             case 'a':
00171               A = atoi(&argv[i][2]);
00172               break;
00173 
00174             /* -b<num> maximum number of sets */
00175             case 'b':
00176               B = atoi(&argv[i][2]);
00177               break;
00178 
00179             /* -l<num> log of the line size of the caches */
00180             case 'l':
00181               L = atoi(&argv[i][2]);
00182               break;
00183 
00184             /* -n<num> log of the maximum degree of associativity */
00185             case 'n':
00186               N = atoi(&argv[i][2]);
00187               break;
00188 
00189             /* -i<num> cache size intervals at which miss ratio is desired */
00190             case 'i':
00191               MISS_RATIO_INTERVAL = atoi(&argv[i][2]);
00192               break;
00193 
00194             /* -M<num> maximum cache size of interest */
00195             case 'M':
00196               MAX_CACHE_SIZE = atoi(&argv[i][2]);
00197               break;
00198 
00199             case 'c':
00200               C = atoi(&argv[i][2]);
00201               break;
00202 
00203 #if 0 /* unneeded */
00204             case 't':
00205               T = atoi(&argv[i][2]);
00206               break;
00207 #endif
00208 
00209 #if 0 /* unneeded */
00210             case 's':
00211               SAVE_INTERVAL = atoi(&argv[i][2]);
00212               break;
00213 #endif
00214 
00215 #if 0 /* unneeded */
00216             case 'p':
00217               P_INTERVAL = atoi(&argv[i][2]);
00218               break;
00219 #endif
00220 
00221             default:
00222               fprintf(stderr, "libcheetah: `-%c' is not a valid option\n",
00223                       argv[i][1]);
00224             }
00225         }
00226     }
00227 
00228   /* initialize modules */
00229   switch (trace_config)
00230     {
00231     case SA:
00232       if (A > B)
00233         {
00234           fprintf(stderr, "libcheetah: min number of sets greater than max\n");
00235           exit (1);
00236         }
00237     
00238       if (repl == LRU)
00239         init_saclru();
00240       else if (repl == OPT)
00241         {
00242           init_sacopt();
00243           init_optpp();
00244         }
00245       break;
00246     
00247     case FA:
00248       if ((int)(ONE << L) > MISS_RATIO_INTERVAL)
00249         {
00250           fprintf(stderr, "libcheetah: line size > output interval!!\n");
00251           fprintf(stderr,
00252                   "libcheetah: output interval changed to line size\n");
00253           MISS_RATIO_INTERVAL = 1;
00254         }
00255       else
00256         MISS_RATIO_INTERVAL = MISS_RATIO_INTERVAL/(ONE << L);
00257     
00258       if (MAX_CACHE_SIZE < (ONE << L))
00259         {
00260           fprintf(stderr,
00261                   "libcheetah: max cache size is less than line size\n");
00262           exit (1);
00263         }
00264       else
00265         {
00266           MAX_LINES = MAX_CACHE_SIZE / (ONE << L);
00267           if ((MAX_LINES*BYTES_PER_LINE) > MAX_PHYSICAL_MEM)
00268             fprintf(stderr,
00269                     "libcheetah: warning: mem limit may be exceeded\n");
00270         }
00271     
00272       if (repl == LRU)
00273         init_faclru();
00274       else if (repl == OPT)
00275         {
00276           init_facopt();
00277           init_optpp();
00278         }
00279       break;
00280 
00281     case DM:
00282       if (A > B)
00283         {
00284           fprintf (stderr,
00285                    "libcheetah: min line size greater than max line\n");
00286           exit (1);
00287         }
00288       if (B > C)
00289         {
00290           fprintf (stderr,
00291                    "libcheetah: max line size greater than cache size\n");
00292           exit (1);
00293         }
00294     
00295       init_dmvl();
00296       break;
00297     
00298     default:
00299       fprintf (stderr, "libcheetah: configuration wrongly set\n");
00300       exit (1);
00301     }
00302 }
00303 
00304 /* output the analysis configuration */
00305 void
00306 cheetah_config(FILE *fd)                /* output stream */
00307 {
00308   fprintf(fd, "\nlibcheetah: ** simulation parameters **\n");
00309   if (trace_config != DM)
00310     fprintf(fd, "libcheetah: %s %s caches being simulated\n",
00311             outstrings[repl], outstrings[trace_config]);
00312   else
00313     fprintf(fd, "libcheetah: %s caches being simulated\n",
00314             outstrings[trace_config]);
00315 
00316   switch (trace_config)
00317     {
00318     case SA:
00319       fprintf(fd, "libcheetah: number of sets from %d to %d\n",
00320               (ONE << A), (ONE << B));
00321       fprintf(fd, "libcheetah: maximum associativity is %d\n", (ONE << N));
00322       fprintf(fd, "libcheetah: line size is %d bytes\n", (ONE << L));
00323       break;
00324     
00325     case FA:
00326       fprintf(fd, "libcheetah: max cache size is %d bytes\n", MAX_CACHE_SIZE);
00327       fprintf(fd, "libcheetah: line size is %d bytes\n", (ONE << L));
00328       break;
00329 
00330     case DM:
00331       fprintf(fd, "libcheetah: cache size is %d bytes\n", (ONE << C));
00332       fprintf(fd, "libcheetah: line sizes from %d to %d bytes\n",
00333               (ONE << A), (ONE << B));
00334       break;
00335 
00336     default:
00337       fprintf(stderr, "libcheetah: configuration wrongly set.\n");
00338       exit (1);
00339     }
00340 }
00341 
00342 /* pass a memory address to libcheetah */
00343 void
00344 cheetah_access(md_addr_t addr)          /* address of access */
00345 {
00346   switch (trace_config)
00347     {
00348     case SA:
00349       if (repl == LRU)
00350         sacnmul_woarr(addr);
00351       else if (repl == OPT)
00352         optpp(addr, L, stack_proc_sa, inf_handler_sa);
00353       break;
00354 
00355     case FA:
00356       if (repl == LRU)
00357         ptc(addr);
00358       else if (repl == OPT)
00359         optpp(addr, L, stack_proc_fa, inf_handler_fa);
00360       break;
00361 
00362     case DM:
00363       dmvl(addr);
00364       break;
00365     
00366     default:
00367       fprintf (stderr, "libcheetah: configuration wrongly set\n");
00368       exit (1);
00369     }
00370 }
00371 
00372 /* output the trace statistics */
00373 void
00374 cheetah_stats(FILE *fd,                 /* output stream */
00375               int mid)                  /* intermediate stats? */
00376 {
00377   fprintf(fd, "\nlibcheetah: ** end of simulation **\n");
00378 
00379   switch (trace_config)
00380     {
00381     case SA:
00382       if (repl == LRU)
00383         outpr_saclru(fd);
00384       else if (repl == OPT)
00385         {
00386           if (!mid) term_optpp(stack_proc_sa);
00387           outpr_sacopt(fd);
00388         }
00389       break;
00390 
00391     case FA:
00392       if (repl == LRU)
00393         outpr_faclru(fd);
00394       else if (repl == OPT)
00395         {
00396           if (!mid) term_optpp(stack_proc_fa);
00397           outpr_facopt(fd);
00398         }
00399       break;
00400 
00401     case DM:
00402       outpr_dmvl(fd);
00403       break;
00404 
00405     default:
00406       fprintf(stderr, "libcheetah: configuration wrongly set\n");
00407       exit(1);
00408     }
00409 }


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