garbage_collector.h revision 6f28d91aab952e3244fbb4e707fa38f85538f374
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 "gc_type.h"
21#include "locks.h"
22#include "base/timing_logger.h"
23
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();
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  base::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 protected:
82  // The initial phase. Done without mutators paused.
83  virtual void InitializePhase() = 0;
84
85  // Mark all reachable objects, done concurrently.
86  virtual void MarkingPhase() = 0;
87
88  // Only called for concurrent GCs. Gets called repeatedly until it succeeds.
89  virtual bool HandleDirtyObjectsPhase() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
90
91  // Called with mutators running.
92  virtual void ReclaimPhase() = 0;
93
94  // Called after the GC is finished. Done without mutators paused.
95  virtual void FinishPhase() = 0;
96
97  Heap* const heap_;
98
99  std::string name_;
100
101  const bool verbose_;
102
103  uint64_t duration_ns_;
104  base::TimingLogger timings_;
105
106  // Cumulative statistics.
107  uint64_t total_time_ns_;
108  uint64_t total_paused_time_ns_;
109  uint64_t total_freed_objects_;
110  uint64_t total_freed_bytes_;
111
112  CumulativeLogger cumulative_timings_;
113
114  std::vector<uint64_t> pause_times_;
115};
116
117}  // namespace collector
118}  // namespace gc
119}  // namespace art
120
121#endif  // ART_RUNTIME_GC_COLLECTOR_GARBAGE_COLLECTOR_H_
122