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 pick from commit 05686dbb6b)

Change-Id: Ie7ca1638533479601c5983bb6e54705003561b6c
This commit is contained in:
Hugo Benichi
2016-10-19 11:17:28 +09:00
parent f6fdb4a552
commit ff0b58627f
3 changed files with 30 additions and 3 deletions

View File

@@ -7162,6 +7162,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} */

View File

@@ -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;
@@ -44,6 +46,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:
@@ -63,11 +67,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");
@@ -86,7 +97,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() {
@@ -226,4 +237,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);
};
}

View File

@@ -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 {