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