1 /* LOCK THE ENTIRE HASH TABLE */
4 extern objstr_t *prefetchcache;
5 extern pthread_mutex_t prefetchcache_mutex; //Mutex to lock Prefetch Cache
6 extern prefetchNodeInfo_t pNodeInfo;
8 prehashtable_t pflookup; //Global prefetch cache table
10 unsigned int prehashCreate(unsigned int size, float loadfactor) {
11 prehashlistnode_t *nodes;
14 // Allocate space for the hash table
15 if((nodes = calloc(size, sizeof(prehashlistnode_t))) == NULL) {
16 printf("Calloc error %s %d\n", __FILE__, __LINE__);
19 pflookup.table = nodes;
21 pflookup.mask = (size << 1) -1;
22 pflookup.numelements = 0; // Initial number of elements in the hash
23 pflookup.loadfactor = loadfactor;
24 pflookup.threshold=loadfactor*size;
26 //Intiliaze and set prefetch table mutex attribute
27 pthread_mutexattr_init(&pflookup.prefetchmutexattr);
28 //NOTE:PTHREAD_MUTEX_RECURSIVE is currently inside a #if_def UNIX98 in the pthread.h file
29 //Therefore use PTHREAD_MUTEX_RECURSIVE_NP instead
30 pthread_mutexattr_settype(&pflookup.prefetchmutexattr, PTHREAD_MUTEX_RECURSIVE_NP);
32 //Initialize mutex var
33 pthread_mutex_init(&pflookup.lock, &pflookup.prefetchmutexattr);
34 //pthread_mutex_init(&pflookup.lock, NULL);
35 pthread_cond_init(&pflookup.cond, NULL);
39 //Assign keys to bins inside hash table
40 unsigned int prehashFunction(unsigned int key) {
41 return ( key & pflookup.mask) >> 1;
44 //Store oids and their pointers into hash
45 void prehashInsert(unsigned int key, void *val) {
46 prehashlistnode_t *ptr;
47 pthread_mutex_lock(&pflookup.lock);
49 if(pflookup.numelements > (pflookup.threshold)) {
51 unsigned int newsize = pflookup.size << 1;
52 prehashResize(newsize);
56 ptr = &pflookup.table[(key & pflookup.mask)>>1];
57 pflookup.numelements++;
62 } else { // Insert in the beginning of linked list
63 prehashlistnode_t * node = calloc(1, sizeof(prehashlistnode_t));
66 node->next = ptr->next;
69 pthread_mutex_unlock(&pflookup.lock);
72 // Search for an address for a given oid
73 void *prehashSearch(unsigned int key) {
75 prehashlistnode_t *ptr, *node;
77 pthread_mutex_lock(&pflookup.lock);
78 node = & pflookup.table[(key & pflookup.mask)>>1];
80 if(node->key == key) {
82 pthread_mutex_unlock(&pflookup.lock);
87 pthread_mutex_unlock(&pflookup.lock);
91 unsigned int prehashRemove(unsigned int key) {
93 prehashlistnode_t *curr, *prev;
94 prehashlistnode_t *ptr, *node;
96 pthread_mutex_lock(&pflookup.lock);
98 index = prehashFunction(key);
101 for (; curr != NULL; curr = curr->next) {
102 if (curr->key == key) { // Find a match in the hash table
103 pflookup.numelements--; // Decrement the number of elements in the global hashtable
104 if ((curr == &ptr[index]) && (curr->next == NULL)) { // Delete the first item inside the hashtable with no linked list of prehashlistnode_t
107 } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first item with a linked list of prehashlistnode_t connected
108 curr->key = curr->next->key;
109 curr->val = curr->next->val;
111 curr->next = curr->next->next;
113 } else { // Regular delete from linked listed
114 prev->next = curr->next;
117 pthread_mutex_unlock(&pflookup.lock);
122 pthread_mutex_unlock(&pflookup.lock);
126 unsigned int prehashResize(unsigned int newsize) {
127 prehashlistnode_t *node, *ptr; // curr and next keep track of the current and the next chashlistnodes in a linked list
128 unsigned int oldsize;
131 ptr = pflookup.table;
132 oldsize = pflookup.size;
134 if((node = calloc(newsize, sizeof(prehashlistnode_t))) == NULL) {
135 printf("Calloc error %s %d\n", __FILE__, __LINE__);
139 pflookup.table = node; //Update the global hashtable upon resize()
140 pflookup.size = newsize;
141 pflookup.threshold=newsize*pflookup.loadfactor;
142 mask=pflookup.mask = (newsize << 1) -1;
144 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
145 prehashlistnode_t * curr = &ptr[i];
146 prehashlistnode_t *tmp, *next;
150 if ((key=curr->key) == 0) { //Exit inner loop if there the first element for a given bin/index is NULL
151 break; //key = val =0 for element if not present within the hash table
154 index = (key & mask)>>1;
155 tmp=&pflookup.table[index];
156 // Insert into the new table
163 NOTE: Add this case if you change this...
164 This case currently never happens because of the way things rehash....
166 prehashlistnode_t * newnode = calloc(1, sizeof(prehashlistnode_t));
167 newnode->key = curr->key;
168 newnode->val = curr->val;
169 newnode->next = tmp->next;
173 curr->next=tmp->next;
182 free(ptr); //Free the memory of the old hash table
186 //Note: This is based on the implementation of the inserting a key in the first position of the hashtable
187 void prehashClear() {
190 prehashlistnode_t *ptr, *prev, *curr;
192 pthread_mutex_lock(&pflookup.lock);
193 ptr = pflookup.table;
194 for(i = 0; i < pflookup.size; i++) {
197 while(prev->next != NULL) {
200 prev->next = curr->next;
203 if(isFirstBin == 1) {
210 pthread_mutex_unlock(&pflookup.lock);
211 pthread_mutex_lock(&prefetchcache_mutex);
212 if (pNodeInfo.newstale==NULL) {
213 //transfer the list wholesale;
214 pNodeInfo.oldstale=pNodeInfo.oldptr;
215 pNodeInfo.newstale=pNodeInfo.newptr;
217 //merge the two lists
218 pNodeInfo.newstale->prev=pNodeInfo.oldptr;
219 pNodeInfo.newstale=pNodeInfo.newptr;
221 stale=STALL_THRESHOLD-pNodeInfo.stale_count;
223 if (stale>0&&stale>pNodeInfo.stall)
224 pNodeInfo.stall=stale;
226 pNodeInfo.stale_count+=pNodeInfo.os_count;
227 pNodeInfo.oldptr=getObjStr(DEFAULT_OBJ_STORE_SIZE);
228 pNodeInfo.newptr=pNodeInfo.oldptr;
229 pNodeInfo.os_count=1;
230 pthread_mutex_unlock(&prefetchcache_mutex);