"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  

sacopt.c File Reference

#include <stdio.h>
#include <stdlib.h>
#include "../host.h"
#include "../misc.h"
#include "../machine.h"
#include "util.h"
#include "libcheetah.h"

Include dependency graph for sacopt.c:

Include dependency graph

Go to the source code of this file.

Defines

Functions

Variables


Define Documentation

#define B80000000   0x80000000
 

Definition at line 30 of file sacopt.c.

#define HASHNO   7211
 

Definition at line 34 of file sacopt.c.

#define INVALID   0
 

Definition at line 31 of file sacopt.c.

#define MAXINT   2147483647
 

Definition at line 33 of file sacopt.c.

Referenced by gfsoptls(), inf_handler_sa(), init_sacopt(), and Priority_sa().

#define MEM_AVAIL_HITARR   2097152
 

Definition at line 32 of file sacopt.c.

#define ONE   1
 

Definition at line 28 of file sacopt.c.

Referenced by gfsoptls(), hash_clean_sa(), inf_handler_sa(), init_sacopt(), outpr_sacopt(), unk_hash_add_sa(), and unk_hash_del_sa().

#define TWO   2U
 

Definition at line 29 of file sacopt.c.

Referenced by gfsoptls(), inf_handler_sa(), and init_sacopt().

#define WORD_SIZE   1
 

Definition at line 54 of file sacopt.c.

Referenced by init_sacopt().


Function Documentation

void gfsoptls md_addr_t    addr,
int    priority
 

Definition at line 331 of file sacopt.c.

References A, all_hit_flag, B, BASE_CACHE_SIZE, ft_hash_del(), hash_clean_sa(), hit0, hits, MAXINT, ONE, prty_arr, SET_SIZE, tag_arr, TWO, and TWO_PWR_N.

Referenced by stack_proc_sa().

00332 {
00333   unsigned setno, base_setno, orig_tag,
00334         rem_tag, de_tag, prev_addr;
00335   int rem_prty, de_prty, real_prty, max_prty;
00336   unsigned cache_size, cache_ptr, set_ptr;
00337   int i, j;
00338   short hit;
00339 
00340   orig_tag = addr >> A;
00341   base_setno = addr & ((ONE << A) - 1);
00342   if (all_hit_flag[base_setno])
00343     {
00344       if (tag_arr[base_setno * SET_SIZE] == orig_tag)
00345         {
00346           ++ hit0;
00347           prty_arr[base_setno * SET_SIZE] = priority;
00348           return;
00349         }
00350       else
00351         {
00352           real_prty = prty_arr[base_setno * SET_SIZE];
00353           prev_addr = (tag_arr[base_setno * SET_SIZE] << A) | base_setno;
00354           cache_ptr = BASE_CACHE_SIZE;
00355           for (i=A+1; i<=B; i++)
00356             {
00357               setno = prev_addr & ((ONE << i) - 1);
00358               set_ptr = cache_ptr + (setno * SET_SIZE);
00359               prty_arr[set_ptr] = real_prty;
00360               cache_ptr = TWO*cache_ptr + BASE_CACHE_SIZE;
00361             }
00362         }
00363     }
00364   all_hit_flag[base_setno] = 1;
00365   i = A;
00366   cache_size = BASE_CACHE_SIZE;
00367   cache_ptr = 0;
00368   max_prty = (5 - MAXINT);
00369   while (i <= B)
00370     {
00371       de_tag = orig_tag;
00372       de_prty = priority;
00373       setno = addr & ((ONE << i) - 1);
00374       set_ptr = cache_ptr + (setno * SET_SIZE);
00375       hit = 0;
00376       for (j=0; j < TWO_PWR_N; j++)
00377         {
00378 #ifdef PERF
00379           ++compares;
00380 #endif
00381           if (tag_arr[set_ptr + j] == orig_tag)
00382             {
00383               hit = 1;
00384               ++hits[i-A][j];
00385               tag_arr[set_ptr + j] = de_tag;
00386               prty_arr[set_ptr + j] = de_prty;
00387               break;
00388             }
00389           else
00390             {
00391               if (de_prty > prty_arr[set_ptr + j])
00392                 {
00393                   rem_tag = tag_arr[set_ptr + j];
00394                   tag_arr[set_ptr + j] = de_tag;
00395                   de_tag = rem_tag;
00396                   rem_prty = prty_arr[set_ptr + j];
00397                   prty_arr[set_ptr + j] = de_prty;
00398                   de_prty = rem_prty;
00399                 }
00400               if (i == B)
00401                 {
00402                   if (prty_arr[set_ptr + j] < 0)
00403                     {
00404                       if (max_prty < prty_arr[set_ptr + j])
00405                         max_prty = prty_arr[set_ptr + j];
00406                     }
00407                 }
00408             }
00409         } /* for */
00410 
00411       if (j > 0)
00412         {
00413           all_hit_flag[base_setno] = 0;
00414           if (j >= TWO_PWR_N)
00415             {
00416               if ((i == B) && (de_prty < 0) && (de_prty > (5-MAXINT)))
00417                 ft_hash_del((de_tag << A) | setno);   /* reforming address */
00418             }
00419         }
00420       cache_ptr += cache_size;
00421       cache_size += cache_size;
00422       ++i;
00423     } /* while */
00424 
00425   if ((i > B) && (hit == 0))
00426     hash_clean_sa(addr, max_prty);
00427 }

void hash_clean_sa md_addr_t    addr,
int    max_prty
 

Definition at line 276 of file sacopt.c.

References hash_table::addr, B, hash_table::grptime, hash_table::nxt, ONE, hash_table::prty, slot_flag, UHT_Add_to_free_list(), and unknowns.

Referenced by gfsoptls().

00277 {
00278   int loc;
00279   struct hash_table *ptr, *oldptr;
00280 
00281   if (max_prty >= 0)
00282     fprintf(stderr, "libcheetah: max unknown priority non-negative\n");
00283 
00284   loc = addr & ((ONE << B) - 1);
00285   while ((slot_flag[loc] != 0) && (slot[loc].prty > max_prty))
00286     {
00287       --unknowns;
00288       if (slot[loc].nxt == NULL)
00289         {
00290           slot_flag[loc] = 0;
00291           continue;
00292         }
00293       else
00294         {
00295           slot[loc].addr = slot[loc].nxt->addr;
00296           slot[loc].grptime = slot[loc].nxt->grptime;
00297           slot[loc].prty = slot[loc].nxt->prty;
00298           ptr = slot[loc].nxt;
00299           slot[loc].nxt = slot[loc].nxt->nxt;
00300           UHT_Add_to_free_list(ptr);
00301         }
00302     } /* while */
00303 
00304   if (slot_flag[loc] != 0)
00305     {
00306       ptr = &slot[loc];
00307       while (ptr)
00308         {
00309           if (ptr->prty > max_prty)
00310             {
00311               --unknowns;
00312               oldptr->nxt = ptr->nxt;
00313               UHT_Add_to_free_list(ptr);
00314               ptr = oldptr;
00315             }
00316           oldptr = ptr;
00317           ptr = ptr->nxt;
00318         }
00319     }
00320 }

void inf_handler_sa md_addr_t    addr,
int    cur_time
 

Definition at line 548 of file sacopt.c.

References A, all_hit_flag, B, BASE_CACHE_SIZE, MAXINT, ONE, prty_arr, SET_SIZE, tag_arr, TWO, TWO_PWR_N, unk_hash_del_sa(), and unknowns.

00549 {
00550   unsigned setno, orig_tag, cache_size,
00551         de_tag, rem_tag, prev_addr,
00552         cache_ptr, set_ptr, base_setno;
00553   int de_prty, rem_prty,
00554         old_prty, priority,
00555         real_prty;
00556   int i, j;
00557 
00558   old_prty = unk_hash_del_sa(addr);
00559   if (old_prty == 0)
00560     return;
00561 
00562   --unknowns;
00563 
00564   priority = MAXINT - cur_time; /* Assumes a priority function */
00565 
00566   base_setno = addr & ((ONE << A) -1);
00567   if (all_hit_flag[base_setno])
00568     {
00569       all_hit_flag[base_setno] = 0;
00570       real_prty = prty_arr[base_setno * SET_SIZE];
00571       prev_addr = (tag_arr[base_setno * SET_SIZE] << A) | base_setno;
00572       cache_ptr = BASE_CACHE_SIZE;
00573       for (i=A+1; i<=B; i++)
00574         {
00575           setno = prev_addr & ((ONE << i) - 1);
00576           set_ptr = cache_ptr + (setno * SET_SIZE);
00577           prty_arr[set_ptr] = real_prty;
00578           cache_ptr = TWO*cache_ptr + BASE_CACHE_SIZE;
00579         }
00580     }
00581   orig_tag = addr >> A;
00582   i = A;
00583   cache_size = BASE_CACHE_SIZE;
00584   cache_ptr = 0;
00585   while (i <= B)
00586     {
00587       de_tag = orig_tag;
00588       de_prty = priority;
00589       setno = addr & ((ONE << i) - 1);
00590       set_ptr = cache_ptr + (setno * SET_SIZE);
00591       for (j=0; j < TWO_PWR_N; j++)
00592         {
00593 #ifdef PERF
00594           ++compares;
00595 #endif
00596           if (tag_arr[set_ptr + j] == orig_tag)
00597             {
00598               if (prty_arr[set_ptr + j] > 0)
00599                 fprintf(stderr, "libcheetah: inconsistency in inf_handler\n");
00600               else
00601                 {
00602                   tag_arr[set_ptr + j] = de_tag;
00603                   prty_arr[set_ptr + j] = de_prty;
00604                   break;
00605                 }
00606             }
00607           else if (prty_arr[set_ptr + j] < 0)
00608             {
00609               if (old_prty < prty_arr[set_ptr + j])
00610                 {
00611                   if (de_prty > prty_arr[set_ptr + j])
00612                     {
00613                       rem_tag = tag_arr[set_ptr + j];
00614                       tag_arr[set_ptr + j] = de_tag;
00615                       de_tag = rem_tag;
00616                       rem_prty = prty_arr[set_ptr + j];
00617                       prty_arr[set_ptr + j] = de_prty;
00618                       de_prty = rem_prty;
00619                     }
00620                 }
00621             }
00622         } /*for*/
00623       cache_ptr += cache_size;
00624       cache_size += cache_size;
00625       ++i;
00626     } /*while*/
00627 }

void init_sacopt void   
 

Definition at line 489 of file sacopt.c.

References A, all_hit_flag, B, BASE_CACHE_SIZE, CLEAN_INTERVAL, DIFF_SET_MASK, fatal(), hits, idim2(), MAX_DEPTH, MAXINT, N, next_save_time, ONE, prty_arr, SAVE_INTERVAL, SET_MASK, SET_SIZE, slot_flag, tag_arr, time_of_next_clean, TWO, TWO_POWER_MAX_DEPTH, TWO_PWR_N, and WORD_SIZE.

Referenced by cheetah_init().

00491 {
00492   unsigned i;
00493 
00494   TWO_PWR_N = (ONE << N);
00495   MAX_DEPTH = B-A;
00496   TWO_POWER_MAX_DEPTH = (ONE << MAX_DEPTH);
00497   SET_MASK = ((ONE << A) - 1);
00498   DIFF_SET_MASK = ((ONE << MAX_DEPTH) - 1);
00499   SET_SIZE = WORD_SIZE*TWO_PWR_N;
00500   BASE_CACHE_SIZE = (ONE<<A)*SET_SIZE;
00501 
00502   next_save_time = SAVE_INTERVAL;
00503 
00504   tag_arr = calloc((TWO * (ONE << B) * TWO_PWR_N), sizeof(unsigned));
00505   if (!tag_arr)
00506     fatal("out of virtual memory");
00507 
00508   prty_arr = calloc((TWO * (ONE << B) * TWO_PWR_N), sizeof(unsigned));
00509   if (!prty_arr)
00510     fatal("out of virtual memory");
00511 
00512   all_hit_flag = calloc((ONE << A), sizeof(short));
00513   if (!all_hit_flag)
00514     fatal("out of virtual memory");
00515 
00516   hits = idim2 ((MAX_DEPTH + 2), (TWO_PWR_N));
00517 
00518   slot = calloc((ONE << B), sizeof(struct hash_table));
00519   if (!slot)
00520     fatal("out of virtual memory");
00521 
00522   slot_flag = calloc((ONE << B), sizeof(short));
00523   if (!slot_flag)
00524     fatal("out of virtual memory");
00525 
00526   for (i=0; i < (TWO * (ONE << B) * TWO_PWR_N); i++)
00527     {
00528       tag_arr[i] = 0xffffffff;
00529       prty_arr[i] = (5-MAXINT);
00530     }
00531 
00532   time_of_next_clean = CLEAN_INTERVAL;
00533 }

void outpr_sacopt FILE *    fd
 

Definition at line 229 of file sacopt.c.

References A, B, hit0, hits, L, MAX_DEPTH, ONE, t_entries, and TWO_PWR_N.

Referenced by cheetah_stats(), and stack_proc_sa().

00230 {
00231   int i, j;
00232   int sum;
00233 
00234   for (i=0; i <= (B-A); i++)
00235     hits[i][0] += hit0;
00236   hit0 = 0;
00237 
00238   fprintf(fd, "Addresses processed: %d\n", t_entries);
00239   fprintf(fd, "Line size: %d bytes\n", (ONE << L));
00240 #ifdef PERF
00241   fprintf(fd, "compares  %d\n", compares);
00242 #endif
00243   fprintf(fd, "\n");
00244   fprintf(fd, "Miss Ratios\n");
00245   fprintf(fd, "___________\n\n");
00246   fprintf(fd, "\t\tAssociativity\n");
00247   fprintf(fd, "\t\t");
00248   for (i=0;i<TWO_PWR_N;i++)
00249     fprintf(fd, "%d\t\t", (i+1));
00250   fprintf(fd, "\n");
00251   fprintf(fd, "No. of sets\n");
00252   for (i=0; i<=MAX_DEPTH; i++)
00253     {
00254       sum = 0;
00255       fprintf(fd, "%d\t\t", (ONE << (i+A)));
00256       for (j=0; j<TWO_PWR_N; j++)
00257         {
00258           sum += hits[i][j];
00259           fprintf(fd, "%f\t", (1.0 - ((double)sum/(double)t_entries)));
00260         }
00261       fprintf(fd, "\n");
00262     }
00263   fprintf (fd, "\n\n\n");
00264 }

int Priority_sa int    i
 

Definition at line 86 of file sacopt.c.

References MAXINT, and time_array.

Referenced by stack_proc_sa().

00087 {
00088   static int inf_count=0;
00089 
00090   if (time_array[i] > 0)
00091     return (MAXINT - time_array[i]); /* inf_handler assumes this fn.
00092                                         inf_handler has to be changed
00093                                         if this is changed */
00094   else if (time_array[i] == 0)
00095     return --inf_count;
00096   else
00097     {
00098       fprintf(stderr, "libcheetah: error in priority function\n");
00099       return 0;
00100     }
00101 }

int stack_proc_sa int    start,
int    end
 

Definition at line 442 of file sacopt.c.

References addr_array, gfsoptls(), next_save_time, outpr_sacopt(), P_INTERVAL, Priority_sa(), SAVE_INTERVAL, T, t_entries, time_array, and unk_hash_add_sa().

00444 {
00445   int l;
00446   unsigned addr;
00447   int priority;
00448 
00449   for (l=start; l<end; l++)
00450     {
00451       ++t_entries;
00452 
00453       if ((t_entries % P_INTERVAL) == 0)
00454         fprintf(stderr, "libcheetah: addresses processed %d\n", t_entries);
00455       if (t_entries > next_save_time)
00456         {
00457           outpr_sacopt(stderr);
00458           next_save_time += SAVE_INTERVAL;
00459         }
00460 
00461       addr = addr_array[l];
00462 
00463       priority = Priority_sa(l);
00464       if (priority < 0)
00465         unk_hash_add_sa(addr, priority);
00466 
00467       gfsoptls(addr, priority);
00468 
00469       addr_array[l] = 0;
00470       time_array[l] = 0;
00471       if (t_entries > (unsigned)T)
00472         return 1;
00473     } /* for */
00474 
00475   return 0;
00476 }

void unk_hash_add_sa md_addr_t    addr,
int    prty
 

Definition at line 113 of file sacopt.c.

References hash_table::addr, B, fatal(), myfprintf(), hash_table::nxt, ONE, hash_table::prty, slot_flag, t_entries, UHT_Get_from_free_list(), and unknowns.

Referenced by stack_proc_sa().

00114 {
00115   int loc;
00116   struct hash_table *ptr, *oldptr;
00117 
00118   ++unknowns;
00119 
00120   loc = addr & ((ONE << B) - 1);
00121   /* loc = addr % HASHNO; */
00122   if (slot_flag[loc] == 0)
00123     {
00124       slot_flag[loc] = 1;
00125       slot[loc].addr = addr;
00126       slot[loc].prty = prty;
00127       slot[loc].nxt = NULL;
00128       return;
00129     }
00130   else
00131     {
00132       ptr = &slot[loc];
00133       while (ptr)
00134         {
00135           oldptr = ptr;
00136           if (ptr->addr == addr)
00137             {
00138               fprintf(stderr, "libcheetah: address already in hash table\n"); 
00139               myfprintf(stderr, "addr: 0x%p; t_entries: %u; prty: %d\n",
00140                         addr, t_entries, ptr->prty);
00141               myfprintf(stderr, "slot addr: 0x%p; prty: %d\n",
00142                         slot[loc].addr, slot[loc].prty);
00143             }
00144           ptr = ptr->nxt;
00145         }
00146       if ((oldptr->nxt = UHT_Get_from_free_list()) == NULL)
00147         {
00148           oldptr->nxt = calloc(1, sizeof(struct hash_table));
00149           if (!oldptr->nxt)
00150             fatal("out of virtual memory");
00151         }
00152       oldptr->nxt->addr = addr;
00153       oldptr->nxt->nxt = NULL;
00154       oldptr->nxt->prty = prty;
00155       return;
00156     }
00157 }

int unk_hash_del_sa md_addr_t    addr
 

Definition at line 169 of file sacopt.c.

References hash_table::addr, B, hash_table::nxt, ONE, hash_table::prty, slot_flag, and UHT_Add_to_free_list().

Referenced by inf_handler_sa().

00170 {
00171   int loc;
00172   struct hash_table *ptr, *oldptr;
00173   int ret_prty;
00174 
00175   loc = addr & ((ONE << B) - 1);
00176   /* loc = addr % HASHNO; */
00177   if (slot_flag[loc] == 0)
00178     return 0;
00179   else if (slot[loc].addr == addr)
00180     {
00181       if (slot[loc].nxt == NULL)
00182         {
00183           slot_flag[loc] = 0;
00184           return slot[loc].prty;
00185         }
00186       else
00187         {
00188           ret_prty = slot[loc].prty;
00189           slot[loc].addr = slot[loc].nxt->addr;
00190           slot[loc].prty = slot[loc].nxt->prty;
00191           ptr = slot[loc].nxt;
00192           slot[loc].nxt = slot[loc].nxt->nxt;
00193           UHT_Add_to_free_list(ptr);
00194           return ret_prty;
00195         }
00196     }
00197   else
00198     {
00199       ptr = &slot[loc];
00200       while (ptr)
00201         {
00202           if (ptr->addr == addr)
00203             break;
00204           oldptr = ptr;
00205           ptr = ptr->nxt;
00206         }
00207       if (ptr)
00208         {
00209           ret_prty = ptr->prty;
00210           oldptr->nxt = ptr->nxt;
00211           UHT_Add_to_free_list(ptr);
00212           return ret_prty;
00213         }
00214       else
00215         return 0;
00216     }
00217 }


Variable Documentation

int A
 

Definition at line 36 of file sacopt.c.

Referenced by gfsoptls(), inf_handler_sa(), init_sacopt(), and outpr_sacopt().

unsigned addr_array[]
 

Definition at line 72 of file sacopt.c.

Referenced by optpp(), and stack_proc_sa().

short* all_hit_flag [static]
 

Definition at line 62 of file sacopt.c.

Referenced by gfsoptls(), inf_handler_sa(), and init_sacopt().

int B
 

Definition at line 36 of file sacopt.c.

Referenced by gfsoptls(), hash_clean_sa(), inf_handler_sa(), init_sacopt(), outpr_sacopt(), unk_hash_add_sa(), and unk_hash_del_sa().

int BASE_CACHE_SIZE [static]
 

Definition at line 44 of file sacopt.c.

Referenced by gfsoptls(), inf_handler_sa(), and init_sacopt().

int CLEAN_INTERVAL [static]
 

Definition at line 44 of file sacopt.c.

Referenced by init_sacopt().

int DIFF_SET_MASK [static]
 

Definition at line 44 of file sacopt.c.

Referenced by init_sacopt().

unsigned hit0 [static]
 

Definition at line 61 of file sacopt.c.

Referenced by gfsoptls(), and outpr_sacopt().

unsigned ** hits [static]
 

Definition at line 59 of file sacopt.c.

Referenced by gfsoptls(), init_sacopt(), and outpr_sacopt().

int L
 

Definition at line 36 of file sacopt.c.

Referenced by cheetah_access(), cheetah_config(), cheetah_init(), and outpr_sacopt().

int MAX_DEPTH [static]
 

Definition at line 44 of file sacopt.c.

Referenced by init_sacopt(), and outpr_sacopt().

int N
 

Definition at line 36 of file sacopt.c.

Referenced by cheetah_config(), cheetah_init(), and init_sacopt().

unsigned next_save_time [static]
 

Definition at line 74 of file sacopt.c.

Referenced by init_sacopt(), and stack_proc_sa().

int P_INTERVAL
 

Definition at line 36 of file sacopt.c.

Referenced by cheetah_init(), and stack_proc_sa().

int* prty_arr [static]
 

Definition at line 63 of file sacopt.c.

Referenced by gfsoptls(), inf_handler_sa(), and init_sacopt().

int SAVE_INTERVAL
 

Definition at line 36 of file sacopt.c.

Referenced by cheetah_init(), init_sacopt(), and stack_proc_sa().

int SET_MASK [static]
 

Definition at line 44 of file sacopt.c.

Referenced by init_sacopt().

int SET_SIZE [static]
 

Definition at line 44 of file sacopt.c.

Referenced by gfsoptls(), inf_handler_sa(), and init_sacopt().

struct hash_table* slot [static]
 

Definition at line 56 of file sacopt.c.

short* slot_flag [static]
 

Definition at line 57 of file sacopt.c.

Referenced by hash_clean_sa(), init_sacopt(), unk_hash_add_sa(), and unk_hash_del_sa().

int T
 

Definition at line 36 of file sacopt.c.

Referenced by stack_proc_sa().

unsigned t_entries [static]
 

Definition at line 65 of file sacopt.c.

Referenced by outpr_sacopt(), stack_proc_sa(), and unk_hash_add_sa().

unsigned* tag_arr [static]
 

Definition at line 59 of file sacopt.c.

Referenced by gfsoptls(), inf_handler_sa(), and init_sacopt().

unsigned time_array[]
 

Definition at line 72 of file sacopt.c.

Referenced by optpp(), Priority_sa(), and stack_proc_sa().

int time_of_next_clean [static]
 

Definition at line 44 of file sacopt.c.

Referenced by init_sacopt().

int TWO_POWER_MAX_DEPTH [static]
 

Definition at line 44 of file sacopt.c.

Referenced by init_sacopt().

int TWO_PWR_N [static]
 

Definition at line 44 of file sacopt.c.

Referenced by gfsoptls(), inf_handler_sa(), init_sacopt(), and outpr_sacopt().

unsigned unknowns [static]
 

Definition at line 66 of file sacopt.c.

Referenced by hash_clean_sa(), inf_handler_sa(), and unk_hash_add_sa().



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