Home | History | Annotate | Download | only in collector
      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 <vector>
     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);
     63   virtual ~GarbageCollector() { }
     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_);
     71   Heap* GetHeap() const {
     72     return heap_;
     73   }
     74   void RegisterPause(uint64_t nano_length);
     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_);
     84   uint64_t GetTotalPausedTimeNs() REQUIRES(!pause_histogram_lock_);
     85   int64_t GetTotalFreedBytes() const {
     86     return total_freed_bytes_;
     87   }
     88   uint64_t GetTotalFreedObjects() const {
     89     return total_freed_objects_;
     90   }
     91   // Reset the cumulative timings and pause histogram.
     92   void ResetMeasurements() REQUIRES(!pause_histogram_lock_);
     93   // Returns the estimated throughput in bytes / second.
     94   uint64_t GetEstimatedMeanThroughput() const;
     95   // Returns how many GC iterations have been run.
     96   size_t NumberOfIterations() const {
     97     return GetCumulativeTimings().GetIterations();
     98   }
     99   // Returns the current GC iteration and assocated info.
    100   Iteration* GetCurrentIteration();
    101   const Iteration* GetCurrentIteration() const;
    102   TimingLogger* GetTimings() {
    103     return &GetCurrentIteration()->timings_;
    104   }
    105   // Record a free of normal objects.
    106   void RecordFree(const ObjectBytePair& freed);
    107   // Record a free of large objects.
    108   void RecordFreeLOS(const ObjectBytePair& freed);
    109   virtual void DumpPerformanceInfo(std::ostream& os) REQUIRES(!pause_histogram_lock_);
    110 
    111   // Helper functions for querying if objects are marked. These are used for processing references,
    112   // and will be used for reading system weaks while the GC is running.
    113   virtual mirror::Object* IsMarked(mirror::Object* obj)
    114       REQUIRES_SHARED(Locks::mutator_lock_) = 0;
    115   // Returns true if the given heap reference is null or is already marked. If it's already marked,
    116   // update the reference (uses a CAS if do_atomic_update is true. Otherwise, returns false.
    117   virtual bool IsNullOrMarkedHeapReference(mirror::HeapReference<mirror::Object>* obj,
    118                                            bool do_atomic_update)
    119       REQUIRES_SHARED(Locks::mutator_lock_) = 0;
    120   // Used by reference processor.
    121   virtual void ProcessMarkStack() REQUIRES_SHARED(Locks::mutator_lock_) = 0;
    122   // Force mark an object.
    123   virtual mirror::Object* MarkObject(mirror::Object* obj)
    124       REQUIRES_SHARED(Locks::mutator_lock_) = 0;
    125   virtual void MarkHeapReference(mirror::HeapReference<mirror::Object>* obj,
    126                                  bool do_atomic_update)
    127       REQUIRES_SHARED(Locks::mutator_lock_) = 0;
    128   virtual void DelayReferenceReferent(ObjPtr<mirror::Class> klass,
    129                                       ObjPtr<mirror::Reference> reference)
    130       REQUIRES_SHARED(Locks::mutator_lock_) = 0;
    131 
    132   bool IsTransactionActive() const {
    133     return is_transaction_active_;
    134   }
    135 
    136  protected:
    137   // Run all of the GC phases.
    138   virtual void RunPhases() = 0;
    139   // Revoke all the thread-local buffers.
    140   virtual void RevokeAllThreadLocalBuffers() = 0;
    141 
    142   static constexpr size_t kPauseBucketSize = 500;
    143   static constexpr size_t kPauseBucketCount = 32;
    144 
    145   Heap* const heap_;
    146   std::string name_;
    147   // Cumulative statistics.
    148   Histogram<uint64_t> pause_histogram_ GUARDED_BY(pause_histogram_lock_);
    149   uint64_t total_time_ns_;
    150   uint64_t total_freed_objects_;
    151   int64_t total_freed_bytes_;
    152   CumulativeLogger cumulative_timings_;
    153   mutable Mutex pause_histogram_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
    154   bool is_transaction_active_;
    155 
    156  private:
    157   DISALLOW_IMPLICIT_CONSTRUCTORS(GarbageCollector);
    158 };
    159 
    160 }  // namespace collector
    161 }  // namespace gc
    162 }  // namespace art
    163 
    164 #endif  // ART_RUNTIME_GC_COLLECTOR_GARBAGE_COLLECTOR_H_
    165