getRuntimeStat() support (ART).

Export some runtime stats (currently GC stats) via
VMDebug.getRuntimeStat().

Added several new GC stats such as blocking GC counts and GC count
histograms.

Bug: 19825248
Change-Id: I8ece9ed241dc3982dfd983d7159090ba82940dce
diff --git a/runtime/gc/heap.h b/runtime/gc/heap.h
index 603cbfd..2f62798 100644
--- a/runtime/gc/heap.h
+++ b/runtime/gc/heap.h
@@ -672,6 +672,14 @@
     min_interval_homogeneous_space_compaction_by_oom_ = interval;
   }
 
+  // Helpers for android.os.Debug.getRuntimeStat().
+  uint64_t GetGcCount() const;
+  uint64_t GetGcTime() const;
+  uint64_t GetBlockingGcCount() const;
+  uint64_t GetBlockingGcTime() const;
+  void DumpGcCountRateHistogram(std::ostream& os) const;
+  void DumpBlockingGcCountRateHistogram(std::ostream& os) const;
+
  private:
   class ConcurrentGCTask;
   class CollectorTransitionTask;
@@ -873,6 +881,8 @@
       EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
       LOCKS_EXCLUDED(Locks::heap_bitmap_lock_);
 
+  void UpdateGcCountRateHistograms() EXCLUSIVE_LOCKS_REQUIRED(gc_complete_lock_);
+
   // All-known continuous spaces, where objects lie within fixed bounds.
   std::vector<space::ContinuousSpace*> continuous_spaces_;
 
@@ -1156,6 +1166,28 @@
   // Whether or not we use homogeneous space compaction to avoid OOM errors.
   bool use_homogeneous_space_compaction_for_oom_;
 
+  // True if the currently running collection has made some thread wait.
+  bool running_collection_is_blocking_ GUARDED_BY(gc_complete_lock_);
+  // The number of blocking GC runs.
+  uint64_t blocking_gc_count_;
+  // The total duration of blocking GC runs.
+  uint64_t blocking_gc_time_;
+  // The duration of the window for the GC count rate histograms.
+  static constexpr uint64_t kGcCountRateHistogramWindowDuration = MsToNs(10 * 1000);  // 10s.
+  // The last time when the GC count rate histograms were updated.
+  // This is rounded by kGcCountRateHistogramWindowDuration (a multiple of 10s).
+  uint64_t last_update_time_gc_count_rate_histograms_;
+  // The running count of GC runs in the last window.
+  uint64_t gc_count_last_window_;
+  // The running count of blocking GC runs in the last window.
+  uint64_t blocking_gc_count_last_window_;
+  // The maximum number of buckets in the GC count rate histograms.
+  static constexpr size_t kGcCountRateMaxBucketCount = 200;
+  // The histogram of the number of GC invocations per window duration.
+  Histogram<uint64_t> gc_count_rate_histogram_ GUARDED_BY(gc_complete_lock_);
+  // The histogram of the number of blocking GC invocations per window duration.
+  Histogram<uint64_t> blocking_gc_count_rate_histogram_ GUARDED_BY(gc_complete_lock_);
+
   friend class CollectorTransitionTask;
   friend class collector::GarbageCollector;
   friend class collector::MarkCompact;