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

Commit 8c9e4b68 authored by Hugo Benichi's avatar Hugo Benichi Committed by Lorenzo Colitti
Browse files

DO NOT MERGE: IpConnectivityMetrics reads buffer size in settings

Test: IpConnectivityMetricsTest passes. Also manually changed the new
setting and verified the buffer size is as expected after flushing the
buffer.
Bug: 32198637

(cherry picked from commit 05686dbb)

Change-Id: Iefbeac3a688b260fb3f92dfe0bfd9db28e26749d
parent b55bf383
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -7172,6 +7172,13 @@ public final class Settings {
        */
       public static final String MOBILE_DATA_ALWAYS_ON = "mobile_data_always_on";

        /**
         * Size of the event buffer for IP connectivity metrics.
         * @hide
         */
        public static final String CONNECTIVITY_METRICS_BUFFER_SIZE =
              "connectivity_metrics_buffer_size";

       /** {@hide} */
       public static final String NETSTATS_ENABLED = "netstats_enabled";
       /** {@hide} */
+22 −2
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.net.IIpConnectivityMetrics;
import android.net.metrics.IpConnectivityLog;
import android.os.IBinder;
import android.os.Parcelable;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
@@ -32,6 +33,7 @@ import java.io.FileDescriptor;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.function.ToIntFunction;

import static com.android.server.connectivity.metrics.IpConnectivityLogClass.IpConnectivityEvent;

@@ -51,6 +53,8 @@ final public class IpConnectivityMetrics extends SystemService {

    // Default size of the event buffer. Once the buffer is full, incoming events are dropped.
    private static final int DEFAULT_BUFFER_SIZE = 2000;
    // Maximum size of the event buffer.
    private static final int MAXIMUM_BUFFER_SIZE = DEFAULT_BUFFER_SIZE * 10;

    // Lock ensuring that concurrent manipulations of the event buffer are correct.
    // There are three concurrent operations to synchronize:
@@ -70,11 +74,18 @@ final public class IpConnectivityMetrics extends SystemService {
    @GuardedBy("mLock")
    private int mCapacity;

    public IpConnectivityMetrics(Context ctx) {
    private final ToIntFunction<Context> mCapacityGetter;

    public IpConnectivityMetrics(Context ctx, ToIntFunction<Context> capacityGetter) {
        super(ctx);
        mCapacityGetter = capacityGetter;
        initBuffer();
    }

    public IpConnectivityMetrics(Context ctx) {
        this(ctx, READ_BUFFER_SIZE);
    }

    @Override
    public void onStart() {
        if (DBG) Log.d(TAG, "onStart");
@@ -93,7 +104,7 @@ final public class IpConnectivityMetrics extends SystemService {

    @VisibleForTesting
    public int bufferCapacity() {
        return DEFAULT_BUFFER_SIZE; // TODO: read from config
        return mCapacityGetter.applyAsInt(getContext());
    }

    private void initBuffer() {
@@ -236,4 +247,13 @@ final public class IpConnectivityMetrics extends SystemService {
            getContext().enforceCallingOrSelfPermission(what, "IpConnectivityMetrics");
        }
    };

    private static final ToIntFunction<Context> READ_BUFFER_SIZE = (ctx) -> {
        int size = Settings.Global.getInt(ctx.getContentResolver(),
                Settings.Global.CONNECTIVITY_METRICS_BUFFER_SIZE, DEFAULT_BUFFER_SIZE);
        if (size <= 0) {
            return DEFAULT_BUFFER_SIZE;
        }
        return Math.min(size, MAXIMUM_BUFFER_SIZE);
    };
}
+1 −1
Original line number Diff line number Diff line
@@ -57,7 +57,7 @@ public class IpConnectivityMetricsTest extends TestCase {

    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mService = new IpConnectivityMetrics(mCtx);
        mService = new IpConnectivityMetrics(mCtx, (ctx) -> 2000);
    }

    public void testLoggingEvents() throws Exception {