2 #include "altprelookup.h"
5 extern pthread_mutex_t prefetchcache_mutex; //Mutex to lock Prefetch Cache
6 extern prehashtable_t pflookup; //Global prefetch cache lookup table
7 prefetchNodeInfo_t pNodeInfo; //Global prefetch holding metadata
9 #define OSUSED(x) (((unsigned int)(x)->top)-((unsigned int) (x+1)))
10 #define OSFREE(x) ((x)->size-OSUSED(x))
12 void initializePCache() {
13 objstr_t * os=objstrCreate(DEFAULT_OBJ_STORE_SIZE);
14 pNodeInfo.oldptr = os;
15 pNodeInfo.newptr = os;
16 pNodeInfo.os_count = 1; //for prefetch cache allocated by objstralloc in trans.c file
17 pNodeInfo.oldstale=NULL;
18 pNodeInfo.newstale=NULL;
19 pNodeInfo.stale_count=0;
23 objstr_t * getObjStr(unsigned int size) {
24 if (pNodeInfo.stall>0)
26 if (size<=DEFAULT_OBJ_STORE_SIZE&&pNodeInfo.stale_count>STALE_MINTHRESHOLD&&pNodeInfo.stall==0) {
28 objstr_t * tmp=pNodeInfo.oldstale;
29 pNodeInfo.oldstale=pNodeInfo.oldstale->prev;
30 if (pNodeInfo.oldstale==NULL)
31 pNodeInfo.newstale=NULL;
32 pNodeInfo.stale_count--;
37 int allocsize=(size>DEFAULT_OBJ_STORE_SIZE) ? size : DEFAULT_OBJ_STORE_SIZE;
38 return objstrCreate(allocsize);
42 void *prefetchobjstrAlloc(unsigned int size) {
43 //try existing space in first two OS
44 objstr_t *os=pNodeInfo.newptr;
47 if (size<=OSFREE(os)) {
49 os->top=((char *)os->top)+size;
52 if ((os=os->next)!=NULL&&(size<=OSFREE(os))) {
54 os->top=((char *)os->top)+size;
57 //need to allocate new space
58 objstr_t *tmp=getObjStr(size);;
61 tmp->next=pNodeInfo.newptr;
62 pNodeInfo.newptr->prev=tmp;
66 if (pNodeInfo.os_count>PREFETCH_FLUSH_THRESHOLD) {
67 //remove oldest from linked list
68 objstr_t *tofree=pNodeInfo.oldptr;
69 pNodeInfo.oldptr=tofree->prev;
73 if (pNodeInfo.newstale==NULL) {
75 pNodeInfo.newstale=pNodeInfo.oldstale=tofree;
77 pNodeInfo.stale_count++;
79 //just add it to the list
80 pNodeInfo.newstale->prev=tofree;
81 pNodeInfo.newstale=tofree;
82 pNodeInfo.stale_count++;
84 if (pNodeInfo.stale_count>STALE_MAXTHRESHOLD) {
85 //need to toss a store
86 tofree=pNodeInfo.oldstale;
87 pNodeInfo.oldstale=tofree->prev;
88 pNodeInfo.stale_count--;
94 tmp->top=((char *)tmp->top)+size;
98 void clearBlock(objstr_t *block) {
100 unsigned long int tmpbegin=(unsigned int)block;
101 unsigned long int tmpend=(unsigned int)block->top;
103 prehashlistnode_t *ptr;
106 ptr = pflookup.table;
107 volatile unsigned int * lockptr_current=&pflookup.larray[lockindex].lock;
108 while(!write_trylock(lockptr_current)) {
112 for(i = 0; i<pflookup.size; i++) {
114 prehashlistnode_t *orig=&ptr[i];
115 prehashlistnode_t *curr = orig;
116 prehashlistnode_t *next=curr->next;
117 for(; next != NULL; curr=next, next = next->next) {
118 unsigned int val=(unsigned int)next->val;
119 if ((val>=tmpbegin)&(val<tmpend)) {
120 prehashlistnode_t *tmp=curr->next=next->next;
123 //loop condition is broken now...need to check before incrementing
129 unsigned int val=(unsigned int)orig->val;
130 if ((val>=tmpbegin)&(val<tmpend)) {
131 if (orig->next==NULL) {
138 orig->next=next->next;
144 if(((i+1)&(pflookup.mask>>4))==0 && (i+1)<pflookup.size) {
145 // try to grab new lock
147 volatile unsigned int * lockptr_new=&pflookup.larray[lockindex].lock;
148 while(!write_trylock(lockptr_new)) {
151 write_unlock(lockptr_current);
152 lockptr_current=lockptr_new;
155 } // end of for (pflokup)
157 write_unlock(lockptr_current);
160 objstr_t *allocateNew(unsigned int size) {
162 if((tmp = (objstr_t *) calloc(1, (sizeof(objstr_t) +size))) == NULL) {
163 printf("Error: %s() Calloc error %s %d\n", __func__, __FILE__, __LINE__);
167 tmp->top = (void *)(((unsigned int)tmp) + sizeof(objstr_t) + size);
168 //Insert newly allocated block into linked list of prefetch cache
169 // Update maxsize of prefetch objstr blocks