]> gitweb.factorcode.org Git - factor.git/blob - vm/gc.cpp
Fix conflict
[factor.git] / vm / gc.cpp
1 #include "master.hpp"
2
3 namespace factor
4 {
5
6 gc_event::gc_event(gc_op op_, factor_vm *parent) :
7         op(op_),
8         cards_scanned(0),
9         decks_scanned(0),
10         code_blocks_scanned(0),
11         start_time(nano_count()),
12         card_scan_time(0),
13         code_scan_time(0),
14         data_sweep_time(0),
15         code_sweep_time(0),
16         compaction_time(0)
17 {
18         data_heap_before = parent->data_room();
19         code_heap_before = parent->code_room();
20         start_time = nano_count();
21 }
22
23 void gc_event::started_card_scan()
24 {
25         temp_time = nano_count();
26 }
27
28 void gc_event::ended_card_scan(cell cards_scanned_, cell decks_scanned_)
29 {
30         cards_scanned += cards_scanned_;
31         decks_scanned += decks_scanned_;
32         card_scan_time = (nano_count() - temp_time);
33 }
34
35 void gc_event::started_code_scan()
36 {
37         temp_time = nano_count();
38 }
39
40 void gc_event::ended_code_scan(cell code_blocks_scanned_)
41 {
42         code_blocks_scanned += code_blocks_scanned_;
43         code_scan_time = (nano_count() - temp_time);
44 }
45
46 void gc_event::started_data_sweep()
47 {
48         temp_time = nano_count();
49 }
50
51 void gc_event::ended_data_sweep()
52 {
53         data_sweep_time = (nano_count() - temp_time);
54 }
55
56 void gc_event::started_code_sweep()
57 {
58         temp_time = nano_count();
59 }
60
61 void gc_event::ended_code_sweep()
62 {
63         code_sweep_time = (nano_count() - temp_time);
64 }
65
66 void gc_event::started_compaction()
67 {
68         temp_time = nano_count();
69 }
70
71 void gc_event::ended_compaction()
72 {
73         compaction_time = (nano_count() - temp_time);
74 }
75
76 void gc_event::ended_gc(factor_vm *parent)
77 {
78         data_heap_after = parent->data_room();
79         code_heap_after = parent->code_room();
80         total_time = nano_count() - start_time;
81 }
82
83 gc_state::gc_state(gc_op op_, factor_vm *parent) : op(op_), start_time(nano_count())
84 {
85         event = new gc_event(op,parent);
86 }
87
88 gc_state::~gc_state()
89 {
90         delete event;
91         event = NULL;
92 }
93
94 void factor_vm::end_gc()
95 {
96         current_gc->event->ended_gc(this);
97         if(gc_events) gc_events->push_back(*current_gc->event);
98         delete current_gc->event;
99         current_gc->event = NULL;
100 }
101
102 void factor_vm::start_gc_again()
103 {
104         end_gc();
105
106         switch(current_gc->op)
107         {
108         case collect_nursery_op:
109                 current_gc->op = collect_aging_op;
110                 break;
111         case collect_aging_op:
112                 current_gc->op = collect_to_tenured_op;
113                 break;
114         case collect_to_tenured_op:
115                 current_gc->op = collect_full_op;
116                 break;
117         case collect_full_op:
118         case collect_compact_op:
119                 current_gc->op = collect_growing_heap_op;
120                 break;
121         default:
122                 critical_error("Bad GC op",current_gc->op);
123                 break;
124         }
125
126         current_gc->event = new gc_event(current_gc->op,this);
127 }
128
129 void factor_vm::update_code_heap_for_minor_gc(std::set<code_block *> *remembered_set)
130 {
131         /* The youngest generation that any code block can now reference */
132         std::set<code_block *>::const_iterator iter = remembered_set->begin();
133         std::set<code_block *>::const_iterator end = remembered_set->end();
134
135         for(; iter != end; iter++) update_literal_references(*iter);
136 }
137
138 void factor_vm::gc(gc_op op, cell requested_bytes, bool trace_contexts_p)
139 {
140         assert(!gc_off);
141         assert(!current_gc);
142
143         save_stacks();
144
145         current_gc = new gc_state(op,this);
146
147         /* Keep trying to GC higher and higher generations until we don't run out
148         of space */
149         if(setjmp(current_gc->gc_unwind))
150         {
151                 /* We come back here if a generation is full */
152                 start_gc_again();
153         }
154
155         current_gc->event->op = current_gc->op;
156
157         switch(current_gc->op)
158         {
159         case collect_nursery_op:
160                 collect_nursery();
161                 break;
162         case collect_aging_op:
163                 collect_aging();
164                 if(data->low_memory_p())
165                 {
166                         current_gc->op = collect_full_op;
167                         current_gc->event->op = collect_full_op;
168                         collect_full(trace_contexts_p);
169                 }
170                 break;
171         case collect_to_tenured_op:
172                 collect_to_tenured();
173                 if(data->low_memory_p())
174                 {
175                         current_gc->op = collect_full_op;
176                         current_gc->event->op = collect_full_op;
177                         collect_full(trace_contexts_p);
178                 }
179                 break;
180         case collect_full_op:
181                 collect_full(trace_contexts_p);
182                 break;
183         case collect_compact_op:
184                 collect_compact(trace_contexts_p);
185                 break;
186         case collect_growing_heap_op:
187                 collect_growing_heap(requested_bytes,trace_contexts_p);
188                 break;
189         default:
190                 critical_error("Bad GC op",current_gc->op);
191                 break;
192         }
193
194         end_gc();
195
196         delete current_gc;
197         current_gc = NULL;
198 }
199
200 void factor_vm::primitive_minor_gc()
201 {
202         gc(collect_nursery_op,
203                 0, /* requested size */
204                 true /* trace contexts? */);
205 }
206
207 void factor_vm::primitive_full_gc()
208 {
209         gc(collect_full_op,
210                 0, /* requested size */
211                 true /* trace contexts? */);
212 }
213
214 void factor_vm::primitive_compact_gc()
215 {
216         gc(collect_compact_op,
217                 0, /* requested size */
218                 true /* trace contexts? */);
219 }
220
221 void factor_vm::inline_gc(cell *data_roots_base, cell data_roots_size)
222 {
223         data_roots.push_back(data_root_range(data_roots_base,data_roots_size));
224         primitive_minor_gc();
225         data_roots.pop_back();
226 }
227
228 VM_C_API void inline_gc(cell *data_roots_base, cell data_roots_size, factor_vm *parent)
229 {
230         parent->inline_gc(data_roots_base,data_roots_size);
231 }
232
233 /*
234  * It is up to the caller to fill in the object's fields in a meaningful
235  * fashion!
236  */
237 object *factor_vm::allot_large_object(cell type, cell size)
238 {
239         /* If tenured space does not have enough room, collect and compact */
240         if(!data->tenured->can_allot_p(size))
241         {
242                 primitive_compact_gc();
243
244                 /* If it still won't fit, grow the heap */
245                 if(!data->tenured->can_allot_p(size))
246                 {
247                         gc(collect_growing_heap_op,
248                                 size, /* requested size */
249                                 true /* trace contexts? */);
250                 }
251         }
252
253         object *obj = data->tenured->allot(size);
254
255         /* Allows initialization code to store old->new pointers
256         without hitting the write barrier in the common case of
257         a nursery allocation */
258         write_barrier(obj,size);
259
260         obj->initialize(type);
261         return obj;
262 }
263
264 void factor_vm::primitive_enable_gc_events()
265 {
266         gc_events = new std::vector<gc_event>();
267 }
268
269 void factor_vm::primitive_disable_gc_events()
270 {
271         if(gc_events)
272         {
273                 growable_array result(this);
274
275                 std::vector<gc_event> *gc_events = this->gc_events;
276                 this->gc_events = NULL;
277
278                 std::vector<gc_event>::const_iterator iter = gc_events->begin();
279                 std::vector<gc_event>::const_iterator end = gc_events->end();
280
281                 for(; iter != end; iter++)
282                 {
283                         gc_event event = *iter;
284                         byte_array *obj = byte_array_from_value(&event);
285                         result.add(tag<byte_array>(obj));
286                 }
287
288                 result.trim();
289                 dpush(result.elements.value());
290
291                 delete this->gc_events;
292         }
293         else
294                 dpush(false_object);
295 }
296
297 }