1 #include "addPrefetchEnhance.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) { \
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) */
61 evalPrefetch[siteid].operMode = 1;
62 evalPrefetch[siteid].uselesscount = SHUTDOWNINTERVAL;
64 if(getOperationMode(siteid) != 0) {
65 evalPrefetch[siteid].uselesscount--;
66 if(evalPrefetch[siteid].uselesscount <= 0) {
68 evalPrefetch[siteid].operMode = 0;
75 /* This function clears from prefetch cache those
76 * entries that caused a transaction abort */
78 unsigned int size = c_size;
79 chashlistnode_t *ptr = c_table;
81 for(i = 0; i < size; i++) {
82 chashlistnode_t *curr = &ptr[i]; //for each entry in the cache lookupTable
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);
97 /* This function clears from prefetch cache those
98 * entries that caused a transaction abort */
100 unsigned int size = c_size;
101 struct chashentry *ptr = c_table;
103 for(i = 0; i < size; i++) {
104 struct chashentry *curr = &ptr[i]; //for each entry in the cache lookupTable
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);
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) {
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__);
131 if(tdata->f.nummod > 0) {
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__);
141 int copyToCache(int numoid, unsigned int *oidarray, char oidType) {
143 for (i = 0; i < numoid; i++) {
146 char * objread = (char *) oidarray;
147 oid = *((unsigned int *)(objread+(sizeof(unsigned int)+
148 sizeof(unsigned short))*i));
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__);
159 //copy into prefetch cache
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__,
166 pthread_mutex_unlock(&prefetchcache_mutex);
169 pthread_mutex_unlock(&prefetchcache_mutex);
170 memcpy(newAddr, header, size+sizeof(objheader_t));
171 //Increment version for every modified object
173 newAddr->version += 1;
174 newAddr->notifylist = NULL;
176 //make an entry in prefetch lookup hashtable
178 if((oldptr = prehashSearch(oid)) != NULL) {
180 prehashInsert(oid, newAddr);
182 prehashInsert(oid, newAddr);