garbage_collector.h revision e76e70f424468f311c2061c291e8384263f3968c
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 class SCOPED_LOCKABLE ScopedPause { 39 public: 40 explicit ScopedPause(GarbageCollector* collector) EXCLUSIVE_LOCK_FUNCTION(Locks::mutator_lock_); 41 ~ScopedPause() UNLOCK_FUNCTION(); 42 43 private: 44 const uint64_t start_time_; 45 GarbageCollector* const collector_; 46 }; 47 48 GarbageCollector(Heap* heap, const std::string& name); 49 virtual ~GarbageCollector() { } 50 51 const char* GetName() const { 52 return name_.c_str(); 53 } 54 55 virtual GcType GetGcType() const = 0; 56 57 virtual CollectorType GetCollectorType() const = 0; 58 59 // Run the garbage collector. 60 void Run(GcCause gc_cause, bool clear_soft_references); 61 62 Heap* GetHeap() const { 63 return heap_; 64 } 65 66 // Returns how long the mutators were paused in nanoseconds. 67 const std::vector<uint64_t>& GetPauseTimes() const { 68 return pause_times_; 69 } 70 71 // Returns how long the GC took to complete in nanoseconds. 72 uint64_t GetDurationNs() const { 73 return duration_ns_; 74 } 75 76 void RegisterPause(uint64_t nano_length); 77 78 TimingLogger& GetTimings() { 79 return timings_; 80 } 81 const CumulativeLogger& GetCumulativeTimings() const { 82 return cumulative_timings_; 83 } 84 85 void ResetCumulativeStatistics(); 86 87 // Swap the live and mark bitmaps of spaces that are active for the collector. For partial GC, 88 // this is the allocation space, for full GC then we swap the zygote bitmaps too. 89 void SwapBitmaps() EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_); 90 91 int64_t GetFreedBytes() const { 92 return freed_bytes_; 93 } 94 95 int64_t GetFreedLargeObjectBytes() const { 96 return freed_large_object_bytes_; 97 } 98 99 uint64_t GetFreedObjects() const { 100 return freed_objects_; 101 } 102 103 uint64_t GetFreedLargeObjects() const { 104 return freed_large_objects_; 105 } 106 107 uint64_t GetTotalPausedTimeNs() const { 108 return pause_histogram_.Sum(); 109 } 110 111 int64_t GetTotalFreedBytes() const { 112 return total_freed_bytes_; 113 } 114 115 uint64_t GetTotalFreedObjects() const { 116 return total_freed_objects_; 117 } 118 119 const Histogram<uint64_t>& GetPauseHistogram() const { 120 return pause_histogram_; 121 } 122 123 // Reset the cumulative timings and pause histogram. 124 void ResetMeasurements(); 125 126 // Returns the estimated throughput in bytes / second. 127 uint64_t GetEstimatedMeanThroughput() const; 128 129 // Returns the estimated throughput of the last GC iteration. 130 uint64_t GetEstimatedLastIterationThroughput() const; 131 132 // Returns how many GC iterations have been run. 133 size_t GetIterations() const { 134 return GetCumulativeTimings().GetIterations(); 135 } 136 137 protected: 138 // Run all of the GC phases. 139 virtual void RunPhases() = 0; 140 141 // Revoke all the thread-local buffers. 142 virtual void RevokeAllThreadLocalBuffers() = 0; 143 144 // Record that you have freed some objects or large objects, calls Heap::RecordFree. 145 // TODO: These are not thread safe, add a lock if we get have parallel sweeping. 146 void RecordFree(uint64_t freed_objects, int64_t freed_bytes); 147 void RecordFreeLargeObjects(uint64_t freed_objects, int64_t freed_bytes); 148 149 static constexpr size_t kPauseBucketSize = 500; 150 static constexpr size_t kPauseBucketCount = 32; 151 152 Heap* const heap_; 153 154 std::string name_; 155 156 GcCause gc_cause_; 157 bool clear_soft_references_; 158 159 uint64_t duration_ns_; 160 TimingLogger timings_; 161 162 // Cumulative statistics. 163 Histogram<uint64_t> pause_histogram_; 164 uint64_t total_time_ns_; 165 uint64_t total_freed_objects_; 166 int64_t total_freed_bytes_; 167 168 // Single GC statitstics, freed bytes are signed since the GC can free negative bytes if it 169 // promotes objects to a space which has a larger allocation size. 170 int64_t freed_bytes_; 171 int64_t freed_large_object_bytes_; 172 uint64_t freed_objects_; 173 uint64_t freed_large_objects_; 174 175 CumulativeLogger cumulative_timings_; 176 177 std::vector<uint64_t> pause_times_; 178}; 179 180} // namespace collector 181} // namespace gc 182} // namespace art 183 184#endif // ART_RUNTIME_GC_COLLECTOR_GARBAGE_COLLECTOR_H_ 185