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