blob: 217577281bf855c31d8013a67c4defaa7918152c [file] [log] [blame]
Bookatz94726412017-08-31 09:26:15 -07001/*
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 */
16package com.android.server.stats;
17
Chenjie Yu05013b32017-11-21 10:21:41 -080018import android.annotation.Nullable;
Rajeev Kumar22d92b72018-02-07 18:38:36 -080019import android.app.ActivityManagerInternal;
Bookatz94726412017-08-31 09:26:15 -070020import android.app.AlarmManager;
Bookatz75158ac2018-05-18 15:55:23 -070021import android.app.AlarmManager.OnAlarmListener;
Bookatz94726412017-08-31 09:26:15 -070022import android.app.PendingIntent;
Rajeev Kumar22d92b72018-02-07 18:38:36 -080023import android.app.ProcessMemoryState;
Bookatzc6977972018-01-16 16:55:05 -080024import android.app.StatsManager;
Chenjie Yu9d7720b2018-01-24 10:34:48 -080025import android.bluetooth.BluetoothActivityEnergyInfo;
26import android.bluetooth.BluetoothAdapter;
27import android.bluetooth.UidTraffic;
Bookatz94726412017-08-31 09:26:15 -070028import android.content.BroadcastReceiver;
29import android.content.Context;
30import android.content.Intent;
David Chende701692017-10-05 13:16:02 -070031import android.content.IntentFilter;
Bookatzc6977972018-01-16 16:55:05 -080032import android.content.IntentSender;
David Chende701692017-10-05 13:16:02 -070033import android.content.pm.PackageInfo;
34import android.content.pm.PackageManager;
35import android.content.pm.UserInfo;
David Chenc8a43242017-10-17 16:23:28 -070036import android.net.NetworkStats;
Chenjie Yu05013b32017-11-21 10:21:41 -080037import android.net.wifi.IWifiManager;
38import android.net.wifi.WifiActivityEnergyInfo;
David Chenc8a43242017-10-17 16:23:28 -070039import android.os.BatteryStatsInternal;
Bookatz94726412017-08-31 09:26:15 -070040import android.os.Binder;
David Chende701692017-10-05 13:16:02 -070041import android.os.Bundle;
Chenjie Yu937d7422018-01-10 16:37:53 -080042import android.os.Environment;
yroe26c97b2018-03-27 10:30:11 -070043import android.os.FileUtils;
Bookatzb487b552017-09-18 11:26:01 -070044import android.os.IBinder;
Bookatz94726412017-08-31 09:26:15 -070045import android.os.IStatsCompanionService;
46import android.os.IStatsManager;
Chenjie Yu05013b32017-11-21 10:21:41 -080047import android.os.Parcelable;
Bookatz94726412017-08-31 09:26:15 -070048import android.os.Process;
Bookatz1b0b1142017-09-08 11:58:42 -070049import android.os.RemoteException;
Bookatz94726412017-08-31 09:26:15 -070050import android.os.ServiceManager;
Chenjie Yu937d7422018-01-10 16:37:53 -080051import android.os.StatFs;
Chenjie Yu9d7720b2018-01-24 10:34:48 -080052import android.os.StatsDimensionsValue;
David Chen1481fe12017-10-16 13:16:34 -070053import android.os.StatsLogEventWrapper;
Chenjie Yu05013b32017-11-21 10:21:41 -080054import android.os.SynchronousResultReceiver;
Chenjie Yu937d7422018-01-10 16:37:53 -080055import android.os.SystemClock;
David Chende701692017-10-05 13:16:02 -070056import android.os.UserHandle;
57import android.os.UserManager;
Chenjie Yu937d7422018-01-10 16:37:53 -080058import android.telephony.ModemActivityInfo;
59import android.telephony.TelephonyManager;
Olivier Gaillard00bfb1b2018-07-10 11:25:09 +010060import android.util.ArrayMap;
Bookatz94726412017-08-31 09:26:15 -070061import android.util.Slog;
David Chenc8a43242017-10-17 16:23:28 -070062import android.util.StatsLog;
63
64import com.android.internal.annotations.GuardedBy;
65import com.android.internal.net.NetworkStatsFactory;
Olivier Gaillard00bfb1b2018-07-10 11:25:09 +010066import com.android.internal.os.BinderCallsStats;
67import com.android.internal.os.BinderCallsStats.ExportedCallStat;
Chenjie Yu937d7422018-01-10 16:37:53 -080068import com.android.internal.os.KernelCpuSpeedReader;
Chenjie Yuec676612018-03-07 09:19:17 -080069import com.android.internal.os.KernelUidCpuTimeReader;
70import com.android.internal.os.KernelUidCpuClusterTimeReader;
71import com.android.internal.os.KernelUidCpuActiveTimeReader;
72import com.android.internal.os.KernelUidCpuFreqTimeReader;
David Chenc8a43242017-10-17 16:23:28 -070073import com.android.internal.os.KernelWakelockReader;
74import com.android.internal.os.KernelWakelockStats;
Chenjie Yu7f8def92017-11-03 09:33:15 -070075import com.android.internal.os.PowerProfile;
yroe26c97b2018-03-27 10:30:11 -070076import com.android.internal.util.DumpUtils;
David Chenc8a43242017-10-17 16:23:28 -070077import com.android.server.LocalServices;
78import com.android.server.SystemService;
Bookatz94726412017-08-31 09:26:15 -070079
yroe26c97b2018-03-27 10:30:11 -070080import java.io.File;
81import java.io.FileDescriptor;
Chenjie Yuec676612018-03-07 09:19:17 -080082import java.io.IOException;
yroe26c97b2018-03-27 10:30:11 -070083import java.io.PrintWriter;
David Chende701692017-10-05 13:16:02 -070084import java.util.ArrayList;
Bookatz058d8692018-03-06 09:53:47 -080085import java.util.Arrays;
yroe26c97b2018-03-27 10:30:11 -070086import java.util.HashMap;
87import java.util.HashSet;
David Chende701692017-10-05 13:16:02 -070088import java.util.List;
Bookatzc68a9d22017-09-27 14:09:55 -070089import java.util.Map;
yroe26c97b2018-03-27 10:30:11 -070090import java.util.concurrent.TimeUnit;
Chenjie Yu05013b32017-11-21 10:21:41 -080091import java.util.concurrent.TimeoutException;
Bookatzc68a9d22017-09-27 14:09:55 -070092
Bookatz94726412017-08-31 09:26:15 -070093/**
94 * Helper service for statsd (the native stats management service in cmds/statsd/).
95 * Used for registering and receiving alarms on behalf of statsd.
David Chen1481fe12017-10-16 13:16:34 -070096 *
Bookatz6bc51d72017-09-28 16:43:40 -070097 * @hide
Bookatz94726412017-08-31 09:26:15 -070098 */
99public class StatsCompanionService extends IStatsCompanionService.Stub {
Chenjie Yu05013b32017-11-21 10:21:41 -0800100 /**
101 * How long to wait on an individual subsystem to return its stats.
102 */
103 private static final long EXTERNAL_STATS_SYNC_TIMEOUT_MILLIS = 2000;
yroe26c97b2018-03-27 10:30:11 -0700104 private static final long MILLIS_IN_A_DAY = TimeUnit.DAYS.toMillis(1);
Chenjie Yu05013b32017-11-21 10:21:41 -0800105
106 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
yroe26c97b2018-03-27 10:30:11 -0700107 public static final String CONFIG_DIR = "/data/misc/stats-service";
Chenjie Yu05013b32017-11-21 10:21:41 -0800108
Bookatz94726412017-08-31 09:26:15 -0700109 static final String TAG = "StatsCompanionService";
Tej Singh484524a2018-02-01 15:10:05 -0800110 static final boolean DEBUG = false;
Bookatzc6977972018-01-16 16:55:05 -0800111
David Chen661f7912018-01-22 17:46:24 -0800112 public static final int CODE_DATA_BROADCAST = 1;
Bookatzc6977972018-01-16 16:55:05 -0800113 public static final int CODE_SUBSCRIBER_BROADCAST = 1;
David Chend37bc232018-04-12 18:05:11 -0700114 /**
115 * The last report time is provided with each intent registered to
116 * StatsManager#setFetchReportsOperation. This allows easy de-duping in the receiver if
117 * statsd is requesting the client to retrieve the same statsd data. The last report time
118 * corresponds to the last_report_elapsed_nanos that will provided in the current
119 * ConfigMetricsReport, and this timestamp also corresponds to the
120 * current_report_elapsed_nanos of the most recently obtained ConfigMetricsReport.
121 */
122 public static final String EXTRA_LAST_REPORT_TIME = "android.app.extra.LAST_REPORT_TIME";
yroe26c97b2018-03-27 10:30:11 -0700123 public static final int DEATH_THRESHOLD = 10;
Bookatzc6977972018-01-16 16:55:05 -0800124
Bookatz94726412017-08-31 09:26:15 -0700125 private final Context mContext;
126 private final AlarmManager mAlarmManager;
Bookatzb487b552017-09-18 11:26:01 -0700127 @GuardedBy("sStatsdLock")
Bookatz1b0b1142017-09-08 11:58:42 -0700128 private static IStatsManager sStatsd;
Bookatzb487b552017-09-18 11:26:01 -0700129 private static final Object sStatsdLock = new Object();
Bookatz94726412017-08-31 09:26:15 -0700130
Bookatz75158ac2018-05-18 15:55:23 -0700131 private final OnAlarmListener mAnomalyAlarmListener = new AnomalyAlarmListener();
132 private final OnAlarmListener mPullingAlarmListener = new PullingAlarmListener();
133 private final OnAlarmListener mPeriodicAlarmListener = new PeriodicAlarmListener();
David Chende701692017-10-05 13:16:02 -0700134 private final BroadcastReceiver mAppUpdateReceiver;
David Chen47e8f4d2017-10-11 15:34:13 -0700135 private final BroadcastReceiver mUserUpdateReceiver;
yro947fbce2017-11-15 22:50:23 -0800136 private final ShutdownEventReceiver mShutdownEventReceiver;
Chenjie Yu7f8def92017-11-03 09:33:15 -0700137 private final KernelWakelockReader mKernelWakelockReader = new KernelWakelockReader();
138 private final KernelWakelockStats mTmpWakelockStats = new KernelWakelockStats();
Chenjie Yu05013b32017-11-21 10:21:41 -0800139 private IWifiManager mWifiManager = null;
140 private TelephonyManager mTelephony = null;
Chenjie Yu937d7422018-01-10 16:37:53 -0800141 private final StatFs mStatFsData = new StatFs(Environment.getDataDirectory().getAbsolutePath());
142 private final StatFs mStatFsSystem =
David Chen661f7912018-01-22 17:46:24 -0800143 new StatFs(Environment.getRootDirectory().getAbsolutePath());
Chenjie Yu937d7422018-01-10 16:37:53 -0800144 private final StatFs mStatFsTemp =
David Chen661f7912018-01-22 17:46:24 -0800145 new StatFs(Environment.getDownloadCacheDirectory().getAbsolutePath());
yroe26c97b2018-03-27 10:30:11 -0700146 @GuardedBy("sStatsdLock")
147 private final HashSet<Long> mDeathTimeMillis = new HashSet<>();
148 @GuardedBy("sStatsdLock")
149 private final HashMap<Long, String> mDeletedFiles = new HashMap<>();
Bookatz94726412017-08-31 09:26:15 -0700150
Chenjie Yuec676612018-03-07 09:19:17 -0800151 private KernelUidCpuTimeReader mKernelUidCpuTimeReader = new KernelUidCpuTimeReader();
152 private KernelCpuSpeedReader[] mKernelCpuSpeedReaders;
153 private KernelUidCpuFreqTimeReader mKernelUidCpuFreqTimeReader =
154 new KernelUidCpuFreqTimeReader();
155 private KernelUidCpuActiveTimeReader mKernelUidCpuActiveTimeReader =
156 new KernelUidCpuActiveTimeReader();
157 private KernelUidCpuClusterTimeReader mKernelUidCpuClusterTimeReader =
158 new KernelUidCpuClusterTimeReader();
159
Bookatz94726412017-08-31 09:26:15 -0700160 public StatsCompanionService(Context context) {
161 super();
162 mContext = context;
163 mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
164
David Chende701692017-10-05 13:16:02 -0700165 mAppUpdateReceiver = new AppUpdateReceiver();
David Chen47e8f4d2017-10-11 15:34:13 -0700166 mUserUpdateReceiver = new BroadcastReceiver() {
167 @Override
168 public void onReceive(Context context, Intent intent) {
169 synchronized (sStatsdLock) {
170 sStatsd = fetchStatsdService();
171 if (sStatsd == null) {
Bookatz111ed732018-02-16 15:54:05 -0800172 Slog.w(TAG, "Could not access statsd for UserUpdateReceiver");
David Chen47e8f4d2017-10-11 15:34:13 -0700173 return;
174 }
175 try {
176 // Pull the latest state of UID->app name, version mapping.
177 // Needed since the new user basically has a version of every app.
178 informAllUidsLocked(context);
179 } catch (RemoteException e) {
David Chen1481fe12017-10-16 13:16:34 -0700180 Slog.e(TAG, "Failed to inform statsd latest update of all apps", e);
yroe26c97b2018-03-27 10:30:11 -0700181 forgetEverythingLocked();
David Chen47e8f4d2017-10-11 15:34:13 -0700182 }
183 }
184 }
185 };
yro947fbce2017-11-15 22:50:23 -0800186 mShutdownEventReceiver = new ShutdownEventReceiver();
Bookatz111ed732018-02-16 15:54:05 -0800187 if (DEBUG) Slog.d(TAG, "Registered receiver for ACTION_PACKAGE_REPLACED and ADDED.");
Chenjie Yu7f8def92017-11-03 09:33:15 -0700188 PowerProfile powerProfile = new PowerProfile(context);
189 final int numClusters = powerProfile.getNumCpuClusters();
190 mKernelCpuSpeedReaders = new KernelCpuSpeedReader[numClusters];
191 int firstCpuOfCluster = 0;
192 for (int i = 0; i < numClusters; i++) {
193 final int numSpeedSteps = powerProfile.getNumSpeedStepsInCpuCluster(i);
194 mKernelCpuSpeedReaders[i] = new KernelCpuSpeedReader(firstCpuOfCluster,
David Chen661f7912018-01-22 17:46:24 -0800195 numSpeedSteps);
Chenjie Yu7f8def92017-11-03 09:33:15 -0700196 firstCpuOfCluster += powerProfile.getNumCoresInCpuCluster(i);
197 }
Chenjie Yuec676612018-03-07 09:19:17 -0800198 // use default throttling in
199 // frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader
200 mKernelUidCpuFreqTimeReader.setThrottleInterval(0);
201 long[] freqs = mKernelUidCpuFreqTimeReader.readFreqs(powerProfile);
Chenjie Yuec676612018-03-07 09:19:17 -0800202 mKernelUidCpuClusterTimeReader.setThrottleInterval(0);
203 mKernelUidCpuActiveTimeReader.setThrottleInterval(0);
Bookatz94726412017-08-31 09:26:15 -0700204 }
205
David Chenadaf8b32017-11-03 15:42:08 -0700206 @Override
David Chend37bc232018-04-12 18:05:11 -0700207 public void sendDataBroadcast(IBinder intentSenderBinder, long lastReportTimeNs) {
Bookatz36920822018-01-26 09:18:07 -0800208 enforceCallingPermission();
David Chen661f7912018-01-22 17:46:24 -0800209 IntentSender intentSender = new IntentSender(intentSenderBinder);
210 Intent intent = new Intent();
David Chend37bc232018-04-12 18:05:11 -0700211 intent.putExtra(EXTRA_LAST_REPORT_TIME, lastReportTimeNs);
David Chen661f7912018-01-22 17:46:24 -0800212 try {
213 intentSender.sendIntent(mContext, CODE_DATA_BROADCAST, intent, null, null);
214 } catch (IntentSender.SendIntentException e) {
215 Slog.w(TAG, "Unable to send using IntentSender");
216 }
David Chenadaf8b32017-11-03 15:42:08 -0700217 }
218
Bookatzc6977972018-01-16 16:55:05 -0800219 @Override
220 public void sendSubscriberBroadcast(IBinder intentSenderBinder, long configUid, long configKey,
221 long subscriptionId, long subscriptionRuleId,
Bookatz058d8692018-03-06 09:53:47 -0800222 String[] cookies,
Bookatzc6977972018-01-16 16:55:05 -0800223 StatsDimensionsValue dimensionsValue) {
Bookatzc6977972018-01-16 16:55:05 -0800224 enforceCallingPermission();
225 IntentSender intentSender = new IntentSender(intentSenderBinder);
226 Intent intent = new Intent()
227 .putExtra(StatsManager.EXTRA_STATS_CONFIG_UID, configUid)
228 .putExtra(StatsManager.EXTRA_STATS_CONFIG_KEY, configKey)
229 .putExtra(StatsManager.EXTRA_STATS_SUBSCRIPTION_ID, subscriptionId)
230 .putExtra(StatsManager.EXTRA_STATS_SUBSCRIPTION_RULE_ID, subscriptionRuleId)
231 .putExtra(StatsManager.EXTRA_STATS_DIMENSIONS_VALUE, dimensionsValue);
Bookatz058d8692018-03-06 09:53:47 -0800232
233 ArrayList<String> cookieList = new ArrayList<>(cookies.length);
234 for (String cookie : cookies) { cookieList.add(cookie); }
235 intent.putStringArrayListExtra(
236 StatsManager.EXTRA_STATS_BROADCAST_SUBSCRIBER_COOKIES, cookieList);
237
Bookatz1476ef22018-02-13 12:26:01 -0800238 if (DEBUG) {
Bookatz058d8692018-03-06 09:53:47 -0800239 Slog.d(TAG, String.format(
240 "Statsd sendSubscriberBroadcast with params {%d %d %d %d %s %s}",
241 configUid, configKey, subscriptionId, subscriptionRuleId,
242 Arrays.toString(cookies), dimensionsValue));
Bookatz1476ef22018-02-13 12:26:01 -0800243 }
Bookatzc6977972018-01-16 16:55:05 -0800244 try {
245 intentSender.sendIntent(mContext, CODE_SUBSCRIBER_BROADCAST, intent, null, null);
246 } catch (IntentSender.SendIntentException e) {
247 Slog.w(TAG, "Unable to send using IntentSender from uid " + configUid
248 + "; presumably it had been cancelled.");
Bookatzc6977972018-01-16 16:55:05 -0800249 }
250 }
251
David Chen1481fe12017-10-16 13:16:34 -0700252 private final static int[] toIntArray(List<Integer> list) {
David Chende701692017-10-05 13:16:02 -0700253 int[] ret = new int[list.size()];
David Chen1481fe12017-10-16 13:16:34 -0700254 for (int i = 0; i < ret.length; i++) {
David Chende701692017-10-05 13:16:02 -0700255 ret[i] = list.get(i);
256 }
257 return ret;
258 }
259
Dianne Hackborn3accca02013-09-20 09:32:11 -0700260 private final static long[] toLongArray(List<Long> list) {
261 long[] ret = new long[list.size()];
262 for (int i = 0; i < ret.length; i++) {
263 ret[i] = list.get(i);
264 }
265 return ret;
266 }
267
David Chende701692017-10-05 13:16:02 -0700268 // Assumes that sStatsdLock is held.
Andreas Gampea36dc622018-02-05 17:19:22 -0800269 @GuardedBy("sStatsdLock")
David Chende701692017-10-05 13:16:02 -0700270 private final void informAllUidsLocked(Context context) throws RemoteException {
271 UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
272 PackageManager pm = context.getPackageManager();
273 final List<UserInfo> users = um.getUsers(true);
274 if (DEBUG) {
Bookatz111ed732018-02-16 15:54:05 -0800275 Slog.d(TAG, "Iterating over " + users.size() + " profiles.");
David Chende701692017-10-05 13:16:02 -0700276 }
277
Rajeev Kumar22d92b72018-02-07 18:38:36 -0800278 List<Integer> uids = new ArrayList<>();
279 List<Long> versions = new ArrayList<>();
280 List<String> apps = new ArrayList<>();
David Chende701692017-10-05 13:16:02 -0700281
282 // Add in all the apps for every user/profile.
283 for (UserInfo profile : users) {
David Chenbd125272018-04-04 19:02:50 -0700284 List<PackageInfo> pi =
David Chend37bc232018-04-12 18:05:11 -0700285 pm.getInstalledPackagesAsUser(PackageManager.MATCH_KNOWN_PACKAGES, profile.id);
David Chen1481fe12017-10-16 13:16:34 -0700286 for (int j = 0; j < pi.size(); j++) {
287 if (pi.get(j).applicationInfo != null) {
288 uids.add(pi.get(j).applicationInfo.uid);
Dianne Hackborn3accca02013-09-20 09:32:11 -0700289 versions.add(pi.get(j).getLongVersionCode());
David Chen1481fe12017-10-16 13:16:34 -0700290 apps.add(pi.get(j).packageName);
291 }
292 }
David Chende701692017-10-05 13:16:02 -0700293 }
Dianne Hackborn3accca02013-09-20 09:32:11 -0700294 sStatsd.informAllUidData(toIntArray(uids), toLongArray(versions), apps.toArray(new
David Chen1481fe12017-10-16 13:16:34 -0700295 String[apps.size()]));
David Chende701692017-10-05 13:16:02 -0700296 if (DEBUG) {
Bookatz111ed732018-02-16 15:54:05 -0800297 Slog.d(TAG, "Sent data for " + uids.size() + " apps");
David Chende701692017-10-05 13:16:02 -0700298 }
299 }
300
Bookatz36920822018-01-26 09:18:07 -0800301 private final static class AppUpdateReceiver extends BroadcastReceiver {
David Chende701692017-10-05 13:16:02 -0700302 @Override
303 public void onReceive(Context context, Intent intent) {
David Chen47e8f4d2017-10-11 15:34:13 -0700304 /**
305 * App updates actually consist of REMOVE, ADD, and then REPLACE broadcasts. To avoid
306 * waste, we ignore the REMOVE and ADD broadcasts that contain the replacing flag.
David Chend6896892017-10-25 11:49:03 -0700307 * If we can't find the value for EXTRA_REPLACING, we default to false.
David Chen47e8f4d2017-10-11 15:34:13 -0700308 */
David Chend6896892017-10-25 11:49:03 -0700309 if (!intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)
310 && intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
David Chen47e8f4d2017-10-11 15:34:13 -0700311 return; // Keep only replacing or normal add and remove.
312 }
Tej Singh90d3aa02018-03-08 19:07:58 -0800313 if (DEBUG) Slog.d(TAG, "StatsCompanionService noticed an app was updated.");
David Chende701692017-10-05 13:16:02 -0700314 synchronized (sStatsdLock) {
315 if (sStatsd == null) {
yrof6d1ca52017-11-15 15:38:34 -0800316 Slog.w(TAG, "Could not access statsd to inform it of an app update");
David Chende701692017-10-05 13:16:02 -0700317 return;
318 }
319 try {
320 if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) {
321 Bundle b = intent.getExtras();
322 int uid = b.getInt(Intent.EXTRA_UID);
323 boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
324 if (!replacing) {
325 // Don't bother sending an update if we're right about to get another
326 // intent for the new version that's added.
327 PackageManager pm = context.getPackageManager();
328 String app = intent.getData().getSchemeSpecificPart();
329 sStatsd.informOnePackageRemoved(app, uid);
330 }
331 } else {
332 PackageManager pm = context.getPackageManager();
333 Bundle b = intent.getExtras();
334 int uid = b.getInt(Intent.EXTRA_UID);
335 String app = intent.getData().getSchemeSpecificPart();
336 PackageInfo pi = pm.getPackageInfo(app, PackageManager.MATCH_ANY_USER);
Dianne Hackborn3accca02013-09-20 09:32:11 -0700337 sStatsd.informOnePackage(app, uid, pi.getLongVersionCode());
David Chende701692017-10-05 13:16:02 -0700338 }
339 } catch (Exception e) {
340 Slog.w(TAG, "Failed to inform statsd of an app update", e);
341 }
342 }
343 }
David Chen1481fe12017-10-16 13:16:34 -0700344 }
David Chende701692017-10-05 13:16:02 -0700345
Bookatz75158ac2018-05-18 15:55:23 -0700346 public final static class AnomalyAlarmListener implements OnAlarmListener {
Bookatz94726412017-08-31 09:26:15 -0700347 @Override
Bookatz75158ac2018-05-18 15:55:23 -0700348 public void onAlarm() {
Bookatz66fe0612018-02-07 18:51:48 -0800349 Slog.i(TAG, "StatsCompanionService believes an anomaly has occurred at time "
350 + System.currentTimeMillis() + "ms.");
Bookatzb487b552017-09-18 11:26:01 -0700351 synchronized (sStatsdLock) {
352 if (sStatsd == null) {
353 Slog.w(TAG, "Could not access statsd to inform it of anomaly alarm firing");
354 return;
355 }
356 try {
357 // Two-way call to statsd to retain AlarmManager wakelock
358 sStatsd.informAnomalyAlarmFired();
359 } catch (RemoteException e) {
360 Slog.w(TAG, "Failed to inform statsd of anomaly alarm firing", e);
361 }
Bookatz94726412017-08-31 09:26:15 -0700362 }
Bookatzb487b552017-09-18 11:26:01 -0700363 // AlarmManager releases its own wakelock here.
Bookatz94726412017-08-31 09:26:15 -0700364 }
David Chen1481fe12017-10-16 13:16:34 -0700365 }
Bookatzb487b552017-09-18 11:26:01 -0700366
Bookatz75158ac2018-05-18 15:55:23 -0700367 public final static class PullingAlarmListener implements OnAlarmListener {
David Chen661f7912018-01-22 17:46:24 -0800368 @Override
Bookatz75158ac2018-05-18 15:55:23 -0700369 public void onAlarm() {
David Chen661f7912018-01-22 17:46:24 -0800370 if (DEBUG)
371 Slog.d(TAG, "Time to poll something.");
372 synchronized (sStatsdLock) {
373 if (sStatsd == null) {
374 Slog.w(TAG, "Could not access statsd to inform it of pulling alarm firing.");
375 return;
376 }
377 try {
378 // Two-way call to statsd to retain AlarmManager wakelock
379 sStatsd.informPollAlarmFired();
380 } catch (RemoteException e) {
381 Slog.w(TAG, "Failed to inform statsd of pulling alarm firing.", e);
382 }
383 }
Bookatzb487b552017-09-18 11:26:01 -0700384 }
David Chen1481fe12017-10-16 13:16:34 -0700385 }
Bookatz94726412017-08-31 09:26:15 -0700386
Bookatz75158ac2018-05-18 15:55:23 -0700387 public final static class PeriodicAlarmListener implements OnAlarmListener {
Yangster-mac932ecec2018-02-01 10:23:52 -0800388 @Override
Bookatz75158ac2018-05-18 15:55:23 -0700389 public void onAlarm() {
Yangster-mac932ecec2018-02-01 10:23:52 -0800390 if (DEBUG)
Yangster-macc04feba2018-04-02 14:37:33 -0700391 Slog.d(TAG, "Time to trigger periodic alarm.");
Yangster-mac932ecec2018-02-01 10:23:52 -0800392 synchronized (sStatsdLock) {
393 if (sStatsd == null) {
394 Slog.w(TAG, "Could not access statsd to inform it of periodic alarm firing.");
395 return;
396 }
397 try {
398 // Two-way call to statsd to retain AlarmManager wakelock
399 sStatsd.informAlarmForSubscriberTriggeringFired();
400 } catch (RemoteException e) {
401 Slog.w(TAG, "Failed to inform statsd of periodic alarm firing.", e);
402 }
403 }
404 // AlarmManager releases its own wakelock here.
405 }
406 }
407
408 public final static class ShutdownEventReceiver extends BroadcastReceiver {
yro947fbce2017-11-15 22:50:23 -0800409 @Override
410 public void onReceive(Context context, Intent intent) {
411 /**
412 * Skip immediately if intent is not relevant to device shutdown.
413 */
414 if (!intent.getAction().equals(Intent.ACTION_REBOOT)
David Chen661f7912018-01-22 17:46:24 -0800415 && !(intent.getAction().equals(Intent.ACTION_SHUTDOWN)
416 && (intent.getFlags() & Intent.FLAG_RECEIVER_FOREGROUND) != 0)) {
417 return;
yro947fbce2017-11-15 22:50:23 -0800418 }
yrof7a3bcb2018-01-24 17:18:55 -0800419
yro947fbce2017-11-15 22:50:23 -0800420 Slog.i(TAG, "StatsCompanionService noticed a shutdown.");
421 synchronized (sStatsdLock) {
422 if (sStatsd == null) {
423 Slog.w(TAG, "Could not access statsd to inform it of a shutdown event.");
424 return;
425 }
426 try {
Yangster-mac892f3d32018-05-02 14:16:48 -0700427 sStatsd.informDeviceShutdown();
yro947fbce2017-11-15 22:50:23 -0800428 } catch (Exception e) {
429 Slog.w(TAG, "Failed to inform statsd of a shutdown event.", e);
430 }
431 }
432 }
433 }
434
Bookatz94726412017-08-31 09:26:15 -0700435 @Override // Binder call
436 public void setAnomalyAlarm(long timestampMs) {
437 enforceCallingPermission();
438 if (DEBUG) Slog.d(TAG, "Setting anomaly alarm for " + timestampMs);
439 final long callingToken = Binder.clearCallingIdentity();
440 try {
Yangster-mac330af582018-02-08 15:24:38 -0800441 // using ELAPSED_REALTIME, not ELAPSED_REALTIME_WAKEUP, so if device is asleep, will
442 // only fire when it awakens.
Bookatz75158ac2018-05-18 15:55:23 -0700443 // AlarmManager will automatically cancel any previous mAnomalyAlarmListener alarm.
444 mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME, timestampMs, TAG + ".anomaly",
445 mAnomalyAlarmListener, null);
Bookatz94726412017-08-31 09:26:15 -0700446 } finally {
447 Binder.restoreCallingIdentity(callingToken);
448 }
449 }
450
451 @Override // Binder call
452 public void cancelAnomalyAlarm() {
453 enforceCallingPermission();
454 if (DEBUG) Slog.d(TAG, "Cancelling anomaly alarm");
455 final long callingToken = Binder.clearCallingIdentity();
456 try {
Bookatz75158ac2018-05-18 15:55:23 -0700457 mAlarmManager.cancel(mAnomalyAlarmListener);
Bookatz94726412017-08-31 09:26:15 -0700458 } finally {
459 Binder.restoreCallingIdentity(callingToken);
460 }
461 }
462
463 @Override // Binder call
Yangster-mac932ecec2018-02-01 10:23:52 -0800464 public void setAlarmForSubscriberTriggering(long timestampMs) {
465 enforceCallingPermission();
466 if (DEBUG)
Yangster-macc04feba2018-04-02 14:37:33 -0700467 Slog.d(TAG, "Setting periodic alarm in about " +
468 (timestampMs - SystemClock.elapsedRealtime()));
Yangster-mac932ecec2018-02-01 10:23:52 -0800469 final long callingToken = Binder.clearCallingIdentity();
470 try {
471 // using ELAPSED_REALTIME, not ELAPSED_REALTIME_WAKEUP, so if device is asleep, will
472 // only fire when it awakens.
Bookatz75158ac2018-05-18 15:55:23 -0700473 mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME, timestampMs, TAG + ".periodic",
474 mPeriodicAlarmListener, null);
Yangster-mac932ecec2018-02-01 10:23:52 -0800475 } finally {
476 Binder.restoreCallingIdentity(callingToken);
477 }
478 }
479
480 @Override // Binder call
481 public void cancelAlarmForSubscriberTriggering() {
482 enforceCallingPermission();
483 if (DEBUG)
484 Slog.d(TAG, "Cancelling periodic alarm");
485 final long callingToken = Binder.clearCallingIdentity();
486 try {
Bookatz75158ac2018-05-18 15:55:23 -0700487 mAlarmManager.cancel(mPeriodicAlarmListener);
Yangster-mac932ecec2018-02-01 10:23:52 -0800488 } finally {
489 Binder.restoreCallingIdentity(callingToken);
490 }
491 }
492
493 @Override // Binder call
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700494 public void setPullingAlarm(long nextPullTimeMs) {
Bookatz75158ac2018-05-18 15:55:23 -0700495 enforceCallingPermission();
496 if (DEBUG) {
497 Slog.d(TAG, "Setting pulling alarm in about "
498 + (nextPullTimeMs - SystemClock.elapsedRealtime()));
499 }
500 final long callingToken = Binder.clearCallingIdentity();
501 try {
502 // using ELAPSED_REALTIME, not ELAPSED_REALTIME_WAKEUP, so if device is asleep, will
503 // only fire when it awakens.
504 mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME, nextPullTimeMs, TAG + ".pull",
505 mPullingAlarmListener, null);
506 } finally {
507 Binder.restoreCallingIdentity(callingToken);
508 }
Bookatz94726412017-08-31 09:26:15 -0700509 }
510
511 @Override // Binder call
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700512 public void cancelPullingAlarm() {
Bookatz75158ac2018-05-18 15:55:23 -0700513 enforceCallingPermission();
514 if (DEBUG)
515 Slog.d(TAG, "Cancelling pulling alarm");
516 final long callingToken = Binder.clearCallingIdentity();
517 try {
518 mAlarmManager.cancel(mPullingAlarmListener);
519 } finally {
520 Binder.restoreCallingIdentity(callingToken);
521 }
Bookatz94726412017-08-31 09:26:15 -0700522 }
523
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800524 private void addNetworkStats(
David Chen661f7912018-01-22 17:46:24 -0800525 int tag, List<StatsLogEventWrapper> ret, NetworkStats stats, boolean withFGBG) {
526 int size = stats.size();
Yangster-macc552b352018-02-22 10:00:45 -0800527 long elapsedNanos = SystemClock.elapsedRealtimeNanos();
David Chen661f7912018-01-22 17:46:24 -0800528 NetworkStats.Entry entry = new NetworkStats.Entry(); // For recycling
529 for (int j = 0; j < size; j++) {
530 stats.getValues(j, entry);
Yangster-macc552b352018-02-22 10:00:45 -0800531 StatsLogEventWrapper e = new StatsLogEventWrapper(elapsedNanos, tag, withFGBG ? 6 : 5);
David Chen661f7912018-01-22 17:46:24 -0800532 e.writeInt(entry.uid);
533 if (withFGBG) {
534 e.writeInt(entry.set);
535 }
536 e.writeLong(entry.rxBytes);
537 e.writeLong(entry.rxPackets);
538 e.writeLong(entry.txBytes);
539 e.writeLong(entry.txPackets);
540 ret.add(e);
David Chenc8a43242017-10-17 16:23:28 -0700541 }
David Chenc8a43242017-10-17 16:23:28 -0700542 }
543
544 /**
545 * Allows rollups per UID but keeping the set (foreground/background) slicing.
546 * Adapted from groupedByUid in frameworks/base/core/java/android/net/NetworkStats.java
547 */
548 private NetworkStats rollupNetworkStatsByFGBG(NetworkStats stats) {
549 final NetworkStats ret = new NetworkStats(stats.getElapsedRealtime(), 1);
550
551 final NetworkStats.Entry entry = new NetworkStats.Entry();
552 entry.iface = NetworkStats.IFACE_ALL;
553 entry.tag = NetworkStats.TAG_NONE;
554 entry.metered = NetworkStats.METERED_ALL;
555 entry.roaming = NetworkStats.ROAMING_ALL;
556
557 int size = stats.size();
558 NetworkStats.Entry recycle = new NetworkStats.Entry(); // Used for retrieving values
559 for (int i = 0; i < size; i++) {
560 stats.getValues(i, recycle);
561
562 // Skip specific tags, since already counted in TAG_NONE
563 if (recycle.tag != NetworkStats.TAG_NONE) continue;
564
565 entry.set = recycle.set; // Allows slicing by background/foreground
566 entry.uid = recycle.uid;
567 entry.rxBytes = recycle.rxBytes;
568 entry.rxPackets = recycle.rxPackets;
569 entry.txBytes = recycle.txBytes;
570 entry.txPackets = recycle.txPackets;
571 // Operations purposefully omitted since we don't use them for statsd.
572 ret.combineValues(entry);
573 }
574 return ret;
575 }
576
Chenjie Yu05013b32017-11-21 10:21:41 -0800577 /**
578 * Helper method to extract the Parcelable controller info from a
579 * SynchronousResultReceiver.
580 */
581 private static <T extends Parcelable> T awaitControllerInfo(
582 @Nullable SynchronousResultReceiver receiver) {
583 if (receiver == null) {
584 return null;
585 }
586
587 try {
588 final SynchronousResultReceiver.Result result =
589 receiver.awaitResult(EXTERNAL_STATS_SYNC_TIMEOUT_MILLIS);
590 if (result.bundle != null) {
591 // This is the final destination for the Bundle.
592 result.bundle.setDefusable(true);
593
594 final T data = result.bundle.getParcelable(
595 RESULT_RECEIVER_CONTROLLER_KEY);
596 if (data != null) {
597 return data;
598 }
599 }
600 Slog.e(TAG, "no controller energy info supplied for " + receiver.getName());
601 } catch (TimeoutException e) {
602 Slog.w(TAG, "timeout reading " + receiver.getName() + " stats");
603 }
604 return null;
605 }
606
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800607 private void pullKernelWakelock(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800608 final KernelWakelockStats wakelockStats =
609 mKernelWakelockReader.readKernelWakelockStats(mTmpWakelockStats);
Yangster-macc552b352018-02-22 10:00:45 -0800610 long elapsedNanos = SystemClock.elapsedRealtimeNanos();
David Chen661f7912018-01-22 17:46:24 -0800611 for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) {
612 String name = ent.getKey();
613 KernelWakelockStats.Entry kws = ent.getValue();
Yangster-macc552b352018-02-22 10:00:45 -0800614 StatsLogEventWrapper e = new StatsLogEventWrapper(elapsedNanos, tagId, 4);
David Chen661f7912018-01-22 17:46:24 -0800615 e.writeString(name);
616 e.writeInt(kws.mCount);
617 e.writeInt(kws.mVersion);
618 e.writeLong(kws.mTotalTime);
619 pulledData.add(e);
620 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800621 }
622
623 private void pullWifiBytesTransfer(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800624 long token = Binder.clearCallingIdentity();
625 try {
626 // TODO: Consider caching the following call to get BatteryStatsInternal.
627 BatteryStatsInternal bs = LocalServices.getService(BatteryStatsInternal.class);
628 String[] ifaces = bs.getWifiIfaces();
629 if (ifaces.length == 0) {
630 return;
631 }
632 NetworkStatsFactory nsf = new NetworkStatsFactory();
633 // Combine all the metrics per Uid into one record.
634 NetworkStats stats =
635 nsf.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces, NetworkStats.TAG_NONE, null)
636 .groupedByUid();
637 addNetworkStats(tagId, pulledData, stats, false);
638 } catch (java.io.IOException e) {
639 Slog.e(TAG, "Pulling netstats for wifi bytes has error", e);
640 } finally {
641 Binder.restoreCallingIdentity(token);
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800642 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800643 }
644
645 private void pullWifiBytesTransferByFgBg(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800646 long token = Binder.clearCallingIdentity();
647 try {
648 BatteryStatsInternal bs = LocalServices.getService(BatteryStatsInternal.class);
649 String[] ifaces = bs.getWifiIfaces();
650 if (ifaces.length == 0) {
651 return;
652 }
653 NetworkStatsFactory nsf = new NetworkStatsFactory();
654 NetworkStats stats = rollupNetworkStatsByFGBG(
655 nsf.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces, NetworkStats.TAG_NONE, null));
656 addNetworkStats(tagId, pulledData, stats, true);
657 } catch (java.io.IOException e) {
658 Slog.e(TAG, "Pulling netstats for wifi bytes w/ fg/bg has error", e);
659 } finally {
660 Binder.restoreCallingIdentity(token);
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800661 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800662 }
663
664 private void pullMobileBytesTransfer(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800665 long token = Binder.clearCallingIdentity();
666 try {
667 BatteryStatsInternal bs = LocalServices.getService(BatteryStatsInternal.class);
668 String[] ifaces = bs.getMobileIfaces();
669 if (ifaces.length == 0) {
670 return;
671 }
672 NetworkStatsFactory nsf = new NetworkStatsFactory();
673 // Combine all the metrics per Uid into one record.
674 NetworkStats stats =
675 nsf.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces, NetworkStats.TAG_NONE, null)
676 .groupedByUid();
677 addNetworkStats(tagId, pulledData, stats, false);
678 } catch (java.io.IOException e) {
679 Slog.e(TAG, "Pulling netstats for mobile bytes has error", e);
680 } finally {
681 Binder.restoreCallingIdentity(token);
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800682 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800683 }
684
685 private void pullBluetoothBytesTransfer(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800686 BluetoothActivityEnergyInfo info = pullBluetoothData();
Yangster-macc552b352018-02-22 10:00:45 -0800687 long elapsedNanos = SystemClock.elapsedRealtimeNanos();
Chenjie Yu9e59f932018-03-30 16:41:38 -0700688 if (info.getUidTraffic() != null) {
689 for (UidTraffic traffic : info.getUidTraffic()) {
690 StatsLogEventWrapper e = new StatsLogEventWrapper(elapsedNanos, tagId, 3);
691 e.writeInt(traffic.getUid());
692 e.writeLong(traffic.getRxBytes());
693 e.writeLong(traffic.getTxBytes());
694 pulledData.add(e);
695 }
David Chen661f7912018-01-22 17:46:24 -0800696 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800697 }
698
699 private void pullMobileBytesTransferByFgBg(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800700 long token = Binder.clearCallingIdentity();
701 try {
702 BatteryStatsInternal bs = LocalServices.getService(BatteryStatsInternal.class);
703 String[] ifaces = bs.getMobileIfaces();
704 if (ifaces.length == 0) {
705 return;
706 }
707 NetworkStatsFactory nsf = new NetworkStatsFactory();
708 NetworkStats stats = rollupNetworkStatsByFGBG(
709 nsf.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces, NetworkStats.TAG_NONE, null));
710 addNetworkStats(tagId, pulledData, stats, true);
711 } catch (java.io.IOException e) {
712 Slog.e(TAG, "Pulling netstats for mobile bytes w/ fg/bg has error", e);
713 } finally {
714 Binder.restoreCallingIdentity(token);
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800715 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800716 }
717
718 private void pullCpuTimePerFreq(int tagId, List<StatsLogEventWrapper> pulledData) {
Yangster-macc552b352018-02-22 10:00:45 -0800719 long elapsedNanos = SystemClock.elapsedRealtimeNanos();
David Chen661f7912018-01-22 17:46:24 -0800720 for (int cluster = 0; cluster < mKernelCpuSpeedReaders.length; cluster++) {
721 long[] clusterTimeMs = mKernelCpuSpeedReaders[cluster].readAbsolute();
722 if (clusterTimeMs != null) {
723 for (int speed = clusterTimeMs.length - 1; speed >= 0; --speed) {
Yangster-macc552b352018-02-22 10:00:45 -0800724 StatsLogEventWrapper e = new StatsLogEventWrapper(elapsedNanos, tagId, 3);
David Chen661f7912018-01-22 17:46:24 -0800725 e.writeInt(cluster);
726 e.writeInt(speed);
727 e.writeLong(clusterTimeMs[speed]);
728 pulledData.add(e);
729 }
730 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800731 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800732 }
733
Chenjie Yuec676612018-03-07 09:19:17 -0800734 private void pullKernelUidCpuTime(int tagId, List<StatsLogEventWrapper> pulledData) {
735 long elapsedNanos = SystemClock.elapsedRealtimeNanos();
736 mKernelUidCpuTimeReader.readAbsolute((uid, userTimeUs, systemTimeUs) -> {
737 StatsLogEventWrapper e = new StatsLogEventWrapper(elapsedNanos, tagId, 3);
738 e.writeInt(uid);
739 e.writeLong(userTimeUs);
740 e.writeLong(systemTimeUs);
741 pulledData.add(e);
742 });
743 }
744
745 private void pullKernelUidCpuFreqTime(int tagId, List<StatsLogEventWrapper> pulledData) {
746 long elapsedNanos = SystemClock.elapsedRealtimeNanos();
747 mKernelUidCpuFreqTimeReader.readAbsolute((uid, cpuFreqTimeMs) -> {
748 for (int freqIndex = 0; freqIndex < cpuFreqTimeMs.length; ++freqIndex) {
Chenjie Yu4460e3c2018-03-14 17:12:59 -0700749 if(cpuFreqTimeMs[freqIndex] != 0) {
750 StatsLogEventWrapper e = new StatsLogEventWrapper(elapsedNanos, tagId, 3);
751 e.writeInt(uid);
752 e.writeInt(freqIndex);
753 e.writeLong(cpuFreqTimeMs[freqIndex]);
754 pulledData.add(e);
755 }
Chenjie Yuec676612018-03-07 09:19:17 -0800756 }
757 });
758 }
759
760 private void pullKernelUidCpuClusterTime(int tagId, List<StatsLogEventWrapper> pulledData) {
761 long elapsedNanos = SystemClock.elapsedRealtimeNanos();
762 mKernelUidCpuClusterTimeReader.readAbsolute((uid, cpuClusterTimesMs) -> {
763 for (int i = 0; i < cpuClusterTimesMs.length; i++) {
764 StatsLogEventWrapper e = new StatsLogEventWrapper(elapsedNanos, tagId, 3);
765 e.writeInt(uid);
766 e.writeInt(i);
767 e.writeLong(cpuClusterTimesMs[i]);
768 pulledData.add(e);
769 }
770 });
771 }
772
773 private void pullKernelUidCpuActiveTime(int tagId, List<StatsLogEventWrapper> pulledData) {
774 long elapsedNanos = SystemClock.elapsedRealtimeNanos();
775 mKernelUidCpuActiveTimeReader.readAbsolute((uid, cpuActiveTimesMs) -> {
776 StatsLogEventWrapper e = new StatsLogEventWrapper(elapsedNanos, tagId, 2);
777 e.writeInt(uid);
778 e.writeLong((long)cpuActiveTimesMs);
779 pulledData.add(e);
780 });
781 }
782
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800783 private void pullWifiActivityInfo(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800784 long token = Binder.clearCallingIdentity();
785 if (mWifiManager == null) {
786 mWifiManager =
787 IWifiManager.Stub.asInterface(ServiceManager.getService(Context.WIFI_SERVICE));
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800788 }
David Chen661f7912018-01-22 17:46:24 -0800789 if (mWifiManager != null) {
790 try {
791 SynchronousResultReceiver wifiReceiver = new SynchronousResultReceiver("wifi");
792 mWifiManager.requestActivityInfo(wifiReceiver);
793 final WifiActivityEnergyInfo wifiInfo = awaitControllerInfo(wifiReceiver);
Yangster-macc552b352018-02-22 10:00:45 -0800794 StatsLogEventWrapper e = new StatsLogEventWrapper(SystemClock.elapsedRealtimeNanos(), tagId, 6);
David Chen661f7912018-01-22 17:46:24 -0800795 e.writeLong(wifiInfo.getTimeStamp());
796 e.writeInt(wifiInfo.getStackState());
797 e.writeLong(wifiInfo.getControllerTxTimeMillis());
798 e.writeLong(wifiInfo.getControllerRxTimeMillis());
799 e.writeLong(wifiInfo.getControllerIdleTimeMillis());
800 e.writeLong(wifiInfo.getControllerEnergyUsed());
801 pulledData.add(e);
802 } catch (RemoteException e) {
803 Slog.e(TAG, "Pulling wifiManager for wifi controller activity energy info has error", e);
804 } finally {
805 Binder.restoreCallingIdentity(token);
806 }
807 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800808 }
809
810 private void pullModemActivityInfo(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800811 long token = Binder.clearCallingIdentity();
812 if (mTelephony == null) {
813 mTelephony = TelephonyManager.from(mContext);
814 }
815 if (mTelephony != null) {
816 SynchronousResultReceiver modemReceiver = new SynchronousResultReceiver("telephony");
817 mTelephony.requestModemActivityInfo(modemReceiver);
818 final ModemActivityInfo modemInfo = awaitControllerInfo(modemReceiver);
David Chen56ae0d92018-05-11 16:00:22 -0700819 StatsLogEventWrapper e = new StatsLogEventWrapper(SystemClock.elapsedRealtimeNanos(), tagId, 10);
David Chen661f7912018-01-22 17:46:24 -0800820 e.writeLong(modemInfo.getTimestamp());
821 e.writeLong(modemInfo.getSleepTimeMillis());
822 e.writeLong(modemInfo.getIdleTimeMillis());
823 e.writeLong(modemInfo.getTxTimeMillis()[0]);
824 e.writeLong(modemInfo.getTxTimeMillis()[1]);
825 e.writeLong(modemInfo.getTxTimeMillis()[2]);
826 e.writeLong(modemInfo.getTxTimeMillis()[3]);
827 e.writeLong(modemInfo.getTxTimeMillis()[4]);
828 e.writeLong(modemInfo.getRxTimeMillis());
829 e.writeLong(modemInfo.getEnergyUsed());
830 pulledData.add(e);
831 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800832 }
833
834 private void pullBluetoothActivityInfo(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800835 BluetoothActivityEnergyInfo info = pullBluetoothData();
Yangster-macc552b352018-02-22 10:00:45 -0800836 StatsLogEventWrapper e = new StatsLogEventWrapper(SystemClock.elapsedRealtimeNanos(), tagId, 6);
David Chen661f7912018-01-22 17:46:24 -0800837 e.writeLong(info.getTimeStamp());
838 e.writeInt(info.getBluetoothStackState());
839 e.writeLong(info.getControllerTxTimeMillis());
840 e.writeLong(info.getControllerRxTimeMillis());
841 e.writeLong(info.getControllerIdleTimeMillis());
842 e.writeLong(info.getControllerEnergyUsed());
843 pulledData.add(e);
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800844 }
845
846 private synchronized BluetoothActivityEnergyInfo pullBluetoothData() {
Chenjie Yuc2d65442018-02-01 22:27:17 -0800847 final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
David Chen661f7912018-01-22 17:46:24 -0800848 if (adapter != null) {
849 SynchronousResultReceiver bluetoothReceiver = new SynchronousResultReceiver("bluetooth");
850 adapter.requestControllerActivityEnergyInfo(bluetoothReceiver);
851 return awaitControllerInfo(bluetoothReceiver);
852 } else {
853 Slog.e(TAG, "Failed to get bluetooth adapter!");
854 return null;
855 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800856 }
857
858 private void pullSystemElapsedRealtime(int tagId, List<StatsLogEventWrapper> pulledData) {
Yangster-macc552b352018-02-22 10:00:45 -0800859 StatsLogEventWrapper e = new StatsLogEventWrapper(SystemClock.elapsedRealtimeNanos(), tagId, 1);
David Chen661f7912018-01-22 17:46:24 -0800860 e.writeLong(SystemClock.elapsedRealtime());
861 pulledData.add(e);
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800862 }
863
864 private void pullDiskSpace(int tagId, List<StatsLogEventWrapper> pulledData) {
Yangster-macc552b352018-02-22 10:00:45 -0800865 StatsLogEventWrapper e = new StatsLogEventWrapper(SystemClock.elapsedRealtimeNanos(), tagId, 3);
David Chen661f7912018-01-22 17:46:24 -0800866 e.writeLong(mStatFsData.getAvailableBytes());
867 e.writeLong(mStatFsSystem.getAvailableBytes());
868 e.writeLong(mStatFsTemp.getAvailableBytes());
869 pulledData.add(e);
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800870 }
871
872 private void pullSystemUpTime(int tagId, List<StatsLogEventWrapper> pulledData) {
Yangster-macc552b352018-02-22 10:00:45 -0800873 StatsLogEventWrapper e = new StatsLogEventWrapper(SystemClock.elapsedRealtimeNanos(), tagId, 1);
David Chen661f7912018-01-22 17:46:24 -0800874 e.writeLong(SystemClock.uptimeMillis());
875 pulledData.add(e);
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800876 }
877
Rajeev Kumar22d92b72018-02-07 18:38:36 -0800878 private void pullProcessMemoryState(int tagId, List<StatsLogEventWrapper> pulledData) {
879 List<ProcessMemoryState> processMemoryStates =
880 LocalServices.getService(ActivityManagerInternal.class)
881 .getMemoryStateForProcesses();
Yangster-macc552b352018-02-22 10:00:45 -0800882 long elapsedNanos = SystemClock.elapsedRealtimeNanos();
Rajeev Kumar22d92b72018-02-07 18:38:36 -0800883 for (ProcessMemoryState processMemoryState : processMemoryStates) {
Yangster-macc552b352018-02-22 10:00:45 -0800884 StatsLogEventWrapper e = new StatsLogEventWrapper(elapsedNanos, tagId, 8 /* fields */);
Rajeev Kumar22d92b72018-02-07 18:38:36 -0800885 e.writeInt(processMemoryState.uid);
886 e.writeString(processMemoryState.processName);
887 e.writeInt(processMemoryState.oomScore);
888 e.writeLong(processMemoryState.pgfault);
889 e.writeLong(processMemoryState.pgmajfault);
890 e.writeLong(processMemoryState.rssInBytes);
891 e.writeLong(processMemoryState.cacheInBytes);
892 e.writeLong(processMemoryState.swapInBytes);
893 pulledData.add(e);
894 }
895 }
896
Olivier Gaillard00bfb1b2018-07-10 11:25:09 +0100897 private void pullBinderCallsStats(int tagId, List<StatsLogEventWrapper> pulledData) {
898 List<ExportedCallStat> callStats = BinderCallsStats.getInstance().getExportedCallStats();
899 long elapsedNanos = SystemClock.elapsedRealtimeNanos();
900 for (ExportedCallStat callStat : callStats) {
901 StatsLogEventWrapper e = new StatsLogEventWrapper(elapsedNanos, tagId, 11 /* fields */);
902 e.writeInt(callStat.uid);
903 e.writeString(callStat.className);
904 e.writeString(callStat.methodName);
905 e.writeLong(callStat.callCount);
906 e.writeLong(callStat.exceptionCount);
907 e.writeLong(callStat.latencyMicros);
908 e.writeLong(callStat.maxLatencyMicros);
909 e.writeLong(callStat.cpuTimeMicros);
910 e.writeLong(callStat.maxCpuTimeMicros);
911 e.writeLong(callStat.maxReplySizeBytes);
912 e.writeLong(callStat.maxRequestSizeBytes);
913 pulledData.add(e);
914 }
915 }
916
Chenjie Yu05013b32017-11-21 10:21:41 -0800917 /**
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800918 * Pulls various data.
Chenjie Yu05013b32017-11-21 10:21:41 -0800919 */
Bookatzc68a9d22017-09-27 14:09:55 -0700920 @Override // Binder call
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700921 public StatsLogEventWrapper[] pullData(int tagId) {
Bookatzc68a9d22017-09-27 14:09:55 -0700922 enforceCallingPermission();
David Chenc8a43242017-10-17 16:23:28 -0700923 if (DEBUG)
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700924 Slog.d(TAG, "Pulling " + tagId);
Rajeev Kumar22d92b72018-02-07 18:38:36 -0800925 List<StatsLogEventWrapper> ret = new ArrayList<>();
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700926 switch (tagId) {
Chenjie Yu31d14d72017-12-12 17:54:33 -0800927 case StatsLog.WIFI_BYTES_TRANSFER: {
David Chen661f7912018-01-22 17:46:24 -0800928 pullWifiBytesTransfer(tagId, ret);
929 break;
David Chenc8a43242017-10-17 16:23:28 -0700930 }
Chenjie Yu31d14d72017-12-12 17:54:33 -0800931 case StatsLog.MOBILE_BYTES_TRANSFER: {
David Chen661f7912018-01-22 17:46:24 -0800932 pullMobileBytesTransfer(tagId, ret);
933 break;
David Chenc8a43242017-10-17 16:23:28 -0700934 }
Chenjie Yu31d14d72017-12-12 17:54:33 -0800935 case StatsLog.WIFI_BYTES_TRANSFER_BY_FG_BG: {
David Chen661f7912018-01-22 17:46:24 -0800936 pullWifiBytesTransferByFgBg(tagId, ret);
937 break;
David Chenc8a43242017-10-17 16:23:28 -0700938 }
Chenjie Yu31d14d72017-12-12 17:54:33 -0800939 case StatsLog.MOBILE_BYTES_TRANSFER_BY_FG_BG: {
David Chen661f7912018-01-22 17:46:24 -0800940 pullMobileBytesTransferByFgBg(tagId, ret);
941 break;
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800942 }
943 case StatsLog.BLUETOOTH_BYTES_TRANSFER: {
David Chen661f7912018-01-22 17:46:24 -0800944 pullBluetoothBytesTransfer(tagId, ret);
945 break;
David Chenc8a43242017-10-17 16:23:28 -0700946 }
Chenjie Yu31d14d72017-12-12 17:54:33 -0800947 case StatsLog.KERNEL_WAKELOCK: {
David Chen661f7912018-01-22 17:46:24 -0800948 pullKernelWakelock(tagId, ret);
949 break;
David Chen1481fe12017-10-16 13:16:34 -0700950 }
Chenjie Yu31d14d72017-12-12 17:54:33 -0800951 case StatsLog.CPU_TIME_PER_FREQ: {
David Chen661f7912018-01-22 17:46:24 -0800952 pullCpuTimePerFreq(tagId, ret);
953 break;
Chenjie Yu7f8def92017-11-03 09:33:15 -0700954 }
Chenjie Yuec676612018-03-07 09:19:17 -0800955 case StatsLog.CPU_TIME_PER_UID: {
956 pullKernelUidCpuTime(tagId, ret);
957 break;
958 }
959 case StatsLog.CPU_TIME_PER_UID_FREQ: {
960 pullKernelUidCpuFreqTime(tagId, ret);
961 break;
962 }
963 case StatsLog.CPU_CLUSTER_TIME: {
964 pullKernelUidCpuClusterTime(tagId, ret);
965 break;
966 }
967 case StatsLog.CPU_ACTIVE_TIME: {
968 pullKernelUidCpuActiveTime(tagId, ret);
969 break;
970 }
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800971 case StatsLog.WIFI_ACTIVITY_INFO: {
972 pullWifiActivityInfo(tagId, ret);
David Chen661f7912018-01-22 17:46:24 -0800973 break;
Chenjie Yu05013b32017-11-21 10:21:41 -0800974 }
Chenjie Yu31d14d72017-12-12 17:54:33 -0800975 case StatsLog.MODEM_ACTIVITY_INFO: {
David Chen661f7912018-01-22 17:46:24 -0800976 pullModemActivityInfo(tagId, ret);
977 break;
Chenjie Yu05013b32017-11-21 10:21:41 -0800978 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800979 case StatsLog.BLUETOOTH_ACTIVITY_INFO: {
David Chen661f7912018-01-22 17:46:24 -0800980 pullBluetoothActivityInfo(tagId, ret);
981 break;
Chenjie Yu937d7422018-01-10 16:37:53 -0800982 }
983 case StatsLog.SYSTEM_UPTIME: {
David Chen661f7912018-01-22 17:46:24 -0800984 pullSystemUpTime(tagId, ret);
985 break;
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800986 }
987 case StatsLog.SYSTEM_ELAPSED_REALTIME: {
David Chen661f7912018-01-22 17:46:24 -0800988 pullSystemElapsedRealtime(tagId, ret);
989 break;
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800990 }
991 case StatsLog.DISK_SPACE: {
David Chen661f7912018-01-22 17:46:24 -0800992 pullDiskSpace(tagId, ret);
993 break;
Chenjie Yu9da105b2018-01-13 12:41:08 -0800994 }
Rajeev Kumar22d92b72018-02-07 18:38:36 -0800995 case StatsLog.PROCESS_MEMORY_STATE: {
996 pullProcessMemoryState(tagId, ret);
997 break;
998 }
Olivier Gaillard00bfb1b2018-07-10 11:25:09 +0100999 case StatsLog.BINDER_CALLS: {
1000 pullBinderCallsStats(tagId, ret);
1001 break;
1002 }
Bookatzc68a9d22017-09-27 14:09:55 -07001003 default:
Chenjie Yu5305e1d2017-10-31 13:49:36 -07001004 Slog.w(TAG, "No such tagId data as " + tagId);
Bookatzc68a9d22017-09-27 14:09:55 -07001005 return null;
1006 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -08001007 return ret.toArray(new StatsLogEventWrapper[ret.size()]);
Bookatzc68a9d22017-09-27 14:09:55 -07001008 }
1009
1010 @Override // Binder call
Bookatzb487b552017-09-18 11:26:01 -07001011 public void statsdReady() {
1012 enforceCallingPermission();
1013 if (DEBUG) Slog.d(TAG, "learned that statsdReady");
1014 sayHiToStatsd(); // tell statsd that we're ready too and link to it
Bookatz81784082018-02-09 14:26:42 -08001015 mContext.sendBroadcastAsUser(
1016 new Intent(StatsManager.ACTION_STATSD_STARTED)
1017 .addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND),
Bookatz0eb58ae392018-01-25 15:49:26 -08001018 UserHandle.SYSTEM,
Bookatz5c800e32018-01-24 14:59:52 -08001019 android.Manifest.permission.DUMP);
Bookatzb487b552017-09-18 11:26:01 -07001020 }
1021
David Chenc136f452017-11-27 11:52:26 -08001022 @Override
1023 public void triggerUidSnapshot() {
David Chen661f7912018-01-22 17:46:24 -08001024 enforceCallingPermission();
1025 synchronized (sStatsdLock) {
David Chen35045cb2018-03-23 22:21:47 -07001026 final long token = Binder.clearCallingIdentity();
David Chen661f7912018-01-22 17:46:24 -08001027 try {
1028 informAllUidsLocked(mContext);
1029 } catch (RemoteException e) {
1030 Slog.e(TAG, "Failed to trigger uid snapshot.", e);
David Chen35045cb2018-03-23 22:21:47 -07001031 } finally {
1032 restoreCallingIdentity(token);
David Chen661f7912018-01-22 17:46:24 -08001033 }
David Chenc136f452017-11-27 11:52:26 -08001034 }
David Chenc136f452017-11-27 11:52:26 -08001035 }
1036
Bookatz94726412017-08-31 09:26:15 -07001037 private void enforceCallingPermission() {
1038 if (Binder.getCallingPid() == Process.myPid()) {
1039 return;
1040 }
1041 mContext.enforceCallingPermission(android.Manifest.permission.STATSCOMPANION, null);
1042 }
1043
Bookatzb487b552017-09-18 11:26:01 -07001044 // Lifecycle and related code
1045
David Chen1481fe12017-10-16 13:16:34 -07001046 /**
1047 * Fetches the statsd IBinder service
1048 */
Bookatzb487b552017-09-18 11:26:01 -07001049 private static IStatsManager fetchStatsdService() {
1050 return IStatsManager.Stub.asInterface(ServiceManager.getService("stats"));
1051 }
1052
1053 public static final class Lifecycle extends SystemService {
1054 private StatsCompanionService mStatsCompanionService;
1055
1056 public Lifecycle(Context context) {
1057 super(context);
1058 }
1059
1060 @Override
1061 public void onStart() {
1062 mStatsCompanionService = new StatsCompanionService(getContext());
1063 try {
1064 publishBinderService(Context.STATS_COMPANION_SERVICE, mStatsCompanionService);
1065 if (DEBUG) Slog.d(TAG, "Published " + Context.STATS_COMPANION_SERVICE);
1066 } catch (Exception e) {
1067 Slog.e(TAG, "Failed to publishBinderService", e);
1068 }
1069 }
1070
1071 @Override
1072 public void onBootPhase(int phase) {
1073 super.onBootPhase(phase);
1074 if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
1075 mStatsCompanionService.systemReady();
1076 }
1077 }
1078 }
1079
David Chen1481fe12017-10-16 13:16:34 -07001080 /**
1081 * Now that the android system is ready, StatsCompanion is ready too, so inform statsd.
1082 */
Bookatzb487b552017-09-18 11:26:01 -07001083 private void systemReady() {
1084 if (DEBUG) Slog.d(TAG, "Learned that systemReady");
1085 sayHiToStatsd();
1086 }
1087
David Chen1481fe12017-10-16 13:16:34 -07001088 /**
1089 * Tells statsd that statscompanion is ready. If the binder call returns, link to statsd.
1090 */
Bookatzb487b552017-09-18 11:26:01 -07001091 private void sayHiToStatsd() {
1092 synchronized (sStatsdLock) {
1093 if (sStatsd != null) {
1094 Slog.e(TAG, "Trying to fetch statsd, but it was already fetched",
1095 new IllegalStateException("sStatsd is not null when being fetched"));
1096 return;
1097 }
1098 sStatsd = fetchStatsdService();
1099 if (sStatsd == null) {
Bookatz111ed732018-02-16 15:54:05 -08001100 Slog.i(TAG, "Could not yet find statsd to tell it that StatsCompanion is alive.");
Bookatzb487b552017-09-18 11:26:01 -07001101 return;
1102 }
1103 if (DEBUG) Slog.d(TAG, "Saying hi to statsd");
1104 try {
1105 sStatsd.statsCompanionReady();
1106 // If the statsCompanionReady two-way binder call returns, link to statsd.
1107 try {
1108 sStatsd.asBinder().linkToDeath(new StatsdDeathRecipient(), 0);
1109 } catch (RemoteException e) {
1110 Slog.e(TAG, "linkToDeath(StatsdDeathRecipient) failed", e);
yroe26c97b2018-03-27 10:30:11 -07001111 forgetEverythingLocked();
Bookatzb487b552017-09-18 11:26:01 -07001112 }
yro947fbce2017-11-15 22:50:23 -08001113 // Setup broadcast receiver for updates.
David Chende701692017-10-05 13:16:02 -07001114 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_REPLACED);
1115 filter.addAction(Intent.ACTION_PACKAGE_ADDED);
1116 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1117 filter.addDataScheme("package");
1118 mContext.registerReceiverAsUser(mAppUpdateReceiver, UserHandle.ALL, filter, null,
David Chen1481fe12017-10-16 13:16:34 -07001119 null);
David Chen47e8f4d2017-10-11 15:34:13 -07001120
1121 // Setup receiver for user initialize (which happens once for a new user) and
1122 // if a user is removed.
1123 filter = new IntentFilter(Intent.ACTION_USER_INITIALIZE);
1124 filter.addAction(Intent.ACTION_USER_REMOVED);
1125 mContext.registerReceiverAsUser(mUserUpdateReceiver, UserHandle.ALL,
David Chen1481fe12017-10-16 13:16:34 -07001126 filter, null, null);
David Chen47e8f4d2017-10-11 15:34:13 -07001127
yro947fbce2017-11-15 22:50:23 -08001128 // Setup receiver for device reboots or shutdowns.
1129 filter = new IntentFilter(Intent.ACTION_REBOOT);
1130 filter.addAction(Intent.ACTION_SHUTDOWN);
1131 mContext.registerReceiverAsUser(
1132 mShutdownEventReceiver, UserHandle.ALL, filter, null, null);
Yao Chen0f217102018-01-09 10:33:15 -08001133 final long token = Binder.clearCallingIdentity();
1134 try {
1135 // Pull the latest state of UID->app name, version mapping when statsd starts.
1136 informAllUidsLocked(mContext);
1137 } finally {
1138 restoreCallingIdentity(token);
1139 }
Bookatz111ed732018-02-16 15:54:05 -08001140 Slog.i(TAG, "Told statsd that StatsCompanionService is alive.");
Bookatzb487b552017-09-18 11:26:01 -07001141 } catch (RemoteException e) {
1142 Slog.e(TAG, "Failed to inform statsd that statscompanion is ready", e);
yroe26c97b2018-03-27 10:30:11 -07001143 forgetEverythingLocked();
Bookatzb487b552017-09-18 11:26:01 -07001144 }
1145 }
1146 }
1147
1148 private class StatsdDeathRecipient implements IBinder.DeathRecipient {
1149 @Override
1150 public void binderDied() {
1151 Slog.i(TAG, "Statsd is dead - erase all my knowledge.");
yroe26c97b2018-03-27 10:30:11 -07001152 synchronized (sStatsdLock) {
1153 long now = SystemClock.elapsedRealtime();
1154 for (Long timeMillis : mDeathTimeMillis) {
1155 long ageMillis = now - timeMillis;
1156 if (ageMillis > MILLIS_IN_A_DAY) {
1157 mDeathTimeMillis.remove(timeMillis);
1158 }
1159 }
1160 for (Long timeMillis : mDeletedFiles.keySet()) {
1161 long ageMillis = now - timeMillis;
1162 if (ageMillis > MILLIS_IN_A_DAY * 7) {
1163 mDeletedFiles.remove(timeMillis);
1164 }
1165 }
1166 mDeathTimeMillis.add(now);
1167 if (mDeathTimeMillis.size() >= DEATH_THRESHOLD) {
1168 mDeathTimeMillis.clear();
1169 File[] configs = FileUtils.listFilesOrEmpty(new File(CONFIG_DIR));
1170 if (configs.length > 0) {
1171 String fileName = configs[0].getName();
1172 if (configs[0].delete()) {
1173 mDeletedFiles.put(now, fileName);
1174 }
1175 }
1176 }
1177 forgetEverythingLocked();
1178 }
Bookatzb487b552017-09-18 11:26:01 -07001179 }
1180 }
1181
Andreas Gampe29520212018-07-20 12:55:37 -07001182 @GuardedBy("StatsCompanionService.sStatsdLock")
yroe26c97b2018-03-27 10:30:11 -07001183 private void forgetEverythingLocked() {
1184 sStatsd = null;
1185 mContext.unregisterReceiver(mAppUpdateReceiver);
1186 mContext.unregisterReceiver(mUserUpdateReceiver);
1187 mContext.unregisterReceiver(mShutdownEventReceiver);
1188 cancelAnomalyAlarm();
1189 cancelPullingAlarm();
1190 }
1191
1192 @Override
1193 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
1194 if (!DumpUtils.checkDumpPermission(mContext, TAG, writer)) return;
1195
Bookatzb487b552017-09-18 11:26:01 -07001196 synchronized (sStatsdLock) {
yroe26c97b2018-03-27 10:30:11 -07001197 writer.println("Number of configuration files deleted: " + mDeletedFiles.size());
1198 if (mDeletedFiles.size() > 0) {
1199 writer.println(" timestamp, deleted file name");
1200 }
1201 long lastBootMillis =
1202 SystemClock.currentThreadTimeMillis() - SystemClock.elapsedRealtime();
1203 for (Long elapsedMillis : mDeletedFiles.keySet()) {
1204 long deletionMillis = lastBootMillis + elapsedMillis;
1205 writer.println(" " + deletionMillis + ", " + mDeletedFiles.get(elapsedMillis));
1206 }
Bookatzb487b552017-09-18 11:26:01 -07001207 }
1208 }
1209
Bookatz94726412017-08-31 09:26:15 -07001210}