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;
310 for(i=0;i<MAXSTATS;i++)
318 head=tail=malloc(sizeof(struct pointerblock));
324 taghead=malloc(sizeof(struct pointerblock));
331 fixtable(&c_table, &c_list, &c_structs, c_size);
334 fixobjlist(lockedobjs);
340 /* Check current stack */
341 #if defined(THREADS)||defined(DSTM)||defined(STM)
343 struct listitem *listptr=list;
347 while(stackptr!=NULL) {
349 for(i=0; i<stackptr->size; i++) {
350 void * orig=stackptr->array[i];
351 ENQUEUE(orig, stackptr->array[i]);
353 stackptr=stackptr->next;
355 #if defined(THREADS)||defined(DSTM)||defined(STM)
356 /* Go to next thread */
359 if (listptr==&litem) {
360 listptr=listptr->next;
364 struct listitem *litem=pthread_getspecific(litemkey);
365 if (listptr==litem) {
366 listptr=listptr->next;
373 void * orig=listptr->locklist;
374 ENQUEUE(orig, listptr->locklist);
377 if ((*listptr->tc_table)!=NULL) {
378 fixtable(listptr->tc_table, listptr->tc_list, listptr->tc_structs, listptr->tc_size);
379 fixobjlist(listptr->objlist);
381 fixobjlist(listptr->lockedlist);
384 *(listptr->base)=NULL;
386 stackptr=listptr->stackptr;
387 listptr=listptr->next;
395 ENQUEUE(___fcrevert___, ___fcrevert___);
400 /* Update objectsets */
402 for(i=0; i<NUMCLASSES; i++) {
403 #if !defined(MULTICORE)
404 struct parameterwrapper * p=objectqueues[i];
406 struct ObjectHash * set=p->objectset;
407 struct ObjectNode * ptr=set->listhead;
409 void *orig=(void *)ptr->key;
410 ENQUEUE(orig, *((void **)(&ptr->key)));
413 ObjectHashrehash(set); /* Rehash the table */
422 struct cnode * ptr=forward->listhead;
424 void * orig=(void *)ptr->key;
425 ENQUEUE(orig, *((void **)(&ptr->key)));
428 crehash(forward); /* Rehash the table */
432 struct cnode * ptr=reverse->listhead;
434 void *orig=(void *)ptr->val;
435 ENQUEUE(orig, *((void**)(&ptr->val)));
442 struct RuntimeNode * ptr=fdtoobject->listhead;
444 void *orig=(void *)ptr->data;
445 ENQUEUE(orig, *((void**)(&ptr->data)));
451 /* Update current task descriptor */
453 for(i=0; i<currtpd->numParameters; i++) {
454 void *orig=currtpd->parameterArray[i];
455 ENQUEUE(orig, currtpd->parameterArray[i]);
460 /* Update active tasks */
462 struct genpointerlist * ptr=activetasks->list;
464 struct taskparamdescriptor *tpd=ptr->src;
466 for(i=0; i<tpd->numParameters; i++) {
467 void * orig=tpd->parameterArray[i];
468 ENQUEUE(orig, tpd->parameterArray[i]);
472 genrehash(activetasks);
475 /* Update failed tasks */
477 struct genpointerlist * ptr=failedtasks->list;
479 struct taskparamdescriptor *tpd=ptr->src;
481 for(i=0; i<tpd->numParameters; i++) {
482 void * orig=tpd->parameterArray[i];
483 ENQUEUE(orig, tpd->parameterArray[i]);
487 genrehash(failedtasks);
492 void * ptr=dequeue();
494 int type=((int *)cpy)[0];
495 unsigned INTPTR * pointer;
499 /* Nothing is inside */
504 pointer=pointerarray[type];
506 /* Array of primitives */
508 #if defined(DSTM)||defined(FASTCHECK)
509 struct ArrayObject *ao=(struct ArrayObject *) ptr;
510 struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
511 ENQUEUE((void *)ao->___nextobject___, *((void **)&ao_cpy->___nextobject___));
512 ENQUEUE((void *)ao->___localcopy___, *((void **)&ao_cpy->___localcopy___));
515 struct ArrayObject *ao=(struct ArrayObject *) ptr;
516 struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
517 SENQUEUE((void *)ao->___objlocation___, *((void **)&ao_cpy->___objlocation___));
519 } else if (((INTPTR)pointer)==1) {
520 /* Array of pointers */
521 struct ArrayObject *ao=(struct ArrayObject *) ptr;
522 struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
523 #if (defined(DSTM)||defined(FASTCHECK))
524 ENQUEUE((void *)ao->___nextobject___, *((void **)&ao_cpy->___nextobject___));
525 ENQUEUE((void *)ao->___localcopy___, *((void **)&ao_cpy->___localcopy___));
528 SENQUEUE((void *)ao->___objlocation___, *((void **)&ao_cpy->___objlocation___));
530 int length=ao->___length___;
532 for(i=0; i<length; i++) {
533 void *objptr=((void **)(((char *)&ao->___length___)+sizeof(int)))[i];
534 ENQUEUE(objptr, ((void **)(((char *)&ao_cpy->___length___)+sizeof(int)))[i]);
537 INTPTR size=pointer[0];
539 for(i=1; i<=size; i++) {
540 unsigned int offset=pointer[i];
541 void * objptr=*((void **)(((char *)ptr)+offset));
542 ENQUEUE(objptr, *((void **)(((char *)cpy)+offset)));
550 #if defined(THREADS)||defined(DSTM)||defined(STM)
552 pthread_mutex_unlock(&gclistlock);
557 /* Fix up the references from tags. This can't be done earlier,
558 because we don't want tags to keep objects alive */
560 while(taghead!=NULL) {
562 struct pointerblock *tmp=taghead->next;
563 for(i=0; i<tagindex; i++) {
564 struct ___TagDescriptor___ *tagd=taghead->ptrs[i];
565 struct ___Object___ *obj=tagd->flagptr;
566 struct ___TagDescriptor___ *copy=((struct ___TagDescriptor___**)tagd)[1];
568 /* Zero object case */
569 } else if (obj->type==-1) {
570 /* Single object case */
571 copy->flagptr=((struct ___Object___**)obj)[1];
572 } else if (obj->type==OBJECTARRAYTYPE) {
574 struct ArrayObject *ao=(struct ArrayObject *) obj;
578 struct ArrayObject *aonew;
580 /* Count live objects */
581 for(j=0; j<ao->___cachedCode___; j++) {
582 struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, j);
587 livecount=((livecount-1)/OBJECTARRAYINTERVAL+1)*OBJECTARRAYINTERVAL;
588 aonew=(struct ArrayObject *) tomalloc(sizeof(struct ArrayObject)+sizeof(struct ___Object___*)*livecount);
589 memcpy(aonew, ao, sizeof(struct ArrayObject));
590 aonew->type=OBJECTARRAYTYPE;
591 aonew->___length___=livecount;
593 for(j=0; j<ao->___cachedCode___; j++) {
594 struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, j);
595 if (tobj->type==-1) {
596 struct ___Object___ * tobjcpy=((struct ___Object___**)tobj)[1];
597 ARRAYSET(aonew, struct ___Object___*, k++,tobjcpy);
600 aonew->___cachedCode___=k;
601 for(; k<livecount; k++) {
602 ARRAYSET(aonew, struct ___Object___*, k, NULL);
605 /* No object live anymore */
616 void * tomalloc(int size) {
617 void * ptr=to_heapptr;
624 #if defined(THREADS)||defined(DSTM)||defined(STM)
625 void checkcollect(void * ptr) {
626 stopforgc((struct garbagelist *)ptr);
631 void checkcollect2(void * ptr) {
632 int ptrarray[]={1, (int)ptr, (int) revertlist};
633 stopforgc((struct garbagelist *)ptrarray);
635 revertlist=(struct ___Object___*)ptrarray[2];
639 void stopforgc(struct garbagelist * ptr) {
641 //just append us to the list
643 ptr=(struct garbagelist *) &ptrstack;
648 litem.locklist=pthread_getspecific(threadlocks);
651 litem.tc_size=c_size;
652 litem.tc_table=&c_table;
653 litem.tc_list=&c_list;
654 litem.tc_structs=&c_structs;
655 litem.objlist=newobjs;
657 litem.lockedlist=lockedobjs;
659 litem.base=&memorybase;
663 struct listitem *litem=pthread_getspecific(litemkey);
666 litem->locklist=pthread_getspecific(threadlocks);
669 pthread_mutex_lock(&gclistlock);
671 if ((listcount+1)==threadcount) {
672 //only do wakeup if we are ready to GC
673 pthread_cond_signal(&gccond);
675 pthread_mutex_unlock(&gclistlock);
678 void restartaftergc() {
680 pthread_mutex_lock(&gclock); // Wait for GC
681 pthread_mutex_unlock(&gclock);
683 pthread_mutex_lock(&gclistlock);
685 pthread_mutex_unlock(&gclistlock);
689 #if defined(STM)||defined(THREADS)
690 #define MEMORYBLOCK 65536
691 void * helper(struct garbagelist *, int);
692 void * mygcmalloc(struct garbagelist * stackptr, int size) {
695 if (memorybase==NULL||size>(memorytop-memorybase)) {
696 int toallocate=(size>MEMORYBLOCK)?size:MEMORYBLOCK;
697 memorybase=helper(stackptr, toallocate);
698 memorytop=memorybase+toallocate;
700 char *retvalue=memorybase;
705 void * helper(struct garbagelist * stackptr, int size) {
707 void * mygcmalloc(struct garbagelist * stackptr, int size) {
710 #if defined(THREADS)||defined(DSTM)||defined(STM)
711 while (pthread_mutex_trylock(&gclock)!=0) {
720 if (curr_heapptr>curr_heapgcpoint||curr_heapptr<curr_heapbase) {
721 if (curr_heapbase==0) {
722 /* Need to allocate base heap */
723 curr_heapbase=malloc(INITIALHEAPSIZE);
724 if (curr_heapbase==NULL) {
725 printf("malloc failed. Garbage collector couldn't get enough memory. Try changing heap size.\n");
728 bzero(curr_heapbase, INITIALHEAPSIZE);
729 curr_heaptop=curr_heapbase+INITIALHEAPSIZE;
730 curr_heapgcpoint=((char *) curr_heapbase)+GCPOINT(INITIALHEAPSIZE);
731 curr_heapptr=curr_heapbase+size;
733 to_heapbase=malloc(INITIALHEAPSIZE);
734 if (to_heapbase==NULL) {
735 printf("malloc failed. Garbage collector couldn't get enough memory. Try changing heap size.\n");
738 to_heaptop=to_heapbase+INITIALHEAPSIZE;
739 to_heapptr=to_heapbase;
741 #if defined(THREADS)||defined(DSTM)||defined(STM)
742 pthread_mutex_unlock(&gclock);
747 /* Grow the to heap if necessary */
749 INTPTR curr_heapsize=curr_heaptop-curr_heapbase;
750 INTPTR to_heapsize=to_heaptop-to_heapbase;
751 INTPTR last_heapsize=0;
753 last_heapsize=HEAPSIZE(lastgcsize, size);
754 if ((last_heapsize&7)!=0)
755 last_heapsize+=(8-(last_heapsize%8));
757 if (curr_heapsize>last_heapsize)
758 last_heapsize=curr_heapsize;
759 if (last_heapsize>to_heapsize) {
761 to_heapbase=malloc(last_heapsize);
762 if (to_heapbase==NULL) {
763 printf("Error Allocating enough memory\n");
766 to_heaptop=to_heapbase+last_heapsize;
767 to_heapptr=to_heapbase;
771 /* Do our collection */
774 /* Update stat on previous gc size */
775 lastgcsize=(to_heapptr-to_heapbase)+size;
778 printf("Garbage collected: Old bytes: %u\n", curr_heapptr-curr_heapbase);
779 printf("New space: %u\n", to_heapptr-to_heapbase);
780 printf("Total space: %u\n", to_heaptop-to_heapbase);
783 for(i=0;i<MAXSTATS;i++) {
784 if (garbagearray[i]!=0)
785 printf("Type=%d Size=%u\n", i, garbagearray[i]);
789 /* Flip to/curr heaps */
791 void * tmp=to_heapbase;
792 to_heapbase=curr_heapbase;
796 to_heaptop=curr_heaptop;
800 curr_heapptr=to_heapptr+size;
801 curr_heapgcpoint=((char *) curr_heapbase)+GCPOINT(curr_heaptop-curr_heapbase);
802 to_heapptr=to_heapbase;
804 /* Not enough room :(, redo gc */
805 if (curr_heapptr>curr_heapgcpoint) {
806 #if defined(THREADS)||defined(DSTM)||defined(STM)
807 pthread_mutex_unlock(&gclock);
809 return mygcmalloc(stackptr, size);
812 bzero(tmp, curr_heaptop-tmp);
813 #if defined(THREADS)||defined(DSTM)||defined(STM)
814 pthread_mutex_unlock(&gclock);
819 #if defined(THREADS)||defined(DSTM)||defined(STM)
820 pthread_mutex_unlock(&gclock);
827 int gc_createcopy(void * orig, void ** copy_ptr) {
832 int type=((int *)orig)[0];
834 *copy_ptr=((void **)orig)[1];
837 if (type<NUMCLASSES) {
838 /* We have a normal object */
840 int size=classsize[type]+sizeof(objheader_t);
841 void *newobj=tomalloc(size);
842 memcpy(newobj,((char *) orig)-sizeof(objheader_t), size);
843 newobj=((char *)newobj)+sizeof(objheader_t);
845 int size=classsize[type];
846 void *newobj=tomalloc(size);
847 memcpy(newobj, orig, size);
850 garbagearray[type]+=size;
853 ((void **)orig)[1]=newobj;
857 /* We have an array */
858 struct ArrayObject *ao=(struct ArrayObject *)orig;
859 int elementsize=classsize[type];
860 int length=ao->___length___;
862 int size=sizeof(struct ArrayObject)+length*elementsize+sizeof(objheader_t);
863 void *newobj=tomalloc(size);
864 memcpy(newobj, ((char*)orig)-sizeof(objheader_t), size);
865 newobj=((char *)newobj)+sizeof(objheader_t);
867 int size=sizeof(struct ArrayObject)+length*elementsize;
868 void *newobj=tomalloc(size);
869 memcpy(newobj, orig, size);
872 garbagearray[type]+=size;
875 ((void **)orig)[1]=newobj;