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;
25 //Intiliaze and set prefetch table mutex attribute
26 pthread_mutexattr_init(&pflookup.prefetchmutexattr);
27 //NOTE:PTHREAD_MUTEX_RECURSIVE is currently inside a #if_def UNIX98 in the pthread.h file
28 //Therefore use PTHREAD_MUTEX_RECURSIVE_NP instead
29 pthread_mutexattr_settype(&pflookup.prefetchmutexattr, PTHREAD_MUTEX_RECURSIVE_NP);
31 //Initialize mutex var
32 pthread_mutex_init(&pflookup.lock, &pflookup.prefetchmutexattr);
33 //pthread_mutex_init(&pflookup.lock, NULL);
34 pthread_cond_init(&pflookup.cond, NULL);
38 //Assign keys to bins inside hash table
39 unsigned int prehashFunction(unsigned int key) {
40 return ( key & pflookup.mask) >> 1;
43 //Store oids and their pointers into hash
44 unsigned int prehashInsert(unsigned int key, void *val) {
47 prehashlistnode_t *ptr, *node;
49 if(pflookup.numelements > (pflookup.loadfactor * pflookup.size)) {
51 newsize = pflookup.size << 1;
52 pthread_mutex_lock(&pflookup.lock);
53 prehashResize(newsize);
54 pthread_mutex_unlock(&pflookup.lock);
58 pflookup.numelements++;
60 pthread_mutex_lock(&pflookup.lock);
61 index = prehashFunction(key);
62 if(ptr[index].next == NULL && ptr[index].key == 0) { // Insert at the first position in the hashtable
65 } else { // Insert in the beginning of linked list
66 if ((node = calloc(1, sizeof(prehashlistnode_t))) == NULL) {
67 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
68 pthread_mutex_unlock(&pflookup.lock);
73 node->next = ptr[index].next;
74 ptr[index].next = node;
76 pthread_mutex_unlock(&pflookup.lock);
80 // Search for an address for a given oid
81 void *prehashSearch(unsigned int key) {
83 prehashlistnode_t *ptr, *node;
85 pthread_mutex_lock(&pflookup.lock);
86 node = & pflookup.table[(key & pflookup.mask)>>1];
88 if(node->key == key) {
90 pthread_mutex_unlock(&pflookup.lock);
95 pthread_mutex_unlock(&pflookup.lock);
99 unsigned int prehashRemove(unsigned int key) {
101 prehashlistnode_t *curr, *prev;
102 prehashlistnode_t *ptr, *node;
104 pthread_mutex_lock(&pflookup.lock);
105 ptr = pflookup.table;
106 index = prehashFunction(key);
109 for (; curr != NULL; curr = curr->next) {
110 if (curr->key == key) { // Find a match in the hash table
111 pflookup.numelements--; // Decrement the number of elements in the global hashtable
112 if ((curr == &ptr[index]) && (curr->next == NULL)) { // Delete the first item inside the hashtable with no linked list of prehashlistnode_t
115 } else if ((curr == &ptr[index]) && (curr->next != NULL)) { //Delete the first item with a linked list of prehashlistnode_t connected
116 curr->key = curr->next->key;
117 curr->val = curr->next->val;
119 curr->next = curr->next->next;
121 } else { // Regular delete from linked listed
122 prev->next = curr->next;
125 pthread_mutex_unlock(&pflookup.lock);
130 pthread_mutex_unlock(&pflookup.lock);
134 unsigned int prehashResize(unsigned int newsize) {
135 prehashlistnode_t *node, *ptr, *curr, *next; // curr and next keep track of the current and the next chashlistnodes in a linked list
136 unsigned int oldsize;
137 int isfirst; // Keeps track of the first element in the prehashlistnode_t for each bin in hashtable
139 prehashlistnode_t *newnode;
141 ptr = pflookup.table;
142 oldsize = pflookup.size;
144 if((node = calloc(newsize, sizeof(prehashlistnode_t))) == NULL) {
145 printf("Calloc error %s %d\n", __FILE__, __LINE__);
149 pflookup.table = node; //Update the global hashtable upon resize()
150 pflookup.size = newsize;
151 pflookup.mask = (newsize << 1) -1;
152 pflookup.numelements = 0;
154 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
157 while (curr != NULL) { //Inner loop to go through linked lists
158 if (curr->key == 0) { //Exit inner loop if there the first element for a given bin/index is NULL
159 break; //key = val =0 for element if not present within the hash table
162 index = prehashFunction(curr->key);
163 // Insert into the new table
164 if(pflookup.table[index].next == NULL && pflookup.table[index].key == 0) {
165 pflookup.table[index].key = curr->key;
166 pflookup.table[index].val = curr->val;
167 pflookup.numelements++;
169 if((newnode = calloc(1, sizeof(prehashlistnode_t))) == NULL) {
170 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
173 newnode->key = curr->key;
174 newnode->val = curr->val;
175 newnode->next = pflookup.table[index].next;
176 pflookup.table[index].next = newnode;
177 pflookup.numelements++;
180 //free the linked list of prehashlistnode_t if not the first element in the hash table
190 free(ptr); //Free the memory of the old hash table
194 //Note: This is based on the implementation of the inserting a key in the first position of the hashtable
195 void prehashClear() {
198 prehashlistnode_t *ptr, *prev, *curr;
200 pthread_mutex_lock(&pflookup.lock);
201 ptr = pflookup.table;
202 for(i = 0; i < pflookup.size; i++) {
205 while(prev->next != NULL) {
208 prev->next = curr->next;
211 if(isFirstBin == 1) {
218 pthread_mutex_unlock(&pflookup.lock);
219 pthread_mutex_lock(&prefetchcache_mutex);
220 if (pNodeInfo.newstale==NULL) {
221 //transfer the list wholesale;
222 pNodeInfo.oldstale=pNodeInfo.oldptr;
223 pNodeInfo.newstale=pNodeInfo.newptr;
225 //merge the two lists
226 pNodeInfo.newstale->prev=pNodeInfo.oldptr;
227 pNodeInfo.newstale=pNodeInfo.newptr;
229 stale=STALL_THRESHOLD-pNodeInfo.stale_count;
231 if (stale>0&&stale>pNodeInfo.stall)
232 pNodeInfo.stall=stale;
234 pNodeInfo.stale_count+=pNodeInfo.os_count;
235 pNodeInfo.oldptr=getObjStr(DEFAULT_OBJ_STORE_SIZE);
236 pNodeInfo.newptr=pNodeInfo.oldptr;
237 pNodeInfo.os_count=1;
238 pthread_mutex_unlock(&prefetchcache_mutex);