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