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