changes to garbage collect STM
[IRC.git] / Robust / src / Runtime / garbage.c
1 #include "garbage.h"
2 #include "runtime.h"
3 #include "structdefs.h"
4 #include "Queue.h"
5 #include "SimpleHash.h"
6 #include "chash.h"
7 #include "GenericHashtable.h"
8 #include <string.h>
9 #if defined(THREADS) || defined(DSTM) || defined(STM)
10 #include "thread.h"
11 #endif
12
13 #ifdef DMALLOC
14 #include "dmalloc.h"
15 #endif
16 #ifdef DSTM
17 #include "dstm.h"
18 #endif
19 #ifdef STM
20 #include "tm.h"
21 #endif
22
23 #define NUMPTRS 100
24
25 #define INITIALHEAPSIZE 32*1024*1024
26 #define GCPOINT(x) ((int)((x)*0.9))
27 /* This define takes in how full the heap is initially and returns a new heap size to use */
28 #define HEAPSIZE(x,y) (((int)((x)/0.6))+y)
29
30 #ifdef TASK
31 extern struct genhashtable * activetasks;
32 #ifndef MULTICORE
33 extern struct parameterwrapper * objectqueues[NUMCLASSES];
34 #endif
35 extern struct genhashtable * failedtasks;
36 extern struct taskparamdescriptor *currtpd;
37 extern struct ctable *forward;
38 extern struct ctable *reverse;
39 extern struct RuntimeHash *fdtoobject;
40 #endif
41
42 #if defined(THREADS) || defined(DSTM) || defined(STM)
43 int needtocollect=0;
44 struct listitem * list=NULL;
45 int listcount=0;
46 #endif
47
48 //Need to check if pointers are transaction pointers
49 //this also catches the special flag value of 1 for local copies
50 #ifdef DSTM
51 #define ENQUEUE(orig, dst) \
52   if ((!(((unsigned int)orig)&0x1))) { \
53     if (orig>=curr_heapbase&&orig<curr_heaptop) { \
54       void *copy; \
55       if (gc_createcopy(orig,&copy)) \
56         enqueue(orig);\
57       dst=copy; \
58     } \
59   }
60 #elif defined(STM)
61 #define ENQUEUE(orig, dst) \
62   if (orig>=curr_heapbase&&orig<curr_heaptop) { \
63     void *copy; \
64     if (gc_createcopy(orig,&copy)) \
65       enqueue(orig);\
66     dst=copy; \
67   }
68 #define SENQUEUE(orig, dst) \
69   { \
70     void *copy; \
71     if (gc_createcopy(orig,&copy)) \
72       enqueue(orig);\
73     dst=copy; \
74   }
75 #elif defined(FASTCHECK)
76 #define ENQUEUE(orig, dst) \
77   if (((unsigned int)orig)!=1) { \
78     void *copy; \
79     if (gc_createcopy(orig,&copy)) \
80       enqueue(orig);\
81     dst=copy; }
82 #else
83 #define ENQUEUE(orig, dst) \
84   void *copy; \
85   if (gc_createcopy(orig,&copy)) \
86     enqueue(orig);\
87   dst=copy
88 #endif
89
90 struct pointerblock {
91   void * ptrs[NUMPTRS];
92   struct pointerblock *next;
93 };
94
95 void * curr_heapbase=0;
96 void * curr_heapptr=0;
97 void * curr_heapgcpoint=0;
98 void * curr_heaptop=0;
99
100 void * to_heapbase=0;
101 void * to_heapptr=0;
102 void * to_heaptop=0;
103 long lastgcsize=0;
104
105 struct pointerblock *head=NULL;
106 int headindex=0;
107 struct pointerblock *tail=NULL;
108 int tailindex=0;
109 struct pointerblock *spare=NULL;
110
111 void enqueue(void *ptr) {
112   if (headindex==NUMPTRS) {
113     struct pointerblock * tmp;
114     if (spare!=NULL) {
115       tmp=spare;
116       spare=NULL;
117     } else
118       tmp=malloc(sizeof(struct pointerblock));
119     head->next=tmp;
120     head=tmp;
121     headindex=0;
122   }
123   head->ptrs[headindex++]=ptr;
124 }
125
126 void * dequeue() {
127   if (tailindex==NUMPTRS) {
128     struct pointerblock *tmp=tail;
129     tail=tail->next;
130     tailindex=0;
131     if (spare!=NULL)
132       free(tmp);
133     else
134       spare=tmp;
135   }
136   return tail->ptrs[tailindex++];
137 }
138
139 #ifdef STM
140 void fixtable(chashlistnode_t ** tc_table, unsigned int tc_size) {
141   unsigned int mask=(tc_size<<1)-1;
142   chashlistnode_t *node=calloc(tc_size, sizeof(chashlistnode_t));
143   chashlistnode_t *ptr=*tc_table;
144   chashlistnode_t *curr;
145   unsigned int i;
146   unsigned int index;
147   int isfirst;
148   for(i=0;i<tc_size;i++) {
149     curr=&ptr[i];
150     isfirst=1;
151     do {                      //Inner loop to go through linked lists
152       void * key;
153       chashlistnode_t *tmp,*next;
154       
155       if ((key=(void *)curr->key) == 0) {             //Exit inner loop if there the first element is 0
156         break;                  //key = val =0 for element if not present within the hash table
157       }
158       SENQUEUE(key, key);
159       if (key>=curr_heapbase&&key<curr_heaptop) {
160         SENQUEUE(curr->val, curr->val);
161       } else {
162         //rewrite transaction cache entry
163         void *ptr=curr->val;
164         int type=((int *)ptr)[0];
165         unsigned int *pointer=pointerarray[type];
166         if (pointer==0) {
167           //array of primitives - do nothing
168         } else if (((int)pointer)==1) {
169           //array of pointers
170           struct ArrayObject *ao=(struct ArrayObject *) ptr;
171           int length=ao->___length___;
172           int i;
173           for(i=0; i<length; i++) {
174             void *objptr=((void **)(((char *)&ao->___length___)+sizeof(int)))[i];
175             ENQUEUE(objptr, ((void **)(((char *)&ao->___length___)+sizeof(int)))[i]);
176           }
177         } else {
178           int size=pointer[0];
179           int i;
180           for(i=1; i<=size; i++) {
181             unsigned int offset=pointer[i];
182             void * objptr=*((void **)(((int)ptr)+offset));
183             ENQUEUE(objptr, *((void **)(((int)ptr)+offset)));
184           }
185         }
186       }      
187
188       next = curr->next;
189       index = (((unsigned int)key) & mask) >>1;
190
191       curr->key=(unsigned int)key;
192       tmp=&node[index];
193       // Insert into the new table
194       if(tmp->key == 0) {
195         tmp->key = curr->key;
196         tmp->val = curr->val;
197         if (!isfirst) {
198           free(curr);
199         }
200       } else {
201         curr->next=tmp->next;
202         tmp->next=curr;
203       }
204       isfirst = 0;
205       curr = next;
206     } while(curr!=NULL);
207   }
208   free(ptr);
209   *tc_table=node;
210 }
211 #endif
212
213 int moreItems() {
214   if ((head==tail)&&(tailindex==headindex))
215     return 0;
216   return 1;
217 }
218
219 #ifdef TASK
220 struct pointerblock *taghead=NULL;
221 int tagindex=0;
222
223 void enqueuetag(struct ___TagDescriptor___ *ptr) {
224   if (tagindex==NUMPTRS) {
225     struct pointerblock * tmp=malloc(sizeof(struct pointerblock));
226     tmp->next=taghead;
227     taghead=tmp;
228     tagindex=0;
229   }
230   taghead->ptrs[tagindex++]=ptr;
231 }
232 #endif
233
234
235 void collect(struct garbagelist * stackptr) {
236 #if defined(THREADS)||defined(DSTM)||defined(STM)
237   needtocollect=1;
238   pthread_mutex_lock(&gclistlock);
239   while(1) {
240     if ((listcount+1)==threadcount) {
241       break; /* Have all other threads stopped */
242     }
243     pthread_cond_wait(&gccond, &gclistlock);
244   }
245 #endif
246
247   if (head==NULL) {
248     headindex=0;
249     tailindex=0;
250     head=tail=malloc(sizeof(struct pointerblock));
251   }
252
253 #ifdef TASK
254   if (taghead==NULL) {
255     tagindex=0;
256     taghead=malloc(sizeof(struct pointerblock));
257     taghead->next=NULL;
258   }
259 #endif
260
261   /* Check current stack */
262 #if defined(THREADS)||defined(DSTM)||defined(STM)
263   {
264     struct listitem *listptr=list;
265     while(1) {
266 #endif
267
268   while(stackptr!=NULL) {
269     int i;
270     for(i=0; i<stackptr->size; i++) {
271       void * orig=stackptr->array[i];
272       ENQUEUE(orig, stackptr->array[i]);
273     }
274     stackptr=stackptr->next;
275   }
276 #if defined(THREADS)||defined(DSTM)||defined(STM)
277   /* Go to next thread */
278   if (listptr!=NULL) {
279 #ifdef THREADS
280     void * orig=listptr->locklist;
281     ENQUEUE(orig, listptr->locklist);
282 #endif
283 #ifdef STM
284     fixtable(listptr->tc_table, listptr->tc_size);
285 #endif
286     stackptr=listptr->stackptr;
287     listptr=listptr->next;
288   } else
289     break;
290 }
291 }
292 #endif
293
294 #ifdef FASTCHECK
295   ENQUEUE(___fcrevert___, ___fcrevert___);
296 #endif
297
298 #ifdef TASK
299   {
300     /* Update objectsets */
301     int i;
302     for(i=0; i<NUMCLASSES; i++) {
303 #if !defined(MULTICORE)
304       struct parameterwrapper * p=objectqueues[i];
305       while(p!=NULL) {
306         struct ObjectHash * set=p->objectset;
307         struct ObjectNode * ptr=set->listhead;
308         while(ptr!=NULL) {
309           void *orig=(void *)ptr->key;
310           ENQUEUE(orig, *((void **)(&ptr->key)));
311           ptr=ptr->lnext;
312         }
313         ObjectHashrehash(set); /* Rehash the table */
314         p=p->next;
315       }
316 #endif
317     }
318   }
319
320 #ifndef FASTCHECK
321   if (forward!=NULL) {
322     struct cnode * ptr=forward->listhead;
323     while(ptr!=NULL) {
324       void * orig=(void *)ptr->key;
325       ENQUEUE(orig, *((void **)(&ptr->key)));
326       ptr=ptr->lnext;
327     }
328     crehash(forward); /* Rehash the table */
329   }
330
331   if (reverse!=NULL) {
332     struct cnode * ptr=reverse->listhead;
333     while(ptr!=NULL) {
334       void *orig=(void *)ptr->val;
335       ENQUEUE(orig, *((void**)(&ptr->val)));
336       ptr=ptr->lnext;
337     }
338   }
339 #endif
340
341   {
342     struct RuntimeNode * ptr=fdtoobject->listhead;
343     while(ptr!=NULL) {
344       void *orig=(void *)ptr->data;
345       ENQUEUE(orig, *((void**)(&ptr->data)));
346       ptr=ptr->lnext;
347     }
348   }
349
350   {
351     /* Update current task descriptor */
352     int i;
353     for(i=0; i<currtpd->numParameters; i++) {
354       void *orig=currtpd->parameterArray[i];
355       ENQUEUE(orig, currtpd->parameterArray[i]);
356     }
357
358   }
359
360   /* Update active tasks */
361   {
362     struct genpointerlist * ptr=activetasks->list;
363     while(ptr!=NULL) {
364       struct taskparamdescriptor *tpd=ptr->src;
365       int i;
366       for(i=0; i<tpd->numParameters; i++) {
367         void * orig=tpd->parameterArray[i];
368         ENQUEUE(orig, tpd->parameterArray[i]);
369       }
370       ptr=ptr->inext;
371     }
372     genrehash(activetasks);
373   }
374
375   /* Update failed tasks */
376   {
377     struct genpointerlist * ptr=failedtasks->list;
378     while(ptr!=NULL) {
379       struct taskparamdescriptor *tpd=ptr->src;
380       int i;
381       for(i=0; i<tpd->numParameters; i++) {
382         void * orig=tpd->parameterArray[i];
383         ENQUEUE(orig, tpd->parameterArray[i]);
384       }
385       ptr=ptr->inext;
386     }
387     genrehash(failedtasks);
388   }
389 #endif
390
391   while(moreItems()) {
392     void * ptr=dequeue();
393     void *cpy=((void **)ptr)[1];
394     int type=((int *)cpy)[0];
395     unsigned int * pointer;
396 #ifdef TASK
397     if(type==TAGTYPE) {
398       /* Enqueue Tag */
399       /* Nothing is inside */
400       enqueuetag(ptr);
401       continue;
402     }
403 #endif
404     pointer=pointerarray[type];
405     if (pointer==0) {
406       /* Array of primitives */
407       /* Do nothing */
408 #if defined(DSTM)||defined(FASTCHECK)
409       struct ArrayObject *ao=(struct ArrayObject *) ptr;
410       struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
411       ENQUEUE((void *)ao->___nextobject___, *((void **)&ao_cpy->___nextobject___));
412       ENQUEUE((void *)ao->___localcopy___, *((void **)&ao_cpy->___localcopy___));
413 #endif
414     } else if (((int)pointer)==1) {
415       /* Array of pointers */
416       struct ArrayObject *ao=(struct ArrayObject *) ptr;
417       struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
418 #if (defined(DSTM)||defined(FASTCHECK))
419       ENQUEUE((void *)ao->___nextobject___, *((void **)&ao_cpy->___nextobject___));
420       ENQUEUE((void *)ao->___localcopy___, *((void **)&ao_cpy->___localcopy___));
421 #endif
422       int length=ao->___length___;
423       int i;
424       for(i=0; i<length; i++) {
425         void *objptr=((void **)(((char *)&ao->___length___)+sizeof(int)))[i];
426         ENQUEUE(objptr, ((void **)(((char *)&ao_cpy->___length___)+sizeof(int)))[i]);
427       }
428     } else {
429       int size=pointer[0];
430       int i;
431       for(i=1; i<=size; i++) {
432         unsigned int offset=pointer[i];
433         void * objptr=*((void **)(((int)ptr)+offset));
434         ENQUEUE(objptr, *((void **)(((int)cpy)+offset)));
435       }
436     }
437   }
438 #ifdef TASK
439   fixtags();
440 #endif
441
442 #if defined(THREADS)||defined(DSTM)
443   needtocollect=0;
444   pthread_mutex_unlock(&gclistlock);
445 #endif
446 }
447
448 #ifdef TASK
449 /* Fix up the references from tags.  This can't be done earlier,
450    because we don't want tags to keep objects alive */
451 void fixtags() {
452   while(taghead!=NULL) {
453     int i;
454     struct pointerblock *tmp=taghead->next;
455     for(i=0; i<tagindex; i++) {
456       struct ___TagDescriptor___ *tagd=taghead->ptrs[i];
457       struct ___Object___ *obj=tagd->flagptr;
458       struct ___TagDescriptor___ *copy=((struct ___TagDescriptor___**)tagd)[1];
459       if (obj==NULL) {
460         /* Zero object case */
461       } else if (obj->type==-1) {
462         /* Single object case */
463         copy->flagptr=((struct ___Object___**)obj)[1];
464       } else if (obj->type==OBJECTARRAYTYPE) {
465         /* Array case */
466         struct ArrayObject *ao=(struct ArrayObject *) obj;
467         int livecount=0;
468         int j;
469         int k=0;
470         struct ArrayObject *aonew;
471
472         /* Count live objects */
473         for(j=0; j<ao->___cachedCode___; j++) {
474           struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, j);
475           if (tobj->type==-1)
476             livecount++;
477         }
478
479         livecount=((livecount-1)/OBJECTARRAYINTERVAL+1)*OBJECTARRAYINTERVAL;
480         aonew=(struct ArrayObject *) tomalloc(sizeof(struct ArrayObject)+sizeof(struct ___Object___*)*livecount);
481         memcpy(aonew, ao, sizeof(struct ArrayObject));
482         aonew->type=OBJECTARRAYTYPE;
483         aonew->___length___=livecount;
484         copy->flagptr=aonew;
485         for(j=0; j<ao->___cachedCode___; j++) {
486           struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, j);
487           if (tobj->type==-1) {
488             struct ___Object___ * tobjcpy=((struct ___Object___**)tobj)[1];
489             ARRAYSET(aonew, struct ___Object___*, k++,tobjcpy);
490           }
491         }
492         aonew->___cachedCode___=k;
493         for(; k<livecount; k++) {
494           ARRAYSET(aonew, struct ___Object___*, k, NULL);
495         }
496       } else {
497         /* No object live anymore */
498         copy->flagptr=NULL;
499       }
500     }
501     free(taghead);
502     taghead=tmp;
503     tagindex=NUMPTRS;
504   }
505 }
506 #endif
507
508 void * tomalloc(int size) {
509   void * ptr=to_heapptr;
510   if ((size&7)!=0)
511     size+=(8-(size%8));
512   to_heapptr+=size;
513   return ptr;
514 }
515
516 #if defined(THREADS)||defined(DSTM)||defined(STM)
517 void checkcollect(void * ptr) {
518   struct listitem * tmp=stopforgc((struct garbagelist *)ptr);
519   pthread_mutex_lock(&gclock); // Wait for GC
520   restartaftergc(tmp);
521   pthread_mutex_unlock(&gclock);
522 }
523
524 #ifdef DSTM
525 void checkcollect2(void * ptr) {
526   int ptrarray[]={1, (int)ptr, (int) revertlist};
527   struct listitem * tmp=stopforgc((struct garbagelist *)ptrarray);
528   pthread_mutex_lock(&gclock); // Wait for GC
529   restartaftergc(tmp);
530   pthread_mutex_unlock(&gclock);
531   revertlist=(struct ___Object___*)ptrarray[2];
532 }
533 #endif
534
535
536 struct listitem * stopforgc(struct garbagelist * ptr) {
537   struct listitem * litem=malloc(sizeof(struct listitem));
538   litem->stackptr=ptr;
539 #ifdef THREADS
540   litem->locklist=pthread_getspecific(threadlocks);
541 #endif
542 #ifdef STM
543   litem->tc_size=c_size;
544   litem->tc_table=&c_table;
545 #endif
546   litem->prev=NULL;
547   pthread_mutex_lock(&gclistlock);
548   litem->next=list;
549   if(list!=NULL)
550     list->prev=litem;
551   list=litem;
552   listcount++;
553   pthread_cond_signal(&gccond);
554   pthread_mutex_unlock(&gclistlock);
555   return litem;
556 }
557
558 void restartaftergc(struct listitem * litem) {
559   pthread_mutex_lock(&gclistlock);
560 #ifdef THREADS
561   pthread_setspecific(threadlocks, litem->locklist);
562 #endif
563   if (litem->prev==NULL) {
564     list=litem->next;
565   } else {
566     litem->prev->next=litem->next;
567   }
568   if (litem->next!=NULL) {
569     litem->next->prev=litem->prev;
570   }
571   listcount--;
572   pthread_mutex_unlock(&gclistlock);
573   free(litem);
574 }
575 #endif
576
577 void * mygcmalloc(struct garbagelist * stackptr, int size) {
578   void *ptr;
579 #if defined(THREADS)||defined(DSTM)||defined(STM)
580   if (pthread_mutex_trylock(&gclock)!=0) {
581     struct listitem *tmp=stopforgc(stackptr);
582     pthread_mutex_lock(&gclock);
583     restartaftergc(tmp);
584   }
585 #endif
586   ptr=curr_heapptr;
587   if ((size&7)!=0)
588     size+=(8-(size%8));
589   curr_heapptr+=size;
590   if (curr_heapptr>curr_heapgcpoint) {
591     if (curr_heapbase==0) {
592       /* Need to allocate base heap */
593       curr_heapbase=malloc(INITIALHEAPSIZE);
594       bzero(curr_heapbase, INITIALHEAPSIZE);
595       curr_heaptop=curr_heapbase+INITIALHEAPSIZE;
596       curr_heapgcpoint=((char *) curr_heapbase)+GCPOINT(INITIALHEAPSIZE);
597       curr_heapptr=curr_heapbase+size;
598
599       to_heapbase=malloc(INITIALHEAPSIZE);
600       to_heaptop=to_heapbase+INITIALHEAPSIZE;
601       to_heapptr=to_heapbase;
602       ptr=curr_heapbase;
603 #if defined(THREADS)||defined(DSTM)||defined(STM)
604       pthread_mutex_unlock(&gclock);
605 #endif
606       return ptr;
607     }
608
609     /* Grow the to heap if necessary */
610     {
611       int curr_heapsize=curr_heaptop-curr_heapbase;
612       int to_heapsize=to_heaptop-to_heapbase;
613       int last_heapsize=0;
614       if (lastgcsize>0) {
615         last_heapsize=HEAPSIZE(lastgcsize, size);
616         if ((last_heapsize&7)!=0)
617           last_heapsize+=(8-(last_heapsize%8));
618       }
619       if (curr_heapsize>last_heapsize)
620         last_heapsize=curr_heapsize;
621       if (last_heapsize>to_heapsize) {
622         free(to_heapbase);
623         to_heapbase=malloc(last_heapsize);
624         if (to_heapbase==NULL) {
625           printf("Error Allocating enough memory\n");
626           exit(-1);
627         }
628         to_heaptop=to_heapbase+last_heapsize;
629         to_heapptr=to_heapbase;
630       }
631     }
632
633     /* Do our collection */
634     collect(stackptr);
635
636     /* Update stat on previous gc size */
637     lastgcsize=(to_heapptr-to_heapbase)+size;
638
639     /* Flip to/curr heaps */
640     {
641       void * tmp=to_heapbase;
642       to_heapbase=curr_heapbase;
643       curr_heapbase=tmp;
644
645       tmp=to_heaptop;
646       to_heaptop=curr_heaptop;
647       curr_heaptop=tmp;
648
649       tmp=to_heapptr;
650       curr_heapptr=to_heapptr+size;
651       curr_heapgcpoint=((char *) curr_heapbase)+GCPOINT(curr_heaptop-curr_heapbase);
652       to_heapptr=to_heapbase;
653
654       /* Not enough room :(, redo gc */
655       if (curr_heapptr>curr_heapgcpoint) {
656 #if defined(THREADS)||defined(DSTM)||defined(STM)
657         pthread_mutex_unlock(&gclock);
658 #endif
659         return mygcmalloc(stackptr, size);
660       }
661
662       bzero(tmp, curr_heaptop-tmp);
663 #if defined(THREADS)||defined(DSTM)||defined(STM)
664       pthread_mutex_unlock(&gclock);
665 #endif
666       return tmp;
667     }
668   } else {
669 #if defined(THREADS)||defined(DSTM)||defined(STM)
670     pthread_mutex_unlock(&gclock);
671 #endif
672     return ptr;
673   }
674 }
675
676
677 int gc_createcopy(void * orig, void ** copy_ptr) {
678   if (orig==0) {
679     *copy_ptr=NULL;
680     return 0;
681   } else {
682     int type=((int *)orig)[0];
683     if (type==-1) {
684       *copy_ptr=((void **)orig)[1];
685       return 0;
686     }
687     if (type<NUMCLASSES) {
688       /* We have a normal object */
689       int size=classsize[type];
690       void *newobj=tomalloc(size);
691       memcpy(newobj, orig, size);
692       ((int *)orig)[0]=-1;
693       ((void **)orig)[1]=newobj;
694       *copy_ptr=newobj;
695       return 1;
696     } else {
697       /* We have an array */
698       struct ArrayObject *ao=(struct ArrayObject *)orig;
699       int elementsize=classsize[type];
700       int length=ao->___length___;
701       int size=sizeof(struct ArrayObject)+length*elementsize;
702       void *newobj=tomalloc(size);
703       memcpy(newobj, orig, size);
704       ((int *)orig)[0]=-1;
705       ((void **)orig)[1]=newobj;
706       *copy_ptr=newobj;
707       return 1;
708     }
709   }
710 }