-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathheap.h
517 lines (425 loc) · 17.3 KB
/
heap.h
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#ifndef RUNTIME_VM_HEAP_HEAP_H_
#define RUNTIME_VM_HEAP_HEAP_H_
#if defined(SHOULD_NOT_INCLUDE_RUNTIME)
#error "Should not include runtime"
#endif
#include "include/dart_tools_api.h"
#include "platform/assert.h"
#include "vm/allocation.h"
#include "vm/flags.h"
#include "vm/globals.h"
#include "vm/heap/pages.h"
#include "vm/heap/scavenger.h"
#include "vm/heap/spaces.h"
#include "vm/heap/weak_table.h"
#include "vm/isolate.h"
namespace dart {
// Forward declarations.
class Isolate;
class IsolateGroup;
class ObjectPointerVisitor;
class ObjectSet;
class ServiceEvent;
class TimelineEventScope;
class VirtualMemory;
class Heap {
public:
enum Space {
kNew,
kOld,
kCode,
};
enum WeakSelector {
kPeers = 0,
#if !defined(HASH_IN_OBJECT_HEADER)
kIdentityHashes,
#endif
kCanonicalHashes,
kObjectIds,
kLoadingUnits,
kNumWeakSelectors
};
// States for a state machine that represents the worst-case set of GCs
// that an unreachable object could survive before begin collected:
// a new-space object that is involved with a cycle with an old-space object
// is copied to survivor space, then promoted during concurrent marking,
// and finally proven unreachable in the next round of old-gen marking.
// We ignore the case of unreachable-but-not-yet-collected objects being
// made reachable again by allInstances.
enum LeakCountState {
kInitial = 0,
kFirstScavenge,
kSecondScavenge,
kMarkingStart,
};
// Pattern for unused new space and swept old space.
static const uint8_t kZapByte = 0xf3;
~Heap();
Scavenger* new_space() { return &new_space_; }
PageSpace* old_space() { return &old_space_; }
uword Allocate(Thread* thread, intptr_t size, Space space) {
ASSERT(!read_only_);
switch (space) {
case kNew:
// Do not attempt to allocate very large objects in new space.
if (!IsAllocatableInNewSpace(size)) {
return AllocateOld(thread, size, Page::kData);
}
return AllocateNew(thread, size);
case kOld:
return AllocateOld(thread, size, Page::kData);
case kCode:
return AllocateOld(thread, size, Page::kExecutable);
default:
UNREACHABLE();
}
return 0;
}
// Tracks an external allocation. Returns false without tracking the
// allocation if it will make the total external size exceed
// kMaxAddrSpaceInWords.
bool AllocatedExternal(intptr_t size, Space space);
void FreedExternal(intptr_t size, Space space);
// Move external size from new to old space. Does not by itself trigger GC.
void PromotedExternal(intptr_t size);
void CheckExternalGC(Thread* thread);
// Heap contains the specified address.
bool Contains(uword addr) const;
bool NewContains(uword addr) const;
bool OldContains(uword addr) const;
bool CodeContains(uword addr) const;
bool DataContains(uword addr) const;
// Find an object by visiting all pointers in the specified heap space,
// the 'visitor' is used to determine if an object is found or not.
// The 'visitor' function should be set up to return true if the
// object is found, traversal through the heap space stops at that
// point.
// The 'visitor' function should return false if the object is not found,
// traversal through the heap space continues.
// Returns null object if nothing is found.
InstructionsPtr FindObjectInCodeSpace(FindObjectVisitor* visitor) const;
ObjectPtr FindOldObject(FindObjectVisitor* visitor) const;
ObjectPtr FindNewObject(FindObjectVisitor* visitor);
ObjectPtr FindObject(FindObjectVisitor* visitor);
void NotifyIdle(int64_t deadline);
void NotifyDestroyed();
Dart_PerformanceMode mode() const { return mode_; }
Dart_PerformanceMode SetMode(Dart_PerformanceMode mode);
// Collect a single generation.
void CollectGarbage(Thread* thread, GCType type, GCReason reason);
// Collect both generations by performing a scavenge followed by a
// mark-sweep. This function may not collect all unreachable objects. Because
// mark-sweep treats new space as roots, a cycle between unreachable old and
// new objects will not be collected until the new objects are promoted.
// Verification based on heap iteration should instead use CollectAllGarbage.
void CollectMostGarbage(GCReason reason = GCReason::kFull,
bool compact = false);
// Collect both generations by performing an evacuation followed by a
// mark-sweep. If incremental marking was in progress, perform another
// mark-sweep. This function will collect all unreachable objects, including
// those in inter-generational cycles or stored during incremental marking.
void CollectAllGarbage(GCReason reason = GCReason::kFull,
bool compact = false);
void CheckCatchUp(Thread* thread);
void CheckConcurrentMarking(Thread* thread, GCReason reason, intptr_t size);
void StartConcurrentMarking(Thread* thread, GCReason reason);
void WaitForMarkerTasks(Thread* thread);
void WaitForSweeperTasks(Thread* thread);
void WaitForSweeperTasksAtSafepoint(Thread* thread);
// Protect access to the heap. Note: Code pages are made
// executable/non-executable when 'read_only' is true/false, respectively.
void WriteProtect(bool read_only);
void WriteProtectCode(bool read_only) {
old_space_.WriteProtectCode(read_only);
}
// Initialize the heap and register it with the isolate.
static void Init(IsolateGroup* isolate_group,
bool is_vm_isolate,
intptr_t max_new_gen_words,
intptr_t max_old_gen_words);
// Verify that all pointers in the heap point to the heap.
bool Verify(MarkExpectation mark_expectation = kForbidMarked);
// Print heap sizes.
void PrintSizes() const;
// Return amount of memory used and capacity in a space, excluding external.
int64_t UsedInWords(Space space) const;
int64_t CapacityInWords(Space space) const;
int64_t ExternalInWords(Space space) const;
int64_t TotalUsedInWords() const;
int64_t TotalCapacityInWords() const;
int64_t TotalExternalInWords() const;
// Return the amount of GCing in microseconds.
int64_t GCTimeInMicros(Space space) const;
intptr_t Collections(Space space) const;
ObjectSet* CreateAllocatedObjectSet(Zone* zone,
MarkExpectation mark_expectation);
static const char* GCTypeToString(GCType type);
static const char* GCReasonToString(GCReason reason);
// Associate a peer with an object. A nonexistent peer is equal to NULL.
void SetPeer(ObjectPtr raw_obj, void* peer) {
SetWeakEntry(raw_obj, kPeers, reinterpret_cast<intptr_t>(peer));
}
void* GetPeer(ObjectPtr raw_obj) const {
return reinterpret_cast<void*>(GetWeakEntry(raw_obj, kPeers));
}
int64_t PeerCount() const;
#if !defined(HASH_IN_OBJECT_HEADER)
// Associate an identity hashCode with an object. An nonexistent hashCode
// is equal to 0.
intptr_t SetHashIfNotSet(ObjectPtr raw_obj, intptr_t hash) {
return SetWeakEntryIfNonExistent(raw_obj, kIdentityHashes, hash);
}
intptr_t GetHash(ObjectPtr raw_obj) const {
return GetWeakEntry(raw_obj, kIdentityHashes);
}
#endif
void SetCanonicalHash(ObjectPtr raw_obj, intptr_t hash) {
SetWeakEntry(raw_obj, kCanonicalHashes, hash);
}
intptr_t GetCanonicalHash(ObjectPtr raw_obj) const {
return GetWeakEntry(raw_obj, kCanonicalHashes);
}
void ResetCanonicalHashTable();
// Associate an id with an object (used when serializing an object).
// A non-existant id is equal to 0.
void SetObjectId(ObjectPtr raw_obj, intptr_t object_id) {
ASSERT(Thread::Current()->IsMutatorThread());
SetWeakEntry(raw_obj, kObjectIds, object_id);
}
intptr_t GetObjectId(ObjectPtr raw_obj) const {
ASSERT(Thread::Current()->IsMutatorThread());
return GetWeakEntry(raw_obj, kObjectIds);
}
void ResetObjectIdTable();
void SetLoadingUnit(ObjectPtr raw_obj, intptr_t unit_id) {
ASSERT(Thread::Current()->IsMutatorThread());
SetWeakEntry(raw_obj, kLoadingUnits, unit_id);
}
intptr_t GetLoadingUnit(ObjectPtr raw_obj) const {
ASSERT(Thread::Current()->IsMutatorThread());
return GetWeakEntry(raw_obj, kLoadingUnits);
}
// Used by the GC algorithms to propagate weak entries.
intptr_t GetWeakEntry(ObjectPtr raw_obj, WeakSelector sel) const;
void SetWeakEntry(ObjectPtr raw_obj, WeakSelector sel, intptr_t val);
intptr_t SetWeakEntryIfNonExistent(ObjectPtr raw_obj,
WeakSelector sel,
intptr_t val);
WeakTable* GetWeakTable(Space space, WeakSelector selector) const {
if (space == kNew) {
return new_weak_tables_[selector];
}
ASSERT(space == kOld);
return old_weak_tables_[selector];
}
void SetWeakTable(Space space, WeakSelector selector, WeakTable* value) {
if (space == kNew) {
new_weak_tables_[selector] = value;
} else {
ASSERT(space == kOld);
old_weak_tables_[selector] = value;
}
}
void ForwardWeakEntries(ObjectPtr before_object, ObjectPtr after_object);
void ForwardWeakTables(ObjectPointerVisitor* visitor);
void UpdateGlobalMaxUsed();
static bool IsAllocatableInNewSpace(intptr_t size) {
return size <= kNewAllocatableSize;
}
static bool IsAllocatableViaFreeLists(intptr_t size) {
return size < kAllocatablePageSize;
}
#ifndef PRODUCT
void PrintToJSONObject(Space space, JSONObject* object) const;
// Returns a JSON object with total memory usage statistics for both new and
// old space combined.
void PrintMemoryUsageJSON(JSONStream* stream) const;
void PrintMemoryUsageJSON(JSONObject* jsobj) const;
// The heap map contains the sizes and class ids for the objects in each page.
void PrintHeapMapToJSONStream(IsolateGroup* isolate_group,
JSONStream* stream) {
old_space_.PrintHeapMapToJSONStream(isolate_group, stream);
}
intptr_t ReachabilityBarrier() { return stats_.reachability_barrier_; }
#endif // PRODUCT
IsolateGroup* isolate_group() const { return isolate_group_; }
bool is_vm_isolate() const { return is_vm_isolate_; }
void SetupImagePage(void* pointer, uword size, bool is_executable) {
old_space_.SetupImagePage(pointer, size, is_executable);
}
static const intptr_t kNewAllocatableSize = 256 * KB;
static const intptr_t kAllocatablePageSize = 64 * KB;
Space SpaceForExternal(intptr_t size) const;
void CollectOnNthAllocation(intptr_t num_allocations);
private:
class GCStats : public ValueObject {
public:
GCStats() {}
intptr_t num_;
GCType type_;
GCReason reason_;
LeakCountState state_; // State to track finalization of GCed object.
intptr_t reachability_barrier_; // Tracks reachability of GCed objects.
class Data : public ValueObject {
public:
Data() {}
int64_t micros_;
SpaceUsage new_;
SpaceUsage old_;
intptr_t store_buffer_;
private:
DISALLOW_COPY_AND_ASSIGN(Data);
};
Data before_;
Data after_;
private:
DISALLOW_COPY_AND_ASSIGN(GCStats);
};
Heap(IsolateGroup* isolate_group,
bool is_vm_isolate,
intptr_t max_new_gen_semi_words, // Max capacity of new semi-space.
intptr_t max_old_gen_words);
uword AllocateNew(Thread* thread, intptr_t size);
uword AllocateOld(Thread* thread, intptr_t size, Page::PageType type);
// Visit all pointers. Caller must ensure concurrent sweeper is not running,
// and the visitor must not allocate.
void VisitObjectPointers(ObjectPointerVisitor* visitor);
// Visit all objects, including FreeListElement "objects". Caller must ensure
// concurrent sweeper is not running, and the visitor must not allocate.
void VisitObjects(ObjectVisitor* visitor);
void VisitObjectsNoImagePages(ObjectVisitor* visitor);
void VisitObjectsImagePages(ObjectVisitor* visitor) const;
// Like Verify, but does not wait for concurrent sweeper, so caller must
// ensure thread-safety.
bool VerifyGC(MarkExpectation mark_expectation = kForbidMarked);
// Helper functions for garbage collection.
void CollectNewSpaceGarbage(Thread* thread, GCType type, GCReason reason);
void CollectOldSpaceGarbage(Thread* thread, GCType type, GCReason reason);
// GC stats collection.
void RecordBeforeGC(GCType type, GCReason reason);
void RecordAfterGC(GCType type);
void PrintStats();
void PrintStatsToTimeline(TimelineEventScope* event, GCReason reason);
void AddRegionsToObjectSet(ObjectSet* set) const;
// Trigger major GC if 'gc_on_nth_allocation_' is set.
void CollectForDebugging(Thread* thread);
IsolateGroup* isolate_group_;
bool is_vm_isolate_;
// The different spaces used for allocation.
Scavenger new_space_;
PageSpace old_space_;
WeakTable* new_weak_tables_[kNumWeakSelectors];
WeakTable* old_weak_tables_[kNumWeakSelectors];
// GC stats collection.
GCStats stats_;
RelaxedAtomic<Dart_PerformanceMode> mode_ = {Dart_PerformanceMode_Default};
// This heap is in read-only mode: No allocation is allowed.
bool read_only_;
bool last_gc_was_old_space_;
bool assume_scavenge_will_fail_;
static const intptr_t kNoForcedGarbageCollection = -1;
// Whether the next heap allocation (new or old) should trigger
// CollectAllGarbage. Used within unit tests for testing GC on certain
// sensitive codepaths.
intptr_t gc_on_nth_allocation_;
friend class Become; // VisitObjectPointers
friend class GCCompactor; // VisitObjectPointers
friend class Precompiler; // VisitObjects
friend class ServiceEvent;
friend class Scavenger; // VerifyGC
friend class PageSpace; // VerifyGC
friend class ProgramReloadContext; // VisitObjects
friend class ClassFinalizer; // VisitObjects
friend class HeapIterationScope; // VisitObjects
friend class GCMarker; // VisitObjects
friend class ProgramVisitor; // VisitObjectsImagePages
friend class Serializer; // VisitObjectsImagePages
friend class HeapTestHelper;
friend class GCTestHelper;
DISALLOW_COPY_AND_ASSIGN(Heap);
};
class HeapIterationScope : public ThreadStackResource {
public:
explicit HeapIterationScope(Thread* thread, bool writable = false);
~HeapIterationScope();
void IterateObjects(ObjectVisitor* visitor) const;
void IterateObjectsNoImagePages(ObjectVisitor* visitor) const;
void IterateOldObjects(ObjectVisitor* visitor) const;
void IterateOldObjectsNoImagePages(ObjectVisitor* visitor) const;
void IterateVMIsolateObjects(ObjectVisitor* visitor) const;
void IterateObjectPointers(ObjectPointerVisitor* visitor,
ValidationPolicy validate_frames);
void IterateStackPointers(ObjectPointerVisitor* visitor,
ValidationPolicy validate_frames);
private:
Heap* heap_;
PageSpace* old_space_;
bool writable_;
DISALLOW_COPY_AND_ASSIGN(HeapIterationScope);
};
class ForceGrowthScope : public ThreadStackResource {
public:
explicit ForceGrowthScope(Thread* thread);
~ForceGrowthScope();
private:
DISALLOW_COPY_AND_ASSIGN(ForceGrowthScope);
};
// Note: During this scope all pages are writable and the code pages are
// non-executable.
class WritableVMIsolateScope : ThreadStackResource {
public:
explicit WritableVMIsolateScope(Thread* thread);
~WritableVMIsolateScope();
};
class WritableCodePages : StackResource {
public:
WritableCodePages(Thread* thread, IsolateGroup* isolate_group);
~WritableCodePages();
private:
IsolateGroup* isolate_group_;
};
#if defined(TESTING)
class GCTestHelper : public AllStatic {
public:
// Collect new gen without triggering any side effects. The normal call to
// CollectGarbage(Heap::kNew) could potentially trigger an old gen collection
// if there is enough promotion, and this can perturb some tests.
static void CollectNewSpace() {
Thread* thread = Thread::Current();
ASSERT(thread->execution_state() == Thread::kThreadInVM);
thread->heap()->CollectGarbage(thread, GCType::kScavenge,
GCReason::kDebugging);
}
// Fully collect old gen and wait for the sweeper to finish. The normal call
// to CollectGarbage(Heap::kOld) may leave so-called "floating garbage",
// objects that were seen by the incremental barrier but later made
// unreachable, and this can perturb some tests.
static void CollectOldSpace() {
Thread* thread = Thread::Current();
ASSERT(thread->execution_state() == Thread::kThreadInVM);
if (thread->is_marking()) {
thread->heap()->CollectGarbage(thread, GCType::kMarkSweep,
GCReason::kDebugging);
}
thread->heap()->CollectGarbage(thread, GCType::kMarkSweep,
GCReason::kDebugging);
WaitForGCTasks();
}
static void CollectAllGarbage(bool compact = false) {
Thread* thread = Thread::Current();
ASSERT(thread->execution_state() == Thread::kThreadInVM);
thread->heap()->CollectAllGarbage(GCReason::kDebugging, compact);
}
static void WaitForGCTasks() {
Thread* thread = Thread::Current();
ASSERT(thread->execution_state() == Thread::kThreadInVM);
thread->heap()->WaitForMarkerTasks(thread);
thread->heap()->WaitForSweeperTasks(thread);
}
};
#endif // TESTING
} // namespace dart
#endif // RUNTIME_VM_HEAP_HEAP_H_