1 #include "addPrefetchEnhance.h"
2 #include "altprelookup.h"
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;
12 extern char bigarray[16*1024*1024];
14 #define LOGEVENT(x) { \
15 int tmp=bigindex ++; \
23 /* This function creates and initializes the
24 * evalPrefetch global array */
25 pfcstats_t *initPrefetchStats() {
27 if((ptr = calloc(numprefetchsites, sizeof(pfcstats_t))) == NULL) {
28 printf("%s() Calloc error in %s at line %d\n", __func__, __FILE__, __LINE__);
32 /* Enable prefetching at the beginning */
33 for(i=0; i<numprefetchsites; i++) {
36 ptr[i].retrycount = RETRYINTERVAL; //N
37 ptr[i].uselesscount = SHUTDOWNINTERVAL; //M
42 int getRetryCount(int siteid) {
43 return evalPrefetch[siteid].retrycount;
46 int getUselessCount(int siteid) {
47 return evalPrefetch[siteid].uselesscount;
50 char getOperationMode(int siteid) {
51 return evalPrefetch[siteid].operMode;
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); turn on prefetching*/
61 evalPrefetch[siteid].operMode = 1;
62 evalPrefetch[siteid].uselesscount = SHUTDOWNINTERVAL;
64 //Turn off prefetch site
65 if(getOperationMode(siteid) != 0) {
66 evalPrefetch[siteid].uselesscount--;
67 if(evalPrefetch[siteid].uselesscount <= 0) {
69 evalPrefetch[siteid].operMode = 0;
76 /* This function clears from prefetch cache those
77 * entries that caused a transaction abort */
79 unsigned int size = c_size;
80 chashlistnode_t *ptr = c_table;
82 for(i = 0; i < size; i++) {
83 chashlistnode_t *curr = &ptr[i]; //for each entry in the cache lookupTable
87 objheader_t *header1, *header2;
88 /* Not found in local machine's object store and found in prefetch cache */
89 if((header1 = mhashSearch(curr->key)) == NULL && ((header2 = prehashSearch(curr->key)) != NULL)) {
90 /* Remove from prefetch cache */
91 prehashRemove(curr->key);
98 /* This function clears from prefetch cache those
99 * entries that caused a transaction abort */
101 unsigned int size = c_size;
102 struct chashentry *ptr = c_table;
104 for(i = 0; i < size; i++) {
105 struct chashentry *curr = &ptr[i]; //for each entry in the cache lookupTable
108 objheader_t *header1, *header2;
109 /* Not found in local machine's object store and found in prefetch cache */
110 if((header1 = mhashSearch(curr->key)) == NULL && ((header2 = prehashSearch(curr->key)) != NULL)) {
111 /* Remove from prefetch cache */
112 prehashRemove(curr->key);
118 /* This function updates the prefetch cache with
119 * entries from the transaction cache when a
120 * transaction commits
121 * Return -1 on error else returns 0 */
122 int updatePrefetchCache(trans_req_data_t *tdata) {
125 /*//TODO comment it for now because objects read are already in the prefetch cache
127 if(tdata->f.numread > 0) {
128 if((retval = copyToCache(tdata->f.numread, (unsigned int *)(tdata->objread), oidType)) != 0) {
129 printf("%s(): Error in copying objects read at %s, %d\n", __func__, __FILE__, __LINE__);
134 if(tdata->f.nummod > 0) {
136 if((retval = copyToCache(tdata->f.nummod, tdata->oidmod, oidType)) != 0) {
137 printf("%s(): Error in copying objects read at %s, %d\n", __func__, __FILE__, __LINE__);
144 int copyToCache(int numoid, unsigned int *oidarray, char oidType) {
146 for (i = 0; i < numoid; i++) {
148 //if(oidType == 'R') {
149 // char * objread = (char *) oidarray;
150 // oid = *((unsigned int *)(objread+(sizeof(unsigned int)+
151 // sizeof(unsigned short))*i));
155 pthread_mutex_lock(&prefetchcache_mutex);
156 objheader_t * header;
157 if((header = (objheader_t *) t_chashSearch(oid)) == NULL) {
158 printf("%s() obj %x is no longer in transaction cache at %s , %d\n", __func__, oid,__FILE__, __LINE__);
162 //copy into prefetch cache
164 GETSIZE(size, header);
165 objheader_t * newAddr;
166 if((newAddr = prefetchobjstrAlloc(size + sizeof(objheader_t))) == NULL) {
167 printf("%s(): Error in getting memory from prefetch cache at %s, %d\n", __func__,
169 pthread_mutex_unlock(&prefetchcache_mutex);
172 pthread_mutex_unlock(&prefetchcache_mutex);
173 memcpy(newAddr, header, size+sizeof(objheader_t));
174 //Increment version for every modified object
176 newAddr->version += 1;
177 newAddr->notifylist = NULL;
181 //make an entry in prefetch lookup hashtable
182 prehashInsert(oid, newAddr);