📄 sacopt.c
字号:
if ((tag_arr == NULL) || (prty_arr == NULL) || (all_hit_flag == 0)) goto error; for (i=0; i < (TWO * (ONE << B) * TWO_PWR_N); i++) { tag_arr[i] = 0xffffffff; prty_arr[i] = (5-MAXINT); } time_of_next_clean = CLEAN_INTERVAL; return;error: fprintf(stderr, "libcheetah: problem allocating space\n"); exit (1);}/*******************************************************************Rearranges the positions of unknowns in the stack, when an unknownbecomes known.Input: Address of unknown that becomes known, and the current time at pre-processor.Output: NoneSide effects: Rearranges the positions of unknowns in the stacks that addr maps to*******************************************************************/voidinf_handler_sa(unsigned long addr, int cur_time){ unsigned setno, orig_tag, cache_size, de_tag, rem_tag, prev_addr, cache_ptr, set_ptr, base_setno; int de_prty, rem_prty, old_prty, priority, real_prty; int i, j; old_prty = unk_hash_del_sa (addr); if (old_prty == 0) return; --unknowns; priority = MAXINT - cur_time; /* Assumes a priority function */ base_setno = addr & ((ONE << A) -1); if (all_hit_flag[base_setno]) { all_hit_flag[base_setno] = 0; real_prty = prty_arr [base_setno * SET_SIZE]; prev_addr = (tag_arr [base_setno * SET_SIZE] << A) | base_setno; cache_ptr = BASE_CACHE_SIZE; for (i=A+1; i<=B; i++) { setno = prev_addr & ((ONE << i) - 1); set_ptr = cache_ptr + (setno * SET_SIZE); prty_arr[set_ptr] = real_prty; cache_ptr = TWO*cache_ptr + BASE_CACHE_SIZE; } } orig_tag = addr >> A; i = A; cache_size = BASE_CACHE_SIZE; cache_ptr = 0; while (i <= B) { de_tag = orig_tag; de_prty = priority; setno = addr & ((ONE << i) - 1); set_ptr = cache_ptr + (setno * SET_SIZE); for (j=0; j < TWO_PWR_N; j++) {#ifdef PERF ++compares;#endif if (tag_arr[set_ptr + j] == orig_tag) if (prty_arr[set_ptr + j] > 0) fprintf(stderr, "libcheetah: inconsistency in inf_handler\n"); else { tag_arr[set_ptr + j] = de_tag; prty_arr[set_ptr + j] = de_prty; break; } else if (prty_arr[set_ptr + j] < 0) { if (old_prty < prty_arr[set_ptr + j]) { if (de_prty > prty_arr[set_ptr + j]) { rem_tag = tag_arr[set_ptr + j]; tag_arr[set_ptr + j] = de_tag; de_tag = rem_tag; rem_prty = prty_arr[set_ptr + j]; prty_arr[set_ptr + j] = de_prty; de_prty = rem_prty; } } } } /*for*/ cache_ptr += cache_size; cache_size += cache_size; ++i; } /*while*/}/**********************************************************************Used to add unknowns to a hash table. The hash table is used by inf_handlerto get the dummy priority of the unknown.Input: The address and the dummy prty of the address.Output: NoneSide effects: The address is added to the hash table**************************************************************************/unk_hash_add_sa (addr, prty)unsigned addr;int prty;{ int loc; struct hash_table *ptr, *oldptr; struct hash_table *UHT_Get_from_free_list (); ++unknowns; loc = addr & ((ONE << B) - 1);/* loc = addr % HASHNO; */ if (slot_flag [loc] == 0){ slot_flag [loc] = 1; slot[loc].addr = addr; slot[loc].prty = prty; slot[loc].nxt = NULL; return(0); } else{ ptr = &slot[loc]; while (ptr) { oldptr = ptr; if (ptr->addr == addr) { fprintf(stderr, "libcheetah: address already in hash table\n"); fprintf(stderr, "addr: %d; t_entries: %d; prty: %d\n", addr, t_entries, ptr->prty); fprintf(stderr, "slot addr: %d; prty: %d\n", slot[loc].addr, slot[loc].prty);} ptr = ptr->nxt; } if ((oldptr->nxt = UHT_Get_from_free_list ()) == NULL) oldptr->nxt = (struct hash_table *) malloc(sizeof(struct hash_table)); oldptr->nxt->addr = addr; oldptr->nxt->nxt = NULL; oldptr->nxt->prty = prty; return(0); }}/*************************************************************************Deletes unknowns from the hash table. Returns the dummy priority ofthe address.Input: AddressOutpur: The dummy prioritySide effects: The entry is deleted from the hash table*************************************************************************/unk_hash_del_sa (addr)unsigned addr;{ int loc; struct hash_table *ptr, *oldptr; int ret_prty; loc = addr & ((ONE << B) - 1);/* loc = addr % HASHNO; */ if (slot_flag[loc] == 0){ return (0); } else if (slot[loc].addr == addr) { if (slot[loc].nxt == NULL) { slot_flag[loc] = 0; return (slot[loc].prty); } else { ret_prty = slot[loc].prty; slot[loc].addr = slot[loc].nxt->addr; slot[loc].prty = slot[loc].nxt->prty; ptr = slot[loc].nxt; slot[loc].nxt = slot[loc].nxt->nxt; UHT_Add_to_free_list(ptr); return (ret_prty); } } else{ ptr = &slot[loc]; while (ptr) { if (ptr->addr == addr) break; oldptr = ptr; ptr = ptr->nxt; } if (ptr){ ret_prty = ptr->prty; oldptr->nxt = ptr->nxt; UHT_Add_to_free_list (ptr); return (ret_prty); } else { return (0); } }}/********************************************************************Given the index of the address in the address array it determines thepriority of the address using the time_array.Input: Index of current address in the addr_array.Output: The priority of the current addressSide effects: None********************************************************************/Priority_sa (i)int i;{ static int inf_count=0; if (time_array [i] > 0) return (MAXINT - time_array [i]); /* inf_handler assumes this fn. inf_handler has to be changed if this is changed */ else if (time_array [i] == 0) return (--inf_count); else { fprintf(stderr, "libcheetah: error in priority function\n"); return(0); }}/********************************************************************Given the address and max_prty, goes through the stack that adress mapsto and removes unknowns of priority greater than max_prtyInput: Address and maximum priority of unknown in stackOutput: NoneSide effects: Removes inessential unknowns from the hash table********************************************************************/hash_clean_sa (addr, max_prty)unsigned addr;int max_prty;{ int loc; struct hash_table *ptr, *oldptr; if (max_prty >= 0) fprintf(stderr, "libcheetah: max unknown priority non-negative\n"); loc = addr & ((ONE << B) - 1); while ((slot_flag[loc] != 0) && (slot[loc].prty > max_prty)) { --unknowns; if (slot[loc].nxt == NULL) { slot_flag[loc] = 0; continue; } else { slot[loc].addr = slot[loc].nxt->addr; slot[loc].grptime = slot[loc].nxt->grptime; slot[loc].prty = slot[loc].nxt->prty; ptr = slot[loc].nxt; slot[loc].nxt = slot[loc].nxt->nxt; UHT_Add_to_free_list (ptr); } } /*while*/ if (slot_flag[loc] != 0) { ptr = &slot[loc]; while (ptr) { if (ptr->prty > max_prty) { --unknowns; oldptr->nxt = ptr->nxt; UHT_Add_to_free_list (ptr); ptr = oldptr; } oldptr = ptr; ptr = ptr->nxt; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -