Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.net.metrics; |
| 18 | |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 19 | import android.net.NetworkCapabilities; |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 20 | import android.system.OsConstants; |
| 21 | import android.util.IntArray; |
| 22 | import android.util.SparseIntArray; |
Hugo Benichi | ab20975 | 2017-09-27 23:28:59 +0900 | [diff] [blame] | 23 | |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 24 | import com.android.internal.util.BitUtils; |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 25 | import com.android.internal.util.TokenBucket; |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 26 | |
| 27 | /** |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 28 | * A class that aggregates connect() statistics. |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 29 | * {@hide} |
| 30 | */ |
| 31 | public class ConnectStats { |
| 32 | private final static int EALREADY = OsConstants.EALREADY; |
| 33 | private final static int EINPROGRESS = OsConstants.EINPROGRESS; |
| 34 | |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 35 | /** Network id of the network associated with the event, or 0 if unspecified. */ |
| 36 | public final int netId; |
| 37 | /** Transports of the network associated with the event, as defined in NetworkCapabilities. */ |
| 38 | public final long transports; |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 39 | /** How many events resulted in a given errno. */ |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 40 | public final SparseIntArray errnos = new SparseIntArray(); |
| 41 | /** Latencies of successful blocking connects. TODO: add non-blocking connects latencies. */ |
| 42 | public final IntArray latencies = new IntArray(); |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 43 | /** TokenBucket for rate limiting latency recording. */ |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 44 | public final TokenBucket mLatencyTb; |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 45 | /** Maximum number of latency values recorded. */ |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 46 | public final int mMaxLatencyRecords; |
Hugo Benichi | ab20975 | 2017-09-27 23:28:59 +0900 | [diff] [blame] | 47 | /** Total count of events */ |
| 48 | public int eventCount = 0; |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 49 | /** Total count of successful connects. */ |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 50 | public int connectCount = 0; |
Hugo Benichi | a2decca | 2017-02-22 14:32:27 +0900 | [diff] [blame] | 51 | /** Total count of successful connects done in blocking mode. */ |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 52 | public int connectBlockingCount = 0; |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 53 | /** Total count of successful connects with IPv6 socket address. */ |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 54 | public int ipv6ConnectCount = 0; |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 55 | |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 56 | public ConnectStats(int netId, long transports, TokenBucket tb, int maxLatencyRecords) { |
| 57 | this.netId = netId; |
| 58 | this.transports = transports; |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 59 | mLatencyTb = tb; |
| 60 | mMaxLatencyRecords = maxLatencyRecords; |
| 61 | } |
| 62 | |
Hugo Benichi | ab20975 | 2017-09-27 23:28:59 +0900 | [diff] [blame] | 63 | boolean addEvent(int errno, int latencyMs, String ipAddr) { |
| 64 | eventCount++; |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 65 | if (isSuccess(errno)) { |
Hugo Benichi | a2decca | 2017-02-22 14:32:27 +0900 | [diff] [blame] | 66 | countConnect(errno, ipAddr); |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 67 | countLatency(errno, latencyMs); |
Hugo Benichi | ab20975 | 2017-09-27 23:28:59 +0900 | [diff] [blame] | 68 | return true; |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 69 | } else { |
| 70 | countError(errno); |
Hugo Benichi | ab20975 | 2017-09-27 23:28:59 +0900 | [diff] [blame] | 71 | return false; |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Hugo Benichi | a2decca | 2017-02-22 14:32:27 +0900 | [diff] [blame] | 75 | private void countConnect(int errno, String ipAddr) { |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 76 | connectCount++; |
Hugo Benichi | a2decca | 2017-02-22 14:32:27 +0900 | [diff] [blame] | 77 | if (!isNonBlocking(errno)) { |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 78 | connectBlockingCount++; |
Hugo Benichi | a2decca | 2017-02-22 14:32:27 +0900 | [diff] [blame] | 79 | } |
| 80 | if (isIPv6(ipAddr)) { |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 81 | ipv6ConnectCount++; |
Hugo Benichi | a2decca | 2017-02-22 14:32:27 +0900 | [diff] [blame] | 82 | } |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | private void countLatency(int errno, int ms) { |
| 86 | if (isNonBlocking(errno)) { |
| 87 | // Ignore connect() on non-blocking sockets |
| 88 | return; |
| 89 | } |
| 90 | if (!mLatencyTb.get()) { |
| 91 | // Rate limited |
| 92 | return; |
| 93 | } |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 94 | if (latencies.size() >= mMaxLatencyRecords) { |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 95 | // Hard limit the total number of latency measurements. |
| 96 | return; |
| 97 | } |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 98 | latencies.add(ms); |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | private void countError(int errno) { |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 102 | final int newcount = errnos.get(errno, 0) + 1; |
| 103 | errnos.put(errno, newcount); |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | private static boolean isSuccess(int errno) { |
| 107 | return (errno == 0) || isNonBlocking(errno); |
| 108 | } |
| 109 | |
Hugo Benichi | ab20975 | 2017-09-27 23:28:59 +0900 | [diff] [blame] | 110 | static boolean isNonBlocking(int errno) { |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 111 | // On non-blocking TCP sockets, connect() immediately returns EINPROGRESS. |
| 112 | // On non-blocking TCP sockets that are connecting, connect() immediately returns EALREADY. |
| 113 | return (errno == EINPROGRESS) || (errno == EALREADY); |
| 114 | } |
| 115 | |
| 116 | private static boolean isIPv6(String ipAddr) { |
| 117 | return ipAddr.contains(":"); |
| 118 | } |
| 119 | |
Hugo Benichi | a2decca | 2017-02-22 14:32:27 +0900 | [diff] [blame] | 120 | @Override |
| 121 | public String toString() { |
Hugo Benichi | c867f78 | 2017-11-07 21:42:10 +0900 | [diff] [blame] | 122 | StringBuilder builder = |
| 123 | new StringBuilder("ConnectStats(").append("netId=").append(netId).append(", "); |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 124 | for (int t : BitUtils.unpackBits(transports)) { |
| 125 | builder.append(NetworkCapabilities.transportNameOf(t)).append(", "); |
| 126 | } |
Hugo Benichi | ab20975 | 2017-09-27 23:28:59 +0900 | [diff] [blame] | 127 | builder.append(String.format("%d events, ", eventCount)); |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 128 | builder.append(String.format("%d success, ", connectCount)); |
| 129 | builder.append(String.format("%d blocking, ", connectBlockingCount)); |
| 130 | builder.append(String.format("%d IPv6 dst", ipv6ConnectCount)); |
| 131 | for (int i = 0; i < errnos.size(); i++) { |
| 132 | String errno = OsConstants.errnoName(errnos.keyAt(i)); |
| 133 | int count = errnos.valueAt(i); |
Hugo Benichi | a2decca | 2017-02-22 14:32:27 +0900 | [diff] [blame] | 134 | builder.append(String.format(", %s: %d", errno, count)); |
| 135 | } |
| 136 | return builder.append(")").toString(); |
| 137 | } |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 138 | } |