[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_test.cc b/libartbase/base/metrics/metrics_test.cc
index 93701e6..7b26e43 100644
--- a/libartbase/base/metrics/metrics_test.cc
+++ b/libartbase/base/metrics/metrics_test.cc
@@ -220,6 +220,51 @@
#undef METRIC
}
+TEST_F(MetricsTest, ResetMetrics) {
+ ArtMetrics metrics;
+
+ // Add something to each of the metrics.
+#define METRIC(name, type, ...) metrics.name()->Add(42);
+ ART_METRICS(METRIC)
+#undef METRIC
+
+ class NonZeroBackend : public TestBackendBase {
+ public:
+ void ReportCounter(DatumId, uint64_t value) override {
+ EXPECT_NE(value, 0u);
+ }
+
+ void ReportHistogram(DatumId, int64_t, int64_t, const std::vector<uint32_t>& buckets) override {
+ bool nonzero = false;
+ for (const auto value : buckets) {
+ nonzero |= (value != 0u);
+ }
+ EXPECT_TRUE(nonzero);
+ }
+ } non_zero_backend;
+
+ // Make sure the metrics all have a nonzero value.
+ metrics.ReportAllMetrics(&non_zero_backend);
+
+ // Reset the metrics and make sure they are all zero again
+ metrics.Reset();
+
+ class ZeroBackend : public TestBackendBase {
+ public:
+ void ReportCounter(DatumId, uint64_t value) override {
+ EXPECT_EQ(value, 0u);
+ }
+
+ void ReportHistogram(DatumId, int64_t, int64_t, const std::vector<uint32_t>& buckets) override {
+ for (const auto value : buckets) {
+ EXPECT_EQ(value, 0u);
+ }
+ }
+ } zero_backend;
+
+ metrics.ReportAllMetrics(&zero_backend);
+}
+
} // namespace metrics
} // namespace art