Lorenzo Colitti | 4372473 | 2016-04-12 23:29:19 +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 com.android.server.connectivity; |
| 18 | |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 19 | import static android.util.TimeUtils.NANOS_PER_MS; |
| 20 | |
Lorenzo Colitti | 4372473 | 2016-04-12 23:29:19 +0900 | [diff] [blame] | 21 | import android.content.Context; |
Lorenzo Colitti | 4372473 | 2016-04-12 23:29:19 +0900 | [diff] [blame] | 22 | import android.net.ConnectivityManager; |
Michal Karpinski | dd9bb4f | 2016-10-12 14:59:26 +0100 | [diff] [blame] | 23 | import android.net.INetdEventCallback; |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 24 | import android.net.Network; |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 25 | import android.net.NetworkCapabilities; |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 26 | import android.net.metrics.ConnectStats; |
Hugo Benichi | 5e05518 | 2016-06-01 08:50:38 +0900 | [diff] [blame] | 27 | import android.net.metrics.DnsEvent; |
Michal Karpinski | d11d18c | 2016-09-15 17:07:08 +0900 | [diff] [blame] | 28 | import android.net.metrics.INetdEventListener; |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 29 | import android.net.metrics.IpConnectivityLog; |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 30 | import android.net.metrics.WakeupEvent; |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 31 | import android.net.metrics.WakeupStats; |
Michal Karpinski | dd9bb4f | 2016-10-12 14:59:26 +0100 | [diff] [blame] | 32 | import android.os.RemoteException; |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 33 | import android.text.format.DateUtils; |
Lorenzo Colitti | 4372473 | 2016-04-12 23:29:19 +0900 | [diff] [blame] | 34 | import android.util.Log; |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 35 | import android.util.ArrayMap; |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 36 | import android.util.SparseArray; |
Lorenzo Colitti | 4372473 | 2016-04-12 23:29:19 +0900 | [diff] [blame] | 37 | import com.android.internal.annotations.GuardedBy; |
Hugo Benichi | 5e05518 | 2016-06-01 08:50:38 +0900 | [diff] [blame] | 38 | import com.android.internal.annotations.VisibleForTesting; |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 39 | import com.android.internal.util.BitUtils; |
Lorenzo Colitti | 4372473 | 2016-04-12 23:29:19 +0900 | [diff] [blame] | 40 | import com.android.internal.util.IndentingPrintWriter; |
Hugo Benichi | 67c5e03 | 2017-09-14 16:31:38 +0900 | [diff] [blame] | 41 | import com.android.internal.util.RingBuffer; |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 42 | import com.android.internal.util.TokenBucket; |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 43 | import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.IpConnectivityEvent; |
Lorenzo Colitti | 4372473 | 2016-04-12 23:29:19 +0900 | [diff] [blame] | 44 | import java.io.PrintWriter; |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 45 | import java.util.List; |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 46 | import java.util.function.Function; |
| 47 | import java.util.function.IntFunction; |
Lorenzo Colitti | 4372473 | 2016-04-12 23:29:19 +0900 | [diff] [blame] | 48 | |
Lorenzo Colitti | 4372473 | 2016-04-12 23:29:19 +0900 | [diff] [blame] | 49 | /** |
Michal Karpinski | d11d18c | 2016-09-15 17:07:08 +0900 | [diff] [blame] | 50 | * Implementation of the INetdEventListener interface. |
Lorenzo Colitti | 4372473 | 2016-04-12 23:29:19 +0900 | [diff] [blame] | 51 | */ |
Michal Karpinski | d11d18c | 2016-09-15 17:07:08 +0900 | [diff] [blame] | 52 | public class NetdEventListenerService extends INetdEventListener.Stub { |
Lorenzo Colitti | 4372473 | 2016-04-12 23:29:19 +0900 | [diff] [blame] | 53 | |
Michal Karpinski | d11d18c | 2016-09-15 17:07:08 +0900 | [diff] [blame] | 54 | public static final String SERVICE_NAME = "netd_listener"; |
Lorenzo Colitti | 4372473 | 2016-04-12 23:29:19 +0900 | [diff] [blame] | 55 | |
Michal Karpinski | d11d18c | 2016-09-15 17:07:08 +0900 | [diff] [blame] | 56 | private static final String TAG = NetdEventListenerService.class.getSimpleName(); |
Hugo Benichi | 8b06bcd | 2016-10-31 15:04:37 +0900 | [diff] [blame] | 57 | private static final boolean DBG = false; |
Lorenzo Colitti | 4372473 | 2016-04-12 23:29:19 +0900 | [diff] [blame] | 58 | private static final boolean VDBG = false; |
| 59 | |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 60 | private static final int INITIAL_DNS_BATCH_SIZE = 100; |
Lorenzo Colitti | 4372473 | 2016-04-12 23:29:19 +0900 | [diff] [blame] | 61 | |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 62 | // Rate limit connect latency logging to 1 measurement per 15 seconds (5760 / day) with maximum |
| 63 | // bursts of 5000 measurements. |
| 64 | private static final int CONNECT_LATENCY_BURST_LIMIT = 5000; |
| 65 | private static final int CONNECT_LATENCY_FILL_RATE = 15 * (int) DateUtils.SECOND_IN_MILLIS; |
| 66 | private static final int CONNECT_LATENCY_MAXIMUM_RECORDS = 20000; |
| 67 | |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 68 | @VisibleForTesting |
| 69 | static final int WAKEUP_EVENT_BUFFER_LENGTH = 1024; |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 70 | // TODO: dedup this String constant with the one used in |
| 71 | // ConnectivityService#wakeupModifyInterface(). |
| 72 | @VisibleForTesting |
| 73 | static final String WAKEUP_EVENT_IFACE_PREFIX = "iface:"; |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 74 | |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 75 | // Sparse arrays of DNS and connect events, grouped by net id. |
Lorenzo Colitti | 4372473 | 2016-04-12 23:29:19 +0900 | [diff] [blame] | 76 | @GuardedBy("this") |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 77 | private final SparseArray<DnsEvent> mDnsEvents = new SparseArray<>(); |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 78 | @GuardedBy("this") |
| 79 | private final SparseArray<ConnectStats> mConnectEvents = new SparseArray<>(); |
Lorenzo Colitti | 4372473 | 2016-04-12 23:29:19 +0900 | [diff] [blame] | 80 | |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 81 | // Array of aggregated wakeup event stats, grouped by interface name. |
| 82 | @GuardedBy("this") |
| 83 | private final ArrayMap<String, WakeupStats> mWakeupStats = new ArrayMap<>(); |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 84 | // Ring buffer array for storing packet wake up events sent by Netd. |
| 85 | @GuardedBy("this") |
Hugo Benichi | 67c5e03 | 2017-09-14 16:31:38 +0900 | [diff] [blame] | 86 | private final RingBuffer<WakeupEvent> mWakeupEvents = |
| 87 | new RingBuffer(WakeupEvent.class, WAKEUP_EVENT_BUFFER_LENGTH); |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 88 | |
Lorenzo Colitti | 4372473 | 2016-04-12 23:29:19 +0900 | [diff] [blame] | 89 | private final ConnectivityManager mCm; |
Lorenzo Colitti | 4372473 | 2016-04-12 23:29:19 +0900 | [diff] [blame] | 90 | |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 91 | @GuardedBy("this") |
| 92 | private final TokenBucket mConnectTb = |
| 93 | new TokenBucket(CONNECT_LATENCY_FILL_RATE, CONNECT_LATENCY_BURST_LIMIT); |
Michal Karpinski | dd9bb4f | 2016-10-12 14:59:26 +0100 | [diff] [blame] | 94 | // Callback should only be registered/unregistered when logging is being enabled/disabled in DPM |
| 95 | // by the device owner. It's DevicePolicyManager's responsibility to ensure that. |
| 96 | @GuardedBy("this") |
| 97 | private INetdEventCallback mNetdEventCallback; |
| 98 | |
| 99 | public synchronized boolean registerNetdEventCallback(INetdEventCallback callback) { |
| 100 | mNetdEventCallback = callback; |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | public synchronized boolean unregisterNetdEventCallback() { |
| 105 | mNetdEventCallback = null; |
| 106 | return true; |
| 107 | } |
| 108 | |
Michal Karpinski | d11d18c | 2016-09-15 17:07:08 +0900 | [diff] [blame] | 109 | public NetdEventListenerService(Context context) { |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 110 | this(context.getSystemService(ConnectivityManager.class)); |
Hugo Benichi | 5e05518 | 2016-06-01 08:50:38 +0900 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @VisibleForTesting |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 114 | public NetdEventListenerService(ConnectivityManager cm) { |
Lorenzo Colitti | 4372473 | 2016-04-12 23:29:19 +0900 | [diff] [blame] | 115 | // We are started when boot is complete, so ConnectivityService should already be running. |
Hugo Benichi | 5e05518 | 2016-06-01 08:50:38 +0900 | [diff] [blame] | 116 | mCm = cm; |
Lorenzo Colitti | 4372473 | 2016-04-12 23:29:19 +0900 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
| 120 | // Called concurrently by multiple binder threads. |
Michal Karpinski | 14c9d2d | 2016-09-27 17:13:57 +0100 | [diff] [blame] | 121 | // This method must not block or perform long-running operations. |
| 122 | public synchronized void onDnsEvent(int netId, int eventType, int returnCode, int latencyMs, |
Michal Karpinski | dd9bb4f | 2016-10-12 14:59:26 +0100 | [diff] [blame] | 123 | String hostname, String[] ipAddresses, int ipAddressesCount, int uid) |
| 124 | throws RemoteException { |
Hugo Benichi | 8b06bcd | 2016-10-31 15:04:37 +0900 | [diff] [blame] | 125 | maybeVerboseLog("onDnsEvent(%d, %d, %d, %dms)", netId, eventType, returnCode, latencyMs); |
Lorenzo Colitti | 4372473 | 2016-04-12 23:29:19 +0900 | [diff] [blame] | 126 | |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 127 | DnsEvent dnsEvent = mDnsEvents.get(netId); |
| 128 | if (dnsEvent == null) { |
| 129 | dnsEvent = makeDnsEvent(netId); |
| 130 | mDnsEvents.put(netId, dnsEvent); |
Lorenzo Colitti | 4372473 | 2016-04-12 23:29:19 +0900 | [diff] [blame] | 131 | } |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 132 | dnsEvent.addResult((byte) eventType, (byte) returnCode, latencyMs); |
Michal Karpinski | dd9bb4f | 2016-10-12 14:59:26 +0100 | [diff] [blame] | 133 | |
| 134 | if (mNetdEventCallback != null) { |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 135 | long timestamp = System.currentTimeMillis(); |
| 136 | mNetdEventCallback.onDnsEvent(hostname, ipAddresses, ipAddressesCount, timestamp, uid); |
Michal Karpinski | dd9bb4f | 2016-10-12 14:59:26 +0100 | [diff] [blame] | 137 | } |
Lorenzo Colitti | 4372473 | 2016-04-12 23:29:19 +0900 | [diff] [blame] | 138 | } |
| 139 | |
Michal Karpinski | 965894e | 2016-09-28 16:06:16 +0100 | [diff] [blame] | 140 | @Override |
| 141 | // Called concurrently by multiple binder threads. |
| 142 | // This method must not block or perform long-running operations. |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 143 | public synchronized void onConnectEvent(int netId, int error, int latencyMs, String ipAddr, |
| 144 | int port, int uid) throws RemoteException { |
Hugo Benichi | 8b06bcd | 2016-10-31 15:04:37 +0900 | [diff] [blame] | 145 | maybeVerboseLog("onConnectEvent(%d, %d, %dms)", netId, error, latencyMs); |
Michal Karpinski | dd9bb4f | 2016-10-12 14:59:26 +0100 | [diff] [blame] | 146 | |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 147 | ConnectStats connectStats = mConnectEvents.get(netId); |
| 148 | if (connectStats == null) { |
| 149 | connectStats = makeConnectStats(netId); |
| 150 | mConnectEvents.put(netId, connectStats); |
| 151 | } |
| 152 | connectStats.addEvent(error, latencyMs, ipAddr); |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 153 | |
Michal Karpinski | dd9bb4f | 2016-10-12 14:59:26 +0100 | [diff] [blame] | 154 | if (mNetdEventCallback != null) { |
| 155 | mNetdEventCallback.onConnectEvent(ipAddr, port, System.currentTimeMillis(), uid); |
| 156 | } |
Michal Karpinski | 965894e | 2016-09-28 16:06:16 +0100 | [diff] [blame] | 157 | } |
| 158 | |
Joel Scherpelz | b369bf5 | 2017-05-22 13:47:41 +0900 | [diff] [blame] | 159 | @Override |
| 160 | public synchronized void onWakeupEvent(String prefix, int uid, int gid, long timestampNs) { |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 161 | maybeVerboseLog("onWakeupEvent(%s, %d, %d, %sns)", prefix, uid, gid, timestampNs); |
| 162 | |
| 163 | // TODO: add ip protocol and port |
| 164 | |
| 165 | String iface = prefix.replaceFirst(WAKEUP_EVENT_IFACE_PREFIX, ""); |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 166 | final long timestampMs; |
| 167 | if (timestampNs > 0) { |
| 168 | timestampMs = timestampNs / NANOS_PER_MS; |
| 169 | } else { |
| 170 | timestampMs = System.currentTimeMillis(); |
| 171 | } |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 172 | |
Hugo Benichi | 175b574 | 2017-09-19 13:15:26 +0900 | [diff] [blame] | 173 | addWakeupEvent(iface, timestampMs, uid); |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @GuardedBy("this") |
Hugo Benichi | 175b574 | 2017-09-19 13:15:26 +0900 | [diff] [blame] | 177 | private void addWakeupEvent(String iface, long timestampMs, int uid) { |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 178 | WakeupEvent event = new WakeupEvent(); |
| 179 | event.iface = iface; |
| 180 | event.timestampMs = timestampMs; |
| 181 | event.uid = uid; |
Hugo Benichi | 67c5e03 | 2017-09-14 16:31:38 +0900 | [diff] [blame] | 182 | mWakeupEvents.append(event); |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 183 | WakeupStats stats = mWakeupStats.get(iface); |
| 184 | if (stats == null) { |
| 185 | stats = new WakeupStats(iface); |
| 186 | mWakeupStats.put(iface, stats); |
| 187 | } |
| 188 | stats.countEvent(event); |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 189 | } |
| 190 | |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 191 | public synchronized void flushStatistics(List<IpConnectivityEvent> events) { |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 192 | flushProtos(events, mConnectEvents, IpConnectivityEventBuilder::toProto); |
| 193 | flushProtos(events, mDnsEvents, IpConnectivityEventBuilder::toProto); |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 194 | for (int i = 0; i < mWakeupStats.size(); i++) { |
| 195 | events.add(IpConnectivityEventBuilder.toProto(mWakeupStats.valueAt(i))); |
| 196 | } |
| 197 | mWakeupStats.clear(); |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 198 | } |
| 199 | |
Lorenzo Colitti | 4372473 | 2016-04-12 23:29:19 +0900 | [diff] [blame] | 200 | public synchronized void dump(PrintWriter writer) { |
| 201 | IndentingPrintWriter pw = new IndentingPrintWriter(writer, " "); |
| 202 | pw.println(TAG + ":"); |
| 203 | pw.increaseIndent(); |
Hugo Benichi | a2decca | 2017-02-22 14:32:27 +0900 | [diff] [blame] | 204 | list(pw); |
| 205 | pw.decreaseIndent(); |
| 206 | } |
| 207 | |
| 208 | public synchronized void list(PrintWriter pw) { |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 209 | listEvents(pw, mConnectEvents, (x) -> x, "\n"); |
| 210 | listEvents(pw, mDnsEvents, (x) -> x, "\n"); |
| 211 | for (int i = 0; i < mWakeupStats.size(); i++) { |
| 212 | pw.println(mWakeupStats.valueAt(i)); |
| 213 | } |
Hugo Benichi | 67c5e03 | 2017-09-14 16:31:38 +0900 | [diff] [blame] | 214 | for (WakeupEvent wakeup : mWakeupEvents.toArray()) { |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 215 | pw.println(wakeup); |
| 216 | } |
Hugo Benichi | a2decca | 2017-02-22 14:32:27 +0900 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | public synchronized void listAsProtos(PrintWriter pw) { |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 220 | listEvents(pw, mConnectEvents, IpConnectivityEventBuilder::toProto, ""); |
| 221 | listEvents(pw, mDnsEvents, IpConnectivityEventBuilder::toProto, ""); |
| 222 | for (int i = 0; i < mWakeupStats.size(); i++) { |
| 223 | pw.print(IpConnectivityEventBuilder.toProto(mWakeupStats.valueAt(i))); |
| 224 | } |
Lorenzo Colitti | 4372473 | 2016-04-12 23:29:19 +0900 | [diff] [blame] | 225 | } |
| 226 | |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 227 | private static <T> void flushProtos(List<IpConnectivityEvent> out, SparseArray<T> in, |
| 228 | Function<T, IpConnectivityEvent> mapper) { |
| 229 | for (int i = 0; i < in.size(); i++) { |
| 230 | out.add(mapper.apply(in.valueAt(i))); |
| 231 | } |
| 232 | in.clear(); |
| 233 | } |
| 234 | |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 235 | private static <T> void listEvents( |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 236 | PrintWriter pw, SparseArray<T> events, Function<T, Object> mapper, String separator) { |
| 237 | // Proto derived Classes have toString method that adds a \n at the end. |
| 238 | // Let the caller control that by passing in the line separator explicitly. |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 239 | for (int i = 0; i < events.size(); i++) { |
Hugo Benichi | 60c9f63 | 2017-09-05 13:34:48 +0900 | [diff] [blame] | 240 | pw.print(mapper.apply(events.valueAt(i))); |
| 241 | pw.print(separator); |
Hugo Benichi | f562ac3 | 2017-09-04 13:24:43 +0900 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
Hugo Benichi | 5eb9053 | 2017-03-23 18:38:22 +0900 | [diff] [blame] | 245 | private ConnectStats makeConnectStats(int netId) { |
| 246 | long transports = getTransports(netId); |
| 247 | return new ConnectStats(netId, transports, mConnectTb, CONNECT_LATENCY_MAXIMUM_RECORDS); |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 248 | } |
| 249 | |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 250 | private DnsEvent makeDnsEvent(int netId) { |
| 251 | long transports = getTransports(netId); |
| 252 | return new DnsEvent(netId, transports, INITIAL_DNS_BATCH_SIZE); |
| 253 | } |
| 254 | |
| 255 | private long getTransports(int netId) { |
| 256 | // TODO: directly query ConnectivityService instead of going through Binder interface. |
| 257 | NetworkCapabilities nc = mCm.getNetworkCapabilities(new Network(netId)); |
| 258 | if (nc == null) { |
| 259 | return 0; |
| 260 | } |
| 261 | return BitUtils.packBits(nc.getTransportTypes()); |
| 262 | } |
| 263 | |
Hugo Benichi | 8b06bcd | 2016-10-31 15:04:37 +0900 | [diff] [blame] | 264 | private static void maybeLog(String s, Object... args) { |
| 265 | if (DBG) Log.d(TAG, String.format(s, args)); |
Lorenzo Colitti | 4372473 | 2016-04-12 23:29:19 +0900 | [diff] [blame] | 266 | } |
| 267 | |
Hugo Benichi | 8b06bcd | 2016-10-31 15:04:37 +0900 | [diff] [blame] | 268 | private static void maybeVerboseLog(String s, Object... args) { |
| 269 | if (VDBG) Log.d(TAG, String.format(s, args)); |
Lorenzo Colitti | 4372473 | 2016-04-12 23:29:19 +0900 | [diff] [blame] | 270 | } |
| 271 | } |