Skip to content

Commit 4c48d2f

Browse files
jsharkeyAndroid (Google) Code Review
authored andcommitted
Merge "Make operation counts monotonically increase." into ics-mr1
2 parents 2048789 + 4abb1b8 commit 4c48d2f

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

core/java/android/net/NetworkStats.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,17 @@ public void writeToParcel(Parcel dest, int flags) {
165165
dest.writeLongArray(operations);
166166
}
167167

168+
@Override
169+
public NetworkStats clone() {
170+
final NetworkStats clone = new NetworkStats(elapsedRealtime, size);
171+
NetworkStats.Entry entry = null;
172+
for (int i = 0; i < size; i++) {
173+
entry = getValues(i, entry);
174+
clone.addValues(entry);
175+
}
176+
return clone;
177+
}
178+
168179
// @VisibleForTesting
169180
public NetworkStats addIfaceValues(
170181
String iface, long rxBytes, long rxPackets, long txBytes, long txPackets) {

core/tests/coretests/src/android/net/NetworkStatsTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,22 @@ public void testWithoutUid() throws Exception {
294294
assertValues(after, 1, TEST_IFACE, 101, SET_DEFAULT, 0xF00D, 128L, 8L, 0L, 0L, 0L);
295295
}
296296

297+
public void testClone() throws Exception {
298+
final NetworkStats original = new NetworkStats(TEST_START, 5)
299+
.addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 128L, 8L, 0L, 2L, 20L)
300+
.addValues(TEST_IFACE2, 100, SET_DEFAULT, TAG_NONE, 512L, 32L, 0L, 0L, 0L);
301+
302+
// make clone and mutate original
303+
final NetworkStats clone = original.clone();
304+
original.addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 128L, 8L, 0L, 0L, 0L);
305+
306+
assertEquals(3, original.size());
307+
assertEquals(2, clone.size());
308+
309+
assertEquals(128L + 512L + 128L, original.getTotalBytes());
310+
assertEquals(128L + 512L, clone.getTotalBytes());
311+
}
312+
297313
private static void assertValues(NetworkStats stats, int index, String iface, int uid, int set,
298314
int tag, long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) {
299315
final NetworkStats.Entry entry = stats.getValues(index, null);

services/java/com/android/server/net/NetworkStatsService.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -971,8 +971,7 @@ private void performUidPollLocked(NetworkStats uidSnapshot, long currentTime) {
971971
}
972972

973973
mLastPollUidSnapshot = uidSnapshot;
974-
mLastPollOperationsSnapshot = mOperations;
975-
mOperations = new NetworkStats(0L, 10);
974+
mLastPollOperationsSnapshot = mOperations.clone();
976975
}
977976

978977
/**

0 commit comments

Comments
 (0)