/
immix.cpp
334 lines (259 loc) · 8.63 KB
/
immix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "objectmemory.hpp"
#include "gc/immix.hpp"
#include "instruments/stats.hpp"
#include "capi/handles.hpp"
#include "capi/tag.hpp"
#include "object_watch.hpp"
#include "configuration.hpp"
#ifdef ENABLE_LLVM
#include "llvm/state.hpp"
#endif
namespace rubinius {
void ImmixGC::ObjectDescriber::added_chunk(int count) {
#ifdef IMMIX_DEBUG
std::cout << "[GC IMMIX: Added a chunk: " << count << "]\n";
#endif
if(object_memory_) {
if(gc_->dec_chunks_left() <= 0) {
// object_memory_->collect_mature_now = true;
gc_->reset_chunks_left();
}
}
}
/**
* This means we're getting low on memory! Time to schedule a garbage
* collection.
*/
void ImmixGC::ObjectDescriber::last_block() {
if(object_memory_) {
object_memory_->collect_mature_now = true;
}
}
void ImmixGC::ObjectDescriber::set_forwarding_pointer(memory::Address from, memory::Address to) {
from.as<Object>()->set_forward(to.as<Object>());
}
ImmixGC::ImmixGC(ObjectMemory* om)
: GarbageCollector(om)
, allocator_(gc_.block_allocator())
, chunks_before_collection_(10)
{
gc_.describer().set_object_memory(om, this);
reset_chunks_left();
}
memory::Address ImmixGC::ObjectDescriber::copy(memory::Address original,
immix::Allocator& alloc) {
Object* orig = original.as<Object>();
memory::Address copy_addr = alloc.allocate(
orig->size_in_bytes(object_memory_->state()));
Object* copy = copy_addr.as<Object>();
copy->initialize_full_state(object_memory_->state(), orig, 0);
copy->set_zone(MatureObjectZone);
copy->set_in_immix();
return copy_addr;
}
int ImmixGC::ObjectDescriber::size(memory::Address addr) {
return addr.as<Object>()->size_in_bytes(object_memory_->state());
}
Object* ImmixGC::allocate(int bytes) {
if(bytes > immix::cMaxObjectSize) return 0;
Object* obj = allocator_.allocate(bytes).as<Object>();
obj->init_header(MatureObjectZone, InvalidType);
obj->set_in_immix();
return obj;
}
Object* ImmixGC::move_object(Object* orig, int bytes) {
if(bytes > immix::cMaxObjectSize) return 0;
Object* obj = allocator_.allocate(bytes).as<Object>();
memcpy(obj, orig, bytes);
obj->set_zone(MatureObjectZone);
obj->set_age(0);
obj->set_in_immix();
orig->set_forward(obj);
return obj;
}
Object* ImmixGC::saw_object(Object* obj) {
#ifdef ENABLE_OBJECT_WATCH
if(watched_p(obj)) {
std::cout << "detected " << obj << " during immix scanning.\n";
}
#endif
if(!obj->reference_p()) return obj;
memory::Address fwd = gc_.mark_address(memory::Address(obj), allocator_);
Object* copy = fwd.as<Object>();
// Check and update an inflated header
if(copy && copy != obj) {
obj->set_forward(copy);
}
return copy;
}
ObjectPosition ImmixGC::validate_object(Object* obj) {
if(gc_.allocated_address(memory::Address(obj))) {
if(obj->in_immix_p()) {
return cInImmix;
} else {
return cInImmixCorruptHeader;
}
}
return cUnknown;
}
/**
* Performs a garbage collection of the immix space.
*/
void ImmixGC::collect(GCData& data) {
gc_.clear_lines();
int via_handles_ = 0;
int via_roots = 0;
for(Roots::Iterator i(data.roots()); i.more(); i.advance()) {
Object* tmp = i->get();
if(tmp->reference_p()) saw_object(tmp);
via_roots++;
}
if(data.threads()) {
for(std::list<ManagedThread*>::iterator i = data.threads()->begin();
i != data.threads()->end();
++i) {
scan(*i, false);
}
}
for(Allocator<capi::Handle>::Iterator i(data.handles()->allocator()); i.more(); i.advance()) {
if(i->in_use_p() && !i->weak_p()) {
saw_object(i->object());
via_handles_++;
}
}
std::list<capi::GlobalHandle*>* gh = data.global_handle_locations();
if(gh) {
for(std::list<capi::GlobalHandle*>::iterator i = gh->begin();
i != gh->end();
++i) {
capi::Handle** loc = (*i)->handle();
if(capi::Handle* hdl = *loc) {
if(!REFERENCE_P(hdl)) continue;
if(hdl->valid_p()) {
Object* obj = hdl->object();
if(obj && obj->reference_p()) {
saw_object(obj);
via_handles_++;
}
} else {
std::cerr << "Detected bad handle checking global capi handles\n";
}
}
}
}
#ifdef ENABLE_LLVM
if(LLVMState* ls = data.llvm_state()) ls->gc_scan(this);
#endif
gc_.process_mark_stack(allocator_);
// We've now finished marking the entire object graph.
// Clean weakrefs before keeping additional objects alive
// for finalization, so people don't get a hold of finalized
// objects through weakrefs.
clean_weakrefs();
// Marking objects to be Finalized can cause more things to continue to
// live, so we must check the mark_stack again.
do {
walk_finalizers();
} while(gc_.process_mark_stack(allocator_));
// Remove unreachable locked objects still in the list
if(data.threads()) {
for(std::list<ManagedThread*>::iterator i = data.threads()->begin();
i != data.threads()->end();
++i) {
clean_locked_objects(*i, false);
}
}
// Clear unreachable objects from the various remember sets
int cleared = 0;
unsigned int mark = object_memory_->mark();
cleared = object_memory_->unremember_objects(mark);
// Sweep up the garbage
gc_.sweep_blocks();
// This resets the allocator state to sync it up with the BlockAllocator
// properly.
allocator_.get_new_block();
// Now, calculate how much space we're still using.
immix::Chunks& chunks = gc_.block_allocator().chunks();
immix::AllBlockIterator iter(chunks);
int live_bytes = 0;
int total_bytes = 0;
while(immix::Block* block = iter.next()) {
total_bytes += immix::cBlockSize;
live_bytes += block->bytes_from_lines();
}
double percentage_live = (double)live_bytes / (double)total_bytes;
if(object_memory_->state()->shared.config.gc_immix_debug) {
std::cerr << "[GC IMMIX: " << clear_marked_objects() << " marked"
<< ", "
<< via_roots << " roots "
<< via_handles_ << " handles "
<< (int)(percentage_live * 100) << "% live"
<< ", " << live_bytes << "/" << total_bytes
<< "]\n";
}
if(percentage_live >= 0.90) {
if(object_memory_->state()->shared.config.gc_immix_debug) {
std::cerr << "[GC IMMIX: expanding. "
<< (int)(percentage_live * 100)
<< "%]\n";
}
gc_.block_allocator().add_chunk();
}
#ifdef IMMIX_DEBUG
std::cout << "Immix: RS size cleared: " << cleared << "\n";
immix::Chunks& chunks = gc_.block_allocator().chunks();
std::cout << "chunks=" << chunks.size() << "\n";
immix::AllBlockIterator iter(chunks);
int blocks_seen = 0;
int total_objects = 0;
int total_object_bytes = 0;
while(immix::Block* block = iter.next()) {
blocks_seen++;
std::cout << "block " << block << ", holes=" << block->holes() << " "
<< "objects=" << block->objects() << " "
<< "object_bytes=" << block->object_bytes() << " "
<< "frag=" << block->fragmentation_ratio()
<< "\n";
total_objects += block->objects();
total_object_bytes += block->object_bytes();
}
std::cout << blocks_seen << " blocks\n";
std::cout << gc_.bytes_allocated() << " bytes allocated\n";
std::cout << total_object_bytes << " object bytes / " << total_objects << " objects\n";
int* holes = new int[10];
for(int i = 0; i < 10; i++) {
holes[i] = 0;
}
immix::AllBlockIterator iter2(chunks);
while(immix::Block* block = iter2.next()) {
int h = block->holes();
if(h > 9) h = 9;
holes[h]++;
}
std::cout << "== hole stats ==\n";
for(int i = 0; i < 10; i++) {
if(holes[i] > 0) {
std::cout << i << ": " << holes[i] << "\n";
}
}
delete[] holes;
holes = NULL;
#endif
}
void ImmixGC::walk_finalizers() {
FinalizerHandler* fh = object_memory_->finalizer_handler();
if(!fh) return;
for(FinalizerHandler::iterator i = fh->begin();
!i.end();
/* advance is handled in the loop */)
{
FinalizeObject& fi = i.current();
bool live = fi.object->marked_p(object_memory_->mark());
if(fi.ruby_finalizer) {
fi.ruby_finalizer = saw_object(fi.ruby_finalizer);
}
fi.object = saw_object(fi.object);
i.next(live);
}
}
}