garbage_collector.h revision b2f9936cab87a187f078187c22d9b29d4a188a62
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_type.h"
23#include "locks.h"
24#include <stdint.h>
25#include <vector>
26
27namespace art {
28namespace gc {
29
30class Heap;
31
32namespace collector {
33
34class GarbageCollector {
35 public:
36  // Returns true iff the garbage collector is concurrent.
37  virtual bool IsConcurrent() const = 0;
38
39  GarbageCollector(Heap* heap, const std::string& name);
40  virtual ~GarbageCollector() { }
41
42  const char* GetName() const {
43    return name_.c_str();
44  }
45
46  virtual GcType GetGcType() const = 0;
47
48  // Run the garbage collector.
49  void Run(bool clear_soft_references);
50
51  Heap* GetHeap() const {
52    return heap_;
53  }
54
55  // Returns how long the mutators were paused in nanoseconds.
56  const std::vector<uint64_t>& GetPauseTimes() const {
57    return pause_times_;
58  }
59
60  // Returns how long the GC took to complete in nanoseconds.
61  uint64_t GetDurationNs() const {
62    return duration_ns_;
63  }
64
65  void RegisterPause(uint64_t nano_length);
66
67  TimingLogger& GetTimings() {
68    return timings_;
69  }
70
71  CumulativeLogger& GetCumulativeTimings() {
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 protected:
114  // The initial phase. Done without mutators paused.
115  virtual void InitializePhase() = 0;
116
117  // Mark all reachable objects, done concurrently.
118  virtual void MarkingPhase() = 0;
119
120  // Only called for concurrent GCs. Gets called repeatedly until it succeeds.
121  virtual bool HandleDirtyObjectsPhase() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
122
123  // Called with mutators running.
124  virtual void ReclaimPhase() = 0;
125
126  // Called after the GC is finished. Done without mutators paused.
127  virtual void FinishPhase() = 0;
128
129  static constexpr size_t kPauseBucketSize = 500;
130  static constexpr size_t kPauseBucketCount = 32;
131
132  Heap* const heap_;
133
134  std::string name_;
135
136  bool clear_soft_references_;
137
138  const bool verbose_;
139
140  uint64_t duration_ns_;
141  TimingLogger timings_;
142
143  // Cumulative statistics.
144  Histogram<uint64_t> pause_histogram_;
145  uint64_t total_time_ns_;
146  uint64_t total_freed_objects_;
147  uint64_t total_freed_bytes_;
148
149  // Single GC statitstics.
150  AtomicInteger freed_bytes_;
151  AtomicInteger freed_large_object_bytes_;
152  AtomicInteger freed_objects_;
153  AtomicInteger freed_large_objects_;
154
155  CumulativeLogger cumulative_timings_;
156
157  std::vector<uint64_t> pause_times_;
158};
159
160}  // namespace collector
161}  // namespace gc
162}  // namespace art
163
164#endif  // ART_RUNTIME_GC_COLLECTOR_GARBAGE_COLLECTOR_H_
165