blob: eca98e271b88b6c3cf1317e55224c74304a28c56 [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;
Bookatz94726412017-08-31 09:26:15 -070019import android.app.AlarmManager;
20import android.app.PendingIntent;
Bookatzc6977972018-01-16 16:55:05 -080021import android.app.StatsManager;
Chenjie Yu9d7720b2018-01-24 10:34:48 -080022import android.bluetooth.BluetoothActivityEnergyInfo;
23import android.bluetooth.BluetoothAdapter;
24import android.bluetooth.UidTraffic;
Bookatz94726412017-08-31 09:26:15 -070025import android.content.BroadcastReceiver;
26import android.content.Context;
27import android.content.Intent;
David Chende701692017-10-05 13:16:02 -070028import android.content.IntentFilter;
Bookatzc6977972018-01-16 16:55:05 -080029import android.content.IntentSender;
David Chende701692017-10-05 13:16:02 -070030import android.content.pm.PackageInfo;
31import android.content.pm.PackageManager;
32import android.content.pm.UserInfo;
David Chenc8a43242017-10-17 16:23:28 -070033import android.net.NetworkStats;
Chenjie Yu05013b32017-11-21 10:21:41 -080034import android.net.wifi.IWifiManager;
35import android.net.wifi.WifiActivityEnergyInfo;
David Chenc8a43242017-10-17 16:23:28 -070036import android.os.BatteryStatsInternal;
Bookatz94726412017-08-31 09:26:15 -070037import android.os.Binder;
David Chende701692017-10-05 13:16:02 -070038import android.os.Bundle;
Chenjie Yu937d7422018-01-10 16:37:53 -080039import android.os.Environment;
Bookatzb487b552017-09-18 11:26:01 -070040import android.os.IBinder;
Bookatz94726412017-08-31 09:26:15 -070041import android.os.IStatsCompanionService;
42import android.os.IStatsManager;
Chenjie Yu05013b32017-11-21 10:21:41 -080043import android.os.Parcelable;
Bookatz94726412017-08-31 09:26:15 -070044import android.os.Process;
Bookatz1b0b1142017-09-08 11:58:42 -070045import android.os.RemoteException;
Bookatz94726412017-08-31 09:26:15 -070046import android.os.ServiceManager;
Chenjie Yu937d7422018-01-10 16:37:53 -080047import android.os.StatFs;
Chenjie Yu9d7720b2018-01-24 10:34:48 -080048import android.os.StatsDimensionsValue;
David Chen1481fe12017-10-16 13:16:34 -070049import android.os.StatsLogEventWrapper;
Chenjie Yu05013b32017-11-21 10:21:41 -080050import android.os.SynchronousResultReceiver;
Chenjie Yu937d7422018-01-10 16:37:53 -080051import android.os.SystemClock;
David Chende701692017-10-05 13:16:02 -070052import android.os.UserHandle;
53import android.os.UserManager;
Chenjie Yu937d7422018-01-10 16:37:53 -080054import android.telephony.ModemActivityInfo;
55import android.telephony.TelephonyManager;
Bookatz94726412017-08-31 09:26:15 -070056import android.util.Slog;
David Chenc8a43242017-10-17 16:23:28 -070057import android.util.StatsLog;
58
59import com.android.internal.annotations.GuardedBy;
60import com.android.internal.net.NetworkStatsFactory;
Chenjie Yu937d7422018-01-10 16:37:53 -080061import com.android.internal.os.KernelCpuSpeedReader;
David Chenc8a43242017-10-17 16:23:28 -070062import com.android.internal.os.KernelWakelockReader;
63import com.android.internal.os.KernelWakelockStats;
Chenjie Yu7f8def92017-11-03 09:33:15 -070064import com.android.internal.os.PowerProfile;
David Chenc8a43242017-10-17 16:23:28 -070065import com.android.server.LocalServices;
66import com.android.server.SystemService;
Bookatz94726412017-08-31 09:26:15 -070067
David Chende701692017-10-05 13:16:02 -070068import java.util.ArrayList;
69import java.util.List;
Bookatzc68a9d22017-09-27 14:09:55 -070070import java.util.Map;
Chenjie Yu05013b32017-11-21 10:21:41 -080071import java.util.concurrent.TimeoutException;
Bookatzc68a9d22017-09-27 14:09:55 -070072
Bookatz94726412017-08-31 09:26:15 -070073/**
74 * Helper service for statsd (the native stats management service in cmds/statsd/).
75 * Used for registering and receiving alarms on behalf of statsd.
David Chen1481fe12017-10-16 13:16:34 -070076 *
Bookatz6bc51d72017-09-28 16:43:40 -070077 * @hide
Bookatz94726412017-08-31 09:26:15 -070078 */
79public class StatsCompanionService extends IStatsCompanionService.Stub {
Chenjie Yu05013b32017-11-21 10:21:41 -080080 /**
81 * How long to wait on an individual subsystem to return its stats.
82 */
83 private static final long EXTERNAL_STATS_SYNC_TIMEOUT_MILLIS = 2000;
84
85 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
86
Bookatz94726412017-08-31 09:26:15 -070087 static final String TAG = "StatsCompanionService";
Tej Singh484524a2018-02-01 15:10:05 -080088 static final boolean DEBUG = false;
Bookatzc6977972018-01-16 16:55:05 -080089
David Chen661f7912018-01-22 17:46:24 -080090 public static final int CODE_DATA_BROADCAST = 1;
Bookatzc6977972018-01-16 16:55:05 -080091 public static final int CODE_SUBSCRIBER_BROADCAST = 1;
92
Bookatz94726412017-08-31 09:26:15 -070093 private final Context mContext;
94 private final AlarmManager mAlarmManager;
Bookatzb487b552017-09-18 11:26:01 -070095 @GuardedBy("sStatsdLock")
Bookatz1b0b1142017-09-08 11:58:42 -070096 private static IStatsManager sStatsd;
Bookatzb487b552017-09-18 11:26:01 -070097 private static final Object sStatsdLock = new Object();
Bookatz94726412017-08-31 09:26:15 -070098
99 private final PendingIntent mAnomalyAlarmIntent;
Chenjie Yub3dda412017-10-24 13:41:59 -0700100 private final PendingIntent mPullingAlarmIntent;
David Chende701692017-10-05 13:16:02 -0700101 private final BroadcastReceiver mAppUpdateReceiver;
David Chen47e8f4d2017-10-11 15:34:13 -0700102 private final BroadcastReceiver mUserUpdateReceiver;
yro947fbce2017-11-15 22:50:23 -0800103 private final ShutdownEventReceiver mShutdownEventReceiver;
Chenjie Yu7f8def92017-11-03 09:33:15 -0700104 private final KernelWakelockReader mKernelWakelockReader = new KernelWakelockReader();
105 private final KernelWakelockStats mTmpWakelockStats = new KernelWakelockStats();
106 private final KernelCpuSpeedReader[] mKernelCpuSpeedReaders;
Chenjie Yu05013b32017-11-21 10:21:41 -0800107 private IWifiManager mWifiManager = null;
108 private TelephonyManager mTelephony = null;
Chenjie Yu937d7422018-01-10 16:37:53 -0800109 private final StatFs mStatFsData = new StatFs(Environment.getDataDirectory().getAbsolutePath());
110 private final StatFs mStatFsSystem =
David Chen661f7912018-01-22 17:46:24 -0800111 new StatFs(Environment.getRootDirectory().getAbsolutePath());
Chenjie Yu937d7422018-01-10 16:37:53 -0800112 private final StatFs mStatFsTemp =
David Chen661f7912018-01-22 17:46:24 -0800113 new StatFs(Environment.getDownloadCacheDirectory().getAbsolutePath());
Bookatz94726412017-08-31 09:26:15 -0700114
Bookatz94726412017-08-31 09:26:15 -0700115 public StatsCompanionService(Context context) {
116 super();
117 mContext = context;
118 mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
119
120 mAnomalyAlarmIntent = PendingIntent.getBroadcast(mContext, 0,
121 new Intent(mContext, AnomalyAlarmReceiver.class), 0);
Chenjie Yub3dda412017-10-24 13:41:59 -0700122 mPullingAlarmIntent = PendingIntent.getBroadcast(
David Chen661f7912018-01-22 17:46:24 -0800123 mContext, 0, new Intent(mContext, PullingAlarmReceiver.class), 0);
David Chende701692017-10-05 13:16:02 -0700124 mAppUpdateReceiver = new AppUpdateReceiver();
David Chen47e8f4d2017-10-11 15:34:13 -0700125 mUserUpdateReceiver = new BroadcastReceiver() {
126 @Override
127 public void onReceive(Context context, Intent intent) {
128 synchronized (sStatsdLock) {
129 sStatsd = fetchStatsdService();
130 if (sStatsd == null) {
Bookatz111ed732018-02-16 15:54:05 -0800131 Slog.w(TAG, "Could not access statsd for UserUpdateReceiver");
David Chen47e8f4d2017-10-11 15:34:13 -0700132 return;
133 }
134 try {
135 // Pull the latest state of UID->app name, version mapping.
136 // Needed since the new user basically has a version of every app.
137 informAllUidsLocked(context);
138 } catch (RemoteException e) {
David Chen1481fe12017-10-16 13:16:34 -0700139 Slog.e(TAG, "Failed to inform statsd latest update of all apps", e);
David Chen47e8f4d2017-10-11 15:34:13 -0700140 forgetEverything();
141 }
142 }
143 }
144 };
yro947fbce2017-11-15 22:50:23 -0800145 mShutdownEventReceiver = new ShutdownEventReceiver();
Bookatz111ed732018-02-16 15:54:05 -0800146 if (DEBUG) Slog.d(TAG, "Registered receiver for ACTION_PACKAGE_REPLACED and ADDED.");
Chenjie Yu7f8def92017-11-03 09:33:15 -0700147 PowerProfile powerProfile = new PowerProfile(context);
148 final int numClusters = powerProfile.getNumCpuClusters();
149 mKernelCpuSpeedReaders = new KernelCpuSpeedReader[numClusters];
150 int firstCpuOfCluster = 0;
151 for (int i = 0; i < numClusters; i++) {
152 final int numSpeedSteps = powerProfile.getNumSpeedStepsInCpuCluster(i);
153 mKernelCpuSpeedReaders[i] = new KernelCpuSpeedReader(firstCpuOfCluster,
David Chen661f7912018-01-22 17:46:24 -0800154 numSpeedSteps);
Chenjie Yu7f8def92017-11-03 09:33:15 -0700155 firstCpuOfCluster += powerProfile.getNumCoresInCpuCluster(i);
156 }
Bookatz94726412017-08-31 09:26:15 -0700157 }
158
David Chenadaf8b32017-11-03 15:42:08 -0700159 @Override
David Chen661f7912018-01-22 17:46:24 -0800160 public void sendDataBroadcast(IBinder intentSenderBinder) {
Bookatz36920822018-01-26 09:18:07 -0800161 enforceCallingPermission();
David Chen661f7912018-01-22 17:46:24 -0800162 IntentSender intentSender = new IntentSender(intentSenderBinder);
163 Intent intent = new Intent();
164 try {
165 intentSender.sendIntent(mContext, CODE_DATA_BROADCAST, intent, null, null);
166 } catch (IntentSender.SendIntentException e) {
167 Slog.w(TAG, "Unable to send using IntentSender");
168 }
David Chenadaf8b32017-11-03 15:42:08 -0700169 }
170
Bookatzc6977972018-01-16 16:55:05 -0800171 @Override
172 public void sendSubscriberBroadcast(IBinder intentSenderBinder, long configUid, long configKey,
173 long subscriptionId, long subscriptionRuleId,
174 StatsDimensionsValue dimensionsValue) {
Bookatzc6977972018-01-16 16:55:05 -0800175 enforceCallingPermission();
176 IntentSender intentSender = new IntentSender(intentSenderBinder);
177 Intent intent = new Intent()
178 .putExtra(StatsManager.EXTRA_STATS_CONFIG_UID, configUid)
179 .putExtra(StatsManager.EXTRA_STATS_CONFIG_KEY, configKey)
180 .putExtra(StatsManager.EXTRA_STATS_SUBSCRIPTION_ID, subscriptionId)
181 .putExtra(StatsManager.EXTRA_STATS_SUBSCRIPTION_RULE_ID, subscriptionRuleId)
182 .putExtra(StatsManager.EXTRA_STATS_DIMENSIONS_VALUE, dimensionsValue);
Bookatz1476ef22018-02-13 12:26:01 -0800183 if (DEBUG) {
184 Slog.d(TAG, String.format("Statsd sendSubscriberBroadcast with params {%d %d %d %d %s}",
185 configUid, configKey, subscriptionId,
186 subscriptionRuleId, dimensionsValue));
187 }
Bookatzc6977972018-01-16 16:55:05 -0800188 try {
189 intentSender.sendIntent(mContext, CODE_SUBSCRIBER_BROADCAST, intent, null, null);
190 } catch (IntentSender.SendIntentException e) {
191 Slog.w(TAG, "Unable to send using IntentSender from uid " + configUid
192 + "; presumably it had been cancelled.");
Bookatzc6977972018-01-16 16:55:05 -0800193 }
194 }
195
David Chen1481fe12017-10-16 13:16:34 -0700196 private final static int[] toIntArray(List<Integer> list) {
David Chende701692017-10-05 13:16:02 -0700197 int[] ret = new int[list.size()];
David Chen1481fe12017-10-16 13:16:34 -0700198 for (int i = 0; i < ret.length; i++) {
David Chende701692017-10-05 13:16:02 -0700199 ret[i] = list.get(i);
200 }
201 return ret;
202 }
203
Dianne Hackborn3accca02013-09-20 09:32:11 -0700204 private final static long[] toLongArray(List<Long> list) {
205 long[] ret = new long[list.size()];
206 for (int i = 0; i < ret.length; i++) {
207 ret[i] = list.get(i);
208 }
209 return ret;
210 }
211
David Chende701692017-10-05 13:16:02 -0700212 // Assumes that sStatsdLock is held.
Andreas Gampea36dc622018-02-05 17:19:22 -0800213 @GuardedBy("sStatsdLock")
David Chende701692017-10-05 13:16:02 -0700214 private final void informAllUidsLocked(Context context) throws RemoteException {
215 UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
216 PackageManager pm = context.getPackageManager();
217 final List<UserInfo> users = um.getUsers(true);
218 if (DEBUG) {
Bookatz111ed732018-02-16 15:54:05 -0800219 Slog.d(TAG, "Iterating over " + users.size() + " profiles.");
David Chende701692017-10-05 13:16:02 -0700220 }
221
222 List<Integer> uids = new ArrayList();
Dianne Hackborn3accca02013-09-20 09:32:11 -0700223 List<Long> versions = new ArrayList();
David Chende701692017-10-05 13:16:02 -0700224 List<String> apps = new ArrayList();
225
226 // Add in all the apps for every user/profile.
227 for (UserInfo profile : users) {
David Chen1481fe12017-10-16 13:16:34 -0700228 List<PackageInfo> pi = pm.getInstalledPackagesAsUser(0, profile.id);
229 for (int j = 0; j < pi.size(); j++) {
230 if (pi.get(j).applicationInfo != null) {
231 uids.add(pi.get(j).applicationInfo.uid);
Dianne Hackborn3accca02013-09-20 09:32:11 -0700232 versions.add(pi.get(j).getLongVersionCode());
David Chen1481fe12017-10-16 13:16:34 -0700233 apps.add(pi.get(j).packageName);
234 }
235 }
David Chende701692017-10-05 13:16:02 -0700236 }
Dianne Hackborn3accca02013-09-20 09:32:11 -0700237 sStatsd.informAllUidData(toIntArray(uids), toLongArray(versions), apps.toArray(new
David Chen1481fe12017-10-16 13:16:34 -0700238 String[apps.size()]));
David Chende701692017-10-05 13:16:02 -0700239 if (DEBUG) {
Bookatz111ed732018-02-16 15:54:05 -0800240 Slog.d(TAG, "Sent data for " + uids.size() + " apps");
David Chende701692017-10-05 13:16:02 -0700241 }
242 }
243
Bookatz36920822018-01-26 09:18:07 -0800244 private final static class AppUpdateReceiver extends BroadcastReceiver {
David Chende701692017-10-05 13:16:02 -0700245 @Override
246 public void onReceive(Context context, Intent intent) {
David Chen47e8f4d2017-10-11 15:34:13 -0700247 /**
248 * App updates actually consist of REMOVE, ADD, and then REPLACE broadcasts. To avoid
249 * waste, we ignore the REMOVE and ADD broadcasts that contain the replacing flag.
David Chend6896892017-10-25 11:49:03 -0700250 * If we can't find the value for EXTRA_REPLACING, we default to false.
David Chen47e8f4d2017-10-11 15:34:13 -0700251 */
David Chend6896892017-10-25 11:49:03 -0700252 if (!intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)
253 && intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
David Chen47e8f4d2017-10-11 15:34:13 -0700254 return; // Keep only replacing or normal add and remove.
255 }
David Chend6896892017-10-25 11:49:03 -0700256 Slog.i(TAG, "StatsCompanionService noticed an app was updated.");
David Chende701692017-10-05 13:16:02 -0700257 synchronized (sStatsdLock) {
258 if (sStatsd == null) {
yrof6d1ca52017-11-15 15:38:34 -0800259 Slog.w(TAG, "Could not access statsd to inform it of an app update");
David Chende701692017-10-05 13:16:02 -0700260 return;
261 }
262 try {
263 if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) {
264 Bundle b = intent.getExtras();
265 int uid = b.getInt(Intent.EXTRA_UID);
266 boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
267 if (!replacing) {
268 // Don't bother sending an update if we're right about to get another
269 // intent for the new version that's added.
270 PackageManager pm = context.getPackageManager();
271 String app = intent.getData().getSchemeSpecificPart();
272 sStatsd.informOnePackageRemoved(app, uid);
273 }
274 } else {
275 PackageManager pm = context.getPackageManager();
276 Bundle b = intent.getExtras();
277 int uid = b.getInt(Intent.EXTRA_UID);
278 String app = intent.getData().getSchemeSpecificPart();
279 PackageInfo pi = pm.getPackageInfo(app, PackageManager.MATCH_ANY_USER);
Dianne Hackborn3accca02013-09-20 09:32:11 -0700280 sStatsd.informOnePackage(app, uid, pi.getLongVersionCode());
David Chende701692017-10-05 13:16:02 -0700281 }
282 } catch (Exception e) {
283 Slog.w(TAG, "Failed to inform statsd of an app update", e);
284 }
285 }
286 }
David Chen1481fe12017-10-16 13:16:34 -0700287 }
David Chende701692017-10-05 13:16:02 -0700288
Bookatz582870e2018-02-06 14:24:49 -0800289 public final static class AnomalyAlarmReceiver extends BroadcastReceiver {
Bookatz94726412017-08-31 09:26:15 -0700290 @Override
Bookatzb487b552017-09-18 11:26:01 -0700291 public void onReceive(Context context, Intent intent) {
Bookatz66fe0612018-02-07 18:51:48 -0800292 Slog.i(TAG, "StatsCompanionService believes an anomaly has occurred at time "
293 + System.currentTimeMillis() + "ms.");
Bookatzb487b552017-09-18 11:26:01 -0700294 synchronized (sStatsdLock) {
295 if (sStatsd == null) {
296 Slog.w(TAG, "Could not access statsd to inform it of anomaly alarm firing");
297 return;
298 }
299 try {
300 // Two-way call to statsd to retain AlarmManager wakelock
301 sStatsd.informAnomalyAlarmFired();
302 } catch (RemoteException e) {
303 Slog.w(TAG, "Failed to inform statsd of anomaly alarm firing", e);
304 }
Bookatz94726412017-08-31 09:26:15 -0700305 }
Bookatzb487b552017-09-18 11:26:01 -0700306 // AlarmManager releases its own wakelock here.
Bookatz94726412017-08-31 09:26:15 -0700307 }
David Chen1481fe12017-10-16 13:16:34 -0700308 }
Bookatzb487b552017-09-18 11:26:01 -0700309
Bookatz582870e2018-02-06 14:24:49 -0800310 public final static class PullingAlarmReceiver extends BroadcastReceiver {
David Chen661f7912018-01-22 17:46:24 -0800311 @Override
312 public void onReceive(Context context, Intent intent) {
313 if (DEBUG)
314 Slog.d(TAG, "Time to poll something.");
315 synchronized (sStatsdLock) {
316 if (sStatsd == null) {
317 Slog.w(TAG, "Could not access statsd to inform it of pulling alarm firing.");
318 return;
319 }
320 try {
321 // Two-way call to statsd to retain AlarmManager wakelock
322 sStatsd.informPollAlarmFired();
323 } catch (RemoteException e) {
324 Slog.w(TAG, "Failed to inform statsd of pulling alarm firing.", e);
325 }
326 }
Bookatzb487b552017-09-18 11:26:01 -0700327 }
David Chen1481fe12017-10-16 13:16:34 -0700328 }
Bookatz94726412017-08-31 09:26:15 -0700329
Bookatz36920822018-01-26 09:18:07 -0800330 private final static class ShutdownEventReceiver extends BroadcastReceiver {
yro947fbce2017-11-15 22:50:23 -0800331 @Override
332 public void onReceive(Context context, Intent intent) {
333 /**
334 * Skip immediately if intent is not relevant to device shutdown.
335 */
336 if (!intent.getAction().equals(Intent.ACTION_REBOOT)
David Chen661f7912018-01-22 17:46:24 -0800337 && !(intent.getAction().equals(Intent.ACTION_SHUTDOWN)
338 && (intent.getFlags() & Intent.FLAG_RECEIVER_FOREGROUND) != 0)) {
339 return;
yro947fbce2017-11-15 22:50:23 -0800340 }
yrof7a3bcb2018-01-24 17:18:55 -0800341
yro947fbce2017-11-15 22:50:23 -0800342 Slog.i(TAG, "StatsCompanionService noticed a shutdown.");
343 synchronized (sStatsdLock) {
344 if (sStatsd == null) {
345 Slog.w(TAG, "Could not access statsd to inform it of a shutdown event.");
346 return;
347 }
348 try {
349 sStatsd.writeDataToDisk();
350 } catch (Exception e) {
351 Slog.w(TAG, "Failed to inform statsd of a shutdown event.", e);
352 }
353 }
354 }
355 }
356
Bookatz94726412017-08-31 09:26:15 -0700357 @Override // Binder call
358 public void setAnomalyAlarm(long timestampMs) {
359 enforceCallingPermission();
360 if (DEBUG) Slog.d(TAG, "Setting anomaly alarm for " + timestampMs);
361 final long callingToken = Binder.clearCallingIdentity();
362 try {
Yangster-mac330af582018-02-08 15:24:38 -0800363 // using ELAPSED_REALTIME, not ELAPSED_REALTIME_WAKEUP, so if device is asleep, will
364 // only fire when it awakens.
365 // This alarm is inexact, leaving its exactness completely up to the OS optimizations.
Bookatz94726412017-08-31 09:26:15 -0700366 // AlarmManager will automatically cancel any previous mAnomalyAlarmIntent alarm.
Yangster-mac330af582018-02-08 15:24:38 -0800367 mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME, timestampMs, mAnomalyAlarmIntent);
Bookatz94726412017-08-31 09:26:15 -0700368 } finally {
369 Binder.restoreCallingIdentity(callingToken);
370 }
371 }
372
373 @Override // Binder call
374 public void cancelAnomalyAlarm() {
375 enforceCallingPermission();
376 if (DEBUG) Slog.d(TAG, "Cancelling anomaly alarm");
377 final long callingToken = Binder.clearCallingIdentity();
378 try {
379 mAlarmManager.cancel(mAnomalyAlarmIntent);
380 } finally {
381 Binder.restoreCallingIdentity(callingToken);
382 }
383 }
384
385 @Override // Binder call
Chenjie Yub3dda412017-10-24 13:41:59 -0700386 public void setPullingAlarms(long timestampMs, long intervalMs) {
David Chen661f7912018-01-22 17:46:24 -0800387 enforceCallingPermission();
388 if (DEBUG)
389 Slog.d(TAG, "Setting pulling alarm for " + timestampMs + " every " + intervalMs + "ms");
390 final long callingToken = Binder.clearCallingIdentity();
391 try {
Yangster-mac330af582018-02-08 15:24:38 -0800392 // using ELAPSED_REALTIME, not ELAPSED_REALTIME_WAKEUP, so if device is asleep, will
393 // only fire when it awakens.
David Chen661f7912018-01-22 17:46:24 -0800394 // This alarm is inexact, leaving its exactness completely up to the OS optimizations.
395 // TODO: totally inexact means that stats per bucket could be quite off. Is this okay?
Yangster-mac330af582018-02-08 15:24:38 -0800396 mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, timestampMs, intervalMs,
397 mPullingAlarmIntent);
David Chen661f7912018-01-22 17:46:24 -0800398 } finally {
399 Binder.restoreCallingIdentity(callingToken);
400 }
Bookatz94726412017-08-31 09:26:15 -0700401 }
402
403 @Override // Binder call
Chenjie Yub3dda412017-10-24 13:41:59 -0700404 public void cancelPullingAlarms() {
David Chen661f7912018-01-22 17:46:24 -0800405 enforceCallingPermission();
406 if (DEBUG)
407 Slog.d(TAG, "Cancelling pulling alarm");
408 final long callingToken = Binder.clearCallingIdentity();
409 try {
410 mAlarmManager.cancel(mPullingAlarmIntent);
411 } finally {
412 Binder.restoreCallingIdentity(callingToken);
413 }
Bookatz94726412017-08-31 09:26:15 -0700414 }
415
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800416 private void addNetworkStats(
David Chen661f7912018-01-22 17:46:24 -0800417 int tag, List<StatsLogEventWrapper> ret, NetworkStats stats, boolean withFGBG) {
418 int size = stats.size();
419 NetworkStats.Entry entry = new NetworkStats.Entry(); // For recycling
420 for (int j = 0; j < size; j++) {
421 stats.getValues(j, entry);
422 StatsLogEventWrapper e = new StatsLogEventWrapper(tag, withFGBG ? 6 : 5);
423 e.writeInt(entry.uid);
424 if (withFGBG) {
425 e.writeInt(entry.set);
426 }
427 e.writeLong(entry.rxBytes);
428 e.writeLong(entry.rxPackets);
429 e.writeLong(entry.txBytes);
430 e.writeLong(entry.txPackets);
431 ret.add(e);
David Chenc8a43242017-10-17 16:23:28 -0700432 }
David Chenc8a43242017-10-17 16:23:28 -0700433 }
434
435 /**
436 * Allows rollups per UID but keeping the set (foreground/background) slicing.
437 * Adapted from groupedByUid in frameworks/base/core/java/android/net/NetworkStats.java
438 */
439 private NetworkStats rollupNetworkStatsByFGBG(NetworkStats stats) {
440 final NetworkStats ret = new NetworkStats(stats.getElapsedRealtime(), 1);
441
442 final NetworkStats.Entry entry = new NetworkStats.Entry();
443 entry.iface = NetworkStats.IFACE_ALL;
444 entry.tag = NetworkStats.TAG_NONE;
445 entry.metered = NetworkStats.METERED_ALL;
446 entry.roaming = NetworkStats.ROAMING_ALL;
447
448 int size = stats.size();
449 NetworkStats.Entry recycle = new NetworkStats.Entry(); // Used for retrieving values
450 for (int i = 0; i < size; i++) {
451 stats.getValues(i, recycle);
452
453 // Skip specific tags, since already counted in TAG_NONE
454 if (recycle.tag != NetworkStats.TAG_NONE) continue;
455
456 entry.set = recycle.set; // Allows slicing by background/foreground
457 entry.uid = recycle.uid;
458 entry.rxBytes = recycle.rxBytes;
459 entry.rxPackets = recycle.rxPackets;
460 entry.txBytes = recycle.txBytes;
461 entry.txPackets = recycle.txPackets;
462 // Operations purposefully omitted since we don't use them for statsd.
463 ret.combineValues(entry);
464 }
465 return ret;
466 }
467
Chenjie Yu05013b32017-11-21 10:21:41 -0800468 /**
469 * Helper method to extract the Parcelable controller info from a
470 * SynchronousResultReceiver.
471 */
472 private static <T extends Parcelable> T awaitControllerInfo(
473 @Nullable SynchronousResultReceiver receiver) {
474 if (receiver == null) {
475 return null;
476 }
477
478 try {
479 final SynchronousResultReceiver.Result result =
480 receiver.awaitResult(EXTERNAL_STATS_SYNC_TIMEOUT_MILLIS);
481 if (result.bundle != null) {
482 // This is the final destination for the Bundle.
483 result.bundle.setDefusable(true);
484
485 final T data = result.bundle.getParcelable(
486 RESULT_RECEIVER_CONTROLLER_KEY);
487 if (data != null) {
488 return data;
489 }
490 }
491 Slog.e(TAG, "no controller energy info supplied for " + receiver.getName());
492 } catch (TimeoutException e) {
493 Slog.w(TAG, "timeout reading " + receiver.getName() + " stats");
494 }
495 return null;
496 }
497
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800498 private void pullKernelWakelock(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800499 final KernelWakelockStats wakelockStats =
500 mKernelWakelockReader.readKernelWakelockStats(mTmpWakelockStats);
501 for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) {
502 String name = ent.getKey();
503 KernelWakelockStats.Entry kws = ent.getValue();
504 StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 4);
505 e.writeString(name);
506 e.writeInt(kws.mCount);
507 e.writeInt(kws.mVersion);
508 e.writeLong(kws.mTotalTime);
509 pulledData.add(e);
510 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800511 }
512
513 private void pullWifiBytesTransfer(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800514 long token = Binder.clearCallingIdentity();
515 try {
516 // TODO: Consider caching the following call to get BatteryStatsInternal.
517 BatteryStatsInternal bs = LocalServices.getService(BatteryStatsInternal.class);
518 String[] ifaces = bs.getWifiIfaces();
519 if (ifaces.length == 0) {
520 return;
521 }
522 NetworkStatsFactory nsf = new NetworkStatsFactory();
523 // Combine all the metrics per Uid into one record.
524 NetworkStats stats =
525 nsf.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces, NetworkStats.TAG_NONE, null)
526 .groupedByUid();
527 addNetworkStats(tagId, pulledData, stats, false);
528 } catch (java.io.IOException e) {
529 Slog.e(TAG, "Pulling netstats for wifi bytes has error", e);
530 } finally {
531 Binder.restoreCallingIdentity(token);
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800532 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800533 }
534
535 private void pullWifiBytesTransferByFgBg(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800536 long token = Binder.clearCallingIdentity();
537 try {
538 BatteryStatsInternal bs = LocalServices.getService(BatteryStatsInternal.class);
539 String[] ifaces = bs.getWifiIfaces();
540 if (ifaces.length == 0) {
541 return;
542 }
543 NetworkStatsFactory nsf = new NetworkStatsFactory();
544 NetworkStats stats = rollupNetworkStatsByFGBG(
545 nsf.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces, NetworkStats.TAG_NONE, null));
546 addNetworkStats(tagId, pulledData, stats, true);
547 } catch (java.io.IOException e) {
548 Slog.e(TAG, "Pulling netstats for wifi bytes w/ fg/bg has error", e);
549 } finally {
550 Binder.restoreCallingIdentity(token);
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800551 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800552 }
553
554 private void pullMobileBytesTransfer(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800555 long token = Binder.clearCallingIdentity();
556 try {
557 BatteryStatsInternal bs = LocalServices.getService(BatteryStatsInternal.class);
558 String[] ifaces = bs.getMobileIfaces();
559 if (ifaces.length == 0) {
560 return;
561 }
562 NetworkStatsFactory nsf = new NetworkStatsFactory();
563 // Combine all the metrics per Uid into one record.
564 NetworkStats stats =
565 nsf.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces, NetworkStats.TAG_NONE, null)
566 .groupedByUid();
567 addNetworkStats(tagId, pulledData, stats, false);
568 } catch (java.io.IOException e) {
569 Slog.e(TAG, "Pulling netstats for mobile bytes has error", e);
570 } finally {
571 Binder.restoreCallingIdentity(token);
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800572 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800573 }
574
575 private void pullBluetoothBytesTransfer(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800576 BluetoothActivityEnergyInfo info = pullBluetoothData();
577 for (UidTraffic traffic : info.getUidTraffic()) {
578 StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 3);
579 e.writeInt(traffic.getUid());
580 e.writeLong(traffic.getRxBytes());
581 e.writeLong(traffic.getTxBytes());
582 pulledData.add(e);
583 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800584 }
585
586 private void pullMobileBytesTransferByFgBg(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800587 long token = Binder.clearCallingIdentity();
588 try {
589 BatteryStatsInternal bs = LocalServices.getService(BatteryStatsInternal.class);
590 String[] ifaces = bs.getMobileIfaces();
591 if (ifaces.length == 0) {
592 return;
593 }
594 NetworkStatsFactory nsf = new NetworkStatsFactory();
595 NetworkStats stats = rollupNetworkStatsByFGBG(
596 nsf.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces, NetworkStats.TAG_NONE, null));
597 addNetworkStats(tagId, pulledData, stats, true);
598 } catch (java.io.IOException e) {
599 Slog.e(TAG, "Pulling netstats for mobile bytes w/ fg/bg has error", e);
600 } finally {
601 Binder.restoreCallingIdentity(token);
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800602 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800603 }
604
605 private void pullCpuTimePerFreq(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800606 for (int cluster = 0; cluster < mKernelCpuSpeedReaders.length; cluster++) {
607 long[] clusterTimeMs = mKernelCpuSpeedReaders[cluster].readAbsolute();
608 if (clusterTimeMs != null) {
609 for (int speed = clusterTimeMs.length - 1; speed >= 0; --speed) {
610 StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 3);
611 e.writeInt(cluster);
612 e.writeInt(speed);
613 e.writeLong(clusterTimeMs[speed]);
614 pulledData.add(e);
615 }
616 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800617 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800618 }
619
620 private void pullWifiActivityEnergyInfo(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800621 long token = Binder.clearCallingIdentity();
622 if (mWifiManager == null) {
623 mWifiManager =
624 IWifiManager.Stub.asInterface(ServiceManager.getService(Context.WIFI_SERVICE));
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800625 }
David Chen661f7912018-01-22 17:46:24 -0800626 if (mWifiManager != null) {
627 try {
628 SynchronousResultReceiver wifiReceiver = new SynchronousResultReceiver("wifi");
629 mWifiManager.requestActivityInfo(wifiReceiver);
630 final WifiActivityEnergyInfo wifiInfo = awaitControllerInfo(wifiReceiver);
631 StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 6);
632 e.writeLong(wifiInfo.getTimeStamp());
633 e.writeInt(wifiInfo.getStackState());
634 e.writeLong(wifiInfo.getControllerTxTimeMillis());
635 e.writeLong(wifiInfo.getControllerRxTimeMillis());
636 e.writeLong(wifiInfo.getControllerIdleTimeMillis());
637 e.writeLong(wifiInfo.getControllerEnergyUsed());
638 pulledData.add(e);
639 } catch (RemoteException e) {
640 Slog.e(TAG, "Pulling wifiManager for wifi controller activity energy info has error", e);
641 } finally {
642 Binder.restoreCallingIdentity(token);
643 }
644 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800645 }
646
647 private void pullModemActivityInfo(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800648 long token = Binder.clearCallingIdentity();
649 if (mTelephony == null) {
650 mTelephony = TelephonyManager.from(mContext);
651 }
652 if (mTelephony != null) {
653 SynchronousResultReceiver modemReceiver = new SynchronousResultReceiver("telephony");
654 mTelephony.requestModemActivityInfo(modemReceiver);
655 final ModemActivityInfo modemInfo = awaitControllerInfo(modemReceiver);
656 StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 6);
657 e.writeLong(modemInfo.getTimestamp());
658 e.writeLong(modemInfo.getSleepTimeMillis());
659 e.writeLong(modemInfo.getIdleTimeMillis());
660 e.writeLong(modemInfo.getTxTimeMillis()[0]);
661 e.writeLong(modemInfo.getTxTimeMillis()[1]);
662 e.writeLong(modemInfo.getTxTimeMillis()[2]);
663 e.writeLong(modemInfo.getTxTimeMillis()[3]);
664 e.writeLong(modemInfo.getTxTimeMillis()[4]);
665 e.writeLong(modemInfo.getRxTimeMillis());
666 e.writeLong(modemInfo.getEnergyUsed());
667 pulledData.add(e);
668 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800669 }
670
671 private void pullBluetoothActivityInfo(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800672 BluetoothActivityEnergyInfo info = pullBluetoothData();
673 StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 6);
674 e.writeLong(info.getTimeStamp());
675 e.writeInt(info.getBluetoothStackState());
676 e.writeLong(info.getControllerTxTimeMillis());
677 e.writeLong(info.getControllerRxTimeMillis());
678 e.writeLong(info.getControllerIdleTimeMillis());
679 e.writeLong(info.getControllerEnergyUsed());
680 pulledData.add(e);
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800681 }
682
683 private synchronized BluetoothActivityEnergyInfo pullBluetoothData() {
Chenjie Yuc2d65442018-02-01 22:27:17 -0800684 final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
David Chen661f7912018-01-22 17:46:24 -0800685 if (adapter != null) {
686 SynchronousResultReceiver bluetoothReceiver = new SynchronousResultReceiver("bluetooth");
687 adapter.requestControllerActivityEnergyInfo(bluetoothReceiver);
688 return awaitControllerInfo(bluetoothReceiver);
689 } else {
690 Slog.e(TAG, "Failed to get bluetooth adapter!");
691 return null;
692 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800693 }
694
695 private void pullSystemElapsedRealtime(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800696 StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 1);
697 e.writeLong(SystemClock.elapsedRealtime());
698 pulledData.add(e);
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800699 }
700
701 private void pullDiskSpace(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800702 StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 3);
703 e.writeLong(mStatFsData.getAvailableBytes());
704 e.writeLong(mStatFsSystem.getAvailableBytes());
705 e.writeLong(mStatFsTemp.getAvailableBytes());
706 pulledData.add(e);
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800707 }
708
709 private void pullSystemUpTime(int tagId, List<StatsLogEventWrapper> pulledData) {
David Chen661f7912018-01-22 17:46:24 -0800710 StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 1);
711 e.writeLong(SystemClock.uptimeMillis());
712 pulledData.add(e);
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800713 }
714
Chenjie Yu05013b32017-11-21 10:21:41 -0800715 /**
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800716 * Pulls various data.
Chenjie Yu05013b32017-11-21 10:21:41 -0800717 */
Bookatzc68a9d22017-09-27 14:09:55 -0700718 @Override // Binder call
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700719 public StatsLogEventWrapper[] pullData(int tagId) {
Bookatzc68a9d22017-09-27 14:09:55 -0700720 enforceCallingPermission();
David Chenc8a43242017-10-17 16:23:28 -0700721 if (DEBUG)
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700722 Slog.d(TAG, "Pulling " + tagId);
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800723 List<StatsLogEventWrapper> ret = new ArrayList();
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700724 switch (tagId) {
Chenjie Yu31d14d72017-12-12 17:54:33 -0800725 case StatsLog.WIFI_BYTES_TRANSFER: {
David Chen661f7912018-01-22 17:46:24 -0800726 pullWifiBytesTransfer(tagId, ret);
727 break;
David Chenc8a43242017-10-17 16:23:28 -0700728 }
Chenjie Yu31d14d72017-12-12 17:54:33 -0800729 case StatsLog.MOBILE_BYTES_TRANSFER: {
David Chen661f7912018-01-22 17:46:24 -0800730 pullMobileBytesTransfer(tagId, ret);
731 break;
David Chenc8a43242017-10-17 16:23:28 -0700732 }
Chenjie Yu31d14d72017-12-12 17:54:33 -0800733 case StatsLog.WIFI_BYTES_TRANSFER_BY_FG_BG: {
David Chen661f7912018-01-22 17:46:24 -0800734 pullWifiBytesTransferByFgBg(tagId, ret);
735 break;
David Chenc8a43242017-10-17 16:23:28 -0700736 }
Chenjie Yu31d14d72017-12-12 17:54:33 -0800737 case StatsLog.MOBILE_BYTES_TRANSFER_BY_FG_BG: {
David Chen661f7912018-01-22 17:46:24 -0800738 pullMobileBytesTransferByFgBg(tagId, ret);
739 break;
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800740 }
741 case StatsLog.BLUETOOTH_BYTES_TRANSFER: {
David Chen661f7912018-01-22 17:46:24 -0800742 pullBluetoothBytesTransfer(tagId, ret);
743 break;
David Chenc8a43242017-10-17 16:23:28 -0700744 }
Chenjie Yu31d14d72017-12-12 17:54:33 -0800745 case StatsLog.KERNEL_WAKELOCK: {
David Chen661f7912018-01-22 17:46:24 -0800746 pullKernelWakelock(tagId, ret);
747 break;
David Chen1481fe12017-10-16 13:16:34 -0700748 }
Chenjie Yu31d14d72017-12-12 17:54:33 -0800749 case StatsLog.CPU_TIME_PER_FREQ: {
David Chen661f7912018-01-22 17:46:24 -0800750 pullCpuTimePerFreq(tagId, ret);
751 break;
Chenjie Yu7f8def92017-11-03 09:33:15 -0700752 }
Chenjie Yu31d14d72017-12-12 17:54:33 -0800753 case StatsLog.WIFI_ACTIVITY_ENERGY_INFO: {
David Chen661f7912018-01-22 17:46:24 -0800754 pullWifiActivityEnergyInfo(tagId, ret);
755 break;
Chenjie Yu05013b32017-11-21 10:21:41 -0800756 }
Chenjie Yu31d14d72017-12-12 17:54:33 -0800757 case StatsLog.MODEM_ACTIVITY_INFO: {
David Chen661f7912018-01-22 17:46:24 -0800758 pullModemActivityInfo(tagId, ret);
759 break;
Chenjie Yu05013b32017-11-21 10:21:41 -0800760 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800761 case StatsLog.BLUETOOTH_ACTIVITY_INFO: {
David Chen661f7912018-01-22 17:46:24 -0800762 pullBluetoothActivityInfo(tagId, ret);
763 break;
Chenjie Yu937d7422018-01-10 16:37:53 -0800764 }
765 case StatsLog.SYSTEM_UPTIME: {
David Chen661f7912018-01-22 17:46:24 -0800766 pullSystemUpTime(tagId, ret);
767 break;
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800768 }
769 case StatsLog.SYSTEM_ELAPSED_REALTIME: {
David Chen661f7912018-01-22 17:46:24 -0800770 pullSystemElapsedRealtime(tagId, ret);
771 break;
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800772 }
773 case StatsLog.DISK_SPACE: {
David Chen661f7912018-01-22 17:46:24 -0800774 pullDiskSpace(tagId, ret);
775 break;
Chenjie Yu9da105b2018-01-13 12:41:08 -0800776 }
Bookatzc68a9d22017-09-27 14:09:55 -0700777 default:
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700778 Slog.w(TAG, "No such tagId data as " + tagId);
Bookatzc68a9d22017-09-27 14:09:55 -0700779 return null;
780 }
Chenjie Yu9d7720b2018-01-24 10:34:48 -0800781 return ret.toArray(new StatsLogEventWrapper[ret.size()]);
Bookatzc68a9d22017-09-27 14:09:55 -0700782 }
783
784 @Override // Binder call
Bookatzb487b552017-09-18 11:26:01 -0700785 public void statsdReady() {
786 enforceCallingPermission();
787 if (DEBUG) Slog.d(TAG, "learned that statsdReady");
788 sayHiToStatsd(); // tell statsd that we're ready too and link to it
Bookatz81784082018-02-09 14:26:42 -0800789 mContext.sendBroadcastAsUser(
790 new Intent(StatsManager.ACTION_STATSD_STARTED)
791 .addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND),
Bookatz0eb58ae392018-01-25 15:49:26 -0800792 UserHandle.SYSTEM,
Bookatz5c800e32018-01-24 14:59:52 -0800793 android.Manifest.permission.DUMP);
Bookatzb487b552017-09-18 11:26:01 -0700794 }
795
David Chenc136f452017-11-27 11:52:26 -0800796 @Override
797 public void triggerUidSnapshot() {
David Chen661f7912018-01-22 17:46:24 -0800798 enforceCallingPermission();
799 synchronized (sStatsdLock) {
800 try {
801 informAllUidsLocked(mContext);
802 } catch (RemoteException e) {
803 Slog.e(TAG, "Failed to trigger uid snapshot.", e);
804 }
David Chenc136f452017-11-27 11:52:26 -0800805 }
David Chenc136f452017-11-27 11:52:26 -0800806 }
807
Bookatz94726412017-08-31 09:26:15 -0700808 private void enforceCallingPermission() {
809 if (Binder.getCallingPid() == Process.myPid()) {
810 return;
811 }
812 mContext.enforceCallingPermission(android.Manifest.permission.STATSCOMPANION, null);
813 }
814
Bookatzb487b552017-09-18 11:26:01 -0700815 // Lifecycle and related code
816
David Chen1481fe12017-10-16 13:16:34 -0700817 /**
818 * Fetches the statsd IBinder service
819 */
Bookatzb487b552017-09-18 11:26:01 -0700820 private static IStatsManager fetchStatsdService() {
821 return IStatsManager.Stub.asInterface(ServiceManager.getService("stats"));
822 }
823
824 public static final class Lifecycle extends SystemService {
825 private StatsCompanionService mStatsCompanionService;
826
827 public Lifecycle(Context context) {
828 super(context);
829 }
830
831 @Override
832 public void onStart() {
833 mStatsCompanionService = new StatsCompanionService(getContext());
834 try {
835 publishBinderService(Context.STATS_COMPANION_SERVICE, mStatsCompanionService);
836 if (DEBUG) Slog.d(TAG, "Published " + Context.STATS_COMPANION_SERVICE);
837 } catch (Exception e) {
838 Slog.e(TAG, "Failed to publishBinderService", e);
839 }
840 }
841
842 @Override
843 public void onBootPhase(int phase) {
844 super.onBootPhase(phase);
845 if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
846 mStatsCompanionService.systemReady();
847 }
848 }
849 }
850
David Chen1481fe12017-10-16 13:16:34 -0700851 /**
852 * Now that the android system is ready, StatsCompanion is ready too, so inform statsd.
853 */
Bookatzb487b552017-09-18 11:26:01 -0700854 private void systemReady() {
855 if (DEBUG) Slog.d(TAG, "Learned that systemReady");
856 sayHiToStatsd();
857 }
858
David Chen1481fe12017-10-16 13:16:34 -0700859 /**
860 * Tells statsd that statscompanion is ready. If the binder call returns, link to statsd.
861 */
Bookatzb487b552017-09-18 11:26:01 -0700862 private void sayHiToStatsd() {
863 synchronized (sStatsdLock) {
864 if (sStatsd != null) {
865 Slog.e(TAG, "Trying to fetch statsd, but it was already fetched",
866 new IllegalStateException("sStatsd is not null when being fetched"));
867 return;
868 }
869 sStatsd = fetchStatsdService();
870 if (sStatsd == null) {
Bookatz111ed732018-02-16 15:54:05 -0800871 Slog.i(TAG, "Could not yet find statsd to tell it that StatsCompanion is alive.");
Bookatzb487b552017-09-18 11:26:01 -0700872 return;
873 }
874 if (DEBUG) Slog.d(TAG, "Saying hi to statsd");
875 try {
876 sStatsd.statsCompanionReady();
877 // If the statsCompanionReady two-way binder call returns, link to statsd.
878 try {
879 sStatsd.asBinder().linkToDeath(new StatsdDeathRecipient(), 0);
880 } catch (RemoteException e) {
881 Slog.e(TAG, "linkToDeath(StatsdDeathRecipient) failed", e);
882 forgetEverything();
883 }
yro947fbce2017-11-15 22:50:23 -0800884 // Setup broadcast receiver for updates.
David Chende701692017-10-05 13:16:02 -0700885 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_REPLACED);
886 filter.addAction(Intent.ACTION_PACKAGE_ADDED);
887 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
888 filter.addDataScheme("package");
889 mContext.registerReceiverAsUser(mAppUpdateReceiver, UserHandle.ALL, filter, null,
David Chen1481fe12017-10-16 13:16:34 -0700890 null);
David Chen47e8f4d2017-10-11 15:34:13 -0700891
892 // Setup receiver for user initialize (which happens once for a new user) and
893 // if a user is removed.
894 filter = new IntentFilter(Intent.ACTION_USER_INITIALIZE);
895 filter.addAction(Intent.ACTION_USER_REMOVED);
896 mContext.registerReceiverAsUser(mUserUpdateReceiver, UserHandle.ALL,
David Chen1481fe12017-10-16 13:16:34 -0700897 filter, null, null);
David Chen47e8f4d2017-10-11 15:34:13 -0700898
yro947fbce2017-11-15 22:50:23 -0800899 // Setup receiver for device reboots or shutdowns.
900 filter = new IntentFilter(Intent.ACTION_REBOOT);
901 filter.addAction(Intent.ACTION_SHUTDOWN);
902 mContext.registerReceiverAsUser(
903 mShutdownEventReceiver, UserHandle.ALL, filter, null, null);
Yao Chen0f217102018-01-09 10:33:15 -0800904 final long token = Binder.clearCallingIdentity();
905 try {
906 // Pull the latest state of UID->app name, version mapping when statsd starts.
907 informAllUidsLocked(mContext);
908 } finally {
909 restoreCallingIdentity(token);
910 }
Bookatz111ed732018-02-16 15:54:05 -0800911 Slog.i(TAG, "Told statsd that StatsCompanionService is alive.");
Bookatzb487b552017-09-18 11:26:01 -0700912 } catch (RemoteException e) {
913 Slog.e(TAG, "Failed to inform statsd that statscompanion is ready", e);
914 forgetEverything();
915 }
916 }
917 }
918
919 private class StatsdDeathRecipient implements IBinder.DeathRecipient {
920 @Override
921 public void binderDied() {
922 Slog.i(TAG, "Statsd is dead - erase all my knowledge.");
923 forgetEverything();
924 }
925 }
926
927 private void forgetEverything() {
928 synchronized (sStatsdLock) {
929 sStatsd = null;
David Chende701692017-10-05 13:16:02 -0700930 mContext.unregisterReceiver(mAppUpdateReceiver);
David Chen47e8f4d2017-10-11 15:34:13 -0700931 mContext.unregisterReceiver(mUserUpdateReceiver);
yro947fbce2017-11-15 22:50:23 -0800932 mContext.unregisterReceiver(mShutdownEventReceiver);
Bookatzb487b552017-09-18 11:26:01 -0700933 cancelAnomalyAlarm();
Chenjie Yub3dda412017-10-24 13:41:59 -0700934 cancelPullingAlarms();
Bookatzb487b552017-09-18 11:26:01 -0700935 }
936 }
937
Bookatz94726412017-08-31 09:26:15 -0700938}