heap.h revision e76e70f424468f311c2061c291e8384263f3968c
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_RUNTIME_GC_HEAP_H_
18#define ART_RUNTIME_GC_HEAP_H_
19
20#include <iosfwd>
21#include <string>
22#include <vector>
23
24#include "allocator_type.h"
25#include "atomic.h"
26#include "base/timing_logger.h"
27#include "gc/accounting/atomic_stack.h"
28#include "gc/accounting/card_table.h"
29#include "gc/gc_cause.h"
30#include "gc/collector/gc_type.h"
31#include "gc/collector_type.h"
32#include "globals.h"
33#include "gtest/gtest.h"
34#include "instruction_set.h"
35#include "jni.h"
36#include "object_callbacks.h"
37#include "offsets.h"
38#include "reference_queue.h"
39#include "safe_map.h"
40#include "thread_pool.h"
41#include "verify_object.h"
42
43namespace art {
44
45class ConditionVariable;
46class Mutex;
47class StackVisitor;
48class Thread;
49class TimingLogger;
50
51namespace mirror {
52  class Class;
53  class Object;
54}  // namespace mirror
55
56namespace gc {
57namespace accounting {
58  class HeapBitmap;
59  class ModUnionTable;
60  class RememberedSet;
61}  // namespace accounting
62
63namespace collector {
64  class ConcurrentCopying;
65  class GarbageCollector;
66  class MarkSweep;
67  class SemiSpace;
68}  // namespace collector
69
70namespace space {
71  class AllocSpace;
72  class BumpPointerSpace;
73  class DiscontinuousSpace;
74  class DlMallocSpace;
75  class ImageSpace;
76  class LargeObjectSpace;
77  class MallocSpace;
78  class RosAllocSpace;
79  class Space;
80  class SpaceTest;
81  class ContinuousMemMapAllocSpace;
82}  // namespace space
83
84class AgeCardVisitor {
85 public:
86  byte operator()(byte card) const {
87    if (card == accounting::CardTable::kCardDirty) {
88      return card - 1;
89    } else {
90      return 0;
91    }
92  }
93};
94
95// If true, use rosalloc/RosAllocSpace instead of dlmalloc/DlMallocSpace
96static constexpr bool kUseRosAlloc = true;
97
98// If true, use thread-local allocation stack.
99static constexpr bool kUseThreadLocalAllocationStack = true;
100
101// The process state passed in from the activity manager, used to determine when to do trimming
102// and compaction.
103enum ProcessState {
104  kProcessStateJankPerceptible = 0,
105  kProcessStateJankImperceptible = 1,
106};
107std::ostream& operator<<(std::ostream& os, const ProcessState& process_state);
108
109std::ostream& operator<<(std::ostream& os, const RootType& root_type);
110
111class Heap {
112 public:
113  // If true, measure the total allocation time.
114  static constexpr bool kMeasureAllocationTime = false;
115  // Primitive arrays larger than this size are put in the large object space.
116  static constexpr size_t kDefaultLargeObjectThreshold = 3 * kPageSize;
117
118  static constexpr size_t kDefaultStartingSize = kPageSize;
119  static constexpr size_t kDefaultInitialSize = 2 * MB;
120  static constexpr size_t kDefaultMaximumSize = 32 * MB;
121  static constexpr size_t kDefaultMaxFree = 2 * MB;
122  static constexpr size_t kDefaultMinFree = kDefaultMaxFree / 4;
123  static constexpr size_t kDefaultLongPauseLogThreshold = MsToNs(5);
124  static constexpr size_t kDefaultLongGCLogThreshold = MsToNs(100);
125  static constexpr size_t kDefaultTLABSize = 256 * KB;
126  static constexpr double kDefaultTargetUtilization = 0.5;
127  static constexpr double kDefaultHeapGrowthMultiplier = 2.0;
128
129  // Used so that we don't overflow the allocation time atomic integer.
130  static constexpr size_t kTimeAdjust = 1024;
131
132  // How often we allow heap trimming to happen (nanoseconds).
133  static constexpr uint64_t kHeapTrimWait = MsToNs(5000);
134  // How long we wait after a transition request to perform a collector transition (nanoseconds).
135  static constexpr uint64_t kCollectorTransitionWait = MsToNs(5000);
136
137  // Create a heap with the requested sizes. The possible empty
138  // image_file_names names specify Spaces to load based on
139  // ImageWriter output.
140  explicit Heap(size_t initial_size, size_t growth_limit, size_t min_free,
141                size_t max_free, double target_utilization,
142                double foreground_heap_growth_multiplier, size_t capacity,
143                const std::string& original_image_file_name,
144                const InstructionSet image_instruction_set,
145                CollectorType foreground_collector_type, CollectorType background_collector_type,
146                size_t parallel_gc_threads, size_t conc_gc_threads, bool low_memory_mode,
147                size_t long_pause_threshold, size_t long_gc_threshold,
148                bool ignore_max_footprint, bool use_tlab,
149                bool verify_pre_gc_heap, bool verify_pre_sweeping_heap, bool verify_post_gc_heap,
150                bool verify_pre_gc_rosalloc, bool verify_pre_sweeping_rosalloc,
151                bool verify_post_gc_rosalloc);
152
153  ~Heap();
154
155  // Allocates and initializes storage for an object instance.
156  template <bool kInstrumented, typename PreFenceVisitor>
157  mirror::Object* AllocObject(Thread* self, mirror::Class* klass, size_t num_bytes,
158                              const PreFenceVisitor& pre_fence_visitor)
159      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
160    return AllocObjectWithAllocator<kInstrumented, true>(self, klass, num_bytes,
161                                                         GetCurrentAllocator(),
162                                                         pre_fence_visitor);
163  }
164
165  template <bool kInstrumented, typename PreFenceVisitor>
166  mirror::Object* AllocNonMovableObject(Thread* self, mirror::Class* klass, size_t num_bytes,
167                                        const PreFenceVisitor& pre_fence_visitor)
168      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
169    return AllocObjectWithAllocator<kInstrumented, true>(self, klass, num_bytes,
170                                                         GetCurrentNonMovingAllocator(),
171                                                         pre_fence_visitor);
172  }
173
174  template <bool kInstrumented, bool kCheckLargeObject, typename PreFenceVisitor>
175  ALWAYS_INLINE mirror::Object* AllocObjectWithAllocator(
176      Thread* self, mirror::Class* klass, size_t byte_count, AllocatorType allocator,
177      const PreFenceVisitor& pre_fence_visitor)
178      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
179
180  AllocatorType GetCurrentAllocator() const {
181    return current_allocator_;
182  }
183
184  AllocatorType GetCurrentNonMovingAllocator() const {
185    return current_non_moving_allocator_;
186  }
187
188  // Visit all of the live objects in the heap.
189  void VisitObjects(ObjectCallback callback, void* arg)
190      SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
191
192  void CheckPreconditionsForAllocObject(mirror::Class* c, size_t byte_count)
193      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
194  void ThrowOutOfMemoryError(size_t byte_count, bool large_object_allocation);
195
196  void RegisterNativeAllocation(JNIEnv* env, int bytes);
197  void RegisterNativeFree(JNIEnv* env, int bytes);
198
199  // Change the allocator, updates entrypoints.
200  void ChangeAllocator(AllocatorType allocator)
201      EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
202      LOCKS_EXCLUDED(Locks::runtime_shutdown_lock_);
203
204  // Transition the garbage collector during runtime, may copy objects from one space to another.
205  void TransitionCollector(CollectorType collector_type);
206
207  // Change the collector to be one of the possible options (MS, CMS, SS).
208  void ChangeCollector(CollectorType collector_type)
209      EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
210
211  // The given reference is believed to be to an object in the Java heap, check the soundness of it.
212  // TODO: NO_THREAD_SAFETY_ANALYSIS since we call this everywhere and it is impossible to find a
213  // proper lock ordering for it.
214  void VerifyObjectBody(mirror::Object* o) NO_THREAD_SAFETY_ANALYSIS;
215
216  // Check sanity of all live references.
217  void VerifyHeap() LOCKS_EXCLUDED(Locks::heap_bitmap_lock_);
218  bool VerifyHeapReferences()
219      EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
220  bool VerifyMissingCardMarks()
221      EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
222
223  // A weaker test than IsLiveObject or VerifyObject that doesn't require the heap lock,
224  // and doesn't abort on error, allowing the caller to report more
225  // meaningful diagnostics.
226  bool IsValidObjectAddress(const mirror::Object* obj) const
227      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
228
229  // Faster alternative to IsHeapAddress since finding if an object is in the large object space is
230  // very slow.
231  bool IsNonDiscontinuousSpaceHeapAddress(const mirror::Object* obj) const
232      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
233
234  // Returns true if 'obj' is a live heap object, false otherwise (including for invalid addresses).
235  // Requires the heap lock to be held.
236  bool IsLiveObjectLocked(mirror::Object* obj, bool search_allocation_stack = true,
237                          bool search_live_stack = true, bool sorted = false)
238      SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
239
240  // Returns true if there is any chance that the object (obj) will move.
241  bool IsMovableObject(const mirror::Object* obj) const;
242
243  // Enables us to compacting GC until objects are released.
244  void IncrementDisableMovingGC(Thread* self);
245  void DecrementDisableMovingGC(Thread* self);
246
247  // Clear all of the mark bits, doesn't clear bitmaps which have the same live bits as mark bits.
248  void ClearMarkedObjects() EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
249
250  // Initiates an explicit garbage collection.
251  void CollectGarbage(bool clear_soft_references);
252
253  // Does a concurrent GC, should only be called by the GC daemon thread
254  // through runtime.
255  void ConcurrentGC(Thread* self) LOCKS_EXCLUDED(Locks::runtime_shutdown_lock_);
256
257  // Implements VMDebug.countInstancesOfClass and JDWP VM_InstanceCount.
258  // The boolean decides whether to use IsAssignableFrom or == when comparing classes.
259  void CountInstances(const std::vector<mirror::Class*>& classes, bool use_is_assignable_from,
260                      uint64_t* counts)
261      LOCKS_EXCLUDED(Locks::heap_bitmap_lock_)
262      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
263  // Implements JDWP RT_Instances.
264  void GetInstances(mirror::Class* c, int32_t max_count, std::vector<mirror::Object*>& instances)
265      LOCKS_EXCLUDED(Locks::heap_bitmap_lock_)
266      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
267  // Implements JDWP OR_ReferringObjects.
268  void GetReferringObjects(mirror::Object* o, int32_t max_count, std::vector<mirror::Object*>& referring_objects)
269      LOCKS_EXCLUDED(Locks::heap_bitmap_lock_)
270      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
271
272  // Removes the growth limit on the alloc space so it may grow to its maximum capacity. Used to
273  // implement dalvik.system.VMRuntime.clearGrowthLimit.
274  void ClearGrowthLimit();
275
276  // Target ideal heap utilization ratio, implements
277  // dalvik.system.VMRuntime.getTargetHeapUtilization.
278  double GetTargetHeapUtilization() const {
279    return target_utilization_;
280  }
281
282  // Data structure memory usage tracking.
283  void RegisterGCAllocation(size_t bytes);
284  void RegisterGCDeAllocation(size_t bytes);
285
286  // Set the heap's private space pointers to be the same as the space based on it's type. Public
287  // due to usage by tests.
288  void SetSpaceAsDefault(space::ContinuousSpace* continuous_space)
289      LOCKS_EXCLUDED(Locks::heap_bitmap_lock_);
290  void AddSpace(space::Space* space) LOCKS_EXCLUDED(Locks::heap_bitmap_lock_);
291  void RemoveSpace(space::Space* space) LOCKS_EXCLUDED(Locks::heap_bitmap_lock_);
292
293  // Set target ideal heap utilization ratio, implements
294  // dalvik.system.VMRuntime.setTargetHeapUtilization.
295  void SetTargetHeapUtilization(float target);
296
297  // For the alloc space, sets the maximum number of bytes that the heap is allowed to allocate
298  // from the system. Doesn't allow the space to exceed its growth limit.
299  void SetIdealFootprint(size_t max_allowed_footprint);
300
301  // Blocks the caller until the garbage collector becomes idle and returns the type of GC we
302  // waited for.
303  collector::GcType WaitForGcToComplete(GcCause cause, Thread* self)
304      LOCKS_EXCLUDED(gc_complete_lock_);
305
306  // Update the heap's process state to a new value, may cause compaction to occur.
307  void UpdateProcessState(ProcessState process_state);
308
309  const std::vector<space::ContinuousSpace*>& GetContinuousSpaces() const {
310    return continuous_spaces_;
311  }
312
313  const std::vector<space::DiscontinuousSpace*>& GetDiscontinuousSpaces() const {
314    return discontinuous_spaces_;
315  }
316
317  static mirror::Object* PreserveSoftReferenceCallback(mirror::Object* obj, void* arg);
318  void ProcessSoftReferences(TimingLogger& timings, bool clear_soft,
319                             IsMarkedCallback* is_marked_callback,
320                             MarkObjectCallback* mark_object_callback,
321                             ProcessMarkStackCallback* process_mark_stack_callback, void* arg)
322      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
323      EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
324  void ProcessReferences(TimingLogger& timings, bool clear_soft,
325                         IsMarkedCallback* is_marked_callback,
326                         MarkObjectCallback* mark_object_callback,
327                         ProcessMarkStackCallback* process_mark_stack_callback,
328                         void* arg)
329      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
330      EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
331
332  // Enable verification of object references when the runtime is sufficiently initialized.
333  void EnableObjectValidation() {
334    verify_object_mode_ = kVerifyObjectSupport;
335    if (verify_object_mode_ > kVerifyObjectModeDisabled) {
336      VerifyHeap();
337    }
338  }
339
340  // Disable object reference verification for image writing.
341  void DisableObjectValidation() {
342    verify_object_mode_ = kVerifyObjectModeDisabled;
343  }
344
345  // Other checks may be performed if we know the heap should be in a sane state.
346  bool IsObjectValidationEnabled() const {
347    return verify_object_mode_ > kVerifyObjectModeDisabled;
348  }
349
350  // Returns true if low memory mode is enabled.
351  bool IsLowMemoryMode() const {
352    return low_memory_mode_;
353  }
354
355  // Returns the heap growth multiplier, this affects how much we grow the heap after a GC.
356  // Scales heap growth, min free, and max free.
357  double HeapGrowthMultiplier() const;
358
359  // Freed bytes can be negative in cases where we copy objects from a compacted space to a
360  // free-list backed space.
361  void RecordFree(uint64_t freed_objects, int64_t freed_bytes);
362
363  // Must be called if a field of an Object in the heap changes, and before any GC safe-point.
364  // The call is not needed if NULL is stored in the field.
365  void WriteBarrierField(const mirror::Object* dst, MemberOffset /*offset*/,
366                         const mirror::Object* /*new_value*/) {
367    card_table_->MarkCard(dst);
368  }
369
370  // Write barrier for array operations that update many field positions
371  void WriteBarrierArray(const mirror::Object* dst, int /*start_offset*/,
372                         size_t /*length TODO: element_count or byte_count?*/) {
373    card_table_->MarkCard(dst);
374  }
375
376  void WriteBarrierEveryFieldOf(const mirror::Object* obj) {
377    card_table_->MarkCard(obj);
378  }
379
380  accounting::CardTable* GetCardTable() const {
381    return card_table_.get();
382  }
383
384  void AddFinalizerReference(Thread* self, mirror::Object** object);
385
386  // Returns the number of bytes currently allocated.
387  size_t GetBytesAllocated() const {
388    return num_bytes_allocated_;
389  }
390
391  // Returns the number of objects currently allocated.
392  size_t GetObjectsAllocated() const LOCKS_EXCLUDED(Locks::heap_bitmap_lock_);
393
394  // Returns the total number of objects allocated since the heap was created.
395  size_t GetObjectsAllocatedEver() const;
396
397  // Returns the total number of bytes allocated since the heap was created.
398  size_t GetBytesAllocatedEver() const;
399
400  // Returns the total number of objects freed since the heap was created.
401  size_t GetObjectsFreedEver() const {
402    return total_objects_freed_ever_;
403  }
404
405  // Returns the total number of bytes freed since the heap was created.
406  size_t GetBytesFreedEver() const {
407    return total_bytes_freed_ever_;
408  }
409
410  // Implements java.lang.Runtime.maxMemory, returning the maximum amount of memory a program can
411  // consume. For a regular VM this would relate to the -Xmx option and would return -1 if no Xmx
412  // were specified. Android apps start with a growth limit (small heap size) which is
413  // cleared/extended for large apps.
414  size_t GetMaxMemory() const {
415    return growth_limit_;
416  }
417
418  // Implements java.lang.Runtime.totalMemory, returning the amount of memory consumed by an
419  // application.
420  size_t GetTotalMemory() const;
421
422  // Implements java.lang.Runtime.freeMemory.
423  size_t GetFreeMemory() const {
424    return GetTotalMemory() - num_bytes_allocated_;
425  }
426
427  // Get the space that corresponds to an object's address. Current implementation searches all
428  // spaces in turn. If fail_ok is false then failing to find a space will cause an abort.
429  // TODO: consider using faster data structure like binary tree.
430  space::ContinuousSpace* FindContinuousSpaceFromObject(const mirror::Object*, bool fail_ok) const;
431  space::DiscontinuousSpace* FindDiscontinuousSpaceFromObject(const mirror::Object*,
432                                                              bool fail_ok) const;
433  space::Space* FindSpaceFromObject(const mirror::Object*, bool fail_ok) const;
434
435  void DumpForSigQuit(std::ostream& os);
436
437
438  // Do a pending heap transition or trim.
439  void DoPendingTransitionOrTrim() LOCKS_EXCLUDED(heap_trim_request_lock_);
440
441  // Trim the managed and native heaps by releasing unused memory back to the OS.
442  void Trim() LOCKS_EXCLUDED(heap_trim_request_lock_);
443
444  void RevokeThreadLocalBuffers(Thread* thread);
445  void RevokeRosAllocThreadLocalBuffers(Thread* thread);
446  void RevokeAllThreadLocalBuffers();
447  void AssertAllBumpPointerSpaceThreadLocalBuffersAreRevoked();
448  void RosAllocVerification(TimingLogger* timings, const char* name)
449      EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
450
451  accounting::HeapBitmap* GetLiveBitmap() SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
452    return live_bitmap_.get();
453  }
454
455  accounting::HeapBitmap* GetMarkBitmap() SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
456    return mark_bitmap_.get();
457  }
458
459  accounting::ObjectStack* GetLiveStack() SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
460    return live_stack_.get();
461  }
462
463  void PreZygoteFork() NO_THREAD_SAFETY_ANALYSIS;
464
465  // Mark and empty stack.
466  void FlushAllocStack()
467      EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
468
469  // Revoke all the thread-local allocation stacks.
470  void RevokeAllThreadLocalAllocationStacks(Thread* self)
471      EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
472      LOCKS_EXCLUDED(Locks::runtime_shutdown_lock_, Locks::thread_list_lock_);
473
474  // Mark all the objects in the allocation stack in the specified bitmap.
475  // TODO: Refactor?
476  void MarkAllocStack(accounting::SpaceBitmap<kObjectAlignment>* bitmap1,
477                      accounting::SpaceBitmap<kObjectAlignment>* bitmap2,
478                      accounting::SpaceBitmap<kLargeObjectAlignment>* large_objects,
479                      accounting::ObjectStack* stack)
480      EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
481
482  // Mark the specified allocation stack as live.
483  void MarkAllocStackAsLive(accounting::ObjectStack* stack)
484      EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
485
486  // Unbind any bound bitmaps.
487  void UnBindBitmaps() EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
488
489  // DEPRECATED: Should remove in "near" future when support for multiple image spaces is added.
490  // Assumes there is only one image space.
491  space::ImageSpace* GetImageSpace() const;
492
493  // Permenantly disable compaction.
494  void DisableCompaction();
495
496  space::DlMallocSpace* GetDlMallocSpace() const {
497    return dlmalloc_space_;
498  }
499
500  space::RosAllocSpace* GetRosAllocSpace() const {
501    return rosalloc_space_;
502  }
503
504  space::MallocSpace* GetNonMovingSpace() const {
505    return non_moving_space_;
506  }
507
508  space::LargeObjectSpace* GetLargeObjectsSpace() const {
509    return large_object_space_;
510  }
511
512  // Returns the free list space that may contain movable objects (the
513  // one that's not the non-moving space), either rosalloc_space_ or
514  // dlmalloc_space_.
515  space::MallocSpace* GetPrimaryFreeListSpace() {
516    if (kUseRosAlloc) {
517      DCHECK(rosalloc_space_ != nullptr);
518      // reinterpret_cast is necessary as the space class hierarchy
519      // isn't known (#included) yet here.
520      return reinterpret_cast<space::MallocSpace*>(rosalloc_space_);
521    } else {
522      DCHECK(dlmalloc_space_ != nullptr);
523      return reinterpret_cast<space::MallocSpace*>(dlmalloc_space_);
524    }
525  }
526
527  void DumpSpaces(std::ostream& stream = LOG(INFO));
528
529  // Dump object should only be used by the signal handler.
530  void DumpObject(std::ostream& stream, mirror::Object* obj) NO_THREAD_SAFETY_ANALYSIS;
531  // Safe version of pretty type of which check to make sure objects are heap addresses.
532  std::string SafeGetClassDescriptor(mirror::Class* klass) NO_THREAD_SAFETY_ANALYSIS;
533  std::string SafePrettyTypeOf(mirror::Object* obj) NO_THREAD_SAFETY_ANALYSIS;
534
535  // GC performance measuring
536  void DumpGcPerformanceInfo(std::ostream& os);
537
538  // Returns true if we currently care about pause times.
539  bool CareAboutPauseTimes() const {
540    return process_state_ == kProcessStateJankPerceptible;
541  }
542
543  // Thread pool.
544  void CreateThreadPool();
545  void DeleteThreadPool();
546  ThreadPool* GetThreadPool() {
547    return thread_pool_.get();
548  }
549  size_t GetParallelGCThreadCount() const {
550    return parallel_gc_threads_;
551  }
552  size_t GetConcGCThreadCount() const {
553    return conc_gc_threads_;
554  }
555  accounting::ModUnionTable* FindModUnionTableFromSpace(space::Space* space);
556  void AddModUnionTable(accounting::ModUnionTable* mod_union_table);
557
558  accounting::RememberedSet* FindRememberedSetFromSpace(space::Space* space);
559  void AddRememberedSet(accounting::RememberedSet* remembered_set);
560  void RemoveRememberedSet(space::Space* space);
561
562  bool IsCompilingBoot() const;
563  bool RunningOnValgrind() const {
564    return running_on_valgrind_;
565  }
566  bool HasImageSpace() const;
567
568 private:
569  void Compact(space::ContinuousMemMapAllocSpace* target_space,
570               space::ContinuousMemMapAllocSpace* source_space)
571      EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
572
573  void FinishGC(Thread* self, collector::GcType gc_type) LOCKS_EXCLUDED(gc_complete_lock_);
574
575  static ALWAYS_INLINE bool AllocatorHasAllocationStack(AllocatorType allocator_type) {
576    return
577        allocator_type != kAllocatorTypeBumpPointer &&
578        allocator_type != kAllocatorTypeTLAB;
579  }
580  static ALWAYS_INLINE bool AllocatorMayHaveConcurrentGC(AllocatorType allocator_type) {
581    return AllocatorHasAllocationStack(allocator_type);
582  }
583  static bool IsMovingGc(CollectorType collector_type) {
584    return collector_type == kCollectorTypeSS || collector_type == kCollectorTypeGSS ||
585        collector_type == kCollectorTypeCC;
586  }
587  bool ShouldAllocLargeObject(mirror::Class* c, size_t byte_count) const
588      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
589  ALWAYS_INLINE void CheckConcurrentGC(Thread* self, size_t new_num_bytes_allocated,
590                                       mirror::Object** obj)
591      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
592
593  // We don't force this to be inlined since it is a slow path.
594  template <bool kInstrumented, typename PreFenceVisitor>
595  mirror::Object* AllocLargeObject(Thread* self, mirror::Class* klass, size_t byte_count,
596                                   const PreFenceVisitor& pre_fence_visitor)
597      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
598
599  // Handles Allocate()'s slow allocation path with GC involved after
600  // an initial allocation attempt failed.
601  mirror::Object* AllocateInternalWithGc(Thread* self, AllocatorType allocator, size_t num_bytes,
602                                         size_t* bytes_allocated, size_t* usable_size,
603                                         mirror::Class** klass)
604      LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
605      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
606
607  // Allocate into a specific space.
608  mirror::Object* AllocateInto(Thread* self, space::AllocSpace* space, mirror::Class* c,
609                               size_t bytes)
610      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
611
612  // Need to do this with mutators paused so that somebody doesn't accidentally allocate into the
613  // wrong space.
614  void SwapSemiSpaces() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
615
616  // Try to allocate a number of bytes, this function never does any GCs. Needs to be inlined so
617  // that the switch statement is constant optimized in the entrypoints.
618  template <const bool kInstrumented, const bool kGrow>
619  ALWAYS_INLINE mirror::Object* TryToAllocate(Thread* self, AllocatorType allocator_type,
620                                              size_t alloc_size, size_t* bytes_allocated,
621                                              size_t* usable_size)
622      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
623
624  void ThrowOutOfMemoryError(Thread* self, size_t byte_count, bool large_object_allocation)
625      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
626
627  template <bool kGrow>
628  bool IsOutOfMemoryOnAllocation(AllocatorType allocator_type, size_t alloc_size);
629
630  // Returns true if the address passed in is within the address range of a continuous space.
631  bool IsValidContinuousSpaceObjectAddress(const mirror::Object* obj) const
632      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
633
634  void EnqueueClearedReferences();
635  // Returns true if the reference object has not yet been enqueued.
636  void DelayReferenceReferent(mirror::Class* klass, mirror::Reference* ref,
637                              IsMarkedCallback is_marked_callback, void* arg)
638      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
639
640  // Run the finalizers.
641  void RunFinalization(JNIEnv* env);
642
643  // Blocks the caller until the garbage collector becomes idle and returns the type of GC we
644  // waited for.
645  collector::GcType WaitForGcToCompleteLocked(GcCause cause, Thread* self)
646      EXCLUSIVE_LOCKS_REQUIRED(gc_complete_lock_);
647
648  void RequestCollectorTransition(CollectorType desired_collector_type, uint64_t delta_time)
649      LOCKS_EXCLUDED(heap_trim_request_lock_);
650  void RequestHeapTrim() LOCKS_EXCLUDED(Locks::runtime_shutdown_lock_);
651  void RequestConcurrentGC(Thread* self) LOCKS_EXCLUDED(Locks::runtime_shutdown_lock_);
652  bool IsGCRequestPending() const;
653
654  // Sometimes CollectGarbageInternal decides to run a different Gc than you requested. Returns
655  // which type of Gc was actually ran.
656  collector::GcType CollectGarbageInternal(collector::GcType gc_plan, GcCause gc_cause,
657                                           bool clear_soft_references)
658      LOCKS_EXCLUDED(gc_complete_lock_,
659                     Locks::heap_bitmap_lock_,
660                     Locks::thread_suspend_count_lock_);
661
662  void PreGcVerification(collector::GarbageCollector* gc)
663      LOCKS_EXCLUDED(Locks::mutator_lock_);
664  void PreGcVerificationPaused(collector::GarbageCollector* gc)
665      EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
666  void PrePauseRosAllocVerification(collector::GarbageCollector* gc)
667      EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
668  void PreSweepingGcVerification(collector::GarbageCollector* gc)
669      EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
670  void PostGcVerification(collector::GarbageCollector* gc)
671      LOCKS_EXCLUDED(Locks::mutator_lock_);
672  void PostGcVerificationPaused(collector::GarbageCollector* gc)
673      EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
674
675  // Update the watermark for the native allocated bytes based on the current number of native
676  // bytes allocated and the target utilization ratio.
677  void UpdateMaxNativeFootprint();
678
679  // Find a collector based on GC type.
680  collector::GarbageCollector* FindCollectorByGcType(collector::GcType gc_type);
681
682  // Create the main free list space, typically either a RosAlloc space or DlMalloc space.
683  void CreateMainMallocSpace(MemMap* mem_map, size_t initial_size, size_t growth_limit,
684                             size_t capacity);
685
686  // Given the current contents of the alloc space, increase the allowed heap footprint to match
687  // the target utilization ratio.  This should only be called immediately after a full garbage
688  // collection.
689  void GrowForUtilization(collector::GarbageCollector* collector_ran);
690
691  size_t GetPercentFree();
692
693  static void VerificationCallback(mirror::Object* obj, void* arg)
694      SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
695
696  // Swap the allocation stack with the live stack.
697  void SwapStacks(Thread* self);
698
699  // Clear cards and update the mod union table.
700  void ProcessCards(TimingLogger& timings, bool use_rem_sets);
701
702  // Signal the heap trim daemon that there is something to do, either a heap transition or heap
703  // trim.
704  void SignalHeapTrimDaemon(Thread* self);
705
706  // Push an object onto the allocation stack.
707  void PushOnAllocationStack(Thread* self, mirror::Object** obj)
708      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
709
710  // What kind of concurrency behavior is the runtime after? Currently true for concurrent mark
711  // sweep GC, false for other GC types.
712  bool IsGcConcurrent() const ALWAYS_INLINE {
713    return collector_type_ == kCollectorTypeCMS || collector_type_ == kCollectorTypeCC;
714  }
715
716  // All-known continuous spaces, where objects lie within fixed bounds.
717  std::vector<space::ContinuousSpace*> continuous_spaces_;
718
719  // All-known discontinuous spaces, where objects may be placed throughout virtual memory.
720  std::vector<space::DiscontinuousSpace*> discontinuous_spaces_;
721
722  // All-known alloc spaces, where objects may be or have been allocated.
723  std::vector<space::AllocSpace*> alloc_spaces_;
724
725  // A space where non-movable objects are allocated, when compaction is enabled it contains
726  // Classes, ArtMethods, ArtFields, and non moving objects.
727  space::MallocSpace* non_moving_space_;
728
729  // Space which we use for the kAllocatorTypeROSAlloc.
730  space::RosAllocSpace* rosalloc_space_;
731
732  // Space which we use for the kAllocatorTypeDlMalloc.
733  space::DlMallocSpace* dlmalloc_space_;
734
735  // The main space is the space which the GC copies to and from on process state updates. This
736  // space is typically either the dlmalloc_space_ or the rosalloc_space_.
737  space::MallocSpace* main_space_;
738
739  // The large object space we are currently allocating into.
740  space::LargeObjectSpace* large_object_space_;
741
742  // The card table, dirtied by the write barrier.
743  UniquePtr<accounting::CardTable> card_table_;
744
745  // A mod-union table remembers all of the references from the it's space to other spaces.
746  SafeMap<space::Space*, accounting::ModUnionTable*> mod_union_tables_;
747
748  // A remembered set remembers all of the references from the it's space to the target space.
749  SafeMap<space::Space*, accounting::RememberedSet*> remembered_sets_;
750
751  // The current collector type.
752  CollectorType collector_type_;
753  // Which collector we use when the app is in the foreground.
754  CollectorType foreground_collector_type_;
755  // Which collector we will use when the app is notified of a transition to background.
756  CollectorType background_collector_type_;
757  // Desired collector type, heap trimming daemon transitions the heap if it is != collector_type_.
758  CollectorType desired_collector_type_;
759
760  // Lock which guards heap trim requests.
761  Mutex* heap_trim_request_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
762  // When we want to perform the next heap trim (nano seconds).
763  uint64_t last_trim_time_ GUARDED_BY(heap_trim_request_lock_);
764  // When we want to perform the next heap transition (nano seconds).
765  uint64_t heap_transition_target_time_ GUARDED_BY(heap_trim_request_lock_);
766  // If we have a heap trim request pending.
767  bool heap_trim_request_pending_ GUARDED_BY(heap_trim_request_lock_);
768
769  // How many GC threads we may use for paused parts of garbage collection.
770  const size_t parallel_gc_threads_;
771
772  // How many GC threads we may use for unpaused parts of garbage collection.
773  const size_t conc_gc_threads_;
774
775  // Boolean for if we are in low memory mode.
776  const bool low_memory_mode_;
777
778  // If we get a pause longer than long pause log threshold, then we print out the GC after it
779  // finishes.
780  const size_t long_pause_log_threshold_;
781
782  // If we get a GC longer than long GC log threshold, then we print out the GC after it finishes.
783  const size_t long_gc_log_threshold_;
784
785  // If we ignore the max footprint it lets the heap grow until it hits the heap capacity, this is
786  // useful for benchmarking since it reduces time spent in GC to a low %.
787  const bool ignore_max_footprint_;
788
789  // If we have a zygote space.
790  bool have_zygote_space_;
791
792  // Minimum allocation size of large object.
793  size_t large_object_threshold_;
794
795  // Guards access to the state of GC, associated conditional variable is used to signal when a GC
796  // completes.
797  Mutex* gc_complete_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
798  UniquePtr<ConditionVariable> gc_complete_cond_ GUARDED_BY(gc_complete_lock_);
799
800  // Reference queues.
801  ReferenceQueue soft_reference_queue_;
802  ReferenceQueue weak_reference_queue_;
803  ReferenceQueue finalizer_reference_queue_;
804  ReferenceQueue phantom_reference_queue_;
805  ReferenceQueue cleared_references_;
806
807  // True while the garbage collector is running.
808  volatile CollectorType collector_type_running_ GUARDED_BY(gc_complete_lock_);
809
810  // Last Gc type we ran. Used by WaitForConcurrentGc to know which Gc was waited on.
811  volatile collector::GcType last_gc_type_ GUARDED_BY(gc_complete_lock_);
812  collector::GcType next_gc_type_;
813
814  // Maximum size that the heap can reach.
815  const size_t capacity_;
816
817  // The size the heap is limited to. This is initially smaller than capacity, but for largeHeap
818  // programs it is "cleared" making it the same as capacity.
819  size_t growth_limit_;
820
821  // When the number of bytes allocated exceeds the footprint TryAllocate returns NULL indicating
822  // a GC should be triggered.
823  size_t max_allowed_footprint_;
824
825  // The watermark at which a concurrent GC is requested by registerNativeAllocation.
826  size_t native_footprint_gc_watermark_;
827
828  // The watermark at which a GC is performed inside of registerNativeAllocation.
829  size_t native_footprint_limit_;
830
831  // Whether or not we need to run finalizers in the next native allocation.
832  bool native_need_to_run_finalization_;
833
834  // Whether or not we currently care about pause times.
835  ProcessState process_state_;
836
837  // When num_bytes_allocated_ exceeds this amount then a concurrent GC should be requested so that
838  // it completes ahead of an allocation failing.
839  size_t concurrent_start_bytes_;
840
841  // Since the heap was created, how many bytes have been freed.
842  size_t total_bytes_freed_ever_;
843
844  // Since the heap was created, how many objects have been freed.
845  size_t total_objects_freed_ever_;
846
847  // Number of bytes allocated.  Adjusted after each allocation and free.
848  Atomic<size_t> num_bytes_allocated_;
849
850  // Bytes which are allocated and managed by native code but still need to be accounted for.
851  Atomic<size_t> native_bytes_allocated_;
852
853  // Data structure GC overhead.
854  Atomic<size_t> gc_memory_overhead_;
855
856  // Heap verification flags.
857  const bool verify_missing_card_marks_;
858  const bool verify_system_weaks_;
859  const bool verify_pre_gc_heap_;
860  const bool verify_pre_sweeping_heap_;
861  const bool verify_post_gc_heap_;
862  const bool verify_mod_union_table_;
863  bool verify_pre_gc_rosalloc_;
864  bool verify_pre_sweeping_rosalloc_;
865  bool verify_post_gc_rosalloc_;
866
867  // RAII that temporarily disables the rosalloc verification during
868  // the zygote fork.
869  class ScopedDisableRosAllocVerification {
870   private:
871    Heap* const heap_;
872    const bool orig_verify_pre_gc_;
873    const bool orig_verify_pre_sweeping_;
874    const bool orig_verify_post_gc_;
875
876   public:
877    explicit ScopedDisableRosAllocVerification(Heap* heap)
878        : heap_(heap),
879          orig_verify_pre_gc_(heap_->verify_pre_gc_rosalloc_),
880          orig_verify_pre_sweeping_(heap_->verify_pre_sweeping_rosalloc_),
881          orig_verify_post_gc_(heap_->verify_post_gc_rosalloc_) {
882      heap_->verify_pre_gc_rosalloc_ = false;
883      heap_->verify_pre_sweeping_rosalloc_ = false;
884      heap_->verify_post_gc_rosalloc_ = false;
885    }
886    ~ScopedDisableRosAllocVerification() {
887      heap_->verify_pre_gc_rosalloc_ = orig_verify_pre_gc_;
888      heap_->verify_pre_sweeping_rosalloc_ = orig_verify_pre_sweeping_;
889      heap_->verify_post_gc_rosalloc_ = orig_verify_post_gc_;
890    }
891  };
892
893  // Parallel GC data structures.
894  UniquePtr<ThreadPool> thread_pool_;
895
896  // The nanosecond time at which the last GC ended.
897  uint64_t last_gc_time_ns_;
898
899  // How many bytes were allocated at the end of the last GC.
900  uint64_t last_gc_size_;
901
902  // Estimated allocation rate (bytes / second). Computed between the time of the last GC cycle
903  // and the start of the current one.
904  uint64_t allocation_rate_;
905
906  // For a GC cycle, a bitmap that is set corresponding to the
907  UniquePtr<accounting::HeapBitmap> live_bitmap_ GUARDED_BY(Locks::heap_bitmap_lock_);
908  UniquePtr<accounting::HeapBitmap> mark_bitmap_ GUARDED_BY(Locks::heap_bitmap_lock_);
909
910  // Mark stack that we reuse to avoid re-allocating the mark stack.
911  UniquePtr<accounting::ObjectStack> mark_stack_;
912
913  // Allocation stack, new allocations go here so that we can do sticky mark bits. This enables us
914  // to use the live bitmap as the old mark bitmap.
915  const size_t max_allocation_stack_size_;
916  UniquePtr<accounting::ObjectStack> allocation_stack_;
917
918  // Second allocation stack so that we can process allocation with the heap unlocked.
919  UniquePtr<accounting::ObjectStack> live_stack_;
920
921  // Allocator type.
922  AllocatorType current_allocator_;
923  const AllocatorType current_non_moving_allocator_;
924
925  // Which GCs we run in order when we an allocation fails.
926  std::vector<collector::GcType> gc_plan_;
927
928  // Bump pointer spaces.
929  space::BumpPointerSpace* bump_pointer_space_;
930  // Temp space is the space which the semispace collector copies to.
931  space::BumpPointerSpace* temp_space_;
932
933  // Minimum free guarantees that you always have at least min_free_ free bytes after growing for
934  // utilization, regardless of target utilization ratio.
935  size_t min_free_;
936
937  // The ideal maximum free size, when we grow the heap for utilization.
938  size_t max_free_;
939
940  // Target ideal heap utilization ratio
941  double target_utilization_;
942
943  // How much more we grow the heap when we are a foreground app instead of background.
944  double foreground_heap_growth_multiplier_;
945
946  // Total time which mutators are paused or waiting for GC to complete.
947  uint64_t total_wait_time_;
948
949  // Total number of objects allocated in microseconds.
950  AtomicInteger total_allocation_time_;
951
952  // The current state of heap verification, may be enabled or disabled.
953  VerifyObjectMode verify_object_mode_;
954
955  // Compacting GC disable count, prevents compacting GC from running iff > 0.
956  size_t disable_moving_gc_count_ GUARDED_BY(gc_complete_lock_);
957
958  std::vector<collector::GarbageCollector*> garbage_collectors_;
959  collector::SemiSpace* semi_space_collector_;
960  collector::ConcurrentCopying* concurrent_copying_collector_;
961
962  const bool running_on_valgrind_;
963  const bool use_tlab_;
964
965  friend class collector::GarbageCollector;
966  friend class collector::MarkSweep;
967  friend class collector::SemiSpace;
968  friend class ReferenceQueue;
969  friend class VerifyReferenceCardVisitor;
970  friend class VerifyReferenceVisitor;
971  friend class VerifyObjectVisitor;
972  friend class ScopedHeapLock;
973  friend class space::SpaceTest;
974
975  class AllocationTimer {
976   private:
977    Heap* heap_;
978    mirror::Object** allocated_obj_ptr_;
979    uint64_t allocation_start_time_;
980   public:
981    AllocationTimer(Heap* heap, mirror::Object** allocated_obj_ptr);
982    ~AllocationTimer();
983  };
984
985  DISALLOW_IMPLICIT_CONSTRUCTORS(Heap);
986};
987
988}  // namespace gc
989}  // namespace art
990
991#endif  // ART_RUNTIME_GC_HEAP_H_
992