Home | History | Annotate | Download | only in metrics
      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 import com.android.internal.util.BitUtils;
     24 import com.android.internal.util.TokenBucket;
     25 
     26 /**
     27  * A class that aggregates connect() statistics.
     28  * {@hide}
     29  */
     30 public class ConnectStats {
     31     private final static int EALREADY     = OsConstants.EALREADY;
     32     private final static int EINPROGRESS  = OsConstants.EINPROGRESS;
     33 
     34     /** Network id of the network associated with the event, or 0 if unspecified. */
     35     public final int netId;
     36     /** Transports of the network associated with the event, as defined in NetworkCapabilities. */
     37     public final long transports;
     38     /** How many events resulted in a given errno. */
     39     public final SparseIntArray errnos = new SparseIntArray();
     40     /** Latencies of successful blocking connects. TODO: add non-blocking connects latencies. */
     41     public final IntArray latencies = new IntArray();
     42     /** TokenBucket for rate limiting latency recording. */
     43     public final TokenBucket mLatencyTb;
     44     /** Maximum number of latency values recorded. */
     45     public final int mMaxLatencyRecords;
     46     /** Total count of successful connects. */
     47     public int connectCount = 0;
     48     /** Total count of successful connects done in blocking mode. */
     49     public int connectBlockingCount = 0;
     50     /** Total count of successful connects with IPv6 socket address. */
     51     public int ipv6ConnectCount = 0;
     52 
     53     public ConnectStats(int netId, long transports, TokenBucket tb, int maxLatencyRecords) {
     54         this.netId = netId;
     55         this.transports = transports;
     56         mLatencyTb = tb;
     57         mMaxLatencyRecords = maxLatencyRecords;
     58     }
     59 
     60     public void addEvent(int errno, int latencyMs, String ipAddr) {
     61         if (isSuccess(errno)) {
     62             countConnect(errno, ipAddr);
     63             countLatency(errno, latencyMs);
     64         } else {
     65             countError(errno);
     66         }
     67     }
     68 
     69     private void countConnect(int errno, String ipAddr) {
     70         connectCount++;
     71         if (!isNonBlocking(errno)) {
     72             connectBlockingCount++;
     73         }
     74         if (isIPv6(ipAddr)) {
     75             ipv6ConnectCount++;
     76         }
     77     }
     78 
     79     private void countLatency(int errno, int ms) {
     80         if (isNonBlocking(errno)) {
     81             // Ignore connect() on non-blocking sockets
     82             return;
     83         }
     84         if (!mLatencyTb.get()) {
     85             // Rate limited
     86             return;
     87         }
     88         if (latencies.size() >= mMaxLatencyRecords) {
     89             // Hard limit the total number of latency measurements.
     90             return;
     91         }
     92         latencies.add(ms);
     93     }
     94 
     95     private void countError(int errno) {
     96         final int newcount = errnos.get(errno, 0) + 1;
     97         errnos.put(errno, newcount);
     98     }
     99 
    100     private static boolean isSuccess(int errno) {
    101         return (errno == 0) || isNonBlocking(errno);
    102     }
    103 
    104     private static boolean isNonBlocking(int errno) {
    105         // On non-blocking TCP sockets, connect() immediately returns EINPROGRESS.
    106         // On non-blocking TCP sockets that are connecting, connect() immediately returns EALREADY.
    107         return (errno == EINPROGRESS) || (errno == EALREADY);
    108     }
    109 
    110     private static boolean isIPv6(String ipAddr) {
    111         return ipAddr.contains(":");
    112     }
    113 
    114     @Override
    115     public String toString() {
    116         StringBuilder builder = new StringBuilder("ConnectStats(").append(netId).append(", ");
    117         for (int t : BitUtils.unpackBits(transports)) {
    118             builder.append(NetworkCapabilities.transportNameOf(t)).append(", ");
    119         }
    120         builder.append(String.format("%d success, ", connectCount));
    121         builder.append(String.format("%d blocking, ", connectBlockingCount));
    122         builder.append(String.format("%d IPv6 dst", ipv6ConnectCount));
    123         for (int i = 0; i < errnos.size(); i++) {
    124             String errno = OsConstants.errnoName(errnos.keyAt(i));
    125             int count = errnos.valueAt(i);
    126             builder.append(String.format(", %s: %d", errno, count));
    127         }
    128         return builder.append(")").toString();
    129     }
    130 }
    131