Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ba10d06b authored by Daniel Bright's avatar Daniel Bright Committed by Jack Yu
Browse files

Add anomaly detection on network unneeded

* Run anomaly detection when "onNetworkUnneeded" is called 12 times
  within 5 minutes.

Bug: 175845538
Bug: 184104164
Test: m
Change-Id: I286980def39566ca9cd6e01129a743b7ba4c2ce1
parent 0b5abf56
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.internal.telephony;

import android.annotation.IntRange;
import android.os.SystemClock;
import android.util.LongArrayQueue;

import com.android.internal.annotations.VisibleForTesting;
@@ -45,6 +46,16 @@ public class SlidingWindowEventCounter {
        mTimestampQueueMillis = new LongArrayQueue(numOccurrences);
    }

    /**
     * Increments the occurrence counter.
     *
     * Returns true if an event has occurred at least mNumOccurrences times within the
     * time span mWindowSizeMillis.
     */
    public synchronized boolean addOccurrence() {
        return addOccurrence(SystemClock.elapsedRealtime());
    }

    /**
     * Increments the occurrence counter.
     *
+31 −0
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ import android.util.SparseArray;
import com.android.internal.telephony.DctConstants;
import com.android.internal.telephony.Phone;
import com.android.internal.telephony.RILConstants;
import com.android.internal.telephony.SlidingWindowEventCounter;
import com.android.internal.telephony.metrics.TelephonyMetrics;
import com.android.internal.util.IndentingPrintWriter;
import com.android.telephony.Rlog;
@@ -59,6 +60,7 @@ import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * This class represents a network agent which is communication channel between
@@ -94,6 +96,9 @@ public class DcNetworkAgent extends NetworkAgent {
    // For interface duplicate detection. Key is the net id, value is the interface name in string.
    private static Map<Integer, String> sInterfaceNames = new ArrayMap<>();

    private static final long NETWORK_UNWANTED_ANOMALY_WINDOW_MS = TimeUnit.MINUTES.toMillis(5);
    private static final int NETWORK_UNWANTED_ANOMALY_NUM_OCCURRENCES =  12;

    DcNetworkAgent(DataConnection dc, Phone phone, int score, NetworkAgentConfig config,
            NetworkProvider networkProvider, int transportType) {
        super(phone.getContext(), dc.getHandler().getLooper(), "DcNetworkAgent",
@@ -191,8 +196,13 @@ public class DcNetworkAgent extends NetworkAgent {
        return mDataConnection;
    }

    private static final SlidingWindowEventCounter sNetworkUnwantedCounter =
            new SlidingWindowEventCounter(NETWORK_UNWANTED_ANOMALY_WINDOW_MS,
                    NETWORK_UNWANTED_ANOMALY_NUM_OCCURRENCES);

    @Override
    public synchronized void onNetworkUnwanted() {
        trackNetworkUnwanted();
        if (mDataConnection == null) {
            loge("onNetworkUnwanted found called on no-owner DcNetworkAgent!");
            return;
@@ -204,6 +214,27 @@ public class DcNetworkAgent extends NetworkAgent {
                DcTracker.RELEASE_TYPE_DETACH, null);
    }

    /**
     * There have been several bugs where a RECONNECT loop kicks off where a DataConnection
     * connects to the Network, ConnectivityService indicates that the Network is unwanted,
     * and then the DataConnection reconnects.  By the time we get the bug report it's too late
     * because there have already been hundreds of RECONNECTS.  This is meant to capture the issue
     * when it first starts.
     *
     * The unwanted counter is configured to only take an anomaly report in extreme cases.
     * This is to avoid having the anomaly message show up on several devices.
     *
     * This is directly related to b/175845538.  But, there have been several other occurrences of
     * this issue.
     */
    private void trackNetworkUnwanted() {
        if (sNetworkUnwantedCounter.addOccurrence()) {
            AnomalyReporter.reportAnomaly(
                    UUID.fromString("3f578b5c-64e9-11eb-ae93-0242ac130002"),
                    "Network Unwanted called 12 times in 5 minutes.");
        }
    }

    @Override
    public synchronized void onBandwidthUpdateRequested() {
        if (mDataConnection == null) {