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