From: adash Date: Tue, 29 Jul 2008 01:49:11 +0000 (+0000) Subject: bug fixes in gCollect.c and gCollect.h X-Git-Tag: preEdgeChange~52 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=88e271edf35f5cb084032edac76b877b5b9e67c1;p=IRC.git bug fixes in gCollect.c and gCollect.h changes to trans.c that aids in prefetch cache allocation --- diff --git a/Robust/src/Runtime/DSTM/interface/addUdpEnhance.h b/Robust/src/Runtime/DSTM/interface/addUdpEnhance.h index 3a0f9174..f68e5ca0 100644 --- a/Robust/src/Runtime/DSTM/interface/addUdpEnhance.h +++ b/Robust/src/Runtime/DSTM/interface/addUdpEnhance.h @@ -1,5 +1,5 @@ -#ifndef _UDP_H -#define _UDP_H +#ifndef _ADDUDPENHANCE_H +#define _ADDUDPENHANCE_H #include "dstm.h" diff --git a/Robust/src/Runtime/DSTM/interface/dstm.h b/Robust/src/Runtime/DSTM/interface/dstm.h index c97787c3..1f32fad6 100644 --- a/Robust/src/Runtime/DSTM/interface/dstm.h +++ b/Robust/src/Runtime/DSTM/interface/dstm.h @@ -142,7 +142,6 @@ typedef struct objheader { #define GETSIZE(size, x) size=classsize[TYPE(x)] #endif - typedef struct objstr { unsigned int size; //this many bytes are allocated after this header void *top; diff --git a/Robust/src/Runtime/DSTM/interface/gCollect.c b/Robust/src/Runtime/DSTM/interface/gCollect.c index 0c0ad9f7..3d1371b3 100644 --- a/Robust/src/Runtime/DSTM/interface/gCollect.c +++ b/Robust/src/Runtime/DSTM/interface/gCollect.c @@ -14,46 +14,39 @@ void initializePCache() { } void *prefetchobjstrAlloc(unsigned int size) { - void * ptr; + void * ptr = NULL; if(pNodeInfo->num_old_objstr <= PREFETCH_FLUSH_COUNT_THRESHOLD) { //regular allocation - pthread_mutex_lock(&prefetchcache_mutex); if((ptr = normalPrefetchAlloc(prefetchcache, size)) == NULL) { printf("Error: %s() prefetch cache alloc error %s, %d\n", __func__, __FILE__, __LINE__); - pthread_mutex_unlock(&prefetchcache_mutex); return NULL; } - pthread_mutex_unlock(&prefetchcache_mutex); return ptr; } else { // Iterate through available blocks to see if size can be allocated - if((ptr = lookUpFreeSpace(size)) != NULL) { + if((ptr = lookUpFreeSpace(pNodeInfo->newptr, pNodeInfo->oldptr, size)) != NULL) { return ptr; } else { //allocate new block if size not available if(size >= pNodeInfo->maxsize) { - objstr_t *tmp; - if((tmp = (objstr_t *) calloc(1, (sizeof(objstr_t) +size))) == NULL) { + if((ptr = allocateNew(size)) == NULL) { printf("Error: %s() Calloc error %s %d\n", __func__, __FILE__, __LINE__); return NULL; } - tmp->size = size; - tmp->top = (void *)(((unsigned int)tmp) + sizeof(objstr_t) + size); - //Insert newly allocated block into linked list of prefetch cache - tmp->next = ((objstr_t *)(pNodeInfo->newptr))->next; - ((objstr_t *)(pNodeInfo->newptr))->next = tmp; - pNodeInfo->num_old_objstr++; - // Update maxsize of prefetch objstr blocks - if(pNodeInfo->maxsize < tmp->size) - pNodeInfo->maxsize = tmp->size; - return (void *)(((unsigned int)tmp) + sizeof(objstr_t)); + return ptr; } else { //If size less then reclaim old blocks - if((ptr = clearNBlocks(pNodeInfo->oldptr, pNodeInfo->newptr, size)) == NULL) { - printf("%s(): Error in flushing from cache blocks %s, %d\n", __func__, __FILE__, __LINE__); - return NULL; - } + clearNBlocks(pNodeInfo->oldptr, pNodeInfo->newptr); //update oldptr and newptr updatePtrs(); - return ptr; + //look for free space if available in the free blocks + if((ptr = lookUpFreeSpace(pNodeInfo->newptr, pNodeInfo->oldptr, size)) != NULL) { + return ptr; + } else { + if((ptr = allocateNew(size)) == NULL) { + printf("Error: %s() Calloc error %s %d\n", __func__, __FILE__, __LINE__); + return NULL; + } + return ptr; + } } } } @@ -88,7 +81,7 @@ void *normalPrefetchAlloc(objstr_t *store, unsigned int size) { } //Update maxsize of objstr blocks, num of blocks and newptr pNodeInfo->num_old_objstr++; - if(pNodeInfo->num_old_objstr == PREFETCH_FLUSH_COUNT_THRESHOLD/2) + if(pNodeInfo->num_old_objstr <= PREFETCH_FLUSH_COUNT_THRESHOLD/2) pNodeInfo->newptr = store; if(pNodeInfo->maxsize < size) pNodeInfo->maxsize = size; @@ -100,22 +93,22 @@ void *normalPrefetchAlloc(objstr_t *store, unsigned int size) { } } -void *lookUpFreeSpace(int size) { +void *lookUpFreeSpace(void *startAddr, void *endAddr, int size) { objstr_t *ptr; void *tmp; - ptr = (objstr_t *) (pNodeInfo->newptr); - while(ptr != NULL && ((unsigned long int)ptr!= (unsigned long int)pNodeInfo->oldptr)) { //always insert in the new region + ptr = (objstr_t *) (startAddr); + while(ptr != NULL && ((unsigned long int)ptr!= (unsigned long int)endAddr)) { if(((unsigned int)ptr->top - (((unsigned int)ptr) + sizeof(objstr_t)) + size) <= ptr->size) { //store not full tmp = ptr->top; ptr->top += size; return tmp; - } + } ptr = ptr->next; } return NULL; } -void *clearNBlocks(void *oldaddr, void * newaddr, unsigned int size){ +void clearNBlocks(void *oldaddr, void * newaddr) { int count = 0; objstr_t *tmp = (objstr_t *) oldaddr; pthread_mutex_lock(&pflookup.lock); @@ -123,15 +116,12 @@ void *clearNBlocks(void *oldaddr, void * newaddr, unsigned int size){ void * begin = (void *)tmp+sizeof(objstr_t); void * end = (void *)tmp+sizeof(objstr_t)+tmp->size; tmp->top = (void *)tmp+sizeof(objstr_t); - //TODO only for testing purpose, remove later clearPLookUpTable(begin, end); + //TODO only for testing purpose, remove later memset(tmp->top, 0, tmp->size); tmp = tmp->next; } pthread_mutex_unlock(&pflookup.lock); - void *ptr = ((objstr_t *)oldaddr)->top; - ((objstr_t *)oldaddr)->top += size; - return ptr; } void clearPLookUpTable(void *begin, void *end) { @@ -158,3 +148,21 @@ void updatePtrs() { pNodeInfo->oldptr = pNodeInfo->newptr; pNodeInfo->newptr = ptr; } + +void *allocateNew(unsigned int size) { + objstr_t *tmp; + if((tmp = (objstr_t *) calloc(1, (sizeof(objstr_t) +size))) == NULL) { + printf("Error: %s() Calloc error %s %d\n", __func__, __FILE__, __LINE__); + return NULL; + } + tmp->size = size; + tmp->top = (void *)(((unsigned int)tmp) + sizeof(objstr_t) + size); + //Insert newly allocated block into linked list of prefetch cache + tmp->next = ((objstr_t *)(pNodeInfo->newptr))->next; + ((objstr_t *)(pNodeInfo->newptr))->next = tmp; + pNodeInfo->num_old_objstr++; + // Update maxsize of prefetch objstr blocks + if(pNodeInfo->maxsize < tmp->size) + pNodeInfo->maxsize = tmp->size; + return (void *)(((unsigned int)tmp) + sizeof(objstr_t)); +} diff --git a/Robust/src/Runtime/DSTM/interface/gCollect.h b/Robust/src/Runtime/DSTM/interface/gCollect.h index 4ffc9a29..f45075a1 100644 --- a/Robust/src/Runtime/DSTM/interface/gCollect.h +++ b/Robust/src/Runtime/DSTM/interface/gCollect.h @@ -2,7 +2,6 @@ #define _GCOLLECT_H #include "dstm.h" -//#include "prelookup.h" /*********************************** ****** Global constants ********** @@ -25,8 +24,9 @@ typedef struct prefetchNodeInfo { void *prefetchobjstrAlloc(unsigned int size); void *normalPrefetchAlloc(objstr_t *, unsigned int); void initializePCache(); -void *lookUpFreeSpace(int); -void *clearNBlocks(void *, void *, unsigned int); +void *lookUpFreeSpace(void *, void *, int); +void clearNBlocks(void *, void *); void clearPLookUpTable(void *, void *); void updatePtrs(); +void *allocateNew(unsigned int size); #endif diff --git a/Robust/src/Runtime/DSTM/interface/ip.c b/Robust/src/Runtime/DSTM/interface/ip.c index bbd8e2f6..9c97347e 100644 --- a/Robust/src/Runtime/DSTM/interface/ip.c +++ b/Robust/src/Runtime/DSTM/interface/ip.c @@ -29,6 +29,9 @@ void midtoIP(unsigned int mid, char *ptr) { i.c = (mid & 0x0000ff00) >> 8; i.d = mid & 0x000000ff; sprintf(ptr, "%d.%d.%d.%d", i.a, i.b, i.c, i.d); +#ifdef DEBUG + printf("DEBUG-> midtoIP() mid = %d.%d.%d.%d\n", i.a, i.b, i.c, i.d); +#endif return; } diff --git a/Robust/src/Runtime/DSTM/interface/objstr.c b/Robust/src/Runtime/DSTM/interface/objstr.c index e7caec56..32bf38de 100644 --- a/Robust/src/Runtime/DSTM/interface/objstr.c +++ b/Robust/src/Runtime/DSTM/interface/objstr.c @@ -1,5 +1,4 @@ #include "dstm.h" -extern objstr_t *prefetchcache; objstr_t *objstrCreate(unsigned int size) { objstr_t *tmp; @@ -36,20 +35,20 @@ void *objstrAlloc(objstr_t *store, unsigned int size) { if (store->next == NULL) { //end of list, all full if (size > DEFAULT_OBJ_STORE_SIZE) { - //in case of large objects - if((store->next = (objstr_t *)calloc(1,(sizeof(objstr_t) + size))) == NULL) { - printf("%s() Calloc error at line %d, %s\n", __func__, __LINE__, __FILE__); - return NULL; - } - store = store->next; - store->size = size; + //in case of large objects + if((store->next = (objstr_t *)calloc(1,(sizeof(objstr_t) + size))) == NULL) { + printf("%s() Calloc error at line %d, %s\n", __func__, __LINE__, __FILE__); + return NULL; + } + store = store->next; + store->size = size; } else { - if((store->next = calloc(1,(sizeof(objstr_t) + DEFAULT_OBJ_STORE_SIZE))) == NULL) { - printf("%s() Calloc error at line %d, %s\n", __func__, __LINE__, __FILE__); - return NULL; - } - store = store->next; - store->size = DEFAULT_OBJ_STORE_SIZE; + if((store->next = calloc(1,(sizeof(objstr_t) + DEFAULT_OBJ_STORE_SIZE))) == NULL) { + printf("%s() Calloc error at line %d, %s\n", __func__, __LINE__, __FILE__); + return NULL; + } + store = store->next; + store->size = DEFAULT_OBJ_STORE_SIZE; } store->top = (void *)(((unsigned int)store) + sizeof(objstr_t) + size); return (void *)(((unsigned int)store) + sizeof(objstr_t)); diff --git a/Robust/src/Runtime/DSTM/interface/trans.c b/Robust/src/Runtime/DSTM/interface/trans.c index c4139fc0..943ea720 100644 --- a/Robust/src/Runtime/DSTM/interface/trans.c +++ b/Robust/src/Runtime/DSTM/interface/trans.c @@ -9,6 +9,7 @@ #include "threadnotify.h" #include "queue.h" #include "addUdpEnhance.h" +#include "gCollect.h" #ifdef COMPILER #include "thread.h" #endif @@ -19,6 +20,7 @@ /* Global Variables */ extern int classsize[]; + objstr_t *prefetchcache; //Global Prefetch cache pthread_mutex_t prefetchcache_mutex;// Mutex to lock Prefetch Cache pthread_mutexattr_t prefetchcache_mutex_attr; /* Attribute for lock to make it a recursive lock */ @@ -44,8 +46,8 @@ pthread_mutex_t atomicObjLock; /*********************************** * Global Variables for statistics **********************************/ -extern int numTransCommit; -extern int numTransAbort; +int numTransCommit = 0; +int numTransAbort = 0; void printhex(unsigned char *, int); plistnode_t *createPiles(transrecord_t *); @@ -92,6 +94,7 @@ int recv_data_errorcode(int fd , void *buf, int buflen) { if (numbytes==0) return 0; if (numbytes == -1) { + perror("recv"); return -1; } buffer += numbytes; @@ -226,6 +229,7 @@ void transInit() { int retval; //Create and initialize prefetch cache structure prefetchcache = objstrCreate(PREFETCH_CACHE_SIZE); + initializePCache(); /* Initialize attributes for mutex */ pthread_mutexattr_init(&prefetchcache_mutex_attr); @@ -588,7 +592,7 @@ int transCommit(transrecord_t *record) { if(treplyctrl == TRANS_ABORT) { #ifdef TRANSSTATS - ++numTransAbort; + numTransAbort++; #endif /* Free Resources */ objstrDelete(record->cache); @@ -599,7 +603,7 @@ int transCommit(transrecord_t *record) { return TRANS_ABORT; } else if(treplyctrl == TRANS_COMMIT) { #ifdef TRANSSTATS - ++numTransCommit; + numTransCommit++; #endif /* Free Resources */ objstrDelete(record->cache); @@ -642,7 +646,7 @@ void *transRequest(void *threadarg) { /* Open Connection */ if (connect(sd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { - perror("Error in connect for TRANS_REQUEST\n"); + perror("transRequest() connect"); close(sd); pthread_exit(NULL); } @@ -679,7 +683,7 @@ void *transRequest(void *threadarg) { recv_data(sd, &length, sizeof(int)); void *newAddr; pthread_mutex_lock(&prefetchcache_mutex); - if ((newAddr = objstrAlloc(prefetchcache, length)) == NULL) { + if ((newAddr = prefetchobjstrAlloc((unsigned int)length)) == NULL) { printf("Error: %s() objstrAlloc error for copying into prefetch cache %s, %d\n", __func__, __FILE__, __LINE__); close(sd); pthread_exit(NULL); @@ -841,8 +845,9 @@ int updatePrefetchCache(thread_data_array_t* tdata, int numoid, char oidType) { header = (objheader_t *) chashSearch(tdata->rec->lookupTable, oid); //copy object into prefetch cache GETSIZE(size, header); - if ((newAddr = objstrAlloc(prefetchcache, (size + sizeof(objheader_t)))) == NULL) { + if ((newAddr = prefetchobjstrAlloc(size + sizeof(objheader_t))) == NULL) { printf("Error: %s() objstrAlloc error for copying into prefetch cache %s, %d\n", __func__, __FILE__, __LINE__); + pthread_mutex_unlock(&prefetchcache_mutex); return -1; } pthread_mutex_unlock(&prefetchcache_mutex); @@ -1320,10 +1325,9 @@ int getPrefetchResponse(int sd) { control = *((char *) recvbuffer); if(control == OBJECT_FOUND) { oid = *((unsigned int *)(recvbuffer + sizeof(char))); - //printf("oid %d found\n",oid); size = size - (sizeof(char) + sizeof(unsigned int)); pthread_mutex_lock(&prefetchcache_mutex); - if ((modptr = objstrAlloc(prefetchcache, size)) == NULL) { + if ((modptr = prefetchobjstrAlloc(size)) == NULL) { printf("Error: objstrAlloc error for copying into prefetch cache %s, %d\n", __FILE__, __LINE__); pthread_mutex_unlock(&prefetchcache_mutex); return -1;