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.numelements = 0; // Initial number of elements in the hash
22 pflookup.loadfactor = loadfactor;
24 //Intiliaze and set prefetch table mutex attribute
25 pthread_mutexattr_init(&pflookup.prefetchmutexattr);
26 //NOTE:PTHREAD_MUTEX_RECURSIVE is currently inside a #if_def UNIX98 in the pthread.h file
27 //Therefore use PTHREAD_MUTEX_RECURSIVE_NP instead
28 pthread_mutexattr_settype(&pflookup.prefetchmutexattr, PTHREAD_MUTEX_RECURSIVE_NP);
30 //Initialize mutex var
31 pthread_mutex_init(&pflookup.lock, &pflookup.prefetchmutexattr);
32 //pthread_mutex_init(&pflookup.lock, NULL);
33 pthread_cond_init(&pflookup.cond, NULL);
37 //Assign keys to bins inside hash table
38 unsigned int prehashFunction(unsigned int key) {
39 return ( key % (pflookup.size));
42 //Store oids and their pointers into hash
43 unsigned int prehashInsert(unsigned int key, void *val) {
46 prehashlistnode_t *ptr, *node;
48 if(pflookup.numelements > (pflookup.loadfactor * pflookup.size)) {
50 newsize = 2 * pflookup.size + 1;
51 pthread_mutex_lock(&pflookup.lock);
52 prehashResize(newsize);
53 pthread_mutex_unlock(&pflookup.lock);
57 pflookup.numelements++;
59 pthread_mutex_lock(&pflookup.lock);
60 index = prehashFunction(key);
61 if(ptr[index].next == NULL && ptr[index].key == 0) { // Insert at the first position in the hashtable
64 } else { // Insert in the beginning of linked list
65 if ((node = calloc(1, sizeof(prehashlistnode_t))) == NULL) {
66 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
67 pthread_mutex_unlock(&pflookup.lock);
72 node->next = ptr[index].next;
73 ptr[index].next = node;
75 pthread_mutex_unlock(&pflookup.lock);
79 // Search for an address for a given oid
80 void *prehashSearch(unsigned int key) {
82 prehashlistnode_t *ptr, *node;
84 pthread_mutex_lock(&pflookup.lock);
86 index = prehashFunction(key);
89 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.numelements = 0;
153 for(i = 0; i < oldsize; i++) { //Outer loop for each bin in hash table
156 while (curr != NULL) { //Inner loop to go through linked lists
157 if (curr->key == 0) { //Exit inner loop if there the first element for a given bin/index is NULL
158 break; //key = val =0 for element if not present within the hash table
161 index = prehashFunction(curr->key);
162 // Insert into the new table
163 if(pflookup.table[index].next == NULL && pflookup.table[index].key == 0) {
164 pflookup.table[index].key = curr->key;
165 pflookup.table[index].val = curr->val;
166 pflookup.numelements++;
168 if((newnode = calloc(1, sizeof(prehashlistnode_t))) == NULL) {
169 printf("Calloc error %s, %d\n", __FILE__, __LINE__);
172 newnode->key = curr->key;
173 newnode->val = curr->val;
174 newnode->next = pflookup.table[index].next;
175 pflookup.table[index].next = newnode;
176 pflookup.numelements++;
179 //free the linked list of prehashlistnode_t if not the first element in the hash table
189 free(ptr); //Free the memory of the old hash table
193 //Note: This is based on the implementation of the inserting a key in the first position of the hashtable
194 void prehashClear() {
197 prehashlistnode_t *ptr, *prev, *curr;
199 pthread_mutex_lock(&pflookup.lock);
200 ptr = pflookup.table;
201 for(i = 0; i < pflookup.size; i++) {
204 while(prev->next != NULL) {
207 prev->next = curr->next;
210 if(isFirstBin == 1) {
217 pthread_mutex_unlock(&pflookup.lock);
218 pthread_mutex_lock(&prefetchcache_mutex);
219 if (pNodeInfo.newstale==NULL) {
220 //transfer the list wholesale;
221 pNodeInfo.oldstale=pNodeInfo.oldptr;
222 pNodeInfo.newstale=pNodeInfo.newptr;
224 //merge the two lists
225 pNodeInfo.newstale->prev=pNodeInfo.oldptr;
226 pNodeInfo.newstale=pNodeInfo.newptr;
228 stale=STALL_THRESHOLD-pNodeInfo.stale_count;
230 if (stale>0&&stale>pNodeInfo.stall)
231 pNodeInfo.stall=stale;
233 pNodeInfo.stale_count+=pNodeInfo.os_count;
234 pNodeInfo.oldptr=getObjStr(DEFAULT_OBJ_STORE_SIZE);
235 pNodeInfo.newptr=pNodeInfo.oldptr;
236 pNodeInfo.os_count=1;
237 pthread_mutex_unlock(&prefetchcache_mutex);