| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.net.metrics; |
| 18 |
|
| 19 |
import android.system.OsConstants; |
| 20 |
import android.util.IntArray; |
| 21 |
import android.util.SparseIntArray; |
| 22 |
|
| 23 |
import com.android.internal.util.TokenBucket; |
| 24 |
|
| 25 |
import java.util.BitSet; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class ConnectStats { |
| 32 |
private final static int EALREADY = OsConstants.EALREADY; |
| 33 |
private final static int EINPROGRESS = OsConstants.EINPROGRESS; |
| 34 |
|
| 35 |
|
| 36 |
public final int netId; |
| 37 |
|
| 38 |
public final long transports; |
| 39 |
|
| 40 |
public final SparseIntArray errnos = new SparseIntArray(); |
| 41 |
|
| 42 |
public final IntArray latencies = new IntArray(); |
| 43 |
|
| 44 |
public final TokenBucket mLatencyTb; |
| 45 |
|
| 46 |
public final int mMaxLatencyRecords; |
| 47 |
|
| 48 |
public int eventCount = 0; |
| 49 |
|
| 50 |
public int connectCount = 0; |
| 51 |
|
| 52 |
public int connectBlockingCount = 0; |
| 53 |
|
| 54 |
public int ipv6ConnectCount = 0; |
| 55 |
|
| 56 |
public ConnectStats(int netId, long transports, TokenBucket tb, int maxLatencyRecords) { |
| 57 |
this.netId = netId; |
| 58 |
this.transports = transports; |
| 59 |
mLatencyTb = tb; |
| 60 |
mMaxLatencyRecords = maxLatencyRecords; |
| 61 |
} |
| 62 |
|
| 63 |
boolean addEvent(int errno, int latencyMs, String ipAddr) { |
| 64 |
eventCount++; |
| 65 |
if (isSuccess(errno)) { |
| 66 |
countConnect(errno, ipAddr); |
| 67 |
countLatency(errno, latencyMs); |
| 68 |
return true; |
| 69 |
} else { |
| 70 |
countError(errno); |
| 71 |
return false; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
private void countConnect(int errno, String ipAddr) { |
| 76 |
connectCount++; |
| 77 |
if (!isNonBlocking(errno)) { |
| 78 |
connectBlockingCount++; |
| 79 |
} |
| 80 |
if (isIPv6(ipAddr)) { |
| 81 |
ipv6ConnectCount++; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
private void countLatency(int errno, int ms) { |
| 86 |
if (isNonBlocking(errno)) { |
| 87 |
|
| 88 |
return; |
| 89 |
} |
| 90 |
if (!mLatencyTb.get()) { |
| 91 |
|
| 92 |
return; |
| 93 |
} |
| 94 |
if (latencies.size() >= mMaxLatencyRecords) { |
| 95 |
|
| 96 |
return; |
| 97 |
} |
| 98 |
latencies.add(ms); |
| 99 |
} |
| 100 |
|
| 101 |
private void countError(int errno) { |
| 102 |
final int newcount = errnos.get(errno, 0) + 1; |
| 103 |
errnos.put(errno, newcount); |
| 104 |
} |
| 105 |
|
| 106 |
private static boolean isSuccess(int errno) { |
| 107 |
return (errno == 0) || isNonBlocking(errno); |
| 108 |
} |
| 109 |
|
| 110 |
static boolean isNonBlocking(int errno) { |
| 111 |
|
| 112 |
|
| 113 |
return (errno == EINPROGRESS) || (errno == EALREADY); |
| 114 |
} |
| 115 |
|
| 116 |
private static boolean isIPv6(String ipAddr) { |
| 117 |
return ipAddr.contains(":"); |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public String toString() { |
| 122 |
StringBuilder builder = |
| 123 |
new StringBuilder("ConnectStats(").append("netId=").append(netId) |
| 124 |
.append(", transports=").append(BitSet.valueOf(new long[] { transports })) |
| 125 |
.append(", "); |
| 126 |
builder.append(String.format("%d events, ", eventCount)); |
| 127 |
builder.append(String.format("%d success, ", connectCount)); |
| 128 |
builder.append(String.format("%d blocking, ", connectBlockingCount)); |
| 129 |
builder.append(String.format("%d IPv6 dst", ipv6ConnectCount)); |
| 130 |
for (int i = 0; i < errnos.size(); i++) { |
| 131 |
String errno = OsConstants.errnoName(errnos.keyAt(i)); |
| 132 |
int count = errnos.valueAt(i); |
| 133 |
builder.append(String.format(", %s: %d", errno, count)); |
| 134 |
} |
| 135 |
return builder.append(")").toString(); |
| 136 |
} |
| 137 |
} |