blob: bf349efba11f18b50a60ce6cacf9b10ccb485ad5 [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;
21import android.app.PendingIntent;
Rajeev Kumar22d92b72018-02-07 18:38:36 -080022import android.app.ProcessMemoryState;
Bookatzc6977972018-01-16 16:55:05 -080023import android.app.StatsManager;
Chenjie Yu9d7720b2018-01-24 10:34:48 -080024import android.bluetooth.BluetoothActivityEnergyInfo;
25import android.bluetooth.BluetoothAdapter;
26import android.bluetooth.UidTraffic;
Bookatz94726412017-08-31 09:26:15 -070027import android.content.BroadcastReceiver;
28import android.content.Context;
29import android.content.Intent;
David Chende701692017-10-05 13:16:02 -070030import android.content.IntentFilter;
Bookatzc6977972018-01-16 16:55:05 -080031import android.content.IntentSender;
David Chende701692017-10-05 13:16:02 -070032import android.content.pm.PackageInfo;
33import android.content.pm.PackageManager;
34import android.content.pm.UserInfo;
David Chenc8a43242017-10-17 16:23:28 -070035import android.net.NetworkStats;
Chenjie Yu05013b32017-11-21 10:21:41 -080036import android.net.wifi.IWifiManager;
37import android.net.wifi.WifiActivityEnergyInfo;
David Chenc8a43242017-10-17 16:23:28 -070038import android.os.BatteryStatsInternal;
Bookatz94726412017-08-31 09:26:15 -070039import android.os.Binder;
David Chende701692017-10-05 13:16:02 -070040import android.os.Bundle;
Chenjie Yu937d7422018-01-10 16:37:53 -080041import android.os.Environment;
yroe26c97b2018-03-27 10:30:11 -070042import android.os.FileUtils;
Bookatzb487b552017-09-18 11:26:01 -070043import android.os.IBinder;
Bookatz94726412017-08-31 09:26:15 -070044import android.os.IStatsCompanionService;
45import android.os.IStatsManager;
Yangster-mac80c44742018-08-26 12:20:16 -070046import android.os.IThermalEventListener;
47import android.os.IThermalService;
Chenjie Yu05013b32017-11-21 10:21:41 -080048import android.os.Parcelable;
Bookatz94726412017-08-31 09:26:15 -070049import android.os.Process;
Bookatz1b0b1142017-09-08 11:58:42 -070050import android.os.RemoteException;
Bookatz94726412017-08-31 09:26:15 -070051import android.os.ServiceManager;
Chenjie Yu937d7422018-01-10 16:37:53 -080052import android.os.StatFs;
Chenjie Yu9d7720b2018-01-24 10:34:48 -080053import android.os.StatsDimensionsValue;
David Chen1481fe12017-10-16 13:16:34 -070054import android.os.StatsLogEventWrapper;
Chenjie Yu05013b32017-11-21 10:21:41 -080055import android.os.SynchronousResultReceiver;
Chenjie Yu937d7422018-01-10 16:37:53 -080056import android.os.SystemClock;
Yangster-mac80c44742018-08-26 12:20:16 -070057import android.os.Temperature;
David Chende701692017-10-05 13:16:02 -070058import android.os.UserHandle;
59import android.os.UserManager;
Chenjie Yu937d7422018-01-10 16:37:53 -080060import android.telephony.ModemActivityInfo;
61import android.telephony.TelephonyManager;
Bookatz94726412017-08-31 09:26:15 -070062import android.util.Slog;
David Chenc8a43242017-10-17 16:23:28 -070063import android.util.StatsLog;
64
65import com.android.internal.annotations.GuardedBy;
66import com.android.internal.net.NetworkStatsFactory;
Chenjie Yu937d7422018-01-10 16:37:53 -080067import com.android.internal.os.KernelCpuSpeedReader;
Chenjie Yuec676612018-03-07 09:19:17 -080068import com.android.internal.os.KernelUidCpuTimeReader;
69import com.android.internal.os.KernelUidCpuClusterTimeReader;
70import com.android.internal.os.KernelUidCpuActiveTimeReader;
71import com.android.internal.os.KernelUidCpuFreqTimeReader;
David Chenc8a43242017-10-17 16:23:28 -070072import com.android.internal.os.KernelWakelockReader;
73import com.android.internal.os.KernelWakelockStats;
Chenjie Yu7f8def92017-11-03 09:33:15 -070074import com.android.internal.os.PowerProfile;
yroe26c97b2018-03-27 10:30:11 -070075import com.android.internal.util.DumpUtils;
David Chenc8a43242017-10-17 16:23:28 -070076import com.android.server.LocalServices;
77import com.android.server.SystemService;
Bookatz94726412017-08-31 09:26:15 -070078
yroe26c97b2018-03-27 10:30:11 -070079import java.io.File;
80import java.io.FileDescriptor;
Chenjie Yuec676612018-03-07 09:19:17 -080081import java.io.IOException;
yroe26c97b2018-03-27 10:30:11 -070082import java.io.PrintWriter;
David Chende701692017-10-05 13:16:02 -070083import java.util.ArrayList;
Bookatz058d8692018-03-06 09:53:47 -080084import java.util.Arrays;
yroe26c97b2018-03-27 10:30:11 -070085import java.util.HashMap;
86import java.util.HashSet;
David Chende701692017-10-05 13:16:02 -070087import java.util.List;
Bookatzc68a9d22017-09-27 14:09:55 -070088import java.util.Map;
yroe26c97b2018-03-27 10:30:11 -070089import java.util.concurrent.TimeUnit;
Chenjie Yu05013b32017-11-21 10:21:41 -080090import java.util.concurrent.TimeoutException;
Bookatzc68a9d22017-09-27 14:09:55 -070091
Bookatz94726412017-08-31 09:26:15 -070092/**
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 Chen1481fe12017-10-16 13:16:34 -070095 *
Bookatz6bc51d72017-09-28 16:43:40 -070096 * @hide
Bookatz94726412017-08-31 09:26:15 -070097 */
98public class StatsCompanionService extends IStatsCompanionService.Stub {
Chenjie Yu05013b32017-11-21 10:21:41 -080099 /**
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;
yroe26c97b2018-03-27 10:30:11 -0700103 private static final long MILLIS_IN_A_DAY = TimeUnit.DAYS.toMillis(1);
Chenjie Yu05013b32017-11-21 10:21:41 -0800104
105 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
yroe26c97b2018-03-27 10:30:11 -0700106 public static final String CONFIG_DIR = "/data/misc/stats-service";
Chenjie Yu05013b32017-11-21 10:21:41 -0800107
Bookatz94726412017-08-31 09:26:15 -0700108 static final String TAG = "StatsCompanionService";
Tej Singh484524a2018-02-01 15:10:05 -0800109 static final boolean DEBUG = false;
Bookatzc6977972018-01-16 16:55:05 -0800110
David Chen661f7912018-01-22 17:46:24 -0800111 public static final int CODE_DATA_BROADCAST = 1;
Bookatzc6977972018-01-16 16:55:05 -0800112 public static final int CODE_SUBSCRIBER_BROADCAST = 1;
David Chend37bc232018-04-12 18:05:11 -0700113 /**
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";
yroe26c97b2018-03-27 10:30:11 -0700122 public static final int DEATH_THRESHOLD = 10;
Bookatzc6977972018-01-16 16:55:05 -0800123
Bookatz94726412017-08-31 09:26:15 -0700124 private final Context mContext;
125 private final AlarmManager mAlarmManager;
Bookatzb487b552017-09-18 11:26:01 -0700126 @GuardedBy("sStatsdLock")
Bookatz1b0b1142017-09-08 11:58:42 -0700127 private static IStatsManager sStatsd;
Bookatzb487b552017-09-18 11:26:01 -0700128 private static final Object sStatsdLock = new Object();
Bookatz94726412017-08-31 09:26:15 -0700129
130 private final PendingIntent mAnomalyAlarmIntent;
Chenjie Yub3dda412017-10-24 13:41:59 -0700131 private final PendingIntent mPullingAlarmIntent;
Yangster-mac932ecec2018-02-01 10:23:52 -0800132 private final PendingIntent mPeriodicAlarmIntent;
David Chende701692017-10-05 13:16:02 -0700133 private final BroadcastReceiver mAppUpdateReceiver;
David Chen47e8f4d2017-10-11 15:34:13 -0700134 private final BroadcastReceiver mUserUpdateReceiver;
yro947fbce2017-11-15 22:50:23 -0800135 private final ShutdownEventReceiver mShutdownEventReceiver;
Chenjie Yu7f8def92017-11-03 09:33:15 -0700136 private final KernelWakelockReader mKernelWakelockReader = new KernelWakelockReader();
137 private final KernelWakelockStats mTmpWakelockStats = new KernelWakelockStats();
Chenjie Yu05013b32017-11-21 10:21:41 -0800138 private IWifiManager mWifiManager = null;
139 private TelephonyManager mTelephony = null;
Chenjie Yu937d7422018-01-10 16:37:53 -0800140 private final StatFs mStatFsData = new StatFs(Environment.getDataDirectory().getAbsolutePath());
141 private final StatFs mStatFsSystem =
David Chen661f7912018-01-22 17:46:24 -0800142 new StatFs(Environment.getRootDirectory().getAbsolutePath());
Chenjie Yu937d7422018-01-10 16:37:53 -0800143 private final StatFs mStatFsTemp =
David Chen661f7912018-01-22 17:46:24 -0800144 new StatFs(Environment.getDownloadCacheDirectory().getAbsolutePath());
yroe26c97b2018-03-27 10:30:11 -0700145 @GuardedBy("sStatsdLock")
146 private final HashSet<Long> mDeathTimeMillis = new HashSet<>();
147 @GuardedBy("sStatsdLock")
148 private final HashMap<Long, String> mDeletedFiles = new HashMap<>();
Bookatz94726412017-08-31 09:26:15 -0700149
Chenjie Yuec676612018-03-07 09:19:17 -0800150 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-mac80c44742018-08-26 12:20:16 -0700159 private static IThermalService sThermalService;
160
Bookatz94726412017-08-31 09:26:15 -0700161 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 Yub3dda412017-10-24 13:41:59 -0700168 mPullingAlarmIntent = PendingIntent.getBroadcast(
David Chen661f7912018-01-22 17:46:24 -0800169 mContext, 0, new Intent(mContext, PullingAlarmReceiver.class), 0);
Yangster-mac932ecec2018-02-01 10:23:52 -0800170 mPeriodicAlarmIntent = PendingIntent.getBroadcast(
171 mContext, 0, new Intent(mContext, PeriodicAlarmReceiver.class), 0);
David Chende701692017-10-05 13:16:02 -0700172 mAppUpdateReceiver = new AppUpdateReceiver();
David Chen47e8f4d2017-10-11 15:34:13 -0700173 mUserUpdateReceiver = new BroadcastReceiver() {
174 @Override
175 public void onReceive(Context context, Intent intent) {
176 synchronized (sStatsdLock) {
177 sStatsd = fetchStatsdService();
178 if (sStatsd == null) {
Bookatz111ed732018-02-16 15:54:05 -0800179 Slog.w(TAG, "Could not access statsd for UserUpdateReceiver");
David Chen47e8f4d2017-10-11 15:34:13 -0700180 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 Chen1481fe12017-10-16 13:16:34 -0700187 Slog.e(TAG, "Failed to inform statsd latest update of all apps", e);
yroe26c97b2018-03-27 10:30:11 -0700188 forgetEverythingLocked();
David Chen47e8f4d2017-10-11 15:34:13 -0700189 }
190 }
191 }
192 };
yro947fbce2017-11-15 22:50:23 -0800193 mShutdownEventReceiver = new ShutdownEventReceiver();
Bookatz111ed732018-02-16 15:54:05 -0800194 if (DEBUG) Slog.d(TAG, "Registered receiver for ACTION_PACKAGE_REPLACED and ADDED.");
Chenjie Yu7f8def92017-11-03 09:33:15 -0700195 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 Chen661f7912018-01-22 17:46:24 -0800202 numSpeedSteps);
Chenjie Yu7f8def92017-11-03 09:33:15 -0700203 firstCpuOfCluster += powerProfile.getNumCoresInCpuCluster(i);
204 }
Chenjie Yuec676612018-03-07 09:19:17 -0800205 // 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 Yuec676612018-03-07 09:19:17 -0800209 mKernelUidCpuClusterTimeReader.setThrottleInterval(0);
210 mKernelUidCpuActiveTimeReader.setThrottleInterval(0);
Yangster-mac80c44742018-08-26 12:20:16 -0700211
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 }
Bookatz94726412017-08-31 09:26:15 -0700229 }
230
David Chenadaf8b32017-11-03 15:42:08 -0700231 @Override
David Chend37bc232018-04-12 18:05:11 -0700232 public void sendDataBroadcast(IBinder intentSenderBinder, long lastReportTimeNs) {
Bookatz36920822018-01-26 09:18:07 -0800233 enforceCallingPermission();
David Chen661f7912018-01-22 17:46:24 -0800234 IntentSender intentSender = new IntentSender(intentSenderBinder);
235 Intent intent = new Intent();
David Chend37bc232018-04-12 18:05:11 -0700236 intent.putExtra(EXTRA_LAST_REPORT_TIME, lastReportTimeNs);
David Chen661f7912018-01-22 17:46:24 -0800237 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 Chenadaf8b32017-11-03 15:42:08 -0700242 }
243
Bookatzc6977972018-01-16 16:55:05 -0800244 @Override
245 public void sendSubscriberBroadcast(IBinder intentSenderBinder, long configUid, long configKey,
246 long subscriptionId, long subscriptionRuleId,
Bookatz058d8692018-03-06 09:53:47 -0800247 String[] cookies,
Bookatzc6977972018-01-16 16:55:05 -0800248 StatsDimensionsValue dimensionsValue) {
Bookatzc6977972018-01-16 16:55:05 -0800249 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);
Bookatz058d8692018-03-06 09:53:47 -0800257
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
Bookatz1476ef22018-02-13 12:26:01 -0800263 if (DEBUG) {
Bookatz058d8692018-03-06 09:53:47 -0800264 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));
Bookatz1476ef22018-02-13 12:26:01 -0800268 }
Bookatzc6977972018-01-16 16:55:05 -0800269 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.");
Bookatzc6977972018-01-16 16:55:05 -0800274 }
275 }
276
David Chen1481fe12017-10-16 13:16:34 -0700277 private final static int[] toIntArray(List<Integer> list) {
David Chende701692017-10-05 13:16:02 -0700278 int[] ret = new int[list.size()];
David Chen1481fe12017-10-16 13:16:34 -0700279 for (int i = 0; i < ret.length; i++) {
David Chende701692017-10-05 13:16:02 -0700280 ret[i] = list.get(i);
281 }
282 return ret;
283 }
284
Dianne Hackborn3accca02013-09-20 09:32:11 -0700285 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 Chende701692017-10-05 13:16:02 -0700293 // Assumes that sStatsdLock is held.
Andreas Gampea36dc622018-02-05 17:19:22 -0800294 @GuardedBy("sStatsdLock")
David Chende701692017-10-05 13:16:02 -0700295 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) {
Bookatz111ed732018-02-16 15:54:05 -0800300 Slog.d(TAG, "Iterating over " + users.size() + " profiles.");
David Chende701692017-10-05 13:16:02 -0700301 }
302
Rajeev Kumar22d92b72018-02-07 18:38:36 -0800303 List<Integer> uids = new ArrayList<>();
304 List<Long> versions = new ArrayList<>();
305 List<String> apps = new ArrayList<>();
David Chende701692017-10-05 13:16:02 -0700306
307 // Add in all the apps for every user/profile.
308 for (UserInfo profile : users) {
David Chenbd125272018-04-04 19:02:50 -0700309 List<PackageInfo> pi =
David Chend37bc232018-04-12 18:05:11 -0700310 pm.getInstalledPackagesAsUser(PackageManager.MATCH_KNOWN_PACKAGES, profile.id);
David Chen1481fe12017-10-16 13:16:34 -0700311 for (int j = 0; j < pi.size(); j++) {
312 if (pi.get(j).applicationInfo != null) {
313 uids.add(pi.get(j).applicationInfo.uid);
Dianne Hackborn3accca02013-09-20 09:32:11 -0700314 versions.add(pi.get(j).getLongVersionCode());
David Chen1481fe12017-10-16 13:16:34 -0700315 apps.add(pi.get(j).packageName);
316 }
317 }
David Chende701692017-10-05 13:16:02 -0700318 }
Dianne Hackborn3accca02013-09-20 09:32:11 -0700319 sStatsd.informAllUidData(toIntArray(uids), toLongArray(versions), apps.toArray(new
David Chen1481fe12017-10-16 13:16:34 -0700320 String[apps.size()]));
David Chende701692017-10-05 13:16:02 -0700321 if (DEBUG) {
Bookatz111ed732018-02-16 15:54:05 -0800322 Slog.d(TAG, "Sent data for " + uids.size() + " apps");
David Chende701692017-10-05 13:16:02 -0700323 }
324 }
325
Bookatz36920822018-01-26 09:18:07 -0800326 private final static class AppUpdateReceiver extends BroadcastReceiver {
David Chende701692017-10-05 13:16:02 -0700327 @Override
328 public void onReceive(Context context, Intent intent) {
David Chen47e8f4d2017-10-11 15:34:13 -0700329 /**
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 Chend6896892017-10-25 11:49:03 -0700332 * If we can't find the value for EXTRA_REPLACING, we default to false.
David Chen47e8f4d2017-10-11 15:34:13 -0700333 */
David Chend6896892017-10-25 11:49:03 -0700334 if (!intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)
335 && intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
David Chen47e8f4d2017-10-11 15:34:13 -0700336 return; // Keep only replacing or normal add and remove.
337 }
Tej Singh90d3aa02018-03-08 19:07:58 -0800338 if (DEBUG) Slog.d(TAG, "StatsCompanionService noticed an app was updated.");
David Chende701692017-10-05 13:16:02 -0700339 synchronized (sStatsdLock) {
340 if (sStatsd == null) {
yrof6d1ca52017-11-15 15:38:34 -0800341 Slog.w(TAG, "Could not access statsd to inform it of an app update");
David Chende701692017-10-05 13:16:02 -0700342 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 Hackborn3accca02013-09-20 09:32:11 -0700362 sStatsd.informOnePackage(app, uid, pi.getLongVersionCode());
David Chende701692017-10-05 13:16:02 -0700363 }
364 } catch (Exception e) {
365 Slog.w(TAG, "Failed to inform statsd of an app update", e);
366 }
367 }
368 }
David Chen1481fe12017-10-16 13:16:34 -0700369 }
David Chende701692017-10-05 13:16:02 -0700370
Bookatz582870e2018-02-06 14:24:49 -0800371 public final static class AnomalyAlarmReceiver extends BroadcastReceiver {
Bookatz94726412017-08-31 09:26:15 -0700372 @Override
Bookatzb487b552017-09-18 11:26:01 -0700373 public void onReceive(Context context, Intent intent) {
Bookatz66fe0612018-02-07 18:51:48 -0800374 Slog.i(TAG, "StatsCompanionService believes an anomaly has occurred at time "
375 + System.currentTimeMillis() + "ms.");
Bookatzb487b552017-09-18 11:26:01 -0700376 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 }
Bookatz94726412017-08-31 09:26:15 -0700387 }
Bookatzb487b552017-09-18 11:26:01 -0700388 // AlarmManager releases its own wakelock here.
Bookatz94726412017-08-31 09:26:15 -0700389 }
David Chen1481fe12017-10-16 13:16:34 -0700390 }
Bookatzb487b552017-09-18 11:26:01 -0700391
Bookatz582870e2018-02-06 14:24:49 -0800392 public final static class PullingAlarmReceiver extends BroadcastReceiver {
David Chen661f7912018-01-22 17:46:24 -0800393 @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 }
Bookatzb487b552017-09-18 11:26:01 -0700409 }
David Chen1481fe12017-10-16 13:16:34 -0700410 }
Bookatz94726412017-08-31 09:26:15 -0700411
Yangster-mac932ecec2018-02-01 10:23:52 -0800412 public final static class PeriodicAlarmReceiver extends BroadcastReceiver {
413 @Override
414 public void onReceive(Context context, Intent intent) {
415 if (DEBUG)
Yangster-macc04feba2018-04-02 14:37:33 -0700416 Slog.d(TAG, "Time to trigger periodic alarm.");
Yangster-mac932ecec2018-02-01 10:23:52 -0800417 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 {
yro947fbce2017-11-15 22:50:23 -0800434 @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 Chen661f7912018-01-22 17:46:24 -0800440 && !(intent.getAction().equals(Intent.ACTION_SHUTDOWN)
441 && (intent.getFlags() & Intent.FLAG_RECEIVER_FOREGROUND) != 0)) {
442 return;
yro947fbce2017-11-15 22:50:23 -0800443 }
yrof7a3bcb2018-01-24 17:18:55 -0800444
yro947fbce2017-11-15 22:50:23 -0800445 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-mac892f3d32018-05-02 14:16:48 -0700452 sStatsd.informDeviceShutdown();
yro947fbce2017-11-15 22:50:23 -0800453 } catch (Exception e) {
454 Slog.w(TAG, "Failed to inform statsd of a shutdown event.", e);
455 }
456 }
457 }
458 }
459
Bookatz94726412017-08-31 09:26:15 -0700460 @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-mac330af582018-02-08 15:24:38 -0800466 // 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.
Bookatz94726412017-08-31 09:26:15 -0700469 // AlarmManager will automatically cancel any previous mAnomalyAlarmIntent alarm.
Yangster-mac330af582018-02-08 15:24:38 -0800470 mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME, timestampMs, mAnomalyAlarmIntent);
Bookatz94726412017-08-31 09:26:15 -0700471 } 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-mac932ecec2018-02-01 10:23:52 -0800489 public void setAlarmForSubscriberTriggering(long timestampMs) {
490 enforceCallingPermission();
491 if (DEBUG)
Yangster-macc04feba2018-04-02 14:37:33 -0700492 Slog.d(TAG, "Setting periodic alarm in about " +
493 (timestampMs - SystemClock.elapsedRealtime()));
Yangster-mac932ecec2018-02-01 10:23:52 -0800494 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-macc04feba2018-04-02 14:37:33 -0700498 mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME, timestampMs, mPeriodicAlarmIntent);
Yangster-mac932ecec2018-02-01 10:23:52 -0800499 } 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 Yu1a0a9412018-03-28 10:07:22 -0700518 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 }
Bookatz94726412017-08-31 09:26:15 -0700531 }
532
533 @Override // Binder call
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700534 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 }
Bookatz94726412017-08-31 09:26:15 -0700544 }
545
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800546 private void addNetworkStats(
David Chen661f7912018-01-22 17:46:24 -0800547 int tag, List<StatsLogEventWrapper> ret, NetworkStats stats, boolean withFGBG) {
548 int size = stats.size();
Yangster-macc552b352018-02-22 10:00:45 -0800549 long elapsedNanos = SystemClock.elapsedRealtimeNanos();
David Chen661f7912018-01-22 17:46:24 -0800550 NetworkStats.Entry entry = new NetworkStats.Entry(); // For recycling
551 for (int j = 0; j < size; j++) {
552 stats.getValues(j, entry);
Yangster-macc552b352018-02-22 10:00:45 -0800553 StatsLogEventWrapper e = new StatsLogEventWrapper(elapsedNanos, tag, withFGBG ? 6 : 5);
David Chen661f7912018-01-22 17:46:24 -0800554 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 Chenc8a43242017-10-17 16:23:28 -0700563 }
David Chenc8a43242017-10-17 16:23:28 -0700564 }
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 Yu05013b32017-11-21 10:21:41 -0800599 /**
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 Yu9d7720b2018-01-24 10:34:48 -0800629 private void pullKernelWakelock(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800630 final KernelWakelockStats wakelockStats =
631 mKernelWakelockReader.readKernelWakelockStats(mTmpWakelockStats);
Yangster-macc552b352018-02-22 10:00:45 -0800632 long elapsedNanos = SystemClock.elapsedRealtimeNanos();
David Chen661f7912018-01-22 17:46:24 -0800633 for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) {
634 String name = ent.getKey();
635 KernelWakelockStats.Entry kws = ent.getValue();
Yangster-macc552b352018-02-22 10:00:45 -0800636 StatsLogEventWrapper e = new StatsLogEventWrapper(elapsedNanos, tagId, 4);
David Chen661f7912018-01-22 17:46:24 -0800637 e.writeString(name);
638 e.writeInt(kws.mCount);
639 e.writeInt(kws.mVersion);
640 e.writeLong(kws.mTotalTime);
641 pulledData.add(e);
642 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800643 }
644
645 private void pullWifiBytesTransfer(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800646 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 Yu9d7720b2018-01-24 10:34:48 -0800664 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800665 }
666
667 private void pullWifiBytesTransferByFgBg(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800668 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 Yu9d7720b2018-01-24 10:34:48 -0800683 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800684 }
685
686 private void pullMobileBytesTransfer(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800687 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 Yu9d7720b2018-01-24 10:34:48 -0800704 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800705 }
706
707 private void pullBluetoothBytesTransfer(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800708 BluetoothActivityEnergyInfo info = pullBluetoothData();
Yangster-macc552b352018-02-22 10:00:45 -0800709 long elapsedNanos = SystemClock.elapsedRealtimeNanos();
Chenjie Yu9e59f932018-03-30 16:41:38 -0700710 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 Chen661f7912018-01-22 17:46:24 -0800718 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800719 }
720
721 private void pullMobileBytesTransferByFgBg(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800722 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 Yu9d7720b2018-01-24 10:34:48 -0800737 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800738 }
739
740 private void pullCpuTimePerFreq(int tagId, List<StatsLogEventWrapper> pulledData) {
Yangster-macc552b352018-02-22 10:00:45 -0800741 long elapsedNanos = SystemClock.elapsedRealtimeNanos();
David Chen661f7912018-01-22 17:46:24 -0800742 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-macc552b352018-02-22 10:00:45 -0800746 StatsLogEventWrapper e = new StatsLogEventWrapper(elapsedNanos, tagId, 3);
David Chen661f7912018-01-22 17:46:24 -0800747 e.writeInt(cluster);
748 e.writeInt(speed);
749 e.writeLong(clusterTimeMs[speed]);
750 pulledData.add(e);
751 }
752 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800753 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800754 }
755
Chenjie Yuec676612018-03-07 09:19:17 -0800756 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 Yu4460e3c2018-03-14 17:12:59 -0700771 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 Yuec676612018-03-07 09:19:17 -0800778 }
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 Yu5caaa9d2018-03-06 15:48:54 -0800805 private void pullWifiActivityInfo(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800806 long token = Binder.clearCallingIdentity();
807 if (mWifiManager == null) {
808 mWifiManager =
809 IWifiManager.Stub.asInterface(ServiceManager.getService(Context.WIFI_SERVICE));
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800810 }
David Chen661f7912018-01-22 17:46:24 -0800811 if (mWifiManager != null) {
812 try {
813 SynchronousResultReceiver wifiReceiver = new SynchronousResultReceiver("wifi");
814 mWifiManager.requestActivityInfo(wifiReceiver);
815 final WifiActivityEnergyInfo wifiInfo = awaitControllerInfo(wifiReceiver);
Yangster-macc552b352018-02-22 10:00:45 -0800816 StatsLogEventWrapper e = new StatsLogEventWrapper(SystemClock.elapsedRealtimeNanos(), tagId, 6);
David Chen661f7912018-01-22 17:46:24 -0800817 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 Yu9d7720b2018-01-24 10:34:48 -0800830 }
831
832 private void pullModemActivityInfo(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800833 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 Chen56ae0d92018-05-11 16:00:22 -0700841 StatsLogEventWrapper e = new StatsLogEventWrapper(SystemClock.elapsedRealtimeNanos(), tagId, 10);
David Chen661f7912018-01-22 17:46:24 -0800842 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 Yu9d7720b2018-01-24 10:34:48 -0800854 }
855
856 private void pullBluetoothActivityInfo(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800857 BluetoothActivityEnergyInfo info = pullBluetoothData();
Yangster-macc552b352018-02-22 10:00:45 -0800858 StatsLogEventWrapper e = new StatsLogEventWrapper(SystemClock.elapsedRealtimeNanos(), tagId, 6);
David Chen661f7912018-01-22 17:46:24 -0800859 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 Yu9d7720b2018-01-24 10:34:48 -0800866 }
867
868 private synchronized BluetoothActivityEnergyInfo pullBluetoothData() {
Chenjie Yuc2d65442018-02-01 22:27:17 -0800869 final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
David Chen661f7912018-01-22 17:46:24 -0800870 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 Yu9d7720b2018-01-24 10:34:48 -0800878 }
879
880 private void pullSystemElapsedRealtime(int tagId, List<StatsLogEventWrapper> pulledData) {
Yangster-macc552b352018-02-22 10:00:45 -0800881 StatsLogEventWrapper e = new StatsLogEventWrapper(SystemClock.elapsedRealtimeNanos(), tagId, 1);
David Chen661f7912018-01-22 17:46:24 -0800882 e.writeLong(SystemClock.elapsedRealtime());
883 pulledData.add(e);
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800884 }
885
886 private void pullDiskSpace(int tagId, List<StatsLogEventWrapper> pulledData) {
Yangster-macc552b352018-02-22 10:00:45 -0800887 StatsLogEventWrapper e = new StatsLogEventWrapper(SystemClock.elapsedRealtimeNanos(), tagId, 3);
David Chen661f7912018-01-22 17:46:24 -0800888 e.writeLong(mStatFsData.getAvailableBytes());
889 e.writeLong(mStatFsSystem.getAvailableBytes());
890 e.writeLong(mStatFsTemp.getAvailableBytes());
891 pulledData.add(e);
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800892 }
893
894 private void pullSystemUpTime(int tagId, List<StatsLogEventWrapper> pulledData) {
Yangster-macc552b352018-02-22 10:00:45 -0800895 StatsLogEventWrapper e = new StatsLogEventWrapper(SystemClock.elapsedRealtimeNanos(), tagId, 1);
David Chen661f7912018-01-22 17:46:24 -0800896 e.writeLong(SystemClock.uptimeMillis());
897 pulledData.add(e);
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800898 }
899
Rajeev Kumar22d92b72018-02-07 18:38:36 -0800900 private void pullProcessMemoryState(int tagId, List<StatsLogEventWrapper> pulledData) {
901 List<ProcessMemoryState> processMemoryStates =
902 LocalServices.getService(ActivityManagerInternal.class)
903 .getMemoryStateForProcesses();
Yangster-macc552b352018-02-22 10:00:45 -0800904 long elapsedNanos = SystemClock.elapsedRealtimeNanos();
Rajeev Kumar22d92b72018-02-07 18:38:36 -0800905 for (ProcessMemoryState processMemoryState : processMemoryStates) {
Yangster-macc552b352018-02-22 10:00:45 -0800906 StatsLogEventWrapper e = new StatsLogEventWrapper(elapsedNanos, tagId, 8 /* fields */);
Rajeev Kumar22d92b72018-02-07 18:38:36 -0800907 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 Yu05013b32017-11-21 10:21:41 -0800919 /**
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800920 * Pulls various data.
Chenjie Yu05013b32017-11-21 10:21:41 -0800921 */
Bookatzc68a9d22017-09-27 14:09:55 -0700922 @Override // Binder call
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700923 public StatsLogEventWrapper[] pullData(int tagId) {
Bookatzc68a9d22017-09-27 14:09:55 -0700924 enforceCallingPermission();
David Chenc8a43242017-10-17 16:23:28 -0700925 if (DEBUG)
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700926 Slog.d(TAG, "Pulling " + tagId);
Rajeev Kumar22d92b72018-02-07 18:38:36 -0800927 List<StatsLogEventWrapper> ret = new ArrayList<>();
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700928 switch (tagId) {
Chenjie Yu31d14d72017-12-12 17:54:33 -0800929 case StatsLog.WIFI_BYTES_TRANSFER: {
David Chen661f7912018-01-22 17:46:24 -0800930 pullWifiBytesTransfer(tagId, ret);
931 break;
David Chenc8a43242017-10-17 16:23:28 -0700932 }
Chenjie Yu31d14d72017-12-12 17:54:33 -0800933 case StatsLog.MOBILE_BYTES_TRANSFER: {
David Chen661f7912018-01-22 17:46:24 -0800934 pullMobileBytesTransfer(tagId, ret);
935 break;
David Chenc8a43242017-10-17 16:23:28 -0700936 }
Chenjie Yu31d14d72017-12-12 17:54:33 -0800937 case StatsLog.WIFI_BYTES_TRANSFER_BY_FG_BG: {
David Chen661f7912018-01-22 17:46:24 -0800938 pullWifiBytesTransferByFgBg(tagId, ret);
939 break;
David Chenc8a43242017-10-17 16:23:28 -0700940 }
Chenjie Yu31d14d72017-12-12 17:54:33 -0800941 case StatsLog.MOBILE_BYTES_TRANSFER_BY_FG_BG: {
David Chen661f7912018-01-22 17:46:24 -0800942 pullMobileBytesTransferByFgBg(tagId, ret);
943 break;
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800944 }
945 case StatsLog.BLUETOOTH_BYTES_TRANSFER: {
David Chen661f7912018-01-22 17:46:24 -0800946 pullBluetoothBytesTransfer(tagId, ret);
947 break;
David Chenc8a43242017-10-17 16:23:28 -0700948 }
Chenjie Yu31d14d72017-12-12 17:54:33 -0800949 case StatsLog.KERNEL_WAKELOCK: {
David Chen661f7912018-01-22 17:46:24 -0800950 pullKernelWakelock(tagId, ret);
951 break;
David Chen1481fe12017-10-16 13:16:34 -0700952 }
Chenjie Yu31d14d72017-12-12 17:54:33 -0800953 case StatsLog.CPU_TIME_PER_FREQ: {
David Chen661f7912018-01-22 17:46:24 -0800954 pullCpuTimePerFreq(tagId, ret);
955 break;
Chenjie Yu7f8def92017-11-03 09:33:15 -0700956 }
Chenjie Yuec676612018-03-07 09:19:17 -0800957 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 Yu5caaa9d2018-03-06 15:48:54 -0800973 case StatsLog.WIFI_ACTIVITY_INFO: {
974 pullWifiActivityInfo(tagId, ret);
David Chen661f7912018-01-22 17:46:24 -0800975 break;
Chenjie Yu05013b32017-11-21 10:21:41 -0800976 }
Chenjie Yu31d14d72017-12-12 17:54:33 -0800977 case StatsLog.MODEM_ACTIVITY_INFO: {
David Chen661f7912018-01-22 17:46:24 -0800978 pullModemActivityInfo(tagId, ret);
979 break;
Chenjie Yu05013b32017-11-21 10:21:41 -0800980 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800981 case StatsLog.BLUETOOTH_ACTIVITY_INFO: {
David Chen661f7912018-01-22 17:46:24 -0800982 pullBluetoothActivityInfo(tagId, ret);
983 break;
Chenjie Yu937d7422018-01-10 16:37:53 -0800984 }
985 case StatsLog.SYSTEM_UPTIME: {
David Chen661f7912018-01-22 17:46:24 -0800986 pullSystemUpTime(tagId, ret);
987 break;
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800988 }
989 case StatsLog.SYSTEM_ELAPSED_REALTIME: {
David Chen661f7912018-01-22 17:46:24 -0800990 pullSystemElapsedRealtime(tagId, ret);
991 break;
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800992 }
993 case StatsLog.DISK_SPACE: {
David Chen661f7912018-01-22 17:46:24 -0800994 pullDiskSpace(tagId, ret);
995 break;
Chenjie Yu9da105b2018-01-13 12:41:08 -0800996 }
Rajeev Kumar22d92b72018-02-07 18:38:36 -0800997 case StatsLog.PROCESS_MEMORY_STATE: {
998 pullProcessMemoryState(tagId, ret);
999 break;
1000 }
Bookatzc68a9d22017-09-27 14:09:55 -07001001 default:
Chenjie Yu5305e1d2017-10-31 13:49:36 -07001002 Slog.w(TAG, "No such tagId data as " + tagId);
Bookatzc68a9d22017-09-27 14:09:55 -07001003 return null;
1004 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -08001005 return ret.toArray(new StatsLogEventWrapper[ret.size()]);
Bookatzc68a9d22017-09-27 14:09:55 -07001006 }
1007
1008 @Override // Binder call
Bookatzb487b552017-09-18 11:26:01 -07001009 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
Bookatz81784082018-02-09 14:26:42 -08001013 mContext.sendBroadcastAsUser(
1014 new Intent(StatsManager.ACTION_STATSD_STARTED)
1015 .addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND),
Bookatz0eb58ae392018-01-25 15:49:26 -08001016 UserHandle.SYSTEM,
Bookatz5c800e32018-01-24 14:59:52 -08001017 android.Manifest.permission.DUMP);
Bookatzb487b552017-09-18 11:26:01 -07001018 }
1019
David Chenc136f452017-11-27 11:52:26 -08001020 @Override
1021 public void triggerUidSnapshot() {
David Chen661f7912018-01-22 17:46:24 -08001022 enforceCallingPermission();
1023 synchronized (sStatsdLock) {
David Chen35045cb2018-03-23 22:21:47 -07001024 final long token = Binder.clearCallingIdentity();
David Chen661f7912018-01-22 17:46:24 -08001025 try {
1026 informAllUidsLocked(mContext);
1027 } catch (RemoteException e) {
1028 Slog.e(TAG, "Failed to trigger uid snapshot.", e);
David Chen35045cb2018-03-23 22:21:47 -07001029 } finally {
1030 restoreCallingIdentity(token);
David Chen661f7912018-01-22 17:46:24 -08001031 }
David Chenc136f452017-11-27 11:52:26 -08001032 }
David Chenc136f452017-11-27 11:52:26 -08001033 }
1034
Bookatz94726412017-08-31 09:26:15 -07001035 private void enforceCallingPermission() {
1036 if (Binder.getCallingPid() == Process.myPid()) {
1037 return;
1038 }
1039 mContext.enforceCallingPermission(android.Manifest.permission.STATSCOMPANION, null);
1040 }
1041
Bookatzb487b552017-09-18 11:26:01 -07001042 // Lifecycle and related code
1043
David Chen1481fe12017-10-16 13:16:34 -07001044 /**
1045 * Fetches the statsd IBinder service
1046 */
Bookatzb487b552017-09-18 11:26:01 -07001047 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 Chen1481fe12017-10-16 13:16:34 -07001078 /**
1079 * Now that the android system is ready, StatsCompanion is ready too, so inform statsd.
1080 */
Bookatzb487b552017-09-18 11:26:01 -07001081 private void systemReady() {
1082 if (DEBUG) Slog.d(TAG, "Learned that systemReady");
1083 sayHiToStatsd();
1084 }
1085
David Chen1481fe12017-10-16 13:16:34 -07001086 /**
1087 * Tells statsd that statscompanion is ready. If the binder call returns, link to statsd.
1088 */
Bookatzb487b552017-09-18 11:26:01 -07001089 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) {
Bookatz111ed732018-02-16 15:54:05 -08001098 Slog.i(TAG, "Could not yet find statsd to tell it that StatsCompanion is alive.");
Bookatzb487b552017-09-18 11:26:01 -07001099 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);
yroe26c97b2018-03-27 10:30:11 -07001109 forgetEverythingLocked();
Bookatzb487b552017-09-18 11:26:01 -07001110 }
yro947fbce2017-11-15 22:50:23 -08001111 // Setup broadcast receiver for updates.
David Chende701692017-10-05 13:16:02 -07001112 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 Chen1481fe12017-10-16 13:16:34 -07001117 null);
David Chen47e8f4d2017-10-11 15:34:13 -07001118
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 Chen1481fe12017-10-16 13:16:34 -07001124 filter, null, null);
David Chen47e8f4d2017-10-11 15:34:13 -07001125
yro947fbce2017-11-15 22:50:23 -08001126 // 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 Chen0f217102018-01-09 10:33:15 -08001131 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 }
Bookatz111ed732018-02-16 15:54:05 -08001138 Slog.i(TAG, "Told statsd that StatsCompanionService is alive.");
Bookatzb487b552017-09-18 11:26:01 -07001139 } catch (RemoteException e) {
1140 Slog.e(TAG, "Failed to inform statsd that statscompanion is ready", e);
yroe26c97b2018-03-27 10:30:11 -07001141 forgetEverythingLocked();
Bookatzb487b552017-09-18 11:26:01 -07001142 }
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.");
yroe26c97b2018-03-27 10:30:11 -07001150 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 }
Bookatzb487b552017-09-18 11:26:01 -07001177 }
1178 }
1179
yroe26c97b2018-03-27 10:30:11 -07001180 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
Bookatzb487b552017-09-18 11:26:01 -07001193 synchronized (sStatsdLock) {
yroe26c97b2018-03-27 10:30:11 -07001194 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 }
Bookatzb487b552017-09-18 11:26:01 -07001204 }
1205 }
1206
Yangster-mac80c44742018-08-26 12:20:16 -07001207 // 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 Singh9ee9ba32018-11-08 16:56:16 -08001211 isThrottling ?
1212 StatsLog.THERMAL_THROTTLING_STATE_CHANGED__STATE__START :
1213 StatsLog.THERMAL_THROTTLING_STATE_CHANGED__STATE__STOP,
1214 temp.getValue());
Yangster-mac80c44742018-08-26 12:20:16 -07001215 }
1216 }
Bookatz94726412017-08-31 09:26:15 -07001217}