From 9df2ae138b9aa0f952fe742efa6e673e6bb02de1 Mon Sep 17 00:00:00 2001 From: Hugo Benichi Date: Thu, 13 Oct 2016 13:16:16 +0900 Subject: [PATCH 1/6] DO NOT MERGE: IpConnectivity metrics: add version number This patch adds a version field to ipconnectivity.proto and populates it to 2, which is the logical version number for NYC-MR2. Test: IpConnectivity{EventBuilder,Metrics}Test pass Bug: 32127906 (cherry picked from commit d680d4c856f54bea0c60dea74ba4204822391e49) Change-Id: If8f167c0dc4c1abe0e235e2adfd131168a4ddc52 --- .../IpConnectivityEventBuilder.java | 4 +++ .../connectivity/IpConnectivityMetrics.java | 7 ++++ services/core/proto/ipconnectivity.proto | 13 +++++++- .../IpConnectivityEventBuilderTest.java | 33 ++++++++++++------- .../IpConnectivityMetricsTest.java | 3 +- 5 files changed, 47 insertions(+), 13 deletions(-) diff --git a/services/core/java/com/android/server/connectivity/IpConnectivityEventBuilder.java b/services/core/java/com/android/server/connectivity/IpConnectivityEventBuilder.java index f1ef947f34726..f1d01e06911a2 100644 --- a/services/core/java/com/android/server/connectivity/IpConnectivityEventBuilder.java +++ b/services/core/java/com/android/server/connectivity/IpConnectivityEventBuilder.java @@ -48,6 +48,10 @@ final public class IpConnectivityEventBuilder { final IpConnectivityLog log = new IpConnectivityLog(); log.events = toProto(events); log.droppedEvents = dropped; + if ((log.events.length > 0) || (dropped > 0)) { + // Only write version number if log has some information at all. + log.version = IpConnectivityMetrics.VERSION; + } return IpConnectivityLog.toByteArray(log); } diff --git a/services/core/java/com/android/server/connectivity/IpConnectivityMetrics.java b/services/core/java/com/android/server/connectivity/IpConnectivityMetrics.java index 28e724c2a7a1d..85c4303e926d1 100644 --- a/services/core/java/com/android/server/connectivity/IpConnectivityMetrics.java +++ b/services/core/java/com/android/server/connectivity/IpConnectivityMetrics.java @@ -40,6 +40,13 @@ final public class IpConnectivityMetrics extends SystemService { private static final String TAG = IpConnectivityMetrics.class.getSimpleName(); private static final boolean DBG = false; + // The logical version numbers of ipconnectivity.proto, corresponding to the + // "version" field of IpConnectivityLog. + private static final int NYC = 0; + private static final int NYC_MR1 = 1; + private static final int NYC_MR2 = 2; + public static final int VERSION = NYC_MR2; + private static final String SERVICE_NAME = IpConnectivityLog.SERVICE_NAME; // Default size of the event buffer. Once the buffer is full, incoming events are dropped. diff --git a/services/core/proto/ipconnectivity.proto b/services/core/proto/ipconnectivity.proto index e0d7f0984eb0d..29b318f5ca892 100644 --- a/services/core/proto/ipconnectivity.proto +++ b/services/core/proto/ipconnectivity.proto @@ -53,6 +53,7 @@ message IpReachabilityEvent { // The event type code of the probe, represented by constants defined in // android.net.metrics.IpReachabilityEvent. + // NUD_FAILED_ORGANIC and PROVISIONING_LOST_ORGANIC recorded since version 1. optional int32 event_type = 2; }; @@ -126,11 +127,12 @@ message DHCPEvent { // Lifetime duration in milliseconds of a DhcpClient state, or transition // time in milliseconds between specific pairs of DhcpClient's states. - // Only populated when state_transition is populated. + // Only populated since version 1, when state_transition is populated. optional int32 duration_ms = 4; } // Represents the generation of an Android Packet Filter program. +// Since version 1. message ApfProgramEvent { // Lifetime of the program in seconds. optional int64 lifetime = 1; @@ -154,6 +156,7 @@ message ApfProgramEvent { // Represents Router Advertisement listening statistics for an interface with // Android Packet Filter enabled. +// Since version 1. message ApfStatistics { // The time interval in milliseconds these stastistics cover. optional int64 duration_ms = 1; @@ -183,6 +186,7 @@ message ApfStatistics { // Represents the reception of a Router Advertisement packet for an interface // with Android Packet Filter enabled. +// Since version 1. message RaEvent { // All lifetime values are expressed in seconds. The default value for an // option lifetime that was not present in the RA option list is -1. @@ -271,4 +275,11 @@ message IpConnectivityLog { // The number of events that had to be dropped due to a full buffer. optional int32 dropped_events = 2; + + // The version number of the metrics events being collected. + // nyc-dev: not populated, implicitly 0 + // nyc-dr1: not populated, implicitly 1 (sailfish and marlin only) + // nyc-mr1: not populated, implicitly 1 + // nyc-mr2: 2 + optional int32 version = 3; }; diff --git a/tests/net/java/com/android/server/connectivity/IpConnectivityEventBuilderTest.java b/tests/net/java/com/android/server/connectivity/IpConnectivityEventBuilderTest.java index aed3635245607..84f0f9040b0c2 100644 --- a/tests/net/java/com/android/server/connectivity/IpConnectivityEventBuilderTest.java +++ b/tests/net/java/com/android/server/connectivity/IpConnectivityEventBuilderTest.java @@ -71,7 +71,8 @@ public class IpConnectivityEventBuilderTest extends TestCase { " transport_types: 3", " >", " time_ms: 1", - ">"); + ">", + "version: 2"); verifySerialization(want, ev); } @@ -93,7 +94,8 @@ public class IpConnectivityEventBuilderTest extends TestCase { " state_transition: \"SomeState\"", " >", " time_ms: 1", - ">"); + ">", + "version: 2"); verifySerialization(want, ev); } @@ -114,7 +116,8 @@ public class IpConnectivityEventBuilderTest extends TestCase { " state_transition: \"\"", " >", " time_ms: 1", - ">"); + ">", + "version: 2"); verifySerialization(want, ev); } @@ -160,7 +163,8 @@ public class IpConnectivityEventBuilderTest extends TestCase { " return_codes: 178", " >", " time_ms: 1", - ">"); + ">", + "version: 2"); verifySerialization(want, ev); } @@ -181,7 +185,8 @@ public class IpConnectivityEventBuilderTest extends TestCase { " latency_ms: 5678", " >", " time_ms: 1", - ">"); + ">", + "version: 2"); verifySerialization(want, ev); } @@ -200,7 +205,8 @@ public class IpConnectivityEventBuilderTest extends TestCase { " if_name: \"wlan0\"", " >", " time_ms: 1", - ">"); + ">", + "version: 2"); verifySerialization(want, ev); } @@ -223,7 +229,8 @@ public class IpConnectivityEventBuilderTest extends TestCase { " >", " >", " time_ms: 1", - ">"); + ">", + "version: 2"); verifySerialization(want, ev); } @@ -248,7 +255,8 @@ public class IpConnectivityEventBuilderTest extends TestCase { " probe_result: 204", " probe_type: 1", " >", - ">"); + ">", + "version: 2"); verifySerialization(want, ev); } @@ -274,7 +282,8 @@ public class IpConnectivityEventBuilderTest extends TestCase { " program_length: 2048", " >", " time_ms: 1", - ">"); + ">", + "version: 2"); verifySerialization(want, ev); } @@ -305,7 +314,8 @@ public class IpConnectivityEventBuilderTest extends TestCase { " zero_lifetime_ras: 1", " >", " time_ms: 1", - ">"); + ">", + "version: 2"); verifySerialization(want, ev); } @@ -332,7 +342,8 @@ public class IpConnectivityEventBuilderTest extends TestCase { " router_lifetime: 2000", " >", " time_ms: 1", - ">"); + ">", + "version: 2"); verifySerialization(want, ev); } diff --git a/tests/net/java/com/android/server/connectivity/IpConnectivityMetricsTest.java b/tests/net/java/com/android/server/connectivity/IpConnectivityMetricsTest.java index 3fc89b9ff12d3..8bdc829f7bfa3 100644 --- a/tests/net/java/com/android/server/connectivity/IpConnectivityMetricsTest.java +++ b/tests/net/java/com/android/server/connectivity/IpConnectivityMetricsTest.java @@ -204,7 +204,8 @@ public class IpConnectivityMetricsTest extends TestCase { " router_lifetime: 2000", " >", " time_ms: 700", - ">"); + ">", + "version: 2"); verifySerialization(want, getdump("flush")); } From 5635c270a1bda538181daa260902a59030f084e6 Mon Sep 17 00:00:00 2001 From: Hugo Benichi Date: Wed, 19 Oct 2016 13:48:40 +0900 Subject: [PATCH 2/6] DO NOT MERGE: Add IP conn metrics to dumpsys and bug reports Test: manually created a bug report and inspected content. (cherry picked from commit 51d14cb60793a9a073b8a29ff7875bb518cd7d54) Change-Id: I9649cc2f9bb7a1975e60baaae55fcdb2a48c3b93 --- .../com/android/server/connectivity/IpConnectivityMetrics.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/services/core/java/com/android/server/connectivity/IpConnectivityMetrics.java b/services/core/java/com/android/server/connectivity/IpConnectivityMetrics.java index 85c4303e926d1..e176fe1575c10 100644 --- a/services/core/java/com/android/server/connectivity/IpConnectivityMetrics.java +++ b/services/core/java/com/android/server/connectivity/IpConnectivityMetrics.java @@ -193,6 +193,7 @@ final public class IpConnectivityMetrics extends SystemService { static final String CMD_FLUSH = "flush"; static final String CMD_LIST = "list"; static final String CMD_STATS = "stats"; + static final String CMD_DUMPSYS = "-a"; // dumpsys.cpp dumps services with "-a" as arguments static final String CMD_DEFAULT = CMD_STATS; @Override @@ -210,6 +211,8 @@ final public class IpConnectivityMetrics extends SystemService { case CMD_FLUSH: cmdFlush(fd, pw, args); return; + case CMD_DUMPSYS: + // Fallthrough to CMD_LIST when dumpsys.cpp dumps services states (bug reports) case CMD_LIST: cmdList(fd, pw, args); return; From b55bf38351fc06d267735e8e377d4049c2a7b5d3 Mon Sep 17 00:00:00 2001 From: Lorenzo Colitti Date: Fri, 21 Oct 2016 18:41:25 +0900 Subject: [PATCH 3/6] DO NOT MERGE: CaptivePortalLogin: set mixed content policy to compatibility. This makes the policy for mixed HTTP/HTTPS content match Chrome. This allows us to display login pages where unimportant subresources are HTTP. Bug: 32171569 (cherry picked from commit 625166377b2d7c5f09d1e7617b3eac3a464fba53) Change-Id: Ia82958192d978280715442e182c34f9da5c88c66 --- .../android/captiveportallogin/CaptivePortalLoginActivity.java | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/CaptivePortalLogin/src/com/android/captiveportallogin/CaptivePortalLoginActivity.java b/packages/CaptivePortalLogin/src/com/android/captiveportallogin/CaptivePortalLoginActivity.java index b58c87a5094d3..bb8eb2cd07977 100644 --- a/packages/CaptivePortalLogin/src/com/android/captiveportallogin/CaptivePortalLoginActivity.java +++ b/packages/CaptivePortalLogin/src/com/android/captiveportallogin/CaptivePortalLoginActivity.java @@ -115,6 +115,7 @@ public class CaptivePortalLoginActivity extends Activity { myWebView.clearCache(true); WebSettings webSettings = myWebView.getSettings(); webSettings.setJavaScriptEnabled(true); + webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE); mWebViewClient = new MyWebViewClient(); myWebView.setWebViewClient(mWebViewClient); myWebView.setWebChromeClient(new MyWebChromeClient()); From 8c9e4b68f09c92f7f1d26916bbac7014c45f5d49 Mon Sep 17 00:00:00 2001 From: Hugo Benichi Date: Wed, 19 Oct 2016 11:17:28 +0900 Subject: [PATCH 4/6] 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 05686dbb6b965eb1a54591d2ed2334337886c6ee) Change-Id: Iefbeac3a688b260fb3f92dfe0bfd9db28e26749d --- core/java/android/provider/Settings.java | 7 ++++++ .../connectivity/IpConnectivityMetrics.java | 24 +++++++++++++++++-- .../IpConnectivityMetricsTest.java | 2 +- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java index db7baac7fdd77..e52983e995dfc 100755 --- a/core/java/android/provider/Settings.java +++ b/core/java/android/provider/Settings.java @@ -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} */ diff --git a/services/core/java/com/android/server/connectivity/IpConnectivityMetrics.java b/services/core/java/com/android/server/connectivity/IpConnectivityMetrics.java index e176fe1575c10..642f2e01987bf 100644 --- a/services/core/java/com/android/server/connectivity/IpConnectivityMetrics.java +++ b/services/core/java/com/android/server/connectivity/IpConnectivityMetrics.java @@ -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 mCapacityGetter; + + public IpConnectivityMetrics(Context ctx, ToIntFunction 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 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); + }; } diff --git a/tests/net/java/com/android/server/connectivity/IpConnectivityMetricsTest.java b/tests/net/java/com/android/server/connectivity/IpConnectivityMetricsTest.java index 8bdc829f7bfa3..14b5cbeb9276d 100644 --- a/tests/net/java/com/android/server/connectivity/IpConnectivityMetricsTest.java +++ b/tests/net/java/com/android/server/connectivity/IpConnectivityMetricsTest.java @@ -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 { From 05ad47add8796880c5ba18d55ab6d5221f3e708d Mon Sep 17 00:00:00 2001 From: Hugo Benichi Date: Tue, 18 Oct 2016 08:43:30 +0900 Subject: [PATCH 5/6] DO NOT MERGE: TokenBucket for rate-limiting and throttling This patch adds a TokenBucket class that can accumulate and distribute tokens over time. Test: comes with unit tests. Bug: 32198637 (cherry picked from commit 998493f0ee39ae0e9ffdea27f48f1b11b0807fcb) Change-Id: I4012ae6d02f7004bceee9a2ba03914f2a2506031 --- .../android/internal/util/TokenBucket.java | 126 ++++++++++++ .../src/android/util/TokenBucketTest.java | 180 ++++++++++++++++++ 2 files changed, 306 insertions(+) create mode 100644 core/java/com/android/internal/util/TokenBucket.java create mode 100644 core/tests/coretests/src/android/util/TokenBucketTest.java diff --git a/core/java/com/android/internal/util/TokenBucket.java b/core/java/com/android/internal/util/TokenBucket.java new file mode 100644 index 0000000000000..effb82ba7a2d0 --- /dev/null +++ b/core/java/com/android/internal/util/TokenBucket.java @@ -0,0 +1,126 @@ +/* + * Copyright (C) 2016 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.internal.util; + +import android.os.SystemClock; + +import static com.android.internal.util.Preconditions.checkArgumentNonnegative; +import static com.android.internal.util.Preconditions.checkArgumentPositive; + +/** + * A class useful for rate-limiting or throttling that stores and distributes tokens. + * + * A TokenBucket starts with a fixed capacity of tokens, an initial amount of tokens, and + * a fixed filling period (in milliseconds). + * + * For every filling period, the bucket gains one token, up to its maximum capacity from + * which point tokens simply overflow and are lost. Tokens can be obtained one by one or n by n. + * + * The available amount of tokens is computed lazily when the bucket state is inspected. + * Therefore it is purely synchronous and does not involve any asynchronous activity. + * It is not synchronized in any way and not a thread-safe object. + */ +public class TokenBucket { + + private final int mFillDelta; // Time in ms it takes to generate one token. + private final int mCapacity; // Maximum number of tokens that can be stored. + private long mLastFill; // Last time in ms the bucket generated tokens. + private int mAvailable; // Current number of available tokens. + + /** + * Create a new TokenBucket. + * @param deltaMs the time in milliseconds it takes to generate a new token. + * Must be strictly positive. + * @param capacity the maximum token capacity. Must be strictly positive. + * @param tokens the starting amount of token. Must be positive or zero. + */ + public TokenBucket(int deltaMs, int capacity, int tokens) { + mFillDelta = checkArgumentPositive(deltaMs, "deltaMs must be strictly positive"); + mCapacity = checkArgumentPositive(capacity, "capacity must be strictly positive"); + mAvailable = Math.min(checkArgumentNonnegative(tokens), mCapacity); + mLastFill = scaledTime(); + } + + /** + * Create a new TokenBucket that starts completely filled. + * @param deltaMs the time in milliseconds it takes to generate a new token. + * Must be strictly positive. + * @param capacity the maximum token capacity. Must be strictly positive. + */ + public TokenBucket(int deltaMs, int capacity) { + this(deltaMs, capacity, capacity); + } + + /** Reset this TokenBucket and set its number of available tokens. */ + public void reset(int tokens) { + checkArgumentNonnegative(tokens); + mAvailable = Math.min(tokens, mCapacity); + mLastFill = scaledTime(); + } + + /** Returns this TokenBucket maximum token capacity. */ + public int capacity() { + return mCapacity; + } + + /** Returns this TokenBucket currently number of available tokens. */ + public int available() { + fill(); + return mAvailable; + } + + /** Returns true if this TokenBucket as one or more tokens available. */ + public boolean has() { + fill(); + return mAvailable > 0; + } + + /** Consumes a token from this TokenBucket and returns true if a token is available. */ + public boolean get() { + return (get(1) == 1); + } + + /** + * Try to consume many tokens from this TokenBucket. + * @param n the number of tokens to consume. + * @return the number of tokens that were actually consumed. + */ + public int get(int n) { + fill(); + if (n <= 0) { + return 0; + } + if (n > mAvailable) { + int got = mAvailable; + mAvailable = 0; + return got; + } + mAvailable -= n; + return n; + } + + private void fill() { + final long now = scaledTime(); + final int diff = (int) (now - mLastFill); + mAvailable = Math.min(mCapacity, mAvailable + diff); + mLastFill = now; + } + + private long scaledTime() { + return SystemClock.elapsedRealtime() / mFillDelta; + } +} diff --git a/core/tests/coretests/src/android/util/TokenBucketTest.java b/core/tests/coretests/src/android/util/TokenBucketTest.java new file mode 100644 index 0000000000000..a053ad33f589f --- /dev/null +++ b/core/tests/coretests/src/android/util/TokenBucketTest.java @@ -0,0 +1,180 @@ +/* + * Copyright (C) 2016 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.internal.util; + +import android.os.SystemClock; +import android.text.format.DateUtils; +import junit.framework.TestCase; + +public class TokenBucketTest extends TestCase { + + static final int FILL_DELTA_VERY_SHORT = 1; + static final int FILL_DELTA_VERY_LONG = Integer.MAX_VALUE; + + public void testArgumentValidation() { + assertThrow(() -> new TokenBucket(0, 1, 1)); + assertThrow(() -> new TokenBucket(1, 0, 1)); + assertThrow(() -> new TokenBucket(1, 1, 0)); + assertThrow(() -> new TokenBucket(0, 1)); + assertThrow(() -> new TokenBucket(1, 0)); + assertThrow(() -> new TokenBucket(-1, 1, 1)); + assertThrow(() -> new TokenBucket(1, -1, 1)); + assertThrow(() -> new TokenBucket(1, 1, -1)); + assertThrow(() -> new TokenBucket(-1, 1)); + assertThrow(() -> new TokenBucket(1, -1)); + + new TokenBucket(1000, 100, 0); + new TokenBucket(1000, 100, 10); + new TokenBucket(5000, 50); + new TokenBucket(5000, 1); + } + + public void testInitialCapacity() { + drain(new TokenBucket(FILL_DELTA_VERY_LONG, 1), 1); + drain(new TokenBucket(FILL_DELTA_VERY_LONG, 10), 10); + drain(new TokenBucket(FILL_DELTA_VERY_LONG, 1000), 1000); + + drain(new TokenBucket(FILL_DELTA_VERY_LONG, 10, 0), 0); + drain(new TokenBucket(FILL_DELTA_VERY_LONG, 10, 3), 3); + drain(new TokenBucket(FILL_DELTA_VERY_LONG, 10, 10), 10); + + drain(new TokenBucket(FILL_DELTA_VERY_LONG, 10, 100), 10); + + drain(new TokenBucket((int)DateUtils.MINUTE_IN_MILLIS, 50), 50); + drain(new TokenBucket((int)DateUtils.HOUR_IN_MILLIS, 10), 10); + drain(new TokenBucket((int)DateUtils.DAY_IN_MILLIS, 200), 200); + } + + public void testReset() { + TokenBucket tb = new TokenBucket(FILL_DELTA_VERY_LONG, 100, 10); + drain(tb, 10); + + tb.reset(50); + drain(tb, 50); + + tb.reset(50); + getOneByOne(tb, 10); + assertTrue(tb.has()); + + tb.reset(30); + drain(tb, 30); + } + + public void testFill() throws Exception { + int delta = 50; + TokenBucket tb = new TokenBucket(delta, 10, 0); + + assertEmpty(tb); + + Thread.sleep(3 * delta / 2); + + assertTrue(tb.has()); + } + + public void testRefill() throws Exception { + TokenBucket tb = new TokenBucket(FILL_DELTA_VERY_SHORT, 10, 10); + + assertEquals(5, tb.get(5)); + assertEquals(5, tb.get(5)); + + while (tb.available() < 10) { + Thread.sleep(2); + } + + assertEquals(10, tb.get(10)); + + while (tb.available() < 10) { + Thread.sleep(2); + } + + assertEquals(10, tb.get(100)); + } + + public void testAverage() throws Exception { + final int delta = 3; + final int want = 60; + + long start = SystemClock.elapsedRealtime(); + TokenBucket tb = new TokenBucket(delta, 20, 0); + + for (int i = 0; i < want; i++) { + while (!tb.has()) { + Thread.sleep(5 * delta); + } + tb.get(); + } + + assertDuration(want * delta, SystemClock.elapsedRealtime() - start); + } + + public void testBurst() throws Exception { + final int delta = 2; + final int capacity = 20; + final int want = 100; + + long start = SystemClock.elapsedRealtime(); + TokenBucket tb = new TokenBucket(delta, capacity, 0); + + int total = 0; + while (total < want) { + while (!tb.has()) { + Thread.sleep(capacity * delta - 2); + } + total += tb.get(tb.available()); + } + + assertDuration(total * delta, SystemClock.elapsedRealtime() - start); + } + + static void getOneByOne(TokenBucket tb, int n) { + while (n > 0) { + assertTrue(tb.has()); + assertTrue(tb.available() >= n); + assertTrue(tb.get()); + assertTrue(tb.available() >= n - 1); + n--; + } + } + + void assertEmpty(TokenBucket tb) { + assertFalse(tb.has()); + assertEquals(0, tb.available()); + assertFalse(tb.get()); + } + + void drain(TokenBucket tb, int n) { + getOneByOne(tb, n); + assertEmpty(tb); + } + + void assertDuration(long expected, long elapsed) { + String msg = String.format( + "expected elapsed time at least %d ms, but was %d ms", expected, elapsed); + elapsed += 1; // one millisecond extra guard + assertTrue(msg, elapsed >= expected); + } + + void assertThrow(Fn fn) { + try { + fn.call(); + fail("expected n exception to be thrown."); + } catch (Throwable t) {} + } + + interface Fn { void call(); } +} + From f99a33a1b4599f6297faa316f408b58deebabefb Mon Sep 17 00:00:00 2001 From: Hugo Benichi Date: Tue, 18 Oct 2016 10:36:33 +0900 Subject: [PATCH 6/6] DO NOT MERGE: IpConnectivityMetrics: rate limit ApfProgramEvents This patch uses the previously introduced TokenBucket to rate limit ApfProgramEvents, still allowing for burst of ApfProgramEvents when a new interface is set up (due to ipv4 provisioning, multicast lock, ipv6 RAs triggering new APF program events in short amounts of time). Test: new test in IpConnectivityMetricsTest Bug: 1550402 (cherry picked from commit e1c173d2240a8eedf7685c9371087dc047a6931f) Change-Id: Idb640dec13ba64180985544b9709a586af66eb6e --- .../android/net/IIpConnectivityMetrics.aidl | 3 ++- .../src/android/util/TokenBucketTest.java | 1 - .../connectivity/IpConnectivityMetrics.java | 24 +++++++++++++++++++ .../IpConnectivityMetricsTest.java | 22 +++++++++++++++++ 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/core/java/android/net/IIpConnectivityMetrics.aidl b/core/java/android/net/IIpConnectivityMetrics.aidl index 8f634bbf0cc9a..d36b7661aaa3d 100644 --- a/core/java/android/net/IIpConnectivityMetrics.aidl +++ b/core/java/android/net/IIpConnectivityMetrics.aidl @@ -23,7 +23,8 @@ import android.net.ConnectivityMetricsEvent; interface IIpConnectivityMetrics { /** - * @return number of remaining available slots in buffer. + * @return the number of remaining available slots in buffer, + * or -1 if the event was dropped due to rate limiting. */ int logEvent(in ConnectivityMetricsEvent event); } diff --git a/core/tests/coretests/src/android/util/TokenBucketTest.java b/core/tests/coretests/src/android/util/TokenBucketTest.java index a053ad33f589f..f7ac20c7287fa 100644 --- a/core/tests/coretests/src/android/util/TokenBucketTest.java +++ b/core/tests/coretests/src/android/util/TokenBucketTest.java @@ -177,4 +177,3 @@ public class TokenBucketTest extends TestCase { interface Fn { void call(); } } - diff --git a/services/core/java/com/android/server/connectivity/IpConnectivityMetrics.java b/services/core/java/com/android/server/connectivity/IpConnectivityMetrics.java index 642f2e01987bf..be681739d2ee1 100644 --- a/services/core/java/com/android/server/connectivity/IpConnectivityMetrics.java +++ b/services/core/java/com/android/server/connectivity/IpConnectivityMetrics.java @@ -19,15 +19,19 @@ package com.android.server.connectivity; import android.content.Context; import android.net.ConnectivityMetricsEvent; import android.net.IIpConnectivityMetrics; +import android.net.metrics.ApfProgramEvent; import android.net.metrics.IpConnectivityLog; import android.os.IBinder; import android.os.Parcelable; import android.provider.Settings; import android.text.TextUtils; +import android.text.format.DateUtils; +import android.util.ArrayMap; import android.util.Base64; import android.util.Log; import com.android.internal.annotations.GuardedBy; import com.android.internal.annotations.VisibleForTesting; +import com.android.internal.util.TokenBucket; import com.android.server.SystemService; import java.io.FileDescriptor; import java.io.IOException; @@ -56,6 +60,8 @@ final public class IpConnectivityMetrics extends SystemService { // Maximum size of the event buffer. private static final int MAXIMUM_BUFFER_SIZE = DEFAULT_BUFFER_SIZE * 10; + private static final int ERROR_RATE_LIMITED = -1; + // Lock ensuring that concurrent manipulations of the event buffer are correct. // There are three concurrent operations to synchronize: // - appending events to the buffer. @@ -73,6 +79,8 @@ final public class IpConnectivityMetrics extends SystemService { private int mDropped; @GuardedBy("mLock") private int mCapacity; + @GuardedBy("mLock") + private final ArrayMap, TokenBucket> mBuckets = makeRateLimitingBuckets(); private final ToIntFunction mCapacityGetter; @@ -122,6 +130,10 @@ final public class IpConnectivityMetrics extends SystemService { if (event == null) { return left; } + if (isRateLimited(event)) { + // Do not count as a dropped event. TODO: consider adding separate counter + return ERROR_RATE_LIMITED; + } if (left == 0) { mDropped++; return 0; @@ -131,6 +143,11 @@ final public class IpConnectivityMetrics extends SystemService { } } + private boolean isRateLimited(ConnectivityMetricsEvent event) { + TokenBucket tb = mBuckets.get(event.data.getClass()); + return (tb != null) && !tb.get(); + } + private String flushEncodedOutput() { final ArrayList events; final int dropped; @@ -256,4 +273,11 @@ final public class IpConnectivityMetrics extends SystemService { } return Math.min(size, MAXIMUM_BUFFER_SIZE); }; + + private static ArrayMap, TokenBucket> makeRateLimitingBuckets() { + ArrayMap, TokenBucket> map = new ArrayMap<>(); + // one token every minute, 50 tokens max: burst of ~50 events every hour. + map.put(ApfProgramEvent.class, new TokenBucket((int)DateUtils.MINUTE_IN_MILLIS, 50)); + return map; + } } diff --git a/tests/net/java/com/android/server/connectivity/IpConnectivityMetricsTest.java b/tests/net/java/com/android/server/connectivity/IpConnectivityMetricsTest.java index 14b5cbeb9276d..aa491bbabd795 100644 --- a/tests/net/java/com/android/server/connectivity/IpConnectivityMetricsTest.java +++ b/tests/net/java/com/android/server/connectivity/IpConnectivityMetricsTest.java @@ -19,6 +19,7 @@ package com.android.server.connectivity; import android.content.Context; import android.net.ConnectivityMetricsEvent; import android.net.IIpConnectivityMetrics; +import android.net.metrics.ApfProgramEvent; import android.net.metrics.ApfStats; import android.net.metrics.DefaultNetworkEvent; import android.net.metrics.DhcpClientEvent; @@ -112,6 +113,27 @@ public class IpConnectivityMetricsTest extends TestCase { assertEquals("", output3); } + public void testRateLimiting() { + final IpConnectivityLog logger = new IpConnectivityLog(mService.impl); + final ApfProgramEvent ev = new ApfProgramEvent(0, 0, 0, 0, 0); + final long fakeTimestamp = 1; + + int attempt = 100; // More than burst quota, but less than buffer size. + for (int i = 0; i < attempt; i++) { + logger.log(ev); + } + + String output1 = getdump("flush"); + assertFalse("".equals(output1)); + + for (int i = 0; i < attempt; i++) { + assertFalse("expected event to be dropped", logger.log(fakeTimestamp, ev)); + } + + String output2 = getdump("flush"); + assertEquals("", output2); + } + public void testEndToEndLogging() { IpConnectivityLog logger = new IpConnectivityLog(mService.impl);