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