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) {
54 evalPrefetch[siteid].operMode = 0;
60 /* This function clears from prefetch cache those
61 * entries that caused a transaction abort */
62 void cleanPCache(transrecord_t *record) {
63 transrecord_t *rec = record;
64 unsigned int size = rec->lookupTable->size;
65 chashlistnode_t *ptr = rec->lookupTable->table;
67 for(i = 0; i < size; i++) {
68 chashlistnode_t *curr = &ptr[i]; //for each entry in the cache lookupTable
72 objheader_t *header1, *header2;
73 /* Not found in local machine's object store and found in prefetch cache */
74 if((header1 = mhashSearch(curr->key)) == NULL && ((header2 = prehashSearch(curr->key)) != NULL)) {
75 /* Remove from prefetch cache */
76 prehashRemove(curr->key);
83 /* This function updates the prefetch cache with
84 * entries from the transaction cache when a
86 * Return -1 on error else returns 0 */
87 int updatePrefetchCache(trans_req_data_t *tdata, transrecord_t *rec) {
91 if(tdata->f.numread > 0) {
92 if((retval = copyToCache(tdata->f.numread, (unsigned int *)(tdata->objread), rec, oidType)) != 0) {
93 printf("%s(): Error in copying objects read at %s, %d\n", __func__, __FILE__, __LINE__);
97 if(tdata->f.nummod > 0) {
99 if((retval = copyToCache(tdata->f.nummod, tdata->oidmod, rec, oidType)) != 0) {
100 printf("%s(): Error in copying objects read at %s, %d\n", __func__, __FILE__, __LINE__);
107 int copyToCache(int numoid, unsigned int *oidarray, transrecord_t *rec, char oidType) {
109 for (i = 0; i < numoid; i++) {
112 char * objread = (char *) oidarray;
113 oid = *((unsigned int *)(objread+(sizeof(unsigned int)+
114 sizeof(unsigned short))*i));
118 pthread_mutex_lock(&prefetchcache_mutex);
119 objheader_t * header;
120 if((header = (objheader_t *) chashSearch(rec->lookupTable, oid)) == NULL) {
121 printf("%s() obj %x is no longer in transaction cache at %s , %d\n", __func__, oid,__FILE__, __LINE__);
125 //copy into prefetch cache
127 GETSIZE(size, header);
128 objheader_t * newAddr;
129 if((newAddr = prefetchobjstrAlloc(size + sizeof(objheader_t))) == NULL) {
130 printf("%s(): Error in getting memory from prefetch cache at %s, %d\n", __func__,
132 pthread_mutex_unlock(&prefetchcache_mutex);
135 pthread_mutex_unlock(&prefetchcache_mutex);
136 memcpy(newAddr, header, size+sizeof(objheader_t));
137 //Increment version for every modified object
139 newAddr->version += 1;
140 newAddr->notifylist = NULL;
142 //make an entry in prefetch lookup hashtable
144 if((oldptr = prehashSearch(oid)) != NULL) {
146 prehashInsert(oid, newAddr);
148 prehashInsert(oid, newAddr);