changes
[IRC.git] / Robust / src / Runtime / DSTM / interface / addPrefetchEnhance.c
1 #include "addPrefetchEnhance.h"
2 #include "prelookup.h"
3
4 extern int numprefetchsites; // Number of prefetch sites
5 extern pfcstats_t *evalPrefetch; //Global array that keeps track of operation mode (ON/OFF) for each prefetch site
6 extern objstr_t *prefetchcache; //Global Prefetch cache
7 extern pthread_mutex_t prefetchcache_mutex; //Mutex to lock Prefetch Cache
8 extern unsigned int myIpAddr;
9
10 /* This function creates and initializes the
11  * evalPrefetch global array */
12 pfcstats_t *initPrefetchStats() {
13   pfcstats_t *ptr;
14   if((ptr = calloc(numprefetchsites, sizeof(pfcstats_t))) == NULL) {
15     printf("%s() Calloc error in %s at line %d\n", __func__, __FILE__, __LINE__);
16     return NULL;
17   }
18   int i;
19   /* Enable prefetching at the beginning */
20   for(i=0; i<numprefetchsites; i++) {
21     ptr[i].operMode = 1;
22     ptr[i].callcount = 0;
23     ptr[i].retrycount = RETRYINTERVAL; //N
24     ptr[i].uselesscount = SHUTDOWNINTERVAL; //M
25   }
26   return ptr;
27 }
28
29 int getRetryCount(int siteid) {
30   return evalPrefetch[siteid].retrycount;
31 }
32
33 int getUselessCount(int siteid) {
34   return evalPrefetch[siteid].uselesscount;
35 }
36
37 char getOperationMode(int siteid) {
38   return evalPrefetch[siteid].operMode;
39 }
40
41 /* This function updates counters and mode of operation of a
42  * prefetch site during runtime. When the prefetch call at a site
43  * generates oids that are found/not found in the prefetch cache,
44  * we take action accordingly */
45 void handleDynPrefetching(int numLocal, int ntuples, int siteid) {
46   if(numLocal < ntuples) {
47     /* prefetch not found locally(miss in cache) */
48     evalPrefetch[siteid].operMode = 1;
49     evalPrefetch[siteid].uselesscount = SHUTDOWNINTERVAL;
50   } else {
51     if(getOperationMode(siteid) != 0) {
52       evalPrefetch[siteid].uselesscount--;
53       if(evalPrefetch[siteid].uselesscount <= 0) {
54         printf("O");
55         evalPrefetch[siteid].operMode = 0;
56       }
57     }
58   }
59 }
60
61 #if 1
62 /* This function clears from prefetch cache those
63  * entries that caused a transaction abort */
64 void cleanPCache() {
65   unsigned int size = c_size;
66   chashlistnode_t *ptr = c_table;
67   int i;
68   for(i = 0; i < size; i++) {
69     chashlistnode_t *curr = &ptr[i]; //for each entry in the cache lookupTable
70     while(curr != NULL) {
71       if(curr->key == 0)
72         break;
73       objheader_t *header1, *header2;
74       /* Not found in local machine's object store and found in prefetch cache */
75       if((header1 = mhashSearch(curr->key)) == NULL && ((header2 = prehashSearch(curr->key)) != NULL)) {
76         /* Remove from prefetch cache */
77         prehashRemove(curr->key);
78       }
79       curr = curr->next;
80     }
81   }
82 }
83 #else
84 /* This function clears from prefetch cache those
85  * entries that caused a transaction abort */
86 void cleanPCache() {
87   unsigned int size = c_size;
88   struct chashentry *ptr = c_table;
89   int i;
90   for(i = 0; i < size; i++) {
91     struct chashentry *curr = &ptr[i]; //for each entry in the cache lookupTable
92     if(curr->key == 0)
93       continue;
94     objheader_t *header1, *header2;
95     /* Not found in local machine's object store and found in prefetch cache */
96     if((header1 = mhashSearch(curr->key)) == NULL && ((header2 = prehashSearch(curr->key)) != NULL)) {
97       /* Remove from prefetch cache */
98       prehashRemove(curr->key);
99     }
100   }
101 }
102 #endif
103
104 /* This function updates the prefetch cache with
105  * entries from the transaction cache when a
106  * transaction commits
107  * Return -1 on error else returns 0 */
108 int updatePrefetchCache(trans_req_data_t *tdata) {
109   int retval;
110   char oidType;
111   oidType = 'R';
112   if(tdata->f.numread > 0) {
113     if((retval = copyToCache(tdata->f.numread, (unsigned int *)(tdata->objread), oidType)) != 0) {
114       printf("%s(): Error in copying objects read at %s, %d\n", __func__, __FILE__, __LINE__);
115       return -1;
116     }
117   }
118   if(tdata->f.nummod > 0) {
119     oidType = 'M';
120     if((retval = copyToCache(tdata->f.nummod, tdata->oidmod, oidType)) != 0) {
121       printf("%s(): Error in copying objects read at %s, %d\n", __func__, __FILE__, __LINE__);
122       return -1;
123     }
124   }
125   return 0;
126 }
127
128 int copyToCache(int numoid, unsigned int *oidarray, char oidType) {
129   int i;
130   for (i = 0; i < numoid; i++) {
131     unsigned int oid;
132     if(oidType == 'R') {
133       char * objread = (char *) oidarray;
134       oid = *((unsigned int *)(objread+(sizeof(unsigned int)+
135                                         sizeof(unsigned short))*i));
136     } else {
137       oid = oidarray[i];
138     }
139     pthread_mutex_lock(&prefetchcache_mutex);
140     objheader_t * header;
141     if((header = (objheader_t *) t_chashSearch(oid)) == NULL) {
142       printf("%s() obj %x is no longer in transaction cache at %s , %d\n", __func__, oid,__FILE__, __LINE__);
143       fflush(stdout);
144       return -1;
145     }
146     //copy into prefetch cache
147     int size;
148     GETSIZE(size, header);
149     objheader_t * newAddr;
150     if((newAddr = prefetchobjstrAlloc(size + sizeof(objheader_t))) == NULL) {
151       printf("%s(): Error in getting memory from prefetch cache at %s, %d\n", __func__,
152              __FILE__, __LINE__);
153       pthread_mutex_unlock(&prefetchcache_mutex);
154       return -1;
155     }
156     pthread_mutex_unlock(&prefetchcache_mutex);
157     memcpy(newAddr, header, size+sizeof(objheader_t));
158     //Increment version for every modified object
159     if(oidType == 'M') {
160       newAddr->version += 1;
161       newAddr->notifylist = NULL;
162     }
163     //make an entry in prefetch lookup hashtable
164     void *oldptr;
165     if((oldptr = prehashSearch(oid)) != NULL) {
166       prehashRemove(oid);
167       prehashInsert(oid, newAddr);
168     } else {
169       prehashInsert(oid, newAddr);
170     }
171   } //end of for
172   return 0;
173 }