f56043d930668e473a4883d49b41cea04776a2dd
[IRC.git] / Robust / src / Runtime / bamboo / pmc_garbage.c
1 #include "multicoregc.h"
2 #include "multicoreruntime.h"
3 #include "pmc_garbage.h"
4 #include "runtime_arch.h"
5
6 struct pmc_heap * pmc_heapptr;
7 struct pmc_queue * pmc_localqueue;
8 volatile bool gcflag;
9
10 void incrementthreads() {
11   tmc_spin_mutex_lock(&pmc_heapptr->lock);
12   pmc_heapptr->numthreads++;
13   tmc_spin_mutex_unlock(&pmc_heapptr->lock);
14 }
15
16 void decrementthreads() {
17   tmc_spin_mutex_lock(&pmc_heapptr->lock);
18   pmc_heapptr->numthreads--;
19   tmc_spin_mutex_unlock(&pmc_heapptr->lock);
20 }
21
22 void * pmc_unitend(unsigned int index) {
23   return gcbaseva+(index+1)*UNITSIZE;
24 }
25
26 void pmc_onceInit() {
27   pmc_localqueue=&pmc_heapptr->regions[BAMBOO_NUM_OF_CORE].markqueue;
28   pmc_queueinit(pmc_localqueue);
29   if (BAMBOO_NUM_OF_CORE==STARTUPCORE) {
30     tmc_spin_barrier_init(&pmc_heapptr->barrier, NUMCORES4GC);
31     for(int i=0;i<NUMPMCUNITS;i++) {
32       pmc_heapptr->units[i].endptr=pmc_unitend(i);
33       tprintf("%u endptr=%x\n", i, pmc_heapptr->units[i].endptr);
34     }
35     
36     for(int i=0;i<NUMCORES4GC;i+=2) {
37       if (i==0) {
38         pmc_heapptr->regions[i].lastptr=gcbaseva;
39       } else
40         pmc_heapptr->regions[i].lastptr=pmc_heapptr->units[i*4-1].endptr;
41       pmc_heapptr->regions[i].lowunit=4*i;
42       pmc_heapptr->regions[i].highunit=4*(i+1);
43       pmc_heapptr->regions[i+1].lastptr=pmc_heapptr->units[(i+1)*4+3].endptr;
44       pmc_heapptr->regions[i+1].lowunit=4*(i+1);
45       pmc_heapptr->regions[i+1].highunit=4*(i+2);
46     }
47     for(int i=0;i<NUMCORES4GC;i++) {
48       tprintf("%u lastptr=%x\n", i, pmc_heapptr->regions[i].lastptr);
49     }
50   }
51 }
52
53 void pmc_init() {
54   if (BAMBOO_NUM_OF_CORE==STARTUPCORE) {
55     pmc_heapptr->numthreads=NUMCORES4GC;
56     for(int i=0;i<NUMCORES4GC;i+=2) {
57       void *startptr=pmc_heapptr->regions[i].lastptr;
58       void *finishptr=pmc_heapptr->regions[i+1].lastptr;
59       struct pmc_region *region=&pmc_heapptr->regions[i];
60       unsigned int startindex=region->lowunit;
61       unsigned int endindex=pmc_heapptr->regions[i+1].highunit;
62
63       for(unsigned int index=startindex;index<endindex;index++) {
64         void *ptr=pmc_unitend(index);
65         if ((ptr>startptr)&&(ptr<=finishptr)) {
66           pmc_heapptr->units[index].endptr=ptr;
67           padspace(startptr, (unsigned int)(ptr-startptr));
68           startptr=ptr;
69         }
70         if (ptr>finishptr) {
71           void *prevunitptr=pmc_heapptr->units[index-1].endptr;
72           padspace(startptr, finishptr-startptr);
73           break;
74         }
75       }
76     }
77   }
78   if (bamboo_smem_size) {
79     padspace(bamboo_cur_msp, bamboo_smem_size);  
80   }
81   tmc_spin_barrier_wait(&pmc_heapptr->barrier);
82 }
83
84 void gc(struct garbagelist *gl) {
85   tprintf("%x\n", pmc_heapptr);
86   tprintf("init\n");
87   pmc_init();
88   //mark live objects
89   tprintf("mark\n");
90   pmc_mark(gl);
91   //count live objects per unit
92   tmc_spin_barrier_wait(&pmc_heapptr->barrier);
93   tprintf("count\n");
94   pmc_count();
95   tmc_spin_barrier_wait(&pmc_heapptr->barrier);
96   //divide up work
97   tprintf("divide\n");
98   if (BAMBOO_NUM_OF_CORE==STARTUPCORE) {
99     pmc_processunits();
100   }
101   tmc_spin_barrier_wait(&pmc_heapptr->barrier);
102   //set up forwarding pointers
103   tprintf("forward\n");
104   pmc_doforward();
105   tmc_spin_barrier_wait(&pmc_heapptr->barrier);
106   //update pointers
107   tprintf("updaterefs\n");
108   pmc_doreferenceupdate(gl);
109   tmc_spin_barrier_wait(&pmc_heapptr->barrier);
110   //compact data
111   tprintf("compact\n");
112   pmc_docompact();
113   //reset memory allocation
114   bamboo_cur_msp=NULL;
115   bamboo_smem_size=0;
116   tprintf("done\n");
117
118   if (BAMBOO_NUM_OF_CORE==STARTUPCORE) {
119     tmc_spin_barrier_wait(&pmc_heapptr->barrier);
120     //people will resend...no need to get gcflag so quickly
121     gcflag=false;
122   } else {
123     //start to listen for gcflags before we exit
124     gcflag=false;
125     tmc_spin_barrier_wait(&pmc_heapptr->barrier);
126   }
127 }
128
129 void padspace(void *ptr, unsigned int length) {
130   //zero small blocks
131   tprintf("Padspace from %x to %x\n", ptr, ptr+length);
132   if (length<sizeof(struct ArrayObject)) {
133     BAMBOO_MEMSET_WH(ptr,0,length);
134   } else {
135     //generate fake arrays for big blocks
136     struct ArrayObject *ao=(struct ArrayObject *)ptr;
137     ao->type=BYTEARRAYTYPE;
138     unsigned arraylength=length-sizeof(struct ArrayObject);
139     ao->___length___=arraylength;
140     ao->marked=0;
141   }
142 }
143
144 void gettype_size(void * ptr, int * ttype, unsigned int * tsize) {
145   int type = ((int *)ptr)[0];
146   if(type < NUMCLASSES) {
147     // a normal object
148     *tsize = classsize[type];
149     *ttype = type;
150   } else {
151     // an array
152     struct ArrayObject *ao=(struct ArrayObject *)ptr;
153     unsigned int elementsize=classsize[type];
154     unsigned int length=ao->___length___;
155     *tsize = sizeof(struct ArrayObject)+length*elementsize;
156     *ttype = type;
157   } 
158 }