Thread local hack to make transRead faster...
[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         evalPrefetch[siteid].operMode = 0;
55       }
56     }
57   }
58 }
59
60 #if 1
61 /* This function clears from prefetch cache those
62  * entries that caused a transaction abort */
63 void cleanPCache() {
64   unsigned int size = c_size;
65   chashlistnode_t *ptr = c_table;
66   int i;
67   for(i = 0; i < size; i++) {
68     chashlistnode_t *curr = &ptr[i]; //for each entry in the cache lookupTable
69     while(curr != NULL) {
70       if(curr->key == 0)
71         break;
72       objheader_t *header1, *header2;
73       /* Not found in local machine's object store and found in prefetch cache */
74       if((header1 = mhashSearch(curr->key)) == NULL && ((header2 = prehashSearch(curr->key)) != NULL)) {
75         /* Remove from prefetch cache */
76         prehashRemove(curr->key);
77       }
78       curr = curr->next;
79     }
80   }
81 }
82 #else
83 /* This function clears from prefetch cache those
84  * entries that caused a transaction abort */
85 void cleanPCache() {
86   unsigned int size = c_size;
87   struct chashentry *ptr = c_table;
88   int i;
89   for(i = 0; i < size; i++) {
90     struct chashentry *curr = &ptr[i]; //for each entry in the cache lookupTable
91     if(curr->key == 0)
92       continue;
93     objheader_t *header1, *header2;
94     /* Not found in local machine's object store and found in prefetch cache */
95     if((header1 = mhashSearch(curr->key)) == NULL && ((header2 = prehashSearch(curr->key)) != NULL)) {
96       /* Remove from prefetch cache */
97       prehashRemove(curr->key);
98     }
99   }
100 }
101 #endif
102
103 /* This function updates the prefetch cache with
104  * entries from the transaction cache when a
105  * transaction commits
106  * Return -1 on error else returns 0 */
107 int updatePrefetchCache(trans_req_data_t *tdata) {
108   int retval;
109   char oidType;
110   oidType = 'R';
111   if(tdata->f.numread > 0) {
112     if((retval = copyToCache(tdata->f.numread, (unsigned int *)(tdata->objread), oidType)) != 0) {
113       printf("%s(): Error in copying objects read at %s, %d\n", __func__, __FILE__, __LINE__);
114       return -1;
115     }
116   }
117   if(tdata->f.nummod > 0) {
118     oidType = 'M';
119     if((retval = copyToCache(tdata->f.nummod, tdata->oidmod, oidType)) != 0) {
120       printf("%s(): Error in copying objects read at %s, %d\n", __func__, __FILE__, __LINE__);
121       return -1;
122     }
123   }
124   return 0;
125 }
126
127 int copyToCache(int numoid, unsigned int *oidarray, char oidType) {
128   int i;
129   for (i = 0; i < numoid; i++) {
130     unsigned int oid;
131     if(oidType == 'R') {
132       char * objread = (char *) oidarray;
133       oid = *((unsigned int *)(objread+(sizeof(unsigned int)+
134                                         sizeof(unsigned short))*i));
135     } else {
136       oid = oidarray[i];
137     }
138     pthread_mutex_lock(&prefetchcache_mutex);
139     objheader_t * header;
140     if((header = (objheader_t *) t_chashSearch(oid)) == NULL) {
141       printf("%s() obj %x is no longer in transaction cache at %s , %d\n", __func__, oid,__FILE__, __LINE__);
142       fflush(stdout);
143       return -1;
144     }
145     //copy into prefetch cache
146     int size;
147     GETSIZE(size, header);
148     objheader_t * newAddr;
149     if((newAddr = prefetchobjstrAlloc(size + sizeof(objheader_t))) == NULL) {
150       printf("%s(): Error in getting memory from prefetch cache at %s, %d\n", __func__,
151              __FILE__, __LINE__);
152       pthread_mutex_unlock(&prefetchcache_mutex);
153       return -1;
154     }
155     pthread_mutex_unlock(&prefetchcache_mutex);
156     memcpy(newAddr, header, size+sizeof(objheader_t));
157     //Increment version for every modified object
158     if(oidType == 'M') {
159       newAddr->version += 1;
160       newAddr->notifylist = NULL;
161     }
162     //make an entry in prefetch lookup hashtable
163     void *oldptr;
164     if((oldptr = prehashSearch(oid)) != NULL) {
165       prehashRemove(oid);
166       prehashInsert(oid, newAddr);
167     } else {
168       prehashInsert(oid, newAddr);
169     }
170   } //end of for
171   return 0;
172 }