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;
10 /* This function creates and initializes the
11 * evalPrefetch global array */
12 pfcstats_t *initPrefetchStats() {
14 if((ptr = calloc(numprefetchsites, sizeof(pfcstats_t))) == NULL) {
15 printf("%s() Calloc error in %s at line %d\n", __func__, __FILE__, __LINE__);
19 /* Enable prefetching at the beginning */
20 for(i=0; i<numprefetchsites; i++) {
23 ptr[i].retrycount = RETRYINTERVAL; //N
24 ptr[i].uselesscount = SHUTDOWNINTERVAL; //M
29 int getRetryCount(int siteid) {
30 return evalPrefetch[siteid].retrycount;
33 int getUselessCount(int siteid) {
34 return evalPrefetch[siteid].uselesscount;
37 char getOperationMode(int siteid) {
38 return evalPrefetch[siteid].operMode;
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;
51 if(getOperationMode(siteid) != 0) {
52 evalPrefetch[siteid].uselesscount--;
53 if(evalPrefetch[siteid].uselesscount <= 0) {
55 evalPrefetch[siteid].operMode = 0;
62 /* This function clears from prefetch cache those
63 * entries that caused a transaction abort */
65 unsigned int size = c_size;
66 chashlistnode_t *ptr = c_table;
68 for(i = 0; i < size; i++) {
69 chashlistnode_t *curr = &ptr[i]; //for each entry in the cache lookupTable
73 objheader_t *header1, *header2;
74 /* Not found in local machine's object store and found in prefetch cache */
75 if((header1 = mhashSearch(curr->key)) == NULL && ((header2 = prehashSearch(curr->key)) != NULL)) {
76 /* Remove from prefetch cache */
77 prehashRemove(curr->key);
84 /* This function clears from prefetch cache those
85 * entries that caused a transaction abort */
87 unsigned int size = c_size;
88 struct chashentry *ptr = c_table;
90 for(i = 0; i < size; i++) {
91 struct chashentry *curr = &ptr[i]; //for each entry in the cache lookupTable
94 objheader_t *header1, *header2;
95 /* Not found in local machine's object store and found in prefetch cache */
96 if((header1 = mhashSearch(curr->key)) == NULL && ((header2 = prehashSearch(curr->key)) != NULL)) {
97 /* Remove from prefetch cache */
98 prehashRemove(curr->key);
104 /* This function updates the prefetch cache with
105 * entries from the transaction cache when a
106 * transaction commits
107 * Return -1 on error else returns 0 */
108 int updatePrefetchCache(trans_req_data_t *tdata) {
112 if(tdata->f.numread > 0) {
113 if((retval = copyToCache(tdata->f.numread, (unsigned int *)(tdata->objread), oidType)) != 0) {
114 printf("%s(): Error in copying objects read at %s, %d\n", __func__, __FILE__, __LINE__);
118 if(tdata->f.nummod > 0) {
120 if((retval = copyToCache(tdata->f.nummod, tdata->oidmod, oidType)) != 0) {
121 printf("%s(): Error in copying objects read at %s, %d\n", __func__, __FILE__, __LINE__);
128 int copyToCache(int numoid, unsigned int *oidarray, char oidType) {
130 for (i = 0; i < numoid; i++) {
133 char * objread = (char *) oidarray;
134 oid = *((unsigned int *)(objread+(sizeof(unsigned int)+
135 sizeof(unsigned short))*i));
139 pthread_mutex_lock(&prefetchcache_mutex);
140 objheader_t * header;
141 if((header = (objheader_t *) t_chashSearch(oid)) == NULL) {
142 printf("%s() obj %x is no longer in transaction cache at %s , %d\n", __func__, oid,__FILE__, __LINE__);
146 //copy into prefetch cache
148 GETSIZE(size, header);
149 objheader_t * newAddr;
150 if((newAddr = prefetchobjstrAlloc(size + sizeof(objheader_t))) == NULL) {
151 printf("%s(): Error in getting memory from prefetch cache at %s, %d\n", __func__,
153 pthread_mutex_unlock(&prefetchcache_mutex);
156 pthread_mutex_unlock(&prefetchcache_mutex);
157 memcpy(newAddr, header, size+sizeof(objheader_t));
158 //Increment version for every modified object
160 newAddr->version += 1;
161 newAddr->notifylist = NULL;
163 //make an entry in prefetch lookup hashtable
165 if((oldptr = prehashSearch(oid)) != NULL) {
167 prehashInsert(oid, newAddr);
169 prehashInsert(oid, newAddr);