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___));
203 for(i=0; i<length; i++) {
204 void *objptr=((void **)(((char *)&ao->___length___)+sizeof(int)))[i];
205 SENQUEUE(objptr, ((void **)(((char *)&ao->___length___)+sizeof(int)))[i]);
208 INTPTR size=pointer[0];
210 for(i=1; i<=size; i++) {
211 unsigned int offset=pointer[i];
212 void * objptr=*((void **)(((char *)vptr)+offset));
213 SENQUEUE(objptr, *((void **)(((char *)vptr)+offset)));
219 index = (((unsigned INTPTR)key) & mask) >>4;
223 // Insert into the new table
225 tmp->key = curr->key;
226 tmp->val = curr->val;
229 } else if (isfirst) {
230 chashlistnode_t *newnode;
231 if ((*cstr)->num<NUMCLIST) {
232 newnode=&(*cstr)->array[(*cstr)->num];
236 cliststruct_t *tcl=calloc(1,sizeof(cliststruct_t));
239 newnode=&tcl->array[0];
242 newnode->key = curr->key;
243 newnode->val = curr->val;
244 newnode->next = tmp->next;
245 newnode->lnext=newlist;
251 curr->next=tmp->next;
265 if ((head==tail)&&(tailindex==headindex))
271 struct pointerblock *taghead=NULL;
274 void enqueuetag(struct ___TagDescriptor___ *ptr) {
275 if (tagindex==NUMPTRS) {
276 struct pointerblock * tmp=malloc(sizeof(struct pointerblock));
281 taghead->ptrs[tagindex++]=ptr;
285 #if defined(STM)||defined(THREADS)
286 __thread char * memorybase=NULL;
287 __thread char * memorytop=NULL;
291 void collect(struct garbagelist * stackptr) {
292 #if defined(THREADS)||defined(DSTM)||defined(STM)
294 pthread_mutex_lock(&gclistlock);
296 if ((listcount+1)==threadcount) {
297 break; /* Have all other threads stopped */
299 pthread_cond_wait(&gccond, &gclistlock);
303 ptrstack.prev=stackptr;
304 stackptr=(struct garbagelist *) &ptrstack;
306 arraystack.prev=stackptr;
307 stackptr=(struct garbagelist *) &arraystack;
314 for(i=0;i<MAXSTATS;i++)
322 head=tail=malloc(sizeof(struct pointerblock));
328 taghead=malloc(sizeof(struct pointerblock));
335 fixtable(&c_table, &c_list, &c_structs, c_size);
338 fixobjlist(lockedobjs);
344 /* Check current stack */
345 #if defined(THREADS)||defined(DSTM)||defined(STM)
347 struct listitem *listptr=list;
351 while(stackptr!=NULL) {
353 for(i=0; i<stackptr->size; i++) {
354 void * orig=stackptr->array[i];
355 ENQUEUE(orig, stackptr->array[i]);
357 stackptr=stackptr->next;
359 #if defined(THREADS)||defined(DSTM)||defined(STM)
360 /* Go to next thread */
363 if (listptr==&litem) {
364 listptr=listptr->next;
368 struct listitem *litem=pthread_getspecific(litemkey);
369 if (listptr==litem) {
370 listptr=listptr->next;
377 void * orig=listptr->locklist;
378 ENQUEUE(orig, listptr->locklist);
381 if ((*listptr->tc_table)!=NULL) {
382 fixtable(listptr->tc_table, listptr->tc_list, listptr->tc_structs, listptr->tc_size);
383 fixobjlist(listptr->objlist);
385 fixobjlist(listptr->lockedlist);
388 *(listptr->base)=NULL;
390 stackptr=listptr->stackptr;
391 listptr=listptr->next;
399 ENQUEUE(___fcrevert___, ___fcrevert___);
404 /* Update objectsets */
406 for(i=0; i<NUMCLASSES; i++) {
407 #if !defined(MULTICORE)
408 struct parameterwrapper * p=objectqueues[i];
410 struct ObjectHash * set=p->objectset;
411 struct ObjectNode * ptr=set->listhead;
413 void *orig=(void *)ptr->key;
414 ENQUEUE(orig, *((void **)(&ptr->key)));
417 ObjectHashrehash(set); /* Rehash the table */
426 struct cnode * ptr=forward->listhead;
428 void * orig=(void *)ptr->key;
429 ENQUEUE(orig, *((void **)(&ptr->key)));
432 crehash(forward); /* Rehash the table */
436 struct cnode * ptr=reverse->listhead;
438 void *orig=(void *)ptr->val;
439 ENQUEUE(orig, *((void**)(&ptr->val)));
446 struct RuntimeNode * ptr=fdtoobject->listhead;
448 void *orig=(void *)ptr->data;
449 ENQUEUE(orig, *((void**)(&ptr->data)));
455 /* Update current task descriptor */
457 for(i=0; i<currtpd->numParameters; i++) {
458 void *orig=currtpd->parameterArray[i];
459 ENQUEUE(orig, currtpd->parameterArray[i]);
464 /* Update active tasks */
466 struct genpointerlist * ptr=activetasks->list;
468 struct taskparamdescriptor *tpd=ptr->src;
470 for(i=0; i<tpd->numParameters; i++) {
471 void * orig=tpd->parameterArray[i];
472 ENQUEUE(orig, tpd->parameterArray[i]);
476 genrehash(activetasks);
479 /* Update failed tasks */
481 struct genpointerlist * ptr=failedtasks->list;
483 struct taskparamdescriptor *tpd=ptr->src;
485 for(i=0; i<tpd->numParameters; i++) {
486 void * orig=tpd->parameterArray[i];
487 ENQUEUE(orig, tpd->parameterArray[i]);
491 genrehash(failedtasks);
496 void * ptr=dequeue();
498 int type=((int *)cpy)[0];
499 unsigned INTPTR * pointer;
503 /* Nothing is inside */
508 pointer=pointerarray[type];
510 /* Array of primitives */
512 #if defined(DSTM)||defined(FASTCHECK)
513 struct ArrayObject *ao=(struct ArrayObject *) ptr;
514 struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
515 ENQUEUE((void *)ao->___nextobject___, *((void **)&ao_cpy->___nextobject___));
516 ENQUEUE((void *)ao->___localcopy___, *((void **)&ao_cpy->___localcopy___));
519 struct ArrayObject *ao=(struct ArrayObject *) ptr;
520 struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
521 SENQUEUE((void *)ao->___objlocation___, *((void **)&ao_cpy->___objlocation___));
523 } else if (((INTPTR)pointer)==1) {
524 /* Array of pointers */
525 struct ArrayObject *ao=(struct ArrayObject *) ptr;
526 struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
527 #if (defined(DSTM)||defined(FASTCHECK))
528 ENQUEUE((void *)ao->___nextobject___, *((void **)&ao_cpy->___nextobject___));
529 ENQUEUE((void *)ao->___localcopy___, *((void **)&ao_cpy->___localcopy___));
532 SENQUEUE((void *)ao->___objlocation___, *((void **)&ao_cpy->___objlocation___));
534 int length=ao->___length___;
536 for(i=0; i<length; i++) {
537 void *objptr=((void **)(((char *)&ao->___length___)+sizeof(int)))[i];
538 ENQUEUE(objptr, ((void **)(((char *)&ao_cpy->___length___)+sizeof(int)))[i]);
541 INTPTR size=pointer[0];
543 for(i=1; i<=size; i++) {
544 unsigned int offset=pointer[i];
545 void * objptr=*((void **)(((char *)ptr)+offset));
546 ENQUEUE(objptr, *((void **)(((char *)cpy)+offset)));
554 #if defined(THREADS)||defined(DSTM)||defined(STM)
556 pthread_mutex_unlock(&gclistlock);
561 /* Fix up the references from tags. This can't be done earlier,
562 because we don't want tags to keep objects alive */
564 while(taghead!=NULL) {
566 struct pointerblock *tmp=taghead->next;
567 for(i=0; i<tagindex; i++) {
568 struct ___TagDescriptor___ *tagd=taghead->ptrs[i];
569 struct ___Object___ *obj=tagd->flagptr;
570 struct ___TagDescriptor___ *copy=((struct ___TagDescriptor___**)tagd)[1];
572 /* Zero object case */
573 } else if (obj->type==-1) {
574 /* Single object case */
575 copy->flagptr=((struct ___Object___**)obj)[1];
576 } else if (obj->type==OBJECTARRAYTYPE) {
578 struct ArrayObject *ao=(struct ArrayObject *) obj;
582 struct ArrayObject *aonew;
584 /* Count live objects */
585 for(j=0; j<ao->___cachedCode___; j++) {
586 struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, j);
591 livecount=((livecount-1)/OBJECTARRAYINTERVAL+1)*OBJECTARRAYINTERVAL;
592 aonew=(struct ArrayObject *) tomalloc(sizeof(struct ArrayObject)+sizeof(struct ___Object___*)*livecount);
593 memcpy(aonew, ao, sizeof(struct ArrayObject));
594 aonew->type=OBJECTARRAYTYPE;
595 aonew->___length___=livecount;
597 for(j=0; j<ao->___cachedCode___; j++) {
598 struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, j);
599 if (tobj->type==-1) {
600 struct ___Object___ * tobjcpy=((struct ___Object___**)tobj)[1];
601 ARRAYSET(aonew, struct ___Object___*, k++,tobjcpy);
604 aonew->___cachedCode___=k;
605 for(; k<livecount; k++) {
606 ARRAYSET(aonew, struct ___Object___*, k, NULL);
609 /* No object live anymore */
620 void * tomalloc(int size) {
621 void * ptr=to_heapptr;
628 #if defined(THREADS)||defined(DSTM)||defined(STM)
629 void checkcollect(void * ptr) {
630 stopforgc((struct garbagelist *)ptr);
635 void checkcollect2(void * ptr) {
636 int ptrarray[]={1, (int)ptr, (int) revertlist};
637 stopforgc((struct garbagelist *)ptrarray);
639 revertlist=(struct ___Object___*)ptrarray[2];
643 void stopforgc(struct garbagelist * ptr) {
645 //just append us to the list
647 ptr=(struct garbagelist *) &ptrstack;
650 ptr=(struct garbagelist *) &arraystack;
656 litem.locklist=pthread_getspecific(threadlocks);
659 litem.tc_size=c_size;
660 litem.tc_table=&c_table;
661 litem.tc_list=&c_list;
662 litem.tc_structs=&c_structs;
663 litem.objlist=newobjs;
665 litem.lockedlist=lockedobjs;
667 litem.base=&memorybase;
671 struct listitem *litem=pthread_getspecific(litemkey);
674 litem->locklist=pthread_getspecific(threadlocks);
677 pthread_mutex_lock(&gclistlock);
679 if ((listcount+1)==threadcount) {
680 //only do wakeup if we are ready to GC
681 pthread_cond_signal(&gccond);
683 pthread_mutex_unlock(&gclistlock);
686 void restartaftergc() {
688 pthread_mutex_lock(&gclock); // Wait for GC
689 pthread_mutex_unlock(&gclock);
691 pthread_mutex_lock(&gclistlock);
693 pthread_mutex_unlock(&gclistlock);
697 #if defined(STM)||defined(THREADS)
698 #define MEMORYBLOCK 65536
699 void * helper(struct garbagelist *, int);
700 void * mygcmalloc(struct garbagelist * stackptr, int size) {
703 if (memorybase==NULL||size>(memorytop-memorybase)) {
704 int toallocate=(size>MEMORYBLOCK)?size:MEMORYBLOCK;
705 memorybase=helper(stackptr, toallocate);
706 memorytop=memorybase+toallocate;
708 char *retvalue=memorybase;
713 void * helper(struct garbagelist * stackptr, int size) {
715 void * mygcmalloc(struct garbagelist * stackptr, int size) {
718 #if defined(THREADS)||defined(DSTM)||defined(STM)
719 while (pthread_mutex_trylock(&gclock)!=0) {
728 if (curr_heapptr>curr_heapgcpoint||curr_heapptr<curr_heapbase) {
729 if (curr_heapbase==0) {
730 /* Need to allocate base heap */
731 curr_heapbase=malloc(INITIALHEAPSIZE);
732 if (curr_heapbase==NULL) {
733 printf("malloc failed. Garbage collector couldn't get enough memory. Try changing heap size.\n");
736 bzero(curr_heapbase, INITIALHEAPSIZE);
737 curr_heaptop=curr_heapbase+INITIALHEAPSIZE;
738 curr_heapgcpoint=((char *) curr_heapbase)+GCPOINT(INITIALHEAPSIZE);
739 curr_heapptr=curr_heapbase+size;
741 to_heapbase=malloc(INITIALHEAPSIZE);
742 if (to_heapbase==NULL) {
743 printf("malloc failed. Garbage collector couldn't get enough memory. Try changing heap size.\n");
746 to_heaptop=to_heapbase+INITIALHEAPSIZE;
747 to_heapptr=to_heapbase;
749 #if defined(THREADS)||defined(DSTM)||defined(STM)
750 pthread_mutex_unlock(&gclock);
755 /* Grow the to heap if necessary */
757 INTPTR curr_heapsize=curr_heaptop-curr_heapbase;
758 INTPTR to_heapsize=to_heaptop-to_heapbase;
759 INTPTR last_heapsize=0;
761 last_heapsize=HEAPSIZE(lastgcsize, size);
762 if ((last_heapsize&7)!=0)
763 last_heapsize+=(8-(last_heapsize%8));
765 if (curr_heapsize>last_heapsize)
766 last_heapsize=curr_heapsize;
767 if (last_heapsize>to_heapsize) {
769 to_heapbase=malloc(last_heapsize);
770 if (to_heapbase==NULL) {
771 printf("Error Allocating enough memory\n");
774 to_heaptop=to_heapbase+last_heapsize;
775 to_heapptr=to_heapbase;
779 /* Do our collection */
782 /* Update stat on previous gc size */
783 lastgcsize=(to_heapptr-to_heapbase)+size;
786 printf("Garbage collected: Old bytes: %u\n", curr_heapptr-curr_heapbase);
787 printf("New space: %u\n", to_heapptr-to_heapbase);
788 printf("Total space: %u\n", to_heaptop-to_heapbase);
791 for(i=0;i<MAXSTATS;i++) {
792 if (garbagearray[i]!=0)
793 printf("Type=%d Size=%u\n", i, garbagearray[i]);
797 /* Flip to/curr heaps */
799 void * tmp=to_heapbase;
800 to_heapbase=curr_heapbase;
804 to_heaptop=curr_heaptop;
808 curr_heapptr=to_heapptr+size;
809 curr_heapgcpoint=((char *) curr_heapbase)+GCPOINT(curr_heaptop-curr_heapbase);
810 to_heapptr=to_heapbase;
812 /* Not enough room :(, redo gc */
813 if (curr_heapptr>curr_heapgcpoint) {
814 #if defined(THREADS)||defined(DSTM)||defined(STM)
815 pthread_mutex_unlock(&gclock);
817 return mygcmalloc(stackptr, size);
820 bzero(tmp, curr_heaptop-tmp);
821 #if defined(THREADS)||defined(DSTM)||defined(STM)
822 pthread_mutex_unlock(&gclock);
827 #if defined(THREADS)||defined(DSTM)||defined(STM)
828 pthread_mutex_unlock(&gclock);
834 int gc_createcopy(void * orig, void ** copy_ptr) {
839 int type=((int *)orig)[0];
841 *copy_ptr=((void **)orig)[1];
844 if (type<NUMCLASSES) {
845 /* We have a normal object */
847 int size=classsize[type]+sizeof(objheader_t);
848 void *newobj=tomalloc(size);
849 memcpy(newobj,((char *) orig)-sizeof(objheader_t), size);
850 newobj=((char *)newobj)+sizeof(objheader_t);
852 int size=classsize[type];
853 void *newobj=tomalloc(size);
854 memcpy(newobj, orig, size);
857 garbagearray[type]+=size;
860 ((void **)orig)[1]=newobj;
864 /* We have an array */
865 struct ArrayObject *ao=(struct ArrayObject *)orig;
866 int elementsize=classsize[type];
867 int length=ao->___length___;
870 int basesize=length*elementsize;
871 basesize=(basesize+LOWMASK)&HIGHMASK;
872 int versionspace=sizeof(int)*2*(basesize>>INDEXSHIFT);
873 int size=sizeof(struct ArrayObject)+basesize+sizeof(objheader_t)+versionspace;
874 void *newobj=tomalloc(size);
875 memcpy(newobj, ((char*)orig)-sizeof(objheader_t)-versionspace, size);
876 newobj=((char *)newobj)+sizeof(objheader_t)+versionspace;
878 int size=sizeof(struct ArrayObject)+length*elementsize+sizeof(objheader_t);
879 void *newobj=tomalloc(size);
880 memcpy(newobj, ((char*)orig)-sizeof(objheader_t), size);
881 newobj=((char *)newobj)+sizeof(objheader_t);
884 int size=sizeof(struct ArrayObject)+length*elementsize;
885 void *newobj=tomalloc(size);
886 memcpy(newobj, orig, size);
889 garbagearray[type]+=size;
892 ((void **)orig)[1]=newobj;