Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.server.stats; |
| 17 | |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 18 | import android.annotation.Nullable; |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 19 | import android.app.AlarmManager; |
| 20 | import android.app.PendingIntent; |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 21 | import android.app.StatsManager; |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 22 | import android.content.BroadcastReceiver; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 25 | import android.content.IntentFilter; |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 26 | import android.content.IntentSender; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 27 | import android.content.pm.PackageInfo; |
| 28 | import android.content.pm.PackageManager; |
| 29 | import android.content.pm.UserInfo; |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 30 | import android.net.NetworkStats; |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 31 | import android.net.wifi.IWifiManager; |
| 32 | import android.net.wifi.WifiActivityEnergyInfo; |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 33 | import android.os.StatsDimensionsValue; |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 34 | import android.os.BatteryStatsInternal; |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 35 | import android.os.Binder; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 36 | import android.os.Bundle; |
Chenjie Yu | 937d742 | 2018-01-10 16:37:53 -0800 | [diff] [blame] | 37 | import android.os.Environment; |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 38 | import android.os.IBinder; |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 39 | import android.os.IStatsCompanionService; |
| 40 | import android.os.IStatsManager; |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 41 | import android.os.Parcelable; |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 42 | import android.os.Process; |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 43 | import android.os.RemoteException; |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 44 | import android.os.ServiceManager; |
Chenjie Yu | 937d742 | 2018-01-10 16:37:53 -0800 | [diff] [blame] | 45 | import android.os.StatFs; |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 46 | import android.os.StatsLogEventWrapper; |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 47 | import android.os.SynchronousResultReceiver; |
Chenjie Yu | 937d742 | 2018-01-10 16:37:53 -0800 | [diff] [blame] | 48 | import android.os.SystemClock; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 49 | import android.os.UserHandle; |
| 50 | import android.os.UserManager; |
Chenjie Yu | 937d742 | 2018-01-10 16:37:53 -0800 | [diff] [blame] | 51 | import android.telephony.ModemActivityInfo; |
| 52 | import android.telephony.TelephonyManager; |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 53 | import android.util.Slog; |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 54 | import android.util.StatsLog; |
| 55 | |
| 56 | import com.android.internal.annotations.GuardedBy; |
| 57 | import com.android.internal.net.NetworkStatsFactory; |
Chenjie Yu | 937d742 | 2018-01-10 16:37:53 -0800 | [diff] [blame] | 58 | import com.android.internal.os.KernelCpuSpeedReader; |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 59 | import com.android.internal.os.KernelWakelockReader; |
| 60 | import com.android.internal.os.KernelWakelockStats; |
Chenjie Yu | 7f8def9 | 2017-11-03 09:33:15 -0700 | [diff] [blame] | 61 | import com.android.internal.os.PowerProfile; |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 62 | import com.android.server.LocalServices; |
| 63 | import com.android.server.SystemService; |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 64 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 65 | import java.util.ArrayList; |
| 66 | import java.util.List; |
Bookatz | c68a9d2 | 2017-09-27 14:09:55 -0700 | [diff] [blame] | 67 | import java.util.Map; |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 68 | import java.util.concurrent.TimeoutException; |
Bookatz | c68a9d2 | 2017-09-27 14:09:55 -0700 | [diff] [blame] | 69 | |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 70 | /** |
| 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 Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 73 | * |
Bookatz | 6bc51d7 | 2017-09-28 16:43:40 -0700 | [diff] [blame] | 74 | * @hide |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 75 | */ |
| 76 | public class StatsCompanionService extends IStatsCompanionService.Stub { |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 77 | /** |
| 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 | |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 84 | static final String TAG = "StatsCompanionService"; |
| 85 | static final boolean DEBUG = true; |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 86 | |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 87 | public static final String ACTION_TRIGGER_COLLECTION = |
| 88 | "com.android.server.stats.action.TRIGGER_COLLECTION"; |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 89 | |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 90 | public static final int CODE_SUBSCRIBER_BROADCAST = 1; |
| 91 | |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 92 | private final Context mContext; |
| 93 | private final AlarmManager mAlarmManager; |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 94 | @GuardedBy("sStatsdLock") |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 95 | private static IStatsManager sStatsd; |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 96 | private static final Object sStatsdLock = new Object(); |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 97 | |
| 98 | private final PendingIntent mAnomalyAlarmIntent; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 99 | private final PendingIntent mPullingAlarmIntent; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 100 | private final BroadcastReceiver mAppUpdateReceiver; |
David Chen | 47e8f4d | 2017-10-11 15:34:13 -0700 | [diff] [blame] | 101 | private final BroadcastReceiver mUserUpdateReceiver; |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 102 | private final ShutdownEventReceiver mShutdownEventReceiver; |
Chenjie Yu | 7f8def9 | 2017-11-03 09:33:15 -0700 | [diff] [blame] | 103 | private final KernelWakelockReader mKernelWakelockReader = new KernelWakelockReader(); |
| 104 | private final KernelWakelockStats mTmpWakelockStats = new KernelWakelockStats(); |
| 105 | private final KernelCpuSpeedReader[] mKernelCpuSpeedReaders; |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 106 | private IWifiManager mWifiManager = null; |
| 107 | private TelephonyManager mTelephony = null; |
Chenjie Yu | 937d742 | 2018-01-10 16:37:53 -0800 | [diff] [blame] | 108 | 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()); |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 113 | |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 114 | 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 Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 121 | mPullingAlarmIntent = PendingIntent.getBroadcast( |
| 122 | mContext, 0, new Intent(mContext, PullingAlarmReceiver.class), 0); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 123 | mAppUpdateReceiver = new AppUpdateReceiver(); |
David Chen | 47e8f4d | 2017-10-11 15:34:13 -0700 | [diff] [blame] | 124 | 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 Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 138 | Slog.e(TAG, "Failed to inform statsd latest update of all apps", e); |
David Chen | 47e8f4d | 2017-10-11 15:34:13 -0700 | [diff] [blame] | 139 | forgetEverything(); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | }; |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 144 | mShutdownEventReceiver = new ShutdownEventReceiver(); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 145 | Slog.w(TAG, "Registered receiver for ACTION_PACKAGE_REPLACE AND ADDED."); |
Chenjie Yu | 7f8def9 | 2017-11-03 09:33:15 -0700 | [diff] [blame] | 146 | 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 | } |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 156 | } |
| 157 | |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 158 | @Override |
| 159 | public void sendBroadcast(String pkg, String cls) { |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 160 | // TODO: Use a pending intent, and enfoceCallingPermission. |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 161 | mContext.sendBroadcastAsUser(new Intent(ACTION_TRIGGER_COLLECTION).setClassName(pkg, cls), |
| 162 | UserHandle.SYSTEM); |
| 163 | } |
| 164 | |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 165 | @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 Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 191 | private final static int[] toIntArray(List<Integer> list) { |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 192 | int[] ret = new int[list.size()]; |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 193 | for (int i = 0; i < ret.length; i++) { |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 194 | ret[i] = list.get(i); |
| 195 | } |
| 196 | return ret; |
| 197 | } |
| 198 | |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 199 | 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 Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 207 | // 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 Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 213 | Slog.w(TAG, "Iterating over " + users.size() + " profiles."); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | List<Integer> uids = new ArrayList(); |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 217 | List<Long> versions = new ArrayList(); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 218 | List<String> apps = new ArrayList(); |
| 219 | |
| 220 | // Add in all the apps for every user/profile. |
| 221 | for (UserInfo profile : users) { |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 222 | 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 Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 226 | versions.add(pi.get(j).getLongVersionCode()); |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 227 | apps.add(pi.get(j).packageName); |
| 228 | } |
| 229 | } |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 230 | } |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 231 | sStatsd.informAllUidData(toIntArray(uids), toLongArray(versions), apps.toArray(new |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 232 | String[apps.size()])); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 233 | if (DEBUG) { |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 234 | Slog.w(TAG, "Sent data for " + uids.size() + " apps"); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 238 | public final static class AppUpdateReceiver extends BroadcastReceiver { |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 239 | @Override |
| 240 | public void onReceive(Context context, Intent intent) { |
David Chen | 47e8f4d | 2017-10-11 15:34:13 -0700 | [diff] [blame] | 241 | /** |
| 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 Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 244 | * If we can't find the value for EXTRA_REPLACING, we default to false. |
David Chen | 47e8f4d | 2017-10-11 15:34:13 -0700 | [diff] [blame] | 245 | */ |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 246 | if (!intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED) |
| 247 | && intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
David Chen | 47e8f4d | 2017-10-11 15:34:13 -0700 | [diff] [blame] | 248 | return; // Keep only replacing or normal add and remove. |
| 249 | } |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 250 | Slog.i(TAG, "StatsCompanionService noticed an app was updated."); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 251 | synchronized (sStatsdLock) { |
| 252 | if (sStatsd == null) { |
yro | f6d1ca5 | 2017-11-15 15:38:34 -0800 | [diff] [blame] | 253 | Slog.w(TAG, "Could not access statsd to inform it of an app update"); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 254 | 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 Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 274 | sStatsd.informOnePackage(app, uid, pi.getLongVersionCode()); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 275 | } |
| 276 | } catch (Exception e) { |
| 277 | Slog.w(TAG, "Failed to inform statsd of an app update", e); |
| 278 | } |
| 279 | } |
| 280 | } |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 281 | } |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 282 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 283 | public final static class AnomalyAlarmReceiver extends BroadcastReceiver { |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 284 | @Override |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 285 | 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 | } |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 298 | } |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 299 | // AlarmManager releases its own wakelock here. |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 300 | } |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 301 | } |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 302 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 303 | 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) { |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 310 | Slog.w(TAG, "Could not access statsd to inform it of pulling alarm firing."); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 311 | return; |
| 312 | } |
| 313 | try { |
| 314 | // Two-way call to statsd to retain AlarmManager wakelock |
| 315 | sStatsd.informPollAlarmFired(); |
| 316 | } catch (RemoteException e) { |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 317 | Slog.w(TAG, "Failed to inform statsd of pulling alarm firing.", e); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 318 | } |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 319 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 320 | // AlarmManager releases its own wakelock here. |
| 321 | } |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 322 | } |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 323 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 324 | 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 | |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 349 | @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 Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 377 | 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 | } |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | @Override // Binder call |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 393 | 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 | } |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 403 | } |
| 404 | |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 405 | 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 Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 458 | /** |
| 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 | */ |
Bookatz | c68a9d2 | 2017-09-27 14:09:55 -0700 | [diff] [blame] | 492 | @Override // Binder call |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 493 | public StatsLogEventWrapper[] pullData(int tagId) { |
Bookatz | c68a9d2 | 2017-09-27 14:09:55 -0700 | [diff] [blame] | 494 | enforceCallingPermission(); |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 495 | if (DEBUG) |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 496 | Slog.d(TAG, "Pulling " + tagId); |
Bookatz | c68a9d2 | 2017-09-27 14:09:55 -0700 | [diff] [blame] | 497 | |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 498 | switch (tagId) { |
Chenjie Yu | 31d14d7 | 2017-12-12 17:54:33 -0800 | [diff] [blame] | 499 | case StatsLog.WIFI_BYTES_TRANSFER: { |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 500 | 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 Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 512 | return addNetworkStats(tagId, stats, false); |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 513 | } 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 Yu | 31d14d7 | 2017-12-12 17:54:33 -0800 | [diff] [blame] | 520 | case StatsLog.MOBILE_BYTES_TRANSFER: { |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 521 | 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 Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 532 | return addNetworkStats(tagId, stats, false); |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 533 | } 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 Yu | 31d14d7 | 2017-12-12 17:54:33 -0800 | [diff] [blame] | 540 | case StatsLog.WIFI_BYTES_TRANSFER_BY_FG_BG: { |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 541 | 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 Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 552 | return addNetworkStats(tagId, stats, true); |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 553 | } 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 Yu | 31d14d7 | 2017-12-12 17:54:33 -0800 | [diff] [blame] | 560 | case StatsLog.MOBILE_BYTES_TRANSFER_BY_FG_BG: { |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 561 | 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 Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 572 | return addNetworkStats(tagId, stats, true); |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 573 | } 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 Yu | 31d14d7 | 2017-12-12 17:54:33 -0800 | [diff] [blame] | 580 | case StatsLog.KERNEL_WAKELOCK: { |
Bookatz | c68a9d2 | 2017-09-27 14:09:55 -0700 | [diff] [blame] | 581 | final KernelWakelockStats wakelockStats = |
| 582 | mKernelWakelockReader.readKernelWakelockStats(mTmpWakelockStats); |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 583 | List<StatsLogEventWrapper> ret = new ArrayList(); |
Bookatz | c68a9d2 | 2017-09-27 14:09:55 -0700 | [diff] [blame] | 584 | for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) { |
| 585 | String name = ent.getKey(); |
| 586 | KernelWakelockStats.Entry kws = ent.getValue(); |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 587 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 4); |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 588 | e.writeString(name); |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 589 | e.writeInt(kws.mCount); |
| 590 | e.writeInt(kws.mVersion); |
| 591 | e.writeLong(kws.mTotalTime); |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 592 | ret.add(e); |
Bookatz | c68a9d2 | 2017-09-27 14:09:55 -0700 | [diff] [blame] | 593 | } |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 594 | return ret.toArray(new StatsLogEventWrapper[ret.size()]); |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 595 | } |
Chenjie Yu | 31d14d7 | 2017-12-12 17:54:33 -0800 | [diff] [blame] | 596 | case StatsLog.CPU_TIME_PER_FREQ: { |
Chenjie Yu | 7f8def9 | 2017-11-03 09:33:15 -0700 | [diff] [blame] | 597 | List<StatsLogEventWrapper> ret = new ArrayList(); |
| 598 | for (int cluster = 0; cluster < mKernelCpuSpeedReaders.length; cluster++) { |
Chenjie Yu | 1ee9b74 | 2018-01-10 16:02:57 -0800 | [diff] [blame] | 599 | long[] clusterTimeMs = mKernelCpuSpeedReaders[cluster].readAbsolute(); |
Chenjie Yu | 7f8def9 | 2017-11-03 09:33:15 -0700 | [diff] [blame] | 600 | if (clusterTimeMs != null) { |
| 601 | for (int speed = clusterTimeMs.length - 1; speed >= 0; --speed) { |
| 602 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 3); |
Chenjie Yu | 937d742 | 2018-01-10 16:37:53 -0800 | [diff] [blame] | 603 | e.writeInt(cluster); |
Chenjie Yu | 7f8def9 | 2017-11-03 09:33:15 -0700 | [diff] [blame] | 604 | 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 Yu | 31d14d7 | 2017-12-12 17:54:33 -0800 | [diff] [blame] | 612 | case StatsLog.WIFI_ACTIVITY_ENERGY_INFO: { |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 613 | 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 Yu | 937d742 | 2018-01-10 16:37:53 -0800 | [diff] [blame] | 632 | return ret.toArray(new StatsLogEventWrapper[ret.size()]); |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 633 | } 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 Yu | 31d14d7 | 2017-12-12 17:54:33 -0800 | [diff] [blame] | 641 | case StatsLog.MODEM_ACTIVITY_INFO: { |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 642 | 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 Yu | 937d742 | 2018-01-10 16:37:53 -0800 | [diff] [blame] | 663 | return ret.toArray(new StatsLogEventWrapper[ret.size()]); |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 664 | } |
| 665 | break; |
| 666 | } |
Chenjie Yu | 9da105b | 2018-01-13 12:41:08 -0800 | [diff] [blame] | 667 | 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 Yu | 937d742 | 2018-01-10 16:37:53 -0800 | [diff] [blame] | 672 | return ret.toArray(new StatsLogEventWrapper[ret.size()]); |
Chenjie Yu | 9da105b | 2018-01-13 12:41:08 -0800 | [diff] [blame] | 673 | } |
| 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 Yu | 937d742 | 2018-01-10 16:37:53 -0800 | [diff] [blame] | 679 | 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 Yu | 9da105b | 2018-01-13 12:41:08 -0800 | [diff] [blame] | 696 | } |
Bookatz | c68a9d2 | 2017-09-27 14:09:55 -0700 | [diff] [blame] | 697 | default: |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 698 | Slog.w(TAG, "No such tagId data as " + tagId); |
Bookatz | c68a9d2 | 2017-09-27 14:09:55 -0700 | [diff] [blame] | 699 | return null; |
| 700 | } |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 701 | return null; |
Bookatz | c68a9d2 | 2017-09-27 14:09:55 -0700 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | @Override // Binder call |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 705 | 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 |
Bookatz | 5c800e3 | 2018-01-24 14:59:52 -0800 | [diff] [blame] | 709 | mContext.sendBroadcast(new Intent(StatsManager.ACTION_STATSD_STARTED), |
| 710 | android.Manifest.permission.DUMP); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 711 | } |
| 712 | |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 713 | @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 | |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 725 | private void enforceCallingPermission() { |
| 726 | if (Binder.getCallingPid() == Process.myPid()) { |
| 727 | return; |
| 728 | } |
| 729 | mContext.enforceCallingPermission(android.Manifest.permission.STATSCOMPANION, null); |
| 730 | } |
| 731 | |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 732 | // Lifecycle and related code |
| 733 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 734 | /** |
| 735 | * Fetches the statsd IBinder service |
| 736 | */ |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 737 | 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 Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 768 | /** |
| 769 | * Now that the android system is ready, StatsCompanion is ready too, so inform statsd. |
| 770 | */ |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 771 | private void systemReady() { |
| 772 | if (DEBUG) Slog.d(TAG, "Learned that systemReady"); |
| 773 | sayHiToStatsd(); |
| 774 | } |
| 775 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 776 | /** |
| 777 | * Tells statsd that statscompanion is ready. If the binder call returns, link to statsd. |
| 778 | */ |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 779 | 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 | } |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 801 | // Setup broadcast receiver for updates. |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 802 | 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 Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 807 | null); |
David Chen | 47e8f4d | 2017-10-11 15:34:13 -0700 | [diff] [blame] | 808 | |
| 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 Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 814 | filter, null, null); |
David Chen | 47e8f4d | 2017-10-11 15:34:13 -0700 | [diff] [blame] | 815 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 816 | // 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 Chen | 0f21710 | 2018-01-09 10:33:15 -0800 | [diff] [blame] | 821 | 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 | } |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 828 | } 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 Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 846 | mContext.unregisterReceiver(mAppUpdateReceiver); |
David Chen | 47e8f4d | 2017-10-11 15:34:13 -0700 | [diff] [blame] | 847 | mContext.unregisterReceiver(mUserUpdateReceiver); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 848 | mContext.unregisterReceiver(mShutdownEventReceiver); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 849 | cancelAnomalyAlarm(); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 850 | cancelPullingAlarms(); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 854 | } |