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 | |
Philip P. Moltmann | f21a350 | 2019-01-30 09:53:04 -0800 | [diff] [blame] | 18 | import static android.content.pm.PackageInfo.REQUESTED_PERMISSION_GRANTED; |
| 19 | import static android.content.pm.PermissionInfo.PROTECTION_DANGEROUS; |
Rafal Slawik | c376262 | 2018-11-07 12:07:26 +0000 | [diff] [blame] | 20 | import static android.os.Process.getPidsForCommands; |
| 21 | import static android.os.Process.getUidForPid; |
| 22 | |
Chenjie Yu | ab53020 | 2018-09-26 12:39:20 -0700 | [diff] [blame] | 23 | import static com.android.internal.util.Preconditions.checkNotNull; |
Rafal Slawik | c376262 | 2018-11-07 12:07:26 +0000 | [diff] [blame] | 24 | import static com.android.server.am.MemoryStatUtil.readCmdlineFromProcfs; |
| 25 | import static com.android.server.am.MemoryStatUtil.readMemoryStatFromProcfs; |
Rafal Slawik | 3bea895 | 2018-11-15 12:39:33 +0000 | [diff] [blame] | 26 | import static com.android.server.am.MemoryStatUtil.readRssHighWaterMarkFromProcfs; |
Rafal Slawik | d4e8757 | 2019-03-12 13:08:38 +0000 | [diff] [blame] | 27 | import static com.android.server.am.MemoryStatUtil.readSystemIonHeapSizeFromDebugfs; |
Chenjie Yu | ab53020 | 2018-09-26 12:39:20 -0700 | [diff] [blame] | 28 | |
Philip P. Moltmann | 70b42ae | 2019-01-29 16:24:19 -0800 | [diff] [blame] | 29 | import android.annotation.NonNull; |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 30 | import android.annotation.Nullable; |
Rajeev Kumar | 22d92b7 | 2018-02-07 18:38:36 -0800 | [diff] [blame] | 31 | import android.app.ActivityManagerInternal; |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 32 | import android.app.AlarmManager; |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 33 | import android.app.AlarmManager.OnAlarmListener; |
Rafal Slawik | 3bea895 | 2018-11-15 12:39:33 +0000 | [diff] [blame] | 34 | import android.app.ProcessMemoryHighWaterMark; |
Rajeev Kumar | 22d92b7 | 2018-02-07 18:38:36 -0800 | [diff] [blame] | 35 | import android.app.ProcessMemoryState; |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 36 | import android.app.StatsManager; |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 37 | import android.bluetooth.BluetoothActivityEnergyInfo; |
| 38 | import android.bluetooth.BluetoothAdapter; |
| 39 | import android.bluetooth.UidTraffic; |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 40 | import android.content.BroadcastReceiver; |
| 41 | import android.content.Context; |
| 42 | import android.content.Intent; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 43 | import android.content.IntentFilter; |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 44 | import android.content.IntentSender; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 45 | import android.content.pm.PackageInfo; |
| 46 | import android.content.pm.PackageManager; |
Philip P. Moltmann | f21a350 | 2019-01-30 09:53:04 -0800 | [diff] [blame] | 47 | import android.content.pm.PermissionInfo; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 48 | import android.content.pm.UserInfo; |
Kevin Chyn | 6ecc570 | 2019-01-28 12:00:37 -0800 | [diff] [blame] | 49 | import android.hardware.biometrics.BiometricsProtoEnums; |
| 50 | import android.hardware.face.FaceManager; |
Tej Singh | d6d6d77 | 2018-09-05 17:41:25 -0700 | [diff] [blame] | 51 | import android.hardware.fingerprint.FingerprintManager; |
Chenjie Yu | ae9dfac | 2018-10-25 16:06:56 -0700 | [diff] [blame] | 52 | import android.net.ConnectivityManager; |
Chenbo Feng | e62e287 | 2019-01-11 15:50:17 -0800 | [diff] [blame] | 53 | import android.net.INetworkStatsService; |
Chenjie Yu | ae9dfac | 2018-10-25 16:06:56 -0700 | [diff] [blame] | 54 | import android.net.Network; |
| 55 | import android.net.NetworkRequest; |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 56 | import android.net.NetworkStats; |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 57 | import android.net.wifi.IWifiManager; |
| 58 | import android.net.wifi.WifiActivityEnergyInfo; |
Bookatz | 75ee604 | 2018-11-09 12:27:37 -0800 | [diff] [blame] | 59 | import android.os.BatteryStats; |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 60 | import android.os.BatteryStatsInternal; |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 61 | import android.os.Binder; |
Bookatz | 366a443 | 2018-11-20 09:42:33 -0800 | [diff] [blame] | 62 | import android.os.Build; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 63 | import android.os.Bundle; |
Chenjie Yu | 937d742 | 2018-01-10 16:37:53 -0800 | [diff] [blame] | 64 | import android.os.Environment; |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 65 | import android.os.FileUtils; |
Olivier Gaillard | 21fb523 | 2018-10-09 13:19:27 +0100 | [diff] [blame] | 66 | import android.os.Handler; |
| 67 | import android.os.HandlerThread; |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 68 | import android.os.IBinder; |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 69 | import android.os.IStatsCompanionService; |
| 70 | import android.os.IStatsManager; |
Tej Singh | 86dc9db | 2018-09-06 00:39:57 +0000 | [diff] [blame] | 71 | import android.os.IStoraged; |
Yangster-mac | e16189a | 2018-08-26 12:20:16 -0700 | [diff] [blame] | 72 | import android.os.IThermalEventListener; |
| 73 | import android.os.IThermalService; |
Olivier Gaillard | 21fb523 | 2018-10-09 13:19:27 +0100 | [diff] [blame] | 74 | import android.os.Looper; |
Chenjie Yu | 2c0d83d | 2018-09-02 06:37:08 -0700 | [diff] [blame] | 75 | import android.os.ParcelFileDescriptor; |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 76 | import android.os.Parcelable; |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 77 | import android.os.Process; |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 78 | import android.os.RemoteException; |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 79 | import android.os.ServiceManager; |
Chenjie Yu | 937d742 | 2018-01-10 16:37:53 -0800 | [diff] [blame] | 80 | import android.os.StatFs; |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 81 | import android.os.StatsDimensionsValue; |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 82 | import android.os.StatsLogEventWrapper; |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 83 | import android.os.SynchronousResultReceiver; |
Chenjie Yu | 937d742 | 2018-01-10 16:37:53 -0800 | [diff] [blame] | 84 | import android.os.SystemClock; |
Rafal Slawik | 44b8814 | 2018-12-15 16:48:09 +0000 | [diff] [blame] | 85 | import android.os.SystemProperties; |
Yangster-mac | e16189a | 2018-08-26 12:20:16 -0700 | [diff] [blame] | 86 | import android.os.Temperature; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 87 | import android.os.UserHandle; |
| 88 | import android.os.UserManager; |
Carmen Agimof | 35f7f6f | 2019-02-05 18:19:21 +0000 | [diff] [blame] | 89 | import android.os.storage.DiskInfo; |
Tej Singh | 86dc9db | 2018-09-06 00:39:57 +0000 | [diff] [blame] | 90 | import android.os.storage.StorageManager; |
Carmen Agimof | 35f7f6f | 2019-02-05 18:19:21 +0000 | [diff] [blame] | 91 | import android.os.storage.VolumeInfo; |
Chenjie Yu | 937d742 | 2018-01-10 16:37:53 -0800 | [diff] [blame] | 92 | import android.telephony.ModemActivityInfo; |
| 93 | import android.telephony.TelephonyManager; |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 94 | import android.util.ArrayMap; |
Philip P. Moltmann | 70b42ae | 2019-01-29 16:24:19 -0800 | [diff] [blame] | 95 | import android.util.ArraySet; |
Chenjie Yu | 2c0d83d | 2018-09-02 06:37:08 -0700 | [diff] [blame] | 96 | import android.util.Log; |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 97 | import android.util.Slog; |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 98 | import android.util.StatsLog; |
Chenjie Yu | ab53020 | 2018-09-26 12:39:20 -0700 | [diff] [blame] | 99 | import android.util.proto.ProtoOutputStream; |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 100 | |
| 101 | import com.android.internal.annotations.GuardedBy; |
Chenjie Yu | 2c0d83d | 2018-09-02 06:37:08 -0700 | [diff] [blame] | 102 | import com.android.internal.app.procstats.IProcessStats; |
| 103 | import com.android.internal.app.procstats.ProcessStats; |
Bookatz | 75ee604 | 2018-11-09 12:27:37 -0800 | [diff] [blame] | 104 | import com.android.internal.os.BatterySipper; |
| 105 | import com.android.internal.os.BatteryStatsHelper; |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 106 | import com.android.internal.os.BinderCallsStats.ExportedCallStat; |
Chenjie Yu | 937d742 | 2018-01-10 16:37:53 -0800 | [diff] [blame] | 107 | import com.android.internal.os.KernelCpuSpeedReader; |
Misha Wagner | 5a51e00 | 2018-10-03 15:04:09 +0100 | [diff] [blame] | 108 | import com.android.internal.os.KernelCpuThreadReader; |
Misha Wagner | 4b32c9f | 2019-01-25 15:30:14 +0000 | [diff] [blame] | 109 | import com.android.internal.os.KernelCpuThreadReaderSettingsObserver; |
Mike Ma | 7ab7fcd | 2019-01-16 15:00:40 -0800 | [diff] [blame] | 110 | import com.android.internal.os.KernelCpuUidTimeReader.KernelCpuUidActiveTimeReader; |
| 111 | import com.android.internal.os.KernelCpuUidTimeReader.KernelCpuUidClusterTimeReader; |
| 112 | import com.android.internal.os.KernelCpuUidTimeReader.KernelCpuUidFreqTimeReader; |
| 113 | import com.android.internal.os.KernelCpuUidTimeReader.KernelCpuUidUserSysTimeReader; |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 114 | import com.android.internal.os.KernelWakelockReader; |
| 115 | import com.android.internal.os.KernelWakelockStats; |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 116 | import com.android.internal.os.LooperStats; |
Chenjie Yu | 7f8def9 | 2017-11-03 09:33:15 -0700 | [diff] [blame] | 117 | import com.android.internal.os.PowerProfile; |
Yangster-mac | 308ea0c | 2018-10-22 13:10:25 -0700 | [diff] [blame] | 118 | import com.android.internal.os.ProcessCpuTracker; |
Tej Singh | e7726dc | 2018-09-21 11:42:12 -0700 | [diff] [blame] | 119 | import com.android.internal.os.StoragedUidIoStatsReader; |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 120 | import com.android.internal.util.DumpUtils; |
Olivier Gaillard | 289ba40 | 2018-07-24 18:50:13 +0100 | [diff] [blame] | 121 | import com.android.server.BinderCallsStatsService; |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 122 | import com.android.server.LocalServices; |
| 123 | import com.android.server.SystemService; |
Chenjie Yu | 2c0d83d | 2018-09-02 06:37:08 -0700 | [diff] [blame] | 124 | import com.android.server.SystemServiceManager; |
Rafal Slawik | c376262 | 2018-11-07 12:07:26 +0000 | [diff] [blame] | 125 | import com.android.server.am.MemoryStatUtil.MemoryStat; |
Hai Zhang | 85fd062 | 2019-02-01 14:06:04 -0800 | [diff] [blame] | 126 | import com.android.server.role.RoleManagerInternal; |
Tej Singh | 86dc9db | 2018-09-06 00:39:57 +0000 | [diff] [blame] | 127 | import com.android.server.storage.DiskStatsFileLogger; |
| 128 | import com.android.server.storage.DiskStatsLoggingService; |
| 129 | |
| 130 | import libcore.io.IoUtils; |
| 131 | |
| 132 | import org.json.JSONArray; |
| 133 | import org.json.JSONException; |
| 134 | import org.json.JSONObject; |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 135 | |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 136 | import java.io.File; |
| 137 | import java.io.FileDescriptor; |
Tej Singh | 86dc9db | 2018-09-06 00:39:57 +0000 | [diff] [blame] | 138 | import java.io.FileOutputStream; |
| 139 | import java.io.IOException; |
Chenjie Yu | 2c0d83d | 2018-09-02 06:37:08 -0700 | [diff] [blame] | 140 | import java.io.InputStream; |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 141 | import java.io.PrintWriter; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 142 | import java.util.ArrayList; |
Bookatz | 058d869 | 2018-03-06 09:53:47 -0800 | [diff] [blame] | 143 | import java.util.Arrays; |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 144 | import java.util.HashMap; |
| 145 | import java.util.HashSet; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 146 | import java.util.List; |
Bookatz | c68a9d2 | 2017-09-27 14:09:55 -0700 | [diff] [blame] | 147 | import java.util.Map; |
Olivier Gaillard | 6f52d15 | 2018-07-25 12:13:12 +0100 | [diff] [blame] | 148 | import java.util.Map.Entry; |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 149 | import java.util.concurrent.TimeUnit; |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 150 | import java.util.concurrent.TimeoutException; |
Bookatz | c68a9d2 | 2017-09-27 14:09:55 -0700 | [diff] [blame] | 151 | |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 152 | /** |
| 153 | * Helper service for statsd (the native stats management service in cmds/statsd/). |
| 154 | * Used for registering and receiving alarms on behalf of statsd. |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 155 | * |
Bookatz | 6bc51d7 | 2017-09-28 16:43:40 -0700 | [diff] [blame] | 156 | * @hide |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 157 | */ |
| 158 | public class StatsCompanionService extends IStatsCompanionService.Stub { |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 159 | /** |
| 160 | * How long to wait on an individual subsystem to return its stats. |
| 161 | */ |
| 162 | private static final long EXTERNAL_STATS_SYNC_TIMEOUT_MILLIS = 2000; |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 163 | private static final long MILLIS_IN_A_DAY = TimeUnit.DAYS.toMillis(1); |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 164 | |
| 165 | public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity"; |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 166 | public static final String CONFIG_DIR = "/data/misc/stats-service"; |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 167 | |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 168 | static final String TAG = "StatsCompanionService"; |
Chenjie Yu | 2c0d83d | 2018-09-02 06:37:08 -0700 | [diff] [blame] | 169 | static final boolean DEBUG = false; |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 170 | |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 171 | public static final int CODE_DATA_BROADCAST = 1; |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 172 | public static final int CODE_SUBSCRIBER_BROADCAST = 1; |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 173 | public static final int CODE_ACTIVE_CONFIGS_BROADCAST = 1; |
David Chen | d37bc23 | 2018-04-12 18:05:11 -0700 | [diff] [blame] | 174 | /** |
| 175 | * The last report time is provided with each intent registered to |
| 176 | * StatsManager#setFetchReportsOperation. This allows easy de-duping in the receiver if |
| 177 | * statsd is requesting the client to retrieve the same statsd data. The last report time |
| 178 | * corresponds to the last_report_elapsed_nanos that will provided in the current |
| 179 | * ConfigMetricsReport, and this timestamp also corresponds to the |
| 180 | * current_report_elapsed_nanos of the most recently obtained ConfigMetricsReport. |
| 181 | */ |
| 182 | public static final String EXTRA_LAST_REPORT_TIME = "android.app.extra.LAST_REPORT_TIME"; |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 183 | public static final int DEATH_THRESHOLD = 10; |
Rafal Slawik | 3bea895 | 2018-11-15 12:39:33 +0000 | [diff] [blame] | 184 | /** |
| 185 | * Which native processes to snapshot memory for. |
| 186 | * |
| 187 | * <p>Processes are matched by their cmdline in procfs. Example: cat /proc/pid/cmdline returns |
| 188 | * /system/bin/statsd for the stats daemon. |
| 189 | */ |
| 190 | private static final String[] MEMORY_INTERESTING_NATIVE_PROCESSES = new String[]{ |
| 191 | "/system/bin/statsd", // Stats daemon. |
| 192 | "/system/bin/surfaceflinger", |
| 193 | "/system/bin/apexd", // APEX daemon. |
| 194 | "/system/bin/audioserver", |
| 195 | "/system/bin/cameraserver", |
| 196 | "/system/bin/drmserver", |
| 197 | "/system/bin/healthd", |
| 198 | "/system/bin/incidentd", |
| 199 | "/system/bin/installd", |
| 200 | "/system/bin/lmkd", // Low memory killer daemon. |
| 201 | "/system/bin/logd", |
| 202 | "media.codec", |
| 203 | "media.extractor", |
| 204 | "media.metrics", |
| 205 | "/system/bin/mediadrmserver", |
| 206 | "/system/bin/mediaserver", |
| 207 | "/system/bin/performanced", |
| 208 | "/system/bin/tombstoned", |
| 209 | "/system/bin/traced", // Perfetto. |
| 210 | "/system/bin/traced_probes", // Perfetto. |
| 211 | "webview_zygote", |
Rafal Slawik | 932c71e | 2018-11-30 16:10:32 +0000 | [diff] [blame] | 212 | "zygote", |
| 213 | "zygote64", |
Rafal Slawik | 3bea895 | 2018-11-15 12:39:33 +0000 | [diff] [blame] | 214 | }; |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 215 | |
Misha Wagner | 4b32c9f | 2019-01-25 15:30:14 +0000 | [diff] [blame] | 216 | private static final int CPU_TIME_PER_THREAD_FREQ_MAX_NUM_FREQUENCIES = 8; |
Olivier Gaillard | 21fb523 | 2018-10-09 13:19:27 +0100 | [diff] [blame] | 217 | |
| 218 | static final class CompanionHandler extends Handler { |
| 219 | CompanionHandler(Looper looper) { |
| 220 | super(looper); |
| 221 | } |
| 222 | } |
| 223 | |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 224 | private final Context mContext; |
| 225 | private final AlarmManager mAlarmManager; |
Chenbo Feng | e62e287 | 2019-01-11 15:50:17 -0800 | [diff] [blame] | 226 | private final INetworkStatsService mNetworkStatsService; |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 227 | @GuardedBy("sStatsdLock") |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 228 | private static IStatsManager sStatsd; |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 229 | private static final Object sStatsdLock = new Object(); |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 230 | |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 231 | private final OnAlarmListener mAnomalyAlarmListener = new AnomalyAlarmListener(); |
| 232 | private final OnAlarmListener mPullingAlarmListener = new PullingAlarmListener(); |
| 233 | private final OnAlarmListener mPeriodicAlarmListener = new PeriodicAlarmListener(); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 234 | private final BroadcastReceiver mAppUpdateReceiver; |
David Chen | 47e8f4d | 2017-10-11 15:34:13 -0700 | [diff] [blame] | 235 | private final BroadcastReceiver mUserUpdateReceiver; |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 236 | private final ShutdownEventReceiver mShutdownEventReceiver; |
Chenjie Yu | 7f8def9 | 2017-11-03 09:33:15 -0700 | [diff] [blame] | 237 | private final KernelWakelockReader mKernelWakelockReader = new KernelWakelockReader(); |
| 238 | private final KernelWakelockStats mTmpWakelockStats = new KernelWakelockStats(); |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 239 | private IWifiManager mWifiManager = null; |
| 240 | private TelephonyManager mTelephony = null; |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 241 | @GuardedBy("sStatsdLock") |
| 242 | private final HashSet<Long> mDeathTimeMillis = new HashSet<>(); |
| 243 | @GuardedBy("sStatsdLock") |
| 244 | private final HashMap<Long, String> mDeletedFiles = new HashMap<>(); |
Olivier Gaillard | 21fb523 | 2018-10-09 13:19:27 +0100 | [diff] [blame] | 245 | private final CompanionHandler mHandler; |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 246 | |
Mike Ma | 7ab7fcd | 2019-01-16 15:00:40 -0800 | [diff] [blame] | 247 | // Disables throttler on CPU time readers. |
| 248 | private KernelCpuUidUserSysTimeReader mCpuUidUserSysTimeReader = |
| 249 | new KernelCpuUidUserSysTimeReader(false); |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 250 | private KernelCpuSpeedReader[] mKernelCpuSpeedReaders; |
Mike Ma | 7ab7fcd | 2019-01-16 15:00:40 -0800 | [diff] [blame] | 251 | private KernelCpuUidFreqTimeReader mCpuUidFreqTimeReader = |
| 252 | new KernelCpuUidFreqTimeReader(false); |
| 253 | private KernelCpuUidActiveTimeReader mCpuUidActiveTimeReader = |
| 254 | new KernelCpuUidActiveTimeReader(false); |
| 255 | private KernelCpuUidClusterTimeReader mCpuUidClusterTimeReader = |
| 256 | new KernelCpuUidClusterTimeReader(false); |
Tej Singh | e7726dc | 2018-09-21 11:42:12 -0700 | [diff] [blame] | 257 | private StoragedUidIoStatsReader mStoragedUidIoStatsReader = |
| 258 | new StoragedUidIoStatsReader(); |
Misha Wagner | 5a51e00 | 2018-10-03 15:04:09 +0100 | [diff] [blame] | 259 | @Nullable |
| 260 | private final KernelCpuThreadReader mKernelCpuThreadReader; |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 261 | |
Olivier Gaillard | 18d45220 | 2019-01-22 15:03:41 +0000 | [diff] [blame] | 262 | private long mDebugElapsedClockPreviousValue = 0; |
| 263 | private long mDebugElapsedClockPullCount = 0; |
| 264 | private long mDebugFailingElapsedClockPreviousValue = 0; |
| 265 | private long mDebugFailingElapsedClockPullCount = 0; |
Bookatz | 75ee604 | 2018-11-09 12:27:37 -0800 | [diff] [blame] | 266 | private BatteryStatsHelper mBatteryStatsHelper = null; |
| 267 | private static final int MAX_BATTERY_STATS_HELPER_FREQUENCY_MS = 1000; |
| 268 | private long mBatteryStatsHelperTimestampMs = -MAX_BATTERY_STATS_HELPER_FREQUENCY_MS; |
| 269 | |
Yangster-mac | e16189a | 2018-08-26 12:20:16 -0700 | [diff] [blame] | 270 | private static IThermalService sThermalService; |
Chenjie Yu | 2c0d83d | 2018-09-02 06:37:08 -0700 | [diff] [blame] | 271 | private File mBaseDir = |
| 272 | new File(SystemServiceManager.ensureSystemDir(), "stats_companion"); |
Yangster-mac | 308ea0c | 2018-10-22 13:10:25 -0700 | [diff] [blame] | 273 | @GuardedBy("this") |
| 274 | ProcessCpuTracker mProcessCpuTracker = null; |
Yangster-mac | e16189a | 2018-08-26 12:20:16 -0700 | [diff] [blame] | 275 | |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 276 | public StatsCompanionService(Context context) { |
| 277 | super(); |
| 278 | mContext = context; |
| 279 | mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); |
Chenbo Feng | e62e287 | 2019-01-11 15:50:17 -0800 | [diff] [blame] | 280 | mNetworkStatsService = INetworkStatsService.Stub.asInterface( |
| 281 | ServiceManager.getService(Context.NETWORK_STATS_SERVICE)); |
Chenjie Yu | 2c0d83d | 2018-09-02 06:37:08 -0700 | [diff] [blame] | 282 | mBaseDir.mkdirs(); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 283 | mAppUpdateReceiver = new AppUpdateReceiver(); |
David Chen | 47e8f4d | 2017-10-11 15:34:13 -0700 | [diff] [blame] | 284 | mUserUpdateReceiver = new BroadcastReceiver() { |
| 285 | @Override |
| 286 | public void onReceive(Context context, Intent intent) { |
| 287 | synchronized (sStatsdLock) { |
| 288 | sStatsd = fetchStatsdService(); |
| 289 | if (sStatsd == null) { |
Bookatz | 111ed73 | 2018-02-16 15:54:05 -0800 | [diff] [blame] | 290 | Slog.w(TAG, "Could not access statsd for UserUpdateReceiver"); |
David Chen | 47e8f4d | 2017-10-11 15:34:13 -0700 | [diff] [blame] | 291 | return; |
| 292 | } |
| 293 | try { |
| 294 | // Pull the latest state of UID->app name, version mapping. |
| 295 | // Needed since the new user basically has a version of every app. |
| 296 | informAllUidsLocked(context); |
| 297 | } catch (RemoteException e) { |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 298 | Slog.e(TAG, "Failed to inform statsd latest update of all apps", e); |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 299 | forgetEverythingLocked(); |
David Chen | 47e8f4d | 2017-10-11 15:34:13 -0700 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | } |
| 303 | }; |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 304 | mShutdownEventReceiver = new ShutdownEventReceiver(); |
Bookatz | 111ed73 | 2018-02-16 15:54:05 -0800 | [diff] [blame] | 305 | if (DEBUG) Slog.d(TAG, "Registered receiver for ACTION_PACKAGE_REPLACED and ADDED."); |
Chenjie Yu | 7f8def9 | 2017-11-03 09:33:15 -0700 | [diff] [blame] | 306 | PowerProfile powerProfile = new PowerProfile(context); |
| 307 | final int numClusters = powerProfile.getNumCpuClusters(); |
| 308 | mKernelCpuSpeedReaders = new KernelCpuSpeedReader[numClusters]; |
| 309 | int firstCpuOfCluster = 0; |
| 310 | for (int i = 0; i < numClusters; i++) { |
| 311 | final int numSpeedSteps = powerProfile.getNumSpeedStepsInCpuCluster(i); |
| 312 | mKernelCpuSpeedReaders[i] = new KernelCpuSpeedReader(firstCpuOfCluster, |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 313 | numSpeedSteps); |
Chenjie Yu | 7f8def9 | 2017-11-03 09:33:15 -0700 | [diff] [blame] | 314 | firstCpuOfCluster += powerProfile.getNumCoresInCpuCluster(i); |
| 315 | } |
Yangster-mac | e16189a | 2018-08-26 12:20:16 -0700 | [diff] [blame] | 316 | |
| 317 | // Enable push notifications of throttling from vendor thermal |
| 318 | // management subsystem via thermalservice. |
| 319 | IBinder b = ServiceManager.getService("thermalservice"); |
| 320 | |
| 321 | if (b != null) { |
| 322 | sThermalService = IThermalService.Stub.asInterface(b); |
| 323 | try { |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 324 | sThermalService.registerThermalEventListenerWithType( |
| 325 | new ThermalEventListener(), Temperature.TYPE_SKIN); |
Yangster-mac | e16189a | 2018-08-26 12:20:16 -0700 | [diff] [blame] | 326 | Slog.i(TAG, "register thermal listener successfully"); |
| 327 | } catch (RemoteException e) { |
| 328 | // Should never happen. |
| 329 | Slog.e(TAG, "register thermal listener error"); |
| 330 | } |
| 331 | } else { |
| 332 | Slog.e(TAG, "cannot find thermalservice, no throttling push notifications"); |
| 333 | } |
Olivier Gaillard | 21fb523 | 2018-10-09 13:19:27 +0100 | [diff] [blame] | 334 | |
Chenjie Yu | ae9dfac | 2018-10-25 16:06:56 -0700 | [diff] [blame] | 335 | // Default NetworkRequest should cover all transport types. |
| 336 | final NetworkRequest request = new NetworkRequest.Builder().build(); |
| 337 | final ConnectivityManager connectivityManager = |
| 338 | (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 339 | connectivityManager.registerNetworkCallback(request, new ConnectivityStatsCallback()); |
| 340 | |
Olivier Gaillard | 21fb523 | 2018-10-09 13:19:27 +0100 | [diff] [blame] | 341 | HandlerThread handlerThread = new HandlerThread(TAG); |
| 342 | handlerThread.start(); |
| 343 | mHandler = new CompanionHandler(handlerThread.getLooper()); |
| 344 | |
Misha Wagner | 4b32c9f | 2019-01-25 15:30:14 +0000 | [diff] [blame] | 345 | mKernelCpuThreadReader = |
| 346 | KernelCpuThreadReaderSettingsObserver.getSettingsModifiedReader(mContext); |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 347 | } |
| 348 | |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 349 | @Override |
David Chen | d37bc23 | 2018-04-12 18:05:11 -0700 | [diff] [blame] | 350 | public void sendDataBroadcast(IBinder intentSenderBinder, long lastReportTimeNs) { |
Bookatz | 3692082 | 2018-01-26 09:18:07 -0800 | [diff] [blame] | 351 | enforceCallingPermission(); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 352 | IntentSender intentSender = new IntentSender(intentSenderBinder); |
| 353 | Intent intent = new Intent(); |
David Chen | d37bc23 | 2018-04-12 18:05:11 -0700 | [diff] [blame] | 354 | intent.putExtra(EXTRA_LAST_REPORT_TIME, lastReportTimeNs); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 355 | try { |
| 356 | intentSender.sendIntent(mContext, CODE_DATA_BROADCAST, intent, null, null); |
| 357 | } catch (IntentSender.SendIntentException e) { |
| 358 | Slog.w(TAG, "Unable to send using IntentSender"); |
| 359 | } |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 362 | @Override |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 363 | public void sendActiveConfigsChangedBroadcast(IBinder intentSenderBinder, long[] configIds) { |
| 364 | enforceCallingPermission(); |
| 365 | IntentSender intentSender = new IntentSender(intentSenderBinder); |
| 366 | Intent intent = new Intent(); |
| 367 | intent.putExtra(StatsManager.EXTRA_STATS_ACTIVE_CONFIG_KEYS, configIds); |
| 368 | try { |
| 369 | intentSender.sendIntent(mContext, CODE_ACTIVE_CONFIGS_BROADCAST, intent, null, null); |
| 370 | if (DEBUG) { |
| 371 | Slog.d(TAG, "Sent broadcast with config ids " + Arrays.toString(configIds)); |
| 372 | } |
| 373 | } catch (IntentSender.SendIntentException e) { |
| 374 | Slog.w(TAG, "Unable to send active configs changed broadcast using IntentSender"); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | @Override |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 379 | public void sendSubscriberBroadcast(IBinder intentSenderBinder, long configUid, long configKey, |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 380 | long subscriptionId, long subscriptionRuleId, String[] cookies, |
| 381 | StatsDimensionsValue dimensionsValue) { |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 382 | enforceCallingPermission(); |
| 383 | IntentSender intentSender = new IntentSender(intentSenderBinder); |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 384 | Intent intent = |
| 385 | new Intent() |
| 386 | .putExtra(StatsManager.EXTRA_STATS_CONFIG_UID, configUid) |
| 387 | .putExtra(StatsManager.EXTRA_STATS_CONFIG_KEY, configKey) |
| 388 | .putExtra(StatsManager.EXTRA_STATS_SUBSCRIPTION_ID, subscriptionId) |
| 389 | .putExtra(StatsManager.EXTRA_STATS_SUBSCRIPTION_RULE_ID, subscriptionRuleId) |
| 390 | .putExtra(StatsManager.EXTRA_STATS_DIMENSIONS_VALUE, dimensionsValue); |
Bookatz | 058d869 | 2018-03-06 09:53:47 -0800 | [diff] [blame] | 391 | |
| 392 | ArrayList<String> cookieList = new ArrayList<>(cookies.length); |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 393 | for (String cookie : cookies) { |
| 394 | cookieList.add(cookie); |
| 395 | } |
Bookatz | 058d869 | 2018-03-06 09:53:47 -0800 | [diff] [blame] | 396 | intent.putStringArrayListExtra( |
| 397 | StatsManager.EXTRA_STATS_BROADCAST_SUBSCRIBER_COOKIES, cookieList); |
| 398 | |
Bookatz | 1476ef2 | 2018-02-13 12:26:01 -0800 | [diff] [blame] | 399 | if (DEBUG) { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 400 | Slog.d(TAG, |
| 401 | String.format("Statsd sendSubscriberBroadcast with params {%d %d %d %d %s %s}", |
| 402 | configUid, configKey, subscriptionId, subscriptionRuleId, |
| 403 | Arrays.toString(cookies), |
| 404 | dimensionsValue)); |
Bookatz | 1476ef2 | 2018-02-13 12:26:01 -0800 | [diff] [blame] | 405 | } |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 406 | try { |
| 407 | intentSender.sendIntent(mContext, CODE_SUBSCRIBER_BROADCAST, intent, null, null); |
| 408 | } catch (IntentSender.SendIntentException e) { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 409 | Slog.w(TAG, |
| 410 | "Unable to send using IntentSender from uid " + configUid |
| 411 | + "; presumably it had been cancelled."); |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 415 | private final static int[] toIntArray(List<Integer> list) { |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 416 | int[] ret = new int[list.size()]; |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 417 | for (int i = 0; i < ret.length; i++) { |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 418 | ret[i] = list.get(i); |
| 419 | } |
| 420 | return ret; |
| 421 | } |
| 422 | |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 423 | private final static long[] toLongArray(List<Long> list) { |
| 424 | long[] ret = new long[list.size()]; |
| 425 | for (int i = 0; i < ret.length; i++) { |
| 426 | ret[i] = list.get(i); |
| 427 | } |
| 428 | return ret; |
| 429 | } |
| 430 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 431 | // Assumes that sStatsdLock is held. |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 432 | @GuardedBy("sStatsdLock") |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 433 | private final void informAllUidsLocked(Context context) throws RemoteException { |
| 434 | UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE); |
| 435 | PackageManager pm = context.getPackageManager(); |
| 436 | final List<UserInfo> users = um.getUsers(true); |
| 437 | if (DEBUG) { |
Bookatz | 111ed73 | 2018-02-16 15:54:05 -0800 | [diff] [blame] | 438 | Slog.d(TAG, "Iterating over " + users.size() + " profiles."); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Rajeev Kumar | 22d92b7 | 2018-02-07 18:38:36 -0800 | [diff] [blame] | 441 | List<Integer> uids = new ArrayList<>(); |
| 442 | List<Long> versions = new ArrayList<>(); |
| 443 | List<String> apps = new ArrayList<>(); |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 444 | List<String> versionStrings = new ArrayList<>(); |
| 445 | List<String> installers = new ArrayList<>(); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 446 | |
| 447 | // Add in all the apps for every user/profile. |
| 448 | for (UserInfo profile : users) { |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 449 | List<PackageInfo> pi = |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 450 | pm.getInstalledPackagesAsUser(PackageManager.MATCH_KNOWN_PACKAGES, profile.id); |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 451 | for (int j = 0; j < pi.size(); j++) { |
| 452 | if (pi.get(j).applicationInfo != null) { |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 453 | String installer; |
| 454 | try { |
| 455 | installer = pm.getInstallerPackageName(pi.get(j).packageName); |
| 456 | } catch (IllegalArgumentException e) { |
| 457 | installer = ""; |
| 458 | } |
| 459 | installers.add(installer == null ? "" : installer); |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 460 | uids.add(pi.get(j).applicationInfo.uid); |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 461 | versions.add(pi.get(j).getLongVersionCode()); |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 462 | versionStrings.add(pi.get(j).versionName); |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 463 | apps.add(pi.get(j).packageName); |
| 464 | } |
| 465 | } |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 466 | } |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 467 | sStatsd.informAllUidData(toIntArray(uids), toLongArray(versions), |
| 468 | versionStrings.toArray(new String[versionStrings.size()]), |
| 469 | apps.toArray(new String[apps.size()]), |
| 470 | installers.toArray(new String[installers.size()])); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 471 | if (DEBUG) { |
Bookatz | 111ed73 | 2018-02-16 15:54:05 -0800 | [diff] [blame] | 472 | Slog.d(TAG, "Sent data for " + uids.size() + " apps"); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
Bookatz | 3692082 | 2018-01-26 09:18:07 -0800 | [diff] [blame] | 476 | private final static class AppUpdateReceiver extends BroadcastReceiver { |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 477 | @Override |
| 478 | public void onReceive(Context context, Intent intent) { |
David Chen | 47e8f4d | 2017-10-11 15:34:13 -0700 | [diff] [blame] | 479 | /** |
| 480 | * App updates actually consist of REMOVE, ADD, and then REPLACE broadcasts. To avoid |
| 481 | * 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] | 482 | * 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] | 483 | */ |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 484 | if (!intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED) |
| 485 | && intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
David Chen | 47e8f4d | 2017-10-11 15:34:13 -0700 | [diff] [blame] | 486 | return; // Keep only replacing or normal add and remove. |
| 487 | } |
Tej Singh | 90d3aa0 | 2018-03-08 19:07:58 -0800 | [diff] [blame] | 488 | if (DEBUG) Slog.d(TAG, "StatsCompanionService noticed an app was updated."); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 489 | synchronized (sStatsdLock) { |
| 490 | if (sStatsd == null) { |
yro | f6d1ca5 | 2017-11-15 15:38:34 -0800 | [diff] [blame] | 491 | 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] | 492 | return; |
| 493 | } |
| 494 | try { |
| 495 | if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) { |
| 496 | Bundle b = intent.getExtras(); |
| 497 | int uid = b.getInt(Intent.EXTRA_UID); |
| 498 | boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); |
| 499 | if (!replacing) { |
| 500 | // Don't bother sending an update if we're right about to get another |
| 501 | // intent for the new version that's added. |
| 502 | PackageManager pm = context.getPackageManager(); |
| 503 | String app = intent.getData().getSchemeSpecificPart(); |
| 504 | sStatsd.informOnePackageRemoved(app, uid); |
| 505 | } |
| 506 | } else { |
| 507 | PackageManager pm = context.getPackageManager(); |
| 508 | Bundle b = intent.getExtras(); |
| 509 | int uid = b.getInt(Intent.EXTRA_UID); |
| 510 | String app = intent.getData().getSchemeSpecificPart(); |
| 511 | PackageInfo pi = pm.getPackageInfo(app, PackageManager.MATCH_ANY_USER); |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 512 | String installer; |
| 513 | try { |
| 514 | installer = pm.getInstallerPackageName(app); |
| 515 | } catch (IllegalArgumentException e) { |
| 516 | installer = ""; |
| 517 | } |
| 518 | sStatsd.informOnePackage(app, uid, pi.getLongVersionCode(), pi.versionName, |
| 519 | installer == null ? "" : installer); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 520 | } |
| 521 | } catch (Exception e) { |
| 522 | Slog.w(TAG, "Failed to inform statsd of an app update", e); |
| 523 | } |
| 524 | } |
| 525 | } |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 526 | } |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 527 | |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 528 | public final static class AnomalyAlarmListener implements OnAlarmListener { |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 529 | @Override |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 530 | public void onAlarm() { |
Bookatz | 66fe061 | 2018-02-07 18:51:48 -0800 | [diff] [blame] | 531 | Slog.i(TAG, "StatsCompanionService believes an anomaly has occurred at time " |
| 532 | + System.currentTimeMillis() + "ms."); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 533 | synchronized (sStatsdLock) { |
| 534 | if (sStatsd == null) { |
| 535 | Slog.w(TAG, "Could not access statsd to inform it of anomaly alarm firing"); |
| 536 | return; |
| 537 | } |
| 538 | try { |
| 539 | // Two-way call to statsd to retain AlarmManager wakelock |
| 540 | sStatsd.informAnomalyAlarmFired(); |
| 541 | } catch (RemoteException e) { |
| 542 | Slog.w(TAG, "Failed to inform statsd of anomaly alarm firing", e); |
| 543 | } |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 544 | } |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 545 | // AlarmManager releases its own wakelock here. |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 546 | } |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 547 | } |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 548 | |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 549 | public final static class PullingAlarmListener implements OnAlarmListener { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 550 | @Override |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 551 | public void onAlarm() { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 552 | if (DEBUG) { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 553 | Slog.d(TAG, "Time to poll something."); |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 554 | } |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 555 | synchronized (sStatsdLock) { |
| 556 | if (sStatsd == null) { |
| 557 | Slog.w(TAG, "Could not access statsd to inform it of pulling alarm firing."); |
| 558 | return; |
| 559 | } |
| 560 | try { |
| 561 | // Two-way call to statsd to retain AlarmManager wakelock |
| 562 | sStatsd.informPollAlarmFired(); |
| 563 | } catch (RemoteException e) { |
| 564 | Slog.w(TAG, "Failed to inform statsd of pulling alarm firing.", e); |
| 565 | } |
| 566 | } |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 567 | } |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 568 | } |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 569 | |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 570 | public final static class PeriodicAlarmListener implements OnAlarmListener { |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 571 | @Override |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 572 | public void onAlarm() { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 573 | if (DEBUG) { |
Yangster-mac | c04feba | 2018-04-02 14:37:33 -0700 | [diff] [blame] | 574 | Slog.d(TAG, "Time to trigger periodic alarm."); |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 575 | } |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 576 | synchronized (sStatsdLock) { |
| 577 | if (sStatsd == null) { |
| 578 | Slog.w(TAG, "Could not access statsd to inform it of periodic alarm firing."); |
| 579 | return; |
| 580 | } |
| 581 | try { |
| 582 | // Two-way call to statsd to retain AlarmManager wakelock |
| 583 | sStatsd.informAlarmForSubscriberTriggeringFired(); |
| 584 | } catch (RemoteException e) { |
| 585 | Slog.w(TAG, "Failed to inform statsd of periodic alarm firing.", e); |
| 586 | } |
| 587 | } |
| 588 | // AlarmManager releases its own wakelock here. |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | public final static class ShutdownEventReceiver extends BroadcastReceiver { |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 593 | @Override |
| 594 | public void onReceive(Context context, Intent intent) { |
| 595 | /** |
| 596 | * Skip immediately if intent is not relevant to device shutdown. |
| 597 | */ |
| 598 | if (!intent.getAction().equals(Intent.ACTION_REBOOT) |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 599 | && !(intent.getAction().equals(Intent.ACTION_SHUTDOWN) |
| 600 | && (intent.getFlags() & Intent.FLAG_RECEIVER_FOREGROUND) != 0)) { |
| 601 | return; |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 602 | } |
yro | f7a3bcb | 2018-01-24 17:18:55 -0800 | [diff] [blame] | 603 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 604 | Slog.i(TAG, "StatsCompanionService noticed a shutdown."); |
| 605 | synchronized (sStatsdLock) { |
| 606 | if (sStatsd == null) { |
| 607 | Slog.w(TAG, "Could not access statsd to inform it of a shutdown event."); |
| 608 | return; |
| 609 | } |
| 610 | try { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 611 | sStatsd.informDeviceShutdown(); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 612 | } catch (Exception e) { |
| 613 | Slog.w(TAG, "Failed to inform statsd of a shutdown event.", e); |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 619 | @Override // Binder call |
| 620 | public void setAnomalyAlarm(long timestampMs) { |
| 621 | enforceCallingPermission(); |
| 622 | if (DEBUG) Slog.d(TAG, "Setting anomaly alarm for " + timestampMs); |
| 623 | final long callingToken = Binder.clearCallingIdentity(); |
| 624 | try { |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 625 | // using ELAPSED_REALTIME, not ELAPSED_REALTIME_WAKEUP, so if device is asleep, will |
| 626 | // only fire when it awakens. |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 627 | // AlarmManager will automatically cancel any previous mAnomalyAlarmListener alarm. |
| 628 | mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME, timestampMs, TAG + ".anomaly", |
Olivier Gaillard | 21fb523 | 2018-10-09 13:19:27 +0100 | [diff] [blame] | 629 | mAnomalyAlarmListener, mHandler); |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 630 | } finally { |
| 631 | Binder.restoreCallingIdentity(callingToken); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | @Override // Binder call |
| 636 | public void cancelAnomalyAlarm() { |
| 637 | enforceCallingPermission(); |
| 638 | if (DEBUG) Slog.d(TAG, "Cancelling anomaly alarm"); |
| 639 | final long callingToken = Binder.clearCallingIdentity(); |
| 640 | try { |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 641 | mAlarmManager.cancel(mAnomalyAlarmListener); |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 642 | } finally { |
| 643 | Binder.restoreCallingIdentity(callingToken); |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | @Override // Binder call |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 648 | public void setAlarmForSubscriberTriggering(long timestampMs) { |
| 649 | enforceCallingPermission(); |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 650 | if (DEBUG) { |
| 651 | Slog.d(TAG, |
| 652 | "Setting periodic alarm in about " + (timestampMs |
| 653 | - SystemClock.elapsedRealtime())); |
| 654 | } |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 655 | final long callingToken = Binder.clearCallingIdentity(); |
| 656 | try { |
| 657 | // using ELAPSED_REALTIME, not ELAPSED_REALTIME_WAKEUP, so if device is asleep, will |
| 658 | // only fire when it awakens. |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 659 | mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME, timestampMs, TAG + ".periodic", |
Olivier Gaillard | 21fb523 | 2018-10-09 13:19:27 +0100 | [diff] [blame] | 660 | mPeriodicAlarmListener, mHandler); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 661 | } finally { |
| 662 | Binder.restoreCallingIdentity(callingToken); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | @Override // Binder call |
| 667 | public void cancelAlarmForSubscriberTriggering() { |
| 668 | enforceCallingPermission(); |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 669 | if (DEBUG) { |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 670 | Slog.d(TAG, "Cancelling periodic alarm"); |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 671 | } |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 672 | final long callingToken = Binder.clearCallingIdentity(); |
| 673 | try { |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 674 | mAlarmManager.cancel(mPeriodicAlarmListener); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 675 | } finally { |
| 676 | Binder.restoreCallingIdentity(callingToken); |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | @Override // Binder call |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 681 | public void setPullingAlarm(long nextPullTimeMs) { |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 682 | enforceCallingPermission(); |
| 683 | if (DEBUG) { |
| 684 | Slog.d(TAG, "Setting pulling alarm in about " |
| 685 | + (nextPullTimeMs - SystemClock.elapsedRealtime())); |
| 686 | } |
| 687 | final long callingToken = Binder.clearCallingIdentity(); |
| 688 | try { |
| 689 | // using ELAPSED_REALTIME, not ELAPSED_REALTIME_WAKEUP, so if device is asleep, will |
| 690 | // only fire when it awakens. |
| 691 | mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME, nextPullTimeMs, TAG + ".pull", |
Olivier Gaillard | 21fb523 | 2018-10-09 13:19:27 +0100 | [diff] [blame] | 692 | mPullingAlarmListener, mHandler); |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 693 | } finally { |
| 694 | Binder.restoreCallingIdentity(callingToken); |
| 695 | } |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | @Override // Binder call |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 699 | public void cancelPullingAlarm() { |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 700 | enforceCallingPermission(); |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 701 | if (DEBUG) { |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 702 | Slog.d(TAG, "Cancelling pulling alarm"); |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 703 | } |
Bookatz | 75158ac | 2018-05-18 15:55:23 -0700 | [diff] [blame] | 704 | final long callingToken = Binder.clearCallingIdentity(); |
| 705 | try { |
| 706 | mAlarmManager.cancel(mPullingAlarmListener); |
| 707 | } finally { |
| 708 | Binder.restoreCallingIdentity(callingToken); |
| 709 | } |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 712 | private void addNetworkStats( |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 713 | int tag, List<StatsLogEventWrapper> ret, NetworkStats stats, boolean withFGBG) { |
| 714 | int size = stats.size(); |
Yangster-mac | c552b35 | 2018-02-22 10:00:45 -0800 | [diff] [blame] | 715 | long elapsedNanos = SystemClock.elapsedRealtimeNanos(); |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 716 | long wallClockNanos = SystemClock.currentTimeMicro() * 1000L; |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 717 | NetworkStats.Entry entry = new NetworkStats.Entry(); // For recycling |
| 718 | for (int j = 0; j < size; j++) { |
| 719 | stats.getValues(j, entry); |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 720 | StatsLogEventWrapper e = new StatsLogEventWrapper(tag, elapsedNanos, wallClockNanos); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 721 | e.writeInt(entry.uid); |
| 722 | if (withFGBG) { |
| 723 | e.writeInt(entry.set); |
| 724 | } |
| 725 | e.writeLong(entry.rxBytes); |
| 726 | e.writeLong(entry.rxPackets); |
| 727 | e.writeLong(entry.txBytes); |
| 728 | e.writeLong(entry.txPackets); |
| 729 | ret.add(e); |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 730 | } |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | /** |
| 734 | * Allows rollups per UID but keeping the set (foreground/background) slicing. |
| 735 | * Adapted from groupedByUid in frameworks/base/core/java/android/net/NetworkStats.java |
| 736 | */ |
| 737 | private NetworkStats rollupNetworkStatsByFGBG(NetworkStats stats) { |
| 738 | final NetworkStats ret = new NetworkStats(stats.getElapsedRealtime(), 1); |
| 739 | |
| 740 | final NetworkStats.Entry entry = new NetworkStats.Entry(); |
| 741 | entry.iface = NetworkStats.IFACE_ALL; |
| 742 | entry.tag = NetworkStats.TAG_NONE; |
| 743 | entry.metered = NetworkStats.METERED_ALL; |
| 744 | entry.roaming = NetworkStats.ROAMING_ALL; |
| 745 | |
| 746 | int size = stats.size(); |
| 747 | NetworkStats.Entry recycle = new NetworkStats.Entry(); // Used for retrieving values |
| 748 | for (int i = 0; i < size; i++) { |
| 749 | stats.getValues(i, recycle); |
| 750 | |
| 751 | // Skip specific tags, since already counted in TAG_NONE |
| 752 | if (recycle.tag != NetworkStats.TAG_NONE) continue; |
| 753 | |
| 754 | entry.set = recycle.set; // Allows slicing by background/foreground |
| 755 | entry.uid = recycle.uid; |
| 756 | entry.rxBytes = recycle.rxBytes; |
| 757 | entry.rxPackets = recycle.rxPackets; |
| 758 | entry.txBytes = recycle.txBytes; |
| 759 | entry.txPackets = recycle.txPackets; |
| 760 | // Operations purposefully omitted since we don't use them for statsd. |
| 761 | ret.combineValues(entry); |
| 762 | } |
| 763 | return ret; |
| 764 | } |
| 765 | |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 766 | /** |
| 767 | * Helper method to extract the Parcelable controller info from a |
| 768 | * SynchronousResultReceiver. |
| 769 | */ |
| 770 | private static <T extends Parcelable> T awaitControllerInfo( |
| 771 | @Nullable SynchronousResultReceiver receiver) { |
| 772 | if (receiver == null) { |
| 773 | return null; |
| 774 | } |
| 775 | |
| 776 | try { |
| 777 | final SynchronousResultReceiver.Result result = |
| 778 | receiver.awaitResult(EXTERNAL_STATS_SYNC_TIMEOUT_MILLIS); |
| 779 | if (result.bundle != null) { |
| 780 | // This is the final destination for the Bundle. |
| 781 | result.bundle.setDefusable(true); |
| 782 | |
| 783 | final T data = result.bundle.getParcelable( |
| 784 | RESULT_RECEIVER_CONTROLLER_KEY); |
| 785 | if (data != null) { |
| 786 | return data; |
| 787 | } |
| 788 | } |
| 789 | Slog.e(TAG, "no controller energy info supplied for " + receiver.getName()); |
| 790 | } catch (TimeoutException e) { |
| 791 | Slog.w(TAG, "timeout reading " + receiver.getName() + " stats"); |
| 792 | } |
| 793 | return null; |
| 794 | } |
| 795 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 796 | private void pullKernelWakelock( |
| 797 | int tagId, long elapsedNanos, long wallClockNanos, |
| 798 | List<StatsLogEventWrapper> pulledData) { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 799 | final KernelWakelockStats wakelockStats = |
| 800 | mKernelWakelockReader.readKernelWakelockStats(mTmpWakelockStats); |
| 801 | for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) { |
| 802 | String name = ent.getKey(); |
| 803 | KernelWakelockStats.Entry kws = ent.getValue(); |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 804 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 805 | e.writeString(name); |
| 806 | e.writeInt(kws.mCount); |
| 807 | e.writeInt(kws.mVersion); |
| 808 | e.writeLong(kws.mTotalTime); |
| 809 | pulledData.add(e); |
| 810 | } |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 811 | } |
| 812 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 813 | private void pullWifiBytesTransfer( |
| 814 | int tagId, long elapsedNanos, long wallClockNanos, |
| 815 | List<StatsLogEventWrapper> pulledData) { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 816 | long token = Binder.clearCallingIdentity(); |
| 817 | try { |
| 818 | // TODO: Consider caching the following call to get BatteryStatsInternal. |
| 819 | BatteryStatsInternal bs = LocalServices.getService(BatteryStatsInternal.class); |
| 820 | String[] ifaces = bs.getWifiIfaces(); |
| 821 | if (ifaces.length == 0) { |
| 822 | return; |
| 823 | } |
Chenbo Feng | e62e287 | 2019-01-11 15:50:17 -0800 | [diff] [blame] | 824 | if (mNetworkStatsService == null) { |
| 825 | Slog.e(TAG, "NetworkStats Service is not available!"); |
| 826 | return; |
| 827 | } |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 828 | // Combine all the metrics per Uid into one record. |
Chenbo Feng | e62e287 | 2019-01-11 15:50:17 -0800 | [diff] [blame] | 829 | NetworkStats stats = mNetworkStatsService.getDetailedUidStats(ifaces).groupedByUid(); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 830 | addNetworkStats(tagId, pulledData, stats, false); |
Chenbo Feng | e62e287 | 2019-01-11 15:50:17 -0800 | [diff] [blame] | 831 | } catch (RemoteException e) { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 832 | Slog.e(TAG, "Pulling netstats for wifi bytes has error", e); |
| 833 | } finally { |
| 834 | Binder.restoreCallingIdentity(token); |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 835 | } |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 836 | } |
| 837 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 838 | private void pullWifiBytesTransferByFgBg( |
| 839 | int tagId, long elapsedNanos, long wallClockNanos, |
| 840 | List<StatsLogEventWrapper> pulledData) { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 841 | long token = Binder.clearCallingIdentity(); |
| 842 | try { |
| 843 | BatteryStatsInternal bs = LocalServices.getService(BatteryStatsInternal.class); |
| 844 | String[] ifaces = bs.getWifiIfaces(); |
| 845 | if (ifaces.length == 0) { |
| 846 | return; |
| 847 | } |
Chenbo Feng | e62e287 | 2019-01-11 15:50:17 -0800 | [diff] [blame] | 848 | if (mNetworkStatsService == null) { |
| 849 | Slog.e(TAG, "NetworkStats Service is not available!"); |
| 850 | return; |
| 851 | } |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 852 | NetworkStats stats = rollupNetworkStatsByFGBG( |
Chenbo Feng | e62e287 | 2019-01-11 15:50:17 -0800 | [diff] [blame] | 853 | mNetworkStatsService.getDetailedUidStats(ifaces)); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 854 | addNetworkStats(tagId, pulledData, stats, true); |
Chenbo Feng | e62e287 | 2019-01-11 15:50:17 -0800 | [diff] [blame] | 855 | } catch (RemoteException e) { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 856 | Slog.e(TAG, "Pulling netstats for wifi bytes w/ fg/bg has error", e); |
| 857 | } finally { |
| 858 | Binder.restoreCallingIdentity(token); |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 859 | } |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 860 | } |
| 861 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 862 | private void pullMobileBytesTransfer( |
| 863 | int tagId, long elapsedNanos, long wallClockNanos, |
| 864 | List<StatsLogEventWrapper> pulledData) { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 865 | long token = Binder.clearCallingIdentity(); |
| 866 | try { |
| 867 | BatteryStatsInternal bs = LocalServices.getService(BatteryStatsInternal.class); |
| 868 | String[] ifaces = bs.getMobileIfaces(); |
| 869 | if (ifaces.length == 0) { |
| 870 | return; |
| 871 | } |
Chenbo Feng | e62e287 | 2019-01-11 15:50:17 -0800 | [diff] [blame] | 872 | if (mNetworkStatsService == null) { |
| 873 | Slog.e(TAG, "NetworkStats Service is not available!"); |
| 874 | return; |
| 875 | } |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 876 | // Combine all the metrics per Uid into one record. |
Chenbo Feng | e62e287 | 2019-01-11 15:50:17 -0800 | [diff] [blame] | 877 | NetworkStats stats = mNetworkStatsService.getDetailedUidStats(ifaces).groupedByUid(); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 878 | addNetworkStats(tagId, pulledData, stats, false); |
Chenbo Feng | e62e287 | 2019-01-11 15:50:17 -0800 | [diff] [blame] | 879 | } catch (RemoteException e) { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 880 | Slog.e(TAG, "Pulling netstats for mobile bytes has error", e); |
| 881 | } finally { |
| 882 | Binder.restoreCallingIdentity(token); |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 883 | } |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 884 | } |
| 885 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 886 | private void pullBluetoothBytesTransfer( |
| 887 | int tagId, long elapsedNanos, long wallClockNanos, |
| 888 | List<StatsLogEventWrapper> pulledData) { |
Yangster-mac | 9554324 | 2018-10-22 11:44:03 -0700 | [diff] [blame] | 889 | BluetoothActivityEnergyInfo info = fetchBluetoothData(); |
Chenjie Yu | 9e59f93 | 2018-03-30 16:41:38 -0700 | [diff] [blame] | 890 | if (info.getUidTraffic() != null) { |
| 891 | for (UidTraffic traffic : info.getUidTraffic()) { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 892 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, |
| 893 | wallClockNanos); |
Chenjie Yu | 9e59f93 | 2018-03-30 16:41:38 -0700 | [diff] [blame] | 894 | e.writeInt(traffic.getUid()); |
| 895 | e.writeLong(traffic.getRxBytes()); |
| 896 | e.writeLong(traffic.getTxBytes()); |
| 897 | pulledData.add(e); |
| 898 | } |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 899 | } |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 900 | } |
| 901 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 902 | private void pullMobileBytesTransferByFgBg( |
| 903 | int tagId, long elapsedNanos, long wallClockNanos, |
| 904 | List<StatsLogEventWrapper> pulledData) { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 905 | long token = Binder.clearCallingIdentity(); |
| 906 | try { |
| 907 | BatteryStatsInternal bs = LocalServices.getService(BatteryStatsInternal.class); |
| 908 | String[] ifaces = bs.getMobileIfaces(); |
| 909 | if (ifaces.length == 0) { |
| 910 | return; |
| 911 | } |
Chenbo Feng | e62e287 | 2019-01-11 15:50:17 -0800 | [diff] [blame] | 912 | if (mNetworkStatsService == null) { |
| 913 | Slog.e(TAG, "NetworkStats Service is not available!"); |
| 914 | return; |
| 915 | } |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 916 | NetworkStats stats = rollupNetworkStatsByFGBG( |
Chenbo Feng | e62e287 | 2019-01-11 15:50:17 -0800 | [diff] [blame] | 917 | mNetworkStatsService.getDetailedUidStats(ifaces)); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 918 | addNetworkStats(tagId, pulledData, stats, true); |
Chenbo Feng | e62e287 | 2019-01-11 15:50:17 -0800 | [diff] [blame] | 919 | } catch (RemoteException e) { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 920 | Slog.e(TAG, "Pulling netstats for mobile bytes w/ fg/bg has error", e); |
| 921 | } finally { |
| 922 | Binder.restoreCallingIdentity(token); |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 923 | } |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 924 | } |
| 925 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 926 | private void pullCpuTimePerFreq( |
| 927 | int tagId, long elapsedNanos, long wallClockNanos, |
| 928 | List<StatsLogEventWrapper> pulledData) { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 929 | for (int cluster = 0; cluster < mKernelCpuSpeedReaders.length; cluster++) { |
| 930 | long[] clusterTimeMs = mKernelCpuSpeedReaders[cluster].readAbsolute(); |
| 931 | if (clusterTimeMs != null) { |
| 932 | for (int speed = clusterTimeMs.length - 1; speed >= 0; --speed) { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 933 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, |
| 934 | wallClockNanos); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 935 | e.writeInt(cluster); |
| 936 | e.writeInt(speed); |
| 937 | e.writeLong(clusterTimeMs[speed]); |
| 938 | pulledData.add(e); |
| 939 | } |
| 940 | } |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 941 | } |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 942 | } |
| 943 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 944 | private void pullKernelUidCpuTime( |
| 945 | int tagId, long elapsedNanos, long wallClockNanos, |
| 946 | List<StatsLogEventWrapper> pulledData) { |
Mike Ma | 7ab7fcd | 2019-01-16 15:00:40 -0800 | [diff] [blame] | 947 | mCpuUidUserSysTimeReader.readAbsolute((uid, timesUs) -> { |
| 948 | long userTimeUs = timesUs[0], systemTimeUs = timesUs[1]; |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 949 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 950 | e.writeInt(uid); |
| 951 | e.writeLong(userTimeUs); |
| 952 | e.writeLong(systemTimeUs); |
| 953 | pulledData.add(e); |
| 954 | }); |
| 955 | } |
| 956 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 957 | private void pullKernelUidCpuFreqTime( |
| 958 | int tagId, long elapsedNanos, long wallClockNanos, |
| 959 | List<StatsLogEventWrapper> pulledData) { |
Mike Ma | 7ab7fcd | 2019-01-16 15:00:40 -0800 | [diff] [blame] | 960 | mCpuUidFreqTimeReader.readAbsolute((uid, cpuFreqTimeMs) -> { |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 961 | for (int freqIndex = 0; freqIndex < cpuFreqTimeMs.length; ++freqIndex) { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 962 | if (cpuFreqTimeMs[freqIndex] != 0) { |
| 963 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, |
| 964 | wallClockNanos); |
Chenjie Yu | 4460e3c | 2018-03-14 17:12:59 -0700 | [diff] [blame] | 965 | e.writeInt(uid); |
| 966 | e.writeInt(freqIndex); |
| 967 | e.writeLong(cpuFreqTimeMs[freqIndex]); |
| 968 | pulledData.add(e); |
| 969 | } |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 970 | } |
| 971 | }); |
| 972 | } |
| 973 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 974 | private void pullKernelUidCpuClusterTime( |
| 975 | int tagId, long elapsedNanos, long wallClockNanos, |
| 976 | List<StatsLogEventWrapper> pulledData) { |
Mike Ma | 7ab7fcd | 2019-01-16 15:00:40 -0800 | [diff] [blame] | 977 | mCpuUidClusterTimeReader.readAbsolute((uid, cpuClusterTimesMs) -> { |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 978 | for (int i = 0; i < cpuClusterTimesMs.length; i++) { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 979 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, |
| 980 | wallClockNanos); |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 981 | e.writeInt(uid); |
| 982 | e.writeInt(i); |
| 983 | e.writeLong(cpuClusterTimesMs[i]); |
| 984 | pulledData.add(e); |
| 985 | } |
| 986 | }); |
| 987 | } |
| 988 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 989 | private void pullKernelUidCpuActiveTime( |
| 990 | int tagId, long elapsedNanos, long wallClockNanos, |
| 991 | List<StatsLogEventWrapper> pulledData) { |
Mike Ma | 7ab7fcd | 2019-01-16 15:00:40 -0800 | [diff] [blame] | 992 | mCpuUidActiveTimeReader.readAbsolute((uid, cpuActiveTimesMs) -> { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 993 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 994 | e.writeInt(uid); |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 995 | e.writeLong((long) cpuActiveTimesMs); |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 996 | pulledData.add(e); |
| 997 | }); |
| 998 | } |
| 999 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1000 | private void pullWifiActivityInfo( |
| 1001 | int tagId, long elapsedNanos, long wallClockNanos, |
| 1002 | List<StatsLogEventWrapper> pulledData) { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 1003 | long token = Binder.clearCallingIdentity(); |
Yangster-mac | 9554324 | 2018-10-22 11:44:03 -0700 | [diff] [blame] | 1004 | synchronized (this) { |
| 1005 | if (mWifiManager == null) { |
| 1006 | mWifiManager = |
| 1007 | IWifiManager.Stub.asInterface( |
| 1008 | ServiceManager.getService(Context.WIFI_SERVICE)); |
| 1009 | } |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 1010 | } |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 1011 | if (mWifiManager != null) { |
| 1012 | try { |
| 1013 | SynchronousResultReceiver wifiReceiver = new SynchronousResultReceiver("wifi"); |
| 1014 | mWifiManager.requestActivityInfo(wifiReceiver); |
| 1015 | final WifiActivityEnergyInfo wifiInfo = awaitControllerInfo(wifiReceiver); |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1016 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, |
| 1017 | wallClockNanos); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 1018 | e.writeLong(wifiInfo.getTimeStamp()); |
| 1019 | e.writeInt(wifiInfo.getStackState()); |
| 1020 | e.writeLong(wifiInfo.getControllerTxTimeMillis()); |
| 1021 | e.writeLong(wifiInfo.getControllerRxTimeMillis()); |
| 1022 | e.writeLong(wifiInfo.getControllerIdleTimeMillis()); |
| 1023 | e.writeLong(wifiInfo.getControllerEnergyUsed()); |
| 1024 | pulledData.add(e); |
| 1025 | } catch (RemoteException e) { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1026 | Slog.e(TAG, |
| 1027 | "Pulling wifiManager for wifi controller activity energy info has error", |
| 1028 | e); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 1029 | } finally { |
| 1030 | Binder.restoreCallingIdentity(token); |
| 1031 | } |
| 1032 | } |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 1033 | } |
| 1034 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1035 | private void pullModemActivityInfo( |
| 1036 | int tagId, long elapsedNanos, long wallClockNanos, |
| 1037 | List<StatsLogEventWrapper> pulledData) { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 1038 | long token = Binder.clearCallingIdentity(); |
Yangster-mac | 9554324 | 2018-10-22 11:44:03 -0700 | [diff] [blame] | 1039 | synchronized (this) { |
| 1040 | if (mTelephony == null) { |
| 1041 | mTelephony = TelephonyManager.from(mContext); |
| 1042 | } |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 1043 | } |
| 1044 | if (mTelephony != null) { |
| 1045 | SynchronousResultReceiver modemReceiver = new SynchronousResultReceiver("telephony"); |
| 1046 | mTelephony.requestModemActivityInfo(modemReceiver); |
| 1047 | final ModemActivityInfo modemInfo = awaitControllerInfo(modemReceiver); |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1048 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 1049 | e.writeLong(modemInfo.getTimestamp()); |
| 1050 | e.writeLong(modemInfo.getSleepTimeMillis()); |
| 1051 | e.writeLong(modemInfo.getIdleTimeMillis()); |
| 1052 | e.writeLong(modemInfo.getTxTimeMillis()[0]); |
| 1053 | e.writeLong(modemInfo.getTxTimeMillis()[1]); |
| 1054 | e.writeLong(modemInfo.getTxTimeMillis()[2]); |
| 1055 | e.writeLong(modemInfo.getTxTimeMillis()[3]); |
| 1056 | e.writeLong(modemInfo.getTxTimeMillis()[4]); |
| 1057 | e.writeLong(modemInfo.getRxTimeMillis()); |
| 1058 | e.writeLong(modemInfo.getEnergyUsed()); |
| 1059 | pulledData.add(e); |
| 1060 | } |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 1061 | } |
| 1062 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1063 | private void pullBluetoothActivityInfo( |
| 1064 | int tagId, long elapsedNanos, long wallClockNanos, |
| 1065 | List<StatsLogEventWrapper> pulledData) { |
Yangster-mac | 9554324 | 2018-10-22 11:44:03 -0700 | [diff] [blame] | 1066 | BluetoothActivityEnergyInfo info = fetchBluetoothData(); |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1067 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 1068 | e.writeLong(info.getTimeStamp()); |
| 1069 | e.writeInt(info.getBluetoothStackState()); |
| 1070 | e.writeLong(info.getControllerTxTimeMillis()); |
| 1071 | e.writeLong(info.getControllerRxTimeMillis()); |
| 1072 | e.writeLong(info.getControllerIdleTimeMillis()); |
| 1073 | e.writeLong(info.getControllerEnergyUsed()); |
| 1074 | pulledData.add(e); |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 1075 | } |
| 1076 | |
Yangster-mac | 9554324 | 2018-10-22 11:44:03 -0700 | [diff] [blame] | 1077 | private synchronized BluetoothActivityEnergyInfo fetchBluetoothData() { |
Chenjie Yu | c2d6544 | 2018-02-01 22:27:17 -0800 | [diff] [blame] | 1078 | final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 1079 | if (adapter != null) { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1080 | SynchronousResultReceiver bluetoothReceiver = new SynchronousResultReceiver( |
| 1081 | "bluetooth"); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 1082 | adapter.requestControllerActivityEnergyInfo(bluetoothReceiver); |
| 1083 | return awaitControllerInfo(bluetoothReceiver); |
| 1084 | } else { |
| 1085 | Slog.e(TAG, "Failed to get bluetooth adapter!"); |
| 1086 | return null; |
| 1087 | } |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 1088 | } |
| 1089 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1090 | private void pullSystemElapsedRealtime( |
| 1091 | int tagId, long elapsedNanos, long wallClockNanos, |
| 1092 | List<StatsLogEventWrapper> pulledData) { |
| 1093 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 1094 | e.writeLong(SystemClock.elapsedRealtime()); |
| 1095 | pulledData.add(e); |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 1096 | } |
| 1097 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1098 | private void pullSystemUpTime(int tagId, long elapsedNanos, long wallClockNanos, |
| 1099 | List<StatsLogEventWrapper> pulledData) { |
| 1100 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 1101 | e.writeLong(SystemClock.uptimeMillis()); |
| 1102 | pulledData.add(e); |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 1103 | } |
| 1104 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1105 | private void pullProcessMemoryState( |
| 1106 | int tagId, long elapsedNanos, long wallClockNanos, |
| 1107 | List<StatsLogEventWrapper> pulledData) { |
Rajeev Kumar | 22d92b7 | 2018-02-07 18:38:36 -0800 | [diff] [blame] | 1108 | List<ProcessMemoryState> processMemoryStates = |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1109 | LocalServices.getService( |
| 1110 | ActivityManagerInternal.class).getMemoryStateForProcesses(); |
Rajeev Kumar | 22d92b7 | 2018-02-07 18:38:36 -0800 | [diff] [blame] | 1111 | for (ProcessMemoryState processMemoryState : processMemoryStates) { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1112 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
Rajeev Kumar | 22d92b7 | 2018-02-07 18:38:36 -0800 | [diff] [blame] | 1113 | e.writeInt(processMemoryState.uid); |
| 1114 | e.writeString(processMemoryState.processName); |
| 1115 | e.writeInt(processMemoryState.oomScore); |
| 1116 | e.writeLong(processMemoryState.pgfault); |
| 1117 | e.writeLong(processMemoryState.pgmajfault); |
| 1118 | e.writeLong(processMemoryState.rssInBytes); |
| 1119 | e.writeLong(processMemoryState.cacheInBytes); |
| 1120 | e.writeLong(processMemoryState.swapInBytes); |
Rafal Slawik | d03ae42 | 2018-11-20 11:27:45 +0000 | [diff] [blame] | 1121 | e.writeLong(0); // unused |
Rafal Slawik | 272a816 | 2018-11-01 15:12:28 +0000 | [diff] [blame] | 1122 | e.writeLong(processMemoryState.startTimeNanos); |
Rajeev Kumar | 22d92b7 | 2018-02-07 18:38:36 -0800 | [diff] [blame] | 1123 | pulledData.add(e); |
| 1124 | } |
| 1125 | } |
| 1126 | |
Rafal Slawik | 0862158 | 2018-10-15 14:53:07 +0100 | [diff] [blame] | 1127 | private void pullNativeProcessMemoryState( |
| 1128 | int tagId, long elapsedNanos, long wallClockNanos, |
| 1129 | List<StatsLogEventWrapper> pulledData) { |
Rafal Slawik | 932c71e | 2018-11-30 16:10:32 +0000 | [diff] [blame] | 1130 | final List<String> processNames = Arrays.asList(MEMORY_INTERESTING_NATIVE_PROCESSES); |
Rafal Slawik | 3bea895 | 2018-11-15 12:39:33 +0000 | [diff] [blame] | 1131 | int[] pids = getPidsForCommands(MEMORY_INTERESTING_NATIVE_PROCESSES); |
Rafal Slawik | c376262 | 2018-11-07 12:07:26 +0000 | [diff] [blame] | 1132 | for (int i = 0; i < pids.length; i++) { |
| 1133 | int pid = pids[i]; |
| 1134 | MemoryStat memoryStat = readMemoryStatFromProcfs(pid); |
| 1135 | if (memoryStat == null) { |
| 1136 | continue; |
| 1137 | } |
| 1138 | int uid = getUidForPid(pid); |
| 1139 | String processName = readCmdlineFromProcfs(pid); |
Rafal Slawik | 932c71e | 2018-11-30 16:10:32 +0000 | [diff] [blame] | 1140 | // Sometimes we get here processName that is not included in the whitelist. It comes |
| 1141 | // from forking the zygote for an app. We can ignore that sample because this process |
| 1142 | // is collected by ProcessMemoryState. |
| 1143 | if (!processNames.contains(processName)) { |
| 1144 | continue; |
| 1145 | } |
Rafal Slawik | 0862158 | 2018-10-15 14:53:07 +0100 | [diff] [blame] | 1146 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
Rafal Slawik | c376262 | 2018-11-07 12:07:26 +0000 | [diff] [blame] | 1147 | e.writeInt(uid); |
| 1148 | e.writeString(processName); |
| 1149 | e.writeLong(memoryStat.pgfault); |
| 1150 | e.writeLong(memoryStat.pgmajfault); |
| 1151 | e.writeLong(memoryStat.rssInBytes); |
Rafal Slawik | d03ae42 | 2018-11-20 11:27:45 +0000 | [diff] [blame] | 1152 | e.writeLong(0); // unused |
Rafal Slawik | c376262 | 2018-11-07 12:07:26 +0000 | [diff] [blame] | 1153 | e.writeLong(memoryStat.startTimeNanos); |
Rafal Slawik | 0862158 | 2018-10-15 14:53:07 +0100 | [diff] [blame] | 1154 | pulledData.add(e); |
| 1155 | } |
| 1156 | } |
| 1157 | |
Rafal Slawik | 3bea895 | 2018-11-15 12:39:33 +0000 | [diff] [blame] | 1158 | private void pullProcessMemoryHighWaterMark( |
| 1159 | int tagId, long elapsedNanos, long wallClockNanos, |
| 1160 | List<StatsLogEventWrapper> pulledData) { |
| 1161 | List<ProcessMemoryHighWaterMark> results = LocalServices.getService( |
| 1162 | ActivityManagerInternal.class).getMemoryHighWaterMarkForProcesses(); |
| 1163 | for (ProcessMemoryHighWaterMark processMemoryHighWaterMark : results) { |
| 1164 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
| 1165 | e.writeInt(processMemoryHighWaterMark.uid); |
| 1166 | e.writeString(processMemoryHighWaterMark.processName); |
| 1167 | e.writeLong(processMemoryHighWaterMark.rssHighWaterMarkInBytes); |
| 1168 | pulledData.add(e); |
| 1169 | } |
| 1170 | int[] pids = getPidsForCommands(MEMORY_INTERESTING_NATIVE_PROCESSES); |
| 1171 | for (int i = 0; i < pids.length; i++) { |
| 1172 | final int pid = pids[i]; |
| 1173 | final int uid = getUidForPid(pid); |
| 1174 | final String processName = readCmdlineFromProcfs(pid); |
| 1175 | final long rssHighWaterMarkInBytes = readRssHighWaterMarkFromProcfs(pid); |
| 1176 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
| 1177 | e.writeInt(uid); |
| 1178 | e.writeString(processName); |
| 1179 | e.writeLong(rssHighWaterMarkInBytes); |
| 1180 | pulledData.add(e); |
| 1181 | } |
Rafal Slawik | 44b8814 | 2018-12-15 16:48:09 +0000 | [diff] [blame] | 1182 | // Invoke rss_hwm_reset binary to reset RSS HWM counters for all processes. |
| 1183 | SystemProperties.set("sys.rss_hwm_reset.on", "1"); |
Rafal Slawik | 3bea895 | 2018-11-15 12:39:33 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
Rafal Slawik | d4e8757 | 2019-03-12 13:08:38 +0000 | [diff] [blame] | 1186 | private void pullSystemIonHeapSize( |
| 1187 | int tagId, long elapsedNanos, long wallClockNanos, |
| 1188 | List<StatsLogEventWrapper> pulledData) { |
| 1189 | final long systemIonHeapSizeInBytes = readSystemIonHeapSizeFromDebugfs(); |
| 1190 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
| 1191 | e.writeLong(systemIonHeapSizeInBytes); |
| 1192 | pulledData.add(e); |
| 1193 | } |
| 1194 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1195 | private void pullBinderCallsStats( |
| 1196 | int tagId, long elapsedNanos, long wallClockNanos, |
| 1197 | List<StatsLogEventWrapper> pulledData) { |
Olivier Gaillard | 289ba40 | 2018-07-24 18:50:13 +0100 | [diff] [blame] | 1198 | BinderCallsStatsService.Internal binderStats = |
| 1199 | LocalServices.getService(BinderCallsStatsService.Internal.class); |
Olivier Gaillard | 44a60a9 | 2018-07-25 22:53:55 +0100 | [diff] [blame] | 1200 | if (binderStats == null) { |
Olivier Gaillard | 59ed8d1 | 2019-02-08 09:52:35 +0000 | [diff] [blame] | 1201 | throw new IllegalStateException("binderStats is null"); |
Olivier Gaillard | 44a60a9 | 2018-07-25 22:53:55 +0100 | [diff] [blame] | 1202 | } |
| 1203 | |
Olivier Gaillard | 289ba40 | 2018-07-24 18:50:13 +0100 | [diff] [blame] | 1204 | List<ExportedCallStat> callStats = binderStats.getExportedCallStats(); |
Olivier Gaillard | d25f7a8 | 2018-09-12 14:28:48 +0100 | [diff] [blame] | 1205 | binderStats.reset(); |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 1206 | for (ExportedCallStat callStat : callStats) { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1207 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
Olivier Gaillard | 88de48f | 2018-10-18 10:21:21 +0100 | [diff] [blame] | 1208 | e.writeInt(callStat.workSourceUid); |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 1209 | e.writeString(callStat.className); |
| 1210 | e.writeString(callStat.methodName); |
| 1211 | e.writeLong(callStat.callCount); |
| 1212 | e.writeLong(callStat.exceptionCount); |
| 1213 | e.writeLong(callStat.latencyMicros); |
| 1214 | e.writeLong(callStat.maxLatencyMicros); |
| 1215 | e.writeLong(callStat.cpuTimeMicros); |
| 1216 | e.writeLong(callStat.maxCpuTimeMicros); |
| 1217 | e.writeLong(callStat.maxReplySizeBytes); |
| 1218 | e.writeLong(callStat.maxRequestSizeBytes); |
Olivier Gaillard | 6f52d15 | 2018-07-25 12:13:12 +0100 | [diff] [blame] | 1219 | e.writeLong(callStat.recordedCallCount); |
| 1220 | e.writeInt(callStat.screenInteractive ? 1 : 0); |
Olivier Gaillard | 720ec5b | 2018-10-30 17:32:56 +0000 | [diff] [blame] | 1221 | e.writeInt(callStat.callingUid); |
Olivier Gaillard | 6f52d15 | 2018-07-25 12:13:12 +0100 | [diff] [blame] | 1222 | pulledData.add(e); |
| 1223 | } |
| 1224 | } |
| 1225 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1226 | private void pullBinderCallsStatsExceptions( |
| 1227 | int tagId, long elapsedNanos, long wallClockNanos, |
| 1228 | List<StatsLogEventWrapper> pulledData) { |
Olivier Gaillard | 6f52d15 | 2018-07-25 12:13:12 +0100 | [diff] [blame] | 1229 | BinderCallsStatsService.Internal binderStats = |
| 1230 | LocalServices.getService(BinderCallsStatsService.Internal.class); |
Olivier Gaillard | 44a60a9 | 2018-07-25 22:53:55 +0100 | [diff] [blame] | 1231 | if (binderStats == null) { |
Olivier Gaillard | 59ed8d1 | 2019-02-08 09:52:35 +0000 | [diff] [blame] | 1232 | throw new IllegalStateException("binderStats is null"); |
Olivier Gaillard | 44a60a9 | 2018-07-25 22:53:55 +0100 | [diff] [blame] | 1233 | } |
| 1234 | |
Olivier Gaillard | 6f52d15 | 2018-07-25 12:13:12 +0100 | [diff] [blame] | 1235 | ArrayMap<String, Integer> exceptionStats = binderStats.getExportedExceptionStats(); |
Olivier Gaillard | d25f7a8 | 2018-09-12 14:28:48 +0100 | [diff] [blame] | 1236 | // TODO: decouple binder calls exceptions with the rest of the binder calls data so that we |
| 1237 | // can reset the exception stats. |
Olivier Gaillard | 6f52d15 | 2018-07-25 12:13:12 +0100 | [diff] [blame] | 1238 | for (Entry<String, Integer> entry : exceptionStats.entrySet()) { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1239 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
Olivier Gaillard | 6f52d15 | 2018-07-25 12:13:12 +0100 | [diff] [blame] | 1240 | e.writeString(entry.getKey()); |
| 1241 | e.writeInt(entry.getValue()); |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 1242 | pulledData.add(e); |
| 1243 | } |
| 1244 | } |
| 1245 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1246 | private void pullLooperStats(int tagId, long elapsedNanos, long wallClockNanos, |
| 1247 | List<StatsLogEventWrapper> pulledData) { |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 1248 | LooperStats looperStats = LocalServices.getService(LooperStats.class); |
| 1249 | if (looperStats == null) { |
Olivier Gaillard | 59ed8d1 | 2019-02-08 09:52:35 +0000 | [diff] [blame] | 1250 | throw new IllegalStateException("looperStats null"); |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | List<LooperStats.ExportedEntry> entries = looperStats.getEntries(); |
Marcin Oczeretko | 3e6494e | 2018-09-10 18:06:52 +0100 | [diff] [blame] | 1254 | looperStats.reset(); |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 1255 | for (LooperStats.ExportedEntry entry : entries) { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1256 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
Marcin Oczeretko | ec75872 | 2018-09-12 12:53:47 +0100 | [diff] [blame] | 1257 | e.writeInt(entry.workSourceUid); |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 1258 | e.writeString(entry.handlerClassName); |
| 1259 | e.writeString(entry.threadName); |
| 1260 | e.writeString(entry.messageName); |
| 1261 | e.writeLong(entry.messageCount); |
| 1262 | e.writeLong(entry.exceptionCount); |
| 1263 | e.writeLong(entry.recordedMessageCount); |
| 1264 | e.writeLong(entry.totalLatencyMicros); |
| 1265 | e.writeLong(entry.cpuUsageMicros); |
Marcin Oczeretko | 3e6494e | 2018-09-10 18:06:52 +0100 | [diff] [blame] | 1266 | e.writeBoolean(entry.isInteractive); |
Marcin Oczeretko | c06331a | 2018-10-02 13:00:38 +0100 | [diff] [blame] | 1267 | e.writeLong(entry.maxCpuUsageMicros); |
| 1268 | e.writeLong(entry.maxLatencyMicros); |
| 1269 | e.writeLong(entry.recordedDelayMessageCount); |
| 1270 | e.writeLong(entry.delayMillis); |
| 1271 | e.writeLong(entry.maxDelayMillis); |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 1272 | pulledData.add(e); |
| 1273 | } |
| 1274 | } |
| 1275 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1276 | private void pullDiskStats(int tagId, long elapsedNanos, long wallClockNanos, |
| 1277 | List<StatsLogEventWrapper> pulledData) { |
Tej Singh | 86dc9db | 2018-09-06 00:39:57 +0000 | [diff] [blame] | 1278 | // Run a quick-and-dirty performance test: write 512 bytes |
| 1279 | byte[] junk = new byte[512]; |
| 1280 | for (int i = 0; i < junk.length; i++) junk[i] = (byte) i; // Write nonzero bytes |
| 1281 | |
| 1282 | File tmp = new File(Environment.getDataDirectory(), "system/statsdperftest.tmp"); |
| 1283 | FileOutputStream fos = null; |
| 1284 | IOException error = null; |
| 1285 | |
| 1286 | long before = SystemClock.elapsedRealtime(); |
| 1287 | try { |
| 1288 | fos = new FileOutputStream(tmp); |
| 1289 | fos.write(junk); |
| 1290 | } catch (IOException e) { |
| 1291 | error = e; |
| 1292 | } finally { |
| 1293 | try { |
| 1294 | if (fos != null) fos.close(); |
| 1295 | } catch (IOException e) { |
| 1296 | // Do nothing. |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | long latency = SystemClock.elapsedRealtime() - before; |
| 1301 | if (tmp.exists()) tmp.delete(); |
| 1302 | |
| 1303 | if (error != null) { |
| 1304 | Slog.e(TAG, "Error performing diskstats latency test"); |
| 1305 | latency = -1; |
| 1306 | } |
| 1307 | // File based encryption. |
| 1308 | boolean fileBased = StorageManager.isFileEncryptedNativeOnly(); |
| 1309 | |
| 1310 | //Recent disk write speed. Binder call to storaged. |
| 1311 | int writeSpeed = -1; |
| 1312 | try { |
| 1313 | IBinder binder = ServiceManager.getService("storaged"); |
| 1314 | if (binder == null) { |
| 1315 | Slog.e(TAG, "storaged not found"); |
| 1316 | } |
| 1317 | IStoraged storaged = IStoraged.Stub.asInterface(binder); |
| 1318 | writeSpeed = storaged.getRecentPerf(); |
| 1319 | } catch (RemoteException e) { |
| 1320 | Slog.e(TAG, "storaged not found"); |
| 1321 | } |
| 1322 | |
| 1323 | // Add info pulledData. |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1324 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
Tej Singh | 86dc9db | 2018-09-06 00:39:57 +0000 | [diff] [blame] | 1325 | e.writeLong(latency); |
| 1326 | e.writeBoolean(fileBased); |
| 1327 | e.writeInt(writeSpeed); |
| 1328 | pulledData.add(e); |
| 1329 | } |
| 1330 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1331 | private void pullDirectoryUsage(int tagId, long elapsedNanos, long wallClockNanos, |
| 1332 | List<StatsLogEventWrapper> pulledData) { |
Tej Singh | 86dc9db | 2018-09-06 00:39:57 +0000 | [diff] [blame] | 1333 | StatFs statFsData = new StatFs(Environment.getDataDirectory().getAbsolutePath()); |
| 1334 | StatFs statFsSystem = new StatFs(Environment.getRootDirectory().getAbsolutePath()); |
| 1335 | StatFs statFsCache = new StatFs(Environment.getDownloadCacheDirectory().getAbsolutePath()); |
| 1336 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1337 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
Tej Singh | 86dc9db | 2018-09-06 00:39:57 +0000 | [diff] [blame] | 1338 | e.writeInt(StatsLog.DIRECTORY_USAGE__DIRECTORY__DATA); |
| 1339 | e.writeLong(statFsData.getAvailableBytes()); |
| 1340 | e.writeLong(statFsData.getTotalBytes()); |
| 1341 | pulledData.add(e); |
| 1342 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1343 | e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
Tej Singh | 86dc9db | 2018-09-06 00:39:57 +0000 | [diff] [blame] | 1344 | e.writeInt(StatsLog.DIRECTORY_USAGE__DIRECTORY__CACHE); |
| 1345 | e.writeLong(statFsCache.getAvailableBytes()); |
| 1346 | e.writeLong(statFsCache.getTotalBytes()); |
| 1347 | pulledData.add(e); |
| 1348 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1349 | e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
Tej Singh | 86dc9db | 2018-09-06 00:39:57 +0000 | [diff] [blame] | 1350 | e.writeInt(StatsLog.DIRECTORY_USAGE__DIRECTORY__SYSTEM); |
| 1351 | e.writeLong(statFsSystem.getAvailableBytes()); |
| 1352 | e.writeLong(statFsSystem.getTotalBytes()); |
| 1353 | pulledData.add(e); |
| 1354 | } |
| 1355 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1356 | private void pullAppSize(int tagId, long elapsedNanos, long wallClockNanos, |
| 1357 | List<StatsLogEventWrapper> pulledData) { |
Tej Singh | 86dc9db | 2018-09-06 00:39:57 +0000 | [diff] [blame] | 1358 | try { |
| 1359 | String jsonStr = IoUtils.readFileAsString(DiskStatsLoggingService.DUMPSYS_CACHE_PATH); |
| 1360 | JSONObject json = new JSONObject(jsonStr); |
| 1361 | long cache_time = json.optLong(DiskStatsFileLogger.LAST_QUERY_TIMESTAMP_KEY, -1L); |
| 1362 | JSONArray pkg_names = json.getJSONArray(DiskStatsFileLogger.PACKAGE_NAMES_KEY); |
| 1363 | JSONArray app_sizes = json.getJSONArray(DiskStatsFileLogger.APP_SIZES_KEY); |
| 1364 | JSONArray app_data_sizes = json.getJSONArray(DiskStatsFileLogger.APP_DATA_KEY); |
| 1365 | JSONArray app_cache_sizes = json.getJSONArray(DiskStatsFileLogger.APP_CACHES_KEY); |
| 1366 | // Sanity check: Ensure all 4 lists have the same length. |
| 1367 | int length = pkg_names.length(); |
| 1368 | if (app_sizes.length() != length || app_data_sizes.length() != length |
| 1369 | || app_cache_sizes.length() != length) { |
| 1370 | Slog.e(TAG, "formatting error in diskstats cache file!"); |
| 1371 | return; |
| 1372 | } |
| 1373 | for (int i = 0; i < length; i++) { |
| 1374 | StatsLogEventWrapper e = |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1375 | new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
Tej Singh | 86dc9db | 2018-09-06 00:39:57 +0000 | [diff] [blame] | 1376 | e.writeString(pkg_names.getString(i)); |
| 1377 | e.writeLong(app_sizes.optLong(i, -1L)); |
| 1378 | e.writeLong(app_data_sizes.optLong(i, -1L)); |
| 1379 | e.writeLong(app_cache_sizes.optLong(i, -1L)); |
| 1380 | e.writeLong(cache_time); |
| 1381 | pulledData.add(e); |
| 1382 | } |
| 1383 | } catch (IOException | JSONException e) { |
| 1384 | Slog.e(TAG, "exception reading diskstats cache file", e); |
| 1385 | } |
| 1386 | } |
| 1387 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1388 | private void pullCategorySize(int tagId, long elapsedNanos, long wallClockNanos, |
| 1389 | List<StatsLogEventWrapper> pulledData) { |
Tej Singh | 86dc9db | 2018-09-06 00:39:57 +0000 | [diff] [blame] | 1390 | try { |
| 1391 | String jsonStr = IoUtils.readFileAsString(DiskStatsLoggingService.DUMPSYS_CACHE_PATH); |
| 1392 | JSONObject json = new JSONObject(jsonStr); |
| 1393 | long cacheTime = json.optLong(DiskStatsFileLogger.LAST_QUERY_TIMESTAMP_KEY, -1L); |
| 1394 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1395 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
Tej Singh | 86dc9db | 2018-09-06 00:39:57 +0000 | [diff] [blame] | 1396 | e.writeInt(StatsLog.CATEGORY_SIZE__CATEGORY__APP_SIZE); |
| 1397 | e.writeLong(json.optLong(DiskStatsFileLogger.APP_SIZE_AGG_KEY, -1L)); |
| 1398 | e.writeLong(cacheTime); |
| 1399 | pulledData.add(e); |
| 1400 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1401 | e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
Tej Singh | 86dc9db | 2018-09-06 00:39:57 +0000 | [diff] [blame] | 1402 | e.writeInt(StatsLog.CATEGORY_SIZE__CATEGORY__APP_DATA_SIZE); |
| 1403 | e.writeLong(json.optLong(DiskStatsFileLogger.APP_DATA_SIZE_AGG_KEY, -1L)); |
| 1404 | e.writeLong(cacheTime); |
| 1405 | pulledData.add(e); |
| 1406 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1407 | e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
Tej Singh | 86dc9db | 2018-09-06 00:39:57 +0000 | [diff] [blame] | 1408 | e.writeInt(StatsLog.CATEGORY_SIZE__CATEGORY__APP_CACHE_SIZE); |
| 1409 | e.writeLong(json.optLong(DiskStatsFileLogger.APP_CACHE_AGG_KEY, -1L)); |
| 1410 | e.writeLong(cacheTime); |
| 1411 | pulledData.add(e); |
| 1412 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1413 | e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
Tej Singh | 86dc9db | 2018-09-06 00:39:57 +0000 | [diff] [blame] | 1414 | e.writeInt(StatsLog.CATEGORY_SIZE__CATEGORY__PHOTOS); |
| 1415 | e.writeLong(json.optLong(DiskStatsFileLogger.PHOTOS_KEY, -1L)); |
| 1416 | e.writeLong(cacheTime); |
| 1417 | pulledData.add(e); |
| 1418 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1419 | e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
Tej Singh | 86dc9db | 2018-09-06 00:39:57 +0000 | [diff] [blame] | 1420 | e.writeInt(StatsLog.CATEGORY_SIZE__CATEGORY__VIDEOS); |
| 1421 | e.writeLong(json.optLong(DiskStatsFileLogger.VIDEOS_KEY, -1L)); |
| 1422 | e.writeLong(cacheTime); |
| 1423 | pulledData.add(e); |
| 1424 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1425 | e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
Tej Singh | 86dc9db | 2018-09-06 00:39:57 +0000 | [diff] [blame] | 1426 | e.writeInt(StatsLog.CATEGORY_SIZE__CATEGORY__AUDIO); |
| 1427 | e.writeLong(json.optLong(DiskStatsFileLogger.AUDIO_KEY, -1L)); |
| 1428 | e.writeLong(cacheTime); |
| 1429 | pulledData.add(e); |
| 1430 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1431 | e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
Tej Singh | 86dc9db | 2018-09-06 00:39:57 +0000 | [diff] [blame] | 1432 | e.writeInt(StatsLog.CATEGORY_SIZE__CATEGORY__DOWNLOADS); |
| 1433 | e.writeLong(json.optLong(DiskStatsFileLogger.DOWNLOADS_KEY, -1L)); |
| 1434 | e.writeLong(cacheTime); |
| 1435 | pulledData.add(e); |
| 1436 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1437 | e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
Tej Singh | 86dc9db | 2018-09-06 00:39:57 +0000 | [diff] [blame] | 1438 | e.writeInt(StatsLog.CATEGORY_SIZE__CATEGORY__SYSTEM); |
| 1439 | e.writeLong(json.optLong(DiskStatsFileLogger.SYSTEM_KEY, -1L)); |
| 1440 | e.writeLong(cacheTime); |
| 1441 | pulledData.add(e); |
| 1442 | |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1443 | e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
Tej Singh | 86dc9db | 2018-09-06 00:39:57 +0000 | [diff] [blame] | 1444 | e.writeInt(StatsLog.CATEGORY_SIZE__CATEGORY__OTHER); |
| 1445 | e.writeLong(json.optLong(DiskStatsFileLogger.MISC_KEY, -1L)); |
| 1446 | e.writeLong(cacheTime); |
| 1447 | pulledData.add(e); |
| 1448 | } catch (IOException | JSONException e) { |
| 1449 | Slog.e(TAG, "exception reading diskstats cache file", e); |
| 1450 | } |
| 1451 | } |
| 1452 | |
Kevin Chyn | 6ecc570 | 2019-01-28 12:00:37 -0800 | [diff] [blame] | 1453 | private void pullNumBiometricsEnrolled(int modality, int tagId, long elapsedNanos, |
| 1454 | long wallClockNanos, List<StatsLogEventWrapper> pulledData) { |
Tej Singh | d6d6d77 | 2018-09-05 17:41:25 -0700 | [diff] [blame] | 1455 | FingerprintManager fingerprintManager = mContext.getSystemService(FingerprintManager.class); |
Kevin Chyn | 6ecc570 | 2019-01-28 12:00:37 -0800 | [diff] [blame] | 1456 | FaceManager faceManager = mContext.getSystemService(FaceManager.class); |
| 1457 | if (modality == BiometricsProtoEnums.MODALITY_FINGERPRINT && fingerprintManager == null) { |
| 1458 | return; |
| 1459 | } |
| 1460 | if (modality == BiometricsProtoEnums.MODALITY_FACE && faceManager == null) { |
Tej Singh | d6d6d77 | 2018-09-05 17:41:25 -0700 | [diff] [blame] | 1461 | return; |
| 1462 | } |
| 1463 | UserManager userManager = mContext.getSystemService(UserManager.class); |
| 1464 | if (userManager == null) { |
| 1465 | return; |
| 1466 | } |
Kevin Chyn | 6ecc570 | 2019-01-28 12:00:37 -0800 | [diff] [blame] | 1467 | |
Tej Singh | d6d6d77 | 2018-09-05 17:41:25 -0700 | [diff] [blame] | 1468 | final long token = Binder.clearCallingIdentity(); |
Tej Singh | d6d6d77 | 2018-09-05 17:41:25 -0700 | [diff] [blame] | 1469 | for (UserInfo user : userManager.getUsers()) { |
| 1470 | final int userId = user.getUserHandle().getIdentifier(); |
Kevin Chyn | 6ecc570 | 2019-01-28 12:00:37 -0800 | [diff] [blame] | 1471 | int numEnrolled = 0; |
| 1472 | if (modality == BiometricsProtoEnums.MODALITY_FINGERPRINT) { |
| 1473 | numEnrolled = fingerprintManager.getEnrolledFingerprints(userId).size(); |
| 1474 | } else if (modality == BiometricsProtoEnums.MODALITY_FACE) { |
| 1475 | numEnrolled = faceManager.getEnrolledFaces(userId).size(); |
| 1476 | } else { |
| 1477 | return; |
| 1478 | } |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1479 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
Tej Singh | d6d6d77 | 2018-09-05 17:41:25 -0700 | [diff] [blame] | 1480 | e.writeInt(userId); |
Kevin Chyn | 6ecc570 | 2019-01-28 12:00:37 -0800 | [diff] [blame] | 1481 | e.writeInt(numEnrolled); |
Tej Singh | d6d6d77 | 2018-09-05 17:41:25 -0700 | [diff] [blame] | 1482 | pulledData.add(e); |
| 1483 | } |
| 1484 | Binder.restoreCallingIdentity(token); |
| 1485 | } |
| 1486 | |
Chenjie Yu | b52779e | 2018-10-05 12:03:36 -0700 | [diff] [blame] | 1487 | // read high watermark for section |
| 1488 | private long readProcStatsHighWaterMark(int section) { |
Chenjie Yu | 2c0d83d | 2018-09-02 06:37:08 -0700 | [diff] [blame] | 1489 | try { |
Chenjie Yu | b52779e | 2018-10-05 12:03:36 -0700 | [diff] [blame] | 1490 | File[] files = mBaseDir.listFiles((d, name) -> { |
| 1491 | return name.toLowerCase().startsWith(String.valueOf(section) + '_'); |
| 1492 | }); |
Chenjie Yu | 2c0d83d | 2018-09-02 06:37:08 -0700 | [diff] [blame] | 1493 | if (files == null || files.length == 0) { |
| 1494 | return 0; |
| 1495 | } |
| 1496 | if (files.length > 1) { |
| 1497 | Log.e(TAG, "Only 1 file expected for high water mark. Found " + files.length); |
| 1498 | } |
Chenjie Yu | b52779e | 2018-10-05 12:03:36 -0700 | [diff] [blame] | 1499 | return Long.valueOf(files[0].getName().split("_")[1]); |
Chenjie Yu | 2c0d83d | 2018-09-02 06:37:08 -0700 | [diff] [blame] | 1500 | } catch (SecurityException e) { |
| 1501 | Log.e(TAG, "Failed to get procstats high watermark file.", e); |
| 1502 | } catch (NumberFormatException e) { |
| 1503 | Log.e(TAG, "Failed to parse file name.", e); |
| 1504 | } |
| 1505 | return 0; |
| 1506 | } |
| 1507 | |
| 1508 | private IProcessStats mProcessStats = |
| 1509 | IProcessStats.Stub.asInterface(ServiceManager.getService(ProcessStats.SERVICE_NAME)); |
| 1510 | |
Chenjie Yu | b52779e | 2018-10-05 12:03:36 -0700 | [diff] [blame] | 1511 | private void pullProcessStats(int section, int tagId, long elapsedNanos, long wallClockNanos, |
Chenjie Yu | 2c0d83d | 2018-09-02 06:37:08 -0700 | [diff] [blame] | 1512 | List<StatsLogEventWrapper> pulledData) { |
Yangster-mac | 9554324 | 2018-10-22 11:44:03 -0700 | [diff] [blame] | 1513 | synchronized (this) { |
| 1514 | try { |
| 1515 | long lastHighWaterMark = readProcStatsHighWaterMark(section); |
| 1516 | List<ParcelFileDescriptor> statsFiles = new ArrayList<>(); |
| 1517 | long highWaterMark = mProcessStats.getCommittedStats( |
| 1518 | lastHighWaterMark, section, true, statsFiles); |
| 1519 | if (statsFiles.size() != 1) { |
| 1520 | return; |
| 1521 | } |
| 1522 | InputStream stream = new ParcelFileDescriptor.AutoCloseInputStream( |
| 1523 | statsFiles.get(0)); |
| 1524 | int[] len = new int[1]; |
| 1525 | byte[] stats = readFully(stream, len); |
| 1526 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, |
| 1527 | wallClockNanos); |
| 1528 | e.writeStorage(Arrays.copyOf(stats, len[0])); |
| 1529 | pulledData.add(e); |
| 1530 | new File(mBaseDir.getAbsolutePath() + "/" + section + "_" |
| 1531 | + lastHighWaterMark).delete(); |
| 1532 | new File( |
| 1533 | mBaseDir.getAbsolutePath() + "/" + section + "_" |
| 1534 | + highWaterMark).createNewFile(); |
| 1535 | } catch (IOException e) { |
| 1536 | Log.e(TAG, "Getting procstats failed: ", e); |
| 1537 | } catch (RemoteException e) { |
| 1538 | Log.e(TAG, "Getting procstats failed: ", e); |
| 1539 | } catch (SecurityException e) { |
| 1540 | Log.e(TAG, "Getting procstats failed: ", e); |
Chenjie Yu | 2c0d83d | 2018-09-02 06:37:08 -0700 | [diff] [blame] | 1541 | } |
Chenjie Yu | 2c0d83d | 2018-09-02 06:37:08 -0700 | [diff] [blame] | 1542 | } |
| 1543 | } |
| 1544 | |
| 1545 | static byte[] readFully(InputStream stream, int[] outLen) throws IOException { |
| 1546 | int pos = 0; |
| 1547 | final int initialAvail = stream.available(); |
| 1548 | byte[] data = new byte[initialAvail > 0 ? (initialAvail + 1) : 16384]; |
| 1549 | while (true) { |
| 1550 | int amt = stream.read(data, pos, data.length - pos); |
| 1551 | if (DEBUG) { |
| 1552 | Slog.i(TAG, "Read " + amt + " bytes at " + pos + " of avail " + data.length); |
| 1553 | } |
| 1554 | if (amt < 0) { |
| 1555 | if (DEBUG) { |
| 1556 | Slog.i(TAG, "**** FINISHED READING: pos=" + pos + " len=" + data.length); |
| 1557 | } |
| 1558 | outLen[0] = pos; |
| 1559 | return data; |
| 1560 | } |
| 1561 | pos += amt; |
| 1562 | if (pos >= data.length) { |
| 1563 | byte[] newData = new byte[pos + 16384]; |
| 1564 | if (DEBUG) { |
| 1565 | Slog.i(TAG, "Copying " + pos + " bytes to new array len " + newData.length); |
| 1566 | } |
| 1567 | System.arraycopy(data, 0, newData, 0, pos); |
| 1568 | data = newData; |
| 1569 | } |
| 1570 | } |
| 1571 | } |
| 1572 | |
Chenjie Yu | ab53020 | 2018-09-26 12:39:20 -0700 | [diff] [blame] | 1573 | private void pullPowerProfile( |
| 1574 | int tagId, long elapsedNanos, long wallClockNanos, |
| 1575 | List<StatsLogEventWrapper> pulledData) { |
| 1576 | PowerProfile powerProfile = new PowerProfile(mContext); |
| 1577 | checkNotNull(powerProfile); |
| 1578 | |
| 1579 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, |
| 1580 | wallClockNanos); |
| 1581 | ProtoOutputStream proto = new ProtoOutputStream(); |
| 1582 | powerProfile.writeToProto(proto); |
| 1583 | proto.flush(); |
| 1584 | e.writeStorage(proto.getBytes()); |
| 1585 | pulledData.add(e); |
| 1586 | } |
| 1587 | |
Bookatz | 366a443 | 2018-11-20 09:42:33 -0800 | [diff] [blame] | 1588 | private void pullBuildInformation(int tagId, |
| 1589 | long elapsedNanos, long wallClockNanos, List<StatsLogEventWrapper> pulledData) { |
| 1590 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
| 1591 | e.writeString(Build.FINGERPRINT); |
| 1592 | e.writeString(Build.BRAND); |
| 1593 | e.writeString(Build.PRODUCT); |
| 1594 | e.writeString(Build.DEVICE); |
| 1595 | e.writeString(Build.VERSION.RELEASE); |
| 1596 | e.writeString(Build.ID); |
| 1597 | e.writeString(Build.VERSION.INCREMENTAL); |
| 1598 | e.writeString(Build.TYPE); |
| 1599 | e.writeString(Build.TAGS); |
| 1600 | pulledData.add(e); |
| 1601 | } |
| 1602 | |
Bookatz | 75ee604 | 2018-11-09 12:27:37 -0800 | [diff] [blame] | 1603 | private BatteryStatsHelper getBatteryStatsHelper() { |
| 1604 | if (mBatteryStatsHelper == null) { |
| 1605 | final long callingToken = Binder.clearCallingIdentity(); |
| 1606 | try { |
| 1607 | // clearCallingIdentity required for BatteryStatsHelper.checkWifiOnly(). |
| 1608 | mBatteryStatsHelper = new BatteryStatsHelper(mContext, false); |
| 1609 | } finally { |
| 1610 | Binder.restoreCallingIdentity(callingToken); |
| 1611 | } |
| 1612 | mBatteryStatsHelper.create((Bundle) null); |
| 1613 | } |
| 1614 | long currentTime = SystemClock.elapsedRealtime(); |
| 1615 | if (currentTime - mBatteryStatsHelperTimestampMs >= MAX_BATTERY_STATS_HELPER_FREQUENCY_MS) { |
| 1616 | // Load BatteryStats and do all the calculations. |
| 1617 | mBatteryStatsHelper.refreshStats(BatteryStats.STATS_SINCE_CHARGED, UserHandle.USER_ALL); |
| 1618 | // Calculations are done so we don't need to save the raw BatteryStats data in RAM. |
| 1619 | mBatteryStatsHelper.clearStats(); |
| 1620 | mBatteryStatsHelperTimestampMs = currentTime; |
| 1621 | } |
| 1622 | return mBatteryStatsHelper; |
| 1623 | } |
| 1624 | |
Bookatz | 3dbc13a | 2018-12-21 12:16:51 -0800 | [diff] [blame] | 1625 | private long milliAmpHrsToNanoAmpSecs(double mAh) { |
| 1626 | final long MILLI_AMP_HR_TO_NANO_AMP_SECS = 1_000_000L * 3600L; |
| 1627 | return (long) (mAh * MILLI_AMP_HR_TO_NANO_AMP_SECS + 0.5); |
| 1628 | } |
| 1629 | |
Bookatz | 75ee604 | 2018-11-09 12:27:37 -0800 | [diff] [blame] | 1630 | private void pullDeviceCalculatedPowerUse(int tagId, |
| 1631 | long elapsedNanos, final long wallClockNanos, List<StatsLogEventWrapper> pulledData) { |
| 1632 | BatteryStatsHelper bsHelper = getBatteryStatsHelper(); |
| 1633 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
Bookatz | 3dbc13a | 2018-12-21 12:16:51 -0800 | [diff] [blame] | 1634 | e.writeLong(milliAmpHrsToNanoAmpSecs(bsHelper.getComputedPower())); |
Bookatz | 75ee604 | 2018-11-09 12:27:37 -0800 | [diff] [blame] | 1635 | pulledData.add(e); |
| 1636 | } |
| 1637 | |
| 1638 | private void pullDeviceCalculatedPowerBlameUid(int tagId, |
| 1639 | long elapsedNanos, final long wallClockNanos, List<StatsLogEventWrapper> pulledData) { |
| 1640 | final List<BatterySipper> sippers = getBatteryStatsHelper().getUsageList(); |
| 1641 | if (sippers == null) { |
| 1642 | return; |
| 1643 | } |
| 1644 | for (BatterySipper bs : sippers) { |
| 1645 | if (bs.drainType != bs.drainType.APP) { |
| 1646 | continue; |
| 1647 | } |
| 1648 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
| 1649 | e.writeInt(bs.uidObj.getUid()); |
Bookatz | 3dbc13a | 2018-12-21 12:16:51 -0800 | [diff] [blame] | 1650 | e.writeLong(milliAmpHrsToNanoAmpSecs(bs.totalPowerMah)); |
Bookatz | 75ee604 | 2018-11-09 12:27:37 -0800 | [diff] [blame] | 1651 | pulledData.add(e); |
| 1652 | } |
| 1653 | } |
| 1654 | |
| 1655 | private void pullDeviceCalculatedPowerBlameOther(int tagId, |
| 1656 | long elapsedNanos, final long wallClockNanos, List<StatsLogEventWrapper> pulledData) { |
| 1657 | final List<BatterySipper> sippers = getBatteryStatsHelper().getUsageList(); |
| 1658 | if (sippers == null) { |
| 1659 | return; |
| 1660 | } |
| 1661 | for (BatterySipper bs : sippers) { |
| 1662 | if (bs.drainType == bs.drainType.APP) { |
| 1663 | continue; // This is a separate atom; see pullDeviceCalculatedPowerBlameUid(). |
| 1664 | } |
| 1665 | if (bs.drainType == bs.drainType.USER) { |
| 1666 | continue; // This is not supported. We purposefully calculate over USER_ALL. |
| 1667 | } |
| 1668 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
| 1669 | e.writeInt(bs.drainType.ordinal()); |
Bookatz | 3dbc13a | 2018-12-21 12:16:51 -0800 | [diff] [blame] | 1670 | e.writeLong(milliAmpHrsToNanoAmpSecs(bs.totalPowerMah)); |
Bookatz | 75ee604 | 2018-11-09 12:27:37 -0800 | [diff] [blame] | 1671 | pulledData.add(e); |
| 1672 | } |
| 1673 | } |
| 1674 | |
Tej Singh | e7726dc | 2018-09-21 11:42:12 -0700 | [diff] [blame] | 1675 | private void pullDiskIo(int tagId, long elapsedNanos, final long wallClockNanos, |
| 1676 | List<StatsLogEventWrapper> pulledData) { |
| 1677 | mStoragedUidIoStatsReader.readAbsolute((uid, fgCharsRead, fgCharsWrite, fgBytesRead, |
| 1678 | fgBytesWrite, bgCharsRead, bgCharsWrite, bgBytesRead, bgBytesWrite, |
| 1679 | fgFsync, bgFsync) -> { |
Chenjie Yu | ab53020 | 2018-09-26 12:39:20 -0700 | [diff] [blame] | 1680 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, |
| 1681 | wallClockNanos); |
Tej Singh | e7726dc | 2018-09-21 11:42:12 -0700 | [diff] [blame] | 1682 | e.writeInt(uid); |
| 1683 | e.writeLong(fgCharsRead); |
| 1684 | e.writeLong(fgCharsWrite); |
| 1685 | e.writeLong(fgBytesRead); |
| 1686 | e.writeLong(fgBytesWrite); |
| 1687 | e.writeLong(bgCharsRead); |
| 1688 | e.writeLong(bgCharsWrite); |
| 1689 | e.writeLong(bgBytesRead); |
| 1690 | e.writeLong(bgBytesWrite); |
| 1691 | e.writeLong(fgFsync); |
| 1692 | e.writeLong(bgFsync); |
| 1693 | pulledData.add(e); |
| 1694 | }); |
| 1695 | } |
| 1696 | |
Yangster-mac | 308ea0c | 2018-10-22 13:10:25 -0700 | [diff] [blame] | 1697 | private void pullProcessCpuTime(int tagId, long elapsedNanos, final long wallClockNanos, |
| 1698 | List<StatsLogEventWrapper> pulledData) { |
| 1699 | synchronized (this) { |
| 1700 | if (mProcessCpuTracker == null) { |
| 1701 | mProcessCpuTracker = new ProcessCpuTracker(false); |
| 1702 | mProcessCpuTracker.init(); |
| 1703 | } |
| 1704 | mProcessCpuTracker.update(); |
| 1705 | for (int i = 0; i < mProcessCpuTracker.countStats(); i++) { |
| 1706 | ProcessCpuTracker.Stats st = mProcessCpuTracker.getStats(i); |
| 1707 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, |
| 1708 | wallClockNanos); |
| 1709 | e.writeInt(st.uid); |
| 1710 | e.writeString(st.name); |
| 1711 | e.writeLong(st.base_utime); |
| 1712 | e.writeLong(st.base_stime); |
| 1713 | pulledData.add(e); |
| 1714 | } |
| 1715 | } |
| 1716 | } |
| 1717 | |
Misha Wagner | 5a51e00 | 2018-10-03 15:04:09 +0100 | [diff] [blame] | 1718 | private void pullCpuTimePerThreadFreq(int tagId, long elapsedNanos, long wallClockNanos, |
| 1719 | List<StatsLogEventWrapper> pulledData) { |
| 1720 | if (this.mKernelCpuThreadReader == null) { |
Olivier Gaillard | 59ed8d1 | 2019-02-08 09:52:35 +0000 | [diff] [blame] | 1721 | throw new IllegalStateException("mKernelCpuThreadReader is null"); |
Misha Wagner | 5a51e00 | 2018-10-03 15:04:09 +0100 | [diff] [blame] | 1722 | } |
Misha Wagner | dc79754 | 2018-10-29 13:21:34 +0000 | [diff] [blame] | 1723 | ArrayList<KernelCpuThreadReader.ProcessCpuUsage> processCpuUsages = |
Misha Wagner | 3989eb0 | 2019-03-19 11:05:37 +0000 | [diff] [blame^] | 1724 | this.mKernelCpuThreadReader.getProcessCpuUsage(); |
Misha Wagner | dc79754 | 2018-10-29 13:21:34 +0000 | [diff] [blame] | 1725 | if (processCpuUsages == null) { |
Olivier Gaillard | 59ed8d1 | 2019-02-08 09:52:35 +0000 | [diff] [blame] | 1726 | throw new IllegalStateException("processCpuUsages is null"); |
Misha Wagner | 5a51e00 | 2018-10-03 15:04:09 +0100 | [diff] [blame] | 1727 | } |
| 1728 | int[] cpuFrequencies = mKernelCpuThreadReader.getCpuFrequenciesKhz(); |
Misha Wagner | 4b32c9f | 2019-01-25 15:30:14 +0000 | [diff] [blame] | 1729 | if (cpuFrequencies.length > CPU_TIME_PER_THREAD_FREQ_MAX_NUM_FREQUENCIES) { |
Olivier Gaillard | 59ed8d1 | 2019-02-08 09:52:35 +0000 | [diff] [blame] | 1730 | String message = "Expected maximum " + CPU_TIME_PER_THREAD_FREQ_MAX_NUM_FREQUENCIES |
| 1731 | + " frequencies, but got " + cpuFrequencies.length; |
| 1732 | Slog.w(TAG, message); |
| 1733 | throw new IllegalStateException(message); |
Misha Wagner | fde6958 | 2018-11-28 13:26:32 +0000 | [diff] [blame] | 1734 | } |
Misha Wagner | dc79754 | 2018-10-29 13:21:34 +0000 | [diff] [blame] | 1735 | for (int i = 0; i < processCpuUsages.size(); i++) { |
| 1736 | KernelCpuThreadReader.ProcessCpuUsage processCpuUsage = processCpuUsages.get(i); |
| 1737 | ArrayList<KernelCpuThreadReader.ThreadCpuUsage> threadCpuUsages = |
| 1738 | processCpuUsage.threadCpuUsages; |
| 1739 | for (int j = 0; j < threadCpuUsages.size(); j++) { |
| 1740 | KernelCpuThreadReader.ThreadCpuUsage threadCpuUsage = threadCpuUsages.get(j); |
| 1741 | if (threadCpuUsage.usageTimesMillis.length != cpuFrequencies.length) { |
Olivier Gaillard | 59ed8d1 | 2019-02-08 09:52:35 +0000 | [diff] [blame] | 1742 | String message = "Unexpected number of usage times," |
Misha Wagner | dc79754 | 2018-10-29 13:21:34 +0000 | [diff] [blame] | 1743 | + " expected " + cpuFrequencies.length |
Olivier Gaillard | 59ed8d1 | 2019-02-08 09:52:35 +0000 | [diff] [blame] | 1744 | + " but got " + threadCpuUsage.usageTimesMillis.length; |
| 1745 | Slog.w(TAG, message); |
| 1746 | throw new IllegalStateException(message); |
Misha Wagner | 5a51e00 | 2018-10-03 15:04:09 +0100 | [diff] [blame] | 1747 | } |
| 1748 | |
Misha Wagner | fde6958 | 2018-11-28 13:26:32 +0000 | [diff] [blame] | 1749 | StatsLogEventWrapper e = |
| 1750 | new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
| 1751 | e.writeInt(processCpuUsage.uid); |
| 1752 | e.writeInt(processCpuUsage.processId); |
| 1753 | e.writeInt(threadCpuUsage.threadId); |
| 1754 | e.writeString(processCpuUsage.processName); |
| 1755 | e.writeString(threadCpuUsage.threadName); |
Misha Wagner | 4b32c9f | 2019-01-25 15:30:14 +0000 | [diff] [blame] | 1756 | for (int k = 0; k < CPU_TIME_PER_THREAD_FREQ_MAX_NUM_FREQUENCIES; k++) { |
| 1757 | if (k < cpuFrequencies.length) { |
| 1758 | e.writeInt(cpuFrequencies[k]); |
| 1759 | e.writeInt(threadCpuUsage.usageTimesMillis[k]); |
| 1760 | } else { |
| 1761 | // If we have no more frequencies to write, we still must write empty data. |
| 1762 | // We know that this data is empty (and not just zero) because all |
| 1763 | // frequencies are expected to be greater than zero |
| 1764 | e.writeInt(0); |
| 1765 | e.writeInt(0); |
| 1766 | } |
Misha Wagner | dc79754 | 2018-10-29 13:21:34 +0000 | [diff] [blame] | 1767 | } |
Misha Wagner | fde6958 | 2018-11-28 13:26:32 +0000 | [diff] [blame] | 1768 | pulledData.add(e); |
Misha Wagner | 5a51e00 | 2018-10-03 15:04:09 +0100 | [diff] [blame] | 1769 | } |
| 1770 | } |
| 1771 | } |
| 1772 | |
Tej Singh | 9876a3b | 2019-01-11 14:28:35 -0800 | [diff] [blame] | 1773 | private void pullTemperature(int tagId, long elapsedNanos, long wallClockNanos, |
| 1774 | List<StatsLogEventWrapper> pulledData) { |
| 1775 | long callingToken = Binder.clearCallingIdentity(); |
| 1776 | try { |
| 1777 | List<Temperature> temperatures = sThermalService.getCurrentTemperatures(); |
| 1778 | for (Temperature temp : temperatures) { |
| 1779 | StatsLogEventWrapper e = |
| 1780 | new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
| 1781 | e.writeInt(temp.getType()); |
| 1782 | e.writeString(temp.getName()); |
| 1783 | e.writeInt((int) (temp.getValue() * 10)); |
| 1784 | pulledData.add(e); |
| 1785 | } |
| 1786 | } catch (RemoteException e) { |
| 1787 | // Should not happen. |
| 1788 | Slog.e(TAG, "Disconnected from thermal service. Cannot pull temperatures."); |
| 1789 | } finally { |
| 1790 | Binder.restoreCallingIdentity(callingToken); |
| 1791 | } |
| 1792 | } |
| 1793 | |
Olivier Gaillard | 18d45220 | 2019-01-22 15:03:41 +0000 | [diff] [blame] | 1794 | private void pullDebugElapsedClock(int tagId, |
| 1795 | long elapsedNanos, final long wallClockNanos, List<StatsLogEventWrapper> pulledData) { |
| 1796 | final long elapsedMillis = SystemClock.elapsedRealtime(); |
| 1797 | final long clockDiffMillis = mDebugElapsedClockPreviousValue == 0 |
| 1798 | ? 0 : elapsedMillis - mDebugElapsedClockPreviousValue; |
| 1799 | |
| 1800 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
| 1801 | e.writeLong(mDebugElapsedClockPullCount); |
| 1802 | e.writeLong(elapsedMillis); |
| 1803 | // Log it twice to be able to test multi-value aggregation from ValueMetric. |
| 1804 | e.writeLong(elapsedMillis); |
| 1805 | e.writeLong(clockDiffMillis); |
| 1806 | e.writeInt(1 /* always set */); |
| 1807 | pulledData.add(e); |
| 1808 | |
| 1809 | if (mDebugElapsedClockPullCount % 2 == 1) { |
| 1810 | StatsLogEventWrapper e2 = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
| 1811 | e2.writeLong(mDebugElapsedClockPullCount); |
| 1812 | e2.writeLong(elapsedMillis); |
| 1813 | // Log it twice to be able to test multi-value aggregation from ValueMetric. |
| 1814 | e2.writeLong(elapsedMillis); |
| 1815 | e2.writeLong(clockDiffMillis); |
| 1816 | e2.writeInt(2 /* set on odd pulls */); |
| 1817 | pulledData.add(e2); |
| 1818 | } |
| 1819 | |
| 1820 | mDebugElapsedClockPullCount++; |
| 1821 | mDebugElapsedClockPreviousValue = elapsedMillis; |
| 1822 | } |
| 1823 | |
| 1824 | private void pullDebugFailingElapsedClock(int tagId, |
| 1825 | long elapsedNanos, final long wallClockNanos, List<StatsLogEventWrapper> pulledData) { |
| 1826 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
| 1827 | final long elapsedMillis = SystemClock.elapsedRealtime(); |
Olivier Gaillard | 4f01a37 | 2019-02-04 14:25:14 +0000 | [diff] [blame] | 1828 | // Fails every 5 buckets. |
| 1829 | if (mDebugFailingElapsedClockPullCount++ % 5 == 0) { |
Olivier Gaillard | 18d45220 | 2019-01-22 15:03:41 +0000 | [diff] [blame] | 1830 | mDebugFailingElapsedClockPreviousValue = elapsedMillis; |
| 1831 | throw new RuntimeException("Failing debug elapsed clock"); |
| 1832 | } |
| 1833 | |
| 1834 | e.writeLong(mDebugFailingElapsedClockPullCount); |
| 1835 | e.writeLong(elapsedMillis); |
| 1836 | // Log it twice to be able to test multi-value aggregation from ValueMetric. |
| 1837 | e.writeLong(elapsedMillis); |
| 1838 | e.writeLong(mDebugFailingElapsedClockPreviousValue == 0 |
| 1839 | ? 0 : elapsedMillis - mDebugFailingElapsedClockPreviousValue); |
| 1840 | mDebugFailingElapsedClockPreviousValue = elapsedMillis; |
| 1841 | pulledData.add(e); |
| 1842 | } |
| 1843 | |
Philip P. Moltmann | f21a350 | 2019-01-30 09:53:04 -0800 | [diff] [blame] | 1844 | private void pullDangerousPermissionState(long elapsedNanos, final long wallClockNanos, |
| 1845 | List<StatsLogEventWrapper> pulledData) { |
| 1846 | long token = Binder.clearCallingIdentity(); |
| 1847 | try { |
| 1848 | PackageManager pm = mContext.getPackageManager(); |
| 1849 | |
| 1850 | List<UserInfo> users = mContext.getSystemService(UserManager.class).getUsers(); |
| 1851 | |
| 1852 | int numUsers = users.size(); |
| 1853 | for (int userNum = 0; userNum < numUsers; userNum++) { |
| 1854 | UserHandle user = users.get(userNum).getUserHandle(); |
| 1855 | |
| 1856 | List<PackageInfo> pkgs = pm.getInstalledPackagesAsUser( |
| 1857 | PackageManager.GET_PERMISSIONS, user.getIdentifier()); |
| 1858 | |
| 1859 | int numPkgs = pkgs.size(); |
| 1860 | for (int pkgNum = 0; pkgNum < numPkgs; pkgNum++) { |
| 1861 | PackageInfo pkg = pkgs.get(pkgNum); |
| 1862 | |
| 1863 | if (pkg.requestedPermissions == null) { |
| 1864 | continue; |
| 1865 | } |
| 1866 | |
| 1867 | int numPerms = pkg.requestedPermissions.length; |
| 1868 | for (int permNum = 0; permNum < numPerms; permNum++) { |
| 1869 | String permName = pkg.requestedPermissions[permNum]; |
| 1870 | |
| 1871 | PermissionInfo permissionInfo; |
| 1872 | try { |
| 1873 | permissionInfo = pm.getPermissionInfo(permName, 0); |
| 1874 | } catch (PackageManager.NameNotFoundException ignored) { |
| 1875 | continue; |
| 1876 | } |
| 1877 | |
| 1878 | if (permissionInfo.getProtection() != PROTECTION_DANGEROUS) { |
| 1879 | continue; |
| 1880 | } |
| 1881 | |
| 1882 | StatsLogEventWrapper e = new StatsLogEventWrapper( |
| 1883 | StatsLog.DANGEROUS_PERMISSION_STATE, elapsedNanos, wallClockNanos); |
| 1884 | |
| 1885 | e.writeString(permName); |
| 1886 | e.writeInt(pkg.applicationInfo.uid); |
| 1887 | e.writeString(pkg.packageName); |
| 1888 | |
| 1889 | e.writeBoolean((pkg.requestedPermissionsFlags[permNum] |
| 1890 | & REQUESTED_PERMISSION_GRANTED) != 0); |
| 1891 | |
| 1892 | pulledData.add(e); |
| 1893 | } |
| 1894 | } |
| 1895 | } |
| 1896 | } catch (Throwable t) { |
| 1897 | Log.e(TAG, "Could not read permissions", t); |
| 1898 | } finally { |
| 1899 | Binder.restoreCallingIdentity(token); |
| 1900 | } |
| 1901 | } |
| 1902 | |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 1903 | /** |
Philip P. Moltmann | 70b42ae | 2019-01-29 16:24:19 -0800 | [diff] [blame] | 1904 | * Add a RoleHolder atom for each package that holds a role. |
| 1905 | * |
| 1906 | * @param elapsedNanos the time since boot |
| 1907 | * @param wallClockNanos the time on the clock |
| 1908 | * @param pulledData the data sink to write to |
| 1909 | */ |
| 1910 | private void pullRoleHolders(long elapsedNanos, final long wallClockNanos, |
| 1911 | @NonNull List<StatsLogEventWrapper> pulledData) { |
| 1912 | long callingToken = Binder.clearCallingIdentity(); |
| 1913 | try { |
| 1914 | PackageManager pm = mContext.getPackageManager(); |
Hai Zhang | 85fd062 | 2019-02-01 14:06:04 -0800 | [diff] [blame] | 1915 | RoleManagerInternal rmi = LocalServices.getService(RoleManagerInternal.class); |
Philip P. Moltmann | 70b42ae | 2019-01-29 16:24:19 -0800 | [diff] [blame] | 1916 | |
| 1917 | List<UserInfo> users = mContext.getSystemService(UserManager.class).getUsers(); |
| 1918 | |
| 1919 | int numUsers = users.size(); |
| 1920 | for (int userNum = 0; userNum < numUsers; userNum++) { |
Hai Zhang | 85fd062 | 2019-02-01 14:06:04 -0800 | [diff] [blame] | 1921 | int userId = users.get(userNum).getUserHandle().getIdentifier(); |
Philip P. Moltmann | 70b42ae | 2019-01-29 16:24:19 -0800 | [diff] [blame] | 1922 | |
Hai Zhang | 85fd062 | 2019-02-01 14:06:04 -0800 | [diff] [blame] | 1923 | ArrayMap<String, ArraySet<String>> roles = rmi.getRolesAndHolders( |
| 1924 | userId); |
Philip P. Moltmann | 70b42ae | 2019-01-29 16:24:19 -0800 | [diff] [blame] | 1925 | |
| 1926 | int numRoles = roles.size(); |
| 1927 | for (int roleNum = 0; roleNum < numRoles; roleNum++) { |
| 1928 | String roleName = roles.keyAt(roleNum); |
| 1929 | ArraySet<String> holders = roles.valueAt(roleNum); |
| 1930 | |
| 1931 | int numHolders = holders.size(); |
| 1932 | for (int holderNum = 0; holderNum < numHolders; holderNum++) { |
| 1933 | String holderName = holders.valueAt(holderNum); |
| 1934 | |
| 1935 | PackageInfo pkg; |
| 1936 | try { |
Hai Zhang | 85fd062 | 2019-02-01 14:06:04 -0800 | [diff] [blame] | 1937 | pkg = pm.getPackageInfoAsUser(holderName, 0, userId); |
Philip P. Moltmann | 70b42ae | 2019-01-29 16:24:19 -0800 | [diff] [blame] | 1938 | } catch (PackageManager.NameNotFoundException e) { |
| 1939 | Log.w(TAG, "Role holder " + holderName + " not found"); |
| 1940 | return; |
| 1941 | } |
| 1942 | |
| 1943 | StatsLogEventWrapper e = new StatsLogEventWrapper(StatsLog.ROLE_HOLDER, |
| 1944 | elapsedNanos, wallClockNanos); |
| 1945 | e.writeInt(pkg.applicationInfo.uid); |
| 1946 | e.writeString(holderName); |
| 1947 | e.writeString(roleName); |
| 1948 | pulledData.add(e); |
| 1949 | } |
| 1950 | } |
| 1951 | } |
| 1952 | } finally { |
| 1953 | Binder.restoreCallingIdentity(callingToken); |
| 1954 | } |
| 1955 | } |
| 1956 | |
Neil Fuller | db14b48 | 2019-02-05 19:44:44 +0000 | [diff] [blame] | 1957 | private void pullTimeZoneDataInfo(int tagId, |
| 1958 | long elapsedNanos, long wallClockNanos, List<StatsLogEventWrapper> pulledData) { |
| 1959 | String tzDbVersion = "Unknown"; |
| 1960 | try { |
| 1961 | tzDbVersion = android.icu.util.TimeZone.getTZDataVersion(); |
| 1962 | } catch (Exception e) { |
| 1963 | Log.e(TAG, "Getting tzdb version failed: ", e); |
| 1964 | } |
| 1965 | |
| 1966 | StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
| 1967 | e.writeString(tzDbVersion); |
| 1968 | pulledData.add(e); |
| 1969 | } |
| 1970 | |
Carmen Agimof | 35f7f6f | 2019-02-05 18:19:21 +0000 | [diff] [blame] | 1971 | private void pullSDCardInfo(int tagId, long elapsedNanos, long wallClockNanos, |
| 1972 | List<StatsLogEventWrapper> pulledData) { |
| 1973 | StorageManager storageManager = mContext.getSystemService(StorageManager.class); |
| 1974 | if (storageManager != null) { |
| 1975 | List<VolumeInfo> volumes = storageManager.getVolumes(); |
| 1976 | for (VolumeInfo vol : volumes) { |
| 1977 | final String envState = VolumeInfo.getEnvironmentForState(vol.getState()); |
| 1978 | final DiskInfo diskInfo = vol.getDisk(); |
| 1979 | if (diskInfo != null && diskInfo.isSd()) { |
| 1980 | if (envState.equals(Environment.MEDIA_MOUNTED)) { |
| 1981 | StatsLogEventWrapper e = |
| 1982 | new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); |
| 1983 | e.writeInt(vol.getType() + 1); |
| 1984 | e.writeLong(diskInfo.size); |
| 1985 | pulledData.add(e); |
| 1986 | } |
| 1987 | } |
| 1988 | } |
| 1989 | } |
| 1990 | } |
| 1991 | |
Philip P. Moltmann | 70b42ae | 2019-01-29 16:24:19 -0800 | [diff] [blame] | 1992 | /** |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 1993 | * Pulls various data. |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 1994 | */ |
Bookatz | c68a9d2 | 2017-09-27 14:09:55 -0700 | [diff] [blame] | 1995 | @Override // Binder call |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 1996 | public StatsLogEventWrapper[] pullData(int tagId) { |
Bookatz | c68a9d2 | 2017-09-27 14:09:55 -0700 | [diff] [blame] | 1997 | enforceCallingPermission(); |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 1998 | if (DEBUG) { |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 1999 | Slog.d(TAG, "Pulling " + tagId); |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 2000 | } |
Rajeev Kumar | 22d92b7 | 2018-02-07 18:38:36 -0800 | [diff] [blame] | 2001 | List<StatsLogEventWrapper> ret = new ArrayList<>(); |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 2002 | long elapsedNanos = SystemClock.elapsedRealtimeNanos(); |
| 2003 | long wallClockNanos = SystemClock.currentTimeMicro() * 1000L; |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 2004 | switch (tagId) { |
Chenjie Yu | 31d14d7 | 2017-12-12 17:54:33 -0800 | [diff] [blame] | 2005 | case StatsLog.WIFI_BYTES_TRANSFER: { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 2006 | pullWifiBytesTransfer(tagId, elapsedNanos, wallClockNanos, ret); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 2007 | break; |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 2008 | } |
Chenjie Yu | 31d14d7 | 2017-12-12 17:54:33 -0800 | [diff] [blame] | 2009 | case StatsLog.MOBILE_BYTES_TRANSFER: { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 2010 | pullMobileBytesTransfer(tagId, elapsedNanos, wallClockNanos, ret); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 2011 | break; |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 2012 | } |
Chenjie Yu | 31d14d7 | 2017-12-12 17:54:33 -0800 | [diff] [blame] | 2013 | case StatsLog.WIFI_BYTES_TRANSFER_BY_FG_BG: { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 2014 | pullWifiBytesTransferByFgBg(tagId, elapsedNanos, wallClockNanos, ret); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 2015 | break; |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 2016 | } |
Chenjie Yu | 31d14d7 | 2017-12-12 17:54:33 -0800 | [diff] [blame] | 2017 | case StatsLog.MOBILE_BYTES_TRANSFER_BY_FG_BG: { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 2018 | pullMobileBytesTransferByFgBg(tagId, elapsedNanos, wallClockNanos, ret); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 2019 | break; |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 2020 | } |
| 2021 | case StatsLog.BLUETOOTH_BYTES_TRANSFER: { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 2022 | pullBluetoothBytesTransfer(tagId, elapsedNanos, wallClockNanos, ret); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 2023 | break; |
David Chen | c8a4324 | 2017-10-17 16:23:28 -0700 | [diff] [blame] | 2024 | } |
Chenjie Yu | 31d14d7 | 2017-12-12 17:54:33 -0800 | [diff] [blame] | 2025 | case StatsLog.KERNEL_WAKELOCK: { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 2026 | pullKernelWakelock(tagId, elapsedNanos, wallClockNanos, ret); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 2027 | break; |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 2028 | } |
Chenjie Yu | 31d14d7 | 2017-12-12 17:54:33 -0800 | [diff] [blame] | 2029 | case StatsLog.CPU_TIME_PER_FREQ: { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 2030 | pullCpuTimePerFreq(tagId, elapsedNanos, wallClockNanos, ret); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 2031 | break; |
Chenjie Yu | 7f8def9 | 2017-11-03 09:33:15 -0700 | [diff] [blame] | 2032 | } |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 2033 | case StatsLog.CPU_TIME_PER_UID: { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 2034 | pullKernelUidCpuTime(tagId, elapsedNanos, wallClockNanos, ret); |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 2035 | break; |
| 2036 | } |
| 2037 | case StatsLog.CPU_TIME_PER_UID_FREQ: { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 2038 | pullKernelUidCpuFreqTime(tagId, elapsedNanos, wallClockNanos, ret); |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 2039 | break; |
| 2040 | } |
| 2041 | case StatsLog.CPU_CLUSTER_TIME: { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 2042 | pullKernelUidCpuClusterTime(tagId, elapsedNanos, wallClockNanos, ret); |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 2043 | break; |
| 2044 | } |
| 2045 | case StatsLog.CPU_ACTIVE_TIME: { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 2046 | pullKernelUidCpuActiveTime(tagId, elapsedNanos, wallClockNanos, ret); |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 2047 | break; |
| 2048 | } |
Chenjie Yu | 5caaa9d | 2018-03-06 15:48:54 -0800 | [diff] [blame] | 2049 | case StatsLog.WIFI_ACTIVITY_INFO: { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 2050 | pullWifiActivityInfo(tagId, elapsedNanos, wallClockNanos, ret); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 2051 | break; |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 2052 | } |
Chenjie Yu | 31d14d7 | 2017-12-12 17:54:33 -0800 | [diff] [blame] | 2053 | case StatsLog.MODEM_ACTIVITY_INFO: { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 2054 | pullModemActivityInfo(tagId, elapsedNanos, wallClockNanos, ret); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 2055 | break; |
Chenjie Yu | 05013b3 | 2017-11-21 10:21:41 -0800 | [diff] [blame] | 2056 | } |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 2057 | case StatsLog.BLUETOOTH_ACTIVITY_INFO: { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 2058 | pullBluetoothActivityInfo(tagId, elapsedNanos, wallClockNanos, ret); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 2059 | break; |
Chenjie Yu | 937d742 | 2018-01-10 16:37:53 -0800 | [diff] [blame] | 2060 | } |
| 2061 | case StatsLog.SYSTEM_UPTIME: { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 2062 | pullSystemUpTime(tagId, elapsedNanos, wallClockNanos, ret); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 2063 | break; |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 2064 | } |
| 2065 | case StatsLog.SYSTEM_ELAPSED_REALTIME: { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 2066 | pullSystemElapsedRealtime(tagId, elapsedNanos, wallClockNanos, ret); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 2067 | break; |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 2068 | } |
Rajeev Kumar | 22d92b7 | 2018-02-07 18:38:36 -0800 | [diff] [blame] | 2069 | case StatsLog.PROCESS_MEMORY_STATE: { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 2070 | pullProcessMemoryState(tagId, elapsedNanos, wallClockNanos, ret); |
Rajeev Kumar | 22d92b7 | 2018-02-07 18:38:36 -0800 | [diff] [blame] | 2071 | break; |
| 2072 | } |
Rafal Slawik | 0862158 | 2018-10-15 14:53:07 +0100 | [diff] [blame] | 2073 | case StatsLog.NATIVE_PROCESS_MEMORY_STATE: { |
| 2074 | pullNativeProcessMemoryState(tagId, elapsedNanos, wallClockNanos, ret); |
| 2075 | break; |
| 2076 | } |
Rafal Slawik | 3bea895 | 2018-11-15 12:39:33 +0000 | [diff] [blame] | 2077 | case StatsLog.PROCESS_MEMORY_HIGH_WATER_MARK: { |
| 2078 | pullProcessMemoryHighWaterMark(tagId, elapsedNanos, wallClockNanos, ret); |
| 2079 | break; |
| 2080 | } |
Rafal Slawik | d4e8757 | 2019-03-12 13:08:38 +0000 | [diff] [blame] | 2081 | case StatsLog.SYSTEM_ION_HEAP_SIZE: { |
| 2082 | pullSystemIonHeapSize(tagId, elapsedNanos, wallClockNanos, ret); |
| 2083 | break; |
| 2084 | } |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 2085 | case StatsLog.BINDER_CALLS: { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 2086 | pullBinderCallsStats(tagId, elapsedNanos, wallClockNanos, ret); |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 2087 | break; |
| 2088 | } |
Olivier Gaillard | 6f52d15 | 2018-07-25 12:13:12 +0100 | [diff] [blame] | 2089 | case StatsLog.BINDER_CALLS_EXCEPTIONS: { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 2090 | pullBinderCallsStatsExceptions(tagId, elapsedNanos, wallClockNanos, ret); |
Olivier Gaillard | 6f52d15 | 2018-07-25 12:13:12 +0100 | [diff] [blame] | 2091 | break; |
| 2092 | } |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 2093 | case StatsLog.LOOPER_STATS: { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 2094 | pullLooperStats(tagId, elapsedNanos, wallClockNanos, ret); |
Marcin Oczeretko | d8cc859 | 2018-08-22 16:07:36 +0100 | [diff] [blame] | 2095 | break; |
| 2096 | } |
Tej Singh | 86dc9db | 2018-09-06 00:39:57 +0000 | [diff] [blame] | 2097 | case StatsLog.DISK_STATS: { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 2098 | pullDiskStats(tagId, elapsedNanos, wallClockNanos, ret); |
Tej Singh | 86dc9db | 2018-09-06 00:39:57 +0000 | [diff] [blame] | 2099 | break; |
| 2100 | } |
| 2101 | case StatsLog.DIRECTORY_USAGE: { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 2102 | pullDirectoryUsage(tagId, elapsedNanos, wallClockNanos, ret); |
Tej Singh | 86dc9db | 2018-09-06 00:39:57 +0000 | [diff] [blame] | 2103 | break; |
| 2104 | } |
| 2105 | case StatsLog.APP_SIZE: { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 2106 | pullAppSize(tagId, elapsedNanos, wallClockNanos, ret); |
Tej Singh | 86dc9db | 2018-09-06 00:39:57 +0000 | [diff] [blame] | 2107 | break; |
| 2108 | } |
| 2109 | case StatsLog.CATEGORY_SIZE: { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 2110 | pullCategorySize(tagId, elapsedNanos, wallClockNanos, ret); |
Tej Singh | 86dc9db | 2018-09-06 00:39:57 +0000 | [diff] [blame] | 2111 | break; |
| 2112 | } |
Kevin Chyn | 1741a07 | 2019-01-17 11:54:40 -0800 | [diff] [blame] | 2113 | case StatsLog.NUM_FINGERPRINTS_ENROLLED: { |
Kevin Chyn | 6ecc570 | 2019-01-28 12:00:37 -0800 | [diff] [blame] | 2114 | pullNumBiometricsEnrolled(BiometricsProtoEnums.MODALITY_FINGERPRINT, tagId, |
| 2115 | elapsedNanos, wallClockNanos, ret); |
| 2116 | break; |
| 2117 | } |
| 2118 | case StatsLog.NUM_FACES_ENROLLED: { |
| 2119 | pullNumBiometricsEnrolled(BiometricsProtoEnums.MODALITY_FACE, tagId, elapsedNanos, |
| 2120 | wallClockNanos, ret); |
Tej Singh | d6d6d77 | 2018-09-05 17:41:25 -0700 | [diff] [blame] | 2121 | break; |
| 2122 | } |
Chenjie Yu | 2c0d83d | 2018-09-02 06:37:08 -0700 | [diff] [blame] | 2123 | case StatsLog.PROC_STATS: { |
Yang Lu | 1587e13 | 2018-10-22 16:19:40 +0000 | [diff] [blame] | 2124 | pullProcessStats(ProcessStats.REPORT_ALL, tagId, elapsedNanos, wallClockNanos, ret); |
Chenjie Yu | b52779e | 2018-10-05 12:03:36 -0700 | [diff] [blame] | 2125 | break; |
| 2126 | } |
| 2127 | case StatsLog.PROC_STATS_PKG_PROC: { |
| 2128 | pullProcessStats(ProcessStats.REPORT_PKG_PROC_STATS, tagId, elapsedNanos, |
| 2129 | wallClockNanos, ret); |
Chenjie Yu | 2c0d83d | 2018-09-02 06:37:08 -0700 | [diff] [blame] | 2130 | break; |
| 2131 | } |
Tej Singh | e7726dc | 2018-09-21 11:42:12 -0700 | [diff] [blame] | 2132 | case StatsLog.DISK_IO: { |
| 2133 | pullDiskIo(tagId, elapsedNanos, wallClockNanos, ret); |
| 2134 | break; |
| 2135 | } |
Chenjie Yu | ab53020 | 2018-09-26 12:39:20 -0700 | [diff] [blame] | 2136 | case StatsLog.POWER_PROFILE: { |
| 2137 | pullPowerProfile(tagId, elapsedNanos, wallClockNanos, ret); |
| 2138 | break; |
| 2139 | } |
Bookatz | 366a443 | 2018-11-20 09:42:33 -0800 | [diff] [blame] | 2140 | case StatsLog.BUILD_INFORMATION: { |
| 2141 | pullBuildInformation(tagId, elapsedNanos, wallClockNanos, ret); |
| 2142 | break; |
| 2143 | } |
Yangster-mac | 308ea0c | 2018-10-22 13:10:25 -0700 | [diff] [blame] | 2144 | case StatsLog.PROCESS_CPU_TIME: { |
| 2145 | pullProcessCpuTime(tagId, elapsedNanos, wallClockNanos, ret); |
| 2146 | break; |
| 2147 | } |
Misha Wagner | 5a51e00 | 2018-10-03 15:04:09 +0100 | [diff] [blame] | 2148 | case StatsLog.CPU_TIME_PER_THREAD_FREQ: { |
| 2149 | pullCpuTimePerThreadFreq(tagId, elapsedNanos, wallClockNanos, ret); |
| 2150 | break; |
| 2151 | } |
Bookatz | 75ee604 | 2018-11-09 12:27:37 -0800 | [diff] [blame] | 2152 | case StatsLog.DEVICE_CALCULATED_POWER_USE: { |
| 2153 | pullDeviceCalculatedPowerUse(tagId, elapsedNanos, wallClockNanos, ret); |
| 2154 | break; |
| 2155 | } |
| 2156 | case StatsLog.DEVICE_CALCULATED_POWER_BLAME_UID: { |
| 2157 | pullDeviceCalculatedPowerBlameUid(tagId, elapsedNanos, wallClockNanos, ret); |
| 2158 | break; |
| 2159 | } |
| 2160 | case StatsLog.DEVICE_CALCULATED_POWER_BLAME_OTHER: { |
| 2161 | pullDeviceCalculatedPowerBlameOther(tagId, elapsedNanos, wallClockNanos, ret); |
| 2162 | break; |
| 2163 | } |
Tej Singh | 9876a3b | 2019-01-11 14:28:35 -0800 | [diff] [blame] | 2164 | case StatsLog.TEMPERATURE: { |
| 2165 | pullTemperature(tagId, elapsedNanos, wallClockNanos, ret); |
| 2166 | break; |
| 2167 | } |
Olivier Gaillard | 18d45220 | 2019-01-22 15:03:41 +0000 | [diff] [blame] | 2168 | case StatsLog.DEBUG_ELAPSED_CLOCK: { |
| 2169 | pullDebugElapsedClock(tagId, elapsedNanos, wallClockNanos, ret); |
| 2170 | break; |
| 2171 | } |
| 2172 | case StatsLog.DEBUG_FAILING_ELAPSED_CLOCK: { |
| 2173 | pullDebugFailingElapsedClock(tagId, elapsedNanos, wallClockNanos, ret); |
| 2174 | break; |
| 2175 | } |
Philip P. Moltmann | 70b42ae | 2019-01-29 16:24:19 -0800 | [diff] [blame] | 2176 | case StatsLog.ROLE_HOLDER: { |
| 2177 | pullRoleHolders(elapsedNanos, wallClockNanos, ret); |
| 2178 | break; |
| 2179 | } |
Philip P. Moltmann | f21a350 | 2019-01-30 09:53:04 -0800 | [diff] [blame] | 2180 | case StatsLog.DANGEROUS_PERMISSION_STATE: { |
| 2181 | pullDangerousPermissionState(elapsedNanos, wallClockNanos, ret); |
| 2182 | break; |
| 2183 | } |
Neil Fuller | db14b48 | 2019-02-05 19:44:44 +0000 | [diff] [blame] | 2184 | case StatsLog.TIME_ZONE_DATA_INFO: { |
| 2185 | pullTimeZoneDataInfo(tagId, elapsedNanos, wallClockNanos, ret); |
| 2186 | break; |
| 2187 | } |
Carmen Agimof | 35f7f6f | 2019-02-05 18:19:21 +0000 | [diff] [blame] | 2188 | case StatsLog.SDCARD_INFO: { |
| 2189 | pullSDCardInfo(tagId, elapsedNanos, wallClockNanos, ret); |
| 2190 | break; |
| 2191 | } |
Bookatz | c68a9d2 | 2017-09-27 14:09:55 -0700 | [diff] [blame] | 2192 | default: |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 2193 | Slog.w(TAG, "No such tagId data as " + tagId); |
Bookatz | c68a9d2 | 2017-09-27 14:09:55 -0700 | [diff] [blame] | 2194 | return null; |
| 2195 | } |
Chenjie Yu | 9d7720b | 2018-01-24 10:34:48 -0800 | [diff] [blame] | 2196 | return ret.toArray(new StatsLogEventWrapper[ret.size()]); |
Bookatz | c68a9d2 | 2017-09-27 14:09:55 -0700 | [diff] [blame] | 2197 | } |
| 2198 | |
| 2199 | @Override // Binder call |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 2200 | public void statsdReady() { |
| 2201 | enforceCallingPermission(); |
Chenjie Yu | 2c0d83d | 2018-09-02 06:37:08 -0700 | [diff] [blame] | 2202 | if (DEBUG) { |
| 2203 | Slog.d(TAG, "learned that statsdReady"); |
| 2204 | } |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 2205 | sayHiToStatsd(); // tell statsd that we're ready too and link to it |
Chenjie Yu | 2c0d83d | 2018-09-02 06:37:08 -0700 | [diff] [blame] | 2206 | mContext.sendBroadcastAsUser(new Intent(StatsManager.ACTION_STATSD_STARTED) |
Bookatz | 8178408 | 2018-02-09 14:26:42 -0800 | [diff] [blame] | 2207 | .addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND), |
Chenjie Yu | 2c0d83d | 2018-09-02 06:37:08 -0700 | [diff] [blame] | 2208 | UserHandle.SYSTEM, android.Manifest.permission.DUMP); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 2209 | } |
| 2210 | |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 2211 | @Override |
| 2212 | public void triggerUidSnapshot() { |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 2213 | enforceCallingPermission(); |
| 2214 | synchronized (sStatsdLock) { |
David Chen | 35045cb | 2018-03-23 22:21:47 -0700 | [diff] [blame] | 2215 | final long token = Binder.clearCallingIdentity(); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 2216 | try { |
| 2217 | informAllUidsLocked(mContext); |
| 2218 | } catch (RemoteException e) { |
| 2219 | Slog.e(TAG, "Failed to trigger uid snapshot.", e); |
David Chen | 35045cb | 2018-03-23 22:21:47 -0700 | [diff] [blame] | 2220 | } finally { |
| 2221 | restoreCallingIdentity(token); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 2222 | } |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 2223 | } |
David Chen | c136f45 | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 2224 | } |
| 2225 | |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 2226 | private void enforceCallingPermission() { |
| 2227 | if (Binder.getCallingPid() == Process.myPid()) { |
| 2228 | return; |
| 2229 | } |
| 2230 | mContext.enforceCallingPermission(android.Manifest.permission.STATSCOMPANION, null); |
| 2231 | } |
| 2232 | |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 2233 | // Lifecycle and related code |
| 2234 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 2235 | /** |
| 2236 | * Fetches the statsd IBinder service |
| 2237 | */ |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 2238 | private static IStatsManager fetchStatsdService() { |
| 2239 | return IStatsManager.Stub.asInterface(ServiceManager.getService("stats")); |
| 2240 | } |
| 2241 | |
| 2242 | public static final class Lifecycle extends SystemService { |
| 2243 | private StatsCompanionService mStatsCompanionService; |
| 2244 | |
| 2245 | public Lifecycle(Context context) { |
| 2246 | super(context); |
| 2247 | } |
| 2248 | |
| 2249 | @Override |
| 2250 | public void onStart() { |
| 2251 | mStatsCompanionService = new StatsCompanionService(getContext()); |
| 2252 | try { |
Chenjie Yu | ab53020 | 2018-09-26 12:39:20 -0700 | [diff] [blame] | 2253 | publishBinderService(Context.STATS_COMPANION_SERVICE, |
| 2254 | mStatsCompanionService); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 2255 | if (DEBUG) Slog.d(TAG, "Published " + Context.STATS_COMPANION_SERVICE); |
| 2256 | } catch (Exception e) { |
| 2257 | Slog.e(TAG, "Failed to publishBinderService", e); |
| 2258 | } |
| 2259 | } |
| 2260 | |
| 2261 | @Override |
| 2262 | public void onBootPhase(int phase) { |
| 2263 | super.onBootPhase(phase); |
| 2264 | if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) { |
| 2265 | mStatsCompanionService.systemReady(); |
| 2266 | } |
| 2267 | } |
| 2268 | } |
| 2269 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 2270 | /** |
| 2271 | * Now that the android system is ready, StatsCompanion is ready too, so inform statsd. |
| 2272 | */ |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 2273 | private void systemReady() { |
| 2274 | if (DEBUG) Slog.d(TAG, "Learned that systemReady"); |
| 2275 | sayHiToStatsd(); |
| 2276 | } |
| 2277 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 2278 | /** |
Chenjie Yu | ab53020 | 2018-09-26 12:39:20 -0700 | [diff] [blame] | 2279 | * Tells statsd that statscompanion is ready. If the binder call returns, link to |
| 2280 | * statsd. |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 2281 | */ |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 2282 | private void sayHiToStatsd() { |
| 2283 | synchronized (sStatsdLock) { |
| 2284 | if (sStatsd != null) { |
| 2285 | Slog.e(TAG, "Trying to fetch statsd, but it was already fetched", |
Chenjie Yu | ab53020 | 2018-09-26 12:39:20 -0700 | [diff] [blame] | 2286 | new IllegalStateException( |
| 2287 | "sStatsd is not null when being fetched")); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 2288 | return; |
| 2289 | } |
| 2290 | sStatsd = fetchStatsdService(); |
| 2291 | if (sStatsd == null) { |
Chenjie Yu | ab53020 | 2018-09-26 12:39:20 -0700 | [diff] [blame] | 2292 | Slog.i(TAG, |
| 2293 | "Could not yet find statsd to tell it that StatsCompanion is " |
| 2294 | + "alive."); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 2295 | return; |
| 2296 | } |
| 2297 | if (DEBUG) Slog.d(TAG, "Saying hi to statsd"); |
| 2298 | try { |
| 2299 | sStatsd.statsCompanionReady(); |
| 2300 | // If the statsCompanionReady two-way binder call returns, link to statsd. |
| 2301 | try { |
| 2302 | sStatsd.asBinder().linkToDeath(new StatsdDeathRecipient(), 0); |
| 2303 | } catch (RemoteException e) { |
| 2304 | Slog.e(TAG, "linkToDeath(StatsdDeathRecipient) failed", e); |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 2305 | forgetEverythingLocked(); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 2306 | } |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 2307 | // Setup broadcast receiver for updates. |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 2308 | IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_REPLACED); |
| 2309 | filter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 2310 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 2311 | filter.addDataScheme("package"); |
Chenjie Yu | ab53020 | 2018-09-26 12:39:20 -0700 | [diff] [blame] | 2312 | mContext.registerReceiverAsUser(mAppUpdateReceiver, UserHandle.ALL, filter, |
| 2313 | null, |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 2314 | null); |
David Chen | 47e8f4d | 2017-10-11 15:34:13 -0700 | [diff] [blame] | 2315 | |
Chenjie Yu | ab53020 | 2018-09-26 12:39:20 -0700 | [diff] [blame] | 2316 | // Setup receiver for user initialize (which happens once for a new user) |
| 2317 | // and |
David Chen | 47e8f4d | 2017-10-11 15:34:13 -0700 | [diff] [blame] | 2318 | // if a user is removed. |
| 2319 | filter = new IntentFilter(Intent.ACTION_USER_INITIALIZE); |
| 2320 | filter.addAction(Intent.ACTION_USER_REMOVED); |
| 2321 | mContext.registerReceiverAsUser(mUserUpdateReceiver, UserHandle.ALL, |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 2322 | filter, null, null); |
David Chen | 47e8f4d | 2017-10-11 15:34:13 -0700 | [diff] [blame] | 2323 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 2324 | // Setup receiver for device reboots or shutdowns. |
| 2325 | filter = new IntentFilter(Intent.ACTION_REBOOT); |
| 2326 | filter.addAction(Intent.ACTION_SHUTDOWN); |
| 2327 | mContext.registerReceiverAsUser( |
| 2328 | mShutdownEventReceiver, UserHandle.ALL, filter, null, null); |
Yao Chen | 0f21710 | 2018-01-09 10:33:15 -0800 | [diff] [blame] | 2329 | final long token = Binder.clearCallingIdentity(); |
| 2330 | try { |
Chenjie Yu | ab53020 | 2018-09-26 12:39:20 -0700 | [diff] [blame] | 2331 | // Pull the latest state of UID->app name, version mapping when |
| 2332 | // statsd starts. |
Yao Chen | 0f21710 | 2018-01-09 10:33:15 -0800 | [diff] [blame] | 2333 | informAllUidsLocked(mContext); |
| 2334 | } finally { |
| 2335 | restoreCallingIdentity(token); |
| 2336 | } |
Bookatz | 111ed73 | 2018-02-16 15:54:05 -0800 | [diff] [blame] | 2337 | Slog.i(TAG, "Told statsd that StatsCompanionService is alive."); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 2338 | } catch (RemoteException e) { |
| 2339 | Slog.e(TAG, "Failed to inform statsd that statscompanion is ready", e); |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 2340 | forgetEverythingLocked(); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 2341 | } |
| 2342 | } |
| 2343 | } |
| 2344 | |
| 2345 | private class StatsdDeathRecipient implements IBinder.DeathRecipient { |
| 2346 | @Override |
| 2347 | public void binderDied() { |
| 2348 | Slog.i(TAG, "Statsd is dead - erase all my knowledge."); |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 2349 | synchronized (sStatsdLock) { |
| 2350 | long now = SystemClock.elapsedRealtime(); |
| 2351 | for (Long timeMillis : mDeathTimeMillis) { |
| 2352 | long ageMillis = now - timeMillis; |
| 2353 | if (ageMillis > MILLIS_IN_A_DAY) { |
| 2354 | mDeathTimeMillis.remove(timeMillis); |
| 2355 | } |
| 2356 | } |
| 2357 | for (Long timeMillis : mDeletedFiles.keySet()) { |
| 2358 | long ageMillis = now - timeMillis; |
| 2359 | if (ageMillis > MILLIS_IN_A_DAY * 7) { |
| 2360 | mDeletedFiles.remove(timeMillis); |
| 2361 | } |
| 2362 | } |
| 2363 | mDeathTimeMillis.add(now); |
| 2364 | if (mDeathTimeMillis.size() >= DEATH_THRESHOLD) { |
| 2365 | mDeathTimeMillis.clear(); |
| 2366 | File[] configs = FileUtils.listFilesOrEmpty(new File(CONFIG_DIR)); |
| 2367 | if (configs.length > 0) { |
| 2368 | String fileName = configs[0].getName(); |
| 2369 | if (configs[0].delete()) { |
| 2370 | mDeletedFiles.put(now, fileName); |
| 2371 | } |
| 2372 | } |
| 2373 | } |
| 2374 | forgetEverythingLocked(); |
| 2375 | } |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 2376 | } |
| 2377 | } |
| 2378 | |
Andreas Gampe | 2952021 | 2018-07-20 12:55:37 -0700 | [diff] [blame] | 2379 | @GuardedBy("StatsCompanionService.sStatsdLock") |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 2380 | private void forgetEverythingLocked() { |
| 2381 | sStatsd = null; |
| 2382 | mContext.unregisterReceiver(mAppUpdateReceiver); |
| 2383 | mContext.unregisterReceiver(mUserUpdateReceiver); |
| 2384 | mContext.unregisterReceiver(mShutdownEventReceiver); |
| 2385 | cancelAnomalyAlarm(); |
| 2386 | cancelPullingAlarm(); |
Olivier Gaillard | 6e72527 | 2019-01-15 14:21:57 +0000 | [diff] [blame] | 2387 | |
| 2388 | BinderCallsStatsService.Internal binderStats = |
| 2389 | LocalServices.getService(BinderCallsStatsService.Internal.class); |
| 2390 | if (binderStats != null) { |
| 2391 | binderStats.reset(); |
| 2392 | } |
| 2393 | |
| 2394 | LooperStats looperStats = LocalServices.getService(LooperStats.class); |
| 2395 | if (looperStats != null) { |
| 2396 | looperStats.reset(); |
| 2397 | } |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 2398 | } |
| 2399 | |
| 2400 | @Override |
| 2401 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
| 2402 | if (!DumpUtils.checkDumpPermission(mContext, TAG, writer)) return; |
| 2403 | |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 2404 | synchronized (sStatsdLock) { |
Chenjie Yu | ab53020 | 2018-09-26 12:39:20 -0700 | [diff] [blame] | 2405 | writer.println( |
| 2406 | "Number of configuration files deleted: " + mDeletedFiles.size()); |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 2407 | if (mDeletedFiles.size() > 0) { |
| 2408 | writer.println(" timestamp, deleted file name"); |
| 2409 | } |
| 2410 | long lastBootMillis = |
| 2411 | SystemClock.currentThreadTimeMillis() - SystemClock.elapsedRealtime(); |
| 2412 | for (Long elapsedMillis : mDeletedFiles.keySet()) { |
| 2413 | long deletionMillis = lastBootMillis + elapsedMillis; |
Chenjie Yu | ab53020 | 2018-09-26 12:39:20 -0700 | [diff] [blame] | 2414 | writer.println( |
| 2415 | " " + deletionMillis + ", " + mDeletedFiles.get(elapsedMillis)); |
yro | e26c97b | 2018-03-27 10:30:11 -0700 | [diff] [blame] | 2416 | } |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 2417 | } |
| 2418 | } |
| 2419 | |
Yangster-mac | e16189a | 2018-08-26 12:20:16 -0700 | [diff] [blame] | 2420 | // Thermal event received from vendor thermal management subsystem |
| 2421 | private static final class ThermalEventListener extends IThermalEventListener.Stub { |
Chenjie Yu | 12e5e67 | 2018-09-14 15:54:59 -0700 | [diff] [blame] | 2422 | @Override |
Wei Wang | bad7c20 | 2018-11-01 11:57:39 -0700 | [diff] [blame] | 2423 | public void notifyThrottling(Temperature temp) { |
| 2424 | boolean isThrottling = temp.getStatus() >= Temperature.THROTTLING_SEVERE; |
Yangster-mac | e16189a | 2018-08-26 12:20:16 -0700 | [diff] [blame] | 2425 | StatsLog.write(StatsLog.THERMAL_THROTTLING, temp.getType(), |
Tejinder Singh | 9ee9ba3 | 2018-11-08 16:56:16 -0800 | [diff] [blame] | 2426 | isThrottling ? |
| 2427 | StatsLog.THERMAL_THROTTLING_STATE_CHANGED__STATE__START : |
| 2428 | StatsLog.THERMAL_THROTTLING_STATE_CHANGED__STATE__STOP, |
| 2429 | temp.getValue()); |
Yangster-mac | e16189a | 2018-08-26 12:20:16 -0700 | [diff] [blame] | 2430 | } |
| 2431 | } |
Chenjie Yu | ae9dfac | 2018-10-25 16:06:56 -0700 | [diff] [blame] | 2432 | |
| 2433 | private static final class ConnectivityStatsCallback extends |
| 2434 | ConnectivityManager.NetworkCallback { |
| 2435 | @Override |
| 2436 | public void onAvailable(Network network) { |
| 2437 | StatsLog.write(StatsLog.CONNECTIVITY_STATE_CHANGED, network.netId, |
| 2438 | StatsLog.CONNECTIVITY_STATE_CHANGED__STATE__CONNECTED); |
| 2439 | } |
| 2440 | |
| 2441 | @Override |
| 2442 | public void onLost(Network network) { |
| 2443 | StatsLog.write(StatsLog.CONNECTIVITY_STATE_CHANGED, network.netId, |
| 2444 | StatsLog.CONNECTIVITY_STATE_CHANGED__STATE__DISCONNECTED); |
| 2445 | } |
| 2446 | } |
Bookatz | 9472641 | 2017-08-31 09:26:15 -0700 | [diff] [blame] | 2447 | } |