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