minor changes for update cache call(where is it called from)
[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 /* This function clears from prefetch cache those 
60  * entries that caused a transaction abort */
61 void cleanPCache(thread_data_array_t *tdata) {
62   transrecord_t *rec = tdata->rec;
63   unsigned int size = rec->lookupTable->size;
64   chashlistnode_t *ptr = rec->lookupTable->table;
65   int i;
66   for(i = 0; i < size; i++) {
67     chashlistnode_t *curr = &ptr[i]; //for each entry in the cache lookupTable
68     while(curr != NULL) {
69       if(curr->key == 0) 
70         break;
71       objheader_t *header1, *header2;
72       if((header1 = mhashSearch(curr->key)) == NULL && ((header2 = prehashSearch(curr->key)) != NULL)) {
73         /* Not found in local machine's object store and found in prefetch cache */
74         /* Remove from prefetch cache */
75         prehashRemove(curr->key);
76       }
77       curr = curr->next;
78     }
79   }
80 }
81
82 /* This function updates the prefetch cache with
83  * entires from the transaction cache when a 
84  * transaction commits 
85  * Return -1 on error else returns 0 */ 
86 int updatePrefetchCache(thread_data_array_t* tdata) {
87   int retval;
88   char oidType;
89   oidType = 'R';
90   if((retval = copyToCache(tdata->buffer->f.numread, (unsigned int *)(tdata->buffer->objread), tdata, oidType)) != 0) {
91     printf("%s(): Error in copying objects read at %s, %d\n", __func__, __FILE__, __LINE__);
92     return -1;
93   }
94   oidType = 'M';
95   if((retval = copyToCache(tdata->buffer->f.nummod, tdata->buffer->oidmod, tdata, oidType)) != 0) {
96     printf("%s(): Error in copying objects read at %s, %d\n", __func__, __FILE__, __LINE__);
97     return -1;
98   }
99   return 0;
100 }
101
102 int copyToCache(int numoid, unsigned int *oidarray, thread_data_array_t *tdata, char oidType) { 
103   int i;
104   for (i = 0; i < numoid; i++) {
105     unsigned int oid;
106     if(oidType == 'R') {
107       char * objread = (char *) oidarray;
108       oid = *((unsigned int *)(objread+(sizeof(unsigned int)+
109               sizeof(unsigned short))*i));
110     } else {
111       oid = oidarray[i];
112     }
113     pthread_mutex_lock(&prefetchcache_mutex);
114     objheader_t *header = (objheader_t *) chashSearch(tdata->rec->lookupTable, oid); 
115     //copy into prefetch cache
116     int size;
117     GETSIZE(size, header);
118     objheader_t * newAddr;
119     if((newAddr = prefetchobjstrAlloc(size + sizeof(objheader_t))) == NULL) {
120       printf("%s(): Error in getting memory from prefetch cache at %s, %d\n", __func__, 
121           __FILE__, __LINE__);
122       pthread_mutex_unlock(&prefetchcache_mutex);
123       return -1;
124     }
125     pthread_mutex_unlock(&prefetchcache_mutex);
126     memcpy(newAddr, header, size+sizeof(objheader_t));
127     //Increment version for every modified object
128     if(oidType == 'M') {
129       newAddr->version += 1;
130       newAddr->notifylist = NULL;
131     }
132     //make an entry in prefetch lookup hashtable
133     void *oldptr;
134     if((oldptr = prehashSearch(oid)) != NULL) {
135       prehashRemove(oid);
136       prehashInsert(oid, newAddr);
137     } else {
138       prehashInsert(oid, newAddr);
139     }
140   } //end of for
141   return 0;
142 }