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 
19 import android.net.NetworkCapabilities;
20 import android.system.OsConstants;
21 import android.util.IntArray;
22 import android.util.SparseIntArray;
23 
24 import com.android.internal.util.BitUtils;
25 import com.android.internal.util.TokenBucket;
26 
27 /**
28  * A class that aggregates connect() statistics.
29  * {@hide}
30  */
31 public class ConnectStats {
32     private final static int EALREADY     = OsConstants.EALREADY;
33     private final static int EINPROGRESS  = OsConstants.EINPROGRESS;
34 
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;
39     /** How many events resulted in a given errno. */
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();
43     /** TokenBucket for rate limiting latency recording. */
44     public final TokenBucket mLatencyTb;
45     /** Maximum number of latency values recorded. */
46     public final int mMaxLatencyRecords;
47     /** Total count of events */
48     public int eventCount = 0;
49     /** Total count of successful connects. */
50     public int connectCount = 0;
51     /** Total count of successful connects done in blocking mode. */
52     public int connectBlockingCount = 0;
53     /** Total count of successful connects with IPv6 socket address. */
54     public int ipv6ConnectCount = 0;
55 
ConnectStats(int netId, long transports, TokenBucket tb, int maxLatencyRecords)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 
addEvent(int errno, int latencyMs, String ipAddr)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 
countConnect(int errno, String ipAddr)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 
countLatency(int errno, int ms)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         }
94         if (latencies.size() >= mMaxLatencyRecords) {
95             // Hard limit the total number of latency measurements.
96             return;
97         }
98         latencies.add(ms);
99     }
100 
countError(int errno)101     private void countError(int errno) {
102         final int newcount = errnos.get(errno, 0) + 1;
103         errnos.put(errno, newcount);
104     }
105 
isSuccess(int errno)106     private static boolean isSuccess(int errno) {
107         return (errno == 0) || isNonBlocking(errno);
108     }
109 
isNonBlocking(int errno)110     static boolean isNonBlocking(int errno) {
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 
isIPv6(String ipAddr)116     private static boolean isIPv6(String ipAddr) {
117         return ipAddr.contains(":");
118     }
119 
120     @Override
toString()121     public String toString() {
122         StringBuilder builder =
123                 new StringBuilder("ConnectStats(").append("netId=").append(netId).append(", ");
124         for (int t : BitUtils.unpackBits(transports)) {
125             builder.append(NetworkCapabilities.transportNameOf(t)).append(", ");
126         }
127         builder.append(String.format("%d events, ", eventCount));
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);
134             builder.append(String.format(", %s: %d", errno, count));
135         }
136         return builder.append(")").toString();
137     }
138 }
139