| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.net.metrics; |
| 18 |
|
| 19 |
import com.android.internal.util.TokenBucket; |
| 20 |
|
| 21 |
import java.util.BitSet; |
| 22 |
import java.util.StringJoiner; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class NetworkMetrics { |
| 34 |
|
| 35 |
private static final int INITIAL_DNS_BATCH_SIZE = 100; |
| 36 |
private static final int CONNECT_LATENCY_MAXIMUM_RECORDS = 20000; |
| 37 |
|
| 38 |
|
| 39 |
public final int netId; |
| 40 |
|
| 41 |
public final long transports; |
| 42 |
|
| 43 |
public final ConnectStats connectMetrics; |
| 44 |
|
| 45 |
public final DnsEvent dnsMetrics; |
| 46 |
|
| 47 |
public final Summary summary; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public Summary pendingSummary; |
| 52 |
|
| 53 |
public NetworkMetrics(int netId, long transports, TokenBucket tb) { |
| 54 |
this.netId = netId; |
| 55 |
this.transports = transports; |
| 56 |
this.connectMetrics = |
| 57 |
new ConnectStats(netId, transports, tb, CONNECT_LATENCY_MAXIMUM_RECORDS); |
| 58 |
this.dnsMetrics = new DnsEvent(netId, transports, INITIAL_DNS_BATCH_SIZE); |
| 59 |
this.summary = new Summary(netId, transports); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public Summary getPendingStats() { |
| 67 |
Summary s = pendingSummary; |
| 68 |
pendingSummary = null; |
| 69 |
if (s != null) { |
| 70 |
summary.merge(s); |
| 71 |
} |
| 72 |
return s; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
public void addDnsResult(int eventType, int returnCode, int latencyMs) { |
| 77 |
if (pendingSummary == null) { |
| 78 |
pendingSummary = new Summary(netId, transports); |
| 79 |
} |
| 80 |
boolean isSuccess = dnsMetrics.addResult((byte) eventType, (byte) returnCode, latencyMs); |
| 81 |
pendingSummary.dnsLatencies.count(latencyMs); |
| 82 |
pendingSummary.dnsErrorRate.count(isSuccess ? 0 : 1); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
public void addConnectResult(int error, int latencyMs, String ipAddr) { |
| 87 |
if (pendingSummary == null) { |
| 88 |
pendingSummary = new Summary(netId, transports); |
| 89 |
} |
| 90 |
boolean isSuccess = connectMetrics.addEvent(error, latencyMs, ipAddr); |
| 91 |
pendingSummary.connectErrorRate.count(isSuccess ? 0 : 1); |
| 92 |
if (ConnectStats.isNonBlocking(error)) { |
| 93 |
pendingSummary.connectLatencies.count(latencyMs); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
public void addTcpStatsResult(int sent, int lost, int rttUs, int sentAckDiffMs) { |
| 99 |
if (pendingSummary == null) { |
| 100 |
pendingSummary = new Summary(netId, transports); |
| 101 |
} |
| 102 |
pendingSummary.tcpLossRate.count(lost, sent); |
| 103 |
pendingSummary.roundTripTimeUs.count(rttUs); |
| 104 |
pendingSummary.sentAckTimeDiffenceMs.count(sentAckDiffMs); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
public static class Summary { |
| 109 |
|
| 110 |
public final int netId; |
| 111 |
public final long transports; |
| 112 |
|
| 113 |
public final Metrics dnsLatencies = new Metrics(); |
| 114 |
|
| 115 |
public final Metrics dnsErrorRate = new Metrics(); |
| 116 |
|
| 117 |
public final Metrics connectLatencies = new Metrics(); |
| 118 |
|
| 119 |
public final Metrics connectErrorRate = new Metrics(); |
| 120 |
|
| 121 |
public final Metrics tcpLossRate = new Metrics(); |
| 122 |
|
| 123 |
public final Metrics roundTripTimeUs = new Metrics(); |
| 124 |
|
| 125 |
|
| 126 |
public final Metrics sentAckTimeDiffenceMs = new Metrics(); |
| 127 |
|
| 128 |
public Summary(int netId, long transports) { |
| 129 |
this.netId = netId; |
| 130 |
this.transports = transports; |
| 131 |
} |
| 132 |
|
| 133 |
void merge(Summary that) { |
| 134 |
dnsLatencies.merge(that.dnsLatencies); |
| 135 |
dnsErrorRate.merge(that.dnsErrorRate); |
| 136 |
connectLatencies.merge(that.connectLatencies); |
| 137 |
connectErrorRate.merge(that.connectErrorRate); |
| 138 |
tcpLossRate.merge(that.tcpLossRate); |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
public String toString() { |
| 143 |
StringJoiner j = new StringJoiner(", ", "{", "}"); |
| 144 |
j.add("netId=" + netId); |
| 145 |
j.add("transports=" + BitSet.valueOf(new long[] { transports })); |
| 146 |
j.add(String.format("dns avg=%dms max=%dms err=%.1f%% tot=%d", |
| 147 |
(int) dnsLatencies.average(), (int) dnsLatencies.max, |
| 148 |
100 * dnsErrorRate.average(), dnsErrorRate.count)); |
| 149 |
j.add(String.format("connect avg=%dms max=%dms err=%.1f%% tot=%d", |
| 150 |
(int) connectLatencies.average(), (int) connectLatencies.max, |
| 151 |
100 * connectErrorRate.average(), connectErrorRate.count)); |
| 152 |
j.add(String.format("tcp avg_loss=%.1f%% total_sent=%d total_lost=%d", |
| 153 |
100 * tcpLossRate.average(), tcpLossRate.count, (int) tcpLossRate.sum)); |
| 154 |
j.add(String.format("tcp rtt=%dms", (int) (roundTripTimeUs.average() / 1000))); |
| 155 |
j.add(String.format("tcp sent-ack_diff=%dms", (int) sentAckTimeDiffenceMs.average())); |
| 156 |
return j.toString(); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
static class Metrics { |
| 162 |
public double sum; |
| 163 |
public double max = Double.MIN_VALUE; |
| 164 |
public int count; |
| 165 |
|
| 166 |
void merge(Metrics that) { |
| 167 |
this.count += that.count; |
| 168 |
this.sum += that.sum; |
| 169 |
this.max = Math.max(this.max, that.max); |
| 170 |
} |
| 171 |
|
| 172 |
void count(double value) { |
| 173 |
count(value, 1); |
| 174 |
} |
| 175 |
|
| 176 |
void count(double value, int subcount) { |
| 177 |
count += subcount; |
| 178 |
sum += value; |
| 179 |
max = Math.max(max, value); |
| 180 |
} |
| 181 |
|
| 182 |
double average() { |
| 183 |
double a = sum / (double) count; |
| 184 |
if (Double.isNaN(a)) { |
| 185 |
a = 0; |
| 186 |
} |
| 187 |
return a; |
| 188 |
} |
| 189 |
} |
| 190 |
} |