Injecting IpClient RA info stats into statsd
1. Fill in each field of the IpClientRaInfoReported
2. Write the IpClientRaInfoReported into statsd
Bug: 313564079
Test: m, flash and boot
Test: atest NetworkStackTests
Change-Id: Ie94ab28e309f704983ff6a179b5e58e243537c42
diff --git a/src/com/android/networkstack/metrics/IpClientRaInfoMetrics.java b/src/com/android/networkstack/metrics/IpClientRaInfoMetrics.java
new file mode 100644
index 0000000..ba310ed
--- /dev/null
+++ b/src/com/android/networkstack/metrics/IpClientRaInfoMetrics.java
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.networkstack.metrics;
+
+/**
+ * Class to record the network stack IpClientRaInfo metrics into statsd.
+ *
+ * This class is not thread-safe, and should always be accessed from the same thread.
+ *
+ * @hide
+ */
+public class IpClientRaInfoMetrics {
+ private final IpClientRaInfoReported.Builder mStatsBuilder =
+ IpClientRaInfoReported.newBuilder();
+
+ /**
+ * Write the maximum number of distinct RAs into mStatsBuilder.
+ */
+ public void setMaxNumberOfDistinctRas(final int maxNum) {
+ mStatsBuilder.setMaxNumberOfDistinctRas(maxNum);
+ }
+
+ /**
+ * Write the number of zero lifetime RAs into mStatsBuilder.
+ */
+ public void setNumberOfZeroLifetimeRas(final int number) {
+ mStatsBuilder.setNumberOfZeroLifetimeRas(number);
+ }
+
+ /**
+ * Write the number of parsing error RAs into mStatsBuilder.
+ */
+ public void setNumberOfParsingErrorRas(final int number) {
+ mStatsBuilder.setNumberOfParsingErrorRas(number);
+ }
+
+ /**
+ * Write the lowest router lifetime into mStatsBuilder.
+ */
+ public void setLowestRouterLifetimeSeconds(final int lifetime) {
+ mStatsBuilder.setLowestRouterLifetimeSeconds(lifetime);
+ }
+
+ /**
+ * Write the lowest valid lifetime of PIOs into mStatsBuilder.
+ */
+ public void setLowestPioValidLifetimeSeconds(final long lifetime) {
+ mStatsBuilder.setLowestPioValidLifetimeSeconds(lifetime);
+ }
+
+ /**
+ * Write the lowest route lifetime of RIOs into mStatsBuilder.
+ */
+ public void setLowestRioRouteLifetimeSeconds(final long lifetime) {
+ mStatsBuilder.setLowestRioRouteLifetimeSeconds(lifetime);
+ }
+
+ /**
+ * Write the lowest lifetime of RDNSSs into mStatsBuilder.
+ */
+ public void setLowestRdnssLifetimeSeconds(final long lifetime) {
+ mStatsBuilder.setLowestRdnssLifetimeSeconds(lifetime);
+ }
+
+ /**
+ * Write the IpClientRaInfoReported proto into statsd.
+ */
+ public IpClientRaInfoReported statsWrite() {
+ final IpClientRaInfoReported stats = mStatsBuilder.build();
+ NetworkStackStatsLog.write(NetworkStackStatsLog.IP_CLIENT_RA_INFO_REPORTED,
+ stats.getMaxNumberOfDistinctRas(),
+ stats.getNumberOfZeroLifetimeRas(),
+ stats.getNumberOfParsingErrorRas(),
+ stats.getLowestRouterLifetimeSeconds(),
+ stats.getLowestPioValidLifetimeSeconds(),
+ stats.getLowestRioRouteLifetimeSeconds(),
+ stats.getLowestRdnssLifetimeSeconds());
+ return stats;
+ }
+}
diff --git a/tests/unit/src/com/android/networkstack/metrics/IpClientRaInfoMetricsTest.java b/tests/unit/src/com/android/networkstack/metrics/IpClientRaInfoMetricsTest.java
new file mode 100644
index 0000000..2c16f86
--- /dev/null
+++ b/tests/unit/src/com/android/networkstack/metrics/IpClientRaInfoMetricsTest.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.networkstack.metrics;
+
+import static org.junit.Assert.assertEquals;
+
+import androidx.test.filters.SmallTest;
+import androidx.test.runner.AndroidJUnit4;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * Tests for IpClientRaInfoMetrics.
+ */
+@RunWith(AndroidJUnit4.class)
+@SmallTest
+public class IpClientRaInfoMetricsTest {
+ @Test
+ public void testIpClientRaInfoMetrics_VerifyCollectMetrics() throws Exception {
+ IpClientRaInfoReported mStats;
+ final IpClientRaInfoMetrics mMetrics = new IpClientRaInfoMetrics();
+ mMetrics.setMaxNumberOfDistinctRas(12);
+ mMetrics.setNumberOfZeroLifetimeRas(34);
+ mMetrics.setNumberOfParsingErrorRas(56);
+ mMetrics.setLowestRouterLifetimeSeconds(78);
+ mMetrics.setLowestPioValidLifetimeSeconds(123);
+ mMetrics.setLowestRioRouteLifetimeSeconds(456);
+ mMetrics.setLowestRdnssLifetimeSeconds(789);
+ mStats = mMetrics.statsWrite();
+ assertEquals(12, mStats.getMaxNumberOfDistinctRas());
+ assertEquals(34, mStats.getNumberOfZeroLifetimeRas());
+ assertEquals(56, mStats.getNumberOfParsingErrorRas());
+ assertEquals(78, mStats.getLowestRouterLifetimeSeconds());
+ assertEquals(123, mStats.getLowestPioValidLifetimeSeconds());
+ assertEquals(456, mStats.getLowestRioRouteLifetimeSeconds());
+ assertEquals(789, mStats.getLowestRdnssLifetimeSeconds());
+ }
+}