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 <stdint.h>
21 #include <list>
22 
23 #include "base/histogram.h"
24 #include "base/mutex.h"
25 #include "base/timing_logger.h"
26 #include "gc/collector_type.h"
27 #include "gc/gc_cause.h"
28 #include "gc_root.h"
29 #include "gc_type.h"
30 #include "iteration.h"
31 #include "object_byte_pair.h"
32 #include "object_callbacks.h"
33 
34 namespace art {
35 
36 namespace mirror {
37 class Class;
38 class Object;
39 class Reference;
40 }  // namespace mirror
41 
42 namespace gc {
43 
44 class Heap;
45 
46 namespace collector {
47 
48 class GarbageCollector : public RootVisitor, public IsMarkedVisitor, public MarkObjectVisitor {
49  public:
50   class SCOPED_LOCKABLE ScopedPause {
51    public:
52     explicit ScopedPause(GarbageCollector* collector, bool with_reporting = true)
53         EXCLUSIVE_LOCK_FUNCTION(Locks::mutator_lock_);
54     ~ScopedPause() UNLOCK_FUNCTION();
55 
56    private:
57     const uint64_t start_time_;
58     GarbageCollector* const collector_;
59     bool with_reporting_;
60   };
61 
62   GarbageCollector(Heap* heap, const std::string& name);
~GarbageCollector()63   virtual ~GarbageCollector() { }
GetName()64   const char* GetName() const {
65     return name_.c_str();
66   }
67   virtual GcType GetGcType() const = 0;
68   virtual CollectorType GetCollectorType() const = 0;
69   // Run the garbage collector.
70   void Run(GcCause gc_cause, bool clear_soft_references) REQUIRES(!pause_histogram_lock_);
GetHeap()71   Heap* GetHeap() const {
72     return heap_;
73   }
74   void RegisterPause(uint64_t nano_length);
GetCumulativeTimings()75   const CumulativeLogger& GetCumulativeTimings() const {
76     return cumulative_timings_;
77   }
78   void ResetCumulativeStatistics() REQUIRES(!pause_histogram_lock_);
79   // Swap the live and mark bitmaps of spaces that are active for the collector. For partial GC,
80   // this is the allocation space, for full GC then we swap the zygote bitmaps too.
81   void SwapBitmaps()
82       REQUIRES(Locks::heap_bitmap_lock_)
83       REQUIRES_SHARED(Locks::mutator_lock_);
GetTotalCpuTime()84   uint64_t GetTotalCpuTime() const {
85     return total_thread_cpu_time_ns_;
86   }
87   uint64_t GetTotalPausedTimeNs() REQUIRES(!pause_histogram_lock_);
GetTotalFreedBytes()88   int64_t GetTotalFreedBytes() const {
89     return total_freed_bytes_;
90   }
GetTotalFreedObjects()91   uint64_t GetTotalFreedObjects() const {
92     return total_freed_objects_;
93   }
94   // Reset the cumulative timings and pause histogram.
95   void ResetMeasurements() REQUIRES(!pause_histogram_lock_);
96   // Returns the estimated throughput in bytes / second.
97   uint64_t GetEstimatedMeanThroughput() const;
98   // Returns how many GC iterations have been run.
NumberOfIterations()99   size_t NumberOfIterations() const {
100     return GetCumulativeTimings().GetIterations();
101   }
102   // Returns the current GC iteration and assocated info.
103   Iteration* GetCurrentIteration();
104   const Iteration* GetCurrentIteration() const;
GetTimings()105   TimingLogger* GetTimings() {
106     return &GetCurrentIteration()->timings_;
107   }
108   // Record a free of normal objects.
109   void RecordFree(const ObjectBytePair& freed);
110   // Record a free of large objects.
111   void RecordFreeLOS(const ObjectBytePair& freed);
112   virtual void DumpPerformanceInfo(std::ostream& os) REQUIRES(!pause_histogram_lock_);
113 
114   // Extract RSS for GC-specific memory ranges using mincore().
115   uint64_t ExtractRssFromMincore(std::list<std::pair<void*, void*>>* gc_ranges);
116 
117   // Helper functions for querying if objects are marked. These are used for processing references,
118   // and will be used for reading system weaks while the GC is running.
119   virtual mirror::Object* IsMarked(mirror::Object* obj)
120       REQUIRES_SHARED(Locks::mutator_lock_) = 0;
121   // Returns true if the given heap reference is null or is already marked. If it's already marked,
122   // update the reference (uses a CAS if do_atomic_update is true). Otherwise, returns false.
123   virtual bool IsNullOrMarkedHeapReference(mirror::HeapReference<mirror::Object>* obj,
124                                            bool do_atomic_update)
125       REQUIRES_SHARED(Locks::mutator_lock_) = 0;
126   // Used by reference processor.
127   virtual void ProcessMarkStack() REQUIRES_SHARED(Locks::mutator_lock_) = 0;
128   // Force mark an object.
129   virtual mirror::Object* MarkObject(mirror::Object* obj)
130       REQUIRES_SHARED(Locks::mutator_lock_) = 0;
131   virtual void MarkHeapReference(mirror::HeapReference<mirror::Object>* obj,
132                                  bool do_atomic_update)
133       REQUIRES_SHARED(Locks::mutator_lock_) = 0;
134   virtual void DelayReferenceReferent(ObjPtr<mirror::Class> klass,
135                                       ObjPtr<mirror::Reference> reference)
136       REQUIRES_SHARED(Locks::mutator_lock_) = 0;
137 
IsTransactionActive()138   bool IsTransactionActive() const {
139     return is_transaction_active_;
140   }
141 
142  protected:
143   // Run all of the GC phases.
144   virtual void RunPhases() = 0;
145   // Revoke all the thread-local buffers.
146   virtual void RevokeAllThreadLocalBuffers() = 0;
147 
148   static constexpr size_t kPauseBucketSize = 500;
149   static constexpr size_t kPauseBucketCount = 32;
150   static constexpr size_t kMemBucketSize = 10;
151   static constexpr size_t kMemBucketCount = 16;
152 
153   Heap* const heap_;
154   std::string name_;
155   // Cumulative statistics.
156   Histogram<uint64_t> pause_histogram_ GUARDED_BY(pause_histogram_lock_);
157   Histogram<uint64_t> rss_histogram_;
158   Histogram<size_t> freed_bytes_histogram_;
159   uint64_t total_thread_cpu_time_ns_;
160   uint64_t total_time_ns_;
161   uint64_t total_freed_objects_;
162   int64_t total_freed_bytes_;
163   CumulativeLogger cumulative_timings_;
164   mutable Mutex pause_histogram_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
165   bool is_transaction_active_;
166 
167  private:
168   DISALLOW_IMPLICIT_CONSTRUCTORS(GarbageCollector);
169 };
170 
171 }  // namespace collector
172 }  // namespace gc
173 }  // namespace art
174 
175 #endif  // ART_RUNTIME_GC_COLLECTOR_GARBAGE_COLLECTOR_H_
176