Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 1 | /* |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 2 | * Copyright (C) 2011 The Android Open Source Project |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 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.net; |
| 18 | |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 19 | import static android.Manifest.permission.ACCESS_NETWORK_STATE; |
Jeff Sharkey | 63d27a9 | 2011-08-03 17:04:22 -0700 | [diff] [blame] | 20 | import static android.Manifest.permission.CONNECTIVITY_INTERNAL; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 21 | import static android.Manifest.permission.READ_NETWORK_USAGE_HISTORY; |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 22 | import static android.content.Intent.ACTION_SHUTDOWN; |
| 23 | import static android.content.Intent.ACTION_UID_REMOVED; |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 24 | import static android.content.Intent.ACTION_USER_REMOVED; |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 25 | import static android.content.Intent.EXTRA_UID; |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 26 | import static android.net.ConnectivityManager.ACTION_TETHER_STATE_CHANGED; |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 27 | import static android.net.ConnectivityManager.isNetworkTypeMobile; |
Lorenzo Colitti | ada23ed | 2018-01-19 01:05:20 +0900 | [diff] [blame] | 28 | import static android.net.NetworkStats.DEFAULT_NETWORK_ALL; |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 29 | import static android.net.NetworkStats.IFACE_ALL; |
Remi NGUYEN VAN | b6a9201 | 2018-03-06 12:36:54 +0900 | [diff] [blame] | 30 | import static android.net.NetworkStats.INTERFACES_ALL; |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 31 | import static android.net.NetworkStats.METERED_ALL; |
| 32 | import static android.net.NetworkStats.ROAMING_ALL; |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 33 | import static android.net.NetworkStats.SET_ALL; |
| 34 | import static android.net.NetworkStats.SET_DEFAULT; |
| 35 | import static android.net.NetworkStats.SET_FOREGROUND; |
Lorenzo Colitti | 5356a35 | 2017-08-17 19:23:08 +0900 | [diff] [blame] | 36 | import static android.net.NetworkStats.STATS_PER_IFACE; |
| 37 | import static android.net.NetworkStats.STATS_PER_UID; |
Remi NGUYEN VAN | b6a9201 | 2018-03-06 12:36:54 +0900 | [diff] [blame] | 38 | import static android.net.NetworkStats.TAG_ALL; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 39 | import static android.net.NetworkStats.TAG_NONE; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 40 | import static android.net.NetworkStats.UID_ALL; |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 41 | import static android.net.NetworkStatsHistory.FIELD_ALL; |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 42 | import static android.net.NetworkTemplate.buildTemplateMobileWildcard; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 43 | import static android.net.NetworkTemplate.buildTemplateWifiWildcard; |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 44 | import static android.net.TrafficStats.KB_IN_BYTES; |
Jeff Sharkey | 241dde2 | 2012-02-03 14:50:07 -0800 | [diff] [blame] | 45 | import static android.net.TrafficStats.MB_IN_BYTES; |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 46 | import static android.os.Trace.TRACE_TAG_NETWORK; |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 47 | import static android.provider.Settings.Global.NETSTATS_AUGMENT_ENABLED; |
Jeff Sharkey | 023c05a | 2012-09-14 13:09:57 -0700 | [diff] [blame] | 48 | import static android.provider.Settings.Global.NETSTATS_DEV_BUCKET_DURATION; |
| 49 | import static android.provider.Settings.Global.NETSTATS_DEV_DELETE_AGE; |
| 50 | import static android.provider.Settings.Global.NETSTATS_DEV_PERSIST_BYTES; |
| 51 | import static android.provider.Settings.Global.NETSTATS_DEV_ROTATE_AGE; |
| 52 | import static android.provider.Settings.Global.NETSTATS_GLOBAL_ALERT_BYTES; |
| 53 | import static android.provider.Settings.Global.NETSTATS_POLL_INTERVAL; |
Jeff Sharkey | 023c05a | 2012-09-14 13:09:57 -0700 | [diff] [blame] | 54 | import static android.provider.Settings.Global.NETSTATS_SAMPLE_ENABLED; |
Jeff Sharkey | 023c05a | 2012-09-14 13:09:57 -0700 | [diff] [blame] | 55 | import static android.provider.Settings.Global.NETSTATS_UID_BUCKET_DURATION; |
| 56 | import static android.provider.Settings.Global.NETSTATS_UID_DELETE_AGE; |
| 57 | import static android.provider.Settings.Global.NETSTATS_UID_PERSIST_BYTES; |
| 58 | import static android.provider.Settings.Global.NETSTATS_UID_ROTATE_AGE; |
| 59 | import static android.provider.Settings.Global.NETSTATS_UID_TAG_BUCKET_DURATION; |
| 60 | import static android.provider.Settings.Global.NETSTATS_UID_TAG_DELETE_AGE; |
| 61 | import static android.provider.Settings.Global.NETSTATS_UID_TAG_PERSIST_BYTES; |
| 62 | import static android.provider.Settings.Global.NETSTATS_UID_TAG_ROTATE_AGE; |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 63 | import static android.text.format.DateUtils.DAY_IN_MILLIS; |
| 64 | import static android.text.format.DateUtils.HOUR_IN_MILLIS; |
| 65 | import static android.text.format.DateUtils.MINUTE_IN_MILLIS; |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 66 | import static android.text.format.DateUtils.SECOND_IN_MILLIS; |
Jack Yu | f9d559c | 2017-05-26 16:08:22 -0700 | [diff] [blame] | 67 | |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 68 | import static com.android.internal.util.Preconditions.checkNotNull; |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 69 | import static com.android.server.NetworkManagementService.LIMIT_GLOBAL_ALERT; |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 70 | import static com.android.server.NetworkManagementSocketTagger.resetKernelUidStats; |
| 71 | import static com.android.server.NetworkManagementSocketTagger.setKernelCounterSet; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 72 | |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 73 | import android.annotation.NonNull; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 74 | import android.app.AlarmManager; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 75 | import android.app.PendingIntent; |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 76 | import android.app.usage.NetworkStatsManager; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 77 | import android.content.BroadcastReceiver; |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 78 | import android.content.ContentResolver; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 79 | import android.content.Context; |
| 80 | import android.content.Intent; |
| 81 | import android.content.IntentFilter; |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 82 | import android.content.pm.ApplicationInfo; |
| 83 | import android.content.pm.PackageManager; |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 84 | import android.net.DataUsageRequest; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 85 | import android.net.IConnectivityManager; |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 86 | import android.net.INetworkManagementEventObserver; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 87 | import android.net.INetworkStatsService; |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 88 | import android.net.INetworkStatsSession; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 89 | import android.net.LinkProperties; |
Lorenzo Colitti | c78da29 | 2018-01-19 00:50:48 +0900 | [diff] [blame] | 90 | import android.net.Network; |
Jack Yu | b6587ea | 2016-06-22 11:35:10 -0700 | [diff] [blame] | 91 | import android.net.NetworkCapabilities; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 92 | import android.net.NetworkIdentity; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 93 | import android.net.NetworkInfo; |
| 94 | import android.net.NetworkState; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 95 | import android.net.NetworkStats; |
Jeff Sharkey | 5a7bcf3 | 2012-01-10 17:24:44 -0800 | [diff] [blame] | 96 | import android.net.NetworkStats.NonMonotonicObserver; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 97 | import android.net.NetworkStatsHistory; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 98 | import android.net.NetworkTemplate; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 99 | import android.net.TrafficStats; |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 100 | import android.os.BestClock; |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 101 | import android.os.Binder; |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 102 | import android.os.DropBoxManager; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 103 | import android.os.Environment; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 104 | import android.os.Handler; |
Amith Yamasani | 450a16b | 2013-09-18 16:28:50 -0700 | [diff] [blame] | 105 | import android.os.HandlerThread; |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 106 | import android.os.IBinder; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 107 | import android.os.INetworkManagementService; |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 108 | import android.os.Message; |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 109 | import android.os.Messenger; |
Jeff Sharkey | 6248926 | 2011-07-17 12:53:28 -0700 | [diff] [blame] | 110 | import android.os.PowerManager; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 111 | import android.os.RemoteException; |
| 112 | import android.os.SystemClock; |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 113 | import android.os.Trace; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 114 | import android.os.UserHandle; |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 115 | import android.provider.Settings; |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 116 | import android.provider.Settings.Global; |
Makoto Onuki | da65a52 | 2017-01-13 10:23:30 -0800 | [diff] [blame] | 117 | import android.service.NetworkInterfaceProto; |
| 118 | import android.service.NetworkStatsServiceDumpProto; |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 119 | import android.telephony.SubscriptionPlan; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 120 | import android.telephony.TelephonyManager; |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 121 | import android.text.format.DateUtils; |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 122 | import android.util.ArrayMap; |
| 123 | import android.util.ArraySet; |
Jeff Sharkey | 07b0dd9 | 2011-09-01 13:06:19 -0700 | [diff] [blame] | 124 | import android.util.EventLog; |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 125 | import android.util.Log; |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 126 | import android.util.MathUtils; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 127 | import android.util.Slog; |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 128 | import android.util.SparseIntArray; |
Makoto Onuki | da65a52 | 2017-01-13 10:23:30 -0800 | [diff] [blame] | 129 | import android.util.proto.ProtoOutputStream; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 130 | |
Jeff Sharkey | 4635f10 | 2017-09-01 11:27:13 -0600 | [diff] [blame] | 131 | import com.android.internal.annotations.GuardedBy; |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 132 | import com.android.internal.annotations.VisibleForTesting; |
Remi NGUYEN VAN | b6a9201 | 2018-03-06 12:36:54 +0900 | [diff] [blame] | 133 | import com.android.internal.net.NetworkStatsFactory; |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 134 | import com.android.internal.net.VpnInfo; |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 135 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 136 | import com.android.internal.util.DumpUtils; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 137 | import com.android.internal.util.FileRotator; |
| 138 | import com.android.internal.util.IndentingPrintWriter; |
Jeff Sharkey | 07b0dd9 | 2011-09-01 13:06:19 -0700 | [diff] [blame] | 139 | import com.android.server.EventLogTags; |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 140 | import com.android.server.LocalServices; |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 141 | import com.android.server.connectivity.Tethering; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 142 | |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 143 | import java.io.File; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 144 | import java.io.FileDescriptor; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 145 | import java.io.IOException; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 146 | import java.io.PrintWriter; |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 147 | import java.time.Clock; |
| 148 | import java.time.ZoneOffset; |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 149 | import java.util.Arrays; |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 150 | import java.util.HashSet; |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 151 | import java.util.List; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 152 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 153 | /** |
| 154 | * Collect and persist detailed network statistics, and provide this data to |
| 155 | * other system services. |
| 156 | */ |
| 157 | public class NetworkStatsService extends INetworkStatsService.Stub { |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 158 | static final String TAG = "NetworkStats"; |
Jeff Sharkey | 4e0d307 | 2018-02-20 13:36:14 -0700 | [diff] [blame] | 159 | static final boolean LOGD = Log.isLoggable(TAG, Log.DEBUG); |
| 160 | static final boolean LOGV = Log.isLoggable(TAG, Log.VERBOSE); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 161 | |
Chalard Jean | 5ecd5f1 | 2018-08-22 10:21:26 +0900 | [diff] [blame] | 162 | // Perform polling and persist all (FLAG_PERSIST_ALL). |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 163 | private static final int MSG_PERFORM_POLL = 1; |
| 164 | private static final int MSG_UPDATE_IFACES = 2; |
Chalard Jean | 5ecd5f1 | 2018-08-22 10:21:26 +0900 | [diff] [blame] | 165 | // Perform polling, persist network, and register the global alert again. |
| 166 | private static final int MSG_PERFORM_POLL_REGISTER_ALERT = 3; |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 167 | |
| 168 | /** Flags to control detail level of poll event. */ |
Jeff Sharkey | 905b589 | 2011-09-30 15:19:49 -0700 | [diff] [blame] | 169 | private static final int FLAG_PERSIST_NETWORK = 0x1; |
| 170 | private static final int FLAG_PERSIST_UID = 0x2; |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 171 | private static final int FLAG_PERSIST_ALL = FLAG_PERSIST_NETWORK | FLAG_PERSIST_UID; |
Jeff Sharkey | 1f0b13b | 2011-09-18 13:30:23 -0700 | [diff] [blame] | 172 | private static final int FLAG_PERSIST_FORCE = 0x100; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 173 | |
Chalard Jean | 5ecd5f1 | 2018-08-22 10:21:26 +0900 | [diff] [blame] | 174 | /** |
| 175 | * When global alert quota is high, wait for this delay before processing each polling, |
| 176 | * and do not schedule further polls once there is already one queued. |
| 177 | * This avoids firing the global alert too often on devices with high transfer speeds and |
| 178 | * high quota. |
| 179 | */ |
| 180 | private static final int PERFORM_POLL_DELAY_MS = 1000; |
| 181 | |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 182 | private static final String TAG_NETSTATS_ERROR = "netstats_error"; |
| 183 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 184 | private final Context mContext; |
| 185 | private final INetworkManagementService mNetworkManager; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 186 | private final AlarmManager mAlarmManager; |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 187 | private final Clock mClock; |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 188 | private final TelephonyManager mTeleManager; |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 189 | private final NetworkStatsSettings mSettings; |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 190 | private final NetworkStatsObservers mStatsObservers; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 191 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 192 | private final File mSystemDir; |
| 193 | private final File mBaseDir; |
| 194 | |
Jeff Sharkey | 6248926 | 2011-07-17 12:53:28 -0700 | [diff] [blame] | 195 | private final PowerManager.WakeLock mWakeLock; |
| 196 | |
Chenbo Feng | 0049982 | 2018-05-18 17:10:07 -0700 | [diff] [blame] | 197 | private final boolean mUseBpfTrafficStats; |
| 198 | |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 199 | private IConnectivityManager mConnManager; |
| 200 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 201 | @VisibleForTesting |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 202 | public static final String ACTION_NETWORK_STATS_POLL = |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 203 | "com.android.server.action.NETWORK_STATS_POLL"; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 204 | public static final String ACTION_NETWORK_STATS_UPDATED = |
| 205 | "com.android.server.action.NETWORK_STATS_UPDATED"; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 206 | |
| 207 | private PendingIntent mPollIntent; |
| 208 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 209 | private static final String PREFIX_DEV = "dev"; |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 210 | private static final String PREFIX_XT = "xt"; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 211 | private static final String PREFIX_UID = "uid"; |
| 212 | private static final String PREFIX_UID_TAG = "uid_tag"; |
| 213 | |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 214 | /** |
Jack Yu | b6587ea | 2016-06-22 11:35:10 -0700 | [diff] [blame] | 215 | * Virtual network interface for video telephony. This is for VT data usage counting purpose. |
| 216 | */ |
| 217 | public static final String VT_INTERFACE = "vt_data0"; |
| 218 | |
| 219 | /** |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 220 | * Settings that can be changed externally. |
| 221 | */ |
| 222 | public interface NetworkStatsSettings { |
| 223 | public long getPollInterval(); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 224 | public boolean getSampleEnabled(); |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 225 | public boolean getAugmentEnabled(); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 226 | |
| 227 | public static class Config { |
| 228 | public final long bucketDuration; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 229 | public final long rotateAgeMillis; |
| 230 | public final long deleteAgeMillis; |
| 231 | |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 232 | public Config(long bucketDuration, long rotateAgeMillis, long deleteAgeMillis) { |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 233 | this.bucketDuration = bucketDuration; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 234 | this.rotateAgeMillis = rotateAgeMillis; |
| 235 | this.deleteAgeMillis = deleteAgeMillis; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | public Config getDevConfig(); |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 240 | public Config getXtConfig(); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 241 | public Config getUidConfig(); |
| 242 | public Config getUidTagConfig(); |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 243 | |
| 244 | public long getGlobalAlertBytes(long def); |
| 245 | public long getDevPersistBytes(long def); |
| 246 | public long getXtPersistBytes(long def); |
| 247 | public long getUidPersistBytes(long def); |
| 248 | public long getUidTagPersistBytes(long def); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 249 | } |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 250 | |
| 251 | private final Object mStatsLock = new Object(); |
| 252 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 253 | /** Set of currently active ifaces. */ |
Lorenzo Colitti | c78da29 | 2018-01-19 00:50:48 +0900 | [diff] [blame] | 254 | @GuardedBy("mStatsLock") |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 255 | private final ArrayMap<String, NetworkIdentitySet> mActiveIfaces = new ArrayMap<>(); |
Lorenzo Colitti | c78da29 | 2018-01-19 00:50:48 +0900 | [diff] [blame] | 256 | |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 257 | /** Set of currently active ifaces for UID stats. */ |
Lorenzo Colitti | c78da29 | 2018-01-19 00:50:48 +0900 | [diff] [blame] | 258 | @GuardedBy("mStatsLock") |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 259 | private final ArrayMap<String, NetworkIdentitySet> mActiveUidIfaces = new ArrayMap<>(); |
Lorenzo Colitti | c78da29 | 2018-01-19 00:50:48 +0900 | [diff] [blame] | 260 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 261 | /** Current default active iface. */ |
| 262 | private String mActiveIface; |
Lorenzo Colitti | c78da29 | 2018-01-19 00:50:48 +0900 | [diff] [blame] | 263 | |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 264 | /** Set of any ifaces associated with mobile networks since boot. */ |
Lorenzo Colitti | c78da29 | 2018-01-19 00:50:48 +0900 | [diff] [blame] | 265 | @GuardedBy("mStatsLock") |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 266 | private String[] mMobileIfaces = new String[0]; |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 267 | |
Lorenzo Colitti | c78da29 | 2018-01-19 00:50:48 +0900 | [diff] [blame] | 268 | /** Set of all ifaces currently used by traffic that does not explicitly specify a Network. */ |
| 269 | @GuardedBy("mStatsLock") |
| 270 | private Network[] mDefaultNetworks = new Network[0]; |
| 271 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 272 | private final DropBoxNonMonotonicObserver mNonMonotonicObserver = |
| 273 | new DropBoxNonMonotonicObserver(); |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 274 | |
Jeff Sharkey | 4635f10 | 2017-09-01 11:27:13 -0600 | [diff] [blame] | 275 | @GuardedBy("mStatsLock") |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 276 | private NetworkStatsRecorder mDevRecorder; |
Jeff Sharkey | 4635f10 | 2017-09-01 11:27:13 -0600 | [diff] [blame] | 277 | @GuardedBy("mStatsLock") |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 278 | private NetworkStatsRecorder mXtRecorder; |
Jeff Sharkey | 4635f10 | 2017-09-01 11:27:13 -0600 | [diff] [blame] | 279 | @GuardedBy("mStatsLock") |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 280 | private NetworkStatsRecorder mUidRecorder; |
Jeff Sharkey | 4635f10 | 2017-09-01 11:27:13 -0600 | [diff] [blame] | 281 | @GuardedBy("mStatsLock") |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 282 | private NetworkStatsRecorder mUidTagRecorder; |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 283 | |
Jeff Sharkey | 70c7053 | 2012-05-16 14:51:19 -0700 | [diff] [blame] | 284 | /** Cached {@link #mXtRecorder} stats. */ |
Jeff Sharkey | 4635f10 | 2017-09-01 11:27:13 -0600 | [diff] [blame] | 285 | @GuardedBy("mStatsLock") |
Jeff Sharkey | 70c7053 | 2012-05-16 14:51:19 -0700 | [diff] [blame] | 286 | private NetworkStatsCollection mXtStatsCached; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 287 | |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 288 | /** Current counter sets for each UID. */ |
| 289 | private SparseIntArray mActiveUidCounterSet = new SparseIntArray(); |
| 290 | |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 291 | /** Data layer operation counters for splicing into other structures. */ |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 292 | private NetworkStats mUidOperations = new NetworkStats(0L, 10); |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 293 | |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 294 | /** Must be set in factory by calling #setHandler. */ |
| 295 | private Handler mHandler; |
| 296 | private Handler.Callback mHandlerCallback; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 297 | |
Jeff Sharkey | 050151e | 2018-04-13 14:28:30 -0600 | [diff] [blame] | 298 | private volatile boolean mSystemReady; |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 299 | private long mPersistThreshold = 2 * MB_IN_BYTES; |
| 300 | private long mGlobalAlertBytes; |
Jeff Sharkey | 6341fce | 2012-03-06 19:59:57 -0800 | [diff] [blame] | 301 | |
Remi NGUYEN VAN | abc6e18 | 2018-04-12 19:26:34 +0900 | [diff] [blame] | 302 | private static final long POLL_RATE_LIMIT_MS = 15_000; |
| 303 | |
| 304 | private long mLastStatsSessionPoll; |
| 305 | |
| 306 | /** Map from UID to number of opened sessions */ |
| 307 | @GuardedBy("mOpenSessionCallsPerUid") |
| 308 | private final SparseIntArray mOpenSessionCallsPerUid = new SparseIntArray(); |
| 309 | |
| 310 | private final static int DUMP_STATS_SESSION_COUNT = 20; |
| 311 | |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 312 | private static @NonNull File getDefaultSystemDir() { |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 313 | return new File(Environment.getDataDirectory(), "system"); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 316 | private static @NonNull File getDefaultBaseDir() { |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 317 | File baseDir = new File(getDefaultSystemDir(), "netstats"); |
| 318 | baseDir.mkdirs(); |
| 319 | return baseDir; |
| 320 | } |
| 321 | |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 322 | private static @NonNull Clock getDefaultClock() { |
| 323 | return new BestClock(ZoneOffset.UTC, SystemClock.currentNetworkTimeClock(), |
| 324 | Clock.systemUTC()); |
| 325 | } |
| 326 | |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 327 | public static NetworkStatsService create(Context context, |
| 328 | INetworkManagementService networkManager) { |
| 329 | AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
| 330 | PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); |
| 331 | PowerManager.WakeLock wakeLock = |
| 332 | powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); |
| 333 | |
| 334 | NetworkStatsService service = new NetworkStatsService(context, networkManager, alarmManager, |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 335 | wakeLock, getDefaultClock(), TelephonyManager.getDefault(), |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 336 | new DefaultNetworkStatsSettings(context), new NetworkStatsObservers(), |
| 337 | getDefaultSystemDir(), getDefaultBaseDir()); |
| 338 | |
| 339 | HandlerThread handlerThread = new HandlerThread(TAG); |
| 340 | Handler.Callback callback = new HandlerCallback(service); |
| 341 | handlerThread.start(); |
| 342 | Handler handler = new Handler(handlerThread.getLooper(), callback); |
| 343 | service.setHandler(handler, callback); |
| 344 | return service; |
| 345 | } |
| 346 | |
| 347 | @VisibleForTesting |
| 348 | NetworkStatsService(Context context, INetworkManagementService networkManager, |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 349 | AlarmManager alarmManager, PowerManager.WakeLock wakeLock, Clock clock, |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 350 | TelephonyManager teleManager, NetworkStatsSettings settings, |
| 351 | NetworkStatsObservers statsObservers, File systemDir, File baseDir) { |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 352 | mContext = checkNotNull(context, "missing Context"); |
| 353 | mNetworkManager = checkNotNull(networkManager, "missing INetworkManagementService"); |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 354 | mAlarmManager = checkNotNull(alarmManager, "missing AlarmManager"); |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 355 | mClock = checkNotNull(clock, "missing Clock"); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 356 | mSettings = checkNotNull(settings, "missing NetworkStatsSettings"); |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 357 | mTeleManager = checkNotNull(teleManager, "missing TelephonyManager"); |
| 358 | mWakeLock = checkNotNull(wakeLock, "missing WakeLock"); |
| 359 | mStatsObservers = checkNotNull(statsObservers, "missing NetworkStatsObservers"); |
| 360 | mSystemDir = checkNotNull(systemDir, "missing systemDir"); |
| 361 | mBaseDir = checkNotNull(baseDir, "missing baseDir"); |
Chenbo Feng | 0049982 | 2018-05-18 17:10:07 -0700 | [diff] [blame] | 362 | mUseBpfTrafficStats = new File("/sys/fs/bpf/traffic_uid_stats_map").exists(); |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 363 | |
| 364 | LocalServices.addService(NetworkStatsManagerInternal.class, |
| 365 | new NetworkStatsManagerInternalImpl()); |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 366 | } |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 367 | |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 368 | @VisibleForTesting |
| 369 | void setHandler(Handler handler, Handler.Callback callback) { |
| 370 | mHandler = handler; |
| 371 | mHandlerCallback = callback; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 374 | public void bindConnectivityManager(IConnectivityManager connManager) { |
| 375 | mConnManager = checkNotNull(connManager, "missing IConnectivityManager"); |
| 376 | } |
| 377 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 378 | public void systemReady() { |
Jeff Sharkey | 6341fce | 2012-03-06 19:59:57 -0800 | [diff] [blame] | 379 | mSystemReady = true; |
| 380 | |
Jeff Sharkey | 418d12d | 2011-12-13 15:38:03 -0800 | [diff] [blame] | 381 | if (!isBandwidthControlEnabled()) { |
| 382 | Slog.w(TAG, "bandwidth controls disabled, unable to track stats"); |
| 383 | return; |
| 384 | } |
| 385 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 386 | synchronized (mStatsLock) { |
Jeff Sharkey | 4635f10 | 2017-09-01 11:27:13 -0600 | [diff] [blame] | 387 | // create data recorders along with historical rotators |
| 388 | mDevRecorder = buildRecorder(PREFIX_DEV, mSettings.getDevConfig(), false); |
| 389 | mXtRecorder = buildRecorder(PREFIX_XT, mSettings.getXtConfig(), false); |
| 390 | mUidRecorder = buildRecorder(PREFIX_UID, mSettings.getUidConfig(), false); |
| 391 | mUidTagRecorder = buildRecorder(PREFIX_UID_TAG, mSettings.getUidTagConfig(), true); |
| 392 | |
| 393 | updatePersistThresholdsLocked(); |
| 394 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 395 | // upgrade any legacy stats, migrating them to rotated files |
| 396 | maybeUpgradeLegacyStatsLocked(); |
| 397 | |
| 398 | // read historical network stats from disk, since policy service |
| 399 | // might need them right away. |
Jeff Sharkey | 70c7053 | 2012-05-16 14:51:19 -0700 | [diff] [blame] | 400 | mXtStatsCached = mXtRecorder.getOrLoadCompleteLocked(); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 401 | |
| 402 | // bootstrap initial stats to prevent double-counting later |
| 403 | bootstrapStatsLocked(); |
| 404 | } |
Jeff Sharkey | 3359aca | 2011-11-08 18:08:48 -0800 | [diff] [blame] | 405 | |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 406 | // watch for tethering changes |
| 407 | final IntentFilter tetherFilter = new IntentFilter(ACTION_TETHER_STATE_CHANGED); |
Vinit Deshpande | 92d141f | 2014-09-10 18:05:10 -0700 | [diff] [blame] | 408 | mContext.registerReceiver(mTetherReceiver, tetherFilter, null, mHandler); |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 409 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 410 | // listen for periodic polling events |
| 411 | final IntentFilter pollFilter = new IntentFilter(ACTION_NETWORK_STATS_POLL); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 412 | mContext.registerReceiver(mPollReceiver, pollFilter, READ_NETWORK_USAGE_HISTORY, mHandler); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 413 | |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 414 | // listen for uid removal to clean stats |
| 415 | final IntentFilter removedFilter = new IntentFilter(ACTION_UID_REMOVED); |
| 416 | mContext.registerReceiver(mRemovedReceiver, removedFilter, null, mHandler); |
| 417 | |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 418 | // listen for user changes to clean stats |
| 419 | final IntentFilter userFilter = new IntentFilter(ACTION_USER_REMOVED); |
| 420 | mContext.registerReceiver(mUserReceiver, userFilter, null, mHandler); |
| 421 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 422 | // persist stats during clean shutdown |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 423 | final IntentFilter shutdownFilter = new IntentFilter(ACTION_SHUTDOWN); |
| 424 | mContext.registerReceiver(mShutdownReceiver, shutdownFilter); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 425 | |
| 426 | try { |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 427 | mNetworkManager.registerObserver(mAlertObserver); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 428 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 429 | // ignored; service lives in system_server |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 430 | } |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 431 | |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 432 | registerPollAlarmLocked(); |
| 433 | registerGlobalAlert(); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 434 | } |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 435 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 436 | private NetworkStatsRecorder buildRecorder( |
| 437 | String prefix, NetworkStatsSettings.Config config, boolean includeTags) { |
Jeff Sharkey | 6de357e | 2012-05-09 13:33:52 -0700 | [diff] [blame] | 438 | final DropBoxManager dropBox = (DropBoxManager) mContext.getSystemService( |
| 439 | Context.DROPBOX_SERVICE); |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 440 | return new NetworkStatsRecorder(new FileRotator( |
| 441 | mBaseDir, prefix, config.rotateAgeMillis, config.deleteAgeMillis), |
Jeff Sharkey | 6de357e | 2012-05-09 13:33:52 -0700 | [diff] [blame] | 442 | mNonMonotonicObserver, dropBox, prefix, config.bucketDuration, includeTags); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 445 | @GuardedBy("mStatsLock") |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 446 | private void shutdownLocked() { |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 447 | mContext.unregisterReceiver(mTetherReceiver); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 448 | mContext.unregisterReceiver(mPollReceiver); |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 449 | mContext.unregisterReceiver(mRemovedReceiver); |
Ryuki Nakamura | a47b0c9 | 2017-03-01 10:40:36 +0900 | [diff] [blame] | 450 | mContext.unregisterReceiver(mUserReceiver); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 451 | mContext.unregisterReceiver(mShutdownReceiver); |
| 452 | |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 453 | final long currentTime = mClock.millis(); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 454 | |
| 455 | // persist any pending stats |
| 456 | mDevRecorder.forcePersistLocked(currentTime); |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 457 | mXtRecorder.forcePersistLocked(currentTime); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 458 | mUidRecorder.forcePersistLocked(currentTime); |
| 459 | mUidTagRecorder.forcePersistLocked(currentTime); |
| 460 | |
Jeff Sharkey | 6341fce | 2012-03-06 19:59:57 -0800 | [diff] [blame] | 461 | mSystemReady = false; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 462 | } |
| 463 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 464 | @GuardedBy("mStatsLock") |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 465 | private void maybeUpgradeLegacyStatsLocked() { |
| 466 | File file; |
| 467 | try { |
| 468 | file = new File(mSystemDir, "netstats.bin"); |
| 469 | if (file.exists()) { |
| 470 | mDevRecorder.importLegacyNetworkLocked(file); |
| 471 | file.delete(); |
| 472 | } |
| 473 | |
| 474 | file = new File(mSystemDir, "netstats_xt.bin"); |
| 475 | if (file.exists()) { |
| 476 | file.delete(); |
| 477 | } |
| 478 | |
| 479 | file = new File(mSystemDir, "netstats_uid.bin"); |
| 480 | if (file.exists()) { |
| 481 | mUidRecorder.importLegacyUidLocked(file); |
| 482 | mUidTagRecorder.importLegacyUidLocked(file); |
| 483 | file.delete(); |
| 484 | } |
| 485 | } catch (IOException e) { |
| 486 | Log.wtf(TAG, "problem during legacy upgrade", e); |
Jeff Sharkey | e4984be | 2013-09-10 21:03:27 -0700 | [diff] [blame] | 487 | } catch (OutOfMemoryError e) { |
| 488 | Log.wtf(TAG, "problem during legacy upgrade", e); |
Jeff Sharkey | c506ff6 | 2011-11-17 11:59:29 -0800 | [diff] [blame] | 489 | } |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 490 | } |
| 491 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 492 | /** |
| 493 | * Clear any existing {@link #ACTION_NETWORK_STATS_POLL} alarms, and |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 494 | * reschedule based on current {@link NetworkStatsSettings#getPollInterval()}. |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 495 | */ |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 496 | private void registerPollAlarmLocked() { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 497 | if (mPollIntent != null) { |
| 498 | mAlarmManager.cancel(mPollIntent); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 499 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 500 | |
| 501 | mPollIntent = PendingIntent.getBroadcast( |
| 502 | mContext, 0, new Intent(ACTION_NETWORK_STATS_POLL), 0); |
| 503 | |
| 504 | final long currentRealtime = SystemClock.elapsedRealtime(); |
| 505 | mAlarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, currentRealtime, |
| 506 | mSettings.getPollInterval(), mPollIntent); |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 507 | } |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 508 | |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 509 | /** |
| 510 | * Register for a global alert that is delivered through |
| 511 | * {@link INetworkManagementEventObserver} once a threshold amount of data |
| 512 | * has been transferred. |
| 513 | */ |
| 514 | private void registerGlobalAlert() { |
| 515 | try { |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 516 | mNetworkManager.setGlobalAlert(mGlobalAlertBytes); |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 517 | } catch (IllegalStateException e) { |
| 518 | Slog.w(TAG, "problem registering for global alert: " + e); |
| 519 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 520 | // ignored; service lives in system_server |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 521 | } |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | @Override |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 525 | public INetworkStatsSession openSession() { |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 526 | // NOTE: if callers want to get non-augmented data, they should go |
| 527 | // through the public API |
| 528 | return openSessionInternal(NetworkStatsManager.FLAG_AUGMENT_WITH_SUBSCRIPTION_PLAN, null); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | @Override |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 532 | public INetworkStatsSession openSessionForUsageStats(int flags, String callingPackage) { |
| 533 | return openSessionInternal(flags, callingPackage); |
Zoltan Szatmary-Ban | 72027d2 | 2015-06-16 15:49:16 +0100 | [diff] [blame] | 534 | } |
| 535 | |
Remi NGUYEN VAN | abc6e18 | 2018-04-12 19:26:34 +0900 | [diff] [blame] | 536 | private boolean isRateLimitedForPoll(int callingUid) { |
| 537 | if (callingUid == android.os.Process.SYSTEM_UID) { |
| 538 | return false; |
| 539 | } |
| 540 | |
| 541 | final long lastCallTime; |
| 542 | final long now = SystemClock.elapsedRealtime(); |
| 543 | synchronized (mOpenSessionCallsPerUid) { |
| 544 | int calls = mOpenSessionCallsPerUid.get(callingUid, 0); |
| 545 | mOpenSessionCallsPerUid.put(callingUid, calls + 1); |
| 546 | lastCallTime = mLastStatsSessionPoll; |
| 547 | mLastStatsSessionPoll = now; |
| 548 | } |
| 549 | |
| 550 | return now - lastCallTime < POLL_RATE_LIMIT_MS; |
| 551 | } |
| 552 | |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 553 | private INetworkStatsSession openSessionInternal(final int flags, final String callingPackage) { |
Jeff Sharkey | 4190a04 | 2012-04-21 15:36:48 -0700 | [diff] [blame] | 554 | assertBandwidthControlEnabled(); |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 555 | |
Remi NGUYEN VAN | abc6e18 | 2018-04-12 19:26:34 +0900 | [diff] [blame] | 556 | final int callingUid = Binder.getCallingUid(); |
| 557 | final int usedFlags = isRateLimitedForPoll(callingUid) |
| 558 | ? flags & (~NetworkStatsManager.FLAG_POLL_ON_OPEN) |
| 559 | : flags; |
Jeff Sharkey | c3c8d16 | 2018-04-20 10:59:09 -0600 | [diff] [blame] | 560 | if ((usedFlags & (NetworkStatsManager.FLAG_POLL_ON_OPEN |
| 561 | | NetworkStatsManager.FLAG_POLL_FORCE)) != 0) { |
Zoltan Szatmary-Ban | 72027d2 | 2015-06-16 15:49:16 +0100 | [diff] [blame] | 562 | final long ident = Binder.clearCallingIdentity(); |
| 563 | try { |
| 564 | performPoll(FLAG_PERSIST_ALL); |
| 565 | } finally { |
| 566 | Binder.restoreCallingIdentity(ident); |
| 567 | } |
| 568 | } |
| 569 | |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 570 | // return an IBinder which holds strong references to any loaded stats |
| 571 | // for its lifetime; when caller closes only weak references remain. |
| 572 | |
| 573 | return new INetworkStatsSession.Stub() { |
Remi NGUYEN VAN | abc6e18 | 2018-04-12 19:26:34 +0900 | [diff] [blame] | 574 | private final int mCallingUid = callingUid; |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 575 | private final String mCallingPackage = callingPackage; |
| 576 | private final @NetworkStatsAccess.Level int mAccessLevel = checkAccessLevel( |
| 577 | callingPackage); |
| 578 | |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 579 | private NetworkStatsCollection mUidComplete; |
| 580 | private NetworkStatsCollection mUidTagComplete; |
| 581 | |
| 582 | private NetworkStatsCollection getUidComplete() { |
Ashish Sharma | c13b945 | 2013-11-01 14:57:30 -0700 | [diff] [blame] | 583 | synchronized (mStatsLock) { |
| 584 | if (mUidComplete == null) { |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 585 | mUidComplete = mUidRecorder.getOrLoadCompleteLocked(); |
| 586 | } |
Ashish Sharma | c13b945 | 2013-11-01 14:57:30 -0700 | [diff] [blame] | 587 | return mUidComplete; |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 588 | } |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | private NetworkStatsCollection getUidTagComplete() { |
Ashish Sharma | c13b945 | 2013-11-01 14:57:30 -0700 | [diff] [blame] | 592 | synchronized (mStatsLock) { |
| 593 | if (mUidTagComplete == null) { |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 594 | mUidTagComplete = mUidTagRecorder.getOrLoadCompleteLocked(); |
| 595 | } |
Ashish Sharma | c13b945 | 2013-11-01 14:57:30 -0700 | [diff] [blame] | 596 | return mUidTagComplete; |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 597 | } |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | @Override |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 601 | public int[] getRelevantUids() { |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 602 | return getUidComplete().getRelevantUids(mAccessLevel); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | @Override |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 606 | public NetworkStats getDeviceSummaryForNetwork( |
| 607 | NetworkTemplate template, long start, long end) { |
Remi NGUYEN VAN | abc6e18 | 2018-04-12 19:26:34 +0900 | [diff] [blame] | 608 | return internalGetSummaryForNetwork(template, usedFlags, start, end, mAccessLevel, |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 609 | mCallingUid); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | @Override |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 613 | public NetworkStats getSummaryForNetwork( |
| 614 | NetworkTemplate template, long start, long end) { |
Remi NGUYEN VAN | abc6e18 | 2018-04-12 19:26:34 +0900 | [diff] [blame] | 615 | return internalGetSummaryForNetwork(template, usedFlags, start, end, mAccessLevel, |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 616 | mCallingUid); |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | @Override |
| 620 | public NetworkStatsHistory getHistoryForNetwork(NetworkTemplate template, int fields) { |
Remi NGUYEN VAN | abc6e18 | 2018-04-12 19:26:34 +0900 | [diff] [blame] | 621 | return internalGetHistoryForNetwork(template, usedFlags, fields, mAccessLevel, |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 622 | mCallingUid); |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | @Override |
| 626 | public NetworkStats getSummaryForAllUid( |
| 627 | NetworkTemplate template, long start, long end, boolean includeTags) { |
Jeff Davidson | 39583b5 | 2016-12-12 11:55:37 -0800 | [diff] [blame] | 628 | try { |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 629 | final NetworkStats stats = getUidComplete() |
| 630 | .getSummary(template, start, end, mAccessLevel, mCallingUid); |
Jeff Davidson | 39583b5 | 2016-12-12 11:55:37 -0800 | [diff] [blame] | 631 | if (includeTags) { |
| 632 | final NetworkStats tagStats = getUidTagComplete() |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 633 | .getSummary(template, start, end, mAccessLevel, mCallingUid); |
Jeff Davidson | 39583b5 | 2016-12-12 11:55:37 -0800 | [diff] [blame] | 634 | stats.combineAllValues(tagStats); |
| 635 | } |
| 636 | return stats; |
| 637 | } catch (NullPointerException e) { |
| 638 | // TODO: Track down and fix the cause of this crash and remove this catch block. |
| 639 | Slog.wtf(TAG, "NullPointerException in getSummaryForAllUid", e); |
| 640 | throw e; |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 641 | } |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | @Override |
| 645 | public NetworkStatsHistory getHistoryForUid( |
| 646 | NetworkTemplate template, int uid, int set, int tag, int fields) { |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 647 | // NOTE: We don't augment UID-level statistics |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 648 | if (tag == TAG_NONE) { |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 649 | return getUidComplete().getHistory(template, null, uid, set, tag, fields, |
| 650 | Long.MIN_VALUE, Long.MAX_VALUE, mAccessLevel, mCallingUid); |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 651 | } else { |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 652 | return getUidTagComplete().getHistory(template, null, uid, set, tag, fields, |
| 653 | Long.MIN_VALUE, Long.MAX_VALUE, mAccessLevel, mCallingUid); |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | |
| 657 | @Override |
Zoltan Szatmary-Ban | 72027d2 | 2015-06-16 15:49:16 +0100 | [diff] [blame] | 658 | public NetworkStatsHistory getHistoryIntervalForUid( |
| 659 | NetworkTemplate template, int uid, int set, int tag, int fields, |
| 660 | long start, long end) { |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 661 | // NOTE: We don't augment UID-level statistics |
Zoltan Szatmary-Ban | 72027d2 | 2015-06-16 15:49:16 +0100 | [diff] [blame] | 662 | if (tag == TAG_NONE) { |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 663 | return getUidComplete().getHistory(template, null, uid, set, tag, fields, |
| 664 | start, end, mAccessLevel, mCallingUid); |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 665 | } else if (uid == Binder.getCallingUid()) { |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 666 | return getUidTagComplete().getHistory(template, null, uid, set, tag, fields, |
| 667 | start, end, mAccessLevel, mCallingUid); |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 668 | } else { |
| 669 | throw new SecurityException("Calling package " + mCallingPackage |
| 670 | + " cannot access tag information from a different uid"); |
Zoltan Szatmary-Ban | 72027d2 | 2015-06-16 15:49:16 +0100 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | |
| 674 | @Override |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 675 | public void close() { |
| 676 | mUidComplete = null; |
| 677 | mUidTagComplete = null; |
| 678 | } |
| 679 | }; |
Jeff Sharkey | 905b589 | 2011-09-30 15:19:49 -0700 | [diff] [blame] | 680 | } |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 681 | |
Jeff Davidson | 1efb133 | 2015-12-09 18:04:50 -0800 | [diff] [blame] | 682 | private @NetworkStatsAccess.Level int checkAccessLevel(String callingPackage) { |
| 683 | return NetworkStatsAccess.checkAccessLevel( |
| 684 | mContext, Binder.getCallingUid(), callingPackage); |
Zoltan Szatmary-Ban | 9c5dfa5 | 2015-02-23 17:20:20 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Jeff Sharkey | 70c7053 | 2012-05-16 14:51:19 -0700 | [diff] [blame] | 687 | /** |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 688 | * Find the most relevant {@link SubscriptionPlan} for the given |
| 689 | * {@link NetworkTemplate} and flags. This is typically used to augment |
| 690 | * local measurement results to match a known anchor from the carrier. |
| 691 | */ |
| 692 | private SubscriptionPlan resolveSubscriptionPlan(NetworkTemplate template, int flags) { |
| 693 | SubscriptionPlan plan = null; |
| 694 | if ((flags & NetworkStatsManager.FLAG_AUGMENT_WITH_SUBSCRIPTION_PLAN) != 0 |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 695 | && mSettings.getAugmentEnabled()) { |
Jeff Sharkey | 4e0d307 | 2018-02-20 13:36:14 -0700 | [diff] [blame] | 696 | if (LOGD) Slog.d(TAG, "Resolving plan for " + template); |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 697 | final long token = Binder.clearCallingIdentity(); |
| 698 | try { |
Jeff Sharkey | 146bb33 | 2018-04-18 15:42:57 -0600 | [diff] [blame] | 699 | plan = LocalServices.getService(NetworkPolicyManagerInternal.class) |
| 700 | .getSubscriptionPlan(template); |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 701 | } finally { |
| 702 | Binder.restoreCallingIdentity(token); |
| 703 | } |
Jeff Sharkey | 4e0d307 | 2018-02-20 13:36:14 -0700 | [diff] [blame] | 704 | if (LOGD) Slog.d(TAG, "Resolved to plan " + plan); |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 705 | } |
| 706 | return plan; |
| 707 | } |
| 708 | |
| 709 | /** |
Jeff Sharkey | 5274dcc | 2014-10-24 13:15:15 -0700 | [diff] [blame] | 710 | * Return network summary, splicing between DEV and XT stats when |
| 711 | * appropriate. |
Jeff Sharkey | 70c7053 | 2012-05-16 14:51:19 -0700 | [diff] [blame] | 712 | */ |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 713 | private NetworkStats internalGetSummaryForNetwork(NetworkTemplate template, int flags, |
| 714 | long start, long end, @NetworkStatsAccess.Level int accessLevel, int callingUid) { |
Jeff Sharkey | 5274dcc | 2014-10-24 13:15:15 -0700 | [diff] [blame] | 715 | // We've been using pure XT stats long enough that we no longer need to |
| 716 | // splice DEV and XT together. |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 717 | final NetworkStatsHistory history = internalGetHistoryForNetwork(template, flags, FIELD_ALL, |
| 718 | accessLevel, callingUid); |
| 719 | |
| 720 | final long now = System.currentTimeMillis(); |
| 721 | final NetworkStatsHistory.Entry entry = history.getValues(start, end, now, null); |
| 722 | |
| 723 | final NetworkStats stats = new NetworkStats(end - start, 1); |
Lorenzo Colitti | ada23ed | 2018-01-19 01:05:20 +0900 | [diff] [blame] | 724 | stats.addValues(new NetworkStats.Entry(IFACE_ALL, UID_ALL, SET_ALL, TAG_NONE, |
| 725 | METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, entry.rxBytes, entry.rxPackets, |
| 726 | entry.txBytes, entry.txPackets, entry.operations)); |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 727 | return stats; |
Jeff Sharkey | 70c7053 | 2012-05-16 14:51:19 -0700 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | /** |
Jeff Sharkey | 5274dcc | 2014-10-24 13:15:15 -0700 | [diff] [blame] | 731 | * Return network history, splicing between DEV and XT stats when |
| 732 | * appropriate. |
Jeff Sharkey | 70c7053 | 2012-05-16 14:51:19 -0700 | [diff] [blame] | 733 | */ |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 734 | private NetworkStatsHistory internalGetHistoryForNetwork(NetworkTemplate template, |
| 735 | int flags, int fields, @NetworkStatsAccess.Level int accessLevel, int callingUid) { |
Jeff Sharkey | 5274dcc | 2014-10-24 13:15:15 -0700 | [diff] [blame] | 736 | // We've been using pure XT stats long enough that we no longer need to |
| 737 | // splice DEV and XT together. |
Jeff Sharkey | 4635f10 | 2017-09-01 11:27:13 -0600 | [diff] [blame] | 738 | final SubscriptionPlan augmentPlan = resolveSubscriptionPlan(template, flags); |
| 739 | synchronized (mStatsLock) { |
| 740 | return mXtStatsCached.getHistory(template, augmentPlan, |
| 741 | UID_ALL, SET_ALL, TAG_NONE, fields, Long.MIN_VALUE, Long.MAX_VALUE, |
| 742 | accessLevel, callingUid); |
| 743 | } |
Jeff Sharkey | 70c7053 | 2012-05-16 14:51:19 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 746 | private long getNetworkTotalBytes(NetworkTemplate template, long start, long end) { |
| 747 | assertSystemReady(); |
Jeff Sharkey | 4190a04 | 2012-04-21 15:36:48 -0700 | [diff] [blame] | 748 | assertBandwidthControlEnabled(); |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 749 | |
| 750 | // NOTE: if callers want to get non-augmented data, they should go |
| 751 | // through the public API |
| 752 | return internalGetSummaryForNetwork(template, |
| 753 | NetworkStatsManager.FLAG_AUGMENT_WITH_SUBSCRIPTION_PLAN, start, end, |
| 754 | NetworkStatsAccess.Level.DEVICE, Binder.getCallingUid()).getTotalBytes(); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 755 | } |
| 756 | |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 757 | private NetworkStats getNetworkUidBytes(NetworkTemplate template, long start, long end) { |
| 758 | assertSystemReady(); |
| 759 | assertBandwidthControlEnabled(); |
| 760 | |
| 761 | final NetworkStatsCollection uidComplete; |
| 762 | synchronized (mStatsLock) { |
| 763 | uidComplete = mUidRecorder.getOrLoadCompleteLocked(); |
| 764 | } |
| 765 | return uidComplete.getSummary(template, start, end, NetworkStatsAccess.Level.DEVICE, |
| 766 | android.os.Process.SYSTEM_UID); |
| 767 | } |
| 768 | |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 769 | @Override |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 770 | public NetworkStats getDataLayerSnapshotForUid(int uid) throws RemoteException { |
| 771 | if (Binder.getCallingUid() != uid) { |
| 772 | mContext.enforceCallingOrSelfPermission(ACCESS_NETWORK_STATE, TAG); |
| 773 | } |
Jeff Sharkey | 4190a04 | 2012-04-21 15:36:48 -0700 | [diff] [blame] | 774 | assertBandwidthControlEnabled(); |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 775 | |
| 776 | // TODO: switch to data layer stats once kernel exports |
| 777 | // for now, read network layer stats and flatten across all ifaces |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 778 | final long token = Binder.clearCallingIdentity(); |
| 779 | final NetworkStats networkLayer; |
| 780 | try { |
Remi NGUYEN VAN | b6a9201 | 2018-03-06 12:36:54 +0900 | [diff] [blame] | 781 | networkLayer = mNetworkManager.getNetworkStatsUidDetail(uid, |
| 782 | NetworkStats.INTERFACES_ALL); |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 783 | } finally { |
| 784 | Binder.restoreCallingIdentity(token); |
| 785 | } |
| 786 | |
Jeff Sharkey | 21a5478 | 2012-04-09 10:27:55 -0700 | [diff] [blame] | 787 | // splice in operation counts |
| 788 | networkLayer.spliceOperationsFrom(mUidOperations); |
| 789 | |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 790 | final NetworkStats dataLayer = new NetworkStats( |
| 791 | networkLayer.getElapsedRealtime(), networkLayer.size()); |
| 792 | |
| 793 | NetworkStats.Entry entry = null; |
| 794 | for (int i = 0; i < networkLayer.size(); i++) { |
| 795 | entry = networkLayer.getValues(i, entry); |
| 796 | entry.iface = IFACE_ALL; |
| 797 | dataLayer.combineValues(entry); |
| 798 | } |
| 799 | |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 800 | return dataLayer; |
| 801 | } |
| 802 | |
| 803 | @Override |
Remi NGUYEN VAN | b6a9201 | 2018-03-06 12:36:54 +0900 | [diff] [blame] | 804 | public NetworkStats getDetailedUidStats(String[] requiredIfaces) { |
| 805 | try { |
| 806 | final String[] ifacesToQuery = |
Remi NGUYEN VAN | 75525b3 | 2018-02-27 16:47:22 +0900 | [diff] [blame] | 807 | NetworkStatsFactory.augmentWithStackedInterfaces(requiredIfaces); |
Remi NGUYEN VAN | b6a9201 | 2018-03-06 12:36:54 +0900 | [diff] [blame] | 808 | return getNetworkStatsUidDetail(ifacesToQuery); |
| 809 | } catch (RemoteException e) { |
| 810 | Log.wtf(TAG, "Error compiling UID stats", e); |
| 811 | return new NetworkStats(0L, 0); |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | @Override |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 816 | public String[] getMobileIfaces() { |
| 817 | return mMobileIfaces; |
| 818 | } |
| 819 | |
| 820 | @Override |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 821 | public void incrementOperationCount(int uid, int tag, int operationCount) { |
| 822 | if (Binder.getCallingUid() != uid) { |
Jeff Sharkey | 9f09e6a7 | 2017-06-26 11:24:47 -0600 | [diff] [blame] | 823 | mContext.enforceCallingOrSelfPermission( |
| 824 | android.Manifest.permission.UPDATE_DEVICE_STATS, TAG); |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 825 | } |
| 826 | |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 827 | if (operationCount < 0) { |
| 828 | throw new IllegalArgumentException("operation count can only be incremented"); |
| 829 | } |
| 830 | if (tag == TAG_NONE) { |
| 831 | throw new IllegalArgumentException("operation count must have specific tag"); |
| 832 | } |
| 833 | |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 834 | synchronized (mStatsLock) { |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 835 | final int set = mActiveUidCounterSet.get(uid, SET_DEFAULT); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 836 | mUidOperations.combineValues( |
| 837 | mActiveIface, uid, set, tag, 0L, 0L, 0L, 0L, operationCount); |
| 838 | mUidOperations.combineValues( |
| 839 | mActiveIface, uid, set, TAG_NONE, 0L, 0L, 0L, 0L, operationCount); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 840 | } |
| 841 | } |
| 842 | |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 843 | @VisibleForTesting |
| 844 | void setUidForeground(int uid, boolean uidForeground) { |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 845 | synchronized (mStatsLock) { |
| 846 | final int set = uidForeground ? SET_FOREGROUND : SET_DEFAULT; |
| 847 | final int oldSet = mActiveUidCounterSet.get(uid, SET_DEFAULT); |
| 848 | if (oldSet != set) { |
| 849 | mActiveUidCounterSet.put(uid, set); |
| 850 | setKernelCounterSet(uid, set); |
| 851 | } |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | |
| 855 | @Override |
Lorenzo Colitti | c78da29 | 2018-01-19 00:50:48 +0900 | [diff] [blame] | 856 | public void forceUpdateIfaces(Network[] defaultNetworks) { |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 857 | mContext.enforceCallingOrSelfPermission(READ_NETWORK_USAGE_HISTORY, TAG); |
| 858 | assertBandwidthControlEnabled(); |
| 859 | |
| 860 | final long token = Binder.clearCallingIdentity(); |
| 861 | try { |
Lorenzo Colitti | c78da29 | 2018-01-19 00:50:48 +0900 | [diff] [blame] | 862 | updateIfaces(defaultNetworks); |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 863 | } finally { |
| 864 | Binder.restoreCallingIdentity(token); |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | @Override |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 869 | public void forceUpdate() { |
| 870 | mContext.enforceCallingOrSelfPermission(READ_NETWORK_USAGE_HISTORY, TAG); |
Jeff Sharkey | 4190a04 | 2012-04-21 15:36:48 -0700 | [diff] [blame] | 871 | assertBandwidthControlEnabled(); |
Jeff Sharkey | e630f7b | 2012-01-31 17:12:53 -0800 | [diff] [blame] | 872 | |
| 873 | final long token = Binder.clearCallingIdentity(); |
| 874 | try { |
| 875 | performPoll(FLAG_PERSIST_ALL); |
| 876 | } finally { |
| 877 | Binder.restoreCallingIdentity(token); |
| 878 | } |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 879 | } |
| 880 | |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 881 | private void advisePersistThreshold(long thresholdBytes) { |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 882 | assertBandwidthControlEnabled(); |
| 883 | |
| 884 | // clamp threshold into safe range |
| 885 | mPersistThreshold = MathUtils.constrain(thresholdBytes, 128 * KB_IN_BYTES, 2 * MB_IN_BYTES); |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 886 | if (LOGV) { |
| 887 | Slog.v(TAG, "advisePersistThreshold() given " + thresholdBytes + ", clamped to " |
| 888 | + mPersistThreshold); |
| 889 | } |
| 890 | |
Jeff Sharkey | 20f5c3d | 2012-05-09 19:59:07 -0700 | [diff] [blame] | 891 | // update and persist if beyond new thresholds |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 892 | final long currentTime = mClock.millis(); |
Jeff Sharkey | 5801597 | 2012-05-07 11:08:49 -0700 | [diff] [blame] | 893 | synchronized (mStatsLock) { |
Jeff Sharkey | 20f5c3d | 2012-05-09 19:59:07 -0700 | [diff] [blame] | 894 | if (!mSystemReady) return; |
| 895 | |
Jeff Sharkey | 4635f10 | 2017-09-01 11:27:13 -0600 | [diff] [blame] | 896 | updatePersistThresholdsLocked(); |
Jeff Sharkey | 20f5c3d | 2012-05-09 19:59:07 -0700 | [diff] [blame] | 897 | |
Jeff Sharkey | 5801597 | 2012-05-07 11:08:49 -0700 | [diff] [blame] | 898 | mDevRecorder.maybePersistLocked(currentTime); |
| 899 | mXtRecorder.maybePersistLocked(currentTime); |
| 900 | mUidRecorder.maybePersistLocked(currentTime); |
| 901 | mUidTagRecorder.maybePersistLocked(currentTime); |
| 902 | } |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 903 | |
| 904 | // re-arm global alert |
| 905 | registerGlobalAlert(); |
| 906 | } |
| 907 | |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 908 | @Override |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 909 | public DataUsageRequest registerUsageCallback(String callingPackage, |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 910 | DataUsageRequest request, Messenger messenger, IBinder binder) { |
| 911 | checkNotNull(callingPackage, "calling package is null"); |
| 912 | checkNotNull(request, "DataUsageRequest is null"); |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 913 | checkNotNull(request.template, "NetworkTemplate is null"); |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 914 | checkNotNull(messenger, "messenger is null"); |
| 915 | checkNotNull(binder, "binder is null"); |
| 916 | |
| 917 | int callingUid = Binder.getCallingUid(); |
| 918 | @NetworkStatsAccess.Level int accessLevel = checkAccessLevel(callingPackage); |
| 919 | DataUsageRequest normalizedRequest; |
| 920 | final long token = Binder.clearCallingIdentity(); |
| 921 | try { |
| 922 | normalizedRequest = mStatsObservers.register(request, messenger, binder, |
| 923 | callingUid, accessLevel); |
| 924 | } finally { |
| 925 | Binder.restoreCallingIdentity(token); |
| 926 | } |
| 927 | |
| 928 | // Create baseline stats |
Chalard Jean | 5ecd5f1 | 2018-08-22 10:21:26 +0900 | [diff] [blame] | 929 | mHandler.sendMessage(mHandler.obtainMessage(MSG_PERFORM_POLL)); |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 930 | |
| 931 | return normalizedRequest; |
| 932 | } |
| 933 | |
| 934 | @Override |
Antonio Cansado | 6965c18 | 2016-03-30 11:37:18 -0700 | [diff] [blame] | 935 | public void unregisterUsageRequest(DataUsageRequest request) { |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 936 | checkNotNull(request, "DataUsageRequest is null"); |
| 937 | |
| 938 | int callingUid = Binder.getCallingUid(); |
| 939 | final long token = Binder.clearCallingIdentity(); |
| 940 | try { |
| 941 | mStatsObservers.unregister(request, callingUid); |
| 942 | } finally { |
| 943 | Binder.restoreCallingIdentity(token); |
| 944 | } |
| 945 | } |
| 946 | |
Chenbo Feng | d3d9c4e | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 947 | @Override |
| 948 | public long getUidStats(int uid, int type) { |
Chenbo Feng | aedd6a3 | 2017-11-20 18:23:46 -0800 | [diff] [blame] | 949 | return nativeGetUidStat(uid, type, checkBpfStatsEnable()); |
Chenbo Feng | d3d9c4e | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | @Override |
| 953 | public long getIfaceStats(String iface, int type) { |
junyulai | a5c3fb1 | 2018-06-25 21:51:14 +0800 | [diff] [blame] | 954 | // eBPF code doesn't provide per-interface TCP counters. Use xt_qtaguid for now. |
| 955 | // TODO: delete getMobileTcp(Rx|Tx)Packets entirely. See b/110443385 . |
| 956 | if (type == TYPE_TCP_TX_PACKETS || type == TYPE_TCP_RX_PACKETS) { |
| 957 | return nativeGetIfaceStat(iface, type, false); |
| 958 | } else { |
| 959 | return nativeGetIfaceStat(iface, type, checkBpfStatsEnable()); |
| 960 | } |
Chenbo Feng | d3d9c4e | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | @Override |
| 964 | public long getTotalStats(int type) { |
Chenbo Feng | aedd6a3 | 2017-11-20 18:23:46 -0800 | [diff] [blame] | 965 | return nativeGetTotalStat(type, checkBpfStatsEnable()); |
| 966 | } |
| 967 | |
| 968 | private boolean checkBpfStatsEnable() { |
Chenbo Feng | 0049982 | 2018-05-18 17:10:07 -0700 | [diff] [blame] | 969 | return mUseBpfTrafficStats; |
Chenbo Feng | d3d9c4e | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 970 | } |
| 971 | |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 972 | /** |
| 973 | * Update {@link NetworkStatsRecorder} and {@link #mGlobalAlertBytes} to |
| 974 | * reflect current {@link #mPersistThreshold} value. Always defers to |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 975 | * {@link Global} values when defined. |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 976 | */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 977 | @GuardedBy("mStatsLock") |
Jeff Sharkey | 4635f10 | 2017-09-01 11:27:13 -0600 | [diff] [blame] | 978 | private void updatePersistThresholdsLocked() { |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 979 | mDevRecorder.setPersistThreshold(mSettings.getDevPersistBytes(mPersistThreshold)); |
| 980 | mXtRecorder.setPersistThreshold(mSettings.getXtPersistBytes(mPersistThreshold)); |
| 981 | mUidRecorder.setPersistThreshold(mSettings.getUidPersistBytes(mPersistThreshold)); |
| 982 | mUidTagRecorder.setPersistThreshold(mSettings.getUidTagPersistBytes(mPersistThreshold)); |
| 983 | mGlobalAlertBytes = mSettings.getGlobalAlertBytes(mPersistThreshold); |
| 984 | } |
| 985 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 986 | /** |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 987 | * Receiver that watches for {@link Tethering} to claim interface pairs. |
| 988 | */ |
| 989 | private BroadcastReceiver mTetherReceiver = new BroadcastReceiver() { |
| 990 | @Override |
| 991 | public void onReceive(Context context, Intent intent) { |
| 992 | // on background handler thread, and verified CONNECTIVITY_INTERNAL |
| 993 | // permission above. |
Jeff Sharkey | 1f0b13b | 2011-09-18 13:30:23 -0700 | [diff] [blame] | 994 | performPoll(FLAG_PERSIST_NETWORK); |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 995 | } |
| 996 | }; |
| 997 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 998 | private BroadcastReceiver mPollReceiver = new BroadcastReceiver() { |
| 999 | @Override |
| 1000 | public void onReceive(Context context, Intent intent) { |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1001 | // on background handler thread, and verified UPDATE_DEVICE_STATS |
| 1002 | // permission above. |
Jeff Sharkey | 1f0b13b | 2011-09-18 13:30:23 -0700 | [diff] [blame] | 1003 | performPoll(FLAG_PERSIST_ALL); |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 1004 | |
| 1005 | // verify that we're watching global alert |
| 1006 | registerGlobalAlert(); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 1007 | } |
| 1008 | }; |
| 1009 | |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 1010 | private BroadcastReceiver mRemovedReceiver = new BroadcastReceiver() { |
| 1011 | @Override |
| 1012 | public void onReceive(Context context, Intent intent) { |
| 1013 | // on background handler thread, and UID_REMOVED is protected |
| 1014 | // broadcast. |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 1015 | |
| 1016 | final int uid = intent.getIntExtra(EXTRA_UID, -1); |
| 1017 | if (uid == -1) return; |
| 1018 | |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 1019 | synchronized (mStatsLock) { |
Jeff Sharkey | 6248926 | 2011-07-17 12:53:28 -0700 | [diff] [blame] | 1020 | mWakeLock.acquire(); |
| 1021 | try { |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 1022 | removeUidsLocked(uid); |
| 1023 | } finally { |
| 1024 | mWakeLock.release(); |
| 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 | }; |
| 1029 | |
| 1030 | private BroadcastReceiver mUserReceiver = new BroadcastReceiver() { |
| 1031 | @Override |
| 1032 | public void onReceive(Context context, Intent intent) { |
| 1033 | // On background handler thread, and USER_REMOVED is protected |
| 1034 | // broadcast. |
| 1035 | |
| 1036 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 1037 | if (userId == -1) return; |
| 1038 | |
| 1039 | synchronized (mStatsLock) { |
| 1040 | mWakeLock.acquire(); |
| 1041 | try { |
| 1042 | removeUserLocked(userId); |
Jeff Sharkey | 6248926 | 2011-07-17 12:53:28 -0700 | [diff] [blame] | 1043 | } finally { |
| 1044 | mWakeLock.release(); |
| 1045 | } |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 1046 | } |
| 1047 | } |
| 1048 | }; |
| 1049 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 1050 | private BroadcastReceiver mShutdownReceiver = new BroadcastReceiver() { |
| 1051 | @Override |
| 1052 | public void onReceive(Context context, Intent intent) { |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 1053 | // SHUTDOWN is protected broadcast. |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 1054 | synchronized (mStatsLock) { |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 1055 | shutdownLocked(); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 1056 | } |
| 1057 | } |
| 1058 | }; |
| 1059 | |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1060 | /** |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 1061 | * Observer that watches for {@link INetworkManagementService} alerts. |
| 1062 | */ |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 1063 | private INetworkManagementEventObserver mAlertObserver = new BaseNetworkObserver() { |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 1064 | @Override |
| 1065 | public void limitReached(String limitName, String iface) { |
| 1066 | // only someone like NMS should be calling us |
| 1067 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
| 1068 | |
| 1069 | if (LIMIT_GLOBAL_ALERT.equals(limitName)) { |
Chalard Jean | 5ecd5f1 | 2018-08-22 10:21:26 +0900 | [diff] [blame] | 1070 | // kick off background poll to collect network stats unless there is already |
| 1071 | // such a call pending; UID stats are handled during normal polling interval. |
| 1072 | if (!mHandler.hasMessages(MSG_PERFORM_POLL_REGISTER_ALERT)) { |
| 1073 | mHandler.sendEmptyMessageDelayed(MSG_PERFORM_POLL_REGISTER_ALERT, |
| 1074 | PERFORM_POLL_DELAY_MS); |
| 1075 | } |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 1076 | } |
| 1077 | } |
| 1078 | }; |
| 1079 | |
Lorenzo Colitti | c78da29 | 2018-01-19 00:50:48 +0900 | [diff] [blame] | 1080 | private void updateIfaces(Network[] defaultNetworks) { |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 1081 | synchronized (mStatsLock) { |
| 1082 | mWakeLock.acquire(); |
| 1083 | try { |
Lorenzo Colitti | c78da29 | 2018-01-19 00:50:48 +0900 | [diff] [blame] | 1084 | updateIfacesLocked(defaultNetworks); |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 1085 | } finally { |
| 1086 | mWakeLock.release(); |
| 1087 | } |
| 1088 | } |
| 1089 | } |
| 1090 | |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 1091 | /** |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1092 | * Inspect all current {@link NetworkState} to derive mapping from {@code |
| 1093 | * iface} to {@link NetworkStatsHistory}. When multiple {@link NetworkInfo} |
| 1094 | * are active on a single {@code iface}, they are combined under a single |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1095 | * {@link NetworkIdentitySet}. |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1096 | */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1097 | @GuardedBy("mStatsLock") |
Lorenzo Colitti | c78da29 | 2018-01-19 00:50:48 +0900 | [diff] [blame] | 1098 | private void updateIfacesLocked(Network[] defaultNetworks) { |
Jeff Sharkey | 6341fce | 2012-03-06 19:59:57 -0800 | [diff] [blame] | 1099 | if (!mSystemReady) return; |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 1100 | if (LOGV) Slog.v(TAG, "updateIfacesLocked()"); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1101 | |
| 1102 | // take one last stats snapshot before updating iface mapping. this |
| 1103 | // isn't perfect, since the kernel may already be counting traffic from |
| 1104 | // the updated network. |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1105 | |
Jeff Sharkey | 1f0b13b | 2011-09-18 13:30:23 -0700 | [diff] [blame] | 1106 | // poll, but only persist network stats to keep codepath fast. UID stats |
| 1107 | // will be persisted during next alarm poll event. |
| 1108 | performPollLocked(FLAG_PERSIST_NETWORK); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1109 | |
| 1110 | final NetworkState[] states; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1111 | final LinkProperties activeLink; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1112 | try { |
| 1113 | states = mConnManager.getAllNetworkState(); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1114 | activeLink = mConnManager.getActiveLinkProperties(); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1115 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1116 | // ignored; service lives in system_server |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1117 | return; |
| 1118 | } |
| 1119 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1120 | mActiveIface = activeLink != null ? activeLink.getInterfaceName() : null; |
| 1121 | |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 1122 | // Rebuild active interfaces based on connected networks |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1123 | mActiveIfaces.clear(); |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 1124 | mActiveUidIfaces.clear(); |
Lorenzo Colitti | c78da29 | 2018-01-19 00:50:48 +0900 | [diff] [blame] | 1125 | if (defaultNetworks != null) { |
| 1126 | // Caller is ConnectivityService. Update the list of default networks. |
| 1127 | mDefaultNetworks = defaultNetworks; |
| 1128 | } |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1129 | |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 1130 | final ArraySet<String> mobileIfaces = new ArraySet<>(); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1131 | for (NetworkState state : states) { |
| 1132 | if (state.networkInfo.isConnected()) { |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 1133 | final boolean isMobile = isNetworkTypeMobile(state.networkInfo.getType()); |
Lorenzo Colitti | d3e4a1e | 2018-01-19 01:12:04 +0900 | [diff] [blame] | 1134 | final boolean isDefault = ArrayUtils.contains(mDefaultNetworks, state.network); |
| 1135 | final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state, |
| 1136 | isDefault); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1137 | |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 1138 | // Traffic occurring on the base interface is always counted for |
| 1139 | // both total usage and UID details. |
| 1140 | final String baseIface = state.linkProperties.getInterfaceName(); |
Jeff Sharkey | 9da2f1e | 2014-08-14 12:55:00 -0700 | [diff] [blame] | 1141 | if (baseIface != null) { |
| 1142 | findOrCreateNetworkIdentitySet(mActiveIfaces, baseIface).add(ident); |
| 1143 | findOrCreateNetworkIdentitySet(mActiveUidIfaces, baseIface).add(ident); |
Jack Yu | b6587ea | 2016-06-22 11:35:10 -0700 | [diff] [blame] | 1144 | |
| 1145 | // Build a separate virtual interface for VT (Video Telephony) data usage. |
| 1146 | // Only do this when IMS is not metered, but VT is metered. |
| 1147 | // If IMS is metered, then the IMS network usage has already included VT usage. |
| 1148 | // VT is considered always metered in framework's layer. If VT is not metered |
| 1149 | // per carrier's policy, modem will report 0 usage for VT calls. |
| 1150 | if (state.networkCapabilities.hasCapability( |
| 1151 | NetworkCapabilities.NET_CAPABILITY_IMS) && !ident.getMetered()) { |
| 1152 | |
| 1153 | // Copy the identify from IMS one but mark it as metered. |
| 1154 | NetworkIdentity vtIdent = new NetworkIdentity(ident.getType(), |
| 1155 | ident.getSubType(), ident.getSubscriberId(), ident.getNetworkId(), |
Lorenzo Colitti | d3e4a1e | 2018-01-19 01:12:04 +0900 | [diff] [blame] | 1156 | ident.getRoaming(), true /* metered */, |
| 1157 | true /* onDefaultNetwork */); |
Jack Yu | b6587ea | 2016-06-22 11:35:10 -0700 | [diff] [blame] | 1158 | findOrCreateNetworkIdentitySet(mActiveIfaces, VT_INTERFACE).add(vtIdent); |
| 1159 | findOrCreateNetworkIdentitySet(mActiveUidIfaces, VT_INTERFACE).add(vtIdent); |
| 1160 | } |
| 1161 | |
Jeff Sharkey | 9da2f1e | 2014-08-14 12:55:00 -0700 | [diff] [blame] | 1162 | if (isMobile) { |
| 1163 | mobileIfaces.add(baseIface); |
| 1164 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1165 | } |
| 1166 | |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 1167 | // Traffic occurring on stacked interfaces is usually clatd, |
| 1168 | // which is already accounted against its final egress interface |
| 1169 | // by the kernel. Thus, we only need to collect stacked |
| 1170 | // interface stats at the UID level. |
| 1171 | final List<LinkProperties> stackedLinks = state.linkProperties.getStackedLinks(); |
| 1172 | for (LinkProperties stackedLink : stackedLinks) { |
| 1173 | final String stackedIface = stackedLink.getInterfaceName(); |
Jeff Sharkey | 9da2f1e | 2014-08-14 12:55:00 -0700 | [diff] [blame] | 1174 | if (stackedIface != null) { |
| 1175 | findOrCreateNetworkIdentitySet(mActiveUidIfaces, stackedIface).add(ident); |
| 1176 | if (isMobile) { |
| 1177 | mobileIfaces.add(stackedIface); |
| 1178 | } |
Remi NGUYEN VAN | b6a9201 | 2018-03-06 12:36:54 +0900 | [diff] [blame] | 1179 | |
| 1180 | NetworkStatsFactory.noteStackedIface(stackedIface, baseIface); |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 1181 | } |
| 1182 | } |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1183 | } |
| 1184 | } |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 1185 | |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 1186 | mMobileIfaces = mobileIfaces.toArray(new String[mobileIfaces.size()]); |
| 1187 | } |
| 1188 | |
| 1189 | private static <K> NetworkIdentitySet findOrCreateNetworkIdentitySet( |
| 1190 | ArrayMap<K, NetworkIdentitySet> map, K key) { |
| 1191 | NetworkIdentitySet ident = map.get(key); |
| 1192 | if (ident == null) { |
| 1193 | ident = new NetworkIdentitySet(); |
| 1194 | map.put(key, ident); |
| 1195 | } |
| 1196 | return ident; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1197 | } |
| 1198 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1199 | @GuardedBy("mStatsLock") |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 1200 | private void recordSnapshotLocked(long currentTime) throws RemoteException { |
| 1201 | // snapshot and record current counters; read UID stats first to |
Jack Yu | b6587ea | 2016-06-22 11:35:10 -0700 | [diff] [blame] | 1202 | // avoid over counting dev stats. |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1203 | Trace.traceBegin(TRACE_TAG_NETWORK, "snapshotUid"); |
Remi NGUYEN VAN | b6a9201 | 2018-03-06 12:36:54 +0900 | [diff] [blame] | 1204 | final NetworkStats uidSnapshot = getNetworkStatsUidDetail(INTERFACES_ALL); |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1205 | Trace.traceEnd(TRACE_TAG_NETWORK); |
| 1206 | Trace.traceBegin(TRACE_TAG_NETWORK, "snapshotXt"); |
Jack Yu | f9d559c | 2017-05-26 16:08:22 -0700 | [diff] [blame] | 1207 | final NetworkStats xtSnapshot = getNetworkStatsXt(); |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1208 | Trace.traceEnd(TRACE_TAG_NETWORK); |
| 1209 | Trace.traceBegin(TRACE_TAG_NETWORK, "snapshotDev"); |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 1210 | final NetworkStats devSnapshot = mNetworkManager.getNetworkStatsSummaryDev(); |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1211 | Trace.traceEnd(TRACE_TAG_NETWORK); |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 1212 | |
Lorenzo Colitti | 5356a35 | 2017-08-17 19:23:08 +0900 | [diff] [blame] | 1213 | // Tethering snapshot for dev and xt stats. Counts per-interface data from tethering stats |
| 1214 | // providers that isn't already counted by dev and XT stats. |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1215 | Trace.traceBegin(TRACE_TAG_NETWORK, "snapshotTether"); |
Lorenzo Colitti | 5356a35 | 2017-08-17 19:23:08 +0900 | [diff] [blame] | 1216 | final NetworkStats tetherSnapshot = getNetworkStatsTethering(STATS_PER_IFACE); |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1217 | Trace.traceEnd(TRACE_TAG_NETWORK); |
Lorenzo Colitti | 5356a35 | 2017-08-17 19:23:08 +0900 | [diff] [blame] | 1218 | xtSnapshot.combineAllValues(tetherSnapshot); |
| 1219 | devSnapshot.combineAllValues(tetherSnapshot); |
Jeff Davidson | 4ff3bcf | 2016-06-15 13:31:52 -0700 | [diff] [blame] | 1220 | |
| 1221 | // For xt/dev, we pass a null VPN array because usage is aggregated by UID, so VPN traffic |
| 1222 | // can't be reattributed to responsible apps. |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1223 | Trace.traceBegin(TRACE_TAG_NETWORK, "recordDev"); |
Jeff Davidson | 4ff3bcf | 2016-06-15 13:31:52 -0700 | [diff] [blame] | 1224 | mDevRecorder.recordSnapshotLocked( |
| 1225 | devSnapshot, mActiveIfaces, null /* vpnArray */, currentTime); |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1226 | Trace.traceEnd(TRACE_TAG_NETWORK); |
| 1227 | Trace.traceBegin(TRACE_TAG_NETWORK, "recordXt"); |
Jeff Davidson | 4ff3bcf | 2016-06-15 13:31:52 -0700 | [diff] [blame] | 1228 | mXtRecorder.recordSnapshotLocked( |
| 1229 | xtSnapshot, mActiveIfaces, null /* vpnArray */, currentTime); |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1230 | Trace.traceEnd(TRACE_TAG_NETWORK); |
Jeff Davidson | 4ff3bcf | 2016-06-15 13:31:52 -0700 | [diff] [blame] | 1231 | |
| 1232 | // For per-UID stats, pass the VPN info so VPN traffic is reattributed to responsible apps. |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 1233 | VpnInfo[] vpnArray = mConnManager.getAllVpnInfo(); |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1234 | Trace.traceBegin(TRACE_TAG_NETWORK, "recordUid"); |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 1235 | mUidRecorder.recordSnapshotLocked(uidSnapshot, mActiveUidIfaces, vpnArray, currentTime); |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1236 | Trace.traceEnd(TRACE_TAG_NETWORK); |
| 1237 | Trace.traceBegin(TRACE_TAG_NETWORK, "recordUidTag"); |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 1238 | mUidTagRecorder.recordSnapshotLocked(uidSnapshot, mActiveUidIfaces, vpnArray, currentTime); |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1239 | Trace.traceEnd(TRACE_TAG_NETWORK); |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 1240 | |
| 1241 | // We need to make copies of member fields that are sent to the observer to avoid |
| 1242 | // a race condition between the service handler thread and the observer's |
| 1243 | mStatsObservers.updateStats(xtSnapshot, uidSnapshot, new ArrayMap<>(mActiveIfaces), |
| 1244 | new ArrayMap<>(mActiveUidIfaces), vpnArray, currentTime); |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 1245 | } |
| 1246 | |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 1247 | /** |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 1248 | * Bootstrap initial stats snapshot, usually during {@link #systemReady()} |
| 1249 | * so we have baseline values without double-counting. |
| 1250 | */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1251 | @GuardedBy("mStatsLock") |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1252 | private void bootstrapStatsLocked() { |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 1253 | final long currentTime = mClock.millis(); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1254 | |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 1255 | try { |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 1256 | recordSnapshotLocked(currentTime); |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 1257 | } catch (IllegalStateException e) { |
| 1258 | Slog.w(TAG, "problem reading network stats: " + e); |
| 1259 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1260 | // ignored; service lives in system_server |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 1261 | } |
| 1262 | } |
| 1263 | |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1264 | private void performPoll(int flags) { |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 1265 | synchronized (mStatsLock) { |
| 1266 | mWakeLock.acquire(); |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 1267 | |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 1268 | try { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1269 | performPollLocked(flags); |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 1270 | } finally { |
| 1271 | mWakeLock.release(); |
| 1272 | } |
| 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | /** |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 1277 | * Periodic poll operation, reading current statistics and recording into |
| 1278 | * {@link NetworkStatsHistory}. |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 1279 | */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1280 | @GuardedBy("mStatsLock") |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1281 | private void performPollLocked(int flags) { |
Jeff Sharkey | 6341fce | 2012-03-06 19:59:57 -0800 | [diff] [blame] | 1282 | if (!mSystemReady) return; |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1283 | if (LOGV) Slog.v(TAG, "performPollLocked(flags=0x" + Integer.toHexString(flags) + ")"); |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1284 | Trace.traceBegin(TRACE_TAG_NETWORK, "performPollLocked"); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 1285 | |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1286 | final boolean persistNetwork = (flags & FLAG_PERSIST_NETWORK) != 0; |
| 1287 | final boolean persistUid = (flags & FLAG_PERSIST_UID) != 0; |
Jeff Sharkey | 1f0b13b | 2011-09-18 13:30:23 -0700 | [diff] [blame] | 1288 | final boolean persistForce = (flags & FLAG_PERSIST_FORCE) != 0; |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1289 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 1290 | // TODO: consider marking "untrusted" times in historical stats |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 1291 | final long currentTime = mClock.millis(); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 1292 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 1293 | try { |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 1294 | recordSnapshotLocked(currentTime); |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1295 | } catch (IllegalStateException e) { |
| 1296 | Log.wtf(TAG, "problem reading network stats", e); |
Jeff Sharkey | 905b589 | 2011-09-30 15:19:49 -0700 | [diff] [blame] | 1297 | return; |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1298 | } catch (RemoteException e) { |
| 1299 | // ignored; service lives in system_server |
Jeff Sharkey | 905b589 | 2011-09-30 15:19:49 -0700 | [diff] [blame] | 1300 | return; |
| 1301 | } |
| 1302 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1303 | // persist any pending data depending on requested flags |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1304 | Trace.traceBegin(TRACE_TAG_NETWORK, "[persisting]"); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1305 | if (persistForce) { |
| 1306 | mDevRecorder.forcePersistLocked(currentTime); |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 1307 | mXtRecorder.forcePersistLocked(currentTime); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1308 | mUidRecorder.forcePersistLocked(currentTime); |
| 1309 | mUidTagRecorder.forcePersistLocked(currentTime); |
| 1310 | } else { |
| 1311 | if (persistNetwork) { |
| 1312 | mDevRecorder.maybePersistLocked(currentTime); |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 1313 | mXtRecorder.maybePersistLocked(currentTime); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1314 | } |
| 1315 | if (persistUid) { |
| 1316 | mUidRecorder.maybePersistLocked(currentTime); |
| 1317 | mUidTagRecorder.maybePersistLocked(currentTime); |
| 1318 | } |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 1319 | } |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1320 | Trace.traceEnd(TRACE_TAG_NETWORK); |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 1321 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1322 | if (mSettings.getSampleEnabled()) { |
Jeff Sharkey | 905b589 | 2011-09-30 15:19:49 -0700 | [diff] [blame] | 1323 | // sample stats after each full poll |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1324 | performSampleLocked(); |
Jeff Sharkey | 905b589 | 2011-09-30 15:19:49 -0700 | [diff] [blame] | 1325 | } |
Jeff Sharkey | 07b0dd9 | 2011-09-01 13:06:19 -0700 | [diff] [blame] | 1326 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1327 | // finally, dispatch updated event to any listeners |
| 1328 | final Intent updatedIntent = new Intent(ACTION_NETWORK_STATS_UPDATED); |
| 1329 | updatedIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1330 | mContext.sendBroadcastAsUser(updatedIntent, UserHandle.ALL, |
| 1331 | READ_NETWORK_USAGE_HISTORY); |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1332 | |
| 1333 | Trace.traceEnd(TRACE_TAG_NETWORK); |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | /** |
Jeff Sharkey | 07b0dd9 | 2011-09-01 13:06:19 -0700 | [diff] [blame] | 1337 | * Sample recent statistics summary into {@link EventLog}. |
| 1338 | */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1339 | @GuardedBy("mStatsLock") |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1340 | private void performSampleLocked() { |
| 1341 | // TODO: migrate trustedtime fixes to separate binary log events |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 1342 | final long currentTime = mClock.millis(); |
Jeff Sharkey | 905b589 | 2011-09-30 15:19:49 -0700 | [diff] [blame] | 1343 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1344 | NetworkTemplate template; |
| 1345 | NetworkStats.Entry devTotal; |
| 1346 | NetworkStats.Entry xtTotal; |
| 1347 | NetworkStats.Entry uidTotal; |
Jeff Sharkey | 07b0dd9 | 2011-09-01 13:06:19 -0700 | [diff] [blame] | 1348 | |
| 1349 | // collect mobile sample |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 1350 | template = buildTemplateMobileWildcard(); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1351 | devTotal = mDevRecorder.getTotalSinceBootLocked(template); |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 1352 | xtTotal = mXtRecorder.getTotalSinceBootLocked(template); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1353 | uidTotal = mUidRecorder.getTotalSinceBootLocked(template); |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 1354 | |
Jeff Sharkey | 905b589 | 2011-09-30 15:19:49 -0700 | [diff] [blame] | 1355 | EventLogTags.writeNetstatsMobileSample( |
| 1356 | devTotal.rxBytes, devTotal.rxPackets, devTotal.txBytes, devTotal.txPackets, |
| 1357 | xtTotal.rxBytes, xtTotal.rxPackets, xtTotal.txBytes, xtTotal.txPackets, |
| 1358 | uidTotal.rxBytes, uidTotal.rxPackets, uidTotal.txBytes, uidTotal.txPackets, |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 1359 | currentTime); |
Jeff Sharkey | 07b0dd9 | 2011-09-01 13:06:19 -0700 | [diff] [blame] | 1360 | |
| 1361 | // collect wifi sample |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1362 | template = buildTemplateWifiWildcard(); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1363 | devTotal = mDevRecorder.getTotalSinceBootLocked(template); |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 1364 | xtTotal = mXtRecorder.getTotalSinceBootLocked(template); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1365 | uidTotal = mUidRecorder.getTotalSinceBootLocked(template); |
| 1366 | |
Jeff Sharkey | 905b589 | 2011-09-30 15:19:49 -0700 | [diff] [blame] | 1367 | EventLogTags.writeNetstatsWifiSample( |
| 1368 | devTotal.rxBytes, devTotal.rxPackets, devTotal.txBytes, devTotal.txPackets, |
| 1369 | xtTotal.rxBytes, xtTotal.rxPackets, xtTotal.txBytes, xtTotal.txPackets, |
| 1370 | uidTotal.rxBytes, uidTotal.rxPackets, uidTotal.txBytes, uidTotal.txPackets, |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 1371 | currentTime); |
Jeff Sharkey | 07b0dd9 | 2011-09-01 13:06:19 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | /** |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1375 | * Clean up {@link #mUidRecorder} after UID is removed. |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 1376 | */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1377 | @GuardedBy("mStatsLock") |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 1378 | private void removeUidsLocked(int... uids) { |
| 1379 | if (LOGV) Slog.v(TAG, "removeUidsLocked() for UIDs " + Arrays.toString(uids)); |
| 1380 | |
| 1381 | // Perform one last poll before removing |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 1382 | performPollLocked(FLAG_PERSIST_ALL); |
| 1383 | |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 1384 | mUidRecorder.removeUidsLocked(uids); |
| 1385 | mUidTagRecorder.removeUidsLocked(uids); |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 1386 | |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 1387 | // Clear kernel stats associated with UID |
| 1388 | for (int uid : uids) { |
| 1389 | resetKernelUidStats(uid); |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | /** |
| 1394 | * Clean up {@link #mUidRecorder} after user is removed. |
| 1395 | */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1396 | @GuardedBy("mStatsLock") |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 1397 | private void removeUserLocked(int userId) { |
| 1398 | if (LOGV) Slog.v(TAG, "removeUserLocked() for userId=" + userId); |
| 1399 | |
| 1400 | // Build list of UIDs that we should clean up |
| 1401 | int[] uids = new int[0]; |
| 1402 | final List<ApplicationInfo> apps = mContext.getPackageManager().getInstalledApplications( |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 1403 | PackageManager.MATCH_ANY_USER |
| 1404 | | PackageManager.MATCH_DISABLED_COMPONENTS); |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 1405 | for (ApplicationInfo app : apps) { |
| 1406 | final int uid = UserHandle.getUid(userId, app.uid); |
| 1407 | uids = ArrayUtils.appendInt(uids, uid); |
| 1408 | } |
| 1409 | |
| 1410 | removeUidsLocked(uids); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 1411 | } |
| 1412 | |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1413 | private class NetworkStatsManagerInternalImpl extends NetworkStatsManagerInternal { |
| 1414 | @Override |
| 1415 | public long getNetworkTotalBytes(NetworkTemplate template, long start, long end) { |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1416 | Trace.traceBegin(TRACE_TAG_NETWORK, "getNetworkTotalBytes"); |
| 1417 | try { |
| 1418 | return NetworkStatsService.this.getNetworkTotalBytes(template, start, end); |
| 1419 | } finally { |
| 1420 | Trace.traceEnd(TRACE_TAG_NETWORK); |
| 1421 | } |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | @Override |
| 1425 | public NetworkStats getNetworkUidBytes(NetworkTemplate template, long start, long end) { |
Jeff Sharkey | 0007239 | 2018-04-12 14:26:32 -0600 | [diff] [blame] | 1426 | Trace.traceBegin(TRACE_TAG_NETWORK, "getNetworkUidBytes"); |
| 1427 | try { |
| 1428 | return NetworkStatsService.this.getNetworkUidBytes(template, start, end); |
| 1429 | } finally { |
| 1430 | Trace.traceEnd(TRACE_TAG_NETWORK); |
| 1431 | } |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | @Override |
| 1435 | public void setUidForeground(int uid, boolean uidForeground) { |
| 1436 | NetworkStatsService.this.setUidForeground(uid, uidForeground); |
| 1437 | } |
| 1438 | |
| 1439 | @Override |
| 1440 | public void advisePersistThreshold(long thresholdBytes) { |
| 1441 | NetworkStatsService.this.advisePersistThreshold(thresholdBytes); |
| 1442 | } |
| 1443 | |
| 1444 | @Override |
| 1445 | public void forceUpdate() { |
| 1446 | NetworkStatsService.this.forceUpdate(); |
| 1447 | } |
| 1448 | } |
| 1449 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 1450 | @Override |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 1451 | protected void dump(FileDescriptor fd, PrintWriter rawWriter, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 1452 | if (!DumpUtils.checkDumpPermission(mContext, TAG, rawWriter)) return; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 1453 | |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 1454 | long duration = DateUtils.DAY_IN_MILLIS; |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 1455 | final HashSet<String> argSet = new HashSet<String>(); |
| 1456 | for (String arg : args) { |
| 1457 | argSet.add(arg); |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 1458 | |
| 1459 | if (arg.startsWith("--duration=")) { |
| 1460 | try { |
| 1461 | duration = Long.parseLong(arg.substring(11)); |
| 1462 | } catch (NumberFormatException ignored) { |
| 1463 | } |
| 1464 | } |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 1465 | } |
| 1466 | |
Jeff Sharkey | 706498d | 2012-02-06 17:35:07 -0800 | [diff] [blame] | 1467 | // usage: dumpsys netstats --full --uid --tag --poll --checkin |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1468 | final boolean poll = argSet.contains("--poll") || argSet.contains("poll"); |
Jeff Sharkey | 706498d | 2012-02-06 17:35:07 -0800 | [diff] [blame] | 1469 | final boolean checkin = argSet.contains("--checkin"); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1470 | final boolean fullHistory = argSet.contains("--full") || argSet.contains("full"); |
| 1471 | final boolean includeUid = argSet.contains("--uid") || argSet.contains("detail"); |
| 1472 | final boolean includeTag = argSet.contains("--tag") || argSet.contains("detail"); |
| 1473 | |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 1474 | final IndentingPrintWriter pw = new IndentingPrintWriter(rawWriter, " "); |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 1475 | |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 1476 | synchronized (mStatsLock) { |
Makoto Onuki | da65a52 | 2017-01-13 10:23:30 -0800 | [diff] [blame] | 1477 | if (args.length > 0 && "--proto".equals(args[0])) { |
| 1478 | // In this case ignore all other arguments. |
Jeff Sharkey | 4635f10 | 2017-09-01 11:27:13 -0600 | [diff] [blame] | 1479 | dumpProtoLocked(fd); |
Makoto Onuki | da65a52 | 2017-01-13 10:23:30 -0800 | [diff] [blame] | 1480 | return; |
| 1481 | } |
| 1482 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1483 | if (poll) { |
Jeff Sharkey | 1f0b13b | 2011-09-18 13:30:23 -0700 | [diff] [blame] | 1484 | performPollLocked(FLAG_PERSIST_ALL | FLAG_PERSIST_FORCE); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 1485 | pw.println("Forced poll"); |
| 1486 | return; |
| 1487 | } |
| 1488 | |
Jeff Sharkey | 706498d | 2012-02-06 17:35:07 -0800 | [diff] [blame] | 1489 | if (checkin) { |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 1490 | final long end = System.currentTimeMillis(); |
| 1491 | final long start = end - duration; |
| 1492 | |
| 1493 | pw.print("v1,"); |
| 1494 | pw.print(start / SECOND_IN_MILLIS); pw.print(','); |
| 1495 | pw.print(end / SECOND_IN_MILLIS); pw.println(); |
| 1496 | |
| 1497 | pw.println("xt"); |
| 1498 | mXtRecorder.dumpCheckin(rawWriter, start, end); |
| 1499 | |
| 1500 | if (includeUid) { |
| 1501 | pw.println("uid"); |
| 1502 | mUidRecorder.dumpCheckin(rawWriter, start, end); |
Jeff Sharkey | 706498d | 2012-02-06 17:35:07 -0800 | [diff] [blame] | 1503 | } |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 1504 | if (includeTag) { |
| 1505 | pw.println("tag"); |
| 1506 | mUidTagRecorder.dumpCheckin(rawWriter, start, end); |
| 1507 | } |
Jeff Sharkey | 706498d | 2012-02-06 17:35:07 -0800 | [diff] [blame] | 1508 | return; |
| 1509 | } |
| 1510 | |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 1511 | pw.println("Active interfaces:"); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1512 | pw.increaseIndent(); |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 1513 | for (int i = 0; i < mActiveIfaces.size(); i++) { |
| 1514 | pw.printPair("iface", mActiveIfaces.keyAt(i)); |
| 1515 | pw.printPair("ident", mActiveIfaces.valueAt(i)); |
| 1516 | pw.println(); |
| 1517 | } |
| 1518 | pw.decreaseIndent(); |
| 1519 | |
| 1520 | pw.println("Active UID interfaces:"); |
| 1521 | pw.increaseIndent(); |
| 1522 | for (int i = 0; i < mActiveUidIfaces.size(); i++) { |
| 1523 | pw.printPair("iface", mActiveUidIfaces.keyAt(i)); |
| 1524 | pw.printPair("ident", mActiveUidIfaces.valueAt(i)); |
| 1525 | pw.println(); |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 1526 | } |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1527 | pw.decreaseIndent(); |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 1528 | |
Remi NGUYEN VAN | abc6e18 | 2018-04-12 19:26:34 +0900 | [diff] [blame] | 1529 | // Get the top openSession callers |
| 1530 | final SparseIntArray calls; |
| 1531 | synchronized (mOpenSessionCallsPerUid) { |
| 1532 | calls = mOpenSessionCallsPerUid.clone(); |
| 1533 | } |
| 1534 | |
| 1535 | final int N = calls.size(); |
| 1536 | final long[] values = new long[N]; |
| 1537 | for (int j = 0; j < N; j++) { |
| 1538 | values[j] = ((long) calls.valueAt(j) << 32) | calls.keyAt(j); |
| 1539 | } |
| 1540 | Arrays.sort(values); |
| 1541 | |
| 1542 | pw.println("Top openSession callers (uid=count):"); |
| 1543 | pw.increaseIndent(); |
| 1544 | final int end = Math.max(0, N - DUMP_STATS_SESSION_COUNT); |
| 1545 | for (int j = N - 1; j >= end; j--) { |
| 1546 | final int uid = (int) (values[j] & 0xffffffff); |
| 1547 | final int count = (int) (values[j] >> 32); |
| 1548 | pw.print(uid); pw.print("="); pw.println(count); |
| 1549 | } |
| 1550 | pw.decreaseIndent(); |
| 1551 | pw.println(); |
| 1552 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1553 | pw.println("Dev stats:"); |
| 1554 | pw.increaseIndent(); |
| 1555 | mDevRecorder.dumpLocked(pw, fullHistory); |
| 1556 | pw.decreaseIndent(); |
| 1557 | |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 1558 | pw.println("Xt stats:"); |
| 1559 | pw.increaseIndent(); |
| 1560 | mXtRecorder.dumpLocked(pw, fullHistory); |
| 1561 | pw.decreaseIndent(); |
| 1562 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1563 | if (includeUid) { |
| 1564 | pw.println("UID stats:"); |
| 1565 | pw.increaseIndent(); |
| 1566 | mUidRecorder.dumpLocked(pw, fullHistory); |
| 1567 | pw.decreaseIndent(); |
Jeff Sharkey | 905b589 | 2011-09-30 15:19:49 -0700 | [diff] [blame] | 1568 | } |
| 1569 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1570 | if (includeTag) { |
| 1571 | pw.println("UID tag stats:"); |
| 1572 | pw.increaseIndent(); |
| 1573 | mUidTagRecorder.dumpLocked(pw, fullHistory); |
| 1574 | pw.decreaseIndent(); |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 1575 | } |
| 1576 | } |
| 1577 | } |
| 1578 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1579 | @GuardedBy("mStatsLock") |
Jeff Sharkey | 4635f10 | 2017-09-01 11:27:13 -0600 | [diff] [blame] | 1580 | private void dumpProtoLocked(FileDescriptor fd) { |
Makoto Onuki | da65a52 | 2017-01-13 10:23:30 -0800 | [diff] [blame] | 1581 | final ProtoOutputStream proto = new ProtoOutputStream(fd); |
| 1582 | |
| 1583 | // TODO Right now it writes all history. Should it limit to the "since-boot" log? |
| 1584 | |
| 1585 | dumpInterfaces(proto, NetworkStatsServiceDumpProto.ACTIVE_INTERFACES, mActiveIfaces); |
| 1586 | dumpInterfaces(proto, NetworkStatsServiceDumpProto.ACTIVE_UID_INTERFACES, mActiveUidIfaces); |
| 1587 | mDevRecorder.writeToProtoLocked(proto, NetworkStatsServiceDumpProto.DEV_STATS); |
| 1588 | mXtRecorder.writeToProtoLocked(proto, NetworkStatsServiceDumpProto.XT_STATS); |
| 1589 | mUidRecorder.writeToProtoLocked(proto, NetworkStatsServiceDumpProto.UID_STATS); |
| 1590 | mUidTagRecorder.writeToProtoLocked(proto, NetworkStatsServiceDumpProto.UID_TAG_STATS); |
| 1591 | |
| 1592 | proto.flush(); |
| 1593 | } |
| 1594 | |
| 1595 | private static void dumpInterfaces(ProtoOutputStream proto, long tag, |
| 1596 | ArrayMap<String, NetworkIdentitySet> ifaces) { |
| 1597 | for (int i = 0; i < ifaces.size(); i++) { |
| 1598 | final long start = proto.start(tag); |
| 1599 | |
| 1600 | proto.write(NetworkInterfaceProto.INTERFACE, ifaces.keyAt(i)); |
| 1601 | ifaces.valueAt(i).writeToProto(proto, NetworkInterfaceProto.IDENTITIES); |
| 1602 | |
| 1603 | proto.end(start); |
| 1604 | } |
| 1605 | } |
| 1606 | |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 1607 | /** |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1608 | * Return snapshot of current UID statistics, including any |
Jack Yu | f9d559c | 2017-05-26 16:08:22 -0700 | [diff] [blame] | 1609 | * {@link TrafficStats#UID_TETHERING}, video calling data usage, and {@link #mUidOperations} |
| 1610 | * values. |
Remi NGUYEN VAN | b6a9201 | 2018-03-06 12:36:54 +0900 | [diff] [blame] | 1611 | * |
| 1612 | * @param ifaces A list of interfaces the stats should be restricted to, or |
| 1613 | * {@link NetworkStats#INTERFACES_ALL}. |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 1614 | */ |
Remi NGUYEN VAN | b6a9201 | 2018-03-06 12:36:54 +0900 | [diff] [blame] | 1615 | private NetworkStats getNetworkStatsUidDetail(String[] ifaces) |
| 1616 | throws RemoteException { |
| 1617 | |
Remi NGUYEN VAN | 75525b3 | 2018-02-27 16:47:22 +0900 | [diff] [blame] | 1618 | // TODO: remove 464xlat adjustments from NetworkStatsFactory and apply all at once here. |
Remi NGUYEN VAN | b6a9201 | 2018-03-06 12:36:54 +0900 | [diff] [blame] | 1619 | final NetworkStats uidSnapshot = mNetworkManager.getNetworkStatsUidDetail(UID_ALL, |
| 1620 | ifaces); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1621 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1622 | // fold tethering stats and operations into uid snapshot |
Lorenzo Colitti | 5356a35 | 2017-08-17 19:23:08 +0900 | [diff] [blame] | 1623 | final NetworkStats tetherSnapshot = getNetworkStatsTethering(STATS_PER_UID); |
Remi NGUYEN VAN | b6a9201 | 2018-03-06 12:36:54 +0900 | [diff] [blame] | 1624 | tetherSnapshot.filter(UID_ALL, ifaces, TAG_ALL); |
Remi NGUYEN VAN | 75525b3 | 2018-02-27 16:47:22 +0900 | [diff] [blame] | 1625 | NetworkStatsFactory.apply464xlatAdjustments(uidSnapshot, tetherSnapshot); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1626 | uidSnapshot.combineAllValues(tetherSnapshot); |
Jack Yu | f9d559c | 2017-05-26 16:08:22 -0700 | [diff] [blame] | 1627 | |
| 1628 | final TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService( |
| 1629 | Context.TELEPHONY_SERVICE); |
| 1630 | |
| 1631 | // fold video calling data usage stats into uid snapshot |
Lorenzo Colitti | 5356a35 | 2017-08-17 19:23:08 +0900 | [diff] [blame] | 1632 | final NetworkStats vtStats = telephonyManager.getVtDataUsage(STATS_PER_UID); |
Jack Yu | f9d559c | 2017-05-26 16:08:22 -0700 | [diff] [blame] | 1633 | if (vtStats != null) { |
Remi NGUYEN VAN | b6a9201 | 2018-03-06 12:36:54 +0900 | [diff] [blame] | 1634 | vtStats.filter(UID_ALL, ifaces, TAG_ALL); |
Remi NGUYEN VAN | 75525b3 | 2018-02-27 16:47:22 +0900 | [diff] [blame] | 1635 | NetworkStatsFactory.apply464xlatAdjustments(uidSnapshot, vtStats); |
Jack Yu | f9d559c | 2017-05-26 16:08:22 -0700 | [diff] [blame] | 1636 | uidSnapshot.combineAllValues(vtStats); |
| 1637 | } |
Remi NGUYEN VAN | b6a9201 | 2018-03-06 12:36:54 +0900 | [diff] [blame] | 1638 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1639 | uidSnapshot.combineAllValues(mUidOperations); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1640 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1641 | return uidSnapshot; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 1642 | } |
| 1643 | |
Jeff Sharkey | d4ef8c8f | 2011-11-10 17:54:23 -0800 | [diff] [blame] | 1644 | /** |
Jack Yu | f9d559c | 2017-05-26 16:08:22 -0700 | [diff] [blame] | 1645 | * Return snapshot of current XT statistics with video calling data usage statistics. |
Jack Yu | b6587ea | 2016-06-22 11:35:10 -0700 | [diff] [blame] | 1646 | */ |
Jack Yu | f9d559c | 2017-05-26 16:08:22 -0700 | [diff] [blame] | 1647 | private NetworkStats getNetworkStatsXt() throws RemoteException { |
Jack Yu | b6587ea | 2016-06-22 11:35:10 -0700 | [diff] [blame] | 1648 | final NetworkStats xtSnapshot = mNetworkManager.getNetworkStatsSummaryXt(); |
| 1649 | |
Jack Yu | f9d559c | 2017-05-26 16:08:22 -0700 | [diff] [blame] | 1650 | final TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService( |
Jack Yu | b6587ea | 2016-06-22 11:35:10 -0700 | [diff] [blame] | 1651 | Context.TELEPHONY_SERVICE); |
| 1652 | |
Jack Yu | f9d559c | 2017-05-26 16:08:22 -0700 | [diff] [blame] | 1653 | // Merge video calling data usage into XT |
Lorenzo Colitti | 5356a35 | 2017-08-17 19:23:08 +0900 | [diff] [blame] | 1654 | final NetworkStats vtSnapshot = telephonyManager.getVtDataUsage(STATS_PER_IFACE); |
Jack Yu | f9d559c | 2017-05-26 16:08:22 -0700 | [diff] [blame] | 1655 | if (vtSnapshot != null) { |
| 1656 | xtSnapshot.combineAllValues(vtSnapshot); |
| 1657 | } |
Jack Yu | b6587ea | 2016-06-22 11:35:10 -0700 | [diff] [blame] | 1658 | |
| 1659 | return xtSnapshot; |
| 1660 | } |
| 1661 | |
| 1662 | /** |
Jeff Sharkey | d4ef8c8f | 2011-11-10 17:54:23 -0800 | [diff] [blame] | 1663 | * Return snapshot of current tethering statistics. Will return empty |
| 1664 | * {@link NetworkStats} if any problems are encountered. |
| 1665 | */ |
Lorenzo Colitti | 5356a35 | 2017-08-17 19:23:08 +0900 | [diff] [blame] | 1666 | private NetworkStats getNetworkStatsTethering(int how) throws RemoteException { |
Jeff Sharkey | d4ef8c8f | 2011-11-10 17:54:23 -0800 | [diff] [blame] | 1667 | try { |
Lorenzo Colitti | 5356a35 | 2017-08-17 19:23:08 +0900 | [diff] [blame] | 1668 | return mNetworkManager.getNetworkStatsTethering(how); |
Jeff Sharkey | d4ef8c8f | 2011-11-10 17:54:23 -0800 | [diff] [blame] | 1669 | } catch (IllegalStateException e) { |
| 1670 | Log.wtf(TAG, "problem reading network stats", e); |
| 1671 | return new NetworkStats(0L, 10); |
| 1672 | } |
| 1673 | } |
| 1674 | |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 1675 | @VisibleForTesting |
| 1676 | static class HandlerCallback implements Handler.Callback { |
| 1677 | private final NetworkStatsService mService; |
| 1678 | |
| 1679 | HandlerCallback(NetworkStatsService service) { |
| 1680 | this.mService = service; |
| 1681 | } |
| 1682 | |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 1683 | @Override |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 1684 | public boolean handleMessage(Message msg) { |
| 1685 | switch (msg.what) { |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 1686 | case MSG_PERFORM_POLL: { |
Chalard Jean | 5ecd5f1 | 2018-08-22 10:21:26 +0900 | [diff] [blame] | 1687 | mService.performPoll(FLAG_PERSIST_ALL); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 1688 | return true; |
| 1689 | } |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 1690 | case MSG_UPDATE_IFACES: { |
Lorenzo Colitti | c78da29 | 2018-01-19 00:50:48 +0900 | [diff] [blame] | 1691 | mService.updateIfaces(null); |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 1692 | return true; |
| 1693 | } |
Chalard Jean | 5ecd5f1 | 2018-08-22 10:21:26 +0900 | [diff] [blame] | 1694 | case MSG_PERFORM_POLL_REGISTER_ALERT: { |
| 1695 | mService.performPoll(FLAG_PERSIST_NETWORK); |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 1696 | mService.registerGlobalAlert(); |
Jeff Sharkey | 25ce9ed | 2012-02-02 13:07:47 -0800 | [diff] [blame] | 1697 | return true; |
| 1698 | } |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 1699 | default: { |
| 1700 | return false; |
| 1701 | } |
| 1702 | } |
| 1703 | } |
Antonio Cansado | cd42acd | 2016-02-17 13:03:38 -0800 | [diff] [blame] | 1704 | } |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 1705 | |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1706 | private void assertSystemReady() { |
| 1707 | if (!mSystemReady) { |
| 1708 | throw new IllegalStateException("System not ready"); |
| 1709 | } |
| 1710 | } |
| 1711 | |
Jeff Sharkey | 4190a04 | 2012-04-21 15:36:48 -0700 | [diff] [blame] | 1712 | private void assertBandwidthControlEnabled() { |
| 1713 | if (!isBandwidthControlEnabled()) { |
| 1714 | throw new IllegalStateException("Bandwidth module disabled"); |
| 1715 | } |
| 1716 | } |
| 1717 | |
Jeff Sharkey | 418d12d | 2011-12-13 15:38:03 -0800 | [diff] [blame] | 1718 | private boolean isBandwidthControlEnabled() { |
Jeff Sharkey | 49c1d17 | 2012-04-23 14:39:19 -0700 | [diff] [blame] | 1719 | final long token = Binder.clearCallingIdentity(); |
Jeff Sharkey | 418d12d | 2011-12-13 15:38:03 -0800 | [diff] [blame] | 1720 | try { |
| 1721 | return mNetworkManager.isBandwidthControlEnabled(); |
| 1722 | } catch (RemoteException e) { |
| 1723 | // ignored; service lives in system_server |
| 1724 | return false; |
Jeff Sharkey | 49c1d17 | 2012-04-23 14:39:19 -0700 | [diff] [blame] | 1725 | } finally { |
| 1726 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | 418d12d | 2011-12-13 15:38:03 -0800 | [diff] [blame] | 1727 | } |
| 1728 | } |
| 1729 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1730 | private class DropBoxNonMonotonicObserver implements NonMonotonicObserver<String> { |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 1731 | @Override |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1732 | public void foundNonMonotonic(NetworkStats left, int leftIndex, NetworkStats right, |
| 1733 | int rightIndex, String cookie) { |
Jeff Sharkey | b5a97e6 | 2018-05-22 11:35:29 -0600 | [diff] [blame] | 1734 | Log.w(TAG, "Found non-monotonic values; saving to dropbox"); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1735 | |
| 1736 | // record error for debugging |
| 1737 | final StringBuilder builder = new StringBuilder(); |
| 1738 | builder.append("found non-monotonic " + cookie + " values at left[" + leftIndex |
| 1739 | + "] - right[" + rightIndex + "]\n"); |
| 1740 | builder.append("left=").append(left).append('\n'); |
| 1741 | builder.append("right=").append(right).append('\n'); |
| 1742 | |
Jeff Sharkey | b5a97e6 | 2018-05-22 11:35:29 -0600 | [diff] [blame] | 1743 | mContext.getSystemService(DropBoxManager.class).addText(TAG_NETSTATS_ERROR, |
| 1744 | builder.toString()); |
| 1745 | } |
| 1746 | |
| 1747 | @Override |
| 1748 | public void foundNonMonotonic( |
| 1749 | NetworkStats stats, int statsIndex, String cookie) { |
| 1750 | Log.w(TAG, "Found non-monotonic values; saving to dropbox"); |
| 1751 | |
| 1752 | final StringBuilder builder = new StringBuilder(); |
| 1753 | builder.append("Found non-monotonic " + cookie + " values at [" + statsIndex + "]\n"); |
| 1754 | builder.append("stats=").append(stats).append('\n'); |
| 1755 | |
| 1756 | mContext.getSystemService(DropBoxManager.class).addText(TAG_NETSTATS_ERROR, |
| 1757 | builder.toString()); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 1758 | } |
| 1759 | } |
| 1760 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1761 | /** |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 1762 | * Default external settings that read from |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 1763 | * {@link android.provider.Settings.Global}. |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 1764 | */ |
| 1765 | private static class DefaultNetworkStatsSettings implements NetworkStatsSettings { |
| 1766 | private final ContentResolver mResolver; |
| 1767 | |
| 1768 | public DefaultNetworkStatsSettings(Context context) { |
| 1769 | mResolver = checkNotNull(context.getContentResolver()); |
| 1770 | // TODO: adjust these timings for production builds |
| 1771 | } |
| 1772 | |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1773 | private long getGlobalLong(String name, long def) { |
| 1774 | return Settings.Global.getLong(mResolver, name, def); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 1775 | } |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1776 | private boolean getGlobalBoolean(String name, boolean def) { |
Jeff Sharkey | 991d1b1 | 2011-09-14 19:31:04 -0700 | [diff] [blame] | 1777 | final int defInt = def ? 1 : 0; |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1778 | return Settings.Global.getInt(mResolver, name, defInt) != 0; |
Jeff Sharkey | 991d1b1 | 2011-09-14 19:31:04 -0700 | [diff] [blame] | 1779 | } |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 1780 | |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 1781 | @Override |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 1782 | public long getPollInterval() { |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1783 | return getGlobalLong(NETSTATS_POLL_INTERVAL, 30 * MINUTE_IN_MILLIS); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 1784 | } |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 1785 | @Override |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 1786 | public long getGlobalAlertBytes(long def) { |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1787 | return getGlobalLong(NETSTATS_GLOBAL_ALERT_BYTES, def); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1788 | } |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 1789 | @Override |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1790 | public boolean getSampleEnabled() { |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1791 | return getGlobalBoolean(NETSTATS_SAMPLE_ENABLED, true); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1792 | } |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 1793 | @Override |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 1794 | public boolean getAugmentEnabled() { |
| 1795 | return getGlobalBoolean(NETSTATS_AUGMENT_ENABLED, true); |
| 1796 | } |
| 1797 | @Override |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1798 | public Config getDevConfig() { |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1799 | return new Config(getGlobalLong(NETSTATS_DEV_BUCKET_DURATION, HOUR_IN_MILLIS), |
| 1800 | getGlobalLong(NETSTATS_DEV_ROTATE_AGE, 15 * DAY_IN_MILLIS), |
| 1801 | getGlobalLong(NETSTATS_DEV_DELETE_AGE, 90 * DAY_IN_MILLIS)); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1802 | } |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 1803 | @Override |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 1804 | public Config getXtConfig() { |
| 1805 | return getDevConfig(); |
| 1806 | } |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 1807 | @Override |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1808 | public Config getUidConfig() { |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1809 | return new Config(getGlobalLong(NETSTATS_UID_BUCKET_DURATION, 2 * HOUR_IN_MILLIS), |
| 1810 | getGlobalLong(NETSTATS_UID_ROTATE_AGE, 15 * DAY_IN_MILLIS), |
| 1811 | getGlobalLong(NETSTATS_UID_DELETE_AGE, 90 * DAY_IN_MILLIS)); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1812 | } |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 1813 | @Override |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1814 | public Config getUidTagConfig() { |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1815 | return new Config(getGlobalLong(NETSTATS_UID_TAG_BUCKET_DURATION, 2 * HOUR_IN_MILLIS), |
| 1816 | getGlobalLong(NETSTATS_UID_TAG_ROTATE_AGE, 5 * DAY_IN_MILLIS), |
| 1817 | getGlobalLong(NETSTATS_UID_TAG_DELETE_AGE, 15 * DAY_IN_MILLIS)); |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 1818 | } |
| 1819 | @Override |
| 1820 | public long getDevPersistBytes(long def) { |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1821 | return getGlobalLong(NETSTATS_DEV_PERSIST_BYTES, def); |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 1822 | } |
| 1823 | @Override |
| 1824 | public long getXtPersistBytes(long def) { |
| 1825 | return getDevPersistBytes(def); |
| 1826 | } |
| 1827 | @Override |
| 1828 | public long getUidPersistBytes(long def) { |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1829 | return getGlobalLong(NETSTATS_UID_PERSIST_BYTES, def); |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 1830 | } |
| 1831 | @Override |
| 1832 | public long getUidTagPersistBytes(long def) { |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1833 | return getGlobalLong(NETSTATS_UID_TAG_PERSIST_BYTES, def); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 1834 | } |
| 1835 | } |
Chenbo Feng | d3d9c4e | 2017-11-14 17:54:17 -0800 | [diff] [blame] | 1836 | |
| 1837 | private static int TYPE_RX_BYTES; |
| 1838 | private static int TYPE_RX_PACKETS; |
| 1839 | private static int TYPE_TX_BYTES; |
| 1840 | private static int TYPE_TX_PACKETS; |
| 1841 | private static int TYPE_TCP_RX_PACKETS; |
| 1842 | private static int TYPE_TCP_TX_PACKETS; |
| 1843 | |
Chenbo Feng | aedd6a3 | 2017-11-20 18:23:46 -0800 | [diff] [blame] | 1844 | private static native long nativeGetTotalStat(int type, boolean useBpfStats); |
| 1845 | private static native long nativeGetIfaceStat(String iface, int type, boolean useBpfStats); |
| 1846 | private static native long nativeGetUidStat(int uid, int type, boolean useBpfStats); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 1847 | } |