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