Hugo Benichi | eab511b | 2016-09-09 09:23:47 +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 | |
| 19 | import android.content.Context; |
| 20 | import android.net.ConnectivityMetricsEvent; |
| 21 | import android.net.IIpConnectivityMetrics; |
Michal Karpinski | dd9bb4f | 2016-10-12 14:59:26 +0100 | [diff] [blame] | 22 | import android.net.INetdEventCallback; |
Hugo Benichi | e1c173d | 2016-10-18 10:36:33 +0900 | [diff] [blame] | 23 | import android.net.metrics.ApfProgramEvent; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 24 | import android.net.metrics.IpConnectivityLog; |
Michal Karpinski | dd9bb4f | 2016-10-12 14:59:26 +0100 | [diff] [blame] | 25 | import android.os.Binder; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 26 | import android.os.IBinder; |
| 27 | import android.os.Parcelable; |
Michal Karpinski | dd9bb4f | 2016-10-12 14:59:26 +0100 | [diff] [blame] | 28 | import android.os.Process; |
Hugo Benichi | 05686db | 2016-10-19 11:17:28 +0900 | [diff] [blame] | 29 | import android.provider.Settings; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 30 | import android.text.TextUtils; |
Hugo Benichi | e1c173d | 2016-10-18 10:36:33 +0900 | [diff] [blame] | 31 | import android.text.format.DateUtils; |
| 32 | import android.util.ArrayMap; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 33 | import android.util.Base64; |
| 34 | import android.util.Log; |
| 35 | import com.android.internal.annotations.GuardedBy; |
| 36 | import com.android.internal.annotations.VisibleForTesting; |
Hugo Benichi | 1198ba1 | 2017-09-15 14:18:57 +0900 | [diff] [blame] | 37 | import com.android.internal.util.RingBuffer; |
Hugo Benichi | e1c173d | 2016-10-18 10:36:33 +0900 | [diff] [blame] | 38 | import com.android.internal.util.TokenBucket; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 39 | import com.android.server.SystemService; |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 40 | import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass.IpConnectivityEvent; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 41 | import java.io.FileDescriptor; |
| 42 | import java.io.IOException; |
| 43 | import java.io.PrintWriter; |
| 44 | import java.util.ArrayList; |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 45 | import java.util.List; |
Hugo Benichi | 05686db | 2016-10-19 11:17:28 +0900 | [diff] [blame] | 46 | import java.util.function.ToIntFunction; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 47 | |
Hugo Benichi | 67c5e03 | 2017-09-14 16:31:38 +0900 | [diff] [blame] | 48 | /** |
| 49 | * Event buffering service for core networking and connectivity metrics. |
| 50 | * |
| 51 | * {@hide} |
| 52 | */ |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 53 | final public class IpConnectivityMetrics extends SystemService { |
| 54 | private static final String TAG = IpConnectivityMetrics.class.getSimpleName(); |
| 55 | private static final boolean DBG = false; |
| 56 | |
Hugo Benichi | d680d4c | 2016-10-13 13:16:16 +0900 | [diff] [blame] | 57 | // The logical version numbers of ipconnectivity.proto, corresponding to the |
| 58 | // "version" field of IpConnectivityLog. |
| 59 | private static final int NYC = 0; |
| 60 | private static final int NYC_MR1 = 1; |
| 61 | private static final int NYC_MR2 = 2; |
| 62 | public static final int VERSION = NYC_MR2; |
| 63 | |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 64 | private static final String SERVICE_NAME = IpConnectivityLog.SERVICE_NAME; |
| 65 | |
Hugo Benichi | 1198ba1 | 2017-09-15 14:18:57 +0900 | [diff] [blame] | 66 | // Default size of the event rolling log for bug report dumps. |
| 67 | private static final int DEFAULT_LOG_SIZE = 500; |
| 68 | // Default size of the event buffer for metrics reporting. |
| 69 | // Once the buffer is full, incoming events are dropped. |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 70 | private static final int DEFAULT_BUFFER_SIZE = 2000; |
Hugo Benichi | 05686db | 2016-10-19 11:17:28 +0900 | [diff] [blame] | 71 | // Maximum size of the event buffer. |
| 72 | private static final int MAXIMUM_BUFFER_SIZE = DEFAULT_BUFFER_SIZE * 10; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 73 | |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 74 | private static final int MAXIMUM_CONNECT_LATENCY_RECORDS = 20000; |
| 75 | |
Hugo Benichi | e1c173d | 2016-10-18 10:36:33 +0900 | [diff] [blame] | 76 | private static final int ERROR_RATE_LIMITED = -1; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 77 | |
Hugo Benichi | 1198ba1 | 2017-09-15 14:18:57 +0900 | [diff] [blame] | 78 | // Lock ensuring that concurrent manipulations of the event buffers are correct. |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 79 | // There are three concurrent operations to synchronize: |
| 80 | // - appending events to the buffer. |
| 81 | // - iterating throught the buffer. |
| 82 | // - flushing the buffer content and replacing it by a new buffer. |
| 83 | private final Object mLock = new Object(); |
| 84 | |
Hugo Benichi | 1198ba1 | 2017-09-15 14:18:57 +0900 | [diff] [blame] | 85 | // Implementation instance of IIpConnectivityMetrics.aidl. |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 86 | @VisibleForTesting |
| 87 | public final Impl impl = new Impl(); |
Hugo Benichi | 1198ba1 | 2017-09-15 14:18:57 +0900 | [diff] [blame] | 88 | // Subservice listening to Netd events via INetdEventListener.aidl. |
Hugo Benichi | 2a5cfb9 | 2017-03-22 22:21:44 +0900 | [diff] [blame] | 89 | @VisibleForTesting |
| 90 | NetdEventListenerService mNetdListener; |
Hugo Benichi | 00a42d4 | 2016-09-13 15:55:09 +0900 | [diff] [blame] | 91 | |
Hugo Benichi | 1198ba1 | 2017-09-15 14:18:57 +0900 | [diff] [blame] | 92 | // Rolling log of the most recent events. This log is used for dumping |
| 93 | // connectivity events in bug reports. |
| 94 | @GuardedBy("mLock") |
| 95 | private final RingBuffer<ConnectivityMetricsEvent> mEventLog = |
| 96 | new RingBuffer(ConnectivityMetricsEvent.class, DEFAULT_LOG_SIZE); |
| 97 | // Buffer of connectivity events used for metrics reporting. This buffer |
| 98 | // does not rotate automatically and instead saturates when it becomes full. |
| 99 | // It is flushed at metrics reporting. |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 100 | @GuardedBy("mLock") |
| 101 | private ArrayList<ConnectivityMetricsEvent> mBuffer; |
Hugo Benichi | 1198ba1 | 2017-09-15 14:18:57 +0900 | [diff] [blame] | 102 | // Total number of events dropped from mBuffer since last metrics reporting. |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 103 | @GuardedBy("mLock") |
| 104 | private int mDropped; |
Hugo Benichi | 1198ba1 | 2017-09-15 14:18:57 +0900 | [diff] [blame] | 105 | // Capacity of mBuffer |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 106 | @GuardedBy("mLock") |
| 107 | private int mCapacity; |
Hugo Benichi | 1198ba1 | 2017-09-15 14:18:57 +0900 | [diff] [blame] | 108 | // A list of rate limiting counters keyed by connectivity event types for |
| 109 | // metrics reporting mBuffer. |
Hugo Benichi | e1c173d | 2016-10-18 10:36:33 +0900 | [diff] [blame] | 110 | @GuardedBy("mLock") |
| 111 | private final ArrayMap<Class<?>, TokenBucket> mBuckets = makeRateLimitingBuckets(); |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 112 | |
Hugo Benichi | 05686db | 2016-10-19 11:17:28 +0900 | [diff] [blame] | 113 | private final ToIntFunction<Context> mCapacityGetter; |
| 114 | |
| 115 | public IpConnectivityMetrics(Context ctx, ToIntFunction<Context> capacityGetter) { |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 116 | super(ctx); |
Hugo Benichi | 05686db | 2016-10-19 11:17:28 +0900 | [diff] [blame] | 117 | mCapacityGetter = capacityGetter; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 118 | initBuffer(); |
| 119 | } |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 120 | |
| 121 | public IpConnectivityMetrics(Context ctx) { |
Hugo Benichi | 05686db | 2016-10-19 11:17:28 +0900 | [diff] [blame] | 122 | this(ctx, READ_BUFFER_SIZE); |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void onStart() { |
| 127 | if (DBG) Log.d(TAG, "onStart"); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void onBootPhase(int phase) { |
| 132 | if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) { |
| 133 | if (DBG) Log.d(TAG, "onBootPhase"); |
Hugo Benichi | e69608f | 2016-09-23 14:48:01 +0900 | [diff] [blame] | 134 | mNetdListener = new NetdEventListenerService(getContext()); |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 135 | |
| 136 | publishBinderService(SERVICE_NAME, impl); |
Hugo Benichi | e69608f | 2016-09-23 14:48:01 +0900 | [diff] [blame] | 137 | publishBinderService(mNetdListener.SERVICE_NAME, mNetdListener); |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | @VisibleForTesting |
| 142 | public int bufferCapacity() { |
Hugo Benichi | 05686db | 2016-10-19 11:17:28 +0900 | [diff] [blame] | 143 | return mCapacityGetter.applyAsInt(getContext()); |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | private void initBuffer() { |
| 147 | synchronized (mLock) { |
| 148 | mDropped = 0; |
| 149 | mCapacity = bufferCapacity(); |
| 150 | mBuffer = new ArrayList<>(mCapacity); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | private int append(ConnectivityMetricsEvent event) { |
| 155 | if (DBG) Log.d(TAG, "logEvent: " + event); |
| 156 | synchronized (mLock) { |
Hugo Benichi | 1198ba1 | 2017-09-15 14:18:57 +0900 | [diff] [blame] | 157 | mEventLog.append(event); |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 158 | final int left = mCapacity - mBuffer.size(); |
| 159 | if (event == null) { |
| 160 | return left; |
| 161 | } |
Hugo Benichi | e1c173d | 2016-10-18 10:36:33 +0900 | [diff] [blame] | 162 | if (isRateLimited(event)) { |
| 163 | // Do not count as a dropped event. TODO: consider adding separate counter |
| 164 | return ERROR_RATE_LIMITED; |
| 165 | } |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 166 | if (left == 0) { |
| 167 | mDropped++; |
| 168 | return 0; |
| 169 | } |
| 170 | mBuffer.add(event); |
| 171 | return left - 1; |
| 172 | } |
| 173 | } |
| 174 | |
Hugo Benichi | e1c173d | 2016-10-18 10:36:33 +0900 | [diff] [blame] | 175 | private boolean isRateLimited(ConnectivityMetricsEvent event) { |
| 176 | TokenBucket tb = mBuckets.get(event.data.getClass()); |
| 177 | return (tb != null) && !tb.get(); |
| 178 | } |
| 179 | |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 180 | private String flushEncodedOutput() { |
| 181 | final ArrayList<ConnectivityMetricsEvent> events; |
| 182 | final int dropped; |
| 183 | synchronized (mLock) { |
| 184 | events = mBuffer; |
| 185 | dropped = mDropped; |
| 186 | initBuffer(); |
| 187 | } |
| 188 | |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 189 | final List<IpConnectivityEvent> protoEvents = IpConnectivityEventBuilder.toProto(events); |
| 190 | |
| 191 | if (mNetdListener != null) { |
| 192 | mNetdListener.flushStatistics(protoEvents); |
| 193 | } |
| 194 | |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 195 | final byte[] data; |
| 196 | try { |
Hugo Benichi | 0d4a398 | 2016-11-25 11:24:22 +0900 | [diff] [blame] | 197 | data = IpConnectivityEventBuilder.serialize(dropped, protoEvents); |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 198 | } catch (IOException e) { |
| 199 | Log.e(TAG, "could not serialize events", e); |
| 200 | return ""; |
| 201 | } |
| 202 | |
| 203 | return Base64.encodeToString(data, Base64.DEFAULT); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Clears the event buffer and prints its content as a protobuf serialized byte array |
| 208 | * inside a base64 encoded string. |
| 209 | */ |
| 210 | private void cmdFlush(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 211 | pw.print(flushEncodedOutput()); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Prints the content of the event buffer, either using the events ASCII representation |
| 216 | * or using protobuf text format. |
| 217 | */ |
| 218 | private void cmdList(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 219 | final ArrayList<ConnectivityMetricsEvent> events; |
| 220 | synchronized (mLock) { |
| 221 | events = new ArrayList(mBuffer); |
| 222 | } |
| 223 | |
| 224 | if (args.length > 1 && args[1].equals("proto")) { |
| 225 | for (IpConnectivityEvent ev : IpConnectivityEventBuilder.toProto(events)) { |
| 226 | pw.print(ev.toString()); |
| 227 | } |
Hugo Benichi | a2decca | 2017-02-22 14:32:27 +0900 | [diff] [blame] | 228 | if (mNetdListener != null) { |
| 229 | mNetdListener.listAsProtos(pw); |
| 230 | } |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 231 | return; |
| 232 | } |
| 233 | |
| 234 | for (ConnectivityMetricsEvent ev : events) { |
| 235 | pw.println(ev.toString()); |
| 236 | } |
Hugo Benichi | a2decca | 2017-02-22 14:32:27 +0900 | [diff] [blame] | 237 | if (mNetdListener != null) { |
| 238 | mNetdListener.list(pw); |
| 239 | } |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 240 | } |
| 241 | |
Hugo Benichi | 1198ba1 | 2017-09-15 14:18:57 +0900 | [diff] [blame] | 242 | /** |
| 243 | * Prints for bug reports the content of the rolling event log and the |
| 244 | * content of Netd event listener. |
| 245 | */ |
| 246 | private void cmdDumpsys(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 247 | final ConnectivityMetricsEvent[] events; |
| 248 | synchronized (mLock) { |
| 249 | events = mEventLog.toArray(); |
| 250 | } |
| 251 | for (ConnectivityMetricsEvent ev : events) { |
| 252 | pw.println(ev.toString()); |
| 253 | } |
| 254 | if (mNetdListener != null) { |
| 255 | mNetdListener.list(pw); |
| 256 | } |
| 257 | } |
| 258 | |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 259 | private void cmdStats(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 260 | synchronized (mLock) { |
| 261 | pw.println("Buffered events: " + mBuffer.size()); |
| 262 | pw.println("Buffer capacity: " + mCapacity); |
| 263 | pw.println("Dropped events: " + mDropped); |
| 264 | } |
Hugo Benichi | e69608f | 2016-09-23 14:48:01 +0900 | [diff] [blame] | 265 | if (mNetdListener != null) { |
| 266 | mNetdListener.dump(pw); |
Hugo Benichi | 00a42d4 | 2016-09-13 15:55:09 +0900 | [diff] [blame] | 267 | } |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | private void cmdDefault(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 271 | if (args.length == 0) { |
| 272 | pw.println("No command"); |
| 273 | return; |
| 274 | } |
| 275 | pw.println("Unknown command " + TextUtils.join(" ", args)); |
| 276 | } |
| 277 | |
| 278 | public final class Impl extends IIpConnectivityMetrics.Stub { |
| 279 | static final String CMD_FLUSH = "flush"; |
| 280 | static final String CMD_LIST = "list"; |
| 281 | static final String CMD_STATS = "stats"; |
Hugo Benichi | 51d14cb | 2016-10-19 13:48:40 +0900 | [diff] [blame] | 282 | static final String CMD_DUMPSYS = "-a"; // dumpsys.cpp dumps services with "-a" as arguments |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 283 | static final String CMD_DEFAULT = CMD_STATS; |
| 284 | |
| 285 | @Override |
| 286 | public int logEvent(ConnectivityMetricsEvent event) { |
| 287 | enforceConnectivityInternalPermission(); |
| 288 | return append(event); |
| 289 | } |
| 290 | |
| 291 | @Override |
| 292 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 293 | enforceDumpPermission(); |
| 294 | if (DBG) Log.d(TAG, "dumpsys " + TextUtils.join(" ", args)); |
| 295 | final String cmd = (args.length > 0) ? args[0] : CMD_DEFAULT; |
| 296 | switch (cmd) { |
| 297 | case CMD_FLUSH: |
| 298 | cmdFlush(fd, pw, args); |
| 299 | return; |
Hugo Benichi | 51d14cb | 2016-10-19 13:48:40 +0900 | [diff] [blame] | 300 | case CMD_DUMPSYS: |
Hugo Benichi | 1198ba1 | 2017-09-15 14:18:57 +0900 | [diff] [blame] | 301 | cmdDumpsys(fd, pw, args); |
| 302 | return; |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 303 | case CMD_LIST: |
| 304 | cmdList(fd, pw, args); |
| 305 | return; |
| 306 | case CMD_STATS: |
| 307 | cmdStats(fd, pw, args); |
| 308 | return; |
| 309 | default: |
| 310 | cmdDefault(fd, pw, args); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | private void enforceConnectivityInternalPermission() { |
| 315 | enforcePermission(android.Manifest.permission.CONNECTIVITY_INTERNAL); |
| 316 | } |
| 317 | |
| 318 | private void enforceDumpPermission() { |
| 319 | enforcePermission(android.Manifest.permission.DUMP); |
| 320 | } |
| 321 | |
| 322 | private void enforcePermission(String what) { |
| 323 | getContext().enforceCallingOrSelfPermission(what, "IpConnectivityMetrics"); |
| 324 | } |
Michal Karpinski | dd9bb4f | 2016-10-12 14:59:26 +0100 | [diff] [blame] | 325 | |
| 326 | private void enforceNetdEventListeningPermission() { |
| 327 | final int uid = Binder.getCallingUid(); |
| 328 | if (uid != Process.SYSTEM_UID) { |
| 329 | throw new SecurityException(String.format("Uid %d has no permission to listen for" |
| 330 | + " netd events.", uid)); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | @Override |
| 335 | public boolean registerNetdEventCallback(INetdEventCallback callback) { |
| 336 | enforceNetdEventListeningPermission(); |
| 337 | if (mNetdListener == null) { |
| 338 | return false; |
| 339 | } |
| 340 | return mNetdListener.registerNetdEventCallback(callback); |
| 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public boolean unregisterNetdEventCallback() { |
| 345 | enforceNetdEventListeningPermission(); |
| 346 | if (mNetdListener == null) { |
| 347 | // if the service is null, we aren't registered anyway |
| 348 | return true; |
| 349 | } |
| 350 | return mNetdListener.unregisterNetdEventCallback(); |
| 351 | } |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 352 | }; |
Hugo Benichi | 05686db | 2016-10-19 11:17:28 +0900 | [diff] [blame] | 353 | |
| 354 | private static final ToIntFunction<Context> READ_BUFFER_SIZE = (ctx) -> { |
| 355 | int size = Settings.Global.getInt(ctx.getContentResolver(), |
| 356 | Settings.Global.CONNECTIVITY_METRICS_BUFFER_SIZE, DEFAULT_BUFFER_SIZE); |
| 357 | if (size <= 0) { |
| 358 | return DEFAULT_BUFFER_SIZE; |
| 359 | } |
| 360 | return Math.min(size, MAXIMUM_BUFFER_SIZE); |
| 361 | }; |
Hugo Benichi | e1c173d | 2016-10-18 10:36:33 +0900 | [diff] [blame] | 362 | |
| 363 | private static ArrayMap<Class<?>, TokenBucket> makeRateLimitingBuckets() { |
| 364 | ArrayMap<Class<?>, TokenBucket> map = new ArrayMap<>(); |
| 365 | // one token every minute, 50 tokens max: burst of ~50 events every hour. |
| 366 | map.put(ApfProgramEvent.class, new TokenBucket((int)DateUtils.MINUTE_IN_MILLIS, 50)); |
| 367 | return map; |
| 368 | } |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 369 | } |