garbage_collector.h revision 0f7bf6a3ad1798fde328a2bff48a4bf2d750a36b
1/* 2 * Copyright (C) 2012 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_COLLECTOR_GARBAGE_COLLECTOR_H_ 18#define ART_RUNTIME_GC_COLLECTOR_GARBAGE_COLLECTOR_H_ 19 20#include "base/histogram.h" 21#include "base/mutex.h" 22#include "base/timing_logger.h" 23#include "gc/collector_type.h" 24#include "gc/gc_cause.h" 25#include "gc_type.h" 26#include <stdint.h> 27#include <vector> 28 29namespace art { 30namespace gc { 31 32class Heap; 33 34namespace collector { 35 36class GarbageCollector { 37 public: 38 GarbageCollector(Heap* heap, const std::string& name); 39 virtual ~GarbageCollector() { } 40 41 const char* GetName() const { 42 return name_.c_str(); 43 } 44 45 virtual GcType GetGcType() const = 0; 46 47 virtual CollectorType GetCollectorType() const = 0; 48 49 // Run the garbage collector. 50 void Run(GcCause gc_cause, bool clear_soft_references); 51 52 Heap* GetHeap() const { 53 return heap_; 54 } 55 56 // Returns how long the mutators were paused in nanoseconds. 57 const std::vector<uint64_t>& GetPauseTimes() const { 58 return pause_times_; 59 } 60 61 // Returns how long the GC took to complete in nanoseconds. 62 uint64_t GetDurationNs() const { 63 return duration_ns_; 64 } 65 66 void RegisterPause(uint64_t nano_length); 67 68 TimingLogger& GetTimings() { 69 return timings_; 70 } 71 const CumulativeLogger& GetCumulativeTimings() const { 72 return cumulative_timings_; 73 } 74 75 void ResetCumulativeStatistics(); 76 77 // Swap the live and mark bitmaps of spaces that are active for the collector. For partial GC, 78 // this is the allocation space, for full GC then we swap the zygote bitmaps too. 79 void SwapBitmaps() EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_); 80 81 size_t GetFreedBytes() const { 82 return freed_bytes_; 83 } 84 85 size_t GetFreedLargeObjectBytes() const { 86 return freed_large_object_bytes_; 87 } 88 89 size_t GetFreedObjects() const { 90 return freed_objects_; 91 } 92 93 size_t GetFreedLargeObjects() const { 94 return freed_large_objects_; 95 } 96 97 uint64_t GetTotalPausedTimeNs() const { 98 return pause_histogram_.Sum(); 99 } 100 101 uint64_t GetTotalFreedBytes() const { 102 return total_freed_bytes_; 103 } 104 105 uint64_t GetTotalFreedObjects() const { 106 return total_freed_objects_; 107 } 108 109 const Histogram<uint64_t>& GetPauseHistogram() const { 110 return pause_histogram_; 111 } 112 113 // Returns the estimated throughput in bytes / second. 114 uint64_t GetEstimatedMeanThroughput() const; 115 116 // Returns the estimated throughput of the last GC iteration. 117 uint64_t GetEstimatedLastIterationThroughput() const; 118 119 // Returns how many GC iterations have been run. 120 size_t GetIterations() const { 121 return GetCumulativeTimings().GetIterations(); 122 } 123 124 protected: 125 // The initial phase. Done without mutators paused. 126 virtual void InitializePhase() = 0; 127 128 // Mark all reachable objects, done concurrently. 129 virtual void MarkingPhase() = 0; 130 131 // Only called for concurrent GCs. 132 virtual void PausePhase(); 133 134 // Called with mutators running. 135 virtual void ReclaimPhase() = 0; 136 137 // Called after the GC is finished. Done without mutators paused. 138 virtual void FinishPhase() = 0; 139 140 // Revoke all the thread-local buffers. 141 virtual void RevokeAllThreadLocalBuffers() = 0; 142 143 static constexpr size_t kPauseBucketSize = 500; 144 static constexpr size_t kPauseBucketCount = 32; 145 146 Heap* const heap_; 147 148 std::string name_; 149 150 GcCause gc_cause_; 151 bool clear_soft_references_; 152 153 uint64_t duration_ns_; 154 TimingLogger timings_; 155 156 // Cumulative statistics. 157 Histogram<uint64_t> pause_histogram_; 158 uint64_t total_time_ns_; 159 uint64_t total_freed_objects_; 160 uint64_t total_freed_bytes_; 161 162 // Single GC statitstics. 163 AtomicInteger freed_bytes_; 164 AtomicInteger freed_large_object_bytes_; 165 AtomicInteger freed_objects_; 166 AtomicInteger freed_large_objects_; 167 168 CumulativeLogger cumulative_timings_; 169 170 std::vector<uint64_t> pause_times_; 171}; 172 173} // namespace collector 174} // namespace gc 175} // namespace art 176 177#endif // ART_RUNTIME_GC_COLLECTOR_GARBAGE_COLLECTOR_H_ 178