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