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 | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 21 | import static android.Manifest.permission.DUMP; |
Jeff Sharkey | 63d27a9 | 2011-08-03 17:04:22 -0700 | [diff] [blame] | 22 | import static android.Manifest.permission.MODIFY_NETWORK_ACCOUNTING; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 23 | import static android.Manifest.permission.READ_NETWORK_USAGE_HISTORY; |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 24 | import static android.content.Intent.ACTION_SHUTDOWN; |
| 25 | import static android.content.Intent.ACTION_UID_REMOVED; |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 26 | import static android.content.Intent.ACTION_USER_REMOVED; |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 27 | import static android.content.Intent.EXTRA_UID; |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 28 | import static android.net.ConnectivityManager.ACTION_TETHER_STATE_CHANGED; |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 29 | import static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE; |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 30 | import static android.net.ConnectivityManager.isNetworkTypeMobile; |
Jeff Sharkey | d4dd771 | 2012-03-16 11:11:54 -0700 | [diff] [blame] | 31 | import static android.net.NetworkIdentity.COMBINE_SUBTYPE_ENABLED; |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 32 | import static android.net.NetworkStats.IFACE_ALL; |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 33 | import static android.net.NetworkStats.SET_ALL; |
| 34 | import static android.net.NetworkStats.SET_DEFAULT; |
| 35 | import static android.net.NetworkStats.SET_FOREGROUND; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 36 | import static android.net.NetworkStats.TAG_NONE; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 37 | import static android.net.NetworkStats.UID_ALL; |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 38 | import static android.net.NetworkTemplate.buildTemplateMobileWildcard; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 39 | import static android.net.NetworkTemplate.buildTemplateWifiWildcard; |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 40 | import static android.net.TrafficStats.KB_IN_BYTES; |
Jeff Sharkey | 241dde2 | 2012-02-03 14:50:07 -0800 | [diff] [blame] | 41 | import static android.net.TrafficStats.MB_IN_BYTES; |
Jeff Sharkey | 023c05a | 2012-09-14 13:09:57 -0700 | [diff] [blame] | 42 | import static android.provider.Settings.Global.NETSTATS_DEV_BUCKET_DURATION; |
| 43 | import static android.provider.Settings.Global.NETSTATS_DEV_DELETE_AGE; |
| 44 | import static android.provider.Settings.Global.NETSTATS_DEV_PERSIST_BYTES; |
| 45 | import static android.provider.Settings.Global.NETSTATS_DEV_ROTATE_AGE; |
| 46 | import static android.provider.Settings.Global.NETSTATS_GLOBAL_ALERT_BYTES; |
| 47 | import static android.provider.Settings.Global.NETSTATS_POLL_INTERVAL; |
| 48 | import static android.provider.Settings.Global.NETSTATS_REPORT_XT_OVER_DEV; |
| 49 | import static android.provider.Settings.Global.NETSTATS_SAMPLE_ENABLED; |
| 50 | import static android.provider.Settings.Global.NETSTATS_TIME_CACHE_MAX_AGE; |
| 51 | import static android.provider.Settings.Global.NETSTATS_UID_BUCKET_DURATION; |
| 52 | import static android.provider.Settings.Global.NETSTATS_UID_DELETE_AGE; |
| 53 | import static android.provider.Settings.Global.NETSTATS_UID_PERSIST_BYTES; |
| 54 | import static android.provider.Settings.Global.NETSTATS_UID_ROTATE_AGE; |
| 55 | import static android.provider.Settings.Global.NETSTATS_UID_TAG_BUCKET_DURATION; |
| 56 | import static android.provider.Settings.Global.NETSTATS_UID_TAG_DELETE_AGE; |
| 57 | import static android.provider.Settings.Global.NETSTATS_UID_TAG_PERSIST_BYTES; |
| 58 | import static android.provider.Settings.Global.NETSTATS_UID_TAG_ROTATE_AGE; |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 59 | import static android.telephony.PhoneStateListener.LISTEN_DATA_CONNECTION_STATE; |
| 60 | import static android.telephony.PhoneStateListener.LISTEN_NONE; |
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; |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 65 | import static com.android.internal.util.ArrayUtils.appendElement; |
| 66 | import static com.android.internal.util.ArrayUtils.contains; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 67 | import static com.android.internal.util.Preconditions.checkNotNull; |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 68 | import static com.android.server.NetworkManagementService.LIMIT_GLOBAL_ALERT; |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 69 | import static com.android.server.NetworkManagementSocketTagger.resetKernelUidStats; |
| 70 | import static com.android.server.NetworkManagementSocketTagger.setKernelCounterSet; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 71 | |
| 72 | import android.app.AlarmManager; |
| 73 | import android.app.IAlarmManager; |
| 74 | import android.app.PendingIntent; |
| 75 | import android.content.BroadcastReceiver; |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 76 | import android.content.ContentResolver; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 77 | import android.content.Context; |
| 78 | import android.content.Intent; |
| 79 | import android.content.IntentFilter; |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 80 | import android.content.pm.ApplicationInfo; |
| 81 | import android.content.pm.PackageManager; |
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; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 87 | import android.net.NetworkIdentity; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 88 | import android.net.NetworkInfo; |
| 89 | import android.net.NetworkState; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 90 | import android.net.NetworkStats; |
Jeff Sharkey | 5a7bcf3 | 2012-01-10 17:24:44 -0800 | [diff] [blame] | 91 | import android.net.NetworkStats.NonMonotonicObserver; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 92 | import android.net.NetworkStatsHistory; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 93 | import android.net.NetworkTemplate; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 94 | import android.net.TrafficStats; |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 95 | import android.os.Binder; |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 96 | import android.os.DropBoxManager; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 97 | import android.os.Environment; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 98 | import android.os.Handler; |
Amith Yamasani | 450a16b | 2013-09-18 16:28:50 -0700 | [diff] [blame] | 99 | import android.os.HandlerThread; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 100 | import android.os.INetworkManagementService; |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 101 | import android.os.Message; |
Jeff Sharkey | 6248926 | 2011-07-17 12:53:28 -0700 | [diff] [blame] | 102 | import android.os.PowerManager; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 103 | import android.os.RemoteException; |
| 104 | import android.os.SystemClock; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 105 | import android.os.UserHandle; |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 106 | import android.provider.Settings; |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 107 | import android.provider.Settings.Global; |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 108 | import android.telephony.PhoneStateListener; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 109 | import android.telephony.TelephonyManager; |
Jeff Sharkey | 07b0dd9 | 2011-09-01 13:06:19 -0700 | [diff] [blame] | 110 | import android.util.EventLog; |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 111 | import android.util.Log; |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 112 | import android.util.MathUtils; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 113 | import android.util.NtpTrustedTime; |
| 114 | import android.util.Slog; |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 115 | import android.util.SparseIntArray; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 116 | import android.util.TrustedTime; |
| 117 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 118 | import com.android.internal.annotations.VisibleForTesting; |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 119 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 120 | import com.android.internal.util.FileRotator; |
| 121 | import com.android.internal.util.IndentingPrintWriter; |
Jeff Sharkey | 07b0dd9 | 2011-09-01 13:06:19 -0700 | [diff] [blame] | 122 | import com.android.server.EventLogTags; |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 123 | import com.android.server.connectivity.Tethering; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 124 | import com.google.android.collect.Maps; |
| 125 | |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 126 | import java.io.File; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 127 | import java.io.FileDescriptor; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 128 | import java.io.IOException; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 129 | import java.io.PrintWriter; |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 130 | import java.util.Arrays; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 131 | import java.util.HashMap; |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 132 | import java.util.HashSet; |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 133 | import java.util.List; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 134 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 135 | /** |
| 136 | * Collect and persist detailed network statistics, and provide this data to |
| 137 | * other system services. |
| 138 | */ |
| 139 | public class NetworkStatsService extends INetworkStatsService.Stub { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 140 | private static final String TAG = "NetworkStats"; |
Jeff Sharkey | 706498d | 2012-02-06 17:35:07 -0800 | [diff] [blame] | 141 | private static final boolean LOGV = false; |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 142 | |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 143 | private static final int MSG_PERFORM_POLL = 1; |
| 144 | private static final int MSG_UPDATE_IFACES = 2; |
Jeff Sharkey | 25ce9ed | 2012-02-02 13:07:47 -0800 | [diff] [blame] | 145 | private static final int MSG_REGISTER_GLOBAL_ALERT = 3; |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 146 | |
| 147 | /** Flags to control detail level of poll event. */ |
Jeff Sharkey | 905b589 | 2011-09-30 15:19:49 -0700 | [diff] [blame] | 148 | private static final int FLAG_PERSIST_NETWORK = 0x1; |
| 149 | private static final int FLAG_PERSIST_UID = 0x2; |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 150 | 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] | 151 | private static final int FLAG_PERSIST_FORCE = 0x100; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 152 | |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 153 | private static final String TAG_NETSTATS_ERROR = "netstats_error"; |
| 154 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 155 | private final Context mContext; |
| 156 | private final INetworkManagementService mNetworkManager; |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 157 | private final AlarmManager mAlarmManager; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 158 | private final TrustedTime mTime; |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 159 | private final TelephonyManager mTeleManager; |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 160 | private final NetworkStatsSettings mSettings; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 161 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 162 | private final File mSystemDir; |
| 163 | private final File mBaseDir; |
| 164 | |
Jeff Sharkey | 6248926 | 2011-07-17 12:53:28 -0700 | [diff] [blame] | 165 | private final PowerManager.WakeLock mWakeLock; |
| 166 | |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 167 | private IConnectivityManager mConnManager; |
| 168 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 169 | @VisibleForTesting |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 170 | public static final String ACTION_NETWORK_STATS_POLL = |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 171 | "com.android.server.action.NETWORK_STATS_POLL"; |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 172 | public static final String ACTION_NETWORK_STATS_UPDATED = |
| 173 | "com.android.server.action.NETWORK_STATS_UPDATED"; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 174 | |
| 175 | private PendingIntent mPollIntent; |
| 176 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 177 | private static final String PREFIX_DEV = "dev"; |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 178 | private static final String PREFIX_XT = "xt"; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 179 | private static final String PREFIX_UID = "uid"; |
| 180 | private static final String PREFIX_UID_TAG = "uid_tag"; |
| 181 | |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 182 | /** |
| 183 | * Settings that can be changed externally. |
| 184 | */ |
| 185 | public interface NetworkStatsSettings { |
| 186 | public long getPollInterval(); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 187 | public long getTimeCacheMaxAge(); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 188 | public boolean getSampleEnabled(); |
Jeff Sharkey | 70c7053 | 2012-05-16 14:51:19 -0700 | [diff] [blame] | 189 | public boolean getReportXtOverDev(); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 190 | |
| 191 | public static class Config { |
| 192 | public final long bucketDuration; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 193 | public final long rotateAgeMillis; |
| 194 | public final long deleteAgeMillis; |
| 195 | |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 196 | public Config(long bucketDuration, long rotateAgeMillis, long deleteAgeMillis) { |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 197 | this.bucketDuration = bucketDuration; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 198 | this.rotateAgeMillis = rotateAgeMillis; |
| 199 | this.deleteAgeMillis = deleteAgeMillis; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | public Config getDevConfig(); |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 204 | public Config getXtConfig(); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 205 | public Config getUidConfig(); |
| 206 | public Config getUidTagConfig(); |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 207 | |
| 208 | public long getGlobalAlertBytes(long def); |
| 209 | public long getDevPersistBytes(long def); |
| 210 | public long getXtPersistBytes(long def); |
| 211 | public long getUidPersistBytes(long def); |
| 212 | public long getUidTagPersistBytes(long def); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 213 | } |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 214 | |
| 215 | private final Object mStatsLock = new Object(); |
| 216 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 217 | /** Set of currently active ifaces. */ |
| 218 | private HashMap<String, NetworkIdentitySet> mActiveIfaces = Maps.newHashMap(); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 219 | /** Current default active iface. */ |
| 220 | private String mActiveIface; |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 221 | /** Set of any ifaces associated with mobile networks since boot. */ |
| 222 | private String[] mMobileIfaces = new String[0]; |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 223 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 224 | private final DropBoxNonMonotonicObserver mNonMonotonicObserver = |
| 225 | new DropBoxNonMonotonicObserver(); |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 226 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 227 | private NetworkStatsRecorder mDevRecorder; |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 228 | private NetworkStatsRecorder mXtRecorder; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 229 | private NetworkStatsRecorder mUidRecorder; |
| 230 | private NetworkStatsRecorder mUidTagRecorder; |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 231 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 232 | /** Cached {@link #mDevRecorder} stats. */ |
| 233 | private NetworkStatsCollection mDevStatsCached; |
Jeff Sharkey | 70c7053 | 2012-05-16 14:51:19 -0700 | [diff] [blame] | 234 | /** Cached {@link #mXtRecorder} stats. */ |
| 235 | private NetworkStatsCollection mXtStatsCached; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 236 | |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 237 | /** Current counter sets for each UID. */ |
| 238 | private SparseIntArray mActiveUidCounterSet = new SparseIntArray(); |
| 239 | |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 240 | /** Data layer operation counters for splicing into other structures. */ |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 241 | private NetworkStats mUidOperations = new NetworkStats(0L, 10); |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 242 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 243 | private final Handler mHandler; |
| 244 | |
Jeff Sharkey | 6341fce | 2012-03-06 19:59:57 -0800 | [diff] [blame] | 245 | private boolean mSystemReady; |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 246 | private long mPersistThreshold = 2 * MB_IN_BYTES; |
| 247 | private long mGlobalAlertBytes; |
Jeff Sharkey | 6341fce | 2012-03-06 19:59:57 -0800 | [diff] [blame] | 248 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 249 | public NetworkStatsService( |
| 250 | Context context, INetworkManagementService networkManager, IAlarmManager alarmManager) { |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 251 | this(context, networkManager, alarmManager, NtpTrustedTime.getInstance(context), |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 252 | getDefaultSystemDir(), new DefaultNetworkStatsSettings(context)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 255 | private static File getDefaultSystemDir() { |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 256 | return new File(Environment.getDataDirectory(), "system"); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | public NetworkStatsService(Context context, INetworkManagementService networkManager, |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 260 | IAlarmManager alarmManager, TrustedTime time, File systemDir, |
| 261 | NetworkStatsSettings settings) { |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 262 | mContext = checkNotNull(context, "missing Context"); |
| 263 | mNetworkManager = checkNotNull(networkManager, "missing INetworkManagementService"); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 264 | mTime = checkNotNull(time, "missing TrustedTime"); |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 265 | mTeleManager = checkNotNull(TelephonyManager.getDefault(), "missing TelephonyManager"); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 266 | mSettings = checkNotNull(settings, "missing NetworkStatsSettings"); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 267 | mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 268 | |
Jeff Sharkey | 6248926 | 2011-07-17 12:53:28 -0700 | [diff] [blame] | 269 | final PowerManager powerManager = (PowerManager) context.getSystemService( |
| 270 | Context.POWER_SERVICE); |
| 271 | mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); |
| 272 | |
Amith Yamasani | 450a16b | 2013-09-18 16:28:50 -0700 | [diff] [blame] | 273 | HandlerThread thread = new HandlerThread(TAG); |
| 274 | thread.start(); |
| 275 | mHandler = new Handler(thread.getLooper(), mHandlerCallback); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 276 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 277 | mSystemDir = checkNotNull(systemDir); |
| 278 | mBaseDir = new File(systemDir, "netstats"); |
| 279 | mBaseDir.mkdirs(); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 282 | public void bindConnectivityManager(IConnectivityManager connManager) { |
| 283 | mConnManager = checkNotNull(connManager, "missing IConnectivityManager"); |
| 284 | } |
| 285 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 286 | public void systemReady() { |
Jeff Sharkey | 6341fce | 2012-03-06 19:59:57 -0800 | [diff] [blame] | 287 | mSystemReady = true; |
| 288 | |
Jeff Sharkey | 418d12d | 2011-12-13 15:38:03 -0800 | [diff] [blame] | 289 | if (!isBandwidthControlEnabled()) { |
| 290 | Slog.w(TAG, "bandwidth controls disabled, unable to track stats"); |
| 291 | return; |
| 292 | } |
| 293 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 294 | // create data recorders along with historical rotators |
| 295 | mDevRecorder = buildRecorder(PREFIX_DEV, mSettings.getDevConfig(), false); |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 296 | mXtRecorder = buildRecorder(PREFIX_XT, mSettings.getXtConfig(), false); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 297 | mUidRecorder = buildRecorder(PREFIX_UID, mSettings.getUidConfig(), false); |
| 298 | mUidTagRecorder = buildRecorder(PREFIX_UID_TAG, mSettings.getUidTagConfig(), true); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 299 | |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 300 | updatePersistThresholds(); |
| 301 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 302 | synchronized (mStatsLock) { |
| 303 | // upgrade any legacy stats, migrating them to rotated files |
| 304 | maybeUpgradeLegacyStatsLocked(); |
| 305 | |
| 306 | // read historical network stats from disk, since policy service |
| 307 | // might need them right away. |
| 308 | mDevStatsCached = mDevRecorder.getOrLoadCompleteLocked(); |
Jeff Sharkey | 70c7053 | 2012-05-16 14:51:19 -0700 | [diff] [blame] | 309 | mXtStatsCached = mXtRecorder.getOrLoadCompleteLocked(); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 310 | |
| 311 | // bootstrap initial stats to prevent double-counting later |
| 312 | bootstrapStatsLocked(); |
| 313 | } |
Jeff Sharkey | 3359aca | 2011-11-08 18:08:48 -0800 | [diff] [blame] | 314 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 315 | // watch for network interfaces to be claimed |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 316 | final IntentFilter connFilter = new IntentFilter(CONNECTIVITY_ACTION_IMMEDIATE); |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 317 | mContext.registerReceiver(mConnReceiver, connFilter, CONNECTIVITY_INTERNAL, mHandler); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 318 | |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 319 | // watch for tethering changes |
| 320 | final IntentFilter tetherFilter = new IntentFilter(ACTION_TETHER_STATE_CHANGED); |
| 321 | mContext.registerReceiver(mTetherReceiver, tetherFilter, CONNECTIVITY_INTERNAL, mHandler); |
| 322 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 323 | // listen for periodic polling events |
| 324 | final IntentFilter pollFilter = new IntentFilter(ACTION_NETWORK_STATS_POLL); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 325 | mContext.registerReceiver(mPollReceiver, pollFilter, READ_NETWORK_USAGE_HISTORY, mHandler); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 326 | |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 327 | // listen for uid removal to clean stats |
| 328 | final IntentFilter removedFilter = new IntentFilter(ACTION_UID_REMOVED); |
| 329 | mContext.registerReceiver(mRemovedReceiver, removedFilter, null, mHandler); |
| 330 | |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 331 | // listen for user changes to clean stats |
| 332 | final IntentFilter userFilter = new IntentFilter(ACTION_USER_REMOVED); |
| 333 | mContext.registerReceiver(mUserReceiver, userFilter, null, mHandler); |
| 334 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 335 | // persist stats during clean shutdown |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 336 | final IntentFilter shutdownFilter = new IntentFilter(ACTION_SHUTDOWN); |
| 337 | mContext.registerReceiver(mShutdownReceiver, shutdownFilter); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 338 | |
| 339 | try { |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 340 | mNetworkManager.registerObserver(mAlertObserver); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 341 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 342 | // ignored; service lives in system_server |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 343 | } |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 344 | |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 345 | // watch for networkType changes that aren't broadcast through |
| 346 | // CONNECTIVITY_ACTION_IMMEDIATE above. |
Jeff Sharkey | d4dd771 | 2012-03-16 11:11:54 -0700 | [diff] [blame] | 347 | if (!COMBINE_SUBTYPE_ENABLED) { |
| 348 | mTeleManager.listen(mPhoneListener, LISTEN_DATA_CONNECTION_STATE); |
| 349 | } |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 350 | |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 351 | registerPollAlarmLocked(); |
| 352 | registerGlobalAlert(); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 353 | } |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 354 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 355 | private NetworkStatsRecorder buildRecorder( |
| 356 | String prefix, NetworkStatsSettings.Config config, boolean includeTags) { |
Jeff Sharkey | 6de357e | 2012-05-09 13:33:52 -0700 | [diff] [blame] | 357 | final DropBoxManager dropBox = (DropBoxManager) mContext.getSystemService( |
| 358 | Context.DROPBOX_SERVICE); |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 359 | return new NetworkStatsRecorder(new FileRotator( |
| 360 | mBaseDir, prefix, config.rotateAgeMillis, config.deleteAgeMillis), |
Jeff Sharkey | 6de357e | 2012-05-09 13:33:52 -0700 | [diff] [blame] | 361 | mNonMonotonicObserver, dropBox, prefix, config.bucketDuration, includeTags); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 364 | private void shutdownLocked() { |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 365 | mContext.unregisterReceiver(mConnReceiver); |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 366 | mContext.unregisterReceiver(mTetherReceiver); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 367 | mContext.unregisterReceiver(mPollReceiver); |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 368 | mContext.unregisterReceiver(mRemovedReceiver); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 369 | mContext.unregisterReceiver(mShutdownReceiver); |
| 370 | |
Jeff Sharkey | d4dd771 | 2012-03-16 11:11:54 -0700 | [diff] [blame] | 371 | if (!COMBINE_SUBTYPE_ENABLED) { |
| 372 | mTeleManager.listen(mPhoneListener, LISTEN_NONE); |
| 373 | } |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 374 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 375 | final long currentTime = mTime.hasCache() ? mTime.currentTimeMillis() |
| 376 | : System.currentTimeMillis(); |
| 377 | |
| 378 | // persist any pending stats |
| 379 | mDevRecorder.forcePersistLocked(currentTime); |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 380 | mXtRecorder.forcePersistLocked(currentTime); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 381 | mUidRecorder.forcePersistLocked(currentTime); |
| 382 | mUidTagRecorder.forcePersistLocked(currentTime); |
| 383 | |
| 384 | mDevRecorder = null; |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 385 | mXtRecorder = null; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 386 | mUidRecorder = null; |
| 387 | mUidTagRecorder = null; |
| 388 | |
| 389 | mDevStatsCached = null; |
Jeff Sharkey | 70c7053 | 2012-05-16 14:51:19 -0700 | [diff] [blame] | 390 | mXtStatsCached = null; |
Jeff Sharkey | 6341fce | 2012-03-06 19:59:57 -0800 | [diff] [blame] | 391 | |
| 392 | mSystemReady = false; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | private void maybeUpgradeLegacyStatsLocked() { |
| 396 | File file; |
| 397 | try { |
| 398 | file = new File(mSystemDir, "netstats.bin"); |
| 399 | if (file.exists()) { |
| 400 | mDevRecorder.importLegacyNetworkLocked(file); |
| 401 | file.delete(); |
| 402 | } |
| 403 | |
| 404 | file = new File(mSystemDir, "netstats_xt.bin"); |
| 405 | if (file.exists()) { |
| 406 | file.delete(); |
| 407 | } |
| 408 | |
| 409 | file = new File(mSystemDir, "netstats_uid.bin"); |
| 410 | if (file.exists()) { |
| 411 | mUidRecorder.importLegacyUidLocked(file); |
| 412 | mUidTagRecorder.importLegacyUidLocked(file); |
| 413 | file.delete(); |
| 414 | } |
| 415 | } catch (IOException e) { |
| 416 | Log.wtf(TAG, "problem during legacy upgrade", e); |
Jeff Sharkey | e4984be | 2013-09-10 21:03:27 -0700 | [diff] [blame] | 417 | } catch (OutOfMemoryError e) { |
| 418 | Log.wtf(TAG, "problem during legacy upgrade", e); |
Jeff Sharkey | c506ff6 | 2011-11-17 11:59:29 -0800 | [diff] [blame] | 419 | } |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 422 | /** |
| 423 | * Clear any existing {@link #ACTION_NETWORK_STATS_POLL} alarms, and |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 424 | * reschedule based on current {@link NetworkStatsSettings#getPollInterval()}. |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 425 | */ |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 426 | private void registerPollAlarmLocked() { |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 427 | if (mPollIntent != null) { |
| 428 | mAlarmManager.cancel(mPollIntent); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 429 | } |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 430 | |
| 431 | mPollIntent = PendingIntent.getBroadcast( |
| 432 | mContext, 0, new Intent(ACTION_NETWORK_STATS_POLL), 0); |
| 433 | |
| 434 | final long currentRealtime = SystemClock.elapsedRealtime(); |
| 435 | mAlarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, currentRealtime, |
| 436 | mSettings.getPollInterval(), mPollIntent); |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 437 | } |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 438 | |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 439 | /** |
| 440 | * Register for a global alert that is delivered through |
| 441 | * {@link INetworkManagementEventObserver} once a threshold amount of data |
| 442 | * has been transferred. |
| 443 | */ |
| 444 | private void registerGlobalAlert() { |
| 445 | try { |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 446 | mNetworkManager.setGlobalAlert(mGlobalAlertBytes); |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 447 | } catch (IllegalStateException e) { |
| 448 | Slog.w(TAG, "problem registering for global alert: " + e); |
| 449 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 450 | // ignored; service lives in system_server |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 451 | } |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | @Override |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 455 | public INetworkStatsSession openSession() { |
| 456 | mContext.enforceCallingOrSelfPermission(READ_NETWORK_USAGE_HISTORY, TAG); |
Jeff Sharkey | 4190a04 | 2012-04-21 15:36:48 -0700 | [diff] [blame] | 457 | assertBandwidthControlEnabled(); |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 458 | |
| 459 | // return an IBinder which holds strong references to any loaded stats |
| 460 | // for its lifetime; when caller closes only weak references remain. |
| 461 | |
| 462 | return new INetworkStatsSession.Stub() { |
| 463 | private NetworkStatsCollection mUidComplete; |
| 464 | private NetworkStatsCollection mUidTagComplete; |
| 465 | |
| 466 | private NetworkStatsCollection getUidComplete() { |
| 467 | if (mUidComplete == null) { |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 468 | synchronized (mStatsLock) { |
| 469 | mUidComplete = mUidRecorder.getOrLoadCompleteLocked(); |
| 470 | } |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 471 | } |
| 472 | return mUidComplete; |
| 473 | } |
| 474 | |
| 475 | private NetworkStatsCollection getUidTagComplete() { |
| 476 | if (mUidTagComplete == null) { |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 477 | synchronized (mStatsLock) { |
| 478 | mUidTagComplete = mUidTagRecorder.getOrLoadCompleteLocked(); |
| 479 | } |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 480 | } |
| 481 | return mUidTagComplete; |
| 482 | } |
| 483 | |
| 484 | @Override |
| 485 | public NetworkStats getSummaryForNetwork( |
| 486 | NetworkTemplate template, long start, long end) { |
Jeff Sharkey | 70c7053 | 2012-05-16 14:51:19 -0700 | [diff] [blame] | 487 | return internalGetSummaryForNetwork(template, start, end); |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | @Override |
| 491 | public NetworkStatsHistory getHistoryForNetwork(NetworkTemplate template, int fields) { |
Jeff Sharkey | 70c7053 | 2012-05-16 14:51:19 -0700 | [diff] [blame] | 492 | return internalGetHistoryForNetwork(template, fields); |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | @Override |
| 496 | public NetworkStats getSummaryForAllUid( |
| 497 | NetworkTemplate template, long start, long end, boolean includeTags) { |
| 498 | final NetworkStats stats = getUidComplete().getSummary(template, start, end); |
| 499 | if (includeTags) { |
| 500 | final NetworkStats tagStats = getUidTagComplete() |
| 501 | .getSummary(template, start, end); |
| 502 | stats.combineAllValues(tagStats); |
| 503 | } |
| 504 | return stats; |
| 505 | } |
| 506 | |
| 507 | @Override |
| 508 | public NetworkStatsHistory getHistoryForUid( |
| 509 | NetworkTemplate template, int uid, int set, int tag, int fields) { |
| 510 | if (tag == TAG_NONE) { |
| 511 | return getUidComplete().getHistory(template, uid, set, tag, fields); |
| 512 | } else { |
| 513 | return getUidTagComplete().getHistory(template, uid, set, tag, fields); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | @Override |
| 518 | public void close() { |
| 519 | mUidComplete = null; |
| 520 | mUidTagComplete = null; |
| 521 | } |
| 522 | }; |
Jeff Sharkey | 905b589 | 2011-09-30 15:19:49 -0700 | [diff] [blame] | 523 | } |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 524 | |
Jeff Sharkey | 70c7053 | 2012-05-16 14:51:19 -0700 | [diff] [blame] | 525 | /** |
| 526 | * Return network summary, splicing between {@link #mDevStatsCached} |
| 527 | * and {@link #mXtStatsCached} when appropriate. |
| 528 | */ |
| 529 | private NetworkStats internalGetSummaryForNetwork( |
| 530 | NetworkTemplate template, long start, long end) { |
| 531 | if (!mSettings.getReportXtOverDev()) { |
| 532 | // shortcut when XT reporting disabled |
| 533 | return mDevStatsCached.getSummary(template, start, end); |
| 534 | } |
| 535 | |
| 536 | // splice stats between DEV and XT, switching over from DEV to XT at |
| 537 | // first atomic bucket. |
| 538 | final long firstAtomicBucket = mXtStatsCached.getFirstAtomicBucketMillis(); |
| 539 | final NetworkStats dev = mDevStatsCached.getSummary( |
| 540 | template, Math.min(start, firstAtomicBucket), Math.min(end, firstAtomicBucket)); |
| 541 | final NetworkStats xt = mXtStatsCached.getSummary( |
| 542 | template, Math.max(start, firstAtomicBucket), Math.max(end, firstAtomicBucket)); |
| 543 | |
| 544 | xt.combineAllValues(dev); |
| 545 | return xt; |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * Return network history, splicing between {@link #mDevStatsCached} |
| 550 | * and {@link #mXtStatsCached} when appropriate. |
| 551 | */ |
| 552 | private NetworkStatsHistory internalGetHistoryForNetwork(NetworkTemplate template, int fields) { |
| 553 | if (!mSettings.getReportXtOverDev()) { |
| 554 | // shortcut when XT reporting disabled |
| 555 | return mDevStatsCached.getHistory(template, UID_ALL, SET_ALL, TAG_NONE, fields); |
| 556 | } |
| 557 | |
| 558 | // splice stats between DEV and XT, switching over from DEV to XT at |
| 559 | // first atomic bucket. |
| 560 | final long firstAtomicBucket = mXtStatsCached.getFirstAtomicBucketMillis(); |
| 561 | final NetworkStatsHistory dev = mDevStatsCached.getHistory( |
| 562 | template, UID_ALL, SET_ALL, TAG_NONE, fields, Long.MIN_VALUE, firstAtomicBucket); |
| 563 | final NetworkStatsHistory xt = mXtStatsCached.getHistory( |
| 564 | template, UID_ALL, SET_ALL, TAG_NONE, fields, firstAtomicBucket, Long.MAX_VALUE); |
| 565 | |
| 566 | xt.recordEntireHistory(dev); |
| 567 | return xt; |
| 568 | } |
| 569 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 570 | @Override |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 571 | public long getNetworkTotalBytes(NetworkTemplate template, long start, long end) { |
| 572 | mContext.enforceCallingOrSelfPermission(READ_NETWORK_USAGE_HISTORY, TAG); |
Jeff Sharkey | 4190a04 | 2012-04-21 15:36:48 -0700 | [diff] [blame] | 573 | assertBandwidthControlEnabled(); |
Jeff Sharkey | 70c7053 | 2012-05-16 14:51:19 -0700 | [diff] [blame] | 574 | return internalGetSummaryForNetwork(template, start, end).getTotalBytes(); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 577 | @Override |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 578 | public NetworkStats getDataLayerSnapshotForUid(int uid) throws RemoteException { |
| 579 | if (Binder.getCallingUid() != uid) { |
| 580 | mContext.enforceCallingOrSelfPermission(ACCESS_NETWORK_STATE, TAG); |
| 581 | } |
Jeff Sharkey | 4190a04 | 2012-04-21 15:36:48 -0700 | [diff] [blame] | 582 | assertBandwidthControlEnabled(); |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 583 | |
| 584 | // TODO: switch to data layer stats once kernel exports |
| 585 | // for now, read network layer stats and flatten across all ifaces |
Jeff Sharkey | 4529bb6 | 2011-12-14 10:31:54 -0800 | [diff] [blame] | 586 | final long token = Binder.clearCallingIdentity(); |
| 587 | final NetworkStats networkLayer; |
| 588 | try { |
| 589 | networkLayer = mNetworkManager.getNetworkStatsUidDetail(uid); |
| 590 | } finally { |
| 591 | Binder.restoreCallingIdentity(token); |
| 592 | } |
| 593 | |
Jeff Sharkey | 21a5478 | 2012-04-09 10:27:55 -0700 | [diff] [blame] | 594 | // splice in operation counts |
| 595 | networkLayer.spliceOperationsFrom(mUidOperations); |
| 596 | |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 597 | final NetworkStats dataLayer = new NetworkStats( |
| 598 | networkLayer.getElapsedRealtime(), networkLayer.size()); |
| 599 | |
| 600 | NetworkStats.Entry entry = null; |
| 601 | for (int i = 0; i < networkLayer.size(); i++) { |
| 602 | entry = networkLayer.getValues(i, entry); |
| 603 | entry.iface = IFACE_ALL; |
| 604 | dataLayer.combineValues(entry); |
| 605 | } |
| 606 | |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 607 | return dataLayer; |
| 608 | } |
| 609 | |
| 610 | @Override |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 611 | public String[] getMobileIfaces() { |
| 612 | return mMobileIfaces; |
| 613 | } |
| 614 | |
| 615 | @Override |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 616 | public void incrementOperationCount(int uid, int tag, int operationCount) { |
| 617 | if (Binder.getCallingUid() != uid) { |
| 618 | mContext.enforceCallingOrSelfPermission(MODIFY_NETWORK_ACCOUNTING, TAG); |
| 619 | } |
| 620 | |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 621 | if (operationCount < 0) { |
| 622 | throw new IllegalArgumentException("operation count can only be incremented"); |
| 623 | } |
| 624 | if (tag == TAG_NONE) { |
| 625 | throw new IllegalArgumentException("operation count must have specific tag"); |
| 626 | } |
| 627 | |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 628 | synchronized (mStatsLock) { |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 629 | final int set = mActiveUidCounterSet.get(uid, SET_DEFAULT); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 630 | mUidOperations.combineValues( |
| 631 | mActiveIface, uid, set, tag, 0L, 0L, 0L, 0L, operationCount); |
| 632 | mUidOperations.combineValues( |
| 633 | mActiveIface, uid, set, TAG_NONE, 0L, 0L, 0L, 0L, operationCount); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
| 637 | @Override |
| 638 | public void setUidForeground(int uid, boolean uidForeground) { |
| 639 | mContext.enforceCallingOrSelfPermission(MODIFY_NETWORK_ACCOUNTING, TAG); |
| 640 | |
| 641 | synchronized (mStatsLock) { |
| 642 | final int set = uidForeground ? SET_FOREGROUND : SET_DEFAULT; |
| 643 | final int oldSet = mActiveUidCounterSet.get(uid, SET_DEFAULT); |
| 644 | if (oldSet != set) { |
| 645 | mActiveUidCounterSet.put(uid, set); |
| 646 | setKernelCounterSet(uid, set); |
| 647 | } |
Jeff Sharkey | a63ba59 | 2011-07-19 23:47:12 -0700 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | |
| 651 | @Override |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 652 | public void forceUpdate() { |
| 653 | mContext.enforceCallingOrSelfPermission(READ_NETWORK_USAGE_HISTORY, TAG); |
Jeff Sharkey | 4190a04 | 2012-04-21 15:36:48 -0700 | [diff] [blame] | 654 | assertBandwidthControlEnabled(); |
Jeff Sharkey | e630f7b | 2012-01-31 17:12:53 -0800 | [diff] [blame] | 655 | |
| 656 | final long token = Binder.clearCallingIdentity(); |
| 657 | try { |
| 658 | performPoll(FLAG_PERSIST_ALL); |
| 659 | } finally { |
| 660 | Binder.restoreCallingIdentity(token); |
| 661 | } |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 662 | } |
| 663 | |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 664 | @Override |
| 665 | public void advisePersistThreshold(long thresholdBytes) { |
| 666 | mContext.enforceCallingOrSelfPermission(MODIFY_NETWORK_ACCOUNTING, TAG); |
| 667 | assertBandwidthControlEnabled(); |
| 668 | |
| 669 | // clamp threshold into safe range |
| 670 | mPersistThreshold = MathUtils.constrain(thresholdBytes, 128 * KB_IN_BYTES, 2 * MB_IN_BYTES); |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 671 | if (LOGV) { |
| 672 | Slog.v(TAG, "advisePersistThreshold() given " + thresholdBytes + ", clamped to " |
| 673 | + mPersistThreshold); |
| 674 | } |
| 675 | |
Jeff Sharkey | 20f5c3d | 2012-05-09 19:59:07 -0700 | [diff] [blame] | 676 | // update and persist if beyond new thresholds |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 677 | final long currentTime = mTime.hasCache() ? mTime.currentTimeMillis() |
| 678 | : System.currentTimeMillis(); |
Jeff Sharkey | 5801597 | 2012-05-07 11:08:49 -0700 | [diff] [blame] | 679 | synchronized (mStatsLock) { |
Jeff Sharkey | 20f5c3d | 2012-05-09 19:59:07 -0700 | [diff] [blame] | 680 | if (!mSystemReady) return; |
| 681 | |
| 682 | updatePersistThresholds(); |
| 683 | |
Jeff Sharkey | 5801597 | 2012-05-07 11:08:49 -0700 | [diff] [blame] | 684 | mDevRecorder.maybePersistLocked(currentTime); |
| 685 | mXtRecorder.maybePersistLocked(currentTime); |
| 686 | mUidRecorder.maybePersistLocked(currentTime); |
| 687 | mUidTagRecorder.maybePersistLocked(currentTime); |
| 688 | } |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 689 | |
| 690 | // re-arm global alert |
| 691 | registerGlobalAlert(); |
| 692 | } |
| 693 | |
| 694 | /** |
| 695 | * Update {@link NetworkStatsRecorder} and {@link #mGlobalAlertBytes} to |
| 696 | * reflect current {@link #mPersistThreshold} value. Always defers to |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 697 | * {@link Global} values when defined. |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 698 | */ |
| 699 | private void updatePersistThresholds() { |
| 700 | mDevRecorder.setPersistThreshold(mSettings.getDevPersistBytes(mPersistThreshold)); |
| 701 | mXtRecorder.setPersistThreshold(mSettings.getXtPersistBytes(mPersistThreshold)); |
| 702 | mUidRecorder.setPersistThreshold(mSettings.getUidPersistBytes(mPersistThreshold)); |
| 703 | mUidTagRecorder.setPersistThreshold(mSettings.getUidTagPersistBytes(mPersistThreshold)); |
| 704 | mGlobalAlertBytes = mSettings.getGlobalAlertBytes(mPersistThreshold); |
| 705 | } |
| 706 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 707 | /** |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 708 | * Receiver that watches for {@link IConnectivityManager} to claim network |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 709 | * interfaces. Used to associate {@link TelephonyManager#getSubscriberId()} |
| 710 | * with mobile interfaces. |
| 711 | */ |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 712 | private BroadcastReceiver mConnReceiver = new BroadcastReceiver() { |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 713 | @Override |
| 714 | public void onReceive(Context context, Intent intent) { |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 715 | // on background handler thread, and verified CONNECTIVITY_INTERNAL |
| 716 | // permission above. |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 717 | updateIfaces(); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 718 | } |
| 719 | }; |
| 720 | |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 721 | /** |
| 722 | * Receiver that watches for {@link Tethering} to claim interface pairs. |
| 723 | */ |
| 724 | private BroadcastReceiver mTetherReceiver = new BroadcastReceiver() { |
| 725 | @Override |
| 726 | public void onReceive(Context context, Intent intent) { |
| 727 | // on background handler thread, and verified CONNECTIVITY_INTERNAL |
| 728 | // permission above. |
Jeff Sharkey | 1f0b13b | 2011-09-18 13:30:23 -0700 | [diff] [blame] | 729 | performPoll(FLAG_PERSIST_NETWORK); |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 730 | } |
| 731 | }; |
| 732 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 733 | private BroadcastReceiver mPollReceiver = new BroadcastReceiver() { |
| 734 | @Override |
| 735 | public void onReceive(Context context, Intent intent) { |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 736 | // on background handler thread, and verified UPDATE_DEVICE_STATS |
| 737 | // permission above. |
Jeff Sharkey | 1f0b13b | 2011-09-18 13:30:23 -0700 | [diff] [blame] | 738 | performPoll(FLAG_PERSIST_ALL); |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 739 | |
| 740 | // verify that we're watching global alert |
| 741 | registerGlobalAlert(); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 742 | } |
| 743 | }; |
| 744 | |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 745 | private BroadcastReceiver mRemovedReceiver = new BroadcastReceiver() { |
| 746 | @Override |
| 747 | public void onReceive(Context context, Intent intent) { |
| 748 | // on background handler thread, and UID_REMOVED is protected |
| 749 | // broadcast. |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 750 | |
| 751 | final int uid = intent.getIntExtra(EXTRA_UID, -1); |
| 752 | if (uid == -1) return; |
| 753 | |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 754 | synchronized (mStatsLock) { |
Jeff Sharkey | 6248926 | 2011-07-17 12:53:28 -0700 | [diff] [blame] | 755 | mWakeLock.acquire(); |
| 756 | try { |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 757 | removeUidsLocked(uid); |
| 758 | } finally { |
| 759 | mWakeLock.release(); |
| 760 | } |
| 761 | } |
| 762 | } |
| 763 | }; |
| 764 | |
| 765 | private BroadcastReceiver mUserReceiver = new BroadcastReceiver() { |
| 766 | @Override |
| 767 | public void onReceive(Context context, Intent intent) { |
| 768 | // On background handler thread, and USER_REMOVED is protected |
| 769 | // broadcast. |
| 770 | |
| 771 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 772 | if (userId == -1) return; |
| 773 | |
| 774 | synchronized (mStatsLock) { |
| 775 | mWakeLock.acquire(); |
| 776 | try { |
| 777 | removeUserLocked(userId); |
Jeff Sharkey | 6248926 | 2011-07-17 12:53:28 -0700 | [diff] [blame] | 778 | } finally { |
| 779 | mWakeLock.release(); |
| 780 | } |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | }; |
| 784 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 785 | private BroadcastReceiver mShutdownReceiver = new BroadcastReceiver() { |
| 786 | @Override |
| 787 | public void onReceive(Context context, Intent intent) { |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 788 | // SHUTDOWN is protected broadcast. |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 789 | synchronized (mStatsLock) { |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 790 | shutdownLocked(); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 791 | } |
| 792 | } |
| 793 | }; |
| 794 | |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 795 | /** |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 796 | * Observer that watches for {@link INetworkManagementService} alerts. |
| 797 | */ |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 798 | private INetworkManagementEventObserver mAlertObserver = new BaseNetworkObserver() { |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 799 | @Override |
| 800 | public void limitReached(String limitName, String iface) { |
| 801 | // only someone like NMS should be calling us |
| 802 | mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG); |
| 803 | |
| 804 | if (LIMIT_GLOBAL_ALERT.equals(limitName)) { |
| 805 | // kick off background poll to collect network stats; UID stats |
| 806 | // are handled during normal polling interval. |
Jeff Sharkey | 1f0b13b | 2011-09-18 13:30:23 -0700 | [diff] [blame] | 807 | final int flags = FLAG_PERSIST_NETWORK; |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 808 | mHandler.obtainMessage(MSG_PERFORM_POLL, flags, 0).sendToTarget(); |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 809 | |
| 810 | // re-arm global alert for next update |
Jeff Sharkey | 25ce9ed | 2012-02-02 13:07:47 -0800 | [diff] [blame] | 811 | mHandler.obtainMessage(MSG_REGISTER_GLOBAL_ALERT).sendToTarget(); |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | }; |
| 815 | |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 816 | private int mLastPhoneState = TelephonyManager.DATA_UNKNOWN; |
| 817 | private int mLastPhoneNetworkType = TelephonyManager.NETWORK_TYPE_UNKNOWN; |
| 818 | |
| 819 | /** |
| 820 | * Receiver that watches for {@link TelephonyManager} changes, such as |
| 821 | * transitioning between network types. |
| 822 | */ |
| 823 | private PhoneStateListener mPhoneListener = new PhoneStateListener() { |
| 824 | @Override |
| 825 | public void onDataConnectionStateChanged(int state, int networkType) { |
| 826 | final boolean stateChanged = state != mLastPhoneState; |
| 827 | final boolean networkTypeChanged = networkType != mLastPhoneNetworkType; |
| 828 | |
| 829 | if (networkTypeChanged && !stateChanged) { |
| 830 | // networkType changed without a state change, which means we |
| 831 | // need to roll our own update. delay long enough for |
| 832 | // ConnectivityManager to process. |
| 833 | // TODO: add direct event to ConnectivityService instead of |
| 834 | // relying on this delay. |
| 835 | if (LOGV) Slog.v(TAG, "triggering delayed updateIfaces()"); |
| 836 | mHandler.sendMessageDelayed( |
| 837 | mHandler.obtainMessage(MSG_UPDATE_IFACES), SECOND_IN_MILLIS); |
| 838 | } |
| 839 | |
| 840 | mLastPhoneState = state; |
| 841 | mLastPhoneNetworkType = networkType; |
| 842 | } |
| 843 | }; |
| 844 | |
| 845 | private void updateIfaces() { |
| 846 | synchronized (mStatsLock) { |
| 847 | mWakeLock.acquire(); |
| 848 | try { |
| 849 | updateIfacesLocked(); |
| 850 | } finally { |
| 851 | mWakeLock.release(); |
| 852 | } |
| 853 | } |
| 854 | } |
| 855 | |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 856 | /** |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 857 | * Inspect all current {@link NetworkState} to derive mapping from {@code |
| 858 | * iface} to {@link NetworkStatsHistory}. When multiple {@link NetworkInfo} |
| 859 | * 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] | 860 | * {@link NetworkIdentitySet}. |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 861 | */ |
| 862 | private void updateIfacesLocked() { |
Jeff Sharkey | 6341fce | 2012-03-06 19:59:57 -0800 | [diff] [blame] | 863 | if (!mSystemReady) return; |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 864 | if (LOGV) Slog.v(TAG, "updateIfacesLocked()"); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 865 | |
| 866 | // take one last stats snapshot before updating iface mapping. this |
| 867 | // isn't perfect, since the kernel may already be counting traffic from |
| 868 | // the updated network. |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 869 | |
Jeff Sharkey | 1f0b13b | 2011-09-18 13:30:23 -0700 | [diff] [blame] | 870 | // poll, but only persist network stats to keep codepath fast. UID stats |
| 871 | // will be persisted during next alarm poll event. |
| 872 | performPollLocked(FLAG_PERSIST_NETWORK); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 873 | |
| 874 | final NetworkState[] states; |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 875 | final LinkProperties activeLink; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 876 | try { |
| 877 | states = mConnManager.getAllNetworkState(); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 878 | activeLink = mConnManager.getActiveLinkProperties(); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 879 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 880 | // ignored; service lives in system_server |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 881 | return; |
| 882 | } |
| 883 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 884 | mActiveIface = activeLink != null ? activeLink.getInterfaceName() : null; |
| 885 | |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 886 | // rebuild active interfaces based on connected networks |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 887 | mActiveIfaces.clear(); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 888 | |
| 889 | for (NetworkState state : states) { |
| 890 | if (state.networkInfo.isConnected()) { |
| 891 | // collect networks under their parent interfaces |
| 892 | final String iface = state.linkProperties.getInterfaceName(); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 893 | |
| 894 | NetworkIdentitySet ident = mActiveIfaces.get(iface); |
| 895 | if (ident == null) { |
| 896 | ident = new NetworkIdentitySet(); |
| 897 | mActiveIfaces.put(iface, ident); |
| 898 | } |
| 899 | |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 900 | ident.add(NetworkIdentity.buildNetworkIdentity(mContext, state)); |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 901 | |
| 902 | // remember any ifaces associated with mobile networks |
Jeff Sharkey | 6a328af | 2012-11-30 17:49:39 -0800 | [diff] [blame] | 903 | if (isNetworkTypeMobile(state.networkInfo.getType()) && iface != null) { |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 904 | if (!contains(mMobileIfaces, iface)) { |
| 905 | mMobileIfaces = appendElement(String.class, mMobileIfaces, iface); |
| 906 | } |
| 907 | } |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 908 | } |
| 909 | } |
| 910 | } |
| 911 | |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 912 | /** |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 913 | * Bootstrap initial stats snapshot, usually during {@link #systemReady()} |
| 914 | * so we have baseline values without double-counting. |
| 915 | */ |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 916 | private void bootstrapStatsLocked() { |
| 917 | final long currentTime = mTime.hasCache() ? mTime.currentTimeMillis() |
| 918 | : System.currentTimeMillis(); |
| 919 | |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 920 | try { |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 921 | // snapshot and record current counters; read UID stats first to |
| 922 | // avoid overcounting dev stats. |
| 923 | final NetworkStats uidSnapshot = getNetworkStatsUidDetail(); |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 924 | final NetworkStats xtSnapshot = mNetworkManager.getNetworkStatsSummaryXt(); |
| 925 | final NetworkStats devSnapshot = mNetworkManager.getNetworkStatsSummaryDev(); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 926 | |
| 927 | mDevRecorder.recordSnapshotLocked(devSnapshot, mActiveIfaces, currentTime); |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 928 | mXtRecorder.recordSnapshotLocked(xtSnapshot, mActiveIfaces, currentTime); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 929 | mUidRecorder.recordSnapshotLocked(uidSnapshot, mActiveIfaces, currentTime); |
| 930 | mUidTagRecorder.recordSnapshotLocked(uidSnapshot, mActiveIfaces, currentTime); |
| 931 | |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 932 | } catch (IllegalStateException e) { |
| 933 | Slog.w(TAG, "problem reading network stats: " + e); |
| 934 | } catch (RemoteException e) { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 935 | // ignored; service lives in system_server |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 936 | } |
| 937 | } |
| 938 | |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 939 | private void performPoll(int flags) { |
Anders 3 Fridlund | 36bc5b6 | 2012-06-07 11:24:10 +0200 | [diff] [blame] | 940 | // try refreshing time source when stale |
| 941 | if (mTime.getCacheAge() > mSettings.getTimeCacheMaxAge()) { |
| 942 | mTime.forceRefresh(); |
| 943 | } |
| 944 | |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 945 | synchronized (mStatsLock) { |
| 946 | mWakeLock.acquire(); |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 947 | |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 948 | try { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 949 | performPollLocked(flags); |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 950 | } finally { |
| 951 | mWakeLock.release(); |
| 952 | } |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | /** |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 957 | * Periodic poll operation, reading current statistics and recording into |
| 958 | * {@link NetworkStatsHistory}. |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 959 | */ |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 960 | private void performPollLocked(int flags) { |
Jeff Sharkey | 6341fce | 2012-03-06 19:59:57 -0800 | [diff] [blame] | 961 | if (!mSystemReady) return; |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 962 | if (LOGV) Slog.v(TAG, "performPollLocked(flags=0x" + Integer.toHexString(flags) + ")"); |
Jeff Sharkey | 6341fce | 2012-03-06 19:59:57 -0800 | [diff] [blame] | 963 | |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 964 | final long startRealtime = SystemClock.elapsedRealtime(); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 965 | |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 966 | final boolean persistNetwork = (flags & FLAG_PERSIST_NETWORK) != 0; |
| 967 | final boolean persistUid = (flags & FLAG_PERSIST_UID) != 0; |
Jeff Sharkey | 1f0b13b | 2011-09-18 13:30:23 -0700 | [diff] [blame] | 968 | final boolean persistForce = (flags & FLAG_PERSIST_FORCE) != 0; |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 969 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 970 | // TODO: consider marking "untrusted" times in historical stats |
| 971 | final long currentTime = mTime.hasCache() ? mTime.currentTimeMillis() |
| 972 | : System.currentTimeMillis(); |
| 973 | |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 974 | try { |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 975 | // snapshot and record current counters; read UID stats first to |
| 976 | // avoid overcounting dev stats. |
| 977 | final NetworkStats uidSnapshot = getNetworkStatsUidDetail(); |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 978 | final NetworkStats xtSnapshot = mNetworkManager.getNetworkStatsSummaryXt(); |
| 979 | final NetworkStats devSnapshot = mNetworkManager.getNetworkStatsSummaryDev(); |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 980 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 981 | mDevRecorder.recordSnapshotLocked(devSnapshot, mActiveIfaces, currentTime); |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 982 | mXtRecorder.recordSnapshotLocked(xtSnapshot, mActiveIfaces, currentTime); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 983 | mUidRecorder.recordSnapshotLocked(uidSnapshot, mActiveIfaces, currentTime); |
| 984 | mUidTagRecorder.recordSnapshotLocked(uidSnapshot, mActiveIfaces, currentTime); |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 985 | |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 986 | } catch (IllegalStateException e) { |
| 987 | Log.wtf(TAG, "problem reading network stats", e); |
Jeff Sharkey | 905b589 | 2011-09-30 15:19:49 -0700 | [diff] [blame] | 988 | return; |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 989 | } catch (RemoteException e) { |
| 990 | // ignored; service lives in system_server |
Jeff Sharkey | 905b589 | 2011-09-30 15:19:49 -0700 | [diff] [blame] | 991 | return; |
| 992 | } |
| 993 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 994 | // persist any pending data depending on requested flags |
| 995 | if (persistForce) { |
| 996 | mDevRecorder.forcePersistLocked(currentTime); |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 997 | mXtRecorder.forcePersistLocked(currentTime); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 998 | mUidRecorder.forcePersistLocked(currentTime); |
| 999 | mUidTagRecorder.forcePersistLocked(currentTime); |
| 1000 | } else { |
| 1001 | if (persistNetwork) { |
| 1002 | mDevRecorder.maybePersistLocked(currentTime); |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 1003 | mXtRecorder.maybePersistLocked(currentTime); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1004 | } |
| 1005 | if (persistUid) { |
| 1006 | mUidRecorder.maybePersistLocked(currentTime); |
| 1007 | mUidTagRecorder.maybePersistLocked(currentTime); |
| 1008 | } |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 1009 | } |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1010 | |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 1011 | if (LOGV) { |
| 1012 | final long duration = SystemClock.elapsedRealtime() - startRealtime; |
| 1013 | Slog.v(TAG, "performPollLocked() took " + duration + "ms"); |
| 1014 | } |
| 1015 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1016 | if (mSettings.getSampleEnabled()) { |
Jeff Sharkey | 905b589 | 2011-09-30 15:19:49 -0700 | [diff] [blame] | 1017 | // sample stats after each full poll |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1018 | performSampleLocked(); |
Jeff Sharkey | 905b589 | 2011-09-30 15:19:49 -0700 | [diff] [blame] | 1019 | } |
Jeff Sharkey | 07b0dd9 | 2011-09-01 13:06:19 -0700 | [diff] [blame] | 1020 | |
Jeff Sharkey | 497e443 | 2011-06-14 17:27:29 -0700 | [diff] [blame] | 1021 | // finally, dispatch updated event to any listeners |
| 1022 | final Intent updatedIntent = new Intent(ACTION_NETWORK_STATS_UPDATED); |
| 1023 | updatedIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1024 | mContext.sendBroadcastAsUser(updatedIntent, UserHandle.ALL, |
| 1025 | READ_NETWORK_USAGE_HISTORY); |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | /** |
Jeff Sharkey | 07b0dd9 | 2011-09-01 13:06:19 -0700 | [diff] [blame] | 1029 | * Sample recent statistics summary into {@link EventLog}. |
| 1030 | */ |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1031 | private void performSampleLocked() { |
| 1032 | // TODO: migrate trustedtime fixes to separate binary log events |
Jeff Sharkey | 905b589 | 2011-09-30 15:19:49 -0700 | [diff] [blame] | 1033 | final long trustedTime = mTime.hasCache() ? mTime.currentTimeMillis() : -1; |
| 1034 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1035 | NetworkTemplate template; |
| 1036 | NetworkStats.Entry devTotal; |
| 1037 | NetworkStats.Entry xtTotal; |
| 1038 | NetworkStats.Entry uidTotal; |
Jeff Sharkey | 07b0dd9 | 2011-09-01 13:06:19 -0700 | [diff] [blame] | 1039 | |
| 1040 | // collect mobile sample |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 1041 | template = buildTemplateMobileWildcard(); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1042 | devTotal = mDevRecorder.getTotalSinceBootLocked(template); |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 1043 | xtTotal = mXtRecorder.getTotalSinceBootLocked(template); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1044 | uidTotal = mUidRecorder.getTotalSinceBootLocked(template); |
Jeff Sharkey | 684c54a | 2011-11-16 17:46:30 -0800 | [diff] [blame] | 1045 | |
Jeff Sharkey | 905b589 | 2011-09-30 15:19:49 -0700 | [diff] [blame] | 1046 | EventLogTags.writeNetstatsMobileSample( |
| 1047 | devTotal.rxBytes, devTotal.rxPackets, devTotal.txBytes, devTotal.txPackets, |
| 1048 | xtTotal.rxBytes, xtTotal.rxPackets, xtTotal.txBytes, xtTotal.txPackets, |
| 1049 | uidTotal.rxBytes, uidTotal.rxPackets, uidTotal.txBytes, uidTotal.txPackets, |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1050 | trustedTime); |
Jeff Sharkey | 07b0dd9 | 2011-09-01 13:06:19 -0700 | [diff] [blame] | 1051 | |
| 1052 | // collect wifi sample |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1053 | template = buildTemplateWifiWildcard(); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1054 | devTotal = mDevRecorder.getTotalSinceBootLocked(template); |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 1055 | xtTotal = mXtRecorder.getTotalSinceBootLocked(template); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1056 | uidTotal = mUidRecorder.getTotalSinceBootLocked(template); |
| 1057 | |
Jeff Sharkey | 905b589 | 2011-09-30 15:19:49 -0700 | [diff] [blame] | 1058 | EventLogTags.writeNetstatsWifiSample( |
| 1059 | devTotal.rxBytes, devTotal.rxPackets, devTotal.txBytes, devTotal.txPackets, |
| 1060 | xtTotal.rxBytes, xtTotal.rxPackets, xtTotal.txBytes, xtTotal.txPackets, |
| 1061 | uidTotal.rxBytes, uidTotal.rxPackets, uidTotal.txBytes, uidTotal.txPackets, |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1062 | trustedTime); |
Jeff Sharkey | 07b0dd9 | 2011-09-01 13:06:19 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | /** |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1066 | * Clean up {@link #mUidRecorder} after UID is removed. |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 1067 | */ |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 1068 | private void removeUidsLocked(int... uids) { |
| 1069 | if (LOGV) Slog.v(TAG, "removeUidsLocked() for UIDs " + Arrays.toString(uids)); |
| 1070 | |
| 1071 | // Perform one last poll before removing |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 1072 | performPollLocked(FLAG_PERSIST_ALL); |
| 1073 | |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 1074 | mUidRecorder.removeUidsLocked(uids); |
| 1075 | mUidTagRecorder.removeUidsLocked(uids); |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 1076 | |
Jeff Sharkey | daa57e8 | 2012-09-19 14:10:39 -0700 | [diff] [blame] | 1077 | // Clear kernel stats associated with UID |
| 1078 | for (int uid : uids) { |
| 1079 | resetKernelUidStats(uid); |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | /** |
| 1084 | * Clean up {@link #mUidRecorder} after user is removed. |
| 1085 | */ |
| 1086 | private void removeUserLocked(int userId) { |
| 1087 | if (LOGV) Slog.v(TAG, "removeUserLocked() for userId=" + userId); |
| 1088 | |
| 1089 | // Build list of UIDs that we should clean up |
| 1090 | int[] uids = new int[0]; |
| 1091 | final List<ApplicationInfo> apps = mContext.getPackageManager().getInstalledApplications( |
| 1092 | PackageManager.GET_UNINSTALLED_PACKAGES | PackageManager.GET_DISABLED_COMPONENTS); |
| 1093 | for (ApplicationInfo app : apps) { |
| 1094 | final int uid = UserHandle.getUid(userId, app.uid); |
| 1095 | uids = ArrayUtils.appendInt(uids, uid); |
| 1096 | } |
| 1097 | |
| 1098 | removeUidsLocked(uids); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | @Override |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1102 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 1103 | mContext.enforceCallingOrSelfPermission(DUMP, TAG); |
| 1104 | |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 1105 | final HashSet<String> argSet = new HashSet<String>(); |
| 1106 | for (String arg : args) { |
| 1107 | argSet.add(arg); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 1108 | } |
| 1109 | |
Jeff Sharkey | 706498d | 2012-02-06 17:35:07 -0800 | [diff] [blame] | 1110 | // usage: dumpsys netstats --full --uid --tag --poll --checkin |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1111 | final boolean poll = argSet.contains("--poll") || argSet.contains("poll"); |
Jeff Sharkey | 706498d | 2012-02-06 17:35:07 -0800 | [diff] [blame] | 1112 | final boolean checkin = argSet.contains("--checkin"); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1113 | final boolean fullHistory = argSet.contains("--full") || argSet.contains("full"); |
| 1114 | final boolean includeUid = argSet.contains("--uid") || argSet.contains("detail"); |
| 1115 | final boolean includeTag = argSet.contains("--tag") || argSet.contains("detail"); |
| 1116 | |
| 1117 | final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " "); |
Jeff Sharkey | 350083e | 2011-06-29 10:45:16 -0700 | [diff] [blame] | 1118 | |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 1119 | synchronized (mStatsLock) { |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1120 | if (poll) { |
Jeff Sharkey | 1f0b13b | 2011-09-18 13:30:23 -0700 | [diff] [blame] | 1121 | performPollLocked(FLAG_PERSIST_ALL | FLAG_PERSIST_FORCE); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 1122 | pw.println("Forced poll"); |
| 1123 | return; |
| 1124 | } |
| 1125 | |
Jeff Sharkey | 706498d | 2012-02-06 17:35:07 -0800 | [diff] [blame] | 1126 | if (checkin) { |
| 1127 | // list current stats files to verify rotation |
| 1128 | pw.println("Current files:"); |
| 1129 | pw.increaseIndent(); |
| 1130 | for (String file : mBaseDir.list()) { |
| 1131 | pw.println(file); |
| 1132 | } |
| 1133 | pw.decreaseIndent(); |
| 1134 | return; |
| 1135 | } |
| 1136 | |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 1137 | pw.println("Active interfaces:"); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1138 | pw.increaseIndent(); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1139 | for (String iface : mActiveIfaces.keySet()) { |
| 1140 | final NetworkIdentitySet ident = mActiveIfaces.get(iface); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1141 | pw.print("iface="); pw.print(iface); |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 1142 | pw.print(" ident="); pw.println(ident.toString()); |
| 1143 | } |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1144 | pw.decreaseIndent(); |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 1145 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1146 | pw.println("Dev stats:"); |
| 1147 | pw.increaseIndent(); |
| 1148 | mDevRecorder.dumpLocked(pw, fullHistory); |
| 1149 | pw.decreaseIndent(); |
| 1150 | |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 1151 | pw.println("Xt stats:"); |
| 1152 | pw.increaseIndent(); |
| 1153 | mXtRecorder.dumpLocked(pw, fullHistory); |
| 1154 | pw.decreaseIndent(); |
| 1155 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1156 | if (includeUid) { |
| 1157 | pw.println("UID stats:"); |
| 1158 | pw.increaseIndent(); |
| 1159 | mUidRecorder.dumpLocked(pw, fullHistory); |
| 1160 | pw.decreaseIndent(); |
Jeff Sharkey | 905b589 | 2011-09-30 15:19:49 -0700 | [diff] [blame] | 1161 | } |
| 1162 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1163 | if (includeTag) { |
| 1164 | pw.println("UID tag stats:"); |
| 1165 | pw.increaseIndent(); |
| 1166 | mUidTagRecorder.dumpLocked(pw, fullHistory); |
| 1167 | pw.decreaseIndent(); |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 1168 | } |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | /** |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1173 | * Return snapshot of current UID statistics, including any |
| 1174 | * {@link TrafficStats#UID_TETHERING} and {@link #mUidOperations} values. |
Jeff Sharkey | 61ee0bb | 2011-05-29 22:50:42 -0700 | [diff] [blame] | 1175 | */ |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1176 | private NetworkStats getNetworkStatsUidDetail() throws RemoteException { |
| 1177 | final NetworkStats uidSnapshot = mNetworkManager.getNetworkStatsUidDetail(UID_ALL); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1178 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1179 | // fold tethering stats and operations into uid snapshot |
| 1180 | final NetworkStats tetherSnapshot = getNetworkStatsTethering(); |
| 1181 | uidSnapshot.combineAllValues(tetherSnapshot); |
| 1182 | uidSnapshot.combineAllValues(mUidOperations); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1183 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1184 | return uidSnapshot; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
Jeff Sharkey | d4ef8c8f | 2011-11-10 17:54:23 -0800 | [diff] [blame] | 1187 | /** |
| 1188 | * Return snapshot of current tethering statistics. Will return empty |
| 1189 | * {@link NetworkStats} if any problems are encountered. |
| 1190 | */ |
| 1191 | private NetworkStats getNetworkStatsTethering() throws RemoteException { |
| 1192 | try { |
Jeff Sharkey | e4984be | 2013-09-10 21:03:27 -0700 | [diff] [blame] | 1193 | return mNetworkManager.getNetworkStatsTethering(); |
Jeff Sharkey | d4ef8c8f | 2011-11-10 17:54:23 -0800 | [diff] [blame] | 1194 | } catch (IllegalStateException e) { |
| 1195 | Log.wtf(TAG, "problem reading network stats", e); |
| 1196 | return new NetworkStats(0L, 10); |
| 1197 | } |
| 1198 | } |
| 1199 | |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 1200 | private Handler.Callback mHandlerCallback = new Handler.Callback() { |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 1201 | @Override |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 1202 | public boolean handleMessage(Message msg) { |
| 1203 | switch (msg.what) { |
Jeff Sharkey | 8e9992a | 2011-08-23 18:37:23 -0700 | [diff] [blame] | 1204 | case MSG_PERFORM_POLL: { |
Jeff Sharkey | b3d5957 | 2011-09-07 17:20:27 -0700 | [diff] [blame] | 1205 | final int flags = msg.arg1; |
| 1206 | performPoll(flags); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 1207 | return true; |
| 1208 | } |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 1209 | case MSG_UPDATE_IFACES: { |
| 1210 | updateIfaces(); |
| 1211 | return true; |
| 1212 | } |
Jeff Sharkey | 25ce9ed | 2012-02-02 13:07:47 -0800 | [diff] [blame] | 1213 | case MSG_REGISTER_GLOBAL_ALERT: { |
| 1214 | registerGlobalAlert(); |
| 1215 | return true; |
| 1216 | } |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 1217 | default: { |
| 1218 | return false; |
| 1219 | } |
| 1220 | } |
| 1221 | } |
| 1222 | }; |
| 1223 | |
Jeff Sharkey | 4190a04 | 2012-04-21 15:36:48 -0700 | [diff] [blame] | 1224 | private void assertBandwidthControlEnabled() { |
| 1225 | if (!isBandwidthControlEnabled()) { |
| 1226 | throw new IllegalStateException("Bandwidth module disabled"); |
| 1227 | } |
| 1228 | } |
| 1229 | |
Jeff Sharkey | 418d12d | 2011-12-13 15:38:03 -0800 | [diff] [blame] | 1230 | private boolean isBandwidthControlEnabled() { |
Jeff Sharkey | 49c1d17 | 2012-04-23 14:39:19 -0700 | [diff] [blame] | 1231 | final long token = Binder.clearCallingIdentity(); |
Jeff Sharkey | 418d12d | 2011-12-13 15:38:03 -0800 | [diff] [blame] | 1232 | try { |
| 1233 | return mNetworkManager.isBandwidthControlEnabled(); |
| 1234 | } catch (RemoteException e) { |
| 1235 | // ignored; service lives in system_server |
| 1236 | return false; |
Jeff Sharkey | 49c1d17 | 2012-04-23 14:39:19 -0700 | [diff] [blame] | 1237 | } finally { |
| 1238 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | 418d12d | 2011-12-13 15:38:03 -0800 | [diff] [blame] | 1239 | } |
| 1240 | } |
| 1241 | |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1242 | private class DropBoxNonMonotonicObserver implements NonMonotonicObserver<String> { |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 1243 | @Override |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1244 | public void foundNonMonotonic(NetworkStats left, int leftIndex, NetworkStats right, |
| 1245 | int rightIndex, String cookie) { |
| 1246 | Log.w(TAG, "found non-monotonic values; saving to dropbox"); |
| 1247 | |
| 1248 | // record error for debugging |
| 1249 | final StringBuilder builder = new StringBuilder(); |
| 1250 | builder.append("found non-monotonic " + cookie + " values at left[" + leftIndex |
| 1251 | + "] - right[" + rightIndex + "]\n"); |
| 1252 | builder.append("left=").append(left).append('\n'); |
| 1253 | builder.append("right=").append(right).append('\n'); |
| 1254 | |
| 1255 | final DropBoxManager dropBox = (DropBoxManager) mContext.getSystemService( |
| 1256 | Context.DROPBOX_SERVICE); |
| 1257 | dropBox.addText(TAG_NETSTATS_ERROR, builder.toString()); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 1258 | } |
| 1259 | } |
| 1260 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1261 | /** |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 1262 | * Default external settings that read from |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 1263 | * {@link android.provider.Settings.Global}. |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 1264 | */ |
| 1265 | private static class DefaultNetworkStatsSettings implements NetworkStatsSettings { |
| 1266 | private final ContentResolver mResolver; |
| 1267 | |
| 1268 | public DefaultNetworkStatsSettings(Context context) { |
| 1269 | mResolver = checkNotNull(context.getContentResolver()); |
| 1270 | // TODO: adjust these timings for production builds |
| 1271 | } |
| 1272 | |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1273 | private long getGlobalLong(String name, long def) { |
| 1274 | return Settings.Global.getLong(mResolver, name, def); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 1275 | } |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1276 | private boolean getGlobalBoolean(String name, boolean def) { |
Jeff Sharkey | 991d1b1 | 2011-09-14 19:31:04 -0700 | [diff] [blame] | 1277 | final int defInt = def ? 1 : 0; |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1278 | return Settings.Global.getInt(mResolver, name, defInt) != 0; |
Jeff Sharkey | 991d1b1 | 2011-09-14 19:31:04 -0700 | [diff] [blame] | 1279 | } |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 1280 | |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 1281 | @Override |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 1282 | public long getPollInterval() { |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1283 | return getGlobalLong(NETSTATS_POLL_INTERVAL, 30 * MINUTE_IN_MILLIS); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 1284 | } |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 1285 | @Override |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 1286 | public long getTimeCacheMaxAge() { |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1287 | return getGlobalLong(NETSTATS_TIME_CACHE_MAX_AGE, DAY_IN_MILLIS); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1288 | } |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 1289 | @Override |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 1290 | public long getGlobalAlertBytes(long def) { |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1291 | return getGlobalLong(NETSTATS_GLOBAL_ALERT_BYTES, def); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1292 | } |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 1293 | @Override |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1294 | public boolean getSampleEnabled() { |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1295 | return getGlobalBoolean(NETSTATS_SAMPLE_ENABLED, true); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1296 | } |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 1297 | @Override |
Jeff Sharkey | 70c7053 | 2012-05-16 14:51:19 -0700 | [diff] [blame] | 1298 | public boolean getReportXtOverDev() { |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1299 | return getGlobalBoolean(NETSTATS_REPORT_XT_OVER_DEV, true); |
Jeff Sharkey | 70c7053 | 2012-05-16 14:51:19 -0700 | [diff] [blame] | 1300 | } |
| 1301 | @Override |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1302 | public Config getDevConfig() { |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1303 | return new Config(getGlobalLong(NETSTATS_DEV_BUCKET_DURATION, HOUR_IN_MILLIS), |
| 1304 | getGlobalLong(NETSTATS_DEV_ROTATE_AGE, 15 * DAY_IN_MILLIS), |
| 1305 | getGlobalLong(NETSTATS_DEV_DELETE_AGE, 90 * DAY_IN_MILLIS)); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1306 | } |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 1307 | @Override |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 1308 | public Config getXtConfig() { |
| 1309 | return getDevConfig(); |
| 1310 | } |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 1311 | @Override |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1312 | public Config getUidConfig() { |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1313 | return new Config(getGlobalLong(NETSTATS_UID_BUCKET_DURATION, 2 * HOUR_IN_MILLIS), |
| 1314 | getGlobalLong(NETSTATS_UID_ROTATE_AGE, 15 * DAY_IN_MILLIS), |
| 1315 | getGlobalLong(NETSTATS_UID_DELETE_AGE, 90 * DAY_IN_MILLIS)); |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1316 | } |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 1317 | @Override |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 1318 | public Config getUidTagConfig() { |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1319 | return new Config(getGlobalLong(NETSTATS_UID_TAG_BUCKET_DURATION, 2 * HOUR_IN_MILLIS), |
| 1320 | getGlobalLong(NETSTATS_UID_TAG_ROTATE_AGE, 5 * DAY_IN_MILLIS), |
| 1321 | getGlobalLong(NETSTATS_UID_TAG_DELETE_AGE, 15 * DAY_IN_MILLIS)); |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 1322 | } |
| 1323 | @Override |
| 1324 | public long getDevPersistBytes(long def) { |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1325 | return getGlobalLong(NETSTATS_DEV_PERSIST_BYTES, def); |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 1326 | } |
| 1327 | @Override |
| 1328 | public long getXtPersistBytes(long def) { |
| 1329 | return getDevPersistBytes(def); |
| 1330 | } |
| 1331 | @Override |
| 1332 | public long getUidPersistBytes(long def) { |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1333 | return getGlobalLong(NETSTATS_UID_PERSIST_BYTES, def); |
Jeff Sharkey | ac3fcb1 | 2012-05-02 18:11:52 -0700 | [diff] [blame] | 1334 | } |
| 1335 | @Override |
| 1336 | public long getUidTagPersistBytes(long def) { |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1337 | return getGlobalLong(NETSTATS_UID_TAG_PERSIST_BYTES, def); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 1338 | } |
| 1339 | } |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 1340 | } |