blob: faafb39cd1f3002d51911e6f871f59fa53582b11 [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;
Bookatz94726412017-08-31 09:26:15 -070022import android.content.BroadcastReceiver;
23import android.content.Context;
24import android.content.Intent;
David Chende701692017-10-05 13:16:02 -070025import android.content.IntentFilter;
Bookatzc6977972018-01-16 16:55:05 -080026import android.content.IntentSender;
David Chende701692017-10-05 13:16:02 -070027import android.content.pm.PackageInfo;
28import android.content.pm.PackageManager;
29import android.content.pm.UserInfo;
David Chenc8a43242017-10-17 16:23:28 -070030import android.net.NetworkStats;
Chenjie Yu05013b32017-11-21 10:21:41 -080031import android.net.wifi.IWifiManager;
32import android.net.wifi.WifiActivityEnergyInfo;
Bookatzc6977972018-01-16 16:55:05 -080033import android.os.StatsDimensionsValue;
David Chenc8a43242017-10-17 16:23:28 -070034import android.os.BatteryStatsInternal;
Bookatz94726412017-08-31 09:26:15 -070035import android.os.Binder;
David Chende701692017-10-05 13:16:02 -070036import android.os.Bundle;
Chenjie Yu937d7422018-01-10 16:37:53 -080037import android.os.Environment;
Bookatzb487b552017-09-18 11:26:01 -070038import android.os.IBinder;
Bookatz94726412017-08-31 09:26:15 -070039import android.os.IStatsCompanionService;
40import android.os.IStatsManager;
Chenjie Yu05013b32017-11-21 10:21:41 -080041import android.os.Parcelable;
Bookatz94726412017-08-31 09:26:15 -070042import android.os.Process;
Bookatz1b0b1142017-09-08 11:58:42 -070043import android.os.RemoteException;
Bookatz94726412017-08-31 09:26:15 -070044import android.os.ServiceManager;
Chenjie Yu937d7422018-01-10 16:37:53 -080045import android.os.StatFs;
David Chen1481fe12017-10-16 13:16:34 -070046import android.os.StatsLogEventWrapper;
Chenjie Yu05013b32017-11-21 10:21:41 -080047import android.os.SynchronousResultReceiver;
Chenjie Yu937d7422018-01-10 16:37:53 -080048import android.os.SystemClock;
David Chende701692017-10-05 13:16:02 -070049import android.os.UserHandle;
50import android.os.UserManager;
Chenjie Yu937d7422018-01-10 16:37:53 -080051import android.telephony.ModemActivityInfo;
52import android.telephony.TelephonyManager;
Bookatz94726412017-08-31 09:26:15 -070053import android.util.Slog;
David Chenc8a43242017-10-17 16:23:28 -070054import android.util.StatsLog;
55
56import com.android.internal.annotations.GuardedBy;
57import com.android.internal.net.NetworkStatsFactory;
Chenjie Yu937d7422018-01-10 16:37:53 -080058import com.android.internal.os.KernelCpuSpeedReader;
David Chenc8a43242017-10-17 16:23:28 -070059import com.android.internal.os.KernelWakelockReader;
60import com.android.internal.os.KernelWakelockStats;
Chenjie Yu7f8def92017-11-03 09:33:15 -070061import com.android.internal.os.PowerProfile;
David Chenc8a43242017-10-17 16:23:28 -070062import com.android.server.LocalServices;
63import com.android.server.SystemService;
Bookatz94726412017-08-31 09:26:15 -070064
David Chende701692017-10-05 13:16:02 -070065import java.util.ArrayList;
66import java.util.List;
Bookatzc68a9d22017-09-27 14:09:55 -070067import java.util.Map;
Chenjie Yu05013b32017-11-21 10:21:41 -080068import java.util.concurrent.TimeoutException;
Bookatzc68a9d22017-09-27 14:09:55 -070069
Bookatz94726412017-08-31 09:26:15 -070070/**
71 * Helper service for statsd (the native stats management service in cmds/statsd/).
72 * Used for registering and receiving alarms on behalf of statsd.
David Chen1481fe12017-10-16 13:16:34 -070073 *
Bookatz6bc51d72017-09-28 16:43:40 -070074 * @hide
Bookatz94726412017-08-31 09:26:15 -070075 */
76public class StatsCompanionService extends IStatsCompanionService.Stub {
Chenjie Yu05013b32017-11-21 10:21:41 -080077 /**
78 * How long to wait on an individual subsystem to return its stats.
79 */
80 private static final long EXTERNAL_STATS_SYNC_TIMEOUT_MILLIS = 2000;
81
82 public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
83
Bookatz94726412017-08-31 09:26:15 -070084 static final String TAG = "StatsCompanionService";
85 static final boolean DEBUG = true;
Bookatzc6977972018-01-16 16:55:05 -080086
David Chenadaf8b32017-11-03 15:42:08 -070087 public static final String ACTION_TRIGGER_COLLECTION =
88 "com.android.server.stats.action.TRIGGER_COLLECTION";
Bookatz94726412017-08-31 09:26:15 -070089
Bookatzc6977972018-01-16 16:55:05 -080090 public static final int CODE_SUBSCRIBER_BROADCAST = 1;
91
Bookatz94726412017-08-31 09:26:15 -070092 private final Context mContext;
93 private final AlarmManager mAlarmManager;
Bookatzb487b552017-09-18 11:26:01 -070094 @GuardedBy("sStatsdLock")
Bookatz1b0b1142017-09-08 11:58:42 -070095 private static IStatsManager sStatsd;
Bookatzb487b552017-09-18 11:26:01 -070096 private static final Object sStatsdLock = new Object();
Bookatz94726412017-08-31 09:26:15 -070097
98 private final PendingIntent mAnomalyAlarmIntent;
Chenjie Yub3dda412017-10-24 13:41:59 -070099 private final PendingIntent mPullingAlarmIntent;
David Chende701692017-10-05 13:16:02 -0700100 private final BroadcastReceiver mAppUpdateReceiver;
David Chen47e8f4d2017-10-11 15:34:13 -0700101 private final BroadcastReceiver mUserUpdateReceiver;
yro947fbce2017-11-15 22:50:23 -0800102 private final ShutdownEventReceiver mShutdownEventReceiver;
Chenjie Yu7f8def92017-11-03 09:33:15 -0700103 private final KernelWakelockReader mKernelWakelockReader = new KernelWakelockReader();
104 private final KernelWakelockStats mTmpWakelockStats = new KernelWakelockStats();
105 private final KernelCpuSpeedReader[] mKernelCpuSpeedReaders;
Chenjie Yu05013b32017-11-21 10:21:41 -0800106 private IWifiManager mWifiManager = null;
107 private TelephonyManager mTelephony = null;
Chenjie Yu937d7422018-01-10 16:37:53 -0800108 private final StatFs mStatFsData = new StatFs(Environment.getDataDirectory().getAbsolutePath());
109 private final StatFs mStatFsSystem =
110 new StatFs(Environment.getRootDirectory().getAbsolutePath());
111 private final StatFs mStatFsTemp =
112 new StatFs(Environment.getDownloadCacheDirectory().getAbsolutePath());
Bookatz94726412017-08-31 09:26:15 -0700113
Bookatz94726412017-08-31 09:26:15 -0700114 public StatsCompanionService(Context context) {
115 super();
116 mContext = context;
117 mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
118
119 mAnomalyAlarmIntent = PendingIntent.getBroadcast(mContext, 0,
120 new Intent(mContext, AnomalyAlarmReceiver.class), 0);
Chenjie Yub3dda412017-10-24 13:41:59 -0700121 mPullingAlarmIntent = PendingIntent.getBroadcast(
122 mContext, 0, new Intent(mContext, PullingAlarmReceiver.class), 0);
David Chende701692017-10-05 13:16:02 -0700123 mAppUpdateReceiver = new AppUpdateReceiver();
David Chen47e8f4d2017-10-11 15:34:13 -0700124 mUserUpdateReceiver = new BroadcastReceiver() {
125 @Override
126 public void onReceive(Context context, Intent intent) {
127 synchronized (sStatsdLock) {
128 sStatsd = fetchStatsdService();
129 if (sStatsd == null) {
130 Slog.w(TAG, "Could not access statsd");
131 return;
132 }
133 try {
134 // Pull the latest state of UID->app name, version mapping.
135 // Needed since the new user basically has a version of every app.
136 informAllUidsLocked(context);
137 } catch (RemoteException e) {
David Chen1481fe12017-10-16 13:16:34 -0700138 Slog.e(TAG, "Failed to inform statsd latest update of all apps", e);
David Chen47e8f4d2017-10-11 15:34:13 -0700139 forgetEverything();
140 }
141 }
142 }
143 };
yro947fbce2017-11-15 22:50:23 -0800144 mShutdownEventReceiver = new ShutdownEventReceiver();
David Chende701692017-10-05 13:16:02 -0700145 Slog.w(TAG, "Registered receiver for ACTION_PACKAGE_REPLACE AND ADDED.");
Chenjie Yu7f8def92017-11-03 09:33:15 -0700146 PowerProfile powerProfile = new PowerProfile(context);
147 final int numClusters = powerProfile.getNumCpuClusters();
148 mKernelCpuSpeedReaders = new KernelCpuSpeedReader[numClusters];
149 int firstCpuOfCluster = 0;
150 for (int i = 0; i < numClusters; i++) {
151 final int numSpeedSteps = powerProfile.getNumSpeedStepsInCpuCluster(i);
152 mKernelCpuSpeedReaders[i] = new KernelCpuSpeedReader(firstCpuOfCluster,
153 numSpeedSteps);
154 firstCpuOfCluster += powerProfile.getNumCoresInCpuCluster(i);
155 }
Bookatz94726412017-08-31 09:26:15 -0700156 }
157
David Chenadaf8b32017-11-03 15:42:08 -0700158 @Override
159 public void sendBroadcast(String pkg, String cls) {
Bookatzc6977972018-01-16 16:55:05 -0800160 // TODO: Use a pending intent, and enfoceCallingPermission.
David Chenadaf8b32017-11-03 15:42:08 -0700161 mContext.sendBroadcastAsUser(new Intent(ACTION_TRIGGER_COLLECTION).setClassName(pkg, cls),
162 UserHandle.SYSTEM);
163 }
164
Bookatzc6977972018-01-16 16:55:05 -0800165 @Override
166 public void sendSubscriberBroadcast(IBinder intentSenderBinder, long configUid, long configKey,
167 long subscriptionId, long subscriptionRuleId,
168 StatsDimensionsValue dimensionsValue) {
169 if (DEBUG) Slog.d(TAG, "Statsd requested to sendSubscriberBroadcast.");
170 enforceCallingPermission();
171 IntentSender intentSender = new IntentSender(intentSenderBinder);
172 Intent intent = new Intent()
173 .putExtra(StatsManager.EXTRA_STATS_CONFIG_UID, configUid)
174 .putExtra(StatsManager.EXTRA_STATS_CONFIG_KEY, configKey)
175 .putExtra(StatsManager.EXTRA_STATS_SUBSCRIPTION_ID, subscriptionId)
176 .putExtra(StatsManager.EXTRA_STATS_SUBSCRIPTION_RULE_ID, subscriptionRuleId)
177 .putExtra(StatsManager.EXTRA_STATS_DIMENSIONS_VALUE, dimensionsValue);
178 try {
179 intentSender.sendIntent(mContext, CODE_SUBSCRIBER_BROADCAST, intent, null, null);
180 } catch (IntentSender.SendIntentException e) {
181 Slog.w(TAG, "Unable to send using IntentSender from uid " + configUid
182 + "; presumably it had been cancelled.");
183 if (DEBUG) {
184 Slog.d(TAG, String.format("SubscriberBroadcast params {%d %d %d %d %s}",
185 configUid, configKey, subscriptionId,
186 subscriptionRuleId, dimensionsValue));
187 }
188 }
189 }
190
David Chen1481fe12017-10-16 13:16:34 -0700191 private final static int[] toIntArray(List<Integer> list) {
David Chende701692017-10-05 13:16:02 -0700192 int[] ret = new int[list.size()];
David Chen1481fe12017-10-16 13:16:34 -0700193 for (int i = 0; i < ret.length; i++) {
David Chende701692017-10-05 13:16:02 -0700194 ret[i] = list.get(i);
195 }
196 return ret;
197 }
198
Dianne Hackborn3accca02013-09-20 09:32:11 -0700199 private final static long[] toLongArray(List<Long> list) {
200 long[] ret = new long[list.size()];
201 for (int i = 0; i < ret.length; i++) {
202 ret[i] = list.get(i);
203 }
204 return ret;
205 }
206
David Chende701692017-10-05 13:16:02 -0700207 // Assumes that sStatsdLock is held.
208 private final void informAllUidsLocked(Context context) throws RemoteException {
209 UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
210 PackageManager pm = context.getPackageManager();
211 final List<UserInfo> users = um.getUsers(true);
212 if (DEBUG) {
David Chen1481fe12017-10-16 13:16:34 -0700213 Slog.w(TAG, "Iterating over " + users.size() + " profiles.");
David Chende701692017-10-05 13:16:02 -0700214 }
215
216 List<Integer> uids = new ArrayList();
Dianne Hackborn3accca02013-09-20 09:32:11 -0700217 List<Long> versions = new ArrayList();
David Chende701692017-10-05 13:16:02 -0700218 List<String> apps = new ArrayList();
219
220 // Add in all the apps for every user/profile.
221 for (UserInfo profile : users) {
David Chen1481fe12017-10-16 13:16:34 -0700222 List<PackageInfo> pi = pm.getInstalledPackagesAsUser(0, profile.id);
223 for (int j = 0; j < pi.size(); j++) {
224 if (pi.get(j).applicationInfo != null) {
225 uids.add(pi.get(j).applicationInfo.uid);
Dianne Hackborn3accca02013-09-20 09:32:11 -0700226 versions.add(pi.get(j).getLongVersionCode());
David Chen1481fe12017-10-16 13:16:34 -0700227 apps.add(pi.get(j).packageName);
228 }
229 }
David Chende701692017-10-05 13:16:02 -0700230 }
Dianne Hackborn3accca02013-09-20 09:32:11 -0700231 sStatsd.informAllUidData(toIntArray(uids), toLongArray(versions), apps.toArray(new
David Chen1481fe12017-10-16 13:16:34 -0700232 String[apps.size()]));
David Chende701692017-10-05 13:16:02 -0700233 if (DEBUG) {
David Chen1481fe12017-10-16 13:16:34 -0700234 Slog.w(TAG, "Sent data for " + uids.size() + " apps");
David Chende701692017-10-05 13:16:02 -0700235 }
236 }
237
David Chen1481fe12017-10-16 13:16:34 -0700238 public final static class AppUpdateReceiver extends BroadcastReceiver {
David Chende701692017-10-05 13:16:02 -0700239 @Override
240 public void onReceive(Context context, Intent intent) {
David Chen47e8f4d2017-10-11 15:34:13 -0700241 /**
242 * App updates actually consist of REMOVE, ADD, and then REPLACE broadcasts. To avoid
243 * waste, we ignore the REMOVE and ADD broadcasts that contain the replacing flag.
David Chend6896892017-10-25 11:49:03 -0700244 * If we can't find the value for EXTRA_REPLACING, we default to false.
David Chen47e8f4d2017-10-11 15:34:13 -0700245 */
David Chend6896892017-10-25 11:49:03 -0700246 if (!intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)
247 && intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
David Chen47e8f4d2017-10-11 15:34:13 -0700248 return; // Keep only replacing or normal add and remove.
249 }
David Chend6896892017-10-25 11:49:03 -0700250 Slog.i(TAG, "StatsCompanionService noticed an app was updated.");
David Chende701692017-10-05 13:16:02 -0700251 synchronized (sStatsdLock) {
252 if (sStatsd == null) {
yrof6d1ca52017-11-15 15:38:34 -0800253 Slog.w(TAG, "Could not access statsd to inform it of an app update");
David Chende701692017-10-05 13:16:02 -0700254 return;
255 }
256 try {
257 if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) {
258 Bundle b = intent.getExtras();
259 int uid = b.getInt(Intent.EXTRA_UID);
260 boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
261 if (!replacing) {
262 // Don't bother sending an update if we're right about to get another
263 // intent for the new version that's added.
264 PackageManager pm = context.getPackageManager();
265 String app = intent.getData().getSchemeSpecificPart();
266 sStatsd.informOnePackageRemoved(app, uid);
267 }
268 } else {
269 PackageManager pm = context.getPackageManager();
270 Bundle b = intent.getExtras();
271 int uid = b.getInt(Intent.EXTRA_UID);
272 String app = intent.getData().getSchemeSpecificPart();
273 PackageInfo pi = pm.getPackageInfo(app, PackageManager.MATCH_ANY_USER);
Dianne Hackborn3accca02013-09-20 09:32:11 -0700274 sStatsd.informOnePackage(app, uid, pi.getLongVersionCode());
David Chende701692017-10-05 13:16:02 -0700275 }
276 } catch (Exception e) {
277 Slog.w(TAG, "Failed to inform statsd of an app update", e);
278 }
279 }
280 }
David Chen1481fe12017-10-16 13:16:34 -0700281 }
David Chende701692017-10-05 13:16:02 -0700282
David Chen1481fe12017-10-16 13:16:34 -0700283 public final static class AnomalyAlarmReceiver extends BroadcastReceiver {
Bookatz94726412017-08-31 09:26:15 -0700284 @Override
Bookatzb487b552017-09-18 11:26:01 -0700285 public void onReceive(Context context, Intent intent) {
286 Slog.i(TAG, "StatsCompanionService believes an anomaly has occurred.");
287 synchronized (sStatsdLock) {
288 if (sStatsd == null) {
289 Slog.w(TAG, "Could not access statsd to inform it of anomaly alarm firing");
290 return;
291 }
292 try {
293 // Two-way call to statsd to retain AlarmManager wakelock
294 sStatsd.informAnomalyAlarmFired();
295 } catch (RemoteException e) {
296 Slog.w(TAG, "Failed to inform statsd of anomaly alarm firing", e);
297 }
Bookatz94726412017-08-31 09:26:15 -0700298 }
Bookatzb487b552017-09-18 11:26:01 -0700299 // AlarmManager releases its own wakelock here.
Bookatz94726412017-08-31 09:26:15 -0700300 }
David Chen1481fe12017-10-16 13:16:34 -0700301 }
Bookatzb487b552017-09-18 11:26:01 -0700302
Chenjie Yub3dda412017-10-24 13:41:59 -0700303 public final static class PullingAlarmReceiver extends BroadcastReceiver {
304 @Override
305 public void onReceive(Context context, Intent intent) {
306 if (DEBUG)
307 Slog.d(TAG, "Time to poll something.");
308 synchronized (sStatsdLock) {
309 if (sStatsd == null) {
yro947fbce2017-11-15 22:50:23 -0800310 Slog.w(TAG, "Could not access statsd to inform it of pulling alarm firing.");
Chenjie Yub3dda412017-10-24 13:41:59 -0700311 return;
312 }
313 try {
314 // Two-way call to statsd to retain AlarmManager wakelock
315 sStatsd.informPollAlarmFired();
316 } catch (RemoteException e) {
yro947fbce2017-11-15 22:50:23 -0800317 Slog.w(TAG, "Failed to inform statsd of pulling alarm firing.", e);
Chenjie Yub3dda412017-10-24 13:41:59 -0700318 }
Bookatzb487b552017-09-18 11:26:01 -0700319 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700320 // AlarmManager releases its own wakelock here.
321 }
David Chen1481fe12017-10-16 13:16:34 -0700322 }
Bookatz94726412017-08-31 09:26:15 -0700323
yro947fbce2017-11-15 22:50:23 -0800324 public final static class ShutdownEventReceiver extends BroadcastReceiver {
325 @Override
326 public void onReceive(Context context, Intent intent) {
327 /**
328 * Skip immediately if intent is not relevant to device shutdown.
329 */
330 if (!intent.getAction().equals(Intent.ACTION_REBOOT)
331 && !intent.getAction().equals(Intent.ACTION_SHUTDOWN)) {
332 return;
333 }
334 Slog.i(TAG, "StatsCompanionService noticed a shutdown.");
335 synchronized (sStatsdLock) {
336 if (sStatsd == null) {
337 Slog.w(TAG, "Could not access statsd to inform it of a shutdown event.");
338 return;
339 }
340 try {
341 sStatsd.writeDataToDisk();
342 } catch (Exception e) {
343 Slog.w(TAG, "Failed to inform statsd of a shutdown event.", e);
344 }
345 }
346 }
347 }
348
Bookatz94726412017-08-31 09:26:15 -0700349 @Override // Binder call
350 public void setAnomalyAlarm(long timestampMs) {
351 enforceCallingPermission();
352 if (DEBUG) Slog.d(TAG, "Setting anomaly alarm for " + timestampMs);
353 final long callingToken = Binder.clearCallingIdentity();
354 try {
355 // using RTC, not RTC_WAKEUP, so if device is asleep, will only fire when it awakens.
356 // This alarm is inexact, leaving its exactness completely up to the OS optimizations.
357 // AlarmManager will automatically cancel any previous mAnomalyAlarmIntent alarm.
358 mAlarmManager.set(AlarmManager.RTC, timestampMs, mAnomalyAlarmIntent);
359 } finally {
360 Binder.restoreCallingIdentity(callingToken);
361 }
362 }
363
364 @Override // Binder call
365 public void cancelAnomalyAlarm() {
366 enforceCallingPermission();
367 if (DEBUG) Slog.d(TAG, "Cancelling anomaly alarm");
368 final long callingToken = Binder.clearCallingIdentity();
369 try {
370 mAlarmManager.cancel(mAnomalyAlarmIntent);
371 } finally {
372 Binder.restoreCallingIdentity(callingToken);
373 }
374 }
375
376 @Override // Binder call
Chenjie Yub3dda412017-10-24 13:41:59 -0700377 public void setPullingAlarms(long timestampMs, long intervalMs) {
378 enforceCallingPermission();
379 if (DEBUG)
380 Slog.d(TAG, "Setting pulling alarm for " + timestampMs + " every " + intervalMs + "ms");
381 final long callingToken = Binder.clearCallingIdentity();
382 try {
383 // using RTC, not RTC_WAKEUP, so if device is asleep, will only fire when it awakens.
384 // This alarm is inexact, leaving its exactness completely up to the OS optimizations.
385 // TODO: totally inexact means that stats per bucket could be quite off. Is this okay?
386 mAlarmManager.setRepeating(AlarmManager.RTC, timestampMs, intervalMs, mPullingAlarmIntent);
387 } finally {
388 Binder.restoreCallingIdentity(callingToken);
389 }
Bookatz94726412017-08-31 09:26:15 -0700390 }
391
392 @Override // Binder call
Chenjie Yub3dda412017-10-24 13:41:59 -0700393 public void cancelPullingAlarms() {
394 enforceCallingPermission();
395 if (DEBUG)
396 Slog.d(TAG, "Cancelling pulling alarm");
397 final long callingToken = Binder.clearCallingIdentity();
398 try {
399 mAlarmManager.cancel(mPullingAlarmIntent);
400 } finally {
401 Binder.restoreCallingIdentity(callingToken);
402 }
Bookatz94726412017-08-31 09:26:15 -0700403 }
404
David Chenc8a43242017-10-17 16:23:28 -0700405 private StatsLogEventWrapper[] addNetworkStats(int tag, NetworkStats stats, boolean withFGBG) {
406 List<StatsLogEventWrapper> ret = new ArrayList<>();
407 int size = stats.size();
408 NetworkStats.Entry entry = new NetworkStats.Entry(); // For recycling
409 for (int j = 0; j < size; j++) {
410 stats.getValues(j, entry);
411 StatsLogEventWrapper e = new StatsLogEventWrapper(tag, withFGBG ? 6 : 5);
412 e.writeInt(entry.uid);
413 if (withFGBG) {
414 e.writeInt(entry.set);
415 }
416 e.writeLong(entry.rxBytes);
417 e.writeLong(entry.rxPackets);
418 e.writeLong(entry.txBytes);
419 e.writeLong(entry.txPackets);
420 ret.add(e);
421 }
422 return ret.toArray(new StatsLogEventWrapper[ret.size()]);
423 }
424
425 /**
426 * Allows rollups per UID but keeping the set (foreground/background) slicing.
427 * Adapted from groupedByUid in frameworks/base/core/java/android/net/NetworkStats.java
428 */
429 private NetworkStats rollupNetworkStatsByFGBG(NetworkStats stats) {
430 final NetworkStats ret = new NetworkStats(stats.getElapsedRealtime(), 1);
431
432 final NetworkStats.Entry entry = new NetworkStats.Entry();
433 entry.iface = NetworkStats.IFACE_ALL;
434 entry.tag = NetworkStats.TAG_NONE;
435 entry.metered = NetworkStats.METERED_ALL;
436 entry.roaming = NetworkStats.ROAMING_ALL;
437
438 int size = stats.size();
439 NetworkStats.Entry recycle = new NetworkStats.Entry(); // Used for retrieving values
440 for (int i = 0; i < size; i++) {
441 stats.getValues(i, recycle);
442
443 // Skip specific tags, since already counted in TAG_NONE
444 if (recycle.tag != NetworkStats.TAG_NONE) continue;
445
446 entry.set = recycle.set; // Allows slicing by background/foreground
447 entry.uid = recycle.uid;
448 entry.rxBytes = recycle.rxBytes;
449 entry.rxPackets = recycle.rxPackets;
450 entry.txBytes = recycle.txBytes;
451 entry.txPackets = recycle.txPackets;
452 // Operations purposefully omitted since we don't use them for statsd.
453 ret.combineValues(entry);
454 }
455 return ret;
456 }
457
Chenjie Yu05013b32017-11-21 10:21:41 -0800458 /**
459 * Helper method to extract the Parcelable controller info from a
460 * SynchronousResultReceiver.
461 */
462 private static <T extends Parcelable> T awaitControllerInfo(
463 @Nullable SynchronousResultReceiver receiver) {
464 if (receiver == null) {
465 return null;
466 }
467
468 try {
469 final SynchronousResultReceiver.Result result =
470 receiver.awaitResult(EXTERNAL_STATS_SYNC_TIMEOUT_MILLIS);
471 if (result.bundle != null) {
472 // This is the final destination for the Bundle.
473 result.bundle.setDefusable(true);
474
475 final T data = result.bundle.getParcelable(
476 RESULT_RECEIVER_CONTROLLER_KEY);
477 if (data != null) {
478 return data;
479 }
480 }
481 Slog.e(TAG, "no controller energy info supplied for " + receiver.getName());
482 } catch (TimeoutException e) {
483 Slog.w(TAG, "timeout reading " + receiver.getName() + " stats");
484 }
485 return null;
486 }
487
488 /**
489 *
490 * Pulls wifi controller activity energy info from WiFiManager
491 */
Bookatzc68a9d22017-09-27 14:09:55 -0700492 @Override // Binder call
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700493 public StatsLogEventWrapper[] pullData(int tagId) {
Bookatzc68a9d22017-09-27 14:09:55 -0700494 enforceCallingPermission();
David Chenc8a43242017-10-17 16:23:28 -0700495 if (DEBUG)
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700496 Slog.d(TAG, "Pulling " + tagId);
Bookatzc68a9d22017-09-27 14:09:55 -0700497
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700498 switch (tagId) {
Chenjie Yu31d14d72017-12-12 17:54:33 -0800499 case StatsLog.WIFI_BYTES_TRANSFER: {
David Chenc8a43242017-10-17 16:23:28 -0700500 long token = Binder.clearCallingIdentity();
501 try {
502 // TODO: Consider caching the following call to get BatteryStatsInternal.
503 BatteryStatsInternal bs = LocalServices.getService(BatteryStatsInternal.class);
504 String[] ifaces = bs.getWifiIfaces();
505 if (ifaces.length == 0) {
506 return null;
507 }
508 NetworkStatsFactory nsf = new NetworkStatsFactory();
509 // Combine all the metrics per Uid into one record.
510 NetworkStats stats = nsf.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces,
511 NetworkStats.TAG_NONE, null).groupedByUid();
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700512 return addNetworkStats(tagId, stats, false);
David Chenc8a43242017-10-17 16:23:28 -0700513 } catch (java.io.IOException e) {
514 Slog.e(TAG, "Pulling netstats for wifi bytes has error", e);
515 } finally {
516 Binder.restoreCallingIdentity(token);
517 }
518 break;
519 }
Chenjie Yu31d14d72017-12-12 17:54:33 -0800520 case StatsLog.MOBILE_BYTES_TRANSFER: {
David Chenc8a43242017-10-17 16:23:28 -0700521 long token = Binder.clearCallingIdentity();
522 try {
523 BatteryStatsInternal bs = LocalServices.getService(BatteryStatsInternal.class);
524 String[] ifaces = bs.getMobileIfaces();
525 if (ifaces.length == 0) {
526 return null;
527 }
528 NetworkStatsFactory nsf = new NetworkStatsFactory();
529 // Combine all the metrics per Uid into one record.
530 NetworkStats stats = nsf.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces,
531 NetworkStats.TAG_NONE, null).groupedByUid();
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700532 return addNetworkStats(tagId, stats, false);
David Chenc8a43242017-10-17 16:23:28 -0700533 } catch (java.io.IOException e) {
534 Slog.e(TAG, "Pulling netstats for mobile bytes has error", e);
535 } finally {
536 Binder.restoreCallingIdentity(token);
537 }
538 break;
539 }
Chenjie Yu31d14d72017-12-12 17:54:33 -0800540 case StatsLog.WIFI_BYTES_TRANSFER_BY_FG_BG: {
David Chenc8a43242017-10-17 16:23:28 -0700541 long token = Binder.clearCallingIdentity();
542 try {
543 BatteryStatsInternal bs = LocalServices.getService(BatteryStatsInternal.class);
544 String[] ifaces = bs.getWifiIfaces();
545 if (ifaces.length == 0) {
546 return null;
547 }
548 NetworkStatsFactory nsf = new NetworkStatsFactory();
549 NetworkStats stats = rollupNetworkStatsByFGBG(
550 nsf.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces,
551 NetworkStats.TAG_NONE, null));
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700552 return addNetworkStats(tagId, stats, true);
David Chenc8a43242017-10-17 16:23:28 -0700553 } catch (java.io.IOException e) {
554 Slog.e(TAG, "Pulling netstats for wifi bytes w/ fg/bg has error", e);
555 } finally {
556 Binder.restoreCallingIdentity(token);
557 }
558 break;
559 }
Chenjie Yu31d14d72017-12-12 17:54:33 -0800560 case StatsLog.MOBILE_BYTES_TRANSFER_BY_FG_BG: {
David Chenc8a43242017-10-17 16:23:28 -0700561 long token = Binder.clearCallingIdentity();
562 try {
563 BatteryStatsInternal bs = LocalServices.getService(BatteryStatsInternal.class);
564 String[] ifaces = bs.getMobileIfaces();
565 if (ifaces.length == 0) {
566 return null;
567 }
568 NetworkStatsFactory nsf = new NetworkStatsFactory();
569 NetworkStats stats = rollupNetworkStatsByFGBG(
570 nsf.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces,
571 NetworkStats.TAG_NONE, null));
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700572 return addNetworkStats(tagId, stats, true);
David Chenc8a43242017-10-17 16:23:28 -0700573 } catch (java.io.IOException e) {
574 Slog.e(TAG, "Pulling netstats for mobile bytes w/ fg/bg has error", e);
575 } finally {
576 Binder.restoreCallingIdentity(token);
577 }
578 break;
579 }
Chenjie Yu31d14d72017-12-12 17:54:33 -0800580 case StatsLog.KERNEL_WAKELOCK: {
Bookatzc68a9d22017-09-27 14:09:55 -0700581 final KernelWakelockStats wakelockStats =
582 mKernelWakelockReader.readKernelWakelockStats(mTmpWakelockStats);
David Chenc8a43242017-10-17 16:23:28 -0700583 List<StatsLogEventWrapper> ret = new ArrayList();
Bookatzc68a9d22017-09-27 14:09:55 -0700584 for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) {
585 String name = ent.getKey();
586 KernelWakelockStats.Entry kws = ent.getValue();
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700587 StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 4);
David Chenc8a43242017-10-17 16:23:28 -0700588 e.writeString(name);
David Chen1481fe12017-10-16 13:16:34 -0700589 e.writeInt(kws.mCount);
590 e.writeInt(kws.mVersion);
591 e.writeLong(kws.mTotalTime);
David Chen1481fe12017-10-16 13:16:34 -0700592 ret.add(e);
Bookatzc68a9d22017-09-27 14:09:55 -0700593 }
David Chenc8a43242017-10-17 16:23:28 -0700594 return ret.toArray(new StatsLogEventWrapper[ret.size()]);
David Chen1481fe12017-10-16 13:16:34 -0700595 }
Chenjie Yu31d14d72017-12-12 17:54:33 -0800596 case StatsLog.CPU_TIME_PER_FREQ: {
Chenjie Yu7f8def92017-11-03 09:33:15 -0700597 List<StatsLogEventWrapper> ret = new ArrayList();
598 for (int cluster = 0; cluster < mKernelCpuSpeedReaders.length; cluster++) {
Chenjie Yu1ee9b742018-01-10 16:02:57 -0800599 long[] clusterTimeMs = mKernelCpuSpeedReaders[cluster].readAbsolute();
Chenjie Yu7f8def92017-11-03 09:33:15 -0700600 if (clusterTimeMs != null) {
601 for (int speed = clusterTimeMs.length - 1; speed >= 0; --speed) {
602 StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 3);
Chenjie Yu937d7422018-01-10 16:37:53 -0800603 e.writeInt(cluster);
Chenjie Yu7f8def92017-11-03 09:33:15 -0700604 e.writeInt(speed);
605 e.writeLong(clusterTimeMs[speed]);
606 ret.add(e);
607 }
608 }
609 }
610 return ret.toArray(new StatsLogEventWrapper[ret.size()]);
611 }
Chenjie Yu31d14d72017-12-12 17:54:33 -0800612 case StatsLog.WIFI_ACTIVITY_ENERGY_INFO: {
Chenjie Yu05013b32017-11-21 10:21:41 -0800613 List<StatsLogEventWrapper> ret = new ArrayList();
614 long token = Binder.clearCallingIdentity();
615 if (mWifiManager == null) {
616 mWifiManager = IWifiManager.Stub.asInterface(ServiceManager.getService(
617 Context.WIFI_SERVICE));
618 }
619 if (mWifiManager != null) {
620 try {
621 SynchronousResultReceiver wifiReceiver = new SynchronousResultReceiver("wifi");
622 mWifiManager.requestActivityInfo(wifiReceiver);
623 final WifiActivityEnergyInfo wifiInfo = awaitControllerInfo(wifiReceiver);
624 StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 6);
625 e.writeLong(wifiInfo.getTimeStamp());
626 e.writeInt(wifiInfo.getStackState());
627 e.writeLong(wifiInfo.getControllerTxTimeMillis());
628 e.writeLong(wifiInfo.getControllerRxTimeMillis());
629 e.writeLong(wifiInfo.getControllerIdleTimeMillis());
630 e.writeLong(wifiInfo.getControllerEnergyUsed());
631 ret.add(e);
Chenjie Yu937d7422018-01-10 16:37:53 -0800632 return ret.toArray(new StatsLogEventWrapper[ret.size()]);
Chenjie Yu05013b32017-11-21 10:21:41 -0800633 } catch (RemoteException e) {
634 Slog.e(TAG, "Pulling wifiManager for wifi controller activity energy info has error", e);
635 } finally {
636 Binder.restoreCallingIdentity(token);
637 }
638 }
639 break;
640 }
Chenjie Yu31d14d72017-12-12 17:54:33 -0800641 case StatsLog.MODEM_ACTIVITY_INFO: {
Chenjie Yu05013b32017-11-21 10:21:41 -0800642 List<StatsLogEventWrapper> ret = new ArrayList();
643 long token = Binder.clearCallingIdentity();
644 if (mTelephony == null) {
645 mTelephony = TelephonyManager.from(mContext);
646 }
647 if (mTelephony != null) {
648 SynchronousResultReceiver modemReceiver = new SynchronousResultReceiver("telephony");
649 mTelephony.requestModemActivityInfo(modemReceiver);
650 final ModemActivityInfo modemInfo = awaitControllerInfo(modemReceiver);
651 StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 6);
652 e.writeLong(modemInfo.getTimestamp());
653 e.writeLong(modemInfo.getSleepTimeMillis());
654 e.writeLong(modemInfo.getIdleTimeMillis());
655 e.writeLong(modemInfo.getTxTimeMillis()[0]);
656 e.writeLong(modemInfo.getTxTimeMillis()[1]);
657 e.writeLong(modemInfo.getTxTimeMillis()[2]);
658 e.writeLong(modemInfo.getTxTimeMillis()[3]);
659 e.writeLong(modemInfo.getTxTimeMillis()[4]);
660 e.writeLong(modemInfo.getRxTimeMillis());
661 e.writeLong(modemInfo.getEnergyUsed());
662 ret.add(e);
Chenjie Yu937d7422018-01-10 16:37:53 -0800663 return ret.toArray(new StatsLogEventWrapper[ret.size()]);
Chenjie Yu05013b32017-11-21 10:21:41 -0800664 }
665 break;
666 }
Chenjie Yu9da105b2018-01-13 12:41:08 -0800667 case StatsLog.CPU_SUSPEND_TIME: {
668 List<StatsLogEventWrapper> ret = new ArrayList();
669 StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 1);
670 e.writeLong(SystemClock.elapsedRealtime());
671 ret.add(e);
Chenjie Yu937d7422018-01-10 16:37:53 -0800672 return ret.toArray(new StatsLogEventWrapper[ret.size()]);
Chenjie Yu9da105b2018-01-13 12:41:08 -0800673 }
674 case StatsLog.CPU_IDLE_TIME: {
675 List<StatsLogEventWrapper> ret = new ArrayList();
676 StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 1);
677 e.writeLong(SystemClock.uptimeMillis());
678 ret.add(e);
Chenjie Yu937d7422018-01-10 16:37:53 -0800679 return ret.toArray(new StatsLogEventWrapper[ret.size()]);
680 }
681 case StatsLog.DISK_SPACE: {
682 List<StatsLogEventWrapper> ret = new ArrayList();
683 StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 3);
684 e.writeLong(mStatFsData.getAvailableBytes());
685 e.writeLong(mStatFsSystem.getAvailableBytes());
686 e.writeLong(mStatFsTemp.getAvailableBytes());
687 ret.add(e);
688 return ret.toArray(new StatsLogEventWrapper[ret.size()]);
689 }
690 case StatsLog.SYSTEM_UPTIME: {
691 List<StatsLogEventWrapper> ret = new ArrayList();
692 StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 1);
693 e.writeLong(SystemClock.uptimeMillis());
694 ret.add(e);
695 return ret.toArray(new StatsLogEventWrapper[ret.size()]);
Chenjie Yu9da105b2018-01-13 12:41:08 -0800696 }
Bookatzc68a9d22017-09-27 14:09:55 -0700697 default:
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700698 Slog.w(TAG, "No such tagId data as " + tagId);
Bookatzc68a9d22017-09-27 14:09:55 -0700699 return null;
700 }
David Chenc8a43242017-10-17 16:23:28 -0700701 return null;
Bookatzc68a9d22017-09-27 14:09:55 -0700702 }
703
704 @Override // Binder call
Bookatzb487b552017-09-18 11:26:01 -0700705 public void statsdReady() {
706 enforceCallingPermission();
707 if (DEBUG) Slog.d(TAG, "learned that statsdReady");
708 sayHiToStatsd(); // tell statsd that we're ready too and link to it
Bookatz5c800e32018-01-24 14:59:52 -0800709 mContext.sendBroadcast(new Intent(StatsManager.ACTION_STATSD_STARTED),
710 android.Manifest.permission.DUMP);
Bookatzb487b552017-09-18 11:26:01 -0700711 }
712
David Chenc136f452017-11-27 11:52:26 -0800713 @Override
714 public void triggerUidSnapshot() {
715 enforceCallingPermission();
716 synchronized (sStatsdLock) {
717 try {
718 informAllUidsLocked(mContext);
719 } catch (RemoteException e) {
720 Slog.e(TAG, "Failed to trigger uid snapshot.", e);
721 }
722 }
723 }
724
Bookatz94726412017-08-31 09:26:15 -0700725 private void enforceCallingPermission() {
726 if (Binder.getCallingPid() == Process.myPid()) {
727 return;
728 }
729 mContext.enforceCallingPermission(android.Manifest.permission.STATSCOMPANION, null);
730 }
731
Bookatzb487b552017-09-18 11:26:01 -0700732 // Lifecycle and related code
733
David Chen1481fe12017-10-16 13:16:34 -0700734 /**
735 * Fetches the statsd IBinder service
736 */
Bookatzb487b552017-09-18 11:26:01 -0700737 private static IStatsManager fetchStatsdService() {
738 return IStatsManager.Stub.asInterface(ServiceManager.getService("stats"));
739 }
740
741 public static final class Lifecycle extends SystemService {
742 private StatsCompanionService mStatsCompanionService;
743
744 public Lifecycle(Context context) {
745 super(context);
746 }
747
748 @Override
749 public void onStart() {
750 mStatsCompanionService = new StatsCompanionService(getContext());
751 try {
752 publishBinderService(Context.STATS_COMPANION_SERVICE, mStatsCompanionService);
753 if (DEBUG) Slog.d(TAG, "Published " + Context.STATS_COMPANION_SERVICE);
754 } catch (Exception e) {
755 Slog.e(TAG, "Failed to publishBinderService", e);
756 }
757 }
758
759 @Override
760 public void onBootPhase(int phase) {
761 super.onBootPhase(phase);
762 if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
763 mStatsCompanionService.systemReady();
764 }
765 }
766 }
767
David Chen1481fe12017-10-16 13:16:34 -0700768 /**
769 * Now that the android system is ready, StatsCompanion is ready too, so inform statsd.
770 */
Bookatzb487b552017-09-18 11:26:01 -0700771 private void systemReady() {
772 if (DEBUG) Slog.d(TAG, "Learned that systemReady");
773 sayHiToStatsd();
774 }
775
David Chen1481fe12017-10-16 13:16:34 -0700776 /**
777 * Tells statsd that statscompanion is ready. If the binder call returns, link to statsd.
778 */
Bookatzb487b552017-09-18 11:26:01 -0700779 private void sayHiToStatsd() {
780 synchronized (sStatsdLock) {
781 if (sStatsd != null) {
782 Slog.e(TAG, "Trying to fetch statsd, but it was already fetched",
783 new IllegalStateException("sStatsd is not null when being fetched"));
784 return;
785 }
786 sStatsd = fetchStatsdService();
787 if (sStatsd == null) {
788 Slog.w(TAG, "Could not access statsd");
789 return;
790 }
791 if (DEBUG) Slog.d(TAG, "Saying hi to statsd");
792 try {
793 sStatsd.statsCompanionReady();
794 // If the statsCompanionReady two-way binder call returns, link to statsd.
795 try {
796 sStatsd.asBinder().linkToDeath(new StatsdDeathRecipient(), 0);
797 } catch (RemoteException e) {
798 Slog.e(TAG, "linkToDeath(StatsdDeathRecipient) failed", e);
799 forgetEverything();
800 }
yro947fbce2017-11-15 22:50:23 -0800801 // Setup broadcast receiver for updates.
David Chende701692017-10-05 13:16:02 -0700802 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_REPLACED);
803 filter.addAction(Intent.ACTION_PACKAGE_ADDED);
804 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
805 filter.addDataScheme("package");
806 mContext.registerReceiverAsUser(mAppUpdateReceiver, UserHandle.ALL, filter, null,
David Chen1481fe12017-10-16 13:16:34 -0700807 null);
David Chen47e8f4d2017-10-11 15:34:13 -0700808
809 // Setup receiver for user initialize (which happens once for a new user) and
810 // if a user is removed.
811 filter = new IntentFilter(Intent.ACTION_USER_INITIALIZE);
812 filter.addAction(Intent.ACTION_USER_REMOVED);
813 mContext.registerReceiverAsUser(mUserUpdateReceiver, UserHandle.ALL,
David Chen1481fe12017-10-16 13:16:34 -0700814 filter, null, null);
David Chen47e8f4d2017-10-11 15:34:13 -0700815
yro947fbce2017-11-15 22:50:23 -0800816 // Setup receiver for device reboots or shutdowns.
817 filter = new IntentFilter(Intent.ACTION_REBOOT);
818 filter.addAction(Intent.ACTION_SHUTDOWN);
819 mContext.registerReceiverAsUser(
820 mShutdownEventReceiver, UserHandle.ALL, filter, null, null);
Yao Chen0f217102018-01-09 10:33:15 -0800821 final long token = Binder.clearCallingIdentity();
822 try {
823 // Pull the latest state of UID->app name, version mapping when statsd starts.
824 informAllUidsLocked(mContext);
825 } finally {
826 restoreCallingIdentity(token);
827 }
Bookatzb487b552017-09-18 11:26:01 -0700828 } catch (RemoteException e) {
829 Slog.e(TAG, "Failed to inform statsd that statscompanion is ready", e);
830 forgetEverything();
831 }
832 }
833 }
834
835 private class StatsdDeathRecipient implements IBinder.DeathRecipient {
836 @Override
837 public void binderDied() {
838 Slog.i(TAG, "Statsd is dead - erase all my knowledge.");
839 forgetEverything();
840 }
841 }
842
843 private void forgetEverything() {
844 synchronized (sStatsdLock) {
845 sStatsd = null;
David Chende701692017-10-05 13:16:02 -0700846 mContext.unregisterReceiver(mAppUpdateReceiver);
David Chen47e8f4d2017-10-11 15:34:13 -0700847 mContext.unregisterReceiver(mUserUpdateReceiver);
yro947fbce2017-11-15 22:50:23 -0800848 mContext.unregisterReceiver(mShutdownEventReceiver);
Bookatzb487b552017-09-18 11:26:01 -0700849 cancelAnomalyAlarm();
Chenjie Yub3dda412017-10-24 13:41:59 -0700850 cancelPullingAlarms();
Bookatzb487b552017-09-18 11:26:01 -0700851 }
852 }
853
Bookatz94726412017-08-31 09:26:15 -0700854}