OSDN Git Service

Fix missing IpConnectivity metrics
authorHugo Benichi <hugobenichi@google.com>
Fri, 29 Jul 2016 07:29:46 +0000 (16:29 +0900)
committerHugo Benichi <hugobenichi@google.com>
Fri, 29 Jul 2016 08:07:38 +0000 (17:07 +0900)
The IpConnectivityLog class looks up MetricsLoggerService once only
at creation. If a IpConnectivityLog user instantiates this class too
early during the boot process, the MetricsLoggerService is not found
and no event can be recorded.

This patch makes IpConnectivityLog attempt to look up
MetricsLoggerService as long as it hasn't found it yet.

This allows IpManager and ConnectivityService to upload
android.net.metrics events.

Bug: 30490301
Change-Id: I97102b95a775ea9e90351b9887ae4661fddc2af9

core/java/android/net/ConnectivityMetricsLogger.java
core/java/android/net/metrics/IpConnectivityLog.java

index 029c5bd..946bed2 100644 (file)
@@ -46,11 +46,12 @@ public class ConnectivityMetricsLogger {
 
     public static final String DATA_KEY_EVENTS_COUNT = "count";
 
-    /** {@hide} */ protected final IConnectivityMetricsLogger mService;
+    /** {@hide} */ protected IConnectivityMetricsLogger mService;
     /** {@hide} */ protected volatile long mServiceUnblockedTimestampMillis;
     private int mNumSkippedEvents;
 
     public ConnectivityMetricsLogger() {
+        // TODO: consider not initializing mService in constructor
         this(IConnectivityMetricsLogger.Stub.asInterface(
                 ServiceManager.getService(CONNECTIVITY_METRICS_LOGGER_SERVICE)));
     }
@@ -61,6 +62,18 @@ public class ConnectivityMetricsLogger {
         mService = service;
     }
 
+    /** {@hide} */
+    protected boolean checkLoggerService() {
+        if (mService != null) {
+            return true;
+        }
+        // Two threads racing here will write the same pointer because getService
+        // is idempotent once MetricsLoggerService is initialized.
+        mService = IConnectivityMetricsLogger.Stub.asInterface(
+                ServiceManager.getService(CONNECTIVITY_METRICS_LOGGER_SERVICE));
+        return mService != null;
+    }
+
     /**
      * Log a ConnectivityMetricsEvent.
      *
index a7c1d40..dd7bd1b 100644 (file)
@@ -31,7 +31,7 @@ import com.android.internal.annotations.VisibleForTesting;
  */
 public class IpConnectivityLog extends ConnectivityMetricsLogger {
     private static String TAG = "IpConnectivityMetricsLogger";
-    private static final boolean DBG = false;
+    private static final boolean DBG = true;
 
     public IpConnectivityLog() {
         // mService initialized in super constructor.
@@ -52,9 +52,9 @@ public class IpConnectivityLog extends ConnectivityMetricsLogger {
      * @return true if the event was successfully logged.
      */
     public boolean log(long timestamp, Parcelable data) {
-        if (mService == null) {
+        if (!checkLoggerService()) {
             if (DBG) {
-                Log.d(TAG, CONNECTIVITY_METRICS_LOGGER_SERVICE + " service not ready");
+                Log.d(TAG, CONNECTIVITY_METRICS_LOGGER_SERVICE + " service was not ready");
             }
             return false;
         }