3 #include "structdefs.h"
5 #include "SimpleHash.h"
7 #include "GenericHashtable.h"
9 #if defined(THREADS) || defined(DSTM) || defined(STM)
18 #include <DSTM/interface_recovery/dstm.h>
20 #include <DSTM/interface/dstm.h>
27 #include "delaycomp.h"
33 #define INITIALHEAPSIZE 256*1024*1024L
34 #define GCPOINT(x) ((INTPTR)((x)*0.99))
35 /* This define takes in how full the heap is initially and returns a new heap size to use */
36 #define HEAPSIZE(x,y) ((INTPTR)(x+y))*2
39 extern struct genhashtable * activetasks;
41 extern struct parameterwrapper * objectqueues[NUMCLASSES];
43 extern struct genhashtable * failedtasks;
44 extern struct taskparamdescriptor *currtpd;
45 extern struct ctable *forward;
46 extern struct ctable *reverse;
47 extern struct RuntimeHash *fdtoobject;
52 long garbagearray[MAXSTATS];
55 #if defined(THREADS) || defined(DSTM) || defined(STM)
57 struct listitem * list=NULL;
60 __thread struct listitem litem;
64 //Need to check if pointers are transaction pointers
65 //this also catches the special flag value of 1 for local copies
67 #define ENQUEUE(orig, dst) \
68 if ((!(((unsigned int)orig)&0x1))) { \
69 if (orig>=curr_heapbase&&orig<curr_heaptop) { \
71 if (gc_createcopy(orig,©)) \
77 #define ENQUEUE(orig, dst) \
78 if (orig>=curr_heapbase&&orig<curr_heaptop) { \
80 if (gc_createcopy(orig,©)) \
84 #define SENQUEUE(orig, dst) \
87 if (gc_createcopy(orig,©)) \
91 #elif defined(FASTCHECK)
92 #define ENQUEUE(orig, dst) \
93 if (((unsigned int)orig)!=1) { \
95 if (gc_createcopy(orig,©)) \
99 #define ENQUEUE(orig, dst) \
101 if (gc_createcopy(orig,©)) \
106 struct pointerblock {
107 void * ptrs[NUMPTRS];
108 struct pointerblock *next;
111 void * curr_heapbase=0;
112 void * curr_heapptr=0;
113 void * curr_heapgcpoint=0;
114 void * curr_heaptop=0;
116 void * to_heapbase=0;
121 struct pointerblock *head=NULL;
123 struct pointerblock *tail=NULL;
125 struct pointerblock *spare=NULL;
127 void enqueue(void *ptr) {
128 if (headindex==NUMPTRS) {
129 struct pointerblock * tmp;
134 tmp=malloc(sizeof(struct pointerblock));
139 head->ptrs[headindex++]=ptr;
143 if (tailindex==NUMPTRS) {
144 struct pointerblock *tmp=tail;
152 return tail->ptrs[tailindex++];
156 void fixobjlist(struct objlist * ptr) {
159 for(i=0;i<ptr->offset;i++) {
160 SENQUEUE(ptr->objs[i], ptr->objs[i]);
166 void fixtable(chashlistnode_t ** tc_table, chashlistnode_t **tc_list, cliststruct_t **cstr, unsigned int tc_size) {
167 unsigned int mask=(tc_size<<4)-1;
168 chashlistnode_t *node=calloc(tc_size, sizeof(chashlistnode_t));
169 chashlistnode_t *ptr=*tc_table;
170 chashlistnode_t *curr;
174 chashlistnode_t *newlist=NULL;
175 for(i=0;i<tc_size;i++) {
178 do { //Inner loop to go through linked lists
180 chashlistnode_t *tmp,*next;
182 if ((key=(void *)curr->key) == 0) { //Exit inner loop if there the first element is 0
183 break; //key = val =0 for element if not present within the hash table
186 if (curr->val>=curr_heapbase&&curr->val<curr_heaptop) {
187 SENQUEUE(curr->val, curr->val);
189 //rewrite transaction cache entry
190 void *vptr=curr->val;
191 int type=((int *)vptr)[0];
192 unsigned INTPTR *pointer=pointerarray[type];
194 //array of primitives - do nothing
195 struct ArrayObject *ao=(struct ArrayObject *) vptr;
196 SENQUEUE((void *)ao->___objlocation___, *((void **)&ao->___objlocation___));
197 } else if (((INTPTR)pointer)==1) {
199 struct ArrayObject *ao=(struct ArrayObject *) vptr;
200 int length=ao->___length___;
202 SENQUEUE((void *)ao->___objlocation___, *((void **)&ao->___objlocation___));
204 int lowindex=(ao->lowindex)/sizeof(void *);
205 int highindex=(ao->highindex+INDEXLENGTH)/sizeof(void *);
206 for(i=lowindex; i<highindex; i++) {
208 for(i=0; i<length; i++) {
210 void *objptr=((void **)(((char *)&ao->___length___)+sizeof(int)))[i];
211 SENQUEUE(objptr, ((void **)(((char *)&ao->___length___)+sizeof(int)))[i]);
214 INTPTR size=pointer[0];
216 for(i=1; i<=size; i++) {
217 unsigned int offset=pointer[i];
218 void * objptr=*((void **)(((char *)vptr)+offset));
219 SENQUEUE(objptr, *((void **)(((char *)vptr)+offset)));
225 index = (((unsigned INTPTR)key) & mask) >>4;
229 // Insert into the new table
231 tmp->key = curr->key;
232 tmp->val = curr->val;
235 } else if (isfirst) {
236 chashlistnode_t *newnode;
237 if ((*cstr)->num<NUMCLIST) {
238 newnode=&(*cstr)->array[(*cstr)->num];
242 cliststruct_t *tcl=calloc(1,sizeof(cliststruct_t));
245 newnode=&tcl->array[0];
248 newnode->key = curr->key;
249 newnode->val = curr->val;
250 newnode->next = tmp->next;
251 newnode->lnext=newlist;
257 curr->next=tmp->next;
271 if ((head==tail)&&(tailindex==headindex))
277 struct pointerblock *taghead=NULL;
280 void enqueuetag(struct ___TagDescriptor___ *ptr) {
281 if (tagindex==NUMPTRS) {
282 struct pointerblock * tmp=malloc(sizeof(struct pointerblock));
287 taghead->ptrs[tagindex++]=ptr;
291 #if defined(STM)||defined(THREADS)
292 __thread char * memorybase=NULL;
293 __thread char * memorytop=NULL;
297 void collect(struct garbagelist * stackptr) {
298 #if defined(THREADS)||defined(DSTM)||defined(STM)
300 pthread_mutex_lock(&gclistlock);
302 if ((listcount+1)==threadcount) {
303 break; /* Have all other threads stopped */
305 pthread_cond_wait(&gccond, &gclistlock);
309 ptrstack.prev=stackptr;
310 stackptr=(struct garbagelist *) &ptrstack;
312 arraystack.prev=stackptr;
313 stackptr=(struct garbagelist *) &arraystack;
320 for(i=0;i<MAXSTATS;i++)
328 head=tail=malloc(sizeof(struct pointerblock));
334 taghead=malloc(sizeof(struct pointerblock));
341 fixtable(&c_table, &c_list, &c_structs, c_size);
344 fixobjlist(lockedobjs);
350 /* Check current stack */
351 #if defined(THREADS)||defined(DSTM)||defined(STM)
353 struct listitem *listptr=list;
357 while(stackptr!=NULL) {
359 for(i=0; i<stackptr->size; i++) {
360 void * orig=stackptr->array[i];
361 ENQUEUE(orig, stackptr->array[i]);
363 stackptr=stackptr->next;
365 #if defined(THREADS)||defined(DSTM)||defined(STM)
366 /* Go to next thread */
369 if (listptr==&litem) {
370 listptr=listptr->next;
374 struct listitem *litem=pthread_getspecific(litemkey);
375 if (listptr==litem) {
376 listptr=listptr->next;
383 void * orig=listptr->locklist;
384 ENQUEUE(orig, listptr->locklist);
387 if ((*listptr->tc_table)!=NULL) {
388 fixtable(listptr->tc_table, listptr->tc_list, listptr->tc_structs, listptr->tc_size);
389 fixobjlist(listptr->objlist);
391 fixobjlist(listptr->lockedlist);
394 *(listptr->base)=NULL;
396 stackptr=listptr->stackptr;
397 listptr=listptr->next;
405 ENQUEUE(___fcrevert___, ___fcrevert___);
410 /* Update objectsets */
412 for(i=0; i<NUMCLASSES; i++) {
413 #if !defined(MULTICORE)
414 struct parameterwrapper * p=objectqueues[i];
416 struct ObjectHash * set=p->objectset;
417 struct ObjectNode * ptr=set->listhead;
419 void *orig=(void *)ptr->key;
420 ENQUEUE(orig, *((void **)(&ptr->key)));
423 ObjectHashrehash(set); /* Rehash the table */
432 struct cnode * ptr=forward->listhead;
434 void * orig=(void *)ptr->key;
435 ENQUEUE(orig, *((void **)(&ptr->key)));
438 crehash(forward); /* Rehash the table */
442 struct cnode * ptr=reverse->listhead;
444 void *orig=(void *)ptr->val;
445 ENQUEUE(orig, *((void**)(&ptr->val)));
452 struct RuntimeNode * ptr=fdtoobject->listhead;
454 void *orig=(void *)ptr->data;
455 ENQUEUE(orig, *((void**)(&ptr->data)));
461 /* Update current task descriptor */
463 for(i=0; i<currtpd->numParameters; i++) {
464 void *orig=currtpd->parameterArray[i];
465 ENQUEUE(orig, currtpd->parameterArray[i]);
470 /* Update active tasks */
472 struct genpointerlist * ptr=activetasks->list;
474 struct taskparamdescriptor *tpd=ptr->src;
476 for(i=0; i<tpd->numParameters; i++) {
477 void * orig=tpd->parameterArray[i];
478 ENQUEUE(orig, tpd->parameterArray[i]);
482 genrehash(activetasks);
485 /* Update failed tasks */
487 struct genpointerlist * ptr=failedtasks->list;
489 struct taskparamdescriptor *tpd=ptr->src;
491 for(i=0; i<tpd->numParameters; i++) {
492 void * orig=tpd->parameterArray[i];
493 ENQUEUE(orig, tpd->parameterArray[i]);
497 genrehash(failedtasks);
502 void * ptr=dequeue();
504 int type=((int *)cpy)[0];
505 unsigned INTPTR * pointer;
509 /* Nothing is inside */
514 pointer=pointerarray[type];
516 /* Array of primitives */
518 #if defined(DSTM)||defined(FASTCHECK)
519 struct ArrayObject *ao=(struct ArrayObject *) ptr;
520 struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
521 ENQUEUE((void *)ao->___nextobject___, *((void **)&ao_cpy->___nextobject___));
522 ENQUEUE((void *)ao->___localcopy___, *((void **)&ao_cpy->___localcopy___));
525 struct ArrayObject *ao=(struct ArrayObject *) ptr;
526 struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
527 SENQUEUE((void *)ao->___objlocation___, *((void **)&ao_cpy->___objlocation___));
529 } else if (((INTPTR)pointer)==1) {
530 /* Array of pointers */
531 struct ArrayObject *ao=(struct ArrayObject *) ptr;
532 struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
533 #if (defined(DSTM)||defined(FASTCHECK))
534 ENQUEUE((void *)ao->___nextobject___, *((void **)&ao_cpy->___nextobject___));
535 ENQUEUE((void *)ao->___localcopy___, *((void **)&ao_cpy->___localcopy___));
538 SENQUEUE((void *)ao->___objlocation___, *((void **)&ao_cpy->___objlocation___));
540 int length=ao->___length___;
542 for(i=0; i<length; i++) {
543 void *objptr=((void **)(((char *)&ao->___length___)+sizeof(int)))[i];
544 ENQUEUE(objptr, ((void **)(((char *)&ao_cpy->___length___)+sizeof(int)))[i]);
547 INTPTR size=pointer[0];
549 for(i=1; i<=size; i++) {
550 unsigned int offset=pointer[i];
551 void * objptr=*((void **)(((char *)ptr)+offset));
552 ENQUEUE(objptr, *((void **)(((char *)cpy)+offset)));
560 #if defined(THREADS)||defined(DSTM)||defined(STM)
562 pthread_mutex_unlock(&gclistlock);
567 /* Fix up the references from tags. This can't be done earlier,
568 because we don't want tags to keep objects alive */
570 while(taghead!=NULL) {
572 struct pointerblock *tmp=taghead->next;
573 for(i=0; i<tagindex; i++) {
574 struct ___TagDescriptor___ *tagd=taghead->ptrs[i];
575 struct ___Object___ *obj=tagd->flagptr;
576 struct ___TagDescriptor___ *copy=((struct ___TagDescriptor___**)tagd)[1];
578 /* Zero object case */
579 } else if (obj->type==-1) {
580 /* Single object case */
581 copy->flagptr=((struct ___Object___**)obj)[1];
582 } else if (obj->type==OBJECTARRAYTYPE) {
584 struct ArrayObject *ao=(struct ArrayObject *) obj;
588 struct ArrayObject *aonew;
590 /* Count live objects */
591 for(j=0; j<ao->___cachedCode___; j++) {
592 struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, j);
597 livecount=((livecount-1)/OBJECTARRAYINTERVAL+1)*OBJECTARRAYINTERVAL;
598 aonew=(struct ArrayObject *) tomalloc(sizeof(struct ArrayObject)+sizeof(struct ___Object___*)*livecount);
599 memcpy(aonew, ao, sizeof(struct ArrayObject));
600 aonew->type=OBJECTARRAYTYPE;
601 aonew->___length___=livecount;
603 for(j=0; j<ao->___cachedCode___; j++) {
604 struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, j);
605 if (tobj->type==-1) {
606 struct ___Object___ * tobjcpy=((struct ___Object___**)tobj)[1];
607 ARRAYSET(aonew, struct ___Object___*, k++,tobjcpy);
610 aonew->___cachedCode___=k;
611 for(; k<livecount; k++) {
612 ARRAYSET(aonew, struct ___Object___*, k, NULL);
615 /* No object live anymore */
626 void * tomalloc(int size) {
627 void * ptr=to_heapptr;
634 #if defined(THREADS)||defined(DSTM)||defined(STM)
635 void checkcollect(void * ptr) {
636 stopforgc((struct garbagelist *)ptr);
641 void checkcollect2(void * ptr) {
642 int ptrarray[]={1, (int)ptr, (int) revertlist};
643 stopforgc((struct garbagelist *)ptrarray);
645 revertlist=(struct ___Object___*)ptrarray[2];
649 void stopforgc(struct garbagelist * ptr) {
651 //just append us to the list
653 ptr=(struct garbagelist *) &ptrstack;
656 ptr=(struct garbagelist *) &arraystack;
662 litem.locklist=pthread_getspecific(threadlocks);
665 litem.tc_size=c_size;
666 litem.tc_table=&c_table;
667 litem.tc_list=&c_list;
668 litem.tc_structs=&c_structs;
669 litem.objlist=newobjs;
671 litem.lockedlist=lockedobjs;
673 litem.base=&memorybase;
677 struct listitem *litem=pthread_getspecific(litemkey);
680 litem->locklist=pthread_getspecific(threadlocks);
683 pthread_mutex_lock(&gclistlock);
685 if ((listcount+1)==threadcount) {
686 //only do wakeup if we are ready to GC
687 pthread_cond_signal(&gccond);
689 pthread_mutex_unlock(&gclistlock);
692 void restartaftergc() {
694 pthread_mutex_lock(&gclock); // Wait for GC
695 pthread_mutex_unlock(&gclock);
697 pthread_mutex_lock(&gclistlock);
699 pthread_mutex_unlock(&gclistlock);
703 #if defined(STM)||defined(THREADS)
704 #define MEMORYBLOCK 65536
705 void * helper(struct garbagelist *, int);
706 void * mygcmalloc(struct garbagelist * stackptr, int size) {
709 if (memorybase==NULL||size>(memorytop-memorybase)) {
710 int toallocate=(size>MEMORYBLOCK)?size:MEMORYBLOCK;
711 memorybase=helper(stackptr, toallocate);
712 memorytop=memorybase+toallocate;
714 char *retvalue=memorybase;
719 void * helper(struct garbagelist * stackptr, int size) {
721 void * mygcmalloc(struct garbagelist * stackptr, int size) {
724 #if defined(THREADS)||defined(DSTM)||defined(STM)
725 while (pthread_mutex_trylock(&gclock)!=0) {
734 if (curr_heapptr>curr_heapgcpoint||curr_heapptr<curr_heapbase) {
735 if (curr_heapbase==0) {
736 /* Need to allocate base heap */
737 curr_heapbase=malloc(INITIALHEAPSIZE);
738 if (curr_heapbase==NULL) {
739 printf("malloc failed. Garbage collector couldn't get enough memory. Try changing heap size.\n");
742 bzero(curr_heapbase, INITIALHEAPSIZE);
743 curr_heaptop=curr_heapbase+INITIALHEAPSIZE;
744 curr_heapgcpoint=((char *) curr_heapbase)+GCPOINT(INITIALHEAPSIZE);
745 curr_heapptr=curr_heapbase+size;
747 to_heapbase=malloc(INITIALHEAPSIZE);
748 if (to_heapbase==NULL) {
749 printf("malloc failed. Garbage collector couldn't get enough memory. Try changing heap size.\n");
752 to_heaptop=to_heapbase+INITIALHEAPSIZE;
753 to_heapptr=to_heapbase;
755 #if defined(THREADS)||defined(DSTM)||defined(STM)
756 pthread_mutex_unlock(&gclock);
761 /* Grow the to heap if necessary */
763 INTPTR curr_heapsize=curr_heaptop-curr_heapbase;
764 INTPTR to_heapsize=to_heaptop-to_heapbase;
765 INTPTR last_heapsize=0;
767 last_heapsize=HEAPSIZE(lastgcsize, size);
768 if ((last_heapsize&7)!=0)
769 last_heapsize+=(8-(last_heapsize%8));
771 if (curr_heapsize>last_heapsize)
772 last_heapsize=curr_heapsize;
773 if (last_heapsize>to_heapsize) {
775 to_heapbase=malloc(last_heapsize);
776 if (to_heapbase==NULL) {
777 printf("Error Allocating enough memory\n");
780 to_heaptop=to_heapbase+last_heapsize;
781 to_heapptr=to_heapbase;
785 /* Do our collection */
788 /* Update stat on previous gc size */
789 lastgcsize=(to_heapptr-to_heapbase)+size;
792 printf("Garbage collected: Old bytes: %u\n", curr_heapptr-curr_heapbase);
793 printf("New space: %u\n", to_heapptr-to_heapbase);
794 printf("Total space: %u\n", to_heaptop-to_heapbase);
797 for(i=0;i<MAXSTATS;i++) {
798 if (garbagearray[i]!=0)
799 printf("Type=%d Size=%u\n", i, garbagearray[i]);
803 /* Flip to/curr heaps */
805 void * tmp=to_heapbase;
806 to_heapbase=curr_heapbase;
810 to_heaptop=curr_heaptop;
814 curr_heapptr=to_heapptr+size;
815 curr_heapgcpoint=((char *) curr_heapbase)+GCPOINT(curr_heaptop-curr_heapbase);
816 to_heapptr=to_heapbase;
818 /* Not enough room :(, redo gc */
819 if (curr_heapptr>curr_heapgcpoint) {
820 #if defined(THREADS)||defined(DSTM)||defined(STM)
821 pthread_mutex_unlock(&gclock);
823 return mygcmalloc(stackptr, size);
826 bzero(tmp, curr_heaptop-tmp);
827 #if defined(THREADS)||defined(DSTM)||defined(STM)
828 pthread_mutex_unlock(&gclock);
833 #if defined(THREADS)||defined(DSTM)||defined(STM)
834 pthread_mutex_unlock(&gclock);
840 int gc_createcopy(void * orig, void ** copy_ptr) {
845 int type=((int *)orig)[0];
847 *copy_ptr=((void **)orig)[1];
850 if (type<NUMCLASSES) {
851 /* We have a normal object */
853 int size=classsize[type]+sizeof(objheader_t);
854 void *newobj=tomalloc(size);
855 memcpy(newobj,((char *) orig)-sizeof(objheader_t), size);
856 newobj=((char *)newobj)+sizeof(objheader_t);
858 int size=classsize[type];
859 void *newobj=tomalloc(size);
860 memcpy(newobj, orig, size);
863 garbagearray[type]+=size;
866 ((void **)orig)[1]=newobj;
870 /* We have an array */
871 struct ArrayObject *ao=(struct ArrayObject *)orig;
872 int elementsize=classsize[type];
873 int length=ao->___length___;
876 int basesize=length*elementsize;
877 basesize=(basesize+LOWMASK)&HIGHMASK;
878 int versionspace=sizeof(int)*2*(basesize>>INDEXSHIFT);
879 int size=sizeof(struct ArrayObject)+basesize+sizeof(objheader_t)+versionspace;
880 void *newobj=tomalloc(size);
881 memcpy(newobj, ((char*)orig)-sizeof(objheader_t)-versionspace, size);
882 newobj=((char *)newobj)+sizeof(objheader_t)+versionspace;
884 int size=sizeof(struct ArrayObject)+length*elementsize+sizeof(objheader_t);
885 void *newobj=tomalloc(size);
886 memcpy(newobj, ((char*)orig)-sizeof(objheader_t), size);
887 newobj=((char *)newobj)+sizeof(objheader_t);
890 int size=sizeof(struct ArrayObject)+length*elementsize;
891 void *newobj=tomalloc(size);
892 memcpy(newobj, orig, size);
895 garbagearray[type]+=size;
898 ((void **)orig)[1]=newobj;