Skip to content

Commit e44365b

Browse files
jsharkeyAndroid (Google) Code Review
authored andcommitted
Merge "Data usage structure optimizations."
2 parents 1357c0a + 69b0f63 commit e44365b

File tree

5 files changed

+135
-50
lines changed

5 files changed

+135
-50
lines changed

core/java/android/net/NetworkStatsHistory.java

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import android.os.Parcel;
3131
import android.os.Parcelable;
32+
import android.util.MathUtils;
3233

3334
import java.io.CharArrayWriter;
3435
import java.io.DataInputStream;
@@ -206,6 +207,34 @@ public long getEnd() {
206207
}
207208
}
208209

210+
/**
211+
* Return index of bucket that contains or is immediately before the
212+
* requested time.
213+
*/
214+
public int getIndexBefore(long time) {
215+
int index = Arrays.binarySearch(bucketStart, 0, bucketCount, time);
216+
if (index < 0) {
217+
index = (~index) - 1;
218+
} else {
219+
index -= 1;
220+
}
221+
return MathUtils.constrain(index, 0, bucketCount - 1);
222+
}
223+
224+
/**
225+
* Return index of bucket that contains or is immediately after the
226+
* requested time.
227+
*/
228+
public int getIndexAfter(long time) {
229+
int index = Arrays.binarySearch(bucketStart, 0, bucketCount, time);
230+
if (index < 0) {
231+
index = ~index;
232+
} else {
233+
index += 1;
234+
}
235+
return MathUtils.constrain(index, 0, bucketCount - 1);
236+
}
237+
209238
/**
210239
* Return specific stats entry.
211240
*/
@@ -247,7 +276,8 @@ public void recordData(long start, long end, NetworkStats.Entry entry) {
247276

248277
// distribute data usage into buckets
249278
long duration = end - start;
250-
for (int i = bucketCount - 1; i >= 0; i--) {
279+
final int startIndex = getIndexAfter(end);
280+
for (int i = startIndex; i >= 0; i--) {
251281
final long curStart = bucketStart[i];
252282
final long curEnd = curStart + bucketDuration;
253283

@@ -406,7 +436,8 @@ public Entry getValues(long start, long end, long now, Entry recycle) {
406436
entry.txPackets = txPackets != null ? 0 : UNKNOWN;
407437
entry.operations = operations != null ? 0 : UNKNOWN;
408438

409-
for (int i = bucketCount - 1; i >= 0; i--) {
439+
final int startIndex = getIndexAfter(end);
440+
for (int i = startIndex; i >= 0; i--) {
410441
final long curStart = bucketStart[i];
411442
final long curEnd = curStart + bucketDuration;
412443

@@ -417,8 +448,14 @@ public Entry getValues(long start, long end, long now, Entry recycle) {
417448

418449
// include full value for active buckets, otherwise only fractional
419450
final boolean activeBucket = curStart < now && curEnd > now;
420-
final long overlap = activeBucket ? bucketDuration
421-
: Math.min(curEnd, end) - Math.max(curStart, start);
451+
final long overlap;
452+
if (activeBucket) {
453+
overlap = bucketDuration;
454+
} else {
455+
final long overlapEnd = curEnd < end ? curEnd : end;
456+
final long overlapStart = curStart > start ? curStart : start;
457+
overlap = overlapEnd - overlapStart;
458+
}
422459
if (overlap <= 0) continue;
423460

424461
// integer math each time is faster than floating point

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,54 @@ public void testVarLong() throws Exception {
407407
assertEquals(Long.MAX_VALUE - 40, performVarLong(Long.MAX_VALUE - 40));
408408
}
409409

410+
public void testIndexBeforeAfter() throws Exception {
411+
final long BUCKET_SIZE = HOUR_IN_MILLIS;
412+
stats = new NetworkStatsHistory(BUCKET_SIZE);
413+
414+
final long FIRST_START = TEST_START;
415+
final long FIRST_END = FIRST_START + (2 * HOUR_IN_MILLIS);
416+
final long SECOND_START = TEST_START + WEEK_IN_MILLIS;
417+
final long SECOND_END = SECOND_START + HOUR_IN_MILLIS;
418+
final long THIRD_START = TEST_START + (2 * WEEK_IN_MILLIS);
419+
final long THIRD_END = THIRD_START + (2 * HOUR_IN_MILLIS);
420+
421+
stats.recordData(FIRST_START, FIRST_END,
422+
new NetworkStats.Entry(1024L, 10L, 2048L, 20L, 2L));
423+
stats.recordData(SECOND_START, SECOND_END,
424+
new NetworkStats.Entry(1024L, 10L, 2048L, 20L, 2L));
425+
stats.recordData(THIRD_START, THIRD_END,
426+
new NetworkStats.Entry(1024L, 10L, 2048L, 20L, 2L));
427+
428+
// should have buckets: 2+1+2
429+
assertEquals(5, stats.size());
430+
431+
assertIndexBeforeAfter(stats, 0, 0, Long.MIN_VALUE);
432+
assertIndexBeforeAfter(stats, 0, 1, FIRST_START);
433+
assertIndexBeforeAfter(stats, 0, 1, FIRST_START + MINUTE_IN_MILLIS);
434+
assertIndexBeforeAfter(stats, 0, 2, FIRST_START + HOUR_IN_MILLIS);
435+
assertIndexBeforeAfter(stats, 1, 2, FIRST_START + HOUR_IN_MILLIS + MINUTE_IN_MILLIS);
436+
assertIndexBeforeAfter(stats, 1, 2, FIRST_END - MINUTE_IN_MILLIS);
437+
assertIndexBeforeAfter(stats, 1, 2, FIRST_END);
438+
assertIndexBeforeAfter(stats, 1, 2, FIRST_END + MINUTE_IN_MILLIS);
439+
assertIndexBeforeAfter(stats, 1, 2, SECOND_START - MINUTE_IN_MILLIS);
440+
assertIndexBeforeAfter(stats, 1, 3, SECOND_START);
441+
assertIndexBeforeAfter(stats, 2, 3, SECOND_END);
442+
assertIndexBeforeAfter(stats, 2, 3, SECOND_END + MINUTE_IN_MILLIS);
443+
assertIndexBeforeAfter(stats, 2, 3, THIRD_START - MINUTE_IN_MILLIS);
444+
assertIndexBeforeAfter(stats, 2, 4, THIRD_START);
445+
assertIndexBeforeAfter(stats, 3, 4, THIRD_START + MINUTE_IN_MILLIS);
446+
assertIndexBeforeAfter(stats, 3, 4, THIRD_START + HOUR_IN_MILLIS);
447+
assertIndexBeforeAfter(stats, 4, 4, THIRD_END);
448+
assertIndexBeforeAfter(stats, 4, 4, THIRD_END + MINUTE_IN_MILLIS);
449+
assertIndexBeforeAfter(stats, 4, 4, Long.MAX_VALUE);
450+
}
451+
452+
private static void assertIndexBeforeAfter(
453+
NetworkStatsHistory stats, int before, int after, long time) {
454+
assertEquals("unexpected before", before, stats.getIndexBefore(time));
455+
assertEquals("unexpected after", after, stats.getIndexAfter(time));
456+
}
457+
410458
private static long performVarLong(long before) throws Exception {
411459
final ByteArrayOutputStream out = new ByteArrayOutputStream();
412460
writeVarLong(new DataOutputStream(out), before);
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
acct_tag_hex uid_tag_int iface rx_bytes rx_packets tx_bytes tx_packets teleported_goats
2-
0x0 1000 test0 1024 10 2048 20 2716057
3-
0x0000F00D00000000 1000 test0 512 5 512 5 3370318
1+
acct_tag_hex uid_tag_int iface rx_bytes rx_packets tx_bytes tx_packets teleported_goats idx
2+
0x0 1000 test0 1024 10 2048 20 2716057 2
3+
0x0000F00D00000000 1000 test0 512 5 512 5 3370318 3
Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
idx iface acct_tag_hex uid_tag_int rx_bytes tx_bytes
2-
1 wlan0 0x0 0 14615 4270
3-
2 wlan0 0x0 1000 5175 915
4-
3 wlan0 0x0 1021 3381 903
5-
4 wlan0 0x0 10004 333821 53558
6-
5 wlan0 0x0 10010 4888 37363
7-
6 wlan0 0x0 10013 52 104
8-
7 wlan0 0x74182ada00000000 10004 18725 1066
9-
8 rmnet0 0x0 0 301274 30244
10-
9 rmnet0 0x0 1000 304 441
11-
10 rmnet0 0x0 1013 2880 2272
12-
11 rmnet0 0x0 1021 31407 8430
13-
12 rmnet0 0x0 10003 32665 3814
14-
13 rmnet0 0x0 10004 2373141 420112
15-
14 rmnet0 0x0 10010 870370 1111727
16-
15 rmnet0 0x0 10013 240 240
17-
16 rmnet0 0x0 10016 16703 13512
18-
17 rmnet0 0x0 10017 3990 3269
19-
18 rmnet0 0x0 10018 474504 14516062
20-
19 rmnet0 0x0 10019 782804 71077
21-
20 rmnet0 0x0 10022 70671 49684
22-
21 rmnet0 0x0 10029 5785354 397159
23-
22 rmnet0 0x0 10033 2102 1686
24-
23 rmnet0 0x0 10034 15495464 227694
25-
24 rmnet0 0x0 10037 31184994 684122
26-
25 rmnet0 0x0 10051 298687 113485
27-
26 rmnet0 0x0 10056 29504 20669
28-
27 rmnet0 0x0 10069 683 596
29-
28 rmnet0 0x0 10072 34051 12453
30-
29 rmnet0 0x0 10077 7025393 213866
31-
30 rmnet0 0x0 10081 354 1178
32-
31 rmnet0 0x74182ada00000000 10037 28507378 437004
2+
2 wlan0 0x0 0 14615 4270
3+
3 wlan0 0x0 1000 5175 915
4+
4 wlan0 0x0 1021 3381 903
5+
5 wlan0 0x0 10004 333821 53558
6+
6 wlan0 0x0 10010 4888 37363
7+
7 wlan0 0x0 10013 52 104
8+
8 wlan0 0x74182ada00000000 10004 18725 1066
9+
9 rmnet0 0x0 0 301274 30244
10+
10 rmnet0 0x0 1000 304 441
11+
11 rmnet0 0x0 1013 2880 2272
12+
12 rmnet0 0x0 1021 31407 8430
13+
13 rmnet0 0x0 10003 32665 3814
14+
14 rmnet0 0x0 10004 2373141 420112
15+
15 rmnet0 0x0 10010 870370 1111727
16+
16 rmnet0 0x0 10013 240 240
17+
17 rmnet0 0x0 10016 16703 13512
18+
18 rmnet0 0x0 10017 3990 3269
19+
19 rmnet0 0x0 10018 474504 14516062
20+
20 rmnet0 0x0 10019 782804 71077
21+
21 rmnet0 0x0 10022 70671 49684
22+
22 rmnet0 0x0 10029 5785354 397159
23+
23 rmnet0 0x0 10033 2102 1686
24+
24 rmnet0 0x0 10034 15495464 227694
25+
25 rmnet0 0x0 10037 31184994 684122
26+
26 rmnet0 0x0 10051 298687 113485
27+
27 rmnet0 0x0 10056 29504 20669
28+
28 rmnet0 0x0 10069 683 596
29+
29 rmnet0 0x0 10072 34051 12453
30+
30 rmnet0 0x0 10077 7025393 213866
31+
31 rmnet0 0x0 10081 354 1178
32+
32 rmnet0 0x74182ada00000000 10037 28507378 437004
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
idx iface acct_tag_hex uid_tag_int cnt_set rx_bytes rx_packets tx_bytes tx_packets rx_tcp_packets rx_tcp_bytes rx_udp_packets rx_udp_bytes rx_other_packets rx_other_bytes tx_tcp_packets tx_tcp_bytes tx_udp_packets tx_udp_bytes tx_other_packets tx_other_bytes
2-
1 rmnet0 0x0 0 0 14855 82 2804 47 2000 45 12799 35 56 2 676 13 2128 34 0 0
3-
1 rmnet0 0x0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4-
2 rmnet0 0x0 1000 0 278102 253 10487 182 277342 243 760 10 0 0 9727 172 760 10 0 0
5-
2 rmnet0 0x0 1000 1 26033 30 1401 26 25881 28 152 2 0 0 1249 24 152 2 0 0
6-
3 rmnet0 0x0 10012 0 40524 272 134138 293 40524 272 0 0 0 0 134138 293 0 0 0 0
7-
3 rmnet0 0x0 10012 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
8-
4 rmnet0 0x0 10034 0 15791 59 9905 69 15791 59 0 0 0 0 9905 69 0 0 0 0
9-
4 rmnet0 0x0 10034 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
10-
5 rmnet0 0x0 10055 0 3602 29 7739 59 3602 29 0 0 0 0 7739 59 0 0 0 0
11-
5 rmnet0 0x0 10055 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
12-
6 rmnet0 0x7fff000300000000 1000 0 483 4 1931 6 483 4 0 0 0 0 1931 6 0 0 0 0
13-
6 rmnet0 0x7fff000300000000 1000 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2+
2 rmnet0 0x0 0 0 14855 82 2804 47 2000 45 12799 35 56 2 676 13 2128 34 0 0
3+
3 rmnet0 0x0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4+
4 rmnet0 0x0 1000 0 278102 253 10487 182 277342 243 760 10 0 0 9727 172 760 10 0 0
5+
5 rmnet0 0x0 1000 1 26033 30 1401 26 25881 28 152 2 0 0 1249 24 152 2 0 0
6+
6 rmnet0 0x0 10012 0 40524 272 134138 293 40524 272 0 0 0 0 134138 293 0 0 0 0
7+
7 rmnet0 0x0 10012 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
8+
8 rmnet0 0x0 10034 0 15791 59 9905 69 15791 59 0 0 0 0 9905 69 0 0 0 0
9+
9 rmnet0 0x0 10034 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
10+
10 rmnet0 0x0 10055 0 3602 29 7739 59 3602 29 0 0 0 0 7739 59 0 0 0 0
11+
11 rmnet0 0x0 10055 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
12+
12 rmnet0 0x7fff000300000000 1000 0 483 4 1931 6 483 4 0 0 0 0 1931 6 0 0 0 0
13+
13 rmnet0 0x7fff000300000000 1000 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 commit comments

Comments
 (0)