garbage_collector.h revision 6f4ffe41649f1e6381e8cda087ad3749206806e5
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/timing_logger.h"
22#include "gc/gc_cause.h"
23#include "gc_type.h"
24#include "locks.h"
25#include <stdint.h>
26#include <vector>
27
28namespace art {
29namespace gc {
30
31class Heap;
32
33namespace collector {
34
35class GarbageCollector {
36 public:
37  // Returns true iff the garbage collector is concurrent.
38  virtual bool IsConcurrent() const = 0;
39
40  GarbageCollector(Heap* heap, const std::string& name);
41  virtual ~GarbageCollector() { }
42
43  const char* GetName() const {
44    return name_.c_str();
45  }
46
47  virtual GcType GetGcType() 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
72  CumulativeLogger& GetCumulativeTimings() {
73    return cumulative_timings_;
74  }
75
76  void ResetCumulativeStatistics();
77
78  // Swap the live and mark bitmaps of spaces that are active for the collector. For partial GC,
79  // this is the allocation space, for full GC then we swap the zygote bitmaps too.
80  void SwapBitmaps() EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
81
82  size_t GetFreedBytes() const {
83    return freed_bytes_;
84  }
85
86  size_t GetFreedLargeObjectBytes() const {
87    return freed_large_object_bytes_;
88  }
89
90  size_t GetFreedObjects() const {
91    return freed_objects_;
92  }
93
94  size_t GetFreedLargeObjects() const {
95    return freed_large_objects_;
96  }
97
98  uint64_t GetTotalPausedTimeNs() const {
99    return pause_histogram_.Sum();
100  }
101
102  uint64_t GetTotalFreedBytes() const {
103    return total_freed_bytes_;
104  }
105
106  uint64_t GetTotalFreedObjects() const {
107    return total_freed_objects_;
108  }
109
110  const Histogram<uint64_t>& GetPauseHistogram() const {
111    return pause_histogram_;
112  }
113
114 protected:
115  // The initial phase. Done without mutators paused.
116  virtual void InitializePhase() = 0;
117
118  // Mark all reachable objects, done concurrently.
119  virtual void MarkingPhase() = 0;
120
121  // Only called for concurrent GCs. Gets called repeatedly until it succeeds.
122  virtual bool HandleDirtyObjectsPhase() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
123
124  // Called with mutators running.
125  virtual void ReclaimPhase() = 0;
126
127  // Called after the GC is finished. Done without mutators paused.
128  virtual void FinishPhase() = 0;
129
130  static constexpr size_t kPauseBucketSize = 500;
131  static constexpr size_t kPauseBucketCount = 32;
132
133  Heap* const heap_;
134
135  std::string name_;
136
137  GcCause gc_cause_;
138  bool clear_soft_references_;
139
140  const bool verbose_;
141
142  uint64_t duration_ns_;
143  TimingLogger timings_;
144
145  // Cumulative statistics.
146  Histogram<uint64_t> pause_histogram_;
147  uint64_t total_time_ns_;
148  uint64_t total_freed_objects_;
149  uint64_t total_freed_bytes_;
150
151  // Single GC statitstics.
152  AtomicInteger freed_bytes_;
153  AtomicInteger freed_large_object_bytes_;
154  AtomicInteger freed_objects_;
155  AtomicInteger freed_large_objects_;
156
157  CumulativeLogger cumulative_timings_;
158
159  std::vector<uint64_t> pause_times_;
160};
161
162}  // namespace collector
163}  // namespace gc
164}  // namespace art
165
166#endif  // ART_RUNTIME_GC_COLLECTOR_GARBAGE_COLLECTOR_H_
167