Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 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 | package com.android.server.stats; |
| 17 | |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 18 | import android.annotation.Nullable; |
Rajeev Kumar | 22d92b7 | 2018-02-07 18:38:36 -0800 | [diff] [blame] | 19 | import android.app.ActivityManagerInternal; |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 20 | import android.app.AlarmManager; |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 21 | import android.app.AlarmManager.OnAlarmListener; |
Tej Singh | 6188aa3 | 2018-08-29 00:21:07 +0000 | [diff] [blame] | 22 | import android.app.PendingIntent; |
Rajeev Kumar | 22d92b7 | 2018-02-07 18:38:36 -0800 | [diff] [blame] | 23 | import android.app.ProcessMemoryState; |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 24 | import android.app.StatsManager; |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 25 | import android.bluetooth.BluetoothActivityEnergyInfo; |
| 26 | import android.bluetooth.BluetoothAdapter; |
| 27 | import android.bluetooth.UidTraffic; |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 28 | import android.content.BroadcastReceiver; |
| 29 | import android.content.Context; |
| 30 | import android.content.Intent; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 31 | import android.content.IntentFilter; |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 32 | import android.content.IntentSender; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 33 | import android.content.pm.PackageInfo; |
| 34 | import android.content.pm.PackageManager; |
| 35 | import android.content.pm.UserInfo; |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 36 | import android.net.NetworkStats; |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 37 | import android.net.wifi.IWifiManager; |
| 38 | import android.net.wifi.WifiActivityEnergyInfo; |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 39 | import android.os.BatteryStatsInternal; |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 40 | import android.os.Binder; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 41 | import android.os.Bundle; |
Chenjie Yu | 937d742 | 2018-01-10 16:37:53 -0800 | [diff] [blame] | 42 | import android.os.Environment; |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 43 | import android.os.FileUtils; |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 44 | import android.os.IBinder; |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 45 | import android.os.IStatsCompanionService; |
| 46 | import android.os.IStatsManager; |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 47 | import android.os.Parcelable; |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 48 | import android.os.Process; |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 49 | import android.os.RemoteException; |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 50 | import android.os.ServiceManager; |
Chenjie Yu | 937d742 | 2018-01-10 16:37:53 -0800 | [diff] [blame] | 51 | import android.os.StatFs; |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 52 | import android.os.StatsDimensionsValue; |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 53 | import android.os.StatsLogEventWrapper; |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 54 | import android.os.SynchronousResultReceiver; |
Chenjie Yu | 937d742 | 2018-01-10 16:37:53 -0800 | [diff] [blame] | 55 | import android.os.SystemClock; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 56 | import android.os.UserHandle; |
| 57 | import android.os.UserManager; |
Chenjie Yu | 937d742 | 2018-01-10 16:37:53 -0800 | [diff] [blame] | 58 | import android.telephony.ModemActivityInfo; |
| 59 | import android.telephony.TelephonyManager; |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 60 | import android.util.ArrayMap; |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 61 | import android.util.Slog; |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 62 | import android.util.StatsLog; |
| 63 | |
| 64 | import com.android.internal.annotations.GuardedBy; |
| 65 | import com.android.internal.net.NetworkStatsFactory; |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 66 | import com.android.internal.os.BinderCallsStats.ExportedCallStat; |
Chenjie Yu | 937d742 | 2018-01-10 16:37:53 -0800 | [diff] [blame] | 67 | import com.android.internal.os.KernelCpuSpeedReader; |
Tej Singh | f154cf0 | 2018-08-16 18:31:54 -0700 | [diff] [blame] | 68 | import com.android.internal.os.KernelUidCpuTimeReader; |
Tej Singh | 6188aa3 | 2018-08-29 00:21:07 +0000 | [diff] [blame] | 69 | import com.android.internal.os.KernelUidCpuClusterTimeReader; |
| 70 | import com.android.internal.os.KernelUidCpuActiveTimeReader; |
| 71 | import com.android.internal.os.KernelUidCpuFreqTimeReader; |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 72 | import com.android.internal.os.KernelWakelockReader; |
| 73 | import com.android.internal.os.KernelWakelockStats; |
Chenjie Yu | 7f8def9 | 2017-11-03 09:33:15 -0700 | [diff] [blame] | 74 | import com.android.internal.os.PowerProfile; |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 75 | import com.android.internal.util.DumpUtils; |
Olivier Gaillard | 289ba40 | 2018-07-24 18:50:13 +0100 | [diff] [blame] | 76 | import com.android.server.BinderCallsStatsService; |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 77 | import com.android.server.LocalServices; |
| 78 | import com.android.server.SystemService; |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 79 | |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 80 | import java.io.File; |
| 81 | import java.io.FileDescriptor; |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 82 | import java.io.IOException; |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 83 | import java.io.PrintWriter; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 84 | import java.util.ArrayList; |
Bookatz | 058d869 | 2018-03-06 09:53:47 -0800 | [diff] [blame] | 85 | import java.util.Arrays; |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 86 | import java.util.HashMap; |
| 87 | import java.util.HashSet; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 88 | import java.util.List; |
Bookatz | c68a9d2 | 2017-09-27 14:09:55 -0700 | [diff] [blame] | 89 | import java.util.Map; |
Olivier Gaillard | 6f52d15 | 2018-07-25 12:13:12 +0100 | [diff] [blame] | 90 | import java.util.Map.Entry; |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 91 | import java.util.concurrent.TimeUnit; |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 92 | import java.util.concurrent.TimeoutException; |
Bookatz | c68a9d2 | 2017-09-27 14:09:55 -0700 | [diff] [blame] | 93 | |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 94 | /** |
| 95 | * Helper service for statsd (the native stats management service in cmds/statsd/). |
| 96 | * Used for registering and receiving alarms on behalf of statsd. |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 97 | * |
Bookatz | 6bc51d7 | 2017-09-28 16:43:40 -0700 | [diff] [blame] | 98 | * @hide |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 99 | */ |
| 100 | public class StatsCompanionService extends IStatsCompanionService.Stub { |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 101 | /** |
| 102 | * How long to wait on an individual subsystem to return its stats. |
| 103 | */ |
| 104 | private static final long EXTERNAL_STATS_SYNC_TIMEOUT_MILLIS = 2000; |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 105 | private static final long MILLIS_IN_A_DAY = TimeUnit.DAYS.toMillis(1); |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 106 | |
| 107 | public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity"; |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 108 | public static final String CONFIG_DIR = "/data/misc/stats-service"; |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 109 | |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 110 | static final String TAG = "StatsCompanionService"; |
Tej Singh | 484524a | 2018-02-01 15:10:05 -0800 | [diff] [blame] | 111 | static final boolean DEBUG = false; |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 112 | |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 113 | public static final int CODE_DATA_BROADCAST = 1; |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 114 | public static final int CODE_SUBSCRIBER_BROADCAST = 1; |
David Chen | d37bc23 | 2018-04-12 18:05:11 -0700 | [diff] [blame] | 115 | /** |
| 116 | * The last report time is provided with each intent registered to |
| 117 | * StatsManager#setFetchReportsOperation. This allows easy de-duping in the receiver if |
| 118 | * statsd is requesting the client to retrieve the same statsd data. The last report time |
| 119 | * corresponds to the last_report_elapsed_nanos that will provided in the current |
| 120 | * ConfigMetricsReport, and this timestamp also corresponds to the |
| 121 | * current_report_elapsed_nanos of the most recently obtained ConfigMetricsReport. |
| 122 | */ |
| 123 | public static final String EXTRA_LAST_REPORT_TIME = "android.app.extra.LAST_REPORT_TIME"; |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 124 | public static final int DEATH_THRESHOLD = 10; |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 125 | |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 126 | private final Context mContext; |
| 127 | private final AlarmManager mAlarmManager; |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 128 | @GuardedBy("sStatsdLock") |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 129 | private static IStatsManager sStatsd; |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 130 | private static final Object sStatsdLock = new Object(); |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 131 | |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 132 | private final OnAlarmListener mAnomalyAlarmListener = new AnomalyAlarmListener(); |
| 133 | private final OnAlarmListener mPullingAlarmListener = new PullingAlarmListener(); |
| 134 | private final OnAlarmListener mPeriodicAlarmListener = new PeriodicAlarmListener(); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 135 | private final BroadcastReceiver mAppUpdateReceiver; |
David Chen | 47e8f4d | 2017-10-11 15:34:13 -0700 | [diff] [blame] | 136 | private final BroadcastReceiver mUserUpdateReceiver; |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 137 | private final ShutdownEventReceiver mShutdownEventReceiver; |
Chenjie Yu | 7f8def9 | 2017-11-03 09:33:15 -0700 | [diff] [blame] | 138 | private final KernelWakelockReader mKernelWakelockReader = new KernelWakelockReader(); |
| 139 | private final KernelWakelockStats mTmpWakelockStats = new KernelWakelockStats(); |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 140 | private IWifiManager mWifiManager = null; |
| 141 | private TelephonyManager mTelephony = null; |
Chenjie Yu | 937d742 | 2018-01-10 16:37:53 -0800 | [diff] [blame] | 142 | private final StatFs mStatFsData = new StatFs(Environment.getDataDirectory().getAbsolutePath()); |
| 143 | private final StatFs mStatFsSystem = |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 144 | new StatFs(Environment.getRootDirectory().getAbsolutePath()); |
Chenjie Yu | 937d742 | 2018-01-10 16:37:53 -0800 | [diff] [blame] | 145 | private final StatFs mStatFsTemp = |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 146 | new StatFs(Environment.getDownloadCacheDirectory().getAbsolutePath()); |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 147 | @GuardedBy("sStatsdLock") |
| 148 | private final HashSet<Long> mDeathTimeMillis = new HashSet<>(); |
| 149 | @GuardedBy("sStatsdLock") |
| 150 | private final HashMap<Long, String> mDeletedFiles = new HashMap<>(); |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 151 | |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 152 | private KernelUidCpuTimeReader mKernelUidCpuTimeReader = new KernelUidCpuTimeReader(); |
| 153 | private KernelCpuSpeedReader[] mKernelCpuSpeedReaders; |
| 154 | private KernelUidCpuFreqTimeReader mKernelUidCpuFreqTimeReader = |
| 155 | new KernelUidCpuFreqTimeReader(); |
| 156 | private KernelUidCpuActiveTimeReader mKernelUidCpuActiveTimeReader = |
| 157 | new KernelUidCpuActiveTimeReader(); |
| 158 | private KernelUidCpuClusterTimeReader mKernelUidCpuClusterTimeReader = |
| 159 | new KernelUidCpuClusterTimeReader(); |
| 160 | |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 161 | public StatsCompanionService(Context context) { |
| 162 | super(); |
| 163 | mContext = context; |
| 164 | mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); |
| 165 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 166 | mAppUpdateReceiver = new AppUpdateReceiver(); |
David Chen | 47e8f4d | 2017-10-11 15:34:13 -0700 | [diff] [blame] | 167 | mUserUpdateReceiver = new BroadcastReceiver() { |
| 168 | @Override |
| 169 | public void onReceive(Context context, Intent intent) { |
| 170 | synchronized (sStatsdLock) { |
| 171 | sStatsd = fetchStatsdService(); |
| 172 | if (sStatsd == null) { |
Bookatz | 111ed73 | 2018-02-16 15:54:05 -0800 | [diff] [blame] | 173 | Slog.w(TAG, "Could not access statsd for UserUpdateReceiver"); |
David Chen | 47e8f4d | 2017-10-11 15:34:13 -0700 | [diff] [blame] | 174 | return; |
| 175 | } |
| 176 | try { |
| 177 | // Pull the latest state of UID->app name, version mapping. |
| 178 | // Needed since the new user basically has a version of every app. |
| 179 | informAllUidsLocked(context); |
| 180 | } catch (RemoteException e) { |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 181 | Slog.e(TAG, "Failed to inform statsd latest update of all apps", e); |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 182 | forgetEverythingLocked(); |
David Chen | 47e8f4d | 2017-10-11 15:34:13 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | } |
| 186 | }; |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 187 | mShutdownEventReceiver = new ShutdownEventReceiver(); |
Bookatz | 111ed73 | 2018-02-16 15:54:05 -0800 | [diff] [blame] | 188 | if (DEBUG) Slog.d(TAG, "Registered receiver for ACTION_PACKAGE_REPLACED and ADDED."); |
Chenjie Yu | 7f8def9 | 2017-11-03 09:33:15 -0700 | [diff] [blame] | 189 | PowerProfile powerProfile = new PowerProfile(context); |
| 190 | final int numClusters = powerProfile.getNumCpuClusters(); |
| 191 | mKernelCpuSpeedReaders = new KernelCpuSpeedReader[numClusters]; |
| 192 | int firstCpuOfCluster = 0; |
| 193 | for (int i = 0; i < numClusters; i++) { |
| 194 | final int numSpeedSteps = powerProfile.getNumSpeedStepsInCpuCluster(i); |
| 195 | mKernelCpuSpeedReaders[i] = new KernelCpuSpeedReader(firstCpuOfCluster, |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 196 | numSpeedSteps); |
Chenjie Yu | 7f8def9 | 2017-11-03 09:33:15 -0700 | [diff] [blame] | 197 | firstCpuOfCluster += powerProfile.getNumCoresInCpuCluster(i); |
| 198 | } |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 199 | // use default throttling in |
| 200 | // frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader |
| 201 | mKernelUidCpuFreqTimeReader.setThrottleInterval(0); |
| 202 | long[] freqs = mKernelUidCpuFreqTimeReader.readFreqs(powerProfile); |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 203 | mKernelUidCpuClusterTimeReader.setThrottleInterval(0); |
| 204 | mKernelUidCpuActiveTimeReader.setThrottleInterval(0); |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 205 | } |
| 206 | |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 207 | @Override |
David Chen | d37bc23 | 2018-04-12 18:05:11 -0700 | [diff] [blame] | 208 | public void sendDataBroadcast(IBinder intentSenderBinder, long lastReportTimeNs) { |
Bookatz | 3692082 | 2018-01-26 09:18:07 -0800 | [diff] [blame] | 209 | enforceCallingPermission(); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 210 | IntentSender intentSender = new IntentSender(intentSenderBinder); |
| 211 | Intent intent = new Intent(); |
David Chen | d37bc23 | 2018-04-12 18:05:11 -0700 | [diff] [blame] | 212 | intent.putExtra(EXTRA_LAST_REPORT_TIME, lastReportTimeNs); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 213 | try { |
| 214 | intentSender.sendIntent(mContext, CODE_DATA_BROADCAST, intent, null, null); |
| 215 | } catch (IntentSender.SendIntentException e) { |
| 216 | Slog.w(TAG, "Unable to send using IntentSender"); |
| 217 | } |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 220 | @Override |
| 221 | public void sendSubscriberBroadcast(IBinder intentSenderBinder, long configUid, long configKey, |
| 222 | long subscriptionId, long subscriptionRuleId, |
Bookatz | 058d869 | 2018-03-06 09:53:47 -0800 | [diff] [blame] | 223 | String[] cookies, |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 224 | StatsDimensionsValue dimensionsValue) { |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 225 | enforceCallingPermission(); |
| 226 | IntentSender intentSender = new IntentSender(intentSenderBinder); |
| 227 | Intent intent = new Intent() |
| 228 | .putExtra(StatsManager.EXTRA_STATS_CONFIG_UID, configUid) |
| 229 | .putExtra(StatsManager.EXTRA_STATS_CONFIG_KEY, configKey) |
| 230 | .putExtra(StatsManager.EXTRA_STATS_SUBSCRIPTION_ID, subscriptionId) |
| 231 | .putExtra(StatsManager.EXTRA_STATS_SUBSCRIPTION_RULE_ID, subscriptionRuleId) |
| 232 | .putExtra(StatsManager.EXTRA_STATS_DIMENSIONS_VALUE, dimensionsValue); |
Bookatz | 058d869 | 2018-03-06 09:53:47 -0800 | [diff] [blame] | 233 | |
| 234 | ArrayList<String> cookieList = new ArrayList<>(cookies.length); |
| 235 | for (String cookie : cookies) { cookieList.add(cookie); } |
| 236 | intent.putStringArrayListExtra( |
| 237 | StatsManager.EXTRA_STATS_BROADCAST_SUBSCRIBER_COOKIES, cookieList); |
| 238 | |
Bookatz | 1476ef2 | 2018-02-13 12:26:01 -0800 | [diff] [blame] | 239 | if (DEBUG) { |
Bookatz | 058d869 | 2018-03-06 09:53:47 -0800 | [diff] [blame] | 240 | Slog.d(TAG, String.format( |
| 241 | "Statsd sendSubscriberBroadcast with params {%d %d %d %d %s %s}", |
| 242 | configUid, configKey, subscriptionId, subscriptionRuleId, |
| 243 | Arrays.toString(cookies), dimensionsValue)); |
Bookatz | 1476ef2 | 2018-02-13 12:26:01 -0800 | [diff] [blame] | 244 | } |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 245 | try { |
| 246 | intentSender.sendIntent(mContext, CODE_SUBSCRIBER_BROADCAST, intent, null, null); |
| 247 | } catch (IntentSender.SendIntentException e) { |
| 248 | Slog.w(TAG, "Unable to send using IntentSender from uid " + configUid |
| 249 | + "; presumably it had been cancelled."); |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 253 | private final static int[] toIntArray(List<Integer> list) { |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 254 | int[] ret = new int[list.size()]; |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 255 | for (int i = 0; i < ret.length; i++) { |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 256 | ret[i] = list.get(i); |
| 257 | } |
| 258 | return ret; |
| 259 | } |
| 260 | |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 261 | private final static long[] toLongArray(List<Long> list) { |
| 262 | long[] ret = new long[list.size()]; |
| 263 | for (int i = 0; i < ret.length; i++) { |
| 264 | ret[i] = list.get(i); |
| 265 | } |
| 266 | return ret; |
| 267 | } |
| 268 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 269 | // Assumes that sStatsdLock is held. |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 270 | @GuardedBy("sStatsdLock") |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 271 | private final void informAllUidsLocked(Context context) throws RemoteException { |
| 272 | UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE); |
| 273 | PackageManager pm = context.getPackageManager(); |
| 274 | final List<UserInfo> users = um.getUsers(true); |
| 275 | if (DEBUG) { |
Bookatz | 111ed73 | 2018-02-16 15:54:05 -0800 | [diff] [blame] | 276 | Slog.d(TAG, "Iterating over " + users.size() + " profiles."); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Rajeev Kumar | 22d92b7 | 2018-02-07 18:38:36 -0800 | [diff] [blame] | 279 | List<Integer> uids = new ArrayList<>(); |
| 280 | List<Long> versions = new ArrayList<>(); |
| 281 | List<String> apps = new ArrayList<>(); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 282 | |
| 283 | // Add in all the apps for every user/profile. |
| 284 | for (UserInfo profile : users) { |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 285 | List<PackageInfo> pi = |
David Chen | d37bc23 | 2018-04-12 18:05:11 -0700 | [diff] [blame] | 286 | pm.getInstalledPackagesAsUser(PackageManager.MATCH_KNOWN_PACKAGES, profile.id); |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 287 | for (int j = 0; j < pi.size(); j++) { |
| 288 | if (pi.get(j).applicationInfo != null) { |
| 289 | uids.add(pi.get(j).applicationInfo.uid); |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 290 | versions.add(pi.get(j).getLongVersionCode()); |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 291 | apps.add(pi.get(j).packageName); |
| 292 | } |
| 293 | } |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 294 | } |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 295 | sStatsd.informAllUidData(toIntArray(uids), toLongArray(versions), apps.toArray(new |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 296 | String[apps.size()])); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 297 | if (DEBUG) { |
Bookatz | 111ed73 | 2018-02-16 15:54:05 -0800 | [diff] [blame] | 298 | Slog.d(TAG, "Sent data for " + uids.size() + " apps"); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
Bookatz | 3692082 | 2018-01-26 09:18:07 -0800 | [diff] [blame] | 302 | private final static class AppUpdateReceiver extends BroadcastReceiver { |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 303 | @Override |
| 304 | public void onReceive(Context context, Intent intent) { |
David Chen | 47e8f4d | 2017-10-11 15:34:13 -0700 | [diff] [blame] | 305 | /** |
| 306 | * App updates actually consist of REMOVE, ADD, and then REPLACE broadcasts. To avoid |
| 307 | * waste, we ignore the REMOVE and ADD broadcasts that contain the replacing flag. |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 308 | * If we can't find the value for EXTRA_REPLACING, we default to false. |
David Chen | 47e8f4d | 2017-10-11 15:34:13 -0700 | [diff] [blame] | 309 | */ |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 310 | if (!intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED) |
| 311 | && intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
David Chen | 47e8f4d | 2017-10-11 15:34:13 -0700 | [diff] [blame] | 312 | return; // Keep only replacing or normal add and remove. |
| 313 | } |
Tej Singh | 90d3aa0 | 2018-03-08 19:07:58 -0800 | [diff] [blame] | 314 | if (DEBUG) Slog.d(TAG, "StatsCompanionService noticed an app was updated."); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 315 | synchronized (sStatsdLock) { |
| 316 | if (sStatsd == null) { |
yro | f6d1ca5 | 2017-11-15 15:38:34 -0800 | [diff] [blame] | 317 | Slog.w(TAG, "Could not access statsd to inform it of an app update"); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 318 | return; |
| 319 | } |
| 320 | try { |
| 321 | if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) { |
| 322 | Bundle b = intent.getExtras(); |
| 323 | int uid = b.getInt(Intent.EXTRA_UID); |
| 324 | boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); |
| 325 | if (!replacing) { |
| 326 | // Don't bother sending an update if we're right about to get another |
| 327 | // intent for the new version that's added. |
| 328 | PackageManager pm = context.getPackageManager(); |
| 329 | String app = intent.getData().getSchemeSpecificPart(); |
| 330 | sStatsd.informOnePackageRemoved(app, uid); |
Howard Ro | e4f3df6 | 2018-08-01 11:19:10 -0700 | [diff] [blame] | 331 | StatsLog.write(StatsLog.GENERIC_ATOM, uid, 1000); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 332 | } |
| 333 | } else { |
| 334 | PackageManager pm = context.getPackageManager(); |
| 335 | Bundle b = intent.getExtras(); |
| 336 | int uid = b.getInt(Intent.EXTRA_UID); |
| 337 | String app = intent.getData().getSchemeSpecificPart(); |
| 338 | PackageInfo pi = pm.getPackageInfo(app, PackageManager.MATCH_ANY_USER); |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 339 | sStatsd.informOnePackage(app, uid, pi.getLongVersionCode()); |
Howard Ro | e4f3df6 | 2018-08-01 11:19:10 -0700 | [diff] [blame] | 340 | StatsLog.write(StatsLog.GENERIC_ATOM, uid, 1001); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 341 | } |
| 342 | } catch (Exception e) { |
| 343 | Slog.w(TAG, "Failed to inform statsd of an app update", e); |
| 344 | } |
| 345 | } |
| 346 | } |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 347 | } |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 348 | |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 349 | public final static class AnomalyAlarmListener implements OnAlarmListener { |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 350 | @Override |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 351 | public void onAlarm() { |
Bookatz | 66fe061 | 2018-02-07 18:51:48 -0800 | [diff] [blame] | 352 | Slog.i(TAG, "StatsCompanionService believes an anomaly has occurred at time " |
| 353 | + System.currentTimeMillis() + "ms."); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 354 | synchronized (sStatsdLock) { |
| 355 | if (sStatsd == null) { |
| 356 | Slog.w(TAG, "Could not access statsd to inform it of anomaly alarm firing"); |
| 357 | return; |
| 358 | } |
| 359 | try { |
| 360 | // Two-way call to statsd to retain AlarmManager wakelock |
| 361 | sStatsd.informAnomalyAlarmFired(); |
| 362 | } catch (RemoteException e) { |
| 363 | Slog.w(TAG, "Failed to inform statsd of anomaly alarm firing", e); |
| 364 | } |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 365 | } |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 366 | // AlarmManager releases its own wakelock here. |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 367 | } |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 368 | } |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 369 | |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 370 | public final static class PullingAlarmListener implements OnAlarmListener { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 371 | @Override |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 372 | public void onAlarm() { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 373 | if (DEBUG) |
| 374 | Slog.d(TAG, "Time to poll something."); |
| 375 | synchronized (sStatsdLock) { |
| 376 | if (sStatsd == null) { |
| 377 | Slog.w(TAG, "Could not access statsd to inform it of pulling alarm firing."); |
| 378 | return; |
| 379 | } |
| 380 | try { |
| 381 | // Two-way call to statsd to retain AlarmManager wakelock |
| 382 | sStatsd.informPollAlarmFired(); |
| 383 | } catch (RemoteException e) { |
| 384 | Slog.w(TAG, "Failed to inform statsd of pulling alarm firing.", e); |
| 385 | } |
| 386 | } |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 387 | } |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 388 | } |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 389 | |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 390 | public final static class PeriodicAlarmListener implements OnAlarmListener { |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 391 | @Override |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 392 | public void onAlarm() { |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 393 | if (DEBUG) |
Yangster-mac | c04feba | 2018-04-02 14:37:33 -0700 | [diff] [blame] | 394 | Slog.d(TAG, "Time to trigger periodic alarm."); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 395 | synchronized (sStatsdLock) { |
| 396 | if (sStatsd == null) { |
| 397 | Slog.w(TAG, "Could not access statsd to inform it of periodic alarm firing."); |
| 398 | return; |
| 399 | } |
| 400 | try { |
| 401 | // Two-way call to statsd to retain AlarmManager wakelock |
| 402 | sStatsd.informAlarmForSubscriberTriggeringFired(); |
| 403 | } catch (RemoteException e) { |
| 404 | Slog.w(TAG, "Failed to inform statsd of periodic alarm firing.", e); |
| 405 | } |
| 406 | } |
| 407 | // AlarmManager releases its own wakelock here. |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | public final static class ShutdownEventReceiver extends BroadcastReceiver { |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 412 | @Override |
| 413 | public void onReceive(Context context, Intent intent) { |
| 414 | /** |
| 415 | * Skip immediately if intent is not relevant to device shutdown. |
| 416 | */ |
| 417 | if (!intent.getAction().equals(Intent.ACTION_REBOOT) |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 418 | && !(intent.getAction().equals(Intent.ACTION_SHUTDOWN) |
| 419 | && (intent.getFlags() & Intent.FLAG_RECEIVER_FOREGROUND) != 0)) { |
| 420 | return; |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 421 | } |
yro | f7a3bcb | 2018-01-24 17:18:55 -0800 | [diff] [blame] | 422 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 423 | Slog.i(TAG, "StatsCompanionService noticed a shutdown."); |
| 424 | synchronized (sStatsdLock) { |
| 425 | if (sStatsd == null) { |
| 426 | Slog.w(TAG, "Could not access statsd to inform it of a shutdown event."); |
| 427 | return; |
| 428 | } |
| 429 | try { |
Yangster-mac | 892f3d3 | 2018-05-02 14:16:48 -0700 | [diff] [blame] | 430 | sStatsd.informDeviceShutdown(); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 431 | } catch (Exception e) { |
| 432 | Slog.w(TAG, "Failed to inform statsd of a shutdown event.", e); |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 438 | @Override // Binder call |
| 439 | public void setAnomalyAlarm(long timestampMs) { |
| 440 | enforceCallingPermission(); |
| 441 | if (DEBUG) Slog.d(TAG, "Setting anomaly alarm for " + timestampMs); |
| 442 | final long callingToken = Binder.clearCallingIdentity(); |
| 443 | try { |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 444 | // using ELAPSED_REALTIME, not ELAPSED_REALTIME_WAKEUP, so if device is asleep, will |
| 445 | // only fire when it awakens. |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 446 | // AlarmManager will automatically cancel any previous mAnomalyAlarmListener alarm. |
| 447 | mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME, timestampMs, TAG + ".anomaly", |
| 448 | mAnomalyAlarmListener, null); |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 449 | } finally { |
| 450 | Binder.restoreCallingIdentity(callingToken); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | @Override // Binder call |
| 455 | public void cancelAnomalyAlarm() { |
| 456 | enforceCallingPermission(); |
| 457 | if (DEBUG) Slog.d(TAG, "Cancelling anomaly alarm"); |
| 458 | final long callingToken = Binder.clearCallingIdentity(); |
| 459 | try { |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 460 | mAlarmManager.cancel(mAnomalyAlarmListener); |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 461 | } finally { |
| 462 | Binder.restoreCallingIdentity(callingToken); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | @Override // Binder call |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 467 | public void setAlarmForSubscriberTriggering(long timestampMs) { |
| 468 | enforceCallingPermission(); |
| 469 | if (DEBUG) |
Yangster-mac | c04feba | 2018-04-02 14:37:33 -0700 | [diff] [blame] | 470 | Slog.d(TAG, "Setting periodic alarm in about " + |
| 471 | (timestampMs - SystemClock.elapsedRealtime())); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 472 | final long callingToken = Binder.clearCallingIdentity(); |
| 473 | try { |
| 474 | // using ELAPSED_REALTIME, not ELAPSED_REALTIME_WAKEUP, so if device is asleep, will |
| 475 | // only fire when it awakens. |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 476 | mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME, timestampMs, TAG + ".periodic", |
| 477 | mPeriodicAlarmListener, null); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 478 | } finally { |
| 479 | Binder.restoreCallingIdentity(callingToken); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | @Override // Binder call |
| 484 | public void cancelAlarmForSubscriberTriggering() { |
| 485 | enforceCallingPermission(); |
| 486 | if (DEBUG) |
| 487 | Slog.d(TAG, "Cancelling periodic alarm"); |
| 488 | final long callingToken = Binder.clearCallingIdentity(); |
| 489 | try { |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 490 | mAlarmManager.cancel(mPeriodicAlarmListener); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 491 | } finally { |
| 492 | Binder.restoreCallingIdentity(callingToken); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | @Override // Binder call |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 497 | public void setPullingAlarm(long nextPullTimeMs) { |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 498 | enforceCallingPermission(); |
| 499 | if (DEBUG) { |
| 500 | Slog.d(TAG, "Setting pulling alarm in about " |
| 501 | + (nextPullTimeMs - SystemClock.elapsedRealtime())); |
| 502 | } |
| 503 | final long callingToken = Binder.clearCallingIdentity(); |
| 504 | try { |
| 505 | // using ELAPSED_REALTIME, not ELAPSED_REALTIME_WAKEUP, so if device is asleep, will |
| 506 | // only fire when it awakens. |
| 507 | mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME, nextPullTimeMs, TAG + ".pull", |
| 508 | mPullingAlarmListener, null); |
| 509 | } finally { |
| 510 | Binder.restoreCallingIdentity(callingToken); |
| 511 | } |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | @Override // Binder call |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 515 | public void cancelPullingAlarm() { |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 516 | enforceCallingPermission(); |
| 517 | if (DEBUG) |
| 518 | Slog.d(TAG, "Cancelling pulling alarm"); |
| 519 | final long callingToken = Binder.clearCallingIdentity(); |
| 520 | try { |
| 521 | mAlarmManager.cancel(mPullingAlarmListener); |
| 522 | } finally { |
| 523 | Binder.restoreCallingIdentity(callingToken); |
| 524 | } |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 527 | private void addNetworkStats( |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 528 | int tag, List<StatsLogEventWrapper> ret, NetworkStats stats, boolean withFGBG) { |
| 529 | int size = stats.size(); |
Yangster-mac | c552b35 | 2018-02-22 10:00:45 -0800 | [diff] [blame] | 530 | long elapsedNanos = SystemClock.elapsedRealtimeNanos(); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 531 | NetworkStats.Entry entry = new NetworkStats.Entry(); // For recycling |
| 532 | for (int j = 0; j < size; j++) { |
| 533 | stats.getValues(j, entry); |
Yangster-mac | c552b35 | 2018-02-22 10:00:45 -0800 | [diff] [blame] | 534 | StatsLogEventWrapper e = new StatsLogEventWrapper(elapsedNanos, tag, withFGBG ? 6 : 5); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 535 | e.writeInt(entry.uid); |
| 536 | if (withFGBG) { |
| 537 | e.writeInt(entry.set); |
| 538 | } |
| 539 | e.writeLong(entry.rxBytes); |
| 540 | e.writeLong(entry.rxPackets); |
| 541 | e.writeLong(entry.txBytes); |
| 542 | e.writeLong(entry.txPackets); |
| 543 | ret.add(e); |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 544 | } |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | /** |
| 548 | * Allows rollups per UID but keeping the set (foreground/background) slicing. |
| 549 | * Adapted from groupedByUid in frameworks/base/core/java/android/net/NetworkStats.java |
| 550 | */ |
| 551 | private NetworkStats rollupNetworkStatsByFGBG(NetworkStats stats) { |
| 552 | final NetworkStats ret = new NetworkStats(stats.getElapsedRealtime(), 1); |
| 553 | |
| 554 | final NetworkStats.Entry entry = new NetworkStats.Entry(); |
| 555 | entry.iface = NetworkStats.IFACE_ALL; |
| 556 | entry.tag = NetworkStats.TAG_NONE; |
| 557 | entry.metered = NetworkStats.METERED_ALL; |
| 558 | entry.roaming = NetworkStats.ROAMING_ALL; |
| 559 | |
| 560 | int size = stats.size(); |
| 561 | NetworkStats.Entry recycle = new NetworkStats.Entry(); // Used for retrieving values |
| 562 | for (int i = 0; i < size; i++) { |
| 563 | stats.getValues(i, recycle); |
| 564 | |
| 565 | // Skip specific tags, since already counted in TAG_NONE |
| 566 | if (recycle.tag != NetworkStats.TAG_NONE) continue; |
| 567 | |
| 568 | entry.set = recycle.set; // Allows slicing by background/foreground |
| 569 | entry.uid = recycle.uid; |
| 570 | entry.rxBytes = recycle.rxBytes; |
| 571 | entry.rxPackets = recycle.rxPackets; |
| 572 | entry.txBytes = recycle.txBytes; |
| 573 | entry.txPackets = recycle.txPackets; |
| 574 | // Operations purposefully omitted since we don't use them for statsd. |
| 575 | ret.combineValues(entry); |
| 576 | } |
| 577 | return ret; |
| 578 | } |
| 579 | |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 580 | /** |
| 581 | * Helper method to extract the Parcelable controller info from a |
| 582 | * SynchronousResultReceiver. |
| 583 | */ |
| 584 | private static <T extends Parcelable> T awaitControllerInfo( |
| 585 | @Nullable SynchronousResultReceiver receiver) { |
| 586 | if (receiver == null) { |
| 587 | return null; |
| 588 | } |
| 589 | |
| 590 | try { |
| 591 | final SynchronousResultReceiver.Result result = |
| 592 | receiver.awaitResult(EXTERNAL_STATS_SYNC_TIMEOUT_MILLIS); |
| 593 | if (result.bundle != null) { |
| 594 | // This is the final destination for the Bundle. |
| 595 | result.bundle.setDefusable(true); |
| 596 | |
| 597 | final T data = result.bundle.getParcelable( |
| 598 | RESULT_RECEIVER_CONTROLLER_KEY); |
| 599 | if (data != null) { |
| 600 | return data; |
| 601 | } |
| 602 | } |
| 603 | Slog.e(TAG, "no controller energy info supplied for " + receiver.getName()); |
| 604 | } catch (TimeoutException e) { |
| 605 | Slog.w(TAG, "timeout reading " + receiver.getName() + " stats"); |
| 606 | } |
| 607 | return null; |
| 608 | } |
| 609 | |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 610 | private void pullKernelWakelock(int tagId, List<StatsLogEventWrapper> pulledData) { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 611 | final KernelWakelockStats wakelockStats = |
| 612 | mKernelWakelockReader.readKernelWakelockStats(mTmpWakelockStats); |
Yangster-mac | c552b35 | 2018-02-22 10:00:45 -0800 | [diff] [blame] | 613 | long elapsedNanos = SystemClock.elapsedRealtimeNanos(); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 614 | for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) { |
| 615 | String name = ent.getKey(); |
| 616 | KernelWakelockStats.Entry kws = ent.getValue(); |
Yangster-mac | c552b35 | 2018-02-22 10:00:45 -0800 | [diff] [blame] | 617 | StatsLogEventWrapper e = new StatsLogEventWrapper(elapsedNanos, tagId, 4); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 618 | e.writeString(name); |
| 619 | e.writeInt(kws.mCount); |
| 620 | e.writeInt(kws.mVersion); |
| 621 | e.writeLong(kws.mTotalTime); |
| 622 | pulledData.add(e); |
| 623 | } |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | private void pullWifiBytesTransfer(int tagId, List<StatsLogEventWrapper> pulledData) { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 627 | long token = Binder.clearCallingIdentity(); |
| 628 | try { |
| 629 | // TODO: Consider caching the following call to get BatteryStatsInternal. |
| 630 | BatteryStatsInternal bs = LocalServices.getService(BatteryStatsInternal.class); |
| 631 | String[] ifaces = bs.getWifiIfaces(); |
| 632 | if (ifaces.length == 0) { |
| 633 | return; |
| 634 | } |
| 635 | NetworkStatsFactory nsf = new NetworkStatsFactory(); |
| 636 | // Combine all the metrics per Uid into one record. |
| 637 | NetworkStats stats = |
| 638 | nsf.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces, NetworkStats.TAG_NONE, null) |
| 639 | .groupedByUid(); |
| 640 | addNetworkStats(tagId, pulledData, stats, false); |
| 641 | } catch (java.io.IOException e) { |
| 642 | Slog.e(TAG, "Pulling netstats for wifi bytes has error", e); |
| 643 | } finally { |
| 644 | Binder.restoreCallingIdentity(token); |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 645 | } |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | private void pullWifiBytesTransferByFgBg(int tagId, List<StatsLogEventWrapper> pulledData) { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 649 | long token = Binder.clearCallingIdentity(); |
| 650 | try { |
| 651 | BatteryStatsInternal bs = LocalServices.getService(BatteryStatsInternal.class); |
| 652 | String[] ifaces = bs.getWifiIfaces(); |
| 653 | if (ifaces.length == 0) { |
| 654 | return; |
| 655 | } |
| 656 | NetworkStatsFactory nsf = new NetworkStatsFactory(); |
| 657 | NetworkStats stats = rollupNetworkStatsByFGBG( |
| 658 | nsf.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces, NetworkStats.TAG_NONE, null)); |
| 659 | addNetworkStats(tagId, pulledData, stats, true); |
| 660 | } catch (java.io.IOException e) { |
| 661 | Slog.e(TAG, "Pulling netstats for wifi bytes w/ fg/bg has error", e); |
| 662 | } finally { |
| 663 | Binder.restoreCallingIdentity(token); |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 664 | } |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | private void pullMobileBytesTransfer(int tagId, List<StatsLogEventWrapper> pulledData) { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 668 | long token = Binder.clearCallingIdentity(); |
| 669 | try { |
| 670 | BatteryStatsInternal bs = LocalServices.getService(BatteryStatsInternal.class); |
| 671 | String[] ifaces = bs.getMobileIfaces(); |
| 672 | if (ifaces.length == 0) { |
| 673 | return; |
| 674 | } |
| 675 | NetworkStatsFactory nsf = new NetworkStatsFactory(); |
| 676 | // Combine all the metrics per Uid into one record. |
| 677 | NetworkStats stats = |
| 678 | nsf.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces, NetworkStats.TAG_NONE, null) |
| 679 | .groupedByUid(); |
| 680 | addNetworkStats(tagId, pulledData, stats, false); |
| 681 | } catch (java.io.IOException e) { |
| 682 | Slog.e(TAG, "Pulling netstats for mobile bytes has error", e); |
| 683 | } finally { |
| 684 | Binder.restoreCallingIdentity(token); |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 685 | } |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | private void pullBluetoothBytesTransfer(int tagId, List<StatsLogEventWrapper> pulledData) { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 689 | BluetoothActivityEnergyInfo info = pullBluetoothData(); |
Yangster-mac | c552b35 | 2018-02-22 10:00:45 -0800 | [diff] [blame] | 690 | long elapsedNanos = SystemClock.elapsedRealtimeNanos(); |
Chenjie Yu | 9e59f93 | 2018-03-30 16:41:38 -0700 | [diff] [blame] | 691 | if (info.getUidTraffic() != null) { |
| 692 | for (UidTraffic traffic : info.getUidTraffic()) { |
| 693 | StatsLogEventWrapper e = new StatsLogEventWrapper(elapsedNanos, tagId, 3); |
| 694 | e.writeInt(traffic.getUid()); |
| 695 | e.writeLong(traffic.getRxBytes()); |
| 696 | e.writeLong(traffic.getTxBytes()); |
| 697 | pulledData.add(e); |
| 698 | } |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 699 | } |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | private void pullMobileBytesTransferByFgBg(int tagId, List<StatsLogEventWrapper> pulledData) { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 703 | long token = Binder.clearCallingIdentity(); |
| 704 | try { |
| 705 | BatteryStatsInternal bs = LocalServices.getService(BatteryStatsInternal.class); |
| 706 | String[] ifaces = bs.getMobileIfaces(); |
| 707 | if (ifaces.length == 0) { |
| 708 | return; |
| 709 | } |
| 710 | NetworkStatsFactory nsf = new NetworkStatsFactory(); |
| 711 | NetworkStats stats = rollupNetworkStatsByFGBG( |
| 712 | nsf.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces, NetworkStats.TAG_NONE, null)); |
| 713 | addNetworkStats(tagId, pulledData, stats, true); |
| 714 | } catch (java.io.IOException e) { |
| 715 | Slog.e(TAG, "Pulling netstats for mobile bytes w/ fg/bg has error", e); |
| 716 | } finally { |
| 717 | Binder.restoreCallingIdentity(token); |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 718 | } |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | private void pullCpuTimePerFreq(int tagId, List<StatsLogEventWrapper> pulledData) { |
Yangster-mac | c552b35 | 2018-02-22 10:00:45 -0800 | [diff] [blame] | 722 | long elapsedNanos = SystemClock.elapsedRealtimeNanos(); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 723 | for (int cluster = 0; cluster < mKernelCpuSpeedReaders.length; cluster++) { |
| 724 | long[] clusterTimeMs = mKernelCpuSpeedReaders[cluster].readAbsolute(); |
| 725 | if (clusterTimeMs != null) { |
| 726 | for (int speed = clusterTimeMs.length - 1; speed >= 0; --speed) { |
Yangster-mac | c552b35 | 2018-02-22 10:00:45 -0800 | [diff] [blame] | 727 | StatsLogEventWrapper e = new StatsLogEventWrapper(elapsedNanos, tagId, 3); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 728 | e.writeInt(cluster); |
| 729 | e.writeInt(speed); |
| 730 | e.writeLong(clusterTimeMs[speed]); |
| 731 | pulledData.add(e); |
| 732 | } |
| 733 | } |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 734 | } |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 735 | } |
| 736 | |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 737 | private void pullKernelUidCpuTime(int tagId, List<StatsLogEventWrapper> pulledData) { |
| 738 | long elapsedNanos = SystemClock.elapsedRealtimeNanos(); |
| 739 | mKernelUidCpuTimeReader.readAbsolute((uid, userTimeUs, systemTimeUs) -> { |
| 740 | StatsLogEventWrapper e = new StatsLogEventWrapper(elapsedNanos, tagId, 3); |
| 741 | e.writeInt(uid); |
| 742 | e.writeLong(userTimeUs); |
| 743 | e.writeLong(systemTimeUs); |
| 744 | pulledData.add(e); |
| 745 | }); |
| 746 | } |
| 747 | |
| 748 | private void pullKernelUidCpuFreqTime(int tagId, List<StatsLogEventWrapper> pulledData) { |
| 749 | long elapsedNanos = SystemClock.elapsedRealtimeNanos(); |
| 750 | mKernelUidCpuFreqTimeReader.readAbsolute((uid, cpuFreqTimeMs) -> { |
| 751 | for (int freqIndex = 0; freqIndex < cpuFreqTimeMs.length; ++freqIndex) { |
Chenjie Yu | 4460e3c | 2018-03-14 17:12:59 -0700 | [diff] [blame] | 752 | if(cpuFreqTimeMs[freqIndex] != 0) { |
| 753 | StatsLogEventWrapper e = new StatsLogEventWrapper(elapsedNanos, tagId, 3); |
| 754 | e.writeInt(uid); |
| 755 | e.writeInt(freqIndex); |
| 756 | e.writeLong(cpuFreqTimeMs[freqIndex]); |
| 757 | pulledData.add(e); |
| 758 | } |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 759 | } |
| 760 | }); |
| 761 | } |
| 762 | |
| 763 | private void pullKernelUidCpuClusterTime(int tagId, List<StatsLogEventWrapper> pulledData) { |
| 764 | long elapsedNanos = SystemClock.elapsedRealtimeNanos(); |
| 765 | mKernelUidCpuClusterTimeReader.readAbsolute((uid, cpuClusterTimesMs) -> { |
| 766 | for (int i = 0; i < cpuClusterTimesMs.length; i++) { |
| 767 | StatsLogEventWrapper e = new StatsLogEventWrapper(elapsedNanos, tagId, 3); |
| 768 | e.writeInt(uid); |
| 769 | e.writeInt(i); |
| 770 | e.writeLong(cpuClusterTimesMs[i]); |
| 771 | pulledData.add(e); |
| 772 | } |
| 773 | }); |
| 774 | } |
| 775 | |
| 776 | private void pullKernelUidCpuActiveTime(int tagId, List<StatsLogEventWrapper> pulledData) { |
| 777 | long elapsedNanos = SystemClock.elapsedRealtimeNanos(); |
| 778 | mKernelUidCpuActiveTimeReader.readAbsolute((uid, cpuActiveTimesMs) -> { |
| 779 | StatsLogEventWrapper e = new StatsLogEventWrapper(elapsedNanos, tagId, 2); |
| 780 | e.writeInt(uid); |
| 781 | e.writeLong((long)cpuActiveTimesMs); |
| 782 | pulledData.add(e); |
| 783 | }); |
| 784 | } |
| 785 | |
Chenjie Yu | 5caaa9d | 2018-03-06 15:48:54 -0800 | [diff] [blame] | 786 | private void pullWifiActivityInfo(int tagId, List<StatsLogEventWrapper> pulledData) { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 787 | long token = Binder.clearCallingIdentity(); |
| 788 | if (mWifiManager == null) { |
| 789 | mWifiManager = |
| 790 | IWifiManager.Stub.asInterface(ServiceManager.getService(Context.WIFI_SERVICE)); |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 791 | } |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 792 | if (mWifiManager != null) { |
| 793 | try { |
| 794 | SynchronousResultReceiver wifiReceiver = new SynchronousResultReceiver("wifi"); |
| 795 | mWifiManager.requestActivityInfo(wifiReceiver); |
| 796 | final WifiActivityEnergyInfo wifiInfo = awaitControllerInfo(wifiReceiver); |
Yangster-mac | c552b35 | 2018-02-22 10:00:45 -0800 | [diff] [blame] | 797 | StatsLogEventWrapper e = new StatsLogEventWrapper(SystemClock.elapsedRealtimeNanos(), tagId, 6); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 798 | e.writeLong(wifiInfo.getTimeStamp()); |
| 799 | e.writeInt(wifiInfo.getStackState()); |
| 800 | e.writeLong(wifiInfo.getControllerTxTimeMillis()); |
| 801 | e.writeLong(wifiInfo.getControllerRxTimeMillis()); |
| 802 | e.writeLong(wifiInfo.getControllerIdleTimeMillis()); |
| 803 | e.writeLong(wifiInfo.getControllerEnergyUsed()); |
| 804 | pulledData.add(e); |
| 805 | } catch (RemoteException e) { |
| 806 | Slog.e(TAG, "Pulling wifiManager for wifi controller activity energy info has error", e); |
| 807 | } finally { |
| 808 | Binder.restoreCallingIdentity(token); |
| 809 | } |
| 810 | } |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | private void pullModemActivityInfo(int tagId, List<StatsLogEventWrapper> pulledData) { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 814 | long token = Binder.clearCallingIdentity(); |
| 815 | if (mTelephony == null) { |
| 816 | mTelephony = TelephonyManager.from(mContext); |
| 817 | } |
| 818 | if (mTelephony != null) { |
| 819 | SynchronousResultReceiver modemReceiver = new SynchronousResultReceiver("telephony"); |
| 820 | mTelephony.requestModemActivityInfo(modemReceiver); |
| 821 | final ModemActivityInfo modemInfo = awaitControllerInfo(modemReceiver); |
David Chen | 56ae0d9 | 2018-05-11 16:00:22 -0700 | [diff] [blame] | 822 | StatsLogEventWrapper e = new StatsLogEventWrapper(SystemClock.elapsedRealtimeNanos(), tagId, 10); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 823 | e.writeLong(modemInfo.getTimestamp()); |
| 824 | e.writeLong(modemInfo.getSleepTimeMillis()); |
| 825 | e.writeLong(modemInfo.getIdleTimeMillis()); |
| 826 | e.writeLong(modemInfo.getTxTimeMillis()[0]); |
| 827 | e.writeLong(modemInfo.getTxTimeMillis()[1]); |
| 828 | e.writeLong(modemInfo.getTxTimeMillis()[2]); |
| 829 | e.writeLong(modemInfo.getTxTimeMillis()[3]); |
| 830 | e.writeLong(modemInfo.getTxTimeMillis()[4]); |
| 831 | e.writeLong(modemInfo.getRxTimeMillis()); |
| 832 | e.writeLong(modemInfo.getEnergyUsed()); |
| 833 | pulledData.add(e); |
| 834 | } |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | private void pullBluetoothActivityInfo(int tagId, List<StatsLogEventWrapper> pulledData) { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 838 | BluetoothActivityEnergyInfo info = pullBluetoothData(); |
Yangster-mac | c552b35 | 2018-02-22 10:00:45 -0800 | [diff] [blame] | 839 | StatsLogEventWrapper e = new StatsLogEventWrapper(SystemClock.elapsedRealtimeNanos(), tagId, 6); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 840 | e.writeLong(info.getTimeStamp()); |
| 841 | e.writeInt(info.getBluetoothStackState()); |
| 842 | e.writeLong(info.getControllerTxTimeMillis()); |
| 843 | e.writeLong(info.getControllerRxTimeMillis()); |
| 844 | e.writeLong(info.getControllerIdleTimeMillis()); |
| 845 | e.writeLong(info.getControllerEnergyUsed()); |
| 846 | pulledData.add(e); |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | private synchronized BluetoothActivityEnergyInfo pullBluetoothData() { |
Chenjie Yu | c2d6544 | 2018-02-01 22:27:17 -0800 | [diff] [blame] | 850 | final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 851 | if (adapter != null) { |
| 852 | SynchronousResultReceiver bluetoothReceiver = new SynchronousResultReceiver("bluetooth"); |
| 853 | adapter.requestControllerActivityEnergyInfo(bluetoothReceiver); |
| 854 | return awaitControllerInfo(bluetoothReceiver); |
| 855 | } else { |
| 856 | Slog.e(TAG, "Failed to get bluetooth adapter!"); |
| 857 | return null; |
| 858 | } |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | private void pullSystemElapsedRealtime(int tagId, List<StatsLogEventWrapper> pulledData) { |
Yangster-mac | c552b35 | 2018-02-22 10:00:45 -0800 | [diff] [blame] | 862 | StatsLogEventWrapper e = new StatsLogEventWrapper(SystemClock.elapsedRealtimeNanos(), tagId, 1); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 863 | e.writeLong(SystemClock.elapsedRealtime()); |
| 864 | pulledData.add(e); |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 865 | } |
| 866 | |
Tej Singh | 6188aa3 | 2018-08-29 00:21:07 +0000 | [diff] [blame] | 867 | private void pullDiskSpace(int tagId, List<StatsLogEventWrapper> pulledData) { |
| 868 | StatsLogEventWrapper e = new StatsLogEventWrapper(SystemClock.elapsedRealtimeNanos(), tagId, 3); |
| 869 | e.writeLong(mStatFsData.getAvailableBytes()); |
| 870 | e.writeLong(mStatFsSystem.getAvailableBytes()); |
| 871 | e.writeLong(mStatFsTemp.getAvailableBytes()); |
| 872 | pulledData.add(e); |
| 873 | } |
| 874 | |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 875 | private void pullSystemUpTime(int tagId, List<StatsLogEventWrapper> pulledData) { |
Yangster-mac | c552b35 | 2018-02-22 10:00:45 -0800 | [diff] [blame] | 876 | StatsLogEventWrapper e = new StatsLogEventWrapper(SystemClock.elapsedRealtimeNanos(), tagId, 1); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 877 | e.writeLong(SystemClock.uptimeMillis()); |
| 878 | pulledData.add(e); |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 879 | } |
| 880 | |
Rajeev Kumar | 22d92b7 | 2018-02-07 18:38:36 -0800 | [diff] [blame] | 881 | private void pullProcessMemoryState(int tagId, List<StatsLogEventWrapper> pulledData) { |
| 882 | List<ProcessMemoryState> processMemoryStates = |
| 883 | LocalServices.getService(ActivityManagerInternal.class) |
| 884 | .getMemoryStateForProcesses(); |
Yangster-mac | c552b35 | 2018-02-22 10:00:45 -0800 | [diff] [blame] | 885 | long elapsedNanos = SystemClock.elapsedRealtimeNanos(); |
Rajeev Kumar | 22d92b7 | 2018-02-07 18:38:36 -0800 | [diff] [blame] | 886 | for (ProcessMemoryState processMemoryState : processMemoryStates) { |
Yangster-mac | c552b35 | 2018-02-22 10:00:45 -0800 | [diff] [blame] | 887 | StatsLogEventWrapper e = new StatsLogEventWrapper(elapsedNanos, tagId, 8 /* fields */); |
Rajeev Kumar | 22d92b7 | 2018-02-07 18:38:36 -0800 | [diff] [blame] | 888 | e.writeInt(processMemoryState.uid); |
| 889 | e.writeString(processMemoryState.processName); |
| 890 | e.writeInt(processMemoryState.oomScore); |
| 891 | e.writeLong(processMemoryState.pgfault); |
| 892 | e.writeLong(processMemoryState.pgmajfault); |
| 893 | e.writeLong(processMemoryState.rssInBytes); |
| 894 | e.writeLong(processMemoryState.cacheInBytes); |
| 895 | e.writeLong(processMemoryState.swapInBytes); |
| 896 | pulledData.add(e); |
| 897 | } |
| 898 | } |
| 899 | |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 900 | private void pullBinderCallsStats(int tagId, List<StatsLogEventWrapper> pulledData) { |
Olivier Gaillard | 289ba40 | 2018-07-24 18:50:13 +0100 | [diff] [blame] | 901 | BinderCallsStatsService.Internal binderStats = |
| 902 | LocalServices.getService(BinderCallsStatsService.Internal.class); |
Olivier Gaillard | 44a60a9 | 2018-07-25 22:53:55 +0100 | [diff] [blame] | 903 | if (binderStats == null) { |
| 904 | return; |
| 905 | } |
| 906 | |
Olivier Gaillard | 289ba40 | 2018-07-24 18:50:13 +0100 | [diff] [blame] | 907 | List<ExportedCallStat> callStats = binderStats.getExportedCallStats(); |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 908 | long elapsedNanos = SystemClock.elapsedRealtimeNanos(); |
| 909 | for (ExportedCallStat callStat : callStats) { |
Olivier Gaillard | 6f52d15 | 2018-07-25 12:13:12 +0100 | [diff] [blame] | 910 | StatsLogEventWrapper e = new StatsLogEventWrapper(elapsedNanos, tagId, 13 /* fields */); |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 911 | e.writeInt(callStat.uid); |
| 912 | e.writeString(callStat.className); |
| 913 | e.writeString(callStat.methodName); |
| 914 | e.writeLong(callStat.callCount); |
| 915 | e.writeLong(callStat.exceptionCount); |
| 916 | e.writeLong(callStat.latencyMicros); |
| 917 | e.writeLong(callStat.maxLatencyMicros); |
| 918 | e.writeLong(callStat.cpuTimeMicros); |
| 919 | e.writeLong(callStat.maxCpuTimeMicros); |
| 920 | e.writeLong(callStat.maxReplySizeBytes); |
| 921 | e.writeLong(callStat.maxRequestSizeBytes); |
Olivier Gaillard | 6f52d15 | 2018-07-25 12:13:12 +0100 | [diff] [blame] | 922 | e.writeLong(callStat.recordedCallCount); |
| 923 | e.writeInt(callStat.screenInteractive ? 1 : 0); |
| 924 | pulledData.add(e); |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | private void pullBinderCallsStatsExceptions(int tagId, List<StatsLogEventWrapper> pulledData) { |
| 929 | BinderCallsStatsService.Internal binderStats = |
| 930 | LocalServices.getService(BinderCallsStatsService.Internal.class); |
Olivier Gaillard | 44a60a9 | 2018-07-25 22:53:55 +0100 | [diff] [blame] | 931 | if (binderStats == null) { |
| 932 | return; |
| 933 | } |
| 934 | |
Olivier Gaillard | 6f52d15 | 2018-07-25 12:13:12 +0100 | [diff] [blame] | 935 | ArrayMap<String, Integer> exceptionStats = binderStats.getExportedExceptionStats(); |
| 936 | long elapsedNanos = SystemClock.elapsedRealtimeNanos(); |
| 937 | for (Entry<String, Integer> entry : exceptionStats.entrySet()) { |
| 938 | StatsLogEventWrapper e = new StatsLogEventWrapper(elapsedNanos, tagId, 2 /* fields */); |
| 939 | e.writeString(entry.getKey()); |
| 940 | e.writeInt(entry.getValue()); |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 941 | pulledData.add(e); |
| 942 | } |
| 943 | } |
| 944 | |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 945 | /** |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 946 | * Pulls various data. |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 947 | */ |
Bookatz | c68a9d2 | 2017-09-27 14:09:55 -0700 | [diff] [blame] | 948 | @Override // Binder call |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 949 | public StatsLogEventWrapper[] pullData(int tagId) { |
Bookatz | c68a9d2 | 2017-09-27 14:09:55 -0700 | [diff] [blame] | 950 | enforceCallingPermission(); |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 951 | if (DEBUG) |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 952 | Slog.d(TAG, "Pulling " + tagId); |
Rajeev Kumar | 22d92b7 | 2018-02-07 18:38:36 -0800 | [diff] [blame] | 953 | List<StatsLogEventWrapper> ret = new ArrayList<>(); |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 954 | switch (tagId) { |
Chenjie Yu | 31d14d7 | 2017-12-12 17:54:33 -0800 | [diff] [blame] | 955 | case StatsLog.WIFI_BYTES_TRANSFER: { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 956 | pullWifiBytesTransfer(tagId, ret); |
| 957 | break; |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 958 | } |
Chenjie Yu | 31d14d7 | 2017-12-12 17:54:33 -0800 | [diff] [blame] | 959 | case StatsLog.MOBILE_BYTES_TRANSFER: { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 960 | pullMobileBytesTransfer(tagId, ret); |
| 961 | break; |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 962 | } |
Chenjie Yu | 31d14d7 | 2017-12-12 17:54:33 -0800 | [diff] [blame] | 963 | case StatsLog.WIFI_BYTES_TRANSFER_BY_FG_BG: { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 964 | pullWifiBytesTransferByFgBg(tagId, ret); |
| 965 | break; |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 966 | } |
Chenjie Yu | 31d14d7 | 2017-12-12 17:54:33 -0800 | [diff] [blame] | 967 | case StatsLog.MOBILE_BYTES_TRANSFER_BY_FG_BG: { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 968 | pullMobileBytesTransferByFgBg(tagId, ret); |
| 969 | break; |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 970 | } |
| 971 | case StatsLog.BLUETOOTH_BYTES_TRANSFER: { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 972 | pullBluetoothBytesTransfer(tagId, ret); |
| 973 | break; |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 974 | } |
Chenjie Yu | 31d14d7 | 2017-12-12 17:54:33 -0800 | [diff] [blame] | 975 | case StatsLog.KERNEL_WAKELOCK: { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 976 | pullKernelWakelock(tagId, ret); |
| 977 | break; |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 978 | } |
Chenjie Yu | 31d14d7 | 2017-12-12 17:54:33 -0800 | [diff] [blame] | 979 | case StatsLog.CPU_TIME_PER_FREQ: { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 980 | pullCpuTimePerFreq(tagId, ret); |
| 981 | break; |
Chenjie Yu | 7f8def9 | 2017-11-03 09:33:15 -0700 | [diff] [blame] | 982 | } |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 983 | case StatsLog.CPU_TIME_PER_UID: { |
| 984 | pullKernelUidCpuTime(tagId, ret); |
| 985 | break; |
| 986 | } |
| 987 | case StatsLog.CPU_TIME_PER_UID_FREQ: { |
| 988 | pullKernelUidCpuFreqTime(tagId, ret); |
| 989 | break; |
| 990 | } |
| 991 | case StatsLog.CPU_CLUSTER_TIME: { |
| 992 | pullKernelUidCpuClusterTime(tagId, ret); |
| 993 | break; |
| 994 | } |
| 995 | case StatsLog.CPU_ACTIVE_TIME: { |
| 996 | pullKernelUidCpuActiveTime(tagId, ret); |
| 997 | break; |
| 998 | } |
Chenjie Yu | 5caaa9d | 2018-03-06 15:48:54 -0800 | [diff] [blame] | 999 | case StatsLog.WIFI_ACTIVITY_INFO: { |
| 1000 | pullWifiActivityInfo(tagId, ret); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 1001 | break; |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 1002 | } |
Chenjie Yu | 31d14d7 | 2017-12-12 17:54:33 -0800 | [diff] [blame] | 1003 | case StatsLog.MODEM_ACTIVITY_INFO: { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 1004 | pullModemActivityInfo(tagId, ret); |
| 1005 | break; |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 1006 | } |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 1007 | case StatsLog.BLUETOOTH_ACTIVITY_INFO: { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 1008 | pullBluetoothActivityInfo(tagId, ret); |
| 1009 | break; |
Chenjie Yu | 937d742 | 2018-01-10 16:37:53 -0800 | [diff] [blame] | 1010 | } |
| 1011 | case StatsLog.SYSTEM_UPTIME: { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 1012 | pullSystemUpTime(tagId, ret); |
| 1013 | break; |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 1014 | } |
| 1015 | case StatsLog.SYSTEM_ELAPSED_REALTIME: { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 1016 | pullSystemElapsedRealtime(tagId, ret); |
| 1017 | break; |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 1018 | } |
Tej Singh | 6188aa3 | 2018-08-29 00:21:07 +0000 | [diff] [blame] | 1019 | case StatsLog.DISK_SPACE: { |
| 1020 | pullDiskSpace(tagId, ret); |
| 1021 | break; |
| 1022 | } |
Rajeev Kumar | 22d92b7 | 2018-02-07 18:38:36 -0800 | [diff] [blame] | 1023 | case StatsLog.PROCESS_MEMORY_STATE: { |
| 1024 | pullProcessMemoryState(tagId, ret); |
| 1025 | break; |
| 1026 | } |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 1027 | case StatsLog.BINDER_CALLS: { |
| 1028 | pullBinderCallsStats(tagId, ret); |
| 1029 | break; |
| 1030 | } |
Olivier Gaillard | 6f52d15 | 2018-07-25 12:13:12 +0100 | [diff] [blame] | 1031 | case StatsLog.BINDER_CALLS_EXCEPTIONS: { |
| 1032 | pullBinderCallsStatsExceptions(tagId, ret); |
| 1033 | break; |
| 1034 | } |
Bookatz | c68a9d2 | 2017-09-27 14:09:55 -0700 | [diff] [blame] | 1035 | default: |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 1036 | Slog.w(TAG, "No such tagId data as " + tagId); |
Bookatz | c68a9d2 | 2017-09-27 14:09:55 -0700 | [diff] [blame] | 1037 | return null; |
| 1038 | } |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 1039 | return ret.toArray(new StatsLogEventWrapper[ret.size()]); |
Bookatz | c68a9d2 | 2017-09-27 14:09:55 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | @Override // Binder call |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 1043 | public void statsdReady() { |
| 1044 | enforceCallingPermission(); |
| 1045 | if (DEBUG) Slog.d(TAG, "learned that statsdReady"); |
| 1046 | sayHiToStatsd(); // tell statsd that we're ready too and link to it |
Bookatz | 8178408 | 2018-02-09 14:26:42 -0800 | [diff] [blame] | 1047 | mContext.sendBroadcastAsUser( |
| 1048 | new Intent(StatsManager.ACTION_STATSD_STARTED) |
| 1049 | .addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND), |
Bookatz | 0eb58ae39 | 2018-01-25 15:49:26 -0800 | [diff] [blame] | 1050 | UserHandle.SYSTEM, |
Bookatz | 5c800e3 | 2018-01-24 14:59:52 -0800 | [diff] [blame] | 1051 | android.Manifest.permission.DUMP); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 1052 | } |
| 1053 | |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 1054 | @Override |
| 1055 | public void triggerUidSnapshot() { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 1056 | enforceCallingPermission(); |
| 1057 | synchronized (sStatsdLock) { |
David Chen | 35045cb | 2018-03-23 22:21:47 -0700 | [diff] [blame] | 1058 | final long token = Binder.clearCallingIdentity(); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 1059 | try { |
| 1060 | informAllUidsLocked(mContext); |
| 1061 | } catch (RemoteException e) { |
| 1062 | Slog.e(TAG, "Failed to trigger uid snapshot.", e); |
David Chen | 35045cb | 2018-03-23 22:21:47 -0700 | [diff] [blame] | 1063 | } finally { |
| 1064 | restoreCallingIdentity(token); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 1065 | } |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 1066 | } |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 1067 | } |
| 1068 | |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 1069 | private void enforceCallingPermission() { |
| 1070 | if (Binder.getCallingPid() == Process.myPid()) { |
| 1071 | return; |
| 1072 | } |
| 1073 | mContext.enforceCallingPermission(android.Manifest.permission.STATSCOMPANION, null); |
| 1074 | } |
| 1075 | |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 1076 | // Lifecycle and related code |
| 1077 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 1078 | /** |
| 1079 | * Fetches the statsd IBinder service |
| 1080 | */ |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 1081 | private static IStatsManager fetchStatsdService() { |
| 1082 | return IStatsManager.Stub.asInterface(ServiceManager.getService("stats")); |
| 1083 | } |
| 1084 | |
| 1085 | public static final class Lifecycle extends SystemService { |
| 1086 | private StatsCompanionService mStatsCompanionService; |
| 1087 | |
| 1088 | public Lifecycle(Context context) { |
| 1089 | super(context); |
| 1090 | } |
| 1091 | |
| 1092 | @Override |
| 1093 | public void onStart() { |
| 1094 | mStatsCompanionService = new StatsCompanionService(getContext()); |
| 1095 | try { |
| 1096 | publishBinderService(Context.STATS_COMPANION_SERVICE, mStatsCompanionService); |
| 1097 | if (DEBUG) Slog.d(TAG, "Published " + Context.STATS_COMPANION_SERVICE); |
| 1098 | } catch (Exception e) { |
| 1099 | Slog.e(TAG, "Failed to publishBinderService", e); |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | @Override |
| 1104 | public void onBootPhase(int phase) { |
| 1105 | super.onBootPhase(phase); |
| 1106 | if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) { |
| 1107 | mStatsCompanionService.systemReady(); |
| 1108 | } |
| 1109 | } |
| 1110 | } |
| 1111 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 1112 | /** |
| 1113 | * Now that the android system is ready, StatsCompanion is ready too, so inform statsd. |
| 1114 | */ |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 1115 | private void systemReady() { |
| 1116 | if (DEBUG) Slog.d(TAG, "Learned that systemReady"); |
| 1117 | sayHiToStatsd(); |
| 1118 | } |
| 1119 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 1120 | /** |
| 1121 | * Tells statsd that statscompanion is ready. If the binder call returns, link to statsd. |
| 1122 | */ |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 1123 | private void sayHiToStatsd() { |
| 1124 | synchronized (sStatsdLock) { |
| 1125 | if (sStatsd != null) { |
| 1126 | Slog.e(TAG, "Trying to fetch statsd, but it was already fetched", |
| 1127 | new IllegalStateException("sStatsd is not null when being fetched")); |
| 1128 | return; |
| 1129 | } |
| 1130 | sStatsd = fetchStatsdService(); |
| 1131 | if (sStatsd == null) { |
Bookatz | 111ed73 | 2018-02-16 15:54:05 -0800 | [diff] [blame] | 1132 | Slog.i(TAG, "Could not yet find statsd to tell it that StatsCompanion is alive."); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 1133 | return; |
| 1134 | } |
| 1135 | if (DEBUG) Slog.d(TAG, "Saying hi to statsd"); |
| 1136 | try { |
| 1137 | sStatsd.statsCompanionReady(); |
| 1138 | // If the statsCompanionReady two-way binder call returns, link to statsd. |
| 1139 | try { |
| 1140 | sStatsd.asBinder().linkToDeath(new StatsdDeathRecipient(), 0); |
| 1141 | } catch (RemoteException e) { |
| 1142 | Slog.e(TAG, "linkToDeath(StatsdDeathRecipient) failed", e); |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 1143 | forgetEverythingLocked(); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 1144 | } |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 1145 | // Setup broadcast receiver for updates. |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 1146 | IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_REPLACED); |
| 1147 | filter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 1148 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 1149 | filter.addDataScheme("package"); |
| 1150 | mContext.registerReceiverAsUser(mAppUpdateReceiver, UserHandle.ALL, filter, null, |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 1151 | null); |
David Chen | 47e8f4d | 2017-10-11 15:34:13 -0700 | [diff] [blame] | 1152 | |
| 1153 | // Setup receiver for user initialize (which happens once for a new user) and |
| 1154 | // if a user is removed. |
| 1155 | filter = new IntentFilter(Intent.ACTION_USER_INITIALIZE); |
| 1156 | filter.addAction(Intent.ACTION_USER_REMOVED); |
| 1157 | mContext.registerReceiverAsUser(mUserUpdateReceiver, UserHandle.ALL, |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 1158 | filter, null, null); |
David Chen | 47e8f4d | 2017-10-11 15:34:13 -0700 | [diff] [blame] | 1159 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 1160 | // Setup receiver for device reboots or shutdowns. |
| 1161 | filter = new IntentFilter(Intent.ACTION_REBOOT); |
| 1162 | filter.addAction(Intent.ACTION_SHUTDOWN); |
| 1163 | mContext.registerReceiverAsUser( |
| 1164 | mShutdownEventReceiver, UserHandle.ALL, filter, null, null); |
Yao Chen | 0f21710 | 2018-01-09 10:33:15 -0800 | [diff] [blame] | 1165 | final long token = Binder.clearCallingIdentity(); |
| 1166 | try { |
| 1167 | // Pull the latest state of UID->app name, version mapping when statsd starts. |
| 1168 | informAllUidsLocked(mContext); |
| 1169 | } finally { |
| 1170 | restoreCallingIdentity(token); |
| 1171 | } |
Bookatz | 111ed73 | 2018-02-16 15:54:05 -0800 | [diff] [blame] | 1172 | Slog.i(TAG, "Told statsd that StatsCompanionService is alive."); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 1173 | } catch (RemoteException e) { |
| 1174 | Slog.e(TAG, "Failed to inform statsd that statscompanion is ready", e); |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 1175 | forgetEverythingLocked(); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 1176 | } |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | private class StatsdDeathRecipient implements IBinder.DeathRecipient { |
| 1181 | @Override |
| 1182 | public void binderDied() { |
| 1183 | Slog.i(TAG, "Statsd is dead - erase all my knowledge."); |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 1184 | synchronized (sStatsdLock) { |
| 1185 | long now = SystemClock.elapsedRealtime(); |
| 1186 | for (Long timeMillis : mDeathTimeMillis) { |
| 1187 | long ageMillis = now - timeMillis; |
| 1188 | if (ageMillis > MILLIS_IN_A_DAY) { |
| 1189 | mDeathTimeMillis.remove(timeMillis); |
| 1190 | } |
| 1191 | } |
| 1192 | for (Long timeMillis : mDeletedFiles.keySet()) { |
| 1193 | long ageMillis = now - timeMillis; |
| 1194 | if (ageMillis > MILLIS_IN_A_DAY * 7) { |
| 1195 | mDeletedFiles.remove(timeMillis); |
| 1196 | } |
| 1197 | } |
| 1198 | mDeathTimeMillis.add(now); |
| 1199 | if (mDeathTimeMillis.size() >= DEATH_THRESHOLD) { |
| 1200 | mDeathTimeMillis.clear(); |
| 1201 | File[] configs = FileUtils.listFilesOrEmpty(new File(CONFIG_DIR)); |
| 1202 | if (configs.length > 0) { |
| 1203 | String fileName = configs[0].getName(); |
| 1204 | if (configs[0].delete()) { |
| 1205 | mDeletedFiles.put(now, fileName); |
| 1206 | } |
| 1207 | } |
| 1208 | } |
| 1209 | forgetEverythingLocked(); |
| 1210 | } |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 1211 | } |
| 1212 | } |
| 1213 | |
Andreas Gampe | 2952021 | 2018-07-20 12:55:37 -0700 | [diff] [blame] | 1214 | @GuardedBy("StatsCompanionService.sStatsdLock") |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 1215 | private void forgetEverythingLocked() { |
| 1216 | sStatsd = null; |
| 1217 | mContext.unregisterReceiver(mAppUpdateReceiver); |
| 1218 | mContext.unregisterReceiver(mUserUpdateReceiver); |
| 1219 | mContext.unregisterReceiver(mShutdownEventReceiver); |
| 1220 | cancelAnomalyAlarm(); |
| 1221 | cancelPullingAlarm(); |
| 1222 | } |
| 1223 | |
| 1224 | @Override |
| 1225 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
| 1226 | if (!DumpUtils.checkDumpPermission(mContext, TAG, writer)) return; |
| 1227 | |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 1228 | synchronized (sStatsdLock) { |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 1229 | writer.println("Number of configuration files deleted: " + mDeletedFiles.size()); |
| 1230 | if (mDeletedFiles.size() > 0) { |
| 1231 | writer.println(" timestamp, deleted file name"); |
| 1232 | } |
| 1233 | long lastBootMillis = |
| 1234 | SystemClock.currentThreadTimeMillis() - SystemClock.elapsedRealtime(); |
| 1235 | for (Long elapsedMillis : mDeletedFiles.keySet()) { |
| 1236 | long deletionMillis = lastBootMillis + elapsedMillis; |
| 1237 | writer.println(" " + deletionMillis + ", " + mDeletedFiles.get(elapsedMillis)); |
| 1238 | } |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 1239 | } |
| 1240 | } |
| 1241 | |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 1242 | } |