Gracefully handle integer overflows.

Avoid recording negative data by doing integer-based
math as much as possible, but switch to double-based
math if we detect that we'd end up causing an overflow.

Test :

    - Builds, Boots
    - High data throughput scenarios
    - NetworkStatsHistoryTest, NetworkStatsCollectionTest,
      NetworkStatsTest

Bug: 119527458
Change-Id: I55d4aa649bea3ae201ad513074ff2b71cb918dad
This commit is contained in:
Pavan Kumar M
2020-07-21 14:18:23 +05:30
parent 28ced6a6ca
commit fc5b6cb326
2 changed files with 36 additions and 46 deletions

View File

@@ -26,6 +26,7 @@ import static android.net.NetworkStatsHistory.DataStreamUtils.writeVarLongArray;
import static android.net.NetworkStatsHistory.Entry.UNKNOWN;
import static android.net.NetworkStatsHistory.ParcelUtils.readLongArray;
import static android.net.NetworkStatsHistory.ParcelUtils.writeLongArray;
import static android.net.NetworkUtils.multiplySafeByRational;
import static android.text.format.DateUtils.SECOND_IN_MILLIS;
import static com.android.internal.util.ArrayUtils.total;
@@ -364,11 +365,12 @@ public class NetworkStatsHistory implements Parcelable {
if (overlap <= 0) continue;
// integer math each time is faster than floating point
final long fracRxBytes = rxBytes * overlap / duration;
final long fracRxPackets = rxPackets * overlap / duration;
final long fracTxBytes = txBytes * overlap / duration;
final long fracTxPackets = txPackets * overlap / duration;
final long fracOperations = operations * overlap / duration;
final long fracRxBytes = multiplySafeByRational(rxBytes, overlap, duration);
final long fracRxPackets = multiplySafeByRational(rxPackets, overlap, duration);
final long fracTxBytes = multiplySafeByRational(txBytes, overlap, duration);
final long fracTxPackets = multiplySafeByRational(txPackets, overlap, duration);
final long fracOperations = multiplySafeByRational(operations, overlap, duration);
addLong(activeTime, i, overlap);
addLong(this.rxBytes, i, fracRxBytes); rxBytes -= fracRxBytes;
@@ -568,12 +570,24 @@ public class NetworkStatsHistory implements Parcelable {
if (overlap <= 0) continue;
// integer math each time is faster than floating point
if (activeTime != null) entry.activeTime += activeTime[i] * overlap / bucketSpan;
if (rxBytes != null) entry.rxBytes += rxBytes[i] * overlap / bucketSpan;
if (rxPackets != null) entry.rxPackets += rxPackets[i] * overlap / bucketSpan;
if (txBytes != null) entry.txBytes += txBytes[i] * overlap / bucketSpan;
if (txPackets != null) entry.txPackets += txPackets[i] * overlap / bucketSpan;
if (operations != null) entry.operations += operations[i] * overlap / bucketSpan;
if (activeTime != null) {
entry.activeTime += multiplySafeByRational(activeTime[i], overlap, bucketSpan);
}
if (rxBytes != null) {
entry.rxBytes += multiplySafeByRational(rxBytes[i], overlap, bucketSpan);
}
if (rxPackets != null) {
entry.rxPackets += multiplySafeByRational(rxPackets[i], overlap, bucketSpan);
}
if (txBytes != null) {
entry.txBytes += multiplySafeByRational(txBytes[i], overlap, bucketSpan);
}
if (txPackets != null) {
entry.txPackets += multiplySafeByRational(txPackets[i], overlap, bucketSpan);
}
if (operations != null) {
entry.operations += multiplySafeByRational(operations[i], overlap, bucketSpan);
}
}
return entry;
}