4 extern objstr_t *prefetchcache; //Global Prefetch cache
5 extern pthread_mutex_t prefetchcache_mutex; //Mutex to lock Prefetch Cache
6 extern prehashtable_t pflookup; //Global prefetch cache lookup table
7 prefetchNodeInfo_t *pNodeInfo; //Global prefetch holding metadata
9 void initializePCache() {
10 pNodeInfo = calloc(1, sizeof(prefetchNodeInfo_t));
11 pNodeInfo->oldptr = prefetchcache;
12 pNodeInfo->newptr = NULL;
13 pNodeInfo->num_old_objstr = 1; //for prefetch cache allocated by objstralloc in trans.c file
14 pNodeInfo->maxsize = DEFAULT_OBJ_STORE_SIZE;
17 void *prefetchobjstrAlloc(unsigned int size) {
19 if(pNodeInfo->num_old_objstr <= PREFETCH_FLUSH_COUNT_THRESHOLD) {
21 if((ptr = normalPrefetchAlloc(prefetchcache, size)) == NULL) {
22 printf("Error: %s() prefetch cache alloc error %s, %d\n", __func__, __FILE__, __LINE__);
27 // Iterate through available blocks to see if size can be allocated
28 if((ptr = lookUpFreeSpace(pNodeInfo->newptr, pNodeInfo->oldptr, size)) != NULL) {
30 } else { //allocate new block if size not available
31 if(size >= pNodeInfo->maxsize) {
32 if((ptr = allocateNew(size)) == NULL) {
33 printf("Error: %s() Calloc error %s %d\n", __func__, __FILE__, __LINE__);
37 } else { //If size less then reclaim old blocks
38 clearNBlocks(pNodeInfo->oldptr, pNodeInfo->newptr);
39 //update oldptr and newptr
41 //look for free space if available in the free blocks
42 if((ptr = lookUpFreeSpace(pNodeInfo->newptr, pNodeInfo->oldptr, size)) != NULL) {
45 if((ptr = allocateNew(size)) == NULL) {
46 printf("Error: %s() Calloc error %s %d\n", __func__, __FILE__, __LINE__);
56 void *normalPrefetchAlloc(objstr_t *store, unsigned int size) {
59 if(((unsigned int)store->top - (((unsigned int)store) + sizeof(objstr_t)) + size) <= store->size) { //store not full
65 if(store->next == NULL) {
66 //end of list, all full
67 if(size > DEFAULT_OBJ_STORE_SIZE) {
68 //in case of large objects
69 if((store->next = (objstr_t *) calloc(1,(sizeof(objstr_t) + size))) == NULL) {
70 printf("%s() Calloc error at line %d, %s\n", __func__, __LINE__, __FILE__);
76 if((store->next = (objstr_t *) calloc(1, (sizeof(objstr_t) + DEFAULT_OBJ_STORE_SIZE))) == NULL) {
77 printf("%s() Calloc error at line %d, %s\n", __func__, __LINE__, __FILE__);
81 store->size = DEFAULT_OBJ_STORE_SIZE;
83 //Update maxsize of objstr blocks, num of blocks and newptr
84 pNodeInfo->num_old_objstr++;
85 if(pNodeInfo->num_old_objstr <= PREFETCH_FLUSH_COUNT_THRESHOLD/2)
86 pNodeInfo->newptr = store;
87 if(pNodeInfo->maxsize < size)
88 pNodeInfo->maxsize = size;
89 store->top = (void *)(((unsigned int)store) + sizeof(objstr_t) + size);
90 return (void *)(((unsigned int)store) + sizeof(objstr_t));
97 void *lookUpFreeSpace(void *startAddr, void *endAddr, int size) {
100 ptr = (objstr_t *) (startAddr);
101 while(ptr != NULL && ((unsigned long int)ptr!= (unsigned long int)endAddr)) {
102 if(((unsigned int)ptr->top - (((unsigned int)ptr) + sizeof(objstr_t)) + size) <= ptr->size) { //store not full
112 void clearNBlocks(void *oldaddr, void * newaddr) {
114 objstr_t *tmp = (objstr_t *) oldaddr;
115 pthread_mutex_lock(&pflookup.lock);
116 while(((unsigned int) tmp != (unsigned int)newaddr) && (tmp != NULL)) {
117 void * begin = (void *)tmp+sizeof(objstr_t);
118 void * end = (void *)tmp+sizeof(objstr_t)+tmp->size;
119 tmp->top = (void *)tmp+sizeof(objstr_t);
120 clearPLookUpTable(begin, end);
121 //TODO only for testing purpose, remove later
122 memset(tmp->top, 0, tmp->size);
125 pthread_mutex_unlock(&pflookup.lock);
128 void clearPLookUpTable(void *begin, void *end) {
129 unsigned long int tmpbegin;
130 unsigned long int tmpend;
131 tmpbegin = (unsigned long int) begin;
132 tmpend = (unsigned long int) end;
134 prehashlistnode_t *ptr = pflookup.table;
135 for(i = 0; i<pflookup.size; i++) {
136 prehashlistnode_t *curr = &ptr[i];
137 for(; curr != NULL; curr = curr->next) {
138 if(((unsigned long int)(curr->val) >= tmpbegin) && ((unsigned long int)(curr->val) < tmpend)) {
139 unsigned int oid = curr->key;
148 ptr = pNodeInfo->oldptr;
149 pNodeInfo->oldptr = pNodeInfo->newptr;
150 pNodeInfo->newptr = ptr;
153 void *allocateNew(unsigned int size) {
155 if((tmp = (objstr_t *) calloc(1, (sizeof(objstr_t) +size))) == NULL) {
156 printf("Error: %s() Calloc error %s %d\n", __func__, __FILE__, __LINE__);
160 tmp->top = (void *)(((unsigned int)tmp) + sizeof(objstr_t) + size);
161 //Insert newly allocated block into linked list of prefetch cache
162 tmp->next = ((objstr_t *)(pNodeInfo->newptr))->next;
163 ((objstr_t *)(pNodeInfo->newptr))->next = tmp;
164 pNodeInfo->num_old_objstr++;
165 // Update maxsize of prefetch objstr blocks
166 if(pNodeInfo->maxsize < tmp->size)
167 pNodeInfo->maxsize = tmp->size;
168 return (void *)(((unsigned int)tmp) + sizeof(objstr_t));