[metrics] Reset metrics at zygote fork

This is needed to prevent metrics in the zygote from being counted
towards child processes. This is especially important as the zygote
can be a very long running process.

Test: libartbase_gtests
Bug: 170149255
Change-Id: I5c34d44c55381dd976b83e81517145f7e23d061f
diff --git a/libartbase/base/metrics/metrics.h b/libartbase/base/metrics/metrics.h
index e9b40ed..e61e982 100644
--- a/libartbase/base/metrics/metrics.h
+++ b/libartbase/base/metrics/metrics.h
@@ -204,11 +204,18 @@
 
   void Report(MetricsBackend* backend) const { backend->ReportCounter(counter_type, Value()); }
 
+ protected:
+  void Reset() {
+    value_ = 0;
+  }
+
  private:
   value_t Value() const { return value_.load(std::memory_order::memory_order_relaxed); }
 
   std::atomic<value_t> value_;
   static_assert(std::atomic<value_t>::is_always_lock_free);
+
+  friend class ArtMetrics;
 };
 
 template <DatumId histogram_type_,
@@ -239,6 +246,13 @@
     backend->ReportHistogram(histogram_type_, minimum_value_, maximum_value_, GetBuckets());
   }
 
+ protected:
+  void Reset() {
+    for (auto& bucket : buckets_) {
+      bucket = 0;
+    }
+  }
+
  private:
   inline constexpr size_t FindBucketId(int64_t value) const {
     // Values below the minimum are clamped into the first bucket.
@@ -263,6 +277,8 @@
 
   std::array<std::atomic<value_t>, num_buckets_> buckets_;
   static_assert(std::atomic<value_t>::is_always_lock_free);
+
+  friend class ArtMetrics;
 };
 
 // A backend that writes metrics in a human-readable format to a string.
@@ -398,6 +414,10 @@
   void ReportAllMetrics(MetricsBackend* backend) const;
   void DumpForSigQuit(std::ostream& os) const;
 
+  // Resets all metrics to their initial value. This is intended to be used after forking from the
+  // zygote so we don't attribute parent values to the child process.
+  void Reset();
+
 #define METRIC_ACCESSORS(name, Kind, ...)                                        \
   Kind<DatumId::k##name, ##__VA_ARGS__>* name() { return &name##_; } \
   const Kind<DatumId::k##name, ##__VA_ARGS__>* name() const { return &name##_; }