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