Tej Singh | 953ad86 | 2020-01-03 12:47:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | |
Tej Singh | 250e7aa | 2020-01-16 13:16:21 -0800 | [diff] [blame] | 17 | package com.android.server.stats.pull; |
Tej Singh | 953ad86 | 2020-01-03 12:47:07 -0800 | [diff] [blame] | 18 | |
Stanislav Zholnin | 050abc3 | 2020-02-17 14:14:52 +0000 | [diff] [blame] | 19 | import static android.app.AppOpsManager.OP_FLAG_SELF; |
Stanislav Zholnin | 73a8bbb | 2020-05-21 19:26:44 +0000 | [diff] [blame] | 20 | import static android.app.AppOpsManager.OP_FLAG_TRUSTED_PROXIED; |
junyulai | 212a5f0e | 2020-02-07 17:42:39 +0800 | [diff] [blame] | 21 | import static android.app.usage.NetworkStatsManager.FLAG_AUGMENT_WITH_SUBSCRIPTION_PLAN; |
junyulai | 1794157 | 2020-05-27 14:44:13 +0800 | [diff] [blame] | 22 | import static android.app.usage.NetworkStatsManager.FLAG_POLL_FORCE; |
| 23 | import static android.app.usage.NetworkStatsManager.FLAG_POLL_ON_OPEN; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 24 | import static android.content.pm.PackageInfo.REQUESTED_PERMISSION_GRANTED; |
| 25 | import static android.content.pm.PermissionInfo.PROTECTION_DANGEROUS; |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 26 | import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR; |
| 27 | import static android.net.NetworkCapabilities.TRANSPORT_WIFI; |
junyulai | 592c605 | 2020-04-29 16:09:01 +0800 | [diff] [blame] | 28 | import static android.net.NetworkTemplate.NETWORK_TYPE_ALL; |
junyulai | ee3ea79 | 2020-05-15 18:26:25 +0800 | [diff] [blame] | 29 | import static android.net.NetworkTemplate.buildTemplateMobileWildcard; |
| 30 | import static android.net.NetworkTemplate.buildTemplateMobileWithRatType; |
| 31 | import static android.net.NetworkTemplate.buildTemplateWifiWildcard; |
| 32 | import static android.net.NetworkTemplate.getAllCollapsedRatTypes; |
Rafal Slawik | bdd5a50 | 2020-01-14 14:14:29 +0000 | [diff] [blame] | 33 | import static android.os.Debug.getIonHeapsSizeKb; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 34 | import static android.os.Process.getUidForPid; |
| 35 | import static android.os.storage.VolumeInfo.TYPE_PRIVATE; |
| 36 | import static android.os.storage.VolumeInfo.TYPE_PUBLIC; |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 37 | import static android.provider.Settings.Global.NETSTATS_UID_BUCKET_DURATION; |
junyulai | aa31d3c | 2020-04-10 15:11:54 +0800 | [diff] [blame] | 38 | import static android.telephony.TelephonyManager.UNKNOWN_CARRIER_ID; |
Stanislav Zholnin | 5919913 | 2020-03-06 14:19:25 +0000 | [diff] [blame] | 39 | import static android.util.MathUtils.constrain; |
Tej Singh | 953ad86 | 2020-01-03 12:47:07 -0800 | [diff] [blame] | 40 | |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 41 | import static com.android.internal.util.ConcurrentUtils.DIRECT_EXECUTOR; |
Muhammad Qureshi | 22e52da | 2020-03-30 21:14:21 -0700 | [diff] [blame] | 42 | import static com.android.internal.util.FrameworkStatsLog.ANNOTATION_ID_IS_UID; |
Muhammad Qureshi | 09fc8c4 | 2020-05-06 14:50:54 -0700 | [diff] [blame] | 43 | import static com.android.internal.util.FrameworkStatsLog.ANNOTATION_ID_TRUNCATE_TIMESTAMP; |
junyulai | aa31d3c | 2020-04-10 15:11:54 +0800 | [diff] [blame] | 44 | import static com.android.internal.util.FrameworkStatsLog.DATA_USAGE_BYTES_TRANSFER__OPPORTUNISTIC_DATA_SUB__NOT_OPPORTUNISTIC; |
| 45 | import static com.android.internal.util.FrameworkStatsLog.DATA_USAGE_BYTES_TRANSFER__OPPORTUNISTIC_DATA_SUB__OPPORTUNISTIC; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 46 | import static com.android.server.am.MemoryStatUtil.readMemoryStatFromFilesystem; |
Tej Singh | 250e7aa | 2020-01-16 13:16:21 -0800 | [diff] [blame] | 47 | import static com.android.server.stats.pull.IonMemoryUtil.readProcessSystemIonHeapSizesFromDebugfs; |
| 48 | import static com.android.server.stats.pull.IonMemoryUtil.readSystemIonHeapSizeFromDebugfs; |
Ioannis Ilkos | 754eb07 | 2020-01-27 16:42:24 +0000 | [diff] [blame] | 49 | import static com.android.server.stats.pull.ProcfsMemoryUtil.getProcessCmdlines; |
Tej Singh | 250e7aa | 2020-01-16 13:16:21 -0800 | [diff] [blame] | 50 | import static com.android.server.stats.pull.ProcfsMemoryUtil.readCmdlineFromProcfs; |
| 51 | import static com.android.server.stats.pull.ProcfsMemoryUtil.readMemorySnapshotFromProcfs; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 52 | |
Stanislav Zholnin | 841c9ab | 2020-05-24 04:56:18 +0100 | [diff] [blame] | 53 | import static java.lang.Math.min; |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 54 | import static java.util.concurrent.TimeUnit.HOURS; |
junyulai | 212a5f0e | 2020-02-07 17:42:39 +0800 | [diff] [blame] | 55 | import static java.util.concurrent.TimeUnit.MICROSECONDS; |
| 56 | |
| 57 | import android.annotation.NonNull; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 58 | import android.annotation.Nullable; |
| 59 | import android.app.ActivityManagerInternal; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 60 | import android.app.AppOpsManager; |
Stanislav Zholnin | 841c9ab | 2020-05-24 04:56:18 +0100 | [diff] [blame] | 61 | import android.app.AppOpsManager.HistoricalOp; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 62 | import android.app.AppOpsManager.HistoricalOps; |
| 63 | import android.app.AppOpsManager.HistoricalOpsRequest; |
| 64 | import android.app.AppOpsManager.HistoricalPackageOps; |
| 65 | import android.app.AppOpsManager.HistoricalUidOps; |
| 66 | import android.app.INotificationManager; |
| 67 | import android.app.ProcessMemoryState; |
Stanislav Zholnin | df3eeac | 2020-02-17 18:06:07 +0000 | [diff] [blame] | 68 | import android.app.RuntimeAppOpAccessMessage; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 69 | import android.app.StatsManager; |
| 70 | import android.app.StatsManager.PullAtomMetadata; |
| 71 | import android.bluetooth.BluetoothActivityEnergyInfo; |
| 72 | import android.bluetooth.BluetoothAdapter; |
| 73 | import android.bluetooth.UidTraffic; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 74 | import android.content.Context; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 75 | import android.content.pm.ApplicationInfo; |
| 76 | import android.content.pm.PackageInfo; |
| 77 | import android.content.pm.PackageManager; |
| 78 | import android.content.pm.PermissionInfo; |
| 79 | import android.content.pm.UserInfo; |
| 80 | import android.hardware.biometrics.BiometricsProtoEnums; |
| 81 | import android.hardware.face.FaceManager; |
| 82 | import android.hardware.fingerprint.FingerprintManager; |
Jeffrey Huang | 9a3c1b6 | 2020-01-27 17:33:13 -0800 | [diff] [blame] | 83 | import android.hardware.health.V2_0.IHealth; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 84 | import android.net.ConnectivityManager; |
| 85 | import android.net.INetworkStatsService; |
junyulai | 212a5f0e | 2020-02-07 17:42:39 +0800 | [diff] [blame] | 86 | import android.net.INetworkStatsSession; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 87 | import android.net.Network; |
| 88 | import android.net.NetworkRequest; |
| 89 | import android.net.NetworkStats; |
junyulai | 212a5f0e | 2020-02-07 17:42:39 +0800 | [diff] [blame] | 90 | import android.net.NetworkTemplate; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 91 | import android.net.wifi.WifiManager; |
Stanislav Zholnin | 841c9ab | 2020-05-24 04:56:18 +0100 | [diff] [blame] | 92 | import android.os.AsyncTask; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 93 | import android.os.BatteryStats; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 94 | import android.os.Binder; |
| 95 | import android.os.Build; |
| 96 | import android.os.Bundle; |
| 97 | import android.os.CoolingDevice; |
| 98 | import android.os.Environment; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 99 | import android.os.IStoraged; |
| 100 | import android.os.IThermalEventListener; |
| 101 | import android.os.IThermalService; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 102 | import android.os.ParcelFileDescriptor; |
| 103 | import android.os.Parcelable; |
| 104 | import android.os.RemoteException; |
| 105 | import android.os.ServiceManager; |
| 106 | import android.os.StatFs; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 107 | import android.os.SynchronousResultReceiver; |
| 108 | import android.os.SystemClock; |
| 109 | import android.os.SystemProperties; |
| 110 | import android.os.Temperature; |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 111 | import android.os.Trace; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 112 | import android.os.UserHandle; |
| 113 | import android.os.UserManager; |
| 114 | import android.os.connectivity.WifiActivityEnergyInfo; |
| 115 | import android.os.storage.DiskInfo; |
| 116 | import android.os.storage.StorageManager; |
| 117 | import android.os.storage.VolumeInfo; |
Stanislav Zholnin | 2f63e58 | 2020-04-19 12:10:05 +0100 | [diff] [blame] | 118 | import android.provider.DeviceConfig; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 119 | import android.provider.Settings; |
| 120 | import android.stats.storage.StorageEnums; |
| 121 | import android.telephony.ModemActivityInfo; |
junyulai | aa31d3c | 2020-04-10 15:11:54 +0800 | [diff] [blame] | 122 | import android.telephony.SubscriptionInfo; |
| 123 | import android.telephony.SubscriptionManager; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 124 | import android.telephony.TelephonyManager; |
junyulai | aa31d3c | 2020-04-10 15:11:54 +0800 | [diff] [blame] | 125 | import android.text.TextUtils; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 126 | import android.util.ArrayMap; |
| 127 | import android.util.ArraySet; |
| 128 | import android.util.Log; |
| 129 | import android.util.Slog; |
Ioannis Ilkos | 754eb07 | 2020-01-27 16:42:24 +0000 | [diff] [blame] | 130 | import android.util.SparseArray; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 131 | import android.util.StatsEvent; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 132 | import android.util.proto.ProtoOutputStream; |
| 133 | |
| 134 | import com.android.internal.annotations.GuardedBy; |
| 135 | import com.android.internal.app.procstats.IProcessStats; |
| 136 | import com.android.internal.app.procstats.ProcessStats; |
Tej Singh | 953ad86 | 2020-01-03 12:47:07 -0800 | [diff] [blame] | 137 | import com.android.internal.os.BackgroundThread; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 138 | import com.android.internal.os.BatterySipper; |
| 139 | import com.android.internal.os.BatteryStatsHelper; |
| 140 | import com.android.internal.os.BinderCallsStats.ExportedCallStat; |
| 141 | import com.android.internal.os.KernelCpuSpeedReader; |
| 142 | import com.android.internal.os.KernelCpuThreadReader; |
| 143 | import com.android.internal.os.KernelCpuThreadReaderDiff; |
| 144 | import com.android.internal.os.KernelCpuThreadReaderSettingsObserver; |
| 145 | import com.android.internal.os.KernelCpuUidTimeReader.KernelCpuUidActiveTimeReader; |
| 146 | import com.android.internal.os.KernelCpuUidTimeReader.KernelCpuUidClusterTimeReader; |
| 147 | import com.android.internal.os.KernelCpuUidTimeReader.KernelCpuUidFreqTimeReader; |
| 148 | import com.android.internal.os.KernelCpuUidTimeReader.KernelCpuUidUserSysTimeReader; |
| 149 | import com.android.internal.os.KernelWakelockReader; |
| 150 | import com.android.internal.os.KernelWakelockStats; |
| 151 | import com.android.internal.os.LooperStats; |
| 152 | import com.android.internal.os.PowerProfile; |
| 153 | import com.android.internal.os.ProcessCpuTracker; |
| 154 | import com.android.internal.os.StoragedUidIoStatsReader; |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 155 | import com.android.internal.util.CollectionUtils; |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 156 | import com.android.internal.util.FrameworkStatsLog; |
Jeffrey Huang | 9a3c1b6 | 2020-01-27 17:33:13 -0800 | [diff] [blame] | 157 | import com.android.server.BatteryService; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 158 | import com.android.server.BinderCallsStatsService; |
| 159 | import com.android.server.LocalServices; |
Tej Singh | 953ad86 | 2020-01-03 12:47:07 -0800 | [diff] [blame] | 160 | import com.android.server.SystemService; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 161 | import com.android.server.SystemServiceManager; |
| 162 | import com.android.server.am.MemoryStatUtil.MemoryStat; |
| 163 | import com.android.server.notification.NotificationManagerService; |
| 164 | import com.android.server.role.RoleManagerInternal; |
Tej Singh | 250e7aa | 2020-01-16 13:16:21 -0800 | [diff] [blame] | 165 | import com.android.server.stats.pull.IonMemoryUtil.IonAllocations; |
| 166 | import com.android.server.stats.pull.ProcfsMemoryUtil.MemorySnapshot; |
junyulai | c2d3a68 | 2020-05-22 11:44:03 +0800 | [diff] [blame] | 167 | import com.android.server.stats.pull.netstats.NetworkStatsExt; |
junyulai | aa31d3c | 2020-04-10 15:11:54 +0800 | [diff] [blame] | 168 | import com.android.server.stats.pull.netstats.SubInfo; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 169 | import com.android.server.storage.DiskStatsFileLogger; |
| 170 | import com.android.server.storage.DiskStatsLoggingService; |
| 171 | |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 172 | import libcore.io.IoUtils; |
| 173 | |
| 174 | import org.json.JSONArray; |
| 175 | import org.json.JSONException; |
| 176 | import org.json.JSONObject; |
| 177 | |
| 178 | import java.io.File; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 179 | import java.io.FileOutputStream; |
| 180 | import java.io.IOException; |
| 181 | import java.io.InputStream; |
Stanislav Zholnin | 5919913 | 2020-03-06 14:19:25 +0000 | [diff] [blame] | 182 | import java.time.Instant; |
| 183 | import java.time.temporal.ChronoUnit; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 184 | import java.util.ArrayList; |
| 185 | import java.util.Arrays; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 186 | import java.util.HashSet; |
| 187 | import java.util.List; |
| 188 | import java.util.Map; |
Ruchir Rastogi | bd2c4ba | 2020-01-15 14:48:36 -0800 | [diff] [blame] | 189 | import java.util.MissingResourceException; |
Stanislav Zholnin | 5919913 | 2020-03-06 14:19:25 +0000 | [diff] [blame] | 190 | import java.util.Random; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 191 | import java.util.Set; |
| 192 | import java.util.UUID; |
| 193 | import java.util.concurrent.CompletableFuture; |
| 194 | import java.util.concurrent.Executor; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 195 | import java.util.concurrent.ThreadLocalRandom; |
| 196 | import java.util.concurrent.TimeUnit; |
| 197 | import java.util.concurrent.TimeoutException; |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 198 | import java.util.function.BiConsumer; |
Tej Singh | 953ad86 | 2020-01-03 12:47:07 -0800 | [diff] [blame] | 199 | |
| 200 | /** |
| 201 | * SystemService containing PullAtomCallbacks that are registered with statsd. |
| 202 | * |
| 203 | * @hide |
| 204 | */ |
| 205 | public class StatsPullAtomService extends SystemService { |
| 206 | private static final String TAG = "StatsPullAtomService"; |
| 207 | private static final boolean DEBUG = true; |
| 208 | |
Stanislav Zholnin | 5919913 | 2020-03-06 14:19:25 +0000 | [diff] [blame] | 209 | // Random seed stable for StatsPullAtomService life cycle - can be used for stable sampling |
| 210 | private static final int RANDOM_SEED = new Random().nextInt(); |
| 211 | |
Stanislav Zholnin | 841c9ab | 2020-05-24 04:56:18 +0100 | [diff] [blame] | 212 | private static final int DIMENSION_KEY_SIZE_HARD_LIMIT = 800; |
| 213 | private static final int DIMENSION_KEY_SIZE_SOFT_LIMIT = 500; |
| 214 | private static final long APP_OPS_SAMPLING_INITIALIZATION_DELAY_MILLIS = 45000; |
| 215 | private static final int APP_OPS_SIZE_ESTIMATE = 5000; |
| 216 | |
Jeffrey Huang | fa91789 | 2020-01-10 16:58:03 -0800 | [diff] [blame] | 217 | private static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity"; |
| 218 | /** |
| 219 | * How long to wait on an individual subsystem to return its stats. |
| 220 | */ |
| 221 | private static final long EXTERNAL_STATS_SYNC_TIMEOUT_MILLIS = 2000; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 222 | private static final long MILLIS_PER_SEC = 1000; |
Jeffrey Huang | a85d8f9 | 2020-01-24 13:28:45 -0800 | [diff] [blame] | 223 | private static final long MILLI_AMP_HR_TO_NANO_AMP_SECS = 1_000_000L * 3600L; |
| 224 | |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 225 | /** |
| 226 | * The default bucket duration used when query a snapshot from NetworkStatsService. |
| 227 | * The value should be sync with NetworkStatsService#DefaultNetworkStatsSettings#getUidConfig. |
| 228 | */ |
| 229 | private static final long NETSTATS_UID_DEFAULT_BUCKET_DURATION_MS = HOURS.toMillis(2); |
| 230 | |
Jeffrey Huang | a85d8f9 | 2020-01-24 13:28:45 -0800 | [diff] [blame] | 231 | private static final int MAX_BATTERY_STATS_HELPER_FREQUENCY_MS = 1000; |
| 232 | private static final int CPU_TIME_PER_THREAD_FREQ_MAX_NUM_FREQUENCIES = 8; |
Stanislav Zholnin | 73a8bbb | 2020-05-21 19:26:44 +0000 | [diff] [blame] | 233 | private static final int OP_FLAGS_PULLED = OP_FLAG_SELF | OP_FLAG_TRUSTED_PROXIED; |
Stanislav Zholnin | dbcf85e | 2020-02-24 18:17:55 +0000 | [diff] [blame] | 234 | private static final String COMMON_PERMISSION_PREFIX = "android.permission."; |
Stanislav Zholnin | 2f63e58 | 2020-04-19 12:10:05 +0100 | [diff] [blame] | 235 | private static final String APP_OPS_TARGET_COLLECTION_SIZE = "app_ops_target_collection_size"; |
Stanislav Zholnin | a093ae7 | 2020-04-18 19:14:17 +0100 | [diff] [blame] | 236 | private static final String DANGEROUS_PERMISSION_STATE_SAMPLE_RATE = |
| 237 | "dangerous_permission_state_sample_rate"; |
Jeffrey Huang | fa91789 | 2020-01-10 16:58:03 -0800 | [diff] [blame] | 238 | |
Richard Gaywood | 52a2edf | 2020-06-14 17:39:15 +0100 | [diff] [blame] | 239 | /** Parameters relating to ProcStats data upload. */ |
| 240 | // Maximum shards to use when generating StatsEvent objects from ProcStats. |
| 241 | private static final int MAX_PROCSTATS_SHARDS = 5; |
| 242 | // Should match MAX_PAYLOAD_SIZE in StatsEvent, minus a small amount for overhead/metadata. |
| 243 | private static final int MAX_PROCSTATS_SHARD_SIZE = 48 * 1024; // 48 KB |
| 244 | // In ProcessStats, we measure the size of a raw ProtoOutputStream, before compaction. This |
| 245 | // typically runs 35-45% larger than the compacted size that will be written to StatsEvent. |
| 246 | // Hence, we can allow a little more room in each shard before moving to the next. Make this |
| 247 | // 20% as a conservative estimate. |
| 248 | private static final int MAX_PROCSTATS_RAW_SHARD_SIZE = (int) (MAX_PROCSTATS_SHARD_SIZE * 1.20); |
| 249 | |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 250 | private final Object mThermalLock = new Object(); |
| 251 | @GuardedBy("mThermalLock") |
| 252 | private IThermalService mThermalService; |
Ruchir Rastogi | 92cf9a2 | 2020-01-17 15:55:39 -0800 | [diff] [blame] | 253 | |
Jeffrey Huang | 95765f0 | 2020-01-16 11:33:58 -0800 | [diff] [blame] | 254 | private final Object mStoragedLock = new Object(); |
| 255 | @GuardedBy("mStoragedLock") |
| 256 | private IStoraged mStorageService; |
Ruchir Rastogi | 92cf9a2 | 2020-01-17 15:55:39 -0800 | [diff] [blame] | 257 | |
Jeffrey Huang | 730bf96 | 2020-01-16 15:59:52 -0800 | [diff] [blame] | 258 | private final Object mNotificationStatsLock = new Object(); |
| 259 | @GuardedBy("mNotificationStatsLock") |
| 260 | private INotificationManager mNotificationManagerService; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 261 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 262 | @GuardedBy("mProcStatsLock") |
Ruchir Rastogi | 92cf9a2 | 2020-01-17 15:55:39 -0800 | [diff] [blame] | 263 | private IProcessStats mProcessStatsService; |
| 264 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 265 | @GuardedBy("mProcessCpuTimeLock") |
Jeffrey Huang | a85d8f9 | 2020-01-24 13:28:45 -0800 | [diff] [blame] | 266 | private ProcessCpuTracker mProcessCpuTracker; |
| 267 | |
Jeffrey Huang | a85d8f9 | 2020-01-24 13:28:45 -0800 | [diff] [blame] | 268 | @GuardedBy("mDebugElapsedClockLock") |
| 269 | private long mDebugElapsedClockPreviousValue = 0; |
| 270 | @GuardedBy("mDebugElapsedClockLock") |
| 271 | private long mDebugElapsedClockPullCount = 0; |
| 272 | |
Jeffrey Huang | a85d8f9 | 2020-01-24 13:28:45 -0800 | [diff] [blame] | 273 | @GuardedBy("mDebugFailingElapsedClockLock") |
| 274 | private long mDebugFailingElapsedClockPreviousValue = 0; |
| 275 | @GuardedBy("mDebugFailingElapsedClockLock") |
| 276 | private long mDebugFailingElapsedClockPullCount = 0; |
| 277 | |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 278 | private final Context mContext; |
| 279 | private StatsManager mStatsManager; |
Ruchir Rastogi | 6fc44a8 | 2020-01-17 10:22:41 -0800 | [diff] [blame] | 280 | private StorageManager mStorageManager; |
Jeffrey Huang | a85d8f9 | 2020-01-24 13:28:45 -0800 | [diff] [blame] | 281 | private WifiManager mWifiManager; |
| 282 | private TelephonyManager mTelephony; |
junyulai | aa31d3c | 2020-04-10 15:11:54 +0800 | [diff] [blame] | 283 | private SubscriptionManager mSubscriptionManager; |
Jeffrey Huang | a85d8f9 | 2020-01-24 13:28:45 -0800 | [diff] [blame] | 284 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 285 | @GuardedBy("mKernelWakelockLock") |
Jeffrey Huang | 1c0fe04 | 2020-01-27 13:22:05 -0800 | [diff] [blame] | 286 | private KernelWakelockReader mKernelWakelockReader; |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 287 | @GuardedBy("mKernelWakelockLock") |
Jeffrey Huang | 1c0fe04 | 2020-01-27 13:22:05 -0800 | [diff] [blame] | 288 | private KernelWakelockStats mTmpWakelockStats; |
Jeffrey Huang | a85d8f9 | 2020-01-24 13:28:45 -0800 | [diff] [blame] | 289 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 290 | @GuardedBy("mDiskIoLock") |
Jeffrey Huang | 1c0fe04 | 2020-01-27 13:22:05 -0800 | [diff] [blame] | 291 | private StoragedUidIoStatsReader mStoragedUidIoStatsReader; |
Jeffrey Huang | a85d8f9 | 2020-01-24 13:28:45 -0800 | [diff] [blame] | 292 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 293 | @GuardedBy("mCpuTimePerFreqLock") |
Jeffrey Huang | a85d8f9 | 2020-01-24 13:28:45 -0800 | [diff] [blame] | 294 | private KernelCpuSpeedReader[] mKernelCpuSpeedReaders; |
| 295 | // Disables throttler on CPU time readers. |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 296 | @GuardedBy("mCpuTimePerUidLock") |
Jeffrey Huang | 1c0fe04 | 2020-01-27 13:22:05 -0800 | [diff] [blame] | 297 | private KernelCpuUidUserSysTimeReader mCpuUidUserSysTimeReader; |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 298 | @GuardedBy("mCpuTimePerUidFreqLock") |
Jeffrey Huang | 1c0fe04 | 2020-01-27 13:22:05 -0800 | [diff] [blame] | 299 | private KernelCpuUidFreqTimeReader mCpuUidFreqTimeReader; |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 300 | @GuardedBy("mCpuActiveTimeLock") |
Jeffrey Huang | 1c0fe04 | 2020-01-27 13:22:05 -0800 | [diff] [blame] | 301 | private KernelCpuUidActiveTimeReader mCpuUidActiveTimeReader; |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 302 | @GuardedBy("mClusterTimeLock") |
Jeffrey Huang | 1c0fe04 | 2020-01-27 13:22:05 -0800 | [diff] [blame] | 303 | private KernelCpuUidClusterTimeReader mCpuUidClusterTimeReader; |
Jeffrey Huang | a85d8f9 | 2020-01-24 13:28:45 -0800 | [diff] [blame] | 304 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 305 | @GuardedBy("mProcStatsLock") |
Jeffrey Huang | 1c0fe04 | 2020-01-27 13:22:05 -0800 | [diff] [blame] | 306 | private File mBaseDir; |
Jeffrey Huang | a85d8f9 | 2020-01-24 13:28:45 -0800 | [diff] [blame] | 307 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 308 | @GuardedBy("mHealthHalLock") |
Jeffrey Huang | 9a3c1b6 | 2020-01-27 17:33:13 -0800 | [diff] [blame] | 309 | private BatteryService.HealthServiceWrapper mHealthService; |
| 310 | |
Jeffrey Huang | a85d8f9 | 2020-01-24 13:28:45 -0800 | [diff] [blame] | 311 | @Nullable |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 312 | @GuardedBy("mCpuTimePerThreadFreqLock") |
Jeffrey Huang | a85d8f9 | 2020-01-24 13:28:45 -0800 | [diff] [blame] | 313 | private KernelCpuThreadReaderDiff mKernelCpuThreadReader; |
| 314 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 315 | private final Object mBatteryStatsHelperLock = new Object(); |
| 316 | @GuardedBy("mBatteryStatsHelperLock") |
Jeffrey Huang | a85d8f9 | 2020-01-24 13:28:45 -0800 | [diff] [blame] | 317 | private BatteryStatsHelper mBatteryStatsHelper = null; |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 318 | @GuardedBy("mBatteryStatsHelperLock") |
Jeffrey Huang | a85d8f9 | 2020-01-24 13:28:45 -0800 | [diff] [blame] | 319 | private long mBatteryStatsHelperTimestampMs = -MAX_BATTERY_STATS_HELPER_FREQUENCY_MS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 320 | |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 321 | private StatsPullAtomCallbackImpl mStatsCallbackImpl; |
| 322 | |
Stanislav Zholnin | 841c9ab | 2020-05-24 04:56:18 +0100 | [diff] [blame] | 323 | private final Object mAppOpsSamplingRateLock = new Object(); |
| 324 | @GuardedBy("mAppOpsSamplingRateLock") |
Stanislav Zholnin | 5919913 | 2020-03-06 14:19:25 +0000 | [diff] [blame] | 325 | private int mAppOpsSamplingRate = 0; |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 326 | private final Object mDangerousAppOpsListLock = new Object(); |
| 327 | @GuardedBy("mDangerousAppOpsListLock") |
Stanislav Zholnin | e08a2bb | 2020-05-17 17:51:31 +0100 | [diff] [blame] | 328 | private final ArraySet<Integer> mDangerousAppOpsList = new ArraySet<>(); |
Stanislav Zholnin | 5919913 | 2020-03-06 14:19:25 +0000 | [diff] [blame] | 329 | |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 330 | // Baselines that stores list of NetworkStats right after initializing, with associated |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 331 | // information. This is used to calculate difference when pulling BytesTransfer atoms. |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 332 | @NonNull |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 333 | @GuardedBy("mDataBytesTransferLock") |
| 334 | private final ArrayList<NetworkStatsExt> mNetworkStatsBaselines = new ArrayList<>(); |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 335 | |
junyulai | aa31d3c | 2020-04-10 15:11:54 +0800 | [diff] [blame] | 336 | // Listener for monitoring subscriptions changed event. |
| 337 | private StatsSubscriptionsListener mStatsSubscriptionsListener; |
junyulai | 66e735e | 2020-05-29 11:24:58 +0800 | [diff] [blame] | 338 | // List that stores SubInfo of subscriptions that ever appeared since boot. |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 339 | @GuardedBy("mDataBytesTransferLock") |
| 340 | private final ArrayList<SubInfo> mHistoricalSubs = new ArrayList<>(); |
| 341 | |
| 342 | // Puller locks |
| 343 | private final Object mDataBytesTransferLock = new Object(); |
| 344 | private final Object mBluetoothBytesTransferLock = new Object(); |
| 345 | private final Object mKernelWakelockLock = new Object(); |
| 346 | private final Object mCpuTimePerFreqLock = new Object(); |
| 347 | private final Object mCpuTimePerUidLock = new Object(); |
| 348 | private final Object mCpuTimePerUidFreqLock = new Object(); |
| 349 | private final Object mCpuActiveTimeLock = new Object(); |
| 350 | private final Object mCpuClusterTimeLock = new Object(); |
| 351 | private final Object mWifiActivityInfoLock = new Object(); |
| 352 | private final Object mModemActivityInfoLock = new Object(); |
| 353 | private final Object mBluetoothActivityInfoLock = new Object(); |
| 354 | private final Object mSystemElapsedRealtimeLock = new Object(); |
| 355 | private final Object mSystemUptimeLock = new Object(); |
| 356 | private final Object mProcessMemoryStateLock = new Object(); |
| 357 | private final Object mProcessMemoryHighWaterMarkLock = new Object(); |
| 358 | private final Object mProcessMemorySnapshotLock = new Object(); |
| 359 | private final Object mSystemIonHeapSizeLock = new Object(); |
| 360 | private final Object mIonHeapSizeLock = new Object(); |
| 361 | private final Object mProcessSystemIonHeapSizeLock = new Object(); |
| 362 | private final Object mTemperatureLock = new Object(); |
| 363 | private final Object mCooldownDeviceLock = new Object(); |
| 364 | private final Object mBinderCallsStatsLock = new Object(); |
| 365 | private final Object mBinderCallsStatsExceptionsLock = new Object(); |
| 366 | private final Object mLooperStatsLock = new Object(); |
| 367 | private final Object mDiskStatsLock = new Object(); |
| 368 | private final Object mDirectoryUsageLock = new Object(); |
| 369 | private final Object mAppSizeLock = new Object(); |
| 370 | private final Object mCategorySizeLock = new Object(); |
| 371 | private final Object mNumBiometricsEnrolledLock = new Object(); |
| 372 | private final Object mProcStatsLock = new Object(); |
| 373 | private final Object mDiskIoLock = new Object(); |
| 374 | private final Object mPowerProfileLock = new Object(); |
| 375 | private final Object mProcessCpuTimeLock = new Object(); |
| 376 | private final Object mCpuTimePerThreadFreqLock = new Object(); |
| 377 | private final Object mDeviceCalculatedPowerUseLock = new Object(); |
| 378 | private final Object mDeviceCalculatedPowerBlameUidLock = new Object(); |
| 379 | private final Object mDeviceCalculatedPowerBlameOtherLock = new Object(); |
| 380 | private final Object mDebugElapsedClockLock = new Object(); |
| 381 | private final Object mDebugFailingElapsedClockLock = new Object(); |
| 382 | private final Object mBuildInformationLock = new Object(); |
| 383 | private final Object mRoleHolderLock = new Object(); |
| 384 | private final Object mTimeZoneDataInfoLock = new Object(); |
| 385 | private final Object mExternalStorageInfoLock = new Object(); |
| 386 | private final Object mAppsOnExternalStorageInfoLock = new Object(); |
| 387 | private final Object mFaceSettingsLock = new Object(); |
| 388 | private final Object mAppOpsLock = new Object(); |
| 389 | private final Object mRuntimeAppOpAccessMessageLock = new Object(); |
| 390 | private final Object mNotificationRemoteViewsLock = new Object(); |
| 391 | private final Object mDangerousPermissionStateLock = new Object(); |
| 392 | private final Object mHealthHalLock = new Object(); |
| 393 | private final Object mAttributedAppOpsLock = new Object(); |
| 394 | private final Object mSettingsStatsLock = new Object(); |
junyulai | aa31d3c | 2020-04-10 15:11:54 +0800 | [diff] [blame] | 395 | |
Tej Singh | 953ad86 | 2020-01-03 12:47:07 -0800 | [diff] [blame] | 396 | public StatsPullAtomService(Context context) { |
| 397 | super(context); |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 398 | mContext = context; |
Tej Singh | 953ad86 | 2020-01-03 12:47:07 -0800 | [diff] [blame] | 399 | } |
| 400 | |
Jeffrey Huang | 4c52716 | 2020-01-30 17:53:13 -0800 | [diff] [blame] | 401 | private native void nativeInit(); |
| 402 | |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 403 | /** |
| 404 | * Use of this StatsPullAtomCallbackImpl means we avoid one class per tagId, which we would |
| 405 | * get if we used lambdas. |
| 406 | * |
| 407 | * The pull methods are intentionally left to be package private to avoid the creation |
| 408 | * of synthetic methods to save unnecessary bytecode. |
| 409 | */ |
| 410 | private class StatsPullAtomCallbackImpl implements StatsManager.StatsPullAtomCallback { |
| 411 | @Override |
| 412 | public int onPullAtom(int atomTag, List<StatsEvent> data) { |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 413 | if (Trace.isTagEnabled(Trace.TRACE_TAG_SYSTEM_SERVER)) { |
| 414 | Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StatsPull-" + atomTag); |
| 415 | } |
| 416 | try { |
| 417 | switch (atomTag) { |
| 418 | case FrameworkStatsLog.WIFI_BYTES_TRANSFER: |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 419 | case FrameworkStatsLog.WIFI_BYTES_TRANSFER_BY_FG_BG: |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 420 | case FrameworkStatsLog.MOBILE_BYTES_TRANSFER: |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 421 | case FrameworkStatsLog.MOBILE_BYTES_TRANSFER_BY_FG_BG: |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 422 | case FrameworkStatsLog.BYTES_TRANSFER_BY_TAG_AND_METERED: |
junyulai | ee3ea79 | 2020-05-15 18:26:25 +0800 | [diff] [blame] | 423 | case FrameworkStatsLog.DATA_USAGE_BYTES_TRANSFER: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 424 | synchronized (mDataBytesTransferLock) { |
| 425 | return pullDataBytesTransferLocked(atomTag, data); |
| 426 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 427 | case FrameworkStatsLog.BLUETOOTH_BYTES_TRANSFER: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 428 | synchronized (mBluetoothBytesTransferLock) { |
| 429 | return pullBluetoothBytesTransferLocked(atomTag, data); |
| 430 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 431 | case FrameworkStatsLog.KERNEL_WAKELOCK: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 432 | synchronized (mKernelWakelockLock) { |
| 433 | return pullKernelWakelockLocked(atomTag, data); |
| 434 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 435 | case FrameworkStatsLog.CPU_TIME_PER_FREQ: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 436 | synchronized (mCpuTimePerFreqLock) { |
| 437 | return pullCpuTimePerFreqLocked(atomTag, data); |
| 438 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 439 | case FrameworkStatsLog.CPU_TIME_PER_UID: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 440 | synchronized (mCpuTimePerUidLock) { |
| 441 | return pullCpuTimePerUidLocked(atomTag, data); |
| 442 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 443 | case FrameworkStatsLog.CPU_TIME_PER_UID_FREQ: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 444 | synchronized (mCpuTimePerUidFreqLock) { |
| 445 | return pullCpuTimePerUidFreqLocked(atomTag, data); |
| 446 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 447 | case FrameworkStatsLog.CPU_ACTIVE_TIME: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 448 | synchronized (mCpuActiveTimeLock) { |
| 449 | return pullCpuActiveTimeLocked(atomTag, data); |
| 450 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 451 | case FrameworkStatsLog.CPU_CLUSTER_TIME: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 452 | synchronized (mCpuClusterTimeLock) { |
| 453 | return pullCpuClusterTimeLocked(atomTag, data); |
| 454 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 455 | case FrameworkStatsLog.WIFI_ACTIVITY_INFO: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 456 | synchronized (mWifiActivityInfoLock) { |
| 457 | return pullWifiActivityInfoLocked(atomTag, data); |
| 458 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 459 | case FrameworkStatsLog.MODEM_ACTIVITY_INFO: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 460 | synchronized (mModemActivityInfoLock) { |
| 461 | return pullModemActivityInfoLocked(atomTag, data); |
| 462 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 463 | case FrameworkStatsLog.BLUETOOTH_ACTIVITY_INFO: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 464 | synchronized (mBluetoothActivityInfoLock) { |
| 465 | return pullBluetoothActivityInfoLocked(atomTag, data); |
| 466 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 467 | case FrameworkStatsLog.SYSTEM_ELAPSED_REALTIME: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 468 | synchronized (mSystemElapsedRealtimeLock) { |
| 469 | return pullSystemElapsedRealtimeLocked(atomTag, data); |
| 470 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 471 | case FrameworkStatsLog.SYSTEM_UPTIME: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 472 | synchronized (mSystemUptimeLock) { |
| 473 | return pullSystemUptimeLocked(atomTag, data); |
| 474 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 475 | case FrameworkStatsLog.PROCESS_MEMORY_STATE: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 476 | synchronized (mProcessMemoryStateLock) { |
| 477 | return pullProcessMemoryStateLocked(atomTag, data); |
| 478 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 479 | case FrameworkStatsLog.PROCESS_MEMORY_HIGH_WATER_MARK: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 480 | synchronized (mProcessMemoryHighWaterMarkLock) { |
| 481 | return pullProcessMemoryHighWaterMarkLocked(atomTag, data); |
| 482 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 483 | case FrameworkStatsLog.PROCESS_MEMORY_SNAPSHOT: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 484 | synchronized (mProcessMemorySnapshotLock) { |
| 485 | return pullProcessMemorySnapshotLocked(atomTag, data); |
| 486 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 487 | case FrameworkStatsLog.SYSTEM_ION_HEAP_SIZE: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 488 | synchronized (mSystemIonHeapSizeLock) { |
| 489 | return pullSystemIonHeapSizeLocked(atomTag, data); |
| 490 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 491 | case FrameworkStatsLog.ION_HEAP_SIZE: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 492 | synchronized (mIonHeapSizeLock) { |
| 493 | return pullIonHeapSizeLocked(atomTag, data); |
| 494 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 495 | case FrameworkStatsLog.PROCESS_SYSTEM_ION_HEAP_SIZE: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 496 | synchronized (mProcessSystemIonHeapSizeLock) { |
| 497 | return pullProcessSystemIonHeapSizeLocked(atomTag, data); |
| 498 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 499 | case FrameworkStatsLog.TEMPERATURE: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 500 | synchronized (mTemperatureLock) { |
| 501 | return pullTemperatureLocked(atomTag, data); |
| 502 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 503 | case FrameworkStatsLog.COOLING_DEVICE: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 504 | synchronized (mCooldownDeviceLock) { |
| 505 | return pullCooldownDeviceLocked(atomTag, data); |
| 506 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 507 | case FrameworkStatsLog.BINDER_CALLS: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 508 | synchronized (mBinderCallsStatsLock) { |
| 509 | return pullBinderCallsStatsLocked(atomTag, data); |
| 510 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 511 | case FrameworkStatsLog.BINDER_CALLS_EXCEPTIONS: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 512 | synchronized (mBinderCallsStatsExceptionsLock) { |
| 513 | return pullBinderCallsStatsExceptionsLocked(atomTag, data); |
| 514 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 515 | case FrameworkStatsLog.LOOPER_STATS: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 516 | synchronized (mLooperStatsLock) { |
| 517 | return pullLooperStatsLocked(atomTag, data); |
| 518 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 519 | case FrameworkStatsLog.DISK_STATS: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 520 | synchronized (mDiskStatsLock) { |
| 521 | return pullDiskStatsLocked(atomTag, data); |
| 522 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 523 | case FrameworkStatsLog.DIRECTORY_USAGE: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 524 | synchronized (mDirectoryUsageLock) { |
| 525 | return pullDirectoryUsageLocked(atomTag, data); |
| 526 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 527 | case FrameworkStatsLog.APP_SIZE: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 528 | synchronized (mAppSizeLock) { |
| 529 | return pullAppSizeLocked(atomTag, data); |
| 530 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 531 | case FrameworkStatsLog.CATEGORY_SIZE: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 532 | synchronized (mCategorySizeLock) { |
| 533 | return pullCategorySizeLocked(atomTag, data); |
| 534 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 535 | case FrameworkStatsLog.NUM_FINGERPRINTS_ENROLLED: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 536 | synchronized (mNumBiometricsEnrolledLock) { |
| 537 | return pullNumBiometricsEnrolledLocked( |
| 538 | BiometricsProtoEnums.MODALITY_FINGERPRINT, atomTag, data); |
| 539 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 540 | case FrameworkStatsLog.NUM_FACES_ENROLLED: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 541 | synchronized (mNumBiometricsEnrolledLock) { |
| 542 | return pullNumBiometricsEnrolledLocked( |
| 543 | BiometricsProtoEnums.MODALITY_FACE, atomTag, data); |
| 544 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 545 | case FrameworkStatsLog.PROC_STATS: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 546 | synchronized (mProcStatsLock) { |
| 547 | return pullProcStatsLocked(ProcessStats.REPORT_ALL, atomTag, data); |
| 548 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 549 | case FrameworkStatsLog.PROC_STATS_PKG_PROC: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 550 | synchronized (mProcStatsLock) { |
| 551 | return pullProcStatsLocked(ProcessStats.REPORT_PKG_PROC_STATS, atomTag, |
| 552 | data); |
| 553 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 554 | case FrameworkStatsLog.DISK_IO: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 555 | synchronized (mDiskIoLock) { |
| 556 | return pullDiskIOLocked(atomTag, data); |
| 557 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 558 | case FrameworkStatsLog.POWER_PROFILE: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 559 | synchronized (mPowerProfileLock) { |
| 560 | return pullPowerProfileLocked(atomTag, data); |
| 561 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 562 | case FrameworkStatsLog.PROCESS_CPU_TIME: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 563 | synchronized (mProcessCpuTimeLock) { |
| 564 | return pullProcessCpuTimeLocked(atomTag, data); |
| 565 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 566 | case FrameworkStatsLog.CPU_TIME_PER_THREAD_FREQ: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 567 | synchronized (mCpuTimePerThreadFreqLock) { |
| 568 | return pullCpuTimePerThreadFreqLocked(atomTag, data); |
| 569 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 570 | case FrameworkStatsLog.DEVICE_CALCULATED_POWER_USE: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 571 | synchronized (mDeviceCalculatedPowerUseLock) { |
| 572 | return pullDeviceCalculatedPowerUseLocked(atomTag, data); |
| 573 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 574 | case FrameworkStatsLog.DEVICE_CALCULATED_POWER_BLAME_UID: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 575 | synchronized (mDeviceCalculatedPowerBlameUidLock) { |
| 576 | return pullDeviceCalculatedPowerBlameUidLocked(atomTag, data); |
| 577 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 578 | case FrameworkStatsLog.DEVICE_CALCULATED_POWER_BLAME_OTHER: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 579 | synchronized (mDeviceCalculatedPowerBlameOtherLock) { |
| 580 | return pullDeviceCalculatedPowerBlameOtherLocked(atomTag, data); |
| 581 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 582 | case FrameworkStatsLog.DEBUG_ELAPSED_CLOCK: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 583 | synchronized (mDebugElapsedClockLock) { |
| 584 | return pullDebugElapsedClockLocked(atomTag, data); |
| 585 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 586 | case FrameworkStatsLog.DEBUG_FAILING_ELAPSED_CLOCK: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 587 | synchronized (mDebugFailingElapsedClockLock) { |
| 588 | return pullDebugFailingElapsedClockLocked(atomTag, data); |
| 589 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 590 | case FrameworkStatsLog.BUILD_INFORMATION: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 591 | synchronized (mBuildInformationLock) { |
| 592 | return pullBuildInformationLocked(atomTag, data); |
| 593 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 594 | case FrameworkStatsLog.ROLE_HOLDER: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 595 | synchronized (mRoleHolderLock) { |
| 596 | return pullRoleHolderLocked(atomTag, data); |
| 597 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 598 | case FrameworkStatsLog.DANGEROUS_PERMISSION_STATE: |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 599 | case FrameworkStatsLog.DANGEROUS_PERMISSION_STATE_SAMPLED: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 600 | synchronized (mDangerousPermissionStateLock) { |
| 601 | return pullDangerousPermissionStateLocked(atomTag, data); |
| 602 | } |
| 603 | case FrameworkStatsLog.TIME_ZONE_DATA_INFO: |
| 604 | synchronized (mTimeZoneDataInfoLock) { |
| 605 | return pullTimeZoneDataInfoLocked(atomTag, data); |
| 606 | } |
| 607 | case FrameworkStatsLog.EXTERNAL_STORAGE_INFO: |
| 608 | synchronized (mExternalStorageInfoLock) { |
| 609 | return pullExternalStorageInfoLocked(atomTag, data); |
| 610 | } |
| 611 | case FrameworkStatsLog.APPS_ON_EXTERNAL_STORAGE_INFO: |
| 612 | synchronized (mAppsOnExternalStorageInfoLock) { |
| 613 | return pullAppsOnExternalStorageInfoLocked(atomTag, data); |
| 614 | } |
| 615 | case FrameworkStatsLog.FACE_SETTINGS: |
| 616 | synchronized (mFaceSettingsLock) { |
| 617 | return pullFaceSettingsLocked(atomTag, data); |
| 618 | } |
| 619 | case FrameworkStatsLog.APP_OPS: |
| 620 | synchronized (mAppOpsLock) { |
| 621 | return pullAppOpsLocked(atomTag, data); |
| 622 | } |
| 623 | case FrameworkStatsLog.RUNTIME_APP_OP_ACCESS: |
| 624 | synchronized (mRuntimeAppOpAccessMessageLock) { |
| 625 | return pullRuntimeAppOpAccessMessageLocked(atomTag, data); |
| 626 | } |
| 627 | case FrameworkStatsLog.NOTIFICATION_REMOTE_VIEWS: |
| 628 | synchronized (mNotificationRemoteViewsLock) { |
| 629 | return pullNotificationRemoteViewsLocked(atomTag, data); |
| 630 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 631 | case FrameworkStatsLog.BATTERY_LEVEL: |
| 632 | case FrameworkStatsLog.REMAINING_BATTERY_CAPACITY: |
| 633 | case FrameworkStatsLog.FULL_BATTERY_CAPACITY: |
| 634 | case FrameworkStatsLog.BATTERY_VOLTAGE: |
| 635 | case FrameworkStatsLog.BATTERY_CYCLE_COUNT: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 636 | synchronized (mHealthHalLock) { |
| 637 | return pullHealthHalLocked(atomTag, data); |
| 638 | } |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 639 | case FrameworkStatsLog.ATTRIBUTED_APP_OPS: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 640 | synchronized (mAttributedAppOpsLock) { |
| 641 | return pullAttributedAppOpsLocked(atomTag, data); |
| 642 | } |
Raff Tsai | 87cefd4 | 2020-04-07 14:25:02 +0800 | [diff] [blame] | 643 | case FrameworkStatsLog.SETTING_SNAPSHOT: |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 644 | synchronized (mSettingsStatsLock) { |
| 645 | return pullSettingsStatsLocked(atomTag, data); |
| 646 | } |
Ioannis Ilkos | 606bf6e | 2020-02-20 13:25:37 +0000 | [diff] [blame] | 647 | default: |
| 648 | throw new UnsupportedOperationException("Unknown tagId=" + atomTag); |
| 649 | } |
| 650 | } finally { |
| 651 | Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER); |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
Tej Singh | 953ad86 | 2020-01-03 12:47:07 -0800 | [diff] [blame] | 656 | @Override |
| 657 | public void onStart() { |
Jeffrey Huang | 1c0fe04 | 2020-01-27 13:22:05 -0800 | [diff] [blame] | 658 | // no op |
| 659 | } |
| 660 | |
| 661 | @Override |
| 662 | public void onBootPhase(int phase) { |
| 663 | super.onBootPhase(phase); |
| 664 | if (phase == PHASE_SYSTEM_SERVICES_READY) { |
| 665 | BackgroundThread.getHandler().post(() -> { |
Jeffrey Huang | 4c52716 | 2020-01-30 17:53:13 -0800 | [diff] [blame] | 666 | nativeInit(); |
Jeffrey Huang | 1c0fe04 | 2020-01-27 13:22:05 -0800 | [diff] [blame] | 667 | initializePullersState(); |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 668 | registerPullers(); |
Jeffrey Huang | 1c0fe04 | 2020-01-27 13:22:05 -0800 | [diff] [blame] | 669 | registerEventListeners(); |
| 670 | }); |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 671 | } else if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) { |
| 672 | // Network stats related pullers can only be initialized after service is ready. |
| 673 | BackgroundThread.getHandler().post(() -> initAndRegisterNetworkStatsPullers()); |
Jeffrey Huang | 1c0fe04 | 2020-01-27 13:22:05 -0800 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 677 | // We do not hold locks within this function because it is guaranteed to be called before the |
| 678 | // pullers are ever run, as the pullers are not yet registered with statsd. |
Jeffrey Huang | 1c0fe04 | 2020-01-27 13:22:05 -0800 | [diff] [blame] | 679 | void initializePullersState() { |
| 680 | // Get Context Managers |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 681 | mStatsManager = (StatsManager) mContext.getSystemService(Context.STATS_MANAGER); |
Jeffrey Huang | 4df5740 | 2020-01-14 11:57:22 -0800 | [diff] [blame] | 682 | mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); |
Jeffrey Huang | 1f81889 | 2020-01-14 12:05:05 -0800 | [diff] [blame] | 683 | mTelephony = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); |
junyulai | aa31d3c | 2020-04-10 15:11:54 +0800 | [diff] [blame] | 684 | mSubscriptionManager = (SubscriptionManager) |
| 685 | mContext.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE); |
| 686 | mStatsSubscriptionsListener = new StatsSubscriptionsListener(mSubscriptionManager); |
Ruchir Rastogi | 6fc44a8 | 2020-01-17 10:22:41 -0800 | [diff] [blame] | 687 | mStorageManager = (StorageManager) mContext.getSystemService(StorageManager.class); |
Jeffrey Huang | 43d5e2d | 2020-01-13 16:22:36 -0800 | [diff] [blame] | 688 | |
Jeffrey Huang | 1c0fe04 | 2020-01-27 13:22:05 -0800 | [diff] [blame] | 689 | // Initialize DiskIO |
| 690 | mStoragedUidIoStatsReader = new StoragedUidIoStatsReader(); |
Jeffrey Huang | bf13083 | 2020-01-22 15:48:26 -0800 | [diff] [blame] | 691 | |
Jeffrey Huang | 1c0fe04 | 2020-01-27 13:22:05 -0800 | [diff] [blame] | 692 | // Initialize PROC_STATS |
Jeffrey Huang | b2cdb1a5 | 2020-02-24 15:26:13 -0800 | [diff] [blame] | 693 | mBaseDir = new File(SystemServiceManager.ensureSystemDir(), "stats_pull"); |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 694 | mBaseDir.mkdirs(); |
Jeffrey Huang | 1c0fe04 | 2020-01-27 13:22:05 -0800 | [diff] [blame] | 695 | |
| 696 | // Disables throttler on CPU time readers. |
| 697 | mCpuUidUserSysTimeReader = new KernelCpuUidUserSysTimeReader(false); |
| 698 | mCpuUidFreqTimeReader = new KernelCpuUidFreqTimeReader(false); |
| 699 | mCpuUidActiveTimeReader = new KernelCpuUidActiveTimeReader(false); |
| 700 | mCpuUidClusterTimeReader = new KernelCpuUidClusterTimeReader(false); |
| 701 | |
| 702 | // Initialize state for KERNEL_WAKELOCK |
| 703 | mKernelWakelockReader = new KernelWakelockReader(); |
| 704 | mTmpWakelockStats = new KernelWakelockStats(); |
Jeffrey Huang | bf13083 | 2020-01-22 15:48:26 -0800 | [diff] [blame] | 705 | |
Ruchir Rastogi | 92cf9a2 | 2020-01-17 15:55:39 -0800 | [diff] [blame] | 706 | // Initialize state for CPU_TIME_PER_FREQ atom |
Jeffrey Huang | 43d5e2d | 2020-01-13 16:22:36 -0800 | [diff] [blame] | 707 | PowerProfile powerProfile = new PowerProfile(mContext); |
| 708 | final int numClusters = powerProfile.getNumCpuClusters(); |
| 709 | mKernelCpuSpeedReaders = new KernelCpuSpeedReader[numClusters]; |
| 710 | int firstCpuOfCluster = 0; |
| 711 | for (int i = 0; i < numClusters; i++) { |
| 712 | final int numSpeedSteps = powerProfile.getNumSpeedStepsInCpuCluster(i); |
| 713 | mKernelCpuSpeedReaders[i] = new KernelCpuSpeedReader(firstCpuOfCluster, |
| 714 | numSpeedSteps); |
| 715 | firstCpuOfCluster += powerProfile.getNumCoresInCpuCluster(i); |
| 716 | } |
Jeffrey Huang | bf4eef8 | 2020-01-16 15:38:58 -0800 | [diff] [blame] | 717 | |
| 718 | // Used for CPU_TIME_PER_THREAD_FREQ |
| 719 | mKernelCpuThreadReader = |
| 720 | KernelCpuThreadReaderSettingsObserver.getSettingsModifiedReader(mContext); |
Ruchir Rastogi | 92cf9a2 | 2020-01-17 15:55:39 -0800 | [diff] [blame] | 721 | |
Jeffrey Huang | 9a3c1b6 | 2020-01-27 17:33:13 -0800 | [diff] [blame] | 722 | // Initialize HealthService |
| 723 | mHealthService = new BatteryService.HealthServiceWrapper(); |
| 724 | try { |
| 725 | mHealthService.init(); |
| 726 | } catch (RemoteException e) { |
| 727 | Slog.e(TAG, "failed to initialize healthHalWrapper"); |
| 728 | } |
Stanislav Zholnin | e08a2bb | 2020-05-17 17:51:31 +0100 | [diff] [blame] | 729 | |
| 730 | // Initialize list of AppOps related to DangerousPermissions |
| 731 | PackageManager pm = mContext.getPackageManager(); |
| 732 | for (int op = 0; op < AppOpsManager._NUM_OP; op++) { |
| 733 | String perm = AppOpsManager.opToPermission(op); |
| 734 | if (perm == null) { |
| 735 | continue; |
| 736 | } else { |
| 737 | PermissionInfo permInfo; |
| 738 | try { |
| 739 | permInfo = pm.getPermissionInfo(perm, 0); |
| 740 | if (permInfo.getProtection() == PROTECTION_DANGEROUS) { |
| 741 | mDangerousAppOpsList.add(op); |
| 742 | } |
| 743 | } catch (PackageManager.NameNotFoundException exception) { |
| 744 | continue; |
| 745 | } |
| 746 | } |
| 747 | } |
Tej Singh | 953ad86 | 2020-01-03 12:47:07 -0800 | [diff] [blame] | 748 | } |
| 749 | |
Jeffrey Huang | 1c0fe04 | 2020-01-27 13:22:05 -0800 | [diff] [blame] | 750 | void registerEventListeners() { |
| 751 | final ConnectivityManager connectivityManager = |
| 752 | (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 753 | // Default NetworkRequest should cover all transport types. |
| 754 | final NetworkRequest request = new NetworkRequest.Builder().build(); |
| 755 | connectivityManager.registerNetworkCallback(request, new ConnectivityStatsCallback()); |
| 756 | |
| 757 | // Enable push notifications of throttling from vendor thermal |
| 758 | // management subsystem via thermalservice. |
| 759 | IThermalService thermalService = getIThermalService(); |
| 760 | if (thermalService != null) { |
| 761 | try { |
| 762 | thermalService.registerThermalEventListener(new ThermalEventListener()); |
| 763 | Slog.i(TAG, "register thermal listener successfully"); |
| 764 | } catch (RemoteException e) { |
| 765 | Slog.i(TAG, "failed to register thermal listener"); |
| 766 | } |
Tej Singh | 953ad86 | 2020-01-03 12:47:07 -0800 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 770 | void registerPullers() { |
Tej Singh | 953ad86 | 2020-01-03 12:47:07 -0800 | [diff] [blame] | 771 | if (DEBUG) { |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 772 | Slog.d(TAG, "Registering pullers with statsd"); |
Tej Singh | 953ad86 | 2020-01-03 12:47:07 -0800 | [diff] [blame] | 773 | } |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 774 | mStatsCallbackImpl = new StatsPullAtomCallbackImpl(); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 775 | registerBluetoothBytesTransfer(); |
| 776 | registerKernelWakelock(); |
| 777 | registerCpuTimePerFreq(); |
| 778 | registerCpuTimePerUid(); |
| 779 | registerCpuTimePerUidFreq(); |
| 780 | registerCpuActiveTime(); |
| 781 | registerCpuClusterTime(); |
| 782 | registerWifiActivityInfo(); |
| 783 | registerModemActivityInfo(); |
| 784 | registerBluetoothActivityInfo(); |
| 785 | registerSystemElapsedRealtime(); |
| 786 | registerSystemUptime(); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 787 | registerProcessMemoryState(); |
| 788 | registerProcessMemoryHighWaterMark(); |
| 789 | registerProcessMemorySnapshot(); |
| 790 | registerSystemIonHeapSize(); |
Rafal Slawik | bdd5a50 | 2020-01-14 14:14:29 +0000 | [diff] [blame] | 791 | registerIonHeapSize(); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 792 | registerProcessSystemIonHeapSize(); |
| 793 | registerTemperature(); |
| 794 | registerCoolingDevice(); |
Jeffrey Huang | 877adfe | 2020-01-15 17:16:43 -0800 | [diff] [blame] | 795 | registerBinderCallsStats(); |
| 796 | registerBinderCallsStatsExceptions(); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 797 | registerLooperStats(); |
| 798 | registerDiskStats(); |
| 799 | registerDirectoryUsage(); |
| 800 | registerAppSize(); |
| 801 | registerCategorySize(); |
| 802 | registerNumFingerprintsEnrolled(); |
| 803 | registerNumFacesEnrolled(); |
| 804 | registerProcStats(); |
| 805 | registerProcStatsPkgProc(); |
| 806 | registerDiskIO(); |
| 807 | registerPowerProfile(); |
| 808 | registerProcessCpuTime(); |
| 809 | registerCpuTimePerThreadFreq(); |
| 810 | registerDeviceCalculatedPowerUse(); |
| 811 | registerDeviceCalculatedPowerBlameUid(); |
| 812 | registerDeviceCalculatedPowerBlameOther(); |
| 813 | registerDebugElapsedClock(); |
| 814 | registerDebugFailingElapsedClock(); |
| 815 | registerBuildInformation(); |
| 816 | registerRoleHolder(); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 817 | registerTimeZoneDataInfo(); |
| 818 | registerExternalStorageInfo(); |
| 819 | registerAppsOnExternalStorageInfo(); |
| 820 | registerFaceSettings(); |
| 821 | registerAppOps(); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 822 | registerAttributedAppOps(); |
Stanislav Zholnin | df3eeac | 2020-02-17 18:06:07 +0000 | [diff] [blame] | 823 | registerRuntimeAppOpAccessMessage(); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 824 | registerNotificationRemoteViews(); |
| 825 | registerDangerousPermissionState(); |
| 826 | registerDangerousPermissionStateSampled(); |
Jeffrey Huang | 9a3c1b6 | 2020-01-27 17:33:13 -0800 | [diff] [blame] | 827 | registerBatteryLevel(); |
Jeffrey Huang | 43f4d26 | 2020-01-30 14:03:31 -0800 | [diff] [blame] | 828 | registerRemainingBatteryCapacity(); |
| 829 | registerFullBatteryCapacity(); |
| 830 | registerBatteryVoltage(); |
| 831 | registerBatteryCycleCount(); |
Raff Tsai | 87cefd4 | 2020-04-07 14:25:02 +0800 | [diff] [blame] | 832 | registerSettingsStats(); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 833 | } |
| 834 | |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 835 | private void initAndRegisterNetworkStatsPullers() { |
| 836 | if (DEBUG) { |
| 837 | Slog.d(TAG, "Registering NetworkStats pullers with statsd"); |
| 838 | } |
| 839 | // Initialize NetworkStats baselines. |
junyulai | 9f0146a | 2020-05-14 15:07:21 +0800 | [diff] [blame] | 840 | mNetworkStatsBaselines.addAll( |
| 841 | collectNetworkStatsSnapshotForAtom(FrameworkStatsLog.WIFI_BYTES_TRANSFER)); |
| 842 | mNetworkStatsBaselines.addAll( |
| 843 | collectNetworkStatsSnapshotForAtom(FrameworkStatsLog.WIFI_BYTES_TRANSFER_BY_FG_BG)); |
| 844 | mNetworkStatsBaselines.addAll( |
| 845 | collectNetworkStatsSnapshotForAtom(FrameworkStatsLog.MOBILE_BYTES_TRANSFER)); |
| 846 | mNetworkStatsBaselines.addAll(collectNetworkStatsSnapshotForAtom( |
| 847 | FrameworkStatsLog.MOBILE_BYTES_TRANSFER_BY_FG_BG)); |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 848 | mNetworkStatsBaselines.addAll(collectNetworkStatsSnapshotForAtom( |
| 849 | FrameworkStatsLog.BYTES_TRANSFER_BY_TAG_AND_METERED)); |
junyulai | ee3ea79 | 2020-05-15 18:26:25 +0800 | [diff] [blame] | 850 | mNetworkStatsBaselines.addAll( |
| 851 | collectNetworkStatsSnapshotForAtom(FrameworkStatsLog.DATA_USAGE_BYTES_TRANSFER)); |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 852 | |
junyulai | aa31d3c | 2020-04-10 15:11:54 +0800 | [diff] [blame] | 853 | // Listen to subscription changes to record historical subscriptions that activated before |
junyulai | 66e735e | 2020-05-29 11:24:58 +0800 | [diff] [blame] | 854 | // pulling, this is used by {@code DATA_USAGE_BYTES_TRANSFER}. |
junyulai | aa31d3c | 2020-04-10 15:11:54 +0800 | [diff] [blame] | 855 | mSubscriptionManager.addOnSubscriptionsChangedListener( |
| 856 | BackgroundThread.getExecutor(), mStatsSubscriptionsListener); |
| 857 | |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 858 | registerWifiBytesTransfer(); |
| 859 | registerWifiBytesTransferBackground(); |
| 860 | registerMobileBytesTransfer(); |
| 861 | registerMobileBytesTransferBackground(); |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 862 | registerBytesTransferByTagAndMetered(); |
junyulai | ee3ea79 | 2020-05-15 18:26:25 +0800 | [diff] [blame] | 863 | registerDataUsageBytesTransfer(); |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 864 | } |
| 865 | |
junyulai | 212a5f0e | 2020-02-07 17:42:39 +0800 | [diff] [blame] | 866 | /** |
| 867 | * Return the {@code INetworkStatsSession} object that holds the necessary properties needed |
| 868 | * for the subsequent queries to {@link com.android.server.net.NetworkStatsService}. Or |
junyulai | ea76fb8 | 2020-05-29 14:32:21 +0800 | [diff] [blame] | 869 | * null if the service or binder cannot be obtained. Calling this method will trigger poll |
| 870 | * in NetworkStatsService with once per 15 seconds rate-limit, unless {@code bypassRateLimit} |
| 871 | * is set to true. This is needed in {@link #getUidNetworkStatsSnapshotForTemplate}, where |
| 872 | * bypassing the limit is necessary for perfd to supply realtime stats to developers looking at |
| 873 | * the network usage of their app. |
junyulai | 212a5f0e | 2020-02-07 17:42:39 +0800 | [diff] [blame] | 874 | */ |
| 875 | @Nullable |
junyulai | ea76fb8 | 2020-05-29 14:32:21 +0800 | [diff] [blame] | 876 | private INetworkStatsSession getNetworkStatsSession(boolean bypassRateLimit) { |
junyulai | 1794157 | 2020-05-27 14:44:13 +0800 | [diff] [blame] | 877 | final INetworkStatsService networkStatsService = |
| 878 | INetworkStatsService.Stub.asInterface( |
| 879 | ServiceManager.getService(Context.NETWORK_STATS_SERVICE)); |
| 880 | if (networkStatsService == null) return null; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 881 | |
junyulai | 1794157 | 2020-05-27 14:44:13 +0800 | [diff] [blame] | 882 | try { |
| 883 | return networkStatsService.openSessionForUsageStats( |
junyulai | ea76fb8 | 2020-05-29 14:32:21 +0800 | [diff] [blame] | 884 | FLAG_AUGMENT_WITH_SUBSCRIPTION_PLAN | (bypassRateLimit ? FLAG_POLL_FORCE |
junyulai | 1794157 | 2020-05-27 14:44:13 +0800 | [diff] [blame] | 885 | : FLAG_POLL_ON_OPEN), mContext.getOpPackageName()); |
| 886 | } catch (RemoteException e) { |
| 887 | Slog.e(TAG, "Cannot get NetworkStats session", e); |
| 888 | return null; |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 889 | } |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 890 | } |
| 891 | |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 892 | private IThermalService getIThermalService() { |
| 893 | synchronized (mThermalLock) { |
| 894 | if (mThermalService == null) { |
| 895 | mThermalService = IThermalService.Stub.asInterface( |
| 896 | ServiceManager.getService(Context.THERMAL_SERVICE)); |
| 897 | if (mThermalService != null) { |
| 898 | try { |
| 899 | mThermalService.asBinder().linkToDeath(() -> { |
| 900 | synchronized (mThermalLock) { |
| 901 | mThermalService = null; |
| 902 | } |
| 903 | }, /* flags */ 0); |
| 904 | } catch (RemoteException e) { |
| 905 | Slog.e(TAG, "linkToDeath with thermalService failed", e); |
| 906 | mThermalService = null; |
| 907 | } |
| 908 | } |
| 909 | } |
| 910 | return mThermalService; |
| 911 | } |
| 912 | } |
Jeffrey Huang | 95765f0 | 2020-01-16 11:33:58 -0800 | [diff] [blame] | 913 | |
| 914 | private IStoraged getIStoragedService() { |
| 915 | synchronized (mStoragedLock) { |
| 916 | if (mStorageService == null) { |
| 917 | mStorageService = IStoraged.Stub.asInterface( |
| 918 | ServiceManager.getService("storaged")); |
| 919 | } |
| 920 | if (mStorageService != null) { |
| 921 | try { |
| 922 | mStorageService.asBinder().linkToDeath(() -> { |
| 923 | synchronized (mStoragedLock) { |
| 924 | mStorageService = null; |
| 925 | } |
| 926 | }, /* flags */ 0); |
| 927 | } catch (RemoteException e) { |
| 928 | Slog.e(TAG, "linkToDeath with storagedService failed", e); |
| 929 | mStorageService = null; |
| 930 | } |
| 931 | } |
| 932 | } |
| 933 | return mStorageService; |
| 934 | } |
| 935 | |
Jeffrey Huang | 730bf96 | 2020-01-16 15:59:52 -0800 | [diff] [blame] | 936 | private INotificationManager getINotificationManagerService() { |
| 937 | synchronized (mNotificationStatsLock) { |
| 938 | if (mNotificationManagerService == null) { |
| 939 | mNotificationManagerService = INotificationManager.Stub.asInterface( |
Ruchir Rastogi | 92cf9a2 | 2020-01-17 15:55:39 -0800 | [diff] [blame] | 940 | ServiceManager.getService(Context.NOTIFICATION_SERVICE)); |
Jeffrey Huang | 730bf96 | 2020-01-16 15:59:52 -0800 | [diff] [blame] | 941 | } |
| 942 | if (mNotificationManagerService != null) { |
| 943 | try { |
| 944 | mNotificationManagerService.asBinder().linkToDeath(() -> { |
| 945 | synchronized (mNotificationStatsLock) { |
| 946 | mNotificationManagerService = null; |
| 947 | } |
| 948 | }, /* flags */ 0); |
| 949 | } catch (RemoteException e) { |
| 950 | Slog.e(TAG, "linkToDeath with notificationManager failed", e); |
| 951 | mNotificationManagerService = null; |
| 952 | } |
| 953 | } |
| 954 | } |
| 955 | return mNotificationManagerService; |
| 956 | } |
| 957 | |
Ruchir Rastogi | 92cf9a2 | 2020-01-17 15:55:39 -0800 | [diff] [blame] | 958 | private IProcessStats getIProcessStatsService() { |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 959 | synchronized (mProcStatsLock) { |
Ruchir Rastogi | 92cf9a2 | 2020-01-17 15:55:39 -0800 | [diff] [blame] | 960 | if (mProcessStatsService == null) { |
| 961 | mProcessStatsService = IProcessStats.Stub.asInterface( |
| 962 | ServiceManager.getService(ProcessStats.SERVICE_NAME)); |
| 963 | } |
| 964 | if (mProcessStatsService != null) { |
| 965 | try { |
| 966 | mProcessStatsService.asBinder().linkToDeath(() -> { |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 967 | synchronized (mProcStatsLock) { |
Ruchir Rastogi | 92cf9a2 | 2020-01-17 15:55:39 -0800 | [diff] [blame] | 968 | mProcessStatsService = null; |
| 969 | } |
| 970 | }, /* flags */ 0); |
| 971 | } catch (RemoteException e) { |
| 972 | Slog.e(TAG, "linkToDeath with ProcessStats failed", e); |
| 973 | mProcessStatsService = null; |
| 974 | } |
| 975 | } |
| 976 | } |
| 977 | return mProcessStatsService; |
| 978 | } |
| 979 | |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 980 | private void registerWifiBytesTransfer() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 981 | int tagId = FrameworkStatsLog.WIFI_BYTES_TRANSFER; |
Tej Singh | 6b97983 | 2020-01-02 15:23:02 -0800 | [diff] [blame] | 982 | PullAtomMetadata metadata = new PullAtomMetadata.Builder() |
Jeffrey Huang | acaee0f | 2020-01-10 14:45:19 -0800 | [diff] [blame] | 983 | .setAdditiveFields(new int[] {2, 3, 4, 5}) |
| 984 | .build(); |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 985 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 986 | tagId, |
Jeffrey Huang | acaee0f | 2020-01-10 14:45:19 -0800 | [diff] [blame] | 987 | metadata, |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 988 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 989 | mStatsCallbackImpl |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 990 | ); |
| 991 | } |
| 992 | |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 993 | @NonNull |
junyulai | 9f0146a | 2020-05-14 15:07:21 +0800 | [diff] [blame] | 994 | private List<NetworkStatsExt> collectNetworkStatsSnapshotForAtom(int atomTag) { |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 995 | List<NetworkStatsExt> ret = new ArrayList<>(); |
junyulai | 9f0146a | 2020-05-14 15:07:21 +0800 | [diff] [blame] | 996 | switch(atomTag) { |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 997 | case FrameworkStatsLog.WIFI_BYTES_TRANSFER: { |
junyulai | ee3ea79 | 2020-05-15 18:26:25 +0800 | [diff] [blame] | 998 | final NetworkStats stats = getUidNetworkStatsSnapshotForTransport(TRANSPORT_WIFI); |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 999 | if (stats != null) { |
| 1000 | ret.add(new NetworkStatsExt(stats.groupedByUid(), new int[] {TRANSPORT_WIFI}, |
| 1001 | /*slicedByFgbg=*/false)); |
| 1002 | } |
| 1003 | break; |
| 1004 | } |
| 1005 | case FrameworkStatsLog.WIFI_BYTES_TRANSFER_BY_FG_BG: { |
junyulai | ee3ea79 | 2020-05-15 18:26:25 +0800 | [diff] [blame] | 1006 | final NetworkStats stats = getUidNetworkStatsSnapshotForTransport(TRANSPORT_WIFI); |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 1007 | if (stats != null) { |
| 1008 | ret.add(new NetworkStatsExt(sliceNetworkStatsByUidAndFgbg(stats), |
| 1009 | new int[] {TRANSPORT_WIFI}, /*slicedByFgbg=*/true)); |
| 1010 | } |
| 1011 | break; |
| 1012 | } |
| 1013 | case FrameworkStatsLog.MOBILE_BYTES_TRANSFER: { |
junyulai | ee3ea79 | 2020-05-15 18:26:25 +0800 | [diff] [blame] | 1014 | final NetworkStats stats = |
| 1015 | getUidNetworkStatsSnapshotForTransport(TRANSPORT_CELLULAR); |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 1016 | if (stats != null) { |
| 1017 | ret.add(new NetworkStatsExt(stats.groupedByUid(), |
| 1018 | new int[] {TRANSPORT_CELLULAR}, /*slicedByFgbg=*/false)); |
| 1019 | } |
| 1020 | break; |
| 1021 | } |
| 1022 | case FrameworkStatsLog.MOBILE_BYTES_TRANSFER_BY_FG_BG: { |
junyulai | ee3ea79 | 2020-05-15 18:26:25 +0800 | [diff] [blame] | 1023 | final NetworkStats stats = |
| 1024 | getUidNetworkStatsSnapshotForTransport(TRANSPORT_CELLULAR); |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 1025 | if (stats != null) { |
| 1026 | ret.add(new NetworkStatsExt(sliceNetworkStatsByUidAndFgbg(stats), |
| 1027 | new int[] {TRANSPORT_CELLULAR}, /*slicedByFgbg=*/true)); |
| 1028 | } |
| 1029 | break; |
| 1030 | } |
| 1031 | case FrameworkStatsLog.BYTES_TRANSFER_BY_TAG_AND_METERED: { |
junyulai | ee3ea79 | 2020-05-15 18:26:25 +0800 | [diff] [blame] | 1032 | final NetworkStats wifiStats = getUidNetworkStatsSnapshotForTemplate( |
| 1033 | buildTemplateWifiWildcard(), /*includeTags=*/true); |
| 1034 | final NetworkStats cellularStats = getUidNetworkStatsSnapshotForTemplate( |
| 1035 | buildTemplateMobileWildcard(), /*includeTags=*/true); |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 1036 | if (wifiStats != null && cellularStats != null) { |
| 1037 | final NetworkStats stats = wifiStats.add(cellularStats); |
| 1038 | ret.add(new NetworkStatsExt(sliceNetworkStatsByUidTagAndMetered(stats), |
junyulai | ee3ea79 | 2020-05-15 18:26:25 +0800 | [diff] [blame] | 1039 | new int[] {TRANSPORT_WIFI, TRANSPORT_CELLULAR}, |
| 1040 | /*slicedByFgbg=*/false, /*slicedByTag=*/true, |
junyulai | aa31d3c | 2020-04-10 15:11:54 +0800 | [diff] [blame] | 1041 | /*slicedByMetered=*/true, TelephonyManager.NETWORK_TYPE_UNKNOWN, |
| 1042 | /*subInfo=*/null)); |
junyulai | ee3ea79 | 2020-05-15 18:26:25 +0800 | [diff] [blame] | 1043 | } |
| 1044 | break; |
| 1045 | } |
| 1046 | case FrameworkStatsLog.DATA_USAGE_BYTES_TRANSFER: { |
junyulai | aa31d3c | 2020-04-10 15:11:54 +0800 | [diff] [blame] | 1047 | for (final SubInfo subInfo : mHistoricalSubs) { |
| 1048 | ret.addAll(getDataUsageBytesTransferSnapshotForSub(subInfo)); |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 1049 | } |
| 1050 | break; |
| 1051 | } |
junyulai | 9f0146a | 2020-05-14 15:07:21 +0800 | [diff] [blame] | 1052 | default: |
| 1053 | throw new IllegalArgumentException("Unknown atomTag " + atomTag); |
| 1054 | } |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 1055 | return ret; |
| 1056 | } |
| 1057 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 1058 | private int pullDataBytesTransferLocked(int atomTag, @NonNull List<StatsEvent> pulledData) { |
junyulai | 9f0146a | 2020-05-14 15:07:21 +0800 | [diff] [blame] | 1059 | final List<NetworkStatsExt> current = collectNetworkStatsSnapshotForAtom(atomTag); |
junyulai | 592c605 | 2020-04-29 16:09:01 +0800 | [diff] [blame] | 1060 | |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 1061 | if (current == null) { |
| 1062 | Slog.e(TAG, "current snapshot is null for " + atomTag + ", return."); |
| 1063 | return StatsManager.PULL_SKIP; |
| 1064 | } |
| 1065 | |
| 1066 | for (final NetworkStatsExt item : current) { |
| 1067 | final NetworkStatsExt baseline = CollectionUtils.find(mNetworkStatsBaselines, |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 1068 | it -> it.hasSameSlicing(item)); |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 1069 | |
| 1070 | // No matched baseline indicates error has occurred during initialization stage, |
| 1071 | // skip reporting anything since the snapshot is invalid. |
| 1072 | if (baseline == null) { |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 1073 | Slog.e(TAG, "baseline is null for " + atomTag + ", return."); |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 1074 | return StatsManager.PULL_SKIP; |
| 1075 | } |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 1076 | final NetworkStatsExt diff = new NetworkStatsExt( |
| 1077 | item.stats.subtract(baseline.stats).removeEmptyEntries(), item.transports, |
junyulai | aa31d3c | 2020-04-10 15:11:54 +0800 | [diff] [blame] | 1078 | item.slicedByFgbg, item.slicedByTag, item.slicedByMetered, item.ratType, |
| 1079 | item.subInfo); |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 1080 | |
| 1081 | // If no diff, skip. |
| 1082 | if (diff.stats.size() == 0) continue; |
| 1083 | |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 1084 | switch (atomTag) { |
| 1085 | case FrameworkStatsLog.BYTES_TRANSFER_BY_TAG_AND_METERED: |
| 1086 | addBytesTransferByTagAndMeteredAtoms(diff, pulledData); |
| 1087 | break; |
junyulai | ee3ea79 | 2020-05-15 18:26:25 +0800 | [diff] [blame] | 1088 | case FrameworkStatsLog.DATA_USAGE_BYTES_TRANSFER: |
| 1089 | addDataUsageBytesTransferAtoms(diff, pulledData); |
| 1090 | break; |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 1091 | default: |
| 1092 | addNetworkStats(atomTag, pulledData, diff); |
| 1093 | } |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 1094 | } |
junyulai | 592c605 | 2020-04-29 16:09:01 +0800 | [diff] [blame] | 1095 | return StatsManager.PULL_SUCCESS; |
junyulai | 212a5f0e | 2020-02-07 17:42:39 +0800 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | private void addNetworkStats(int atomTag, @NonNull List<StatsEvent> ret, |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 1099 | @NonNull NetworkStatsExt statsExt) { |
| 1100 | int size = statsExt.stats.size(); |
junyulai | 212a5f0e | 2020-02-07 17:42:39 +0800 | [diff] [blame] | 1101 | final NetworkStats.Entry entry = new NetworkStats.Entry(); // For recycling |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 1102 | for (int j = 0; j < size; j++) { |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 1103 | statsExt.stats.getValues(j, entry); |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 1104 | StatsEvent.Builder e = StatsEvent.newBuilder(); |
junyulai | 212a5f0e | 2020-02-07 17:42:39 +0800 | [diff] [blame] | 1105 | e.setAtomId(atomTag); |
Muhammad Qureshi | 09fc8c4 | 2020-05-06 14:50:54 -0700 | [diff] [blame] | 1106 | switch (atomTag) { |
| 1107 | case FrameworkStatsLog.MOBILE_BYTES_TRANSFER: |
| 1108 | case FrameworkStatsLog.MOBILE_BYTES_TRANSFER_BY_FG_BG: |
| 1109 | e.addBooleanAnnotation(ANNOTATION_ID_TRUNCATE_TIMESTAMP, true); |
| 1110 | break; |
| 1111 | default: |
| 1112 | } |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 1113 | e.writeInt(entry.uid); |
Muhammad Qureshi | 22e52da | 2020-03-30 21:14:21 -0700 | [diff] [blame] | 1114 | e.addBooleanAnnotation(ANNOTATION_ID_IS_UID, true); |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 1115 | if (statsExt.slicedByFgbg) { |
Jeffrey Huang | 3ee8e20 | 2020-01-09 15:31:16 -0800 | [diff] [blame] | 1116 | e.writeInt(entry.set); |
| 1117 | } |
| 1118 | e.writeLong(entry.rxBytes); |
| 1119 | e.writeLong(entry.rxPackets); |
| 1120 | e.writeLong(entry.txBytes); |
| 1121 | e.writeLong(entry.txPackets); |
| 1122 | ret.add(e.build()); |
| 1123 | } |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1124 | } |
| 1125 | |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 1126 | private void addBytesTransferByTagAndMeteredAtoms(@NonNull NetworkStatsExt statsExt, |
| 1127 | @NonNull List<StatsEvent> pulledData) { |
| 1128 | final NetworkStats.Entry entry = new NetworkStats.Entry(); // for recycling |
| 1129 | for (int i = 0; i < statsExt.stats.size(); i++) { |
| 1130 | statsExt.stats.getValues(i, entry); |
| 1131 | StatsEvent e = StatsEvent.newBuilder() |
| 1132 | .setAtomId(FrameworkStatsLog.BYTES_TRANSFER_BY_TAG_AND_METERED) |
| 1133 | .addBooleanAnnotation(ANNOTATION_ID_TRUNCATE_TIMESTAMP, true) |
| 1134 | .writeInt(entry.uid) |
| 1135 | .addBooleanAnnotation(ANNOTATION_ID_IS_UID, true) |
| 1136 | .writeBoolean(entry.metered == NetworkStats.METERED_YES) |
| 1137 | .writeInt(entry.tag) |
| 1138 | .writeLong(entry.rxBytes) |
| 1139 | .writeLong(entry.rxPackets) |
| 1140 | .writeLong(entry.txBytes) |
| 1141 | .writeLong(entry.txPackets) |
| 1142 | .build(); |
| 1143 | pulledData.add(e); |
| 1144 | } |
| 1145 | } |
| 1146 | |
junyulai | ee3ea79 | 2020-05-15 18:26:25 +0800 | [diff] [blame] | 1147 | private void addDataUsageBytesTransferAtoms(@NonNull NetworkStatsExt statsExt, |
| 1148 | @NonNull List<StatsEvent> pulledData) { |
| 1149 | final NetworkStats.Entry entry = new NetworkStats.Entry(); // for recycling |
| 1150 | for (int i = 0; i < statsExt.stats.size(); i++) { |
| 1151 | statsExt.stats.getValues(i, entry); |
| 1152 | StatsEvent e = StatsEvent.newBuilder() |
| 1153 | .setAtomId(FrameworkStatsLog.DATA_USAGE_BYTES_TRANSFER) |
| 1154 | .addBooleanAnnotation(ANNOTATION_ID_TRUNCATE_TIMESTAMP, true) |
| 1155 | .writeInt(entry.set) |
| 1156 | .writeLong(entry.rxBytes) |
| 1157 | .writeLong(entry.rxPackets) |
| 1158 | .writeLong(entry.txBytes) |
| 1159 | .writeLong(entry.txPackets) |
| 1160 | .writeInt(statsExt.ratType) |
junyulai | aa31d3c | 2020-04-10 15:11:54 +0800 | [diff] [blame] | 1161 | // Fill information about subscription, these cannot be null since invalid data |
| 1162 | // would be filtered when adding into subInfo list. |
| 1163 | .writeString(statsExt.subInfo.mcc) |
| 1164 | .writeString(statsExt.subInfo.mnc) |
| 1165 | .writeInt(statsExt.subInfo.carrierId) |
| 1166 | .writeInt(statsExt.subInfo.isOpportunistic |
| 1167 | ? DATA_USAGE_BYTES_TRANSFER__OPPORTUNISTIC_DATA_SUB__OPPORTUNISTIC |
| 1168 | : DATA_USAGE_BYTES_TRANSFER__OPPORTUNISTIC_DATA_SUB__NOT_OPPORTUNISTIC) |
junyulai | ee3ea79 | 2020-05-15 18:26:25 +0800 | [diff] [blame] | 1169 | .build(); |
| 1170 | pulledData.add(e); |
| 1171 | } |
| 1172 | } |
| 1173 | |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 1174 | /** |
junyulai | ee3ea79 | 2020-05-15 18:26:25 +0800 | [diff] [blame] | 1175 | * Create a snapshot of NetworkStats for a given transport. |
| 1176 | */ |
| 1177 | @Nullable private NetworkStats getUidNetworkStatsSnapshotForTransport(int transport) { |
| 1178 | final NetworkTemplate template = (transport == TRANSPORT_CELLULAR) |
| 1179 | ? NetworkTemplate.buildTemplateMobileWithRatType( |
| 1180 | /*subscriptionId=*/null, NETWORK_TYPE_ALL) |
| 1181 | : NetworkTemplate.buildTemplateWifiWildcard(); |
| 1182 | return getUidNetworkStatsSnapshotForTemplate(template, /*includeTags=*/false); |
| 1183 | } |
| 1184 | |
| 1185 | /** |
| 1186 | * Create a snapshot of NetworkStats since boot for the given template, but add 1 bucket |
| 1187 | * duration before boot as a buffer to ensure at least one full bucket will be included. |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 1188 | * Note that this should be only used to calculate diff since the snapshot might contains |
| 1189 | * some traffic before boot. |
| 1190 | */ |
junyulai | ee3ea79 | 2020-05-15 18:26:25 +0800 | [diff] [blame] | 1191 | @Nullable private NetworkStats getUidNetworkStatsSnapshotForTemplate( |
| 1192 | @NonNull NetworkTemplate template, boolean includeTags) { |
junyulai | 212a5f0e | 2020-02-07 17:42:39 +0800 | [diff] [blame] | 1193 | final long elapsedMillisSinceBoot = SystemClock.elapsedRealtime(); |
| 1194 | final long currentTimeInMillis = MICROSECONDS.toMillis(SystemClock.currentTimeMicro()); |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 1195 | final long bucketDuration = Settings.Global.getLong(mContext.getContentResolver(), |
| 1196 | NETSTATS_UID_BUCKET_DURATION, NETSTATS_UID_DEFAULT_BUCKET_DURATION_MS); |
junyulai | 212a5f0e | 2020-02-07 17:42:39 +0800 | [diff] [blame] | 1197 | try { |
junyulai | 1794157 | 2020-05-27 14:44:13 +0800 | [diff] [blame] | 1198 | // TODO (b/156313635): This is short-term hack to allow perfd gets updated networkStats |
| 1199 | // history when query in every second in order to show realtime statistics. However, |
| 1200 | // this is not a good long-term solution since NetworkStatsService will make frequent |
| 1201 | // I/O and also block main thread when polling. |
| 1202 | // Consider making perfd queries NetworkStatsService directly. |
| 1203 | final NetworkStats stats = getNetworkStatsSession(template.getMatchRule() |
| 1204 | == NetworkTemplate.MATCH_WIFI_WILDCARD).getSummaryForAllUid(template, |
junyulai | 39f15c8 | 2020-04-17 10:48:36 +0800 | [diff] [blame] | 1205 | currentTimeInMillis - elapsedMillisSinceBoot - bucketDuration, |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 1206 | currentTimeInMillis, includeTags); |
| 1207 | return stats; |
junyulai | 212a5f0e | 2020-02-07 17:42:39 +0800 | [diff] [blame] | 1208 | } catch (RemoteException | NullPointerException e) { |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 1209 | Slog.e(TAG, "Pulling netstats for template=" + template + " and includeTags=" |
| 1210 | + includeTags + " causes error", e); |
junyulai | 212a5f0e | 2020-02-07 17:42:39 +0800 | [diff] [blame] | 1211 | } |
| 1212 | return null; |
| 1213 | } |
| 1214 | |
junyulai | aa31d3c | 2020-04-10 15:11:54 +0800 | [diff] [blame] | 1215 | @NonNull private List<NetworkStatsExt> getDataUsageBytesTransferSnapshotForSub( |
| 1216 | @NonNull SubInfo subInfo) { |
| 1217 | final List<NetworkStatsExt> ret = new ArrayList<>(); |
| 1218 | for (final int ratType : getAllCollapsedRatTypes()) { |
| 1219 | final NetworkTemplate template = |
| 1220 | buildTemplateMobileWithRatType(subInfo.subscriberId, ratType); |
| 1221 | final NetworkStats stats = |
| 1222 | getUidNetworkStatsSnapshotForTemplate(template, /*includeTags=*/false); |
| 1223 | if (stats != null) { |
| 1224 | ret.add(new NetworkStatsExt(sliceNetworkStatsByFgbg(stats), |
| 1225 | new int[] {TRANSPORT_CELLULAR}, /*slicedByFgbg=*/true, |
| 1226 | /*slicedByTag=*/false, /*slicedByMetered=*/false, ratType, subInfo)); |
| 1227 | } |
| 1228 | } |
| 1229 | return ret; |
| 1230 | } |
| 1231 | |
junyulai | ee3ea79 | 2020-05-15 18:26:25 +0800 | [diff] [blame] | 1232 | @NonNull private NetworkStats sliceNetworkStatsByFgbg(@NonNull NetworkStats stats) { |
| 1233 | return sliceNetworkStats(stats, |
| 1234 | (newEntry, oldEntry) -> { |
| 1235 | newEntry.set = oldEntry.set; |
| 1236 | }); |
| 1237 | } |
| 1238 | |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 1239 | @NonNull private NetworkStats sliceNetworkStatsByUidAndFgbg(@NonNull NetworkStats stats) { |
| 1240 | return sliceNetworkStats(stats, |
| 1241 | (newEntry, oldEntry) -> { |
| 1242 | newEntry.uid = oldEntry.uid; |
| 1243 | newEntry.set = oldEntry.set; |
| 1244 | }); |
| 1245 | } |
| 1246 | |
| 1247 | @NonNull private NetworkStats sliceNetworkStatsByUidTagAndMetered(@NonNull NetworkStats stats) { |
| 1248 | return sliceNetworkStats(stats, |
| 1249 | (newEntry, oldEntry) -> { |
| 1250 | newEntry.uid = oldEntry.uid; |
| 1251 | newEntry.tag = oldEntry.tag; |
| 1252 | newEntry.metered = oldEntry.metered; |
| 1253 | }); |
| 1254 | } |
| 1255 | |
Jeffrey Huang | acaee0f | 2020-01-10 14:45:19 -0800 | [diff] [blame] | 1256 | /** |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 1257 | * Slices NetworkStats along the dimensions specified in the slicer lambda and aggregates over |
| 1258 | * non-sliced dimensions. |
| 1259 | * |
| 1260 | * This function iterates through each NetworkStats.Entry, sets its dimensions equal to the |
| 1261 | * default state (with the presumption that we don't want to slice on anything), and then |
| 1262 | * applies the slicer lambda to allow users to control which dimensions to slice on. This is |
| 1263 | * adapted from groupedByUid within NetworkStats.java |
| 1264 | * |
| 1265 | * @param slicer An operation taking into two parameters, new NetworkStats.Entry and old |
| 1266 | * NetworkStats.Entry, that should be used to copy state from the old to the new. |
| 1267 | * This is useful for slicing by particular dimensions. For example, if we wished |
| 1268 | * to slice by uid and tag, we could write the following lambda: |
| 1269 | * (new, old) -> { |
| 1270 | * new.uid = old.uid; |
| 1271 | * new.tag = old.tag; |
| 1272 | * } |
| 1273 | * If no slicer is provided, the data is not sliced by any dimensions. |
| 1274 | * @return new NeworkStats object appropriately sliced |
Jeffrey Huang | acaee0f | 2020-01-10 14:45:19 -0800 | [diff] [blame] | 1275 | */ |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 1276 | @NonNull private NetworkStats sliceNetworkStats(@NonNull NetworkStats stats, |
| 1277 | @Nullable BiConsumer<NetworkStats.Entry, NetworkStats.Entry> slicer) { |
Jeffrey Huang | acaee0f | 2020-01-10 14:45:19 -0800 | [diff] [blame] | 1278 | final NetworkStats ret = new NetworkStats(stats.getElapsedRealtime(), 1); |
| 1279 | |
| 1280 | final NetworkStats.Entry entry = new NetworkStats.Entry(); |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 1281 | entry.uid = NetworkStats.UID_ALL; |
Jeffrey Huang | acaee0f | 2020-01-10 14:45:19 -0800 | [diff] [blame] | 1282 | entry.iface = NetworkStats.IFACE_ALL; |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 1283 | entry.set = NetworkStats.SET_ALL; |
Jeffrey Huang | acaee0f | 2020-01-10 14:45:19 -0800 | [diff] [blame] | 1284 | entry.tag = NetworkStats.TAG_NONE; |
| 1285 | entry.metered = NetworkStats.METERED_ALL; |
| 1286 | entry.roaming = NetworkStats.ROAMING_ALL; |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 1287 | entry.defaultNetwork = NetworkStats.DEFAULT_NETWORK_ALL; |
Jeffrey Huang | acaee0f | 2020-01-10 14:45:19 -0800 | [diff] [blame] | 1288 | |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 1289 | final NetworkStats.Entry recycle = new NetworkStats.Entry(); // used for retrieving values |
| 1290 | for (int i = 0; i < stats.size(); i++) { |
Jeffrey Huang | acaee0f | 2020-01-10 14:45:19 -0800 | [diff] [blame] | 1291 | stats.getValues(i, recycle); |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 1292 | if (slicer != null) { |
| 1293 | slicer.accept(entry, recycle); |
| 1294 | } |
Jeffrey Huang | acaee0f | 2020-01-10 14:45:19 -0800 | [diff] [blame] | 1295 | |
Jeffrey Huang | acaee0f | 2020-01-10 14:45:19 -0800 | [diff] [blame] | 1296 | entry.rxBytes = recycle.rxBytes; |
| 1297 | entry.rxPackets = recycle.rxPackets; |
| 1298 | entry.txBytes = recycle.txBytes; |
| 1299 | entry.txPackets = recycle.txPackets; |
| 1300 | // Operations purposefully omitted since we don't use them for statsd. |
| 1301 | ret.combineValues(entry); |
| 1302 | } |
| 1303 | return ret; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1304 | } |
| 1305 | |
Jeffrey Huang | acaee0f | 2020-01-10 14:45:19 -0800 | [diff] [blame] | 1306 | private void registerWifiBytesTransferBackground() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 1307 | int tagId = FrameworkStatsLog.WIFI_BYTES_TRANSFER_BY_FG_BG; |
Tej Singh | 6b97983 | 2020-01-02 15:23:02 -0800 | [diff] [blame] | 1308 | PullAtomMetadata metadata = new PullAtomMetadata.Builder() |
Jeffrey Huang | acaee0f | 2020-01-10 14:45:19 -0800 | [diff] [blame] | 1309 | .setAdditiveFields(new int[] {3, 4, 5, 6}) |
| 1310 | .build(); |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 1311 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | acaee0f | 2020-01-10 14:45:19 -0800 | [diff] [blame] | 1312 | tagId, |
| 1313 | metadata, |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 1314 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 1315 | mStatsCallbackImpl |
Jeffrey Huang | acaee0f | 2020-01-10 14:45:19 -0800 | [diff] [blame] | 1316 | ); |
| 1317 | } |
| 1318 | |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1319 | private void registerMobileBytesTransfer() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 1320 | int tagId = FrameworkStatsLog.MOBILE_BYTES_TRANSFER; |
Tej Singh | 6b97983 | 2020-01-02 15:23:02 -0800 | [diff] [blame] | 1321 | PullAtomMetadata metadata = new PullAtomMetadata.Builder() |
Jeffrey Huang | c878542 | 2020-01-10 15:54:53 -0800 | [diff] [blame] | 1322 | .setAdditiveFields(new int[] {2, 3, 4, 5}) |
| 1323 | .build(); |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 1324 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | c878542 | 2020-01-10 15:54:53 -0800 | [diff] [blame] | 1325 | tagId, |
| 1326 | metadata, |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 1327 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 1328 | mStatsCallbackImpl |
Jeffrey Huang | c878542 | 2020-01-10 15:54:53 -0800 | [diff] [blame] | 1329 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1330 | } |
| 1331 | |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1332 | private void registerMobileBytesTransferBackground() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 1333 | int tagId = FrameworkStatsLog.MOBILE_BYTES_TRANSFER_BY_FG_BG; |
Tej Singh | 6b97983 | 2020-01-02 15:23:02 -0800 | [diff] [blame] | 1334 | PullAtomMetadata metadata = new PullAtomMetadata.Builder() |
Jeffrey Huang | 8986444 | 2020-01-10 16:35:51 -0800 | [diff] [blame] | 1335 | .setAdditiveFields(new int[] {3, 4, 5, 6}) |
| 1336 | .build(); |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 1337 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | 8986444 | 2020-01-10 16:35:51 -0800 | [diff] [blame] | 1338 | tagId, |
| 1339 | metadata, |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 1340 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 1341 | mStatsCallbackImpl |
Jeffrey Huang | 8986444 | 2020-01-10 16:35:51 -0800 | [diff] [blame] | 1342 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1343 | } |
| 1344 | |
Ruchir Rastogi | e3cd20f | 2020-05-16 19:46:58 -0700 | [diff] [blame] | 1345 | private void registerBytesTransferByTagAndMetered() { |
| 1346 | int tagId = FrameworkStatsLog.BYTES_TRANSFER_BY_TAG_AND_METERED; |
| 1347 | PullAtomMetadata metadata = new PullAtomMetadata.Builder() |
| 1348 | .setAdditiveFields(new int[] {4, 5, 6, 7}) |
| 1349 | .build(); |
| 1350 | mStatsManager.setPullAtomCallback( |
| 1351 | tagId, |
| 1352 | metadata, |
| 1353 | BackgroundThread.getExecutor(), |
| 1354 | mStatsCallbackImpl |
| 1355 | ); |
| 1356 | } |
| 1357 | |
junyulai | ee3ea79 | 2020-05-15 18:26:25 +0800 | [diff] [blame] | 1358 | private void registerDataUsageBytesTransfer() { |
| 1359 | int tagId = FrameworkStatsLog.DATA_USAGE_BYTES_TRANSFER; |
| 1360 | PullAtomMetadata metadata = new PullAtomMetadata.Builder() |
| 1361 | .setAdditiveFields(new int[] {2, 3, 4, 5}) |
| 1362 | .build(); |
| 1363 | mStatsManager.setPullAtomCallback( |
| 1364 | tagId, |
| 1365 | metadata, |
| 1366 | BackgroundThread.getExecutor(), |
| 1367 | mStatsCallbackImpl |
| 1368 | ); |
| 1369 | } |
| 1370 | |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1371 | private void registerBluetoothBytesTransfer() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 1372 | int tagId = FrameworkStatsLog.BLUETOOTH_BYTES_TRANSFER; |
Tej Singh | 6b97983 | 2020-01-02 15:23:02 -0800 | [diff] [blame] | 1373 | PullAtomMetadata metadata = new PullAtomMetadata.Builder() |
Jeffrey Huang | fa91789 | 2020-01-10 16:58:03 -0800 | [diff] [blame] | 1374 | .setAdditiveFields(new int[] {2, 3}) |
| 1375 | .build(); |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 1376 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | fa91789 | 2020-01-10 16:58:03 -0800 | [diff] [blame] | 1377 | tagId, |
| 1378 | metadata, |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 1379 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 1380 | mStatsCallbackImpl |
Jeffrey Huang | fa91789 | 2020-01-10 16:58:03 -0800 | [diff] [blame] | 1381 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1382 | } |
| 1383 | |
Jeffrey Huang | fa91789 | 2020-01-10 16:58:03 -0800 | [diff] [blame] | 1384 | /** |
| 1385 | * Helper method to extract the Parcelable controller info from a |
| 1386 | * SynchronousResultReceiver. |
| 1387 | */ |
| 1388 | private static <T extends Parcelable> T awaitControllerInfo( |
| 1389 | @Nullable SynchronousResultReceiver receiver) { |
| 1390 | if (receiver == null) { |
| 1391 | return null; |
| 1392 | } |
| 1393 | |
| 1394 | try { |
| 1395 | final SynchronousResultReceiver.Result result = |
| 1396 | receiver.awaitResult(EXTERNAL_STATS_SYNC_TIMEOUT_MILLIS); |
| 1397 | if (result.bundle != null) { |
| 1398 | // This is the final destination for the Bundle. |
| 1399 | result.bundle.setDefusable(true); |
| 1400 | |
| 1401 | final T data = result.bundle.getParcelable(RESULT_RECEIVER_CONTROLLER_KEY); |
| 1402 | if (data != null) { |
| 1403 | return data; |
| 1404 | } |
| 1405 | } |
Jeffrey Huang | fa91789 | 2020-01-10 16:58:03 -0800 | [diff] [blame] | 1406 | } catch (TimeoutException e) { |
| 1407 | Slog.w(TAG, "timeout reading " + receiver.getName() + " stats"); |
| 1408 | } |
| 1409 | return null; |
| 1410 | } |
| 1411 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 1412 | private BluetoothActivityEnergyInfo fetchBluetoothData() { |
Jeffrey Huang | fa91789 | 2020-01-10 16:58:03 -0800 | [diff] [blame] | 1413 | final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 1414 | if (adapter != null) { |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 1415 | SynchronousResultReceiver bluetoothReceiver = |
| 1416 | new SynchronousResultReceiver("bluetooth"); |
Jeffrey Huang | fa91789 | 2020-01-10 16:58:03 -0800 | [diff] [blame] | 1417 | adapter.requestControllerActivityEnergyInfo(bluetoothReceiver); |
| 1418 | return awaitControllerInfo(bluetoothReceiver); |
| 1419 | } else { |
| 1420 | Slog.e(TAG, "Failed to get bluetooth adapter!"); |
| 1421 | return null; |
| 1422 | } |
| 1423 | } |
| 1424 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 1425 | int pullBluetoothBytesTransferLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | fa91789 | 2020-01-10 16:58:03 -0800 | [diff] [blame] | 1426 | BluetoothActivityEnergyInfo info = fetchBluetoothData(); |
| 1427 | if (info == null || info.getUidTraffic() == null) { |
| 1428 | return StatsManager.PULL_SKIP; |
| 1429 | } |
| 1430 | for (UidTraffic traffic : info.getUidTraffic()) { |
| 1431 | StatsEvent e = StatsEvent.newBuilder() |
| 1432 | .setAtomId(atomTag) |
| 1433 | .writeInt(traffic.getUid()) |
Muhammad Qureshi | 22e52da | 2020-03-30 21:14:21 -0700 | [diff] [blame] | 1434 | .addBooleanAnnotation(ANNOTATION_ID_IS_UID, true) |
Jeffrey Huang | fa91789 | 2020-01-10 16:58:03 -0800 | [diff] [blame] | 1435 | .writeLong(traffic.getRxBytes()) |
| 1436 | .writeLong(traffic.getTxBytes()) |
| 1437 | .build(); |
| 1438 | pulledData.add(e); |
| 1439 | } |
| 1440 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | private void registerKernelWakelock() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 1444 | int tagId = FrameworkStatsLog.KERNEL_WAKELOCK; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 1445 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | a6e5a9b | 2020-01-13 15:59:16 -0800 | [diff] [blame] | 1446 | tagId, |
| 1447 | /* PullAtomMetadata */ null, |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 1448 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 1449 | mStatsCallbackImpl |
Jeffrey Huang | a6e5a9b | 2020-01-13 15:59:16 -0800 | [diff] [blame] | 1450 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1451 | } |
| 1452 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 1453 | int pullKernelWakelockLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | a6e5a9b | 2020-01-13 15:59:16 -0800 | [diff] [blame] | 1454 | final KernelWakelockStats wakelockStats = |
| 1455 | mKernelWakelockReader.readKernelWakelockStats(mTmpWakelockStats); |
| 1456 | for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) { |
| 1457 | String name = ent.getKey(); |
| 1458 | KernelWakelockStats.Entry kws = ent.getValue(); |
| 1459 | StatsEvent e = StatsEvent.newBuilder() |
| 1460 | .setAtomId(atomTag) |
| 1461 | .writeString(name) |
| 1462 | .writeInt(kws.mCount) |
| 1463 | .writeInt(kws.mVersion) |
| 1464 | .writeLong(kws.mTotalTime) |
| 1465 | .build(); |
| 1466 | pulledData.add(e); |
| 1467 | } |
| 1468 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | private void registerCpuTimePerFreq() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 1472 | int tagId = FrameworkStatsLog.CPU_TIME_PER_FREQ; |
Tej Singh | 6b97983 | 2020-01-02 15:23:02 -0800 | [diff] [blame] | 1473 | PullAtomMetadata metadata = new PullAtomMetadata.Builder() |
Jeffrey Huang | 43d5e2d | 2020-01-13 16:22:36 -0800 | [diff] [blame] | 1474 | .setAdditiveFields(new int[] {3}) |
| 1475 | .build(); |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 1476 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | 43d5e2d | 2020-01-13 16:22:36 -0800 | [diff] [blame] | 1477 | tagId, |
| 1478 | metadata, |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 1479 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 1480 | mStatsCallbackImpl |
Jeffrey Huang | 43d5e2d | 2020-01-13 16:22:36 -0800 | [diff] [blame] | 1481 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1482 | } |
| 1483 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 1484 | int pullCpuTimePerFreqLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | 43d5e2d | 2020-01-13 16:22:36 -0800 | [diff] [blame] | 1485 | for (int cluster = 0; cluster < mKernelCpuSpeedReaders.length; cluster++) { |
| 1486 | long[] clusterTimeMs = mKernelCpuSpeedReaders[cluster].readAbsolute(); |
| 1487 | if (clusterTimeMs != null) { |
| 1488 | for (int speed = clusterTimeMs.length - 1; speed >= 0; --speed) { |
| 1489 | StatsEvent e = StatsEvent.newBuilder() |
| 1490 | .setAtomId(atomTag) |
| 1491 | .writeInt(cluster) |
| 1492 | .writeInt(speed) |
| 1493 | .writeLong(clusterTimeMs[speed]) |
| 1494 | .build(); |
| 1495 | pulledData.add(e); |
| 1496 | } |
| 1497 | } |
| 1498 | } |
| 1499 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | private void registerCpuTimePerUid() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 1503 | int tagId = FrameworkStatsLog.CPU_TIME_PER_UID; |
Tej Singh | 6b97983 | 2020-01-02 15:23:02 -0800 | [diff] [blame] | 1504 | PullAtomMetadata metadata = new PullAtomMetadata.Builder() |
Jeffrey Huang | d5f053e | 2020-01-13 16:27:53 -0800 | [diff] [blame] | 1505 | .setAdditiveFields(new int[] {2, 3}) |
| 1506 | .build(); |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 1507 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | d5f053e | 2020-01-13 16:27:53 -0800 | [diff] [blame] | 1508 | tagId, |
| 1509 | metadata, |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 1510 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 1511 | mStatsCallbackImpl |
Jeffrey Huang | d5f053e | 2020-01-13 16:27:53 -0800 | [diff] [blame] | 1512 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1513 | } |
| 1514 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 1515 | int pullCpuTimePerUidLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | d5f053e | 2020-01-13 16:27:53 -0800 | [diff] [blame] | 1516 | mCpuUidUserSysTimeReader.readAbsolute((uid, timesUs) -> { |
| 1517 | long userTimeUs = timesUs[0], systemTimeUs = timesUs[1]; |
| 1518 | StatsEvent e = StatsEvent.newBuilder() |
| 1519 | .setAtomId(atomTag) |
| 1520 | .writeInt(uid) |
Muhammad Qureshi | 22e52da | 2020-03-30 21:14:21 -0700 | [diff] [blame] | 1521 | .addBooleanAnnotation(ANNOTATION_ID_IS_UID, true) |
Jeffrey Huang | d5f053e | 2020-01-13 16:27:53 -0800 | [diff] [blame] | 1522 | .writeLong(userTimeUs) |
| 1523 | .writeLong(systemTimeUs) |
| 1524 | .build(); |
| 1525 | pulledData.add(e); |
| 1526 | }); |
| 1527 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | private void registerCpuTimePerUidFreq() { |
Jeffrey Huang | a989930 | 2020-01-13 16:34:03 -0800 | [diff] [blame] | 1531 | // the throttling is 3sec, handled in |
| 1532 | // frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 1533 | int tagId = FrameworkStatsLog.CPU_TIME_PER_UID_FREQ; |
Tej Singh | 6b97983 | 2020-01-02 15:23:02 -0800 | [diff] [blame] | 1534 | PullAtomMetadata metadata = new PullAtomMetadata.Builder() |
Tej Singh | 137aa83 | 2020-05-28 18:21:52 -0700 | [diff] [blame] | 1535 | .setAdditiveFields(new int[] {3}) |
Jeffrey Huang | a989930 | 2020-01-13 16:34:03 -0800 | [diff] [blame] | 1536 | .build(); |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 1537 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | a989930 | 2020-01-13 16:34:03 -0800 | [diff] [blame] | 1538 | tagId, |
| 1539 | metadata, |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 1540 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 1541 | mStatsCallbackImpl |
Jeffrey Huang | a989930 | 2020-01-13 16:34:03 -0800 | [diff] [blame] | 1542 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1543 | } |
| 1544 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 1545 | int pullCpuTimePerUidFreqLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | a989930 | 2020-01-13 16:34:03 -0800 | [diff] [blame] | 1546 | mCpuUidFreqTimeReader.readAbsolute((uid, cpuFreqTimeMs) -> { |
| 1547 | for (int freqIndex = 0; freqIndex < cpuFreqTimeMs.length; ++freqIndex) { |
| 1548 | if (cpuFreqTimeMs[freqIndex] != 0) { |
| 1549 | StatsEvent e = StatsEvent.newBuilder() |
| 1550 | .setAtomId(atomTag) |
| 1551 | .writeInt(uid) |
Muhammad Qureshi | 22e52da | 2020-03-30 21:14:21 -0700 | [diff] [blame] | 1552 | .addBooleanAnnotation(ANNOTATION_ID_IS_UID, true) |
Jeffrey Huang | a989930 | 2020-01-13 16:34:03 -0800 | [diff] [blame] | 1553 | .writeInt(freqIndex) |
| 1554 | .writeLong(cpuFreqTimeMs[freqIndex]) |
| 1555 | .build(); |
| 1556 | pulledData.add(e); |
| 1557 | } |
| 1558 | } |
| 1559 | }); |
| 1560 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | private void registerCpuActiveTime() { |
Jeffrey Huang | ccfcdf0 | 2020-01-13 16:38:27 -0800 | [diff] [blame] | 1564 | // the throttling is 3sec, handled in |
| 1565 | // frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 1566 | int tagId = FrameworkStatsLog.CPU_ACTIVE_TIME; |
Tej Singh | 6b97983 | 2020-01-02 15:23:02 -0800 | [diff] [blame] | 1567 | PullAtomMetadata metadata = new PullAtomMetadata.Builder() |
Jeffrey Huang | ccfcdf0 | 2020-01-13 16:38:27 -0800 | [diff] [blame] | 1568 | .setAdditiveFields(new int[] {2}) |
| 1569 | .build(); |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 1570 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | ccfcdf0 | 2020-01-13 16:38:27 -0800 | [diff] [blame] | 1571 | tagId, |
| 1572 | metadata, |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 1573 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 1574 | mStatsCallbackImpl |
Jeffrey Huang | ccfcdf0 | 2020-01-13 16:38:27 -0800 | [diff] [blame] | 1575 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1576 | } |
| 1577 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 1578 | int pullCpuActiveTimeLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | ccfcdf0 | 2020-01-13 16:38:27 -0800 | [diff] [blame] | 1579 | mCpuUidActiveTimeReader.readAbsolute((uid, cpuActiveTimesMs) -> { |
| 1580 | StatsEvent e = StatsEvent.newBuilder() |
| 1581 | .setAtomId(atomTag) |
| 1582 | .writeInt(uid) |
Muhammad Qureshi | 22e52da | 2020-03-30 21:14:21 -0700 | [diff] [blame] | 1583 | .addBooleanAnnotation(ANNOTATION_ID_IS_UID, true) |
Jeffrey Huang | ccfcdf0 | 2020-01-13 16:38:27 -0800 | [diff] [blame] | 1584 | .writeLong(cpuActiveTimesMs) |
| 1585 | .build(); |
| 1586 | pulledData.add(e); |
| 1587 | }); |
| 1588 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | private void registerCpuClusterTime() { |
Jeffrey Huang | ceb1d25 | 2020-01-13 16:47:44 -0800 | [diff] [blame] | 1592 | // the throttling is 3sec, handled in |
| 1593 | // frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 1594 | int tagId = FrameworkStatsLog.CPU_CLUSTER_TIME; |
Tej Singh | 6b97983 | 2020-01-02 15:23:02 -0800 | [diff] [blame] | 1595 | PullAtomMetadata metadata = new PullAtomMetadata.Builder() |
Jeffrey Huang | ceb1d25 | 2020-01-13 16:47:44 -0800 | [diff] [blame] | 1596 | .setAdditiveFields(new int[] {3}) |
| 1597 | .build(); |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 1598 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | ceb1d25 | 2020-01-13 16:47:44 -0800 | [diff] [blame] | 1599 | tagId, |
| 1600 | metadata, |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 1601 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 1602 | mStatsCallbackImpl |
Jeffrey Huang | ceb1d25 | 2020-01-13 16:47:44 -0800 | [diff] [blame] | 1603 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1604 | } |
| 1605 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 1606 | int pullCpuClusterTimeLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | ceb1d25 | 2020-01-13 16:47:44 -0800 | [diff] [blame] | 1607 | mCpuUidClusterTimeReader.readAbsolute((uid, cpuClusterTimesMs) -> { |
| 1608 | for (int i = 0; i < cpuClusterTimesMs.length; i++) { |
| 1609 | StatsEvent e = StatsEvent.newBuilder() |
| 1610 | .setAtomId(atomTag) |
| 1611 | .writeInt(uid) |
Muhammad Qureshi | 22e52da | 2020-03-30 21:14:21 -0700 | [diff] [blame] | 1612 | .addBooleanAnnotation(ANNOTATION_ID_IS_UID, true) |
Jeffrey Huang | ceb1d25 | 2020-01-13 16:47:44 -0800 | [diff] [blame] | 1613 | .writeInt(i) |
| 1614 | .writeLong(cpuClusterTimesMs[i]) |
| 1615 | .build(); |
| 1616 | pulledData.add(e); |
| 1617 | } |
| 1618 | }); |
| 1619 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | private void registerWifiActivityInfo() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 1623 | int tagId = FrameworkStatsLog.WIFI_ACTIVITY_INFO; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 1624 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | 4df5740 | 2020-01-14 11:57:22 -0800 | [diff] [blame] | 1625 | tagId, |
| 1626 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 1627 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 1628 | mStatsCallbackImpl |
Jeffrey Huang | 4df5740 | 2020-01-14 11:57:22 -0800 | [diff] [blame] | 1629 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1630 | } |
| 1631 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 1632 | int pullWifiActivityInfoLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | 4df5740 | 2020-01-14 11:57:22 -0800 | [diff] [blame] | 1633 | long token = Binder.clearCallingIdentity(); |
| 1634 | try { |
| 1635 | SynchronousResultReceiver wifiReceiver = new SynchronousResultReceiver("wifi"); |
| 1636 | mWifiManager.getWifiActivityEnergyInfoAsync( |
| 1637 | new Executor() { |
| 1638 | @Override |
| 1639 | public void execute(Runnable runnable) { |
| 1640 | // run the listener on the binder thread, if it was run on the main |
| 1641 | // thread it would deadlock since we would be waiting on ourselves |
| 1642 | runnable.run(); |
| 1643 | } |
| 1644 | }, |
| 1645 | info -> { |
| 1646 | Bundle bundle = new Bundle(); |
| 1647 | bundle.putParcelable(BatteryStats.RESULT_RECEIVER_CONTROLLER_KEY, info); |
| 1648 | wifiReceiver.send(0, bundle); |
| 1649 | } |
| 1650 | ); |
| 1651 | final WifiActivityEnergyInfo wifiInfo = awaitControllerInfo(wifiReceiver); |
| 1652 | if (wifiInfo == null) { |
| 1653 | return StatsManager.PULL_SKIP; |
| 1654 | } |
| 1655 | StatsEvent e = StatsEvent.newBuilder() |
| 1656 | .setAtomId(atomTag) |
| 1657 | .writeLong(wifiInfo.getTimeSinceBootMillis()) |
| 1658 | .writeInt(wifiInfo.getStackState()) |
| 1659 | .writeLong(wifiInfo.getControllerTxDurationMillis()) |
| 1660 | .writeLong(wifiInfo.getControllerRxDurationMillis()) |
| 1661 | .writeLong(wifiInfo.getControllerIdleDurationMillis()) |
| 1662 | .writeLong(wifiInfo.getControllerEnergyUsedMicroJoules()) |
| 1663 | .build(); |
| 1664 | pulledData.add(e); |
| 1665 | } catch (RuntimeException e) { |
| 1666 | Slog.e(TAG, "failed to getWifiActivityEnergyInfoAsync", e); |
| 1667 | return StatsManager.PULL_SKIP; |
| 1668 | } finally { |
| 1669 | Binder.restoreCallingIdentity(token); |
| 1670 | } |
| 1671 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | private void registerModemActivityInfo() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 1675 | int tagId = FrameworkStatsLog.MODEM_ACTIVITY_INFO; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 1676 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | 1f81889 | 2020-01-14 12:05:05 -0800 | [diff] [blame] | 1677 | tagId, |
| 1678 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 1679 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 1680 | mStatsCallbackImpl |
Jeffrey Huang | 1f81889 | 2020-01-14 12:05:05 -0800 | [diff] [blame] | 1681 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1682 | } |
| 1683 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 1684 | int pullModemActivityInfoLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | 1f81889 | 2020-01-14 12:05:05 -0800 | [diff] [blame] | 1685 | long token = Binder.clearCallingIdentity(); |
| 1686 | try { |
| 1687 | SynchronousResultReceiver modemReceiver = new SynchronousResultReceiver("telephony"); |
| 1688 | mTelephony.requestModemActivityInfo(modemReceiver); |
| 1689 | final ModemActivityInfo modemInfo = awaitControllerInfo(modemReceiver); |
| 1690 | if (modemInfo == null) { |
| 1691 | return StatsManager.PULL_SKIP; |
| 1692 | } |
| 1693 | StatsEvent e = StatsEvent.newBuilder() |
| 1694 | .setAtomId(atomTag) |
| 1695 | .writeLong(modemInfo.getTimestamp()) |
| 1696 | .writeLong(modemInfo.getSleepTimeMillis()) |
| 1697 | .writeLong(modemInfo.getIdleTimeMillis()) |
| 1698 | .writeLong(modemInfo.getTransmitPowerInfo().get(0).getTimeInMillis()) |
| 1699 | .writeLong(modemInfo.getTransmitPowerInfo().get(1).getTimeInMillis()) |
| 1700 | .writeLong(modemInfo.getTransmitPowerInfo().get(2).getTimeInMillis()) |
| 1701 | .writeLong(modemInfo.getTransmitPowerInfo().get(3).getTimeInMillis()) |
| 1702 | .writeLong(modemInfo.getTransmitPowerInfo().get(4).getTimeInMillis()) |
| 1703 | .writeLong(modemInfo.getReceiveTimeMillis()) |
| 1704 | .build(); |
| 1705 | pulledData.add(e); |
| 1706 | } finally { |
| 1707 | Binder.restoreCallingIdentity(token); |
| 1708 | } |
| 1709 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | private void registerBluetoothActivityInfo() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 1713 | int tagId = FrameworkStatsLog.BLUETOOTH_ACTIVITY_INFO; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 1714 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | aafccb3 | 2020-01-13 10:16:58 -0800 | [diff] [blame] | 1715 | tagId, |
| 1716 | /* metadata */ null, |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 1717 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 1718 | mStatsCallbackImpl |
Jeffrey Huang | aafccb3 | 2020-01-13 10:16:58 -0800 | [diff] [blame] | 1719 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1720 | } |
| 1721 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 1722 | int pullBluetoothActivityInfoLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | aafccb3 | 2020-01-13 10:16:58 -0800 | [diff] [blame] | 1723 | BluetoothActivityEnergyInfo info = fetchBluetoothData(); |
| 1724 | if (info == null) { |
| 1725 | return StatsManager.PULL_SKIP; |
| 1726 | } |
| 1727 | StatsEvent e = StatsEvent.newBuilder() |
| 1728 | .setAtomId(atomTag) |
| 1729 | .writeLong(info.getTimeStamp()) |
| 1730 | .writeInt(info.getBluetoothStackState()) |
| 1731 | .writeLong(info.getControllerTxTimeMillis()) |
| 1732 | .writeLong(info.getControllerRxTimeMillis()) |
| 1733 | .writeLong(info.getControllerIdleTimeMillis()) |
| 1734 | .writeLong(info.getControllerEnergyUsed()) |
| 1735 | .build(); |
| 1736 | pulledData.add(e); |
| 1737 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1738 | } |
| 1739 | |
| 1740 | private void registerSystemElapsedRealtime() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 1741 | int tagId = FrameworkStatsLog.SYSTEM_ELAPSED_REALTIME; |
Jeffrey Huang | a85d8f9 | 2020-01-24 13:28:45 -0800 | [diff] [blame] | 1742 | PullAtomMetadata metadata = new PullAtomMetadata.Builder() |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 1743 | .setCoolDownMillis(MILLIS_PER_SEC) |
| 1744 | .setTimeoutMillis(MILLIS_PER_SEC / 2) |
Jeffrey Huang | 882f99a | 2020-01-14 14:33:52 -0800 | [diff] [blame] | 1745 | .build(); |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 1746 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | 882f99a | 2020-01-14 14:33:52 -0800 | [diff] [blame] | 1747 | tagId, |
| 1748 | metadata, |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 1749 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 1750 | mStatsCallbackImpl |
Jeffrey Huang | 882f99a | 2020-01-14 14:33:52 -0800 | [diff] [blame] | 1751 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1752 | } |
| 1753 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 1754 | int pullSystemElapsedRealtimeLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | 882f99a | 2020-01-14 14:33:52 -0800 | [diff] [blame] | 1755 | StatsEvent e = StatsEvent.newBuilder() |
| 1756 | .setAtomId(atomTag) |
| 1757 | .writeLong(SystemClock.elapsedRealtime()) |
| 1758 | .build(); |
| 1759 | pulledData.add(e); |
| 1760 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1761 | } |
| 1762 | |
| 1763 | private void registerSystemUptime() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 1764 | int tagId = FrameworkStatsLog.SYSTEM_UPTIME; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 1765 | mStatsManager.setPullAtomCallback( |
Ruchir Rastogi | ce29fc1 | 2020-01-09 17:08:20 -0800 | [diff] [blame] | 1766 | tagId, |
| 1767 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 1768 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 1769 | mStatsCallbackImpl |
Ruchir Rastogi | ce29fc1 | 2020-01-09 17:08:20 -0800 | [diff] [blame] | 1770 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1771 | } |
| 1772 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 1773 | int pullSystemUptimeLocked(int atomTag, List<StatsEvent> pulledData) { |
Ruchir Rastogi | ce29fc1 | 2020-01-09 17:08:20 -0800 | [diff] [blame] | 1774 | StatsEvent e = StatsEvent.newBuilder() |
| 1775 | .setAtomId(atomTag) |
tsaichristine | dc1c34a | 2020-03-18 14:43:06 -0700 | [diff] [blame] | 1776 | .writeLong(SystemClock.uptimeMillis()) |
Ruchir Rastogi | ce29fc1 | 2020-01-09 17:08:20 -0800 | [diff] [blame] | 1777 | .build(); |
| 1778 | pulledData.add(e); |
| 1779 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1780 | } |
| 1781 | |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1782 | private void registerProcessMemoryState() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 1783 | int tagId = FrameworkStatsLog.PROCESS_MEMORY_STATE; |
Jeffrey Huang | a85d8f9 | 2020-01-24 13:28:45 -0800 | [diff] [blame] | 1784 | PullAtomMetadata metadata = new PullAtomMetadata.Builder() |
Jeffrey Huang | b1c1634 | 2020-01-15 15:36:42 -0800 | [diff] [blame] | 1785 | .setAdditiveFields(new int[] {4, 5, 6, 7, 8}) |
| 1786 | .build(); |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 1787 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | b1c1634 | 2020-01-15 15:36:42 -0800 | [diff] [blame] | 1788 | tagId, |
| 1789 | metadata, |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 1790 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 1791 | mStatsCallbackImpl |
Jeffrey Huang | b1c1634 | 2020-01-15 15:36:42 -0800 | [diff] [blame] | 1792 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1793 | } |
| 1794 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 1795 | int pullProcessMemoryStateLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | b1c1634 | 2020-01-15 15:36:42 -0800 | [diff] [blame] | 1796 | List<ProcessMemoryState> processMemoryStates = |
| 1797 | LocalServices.getService(ActivityManagerInternal.class) |
| 1798 | .getMemoryStateForProcesses(); |
| 1799 | for (ProcessMemoryState processMemoryState : processMemoryStates) { |
| 1800 | final MemoryStat memoryStat = readMemoryStatFromFilesystem(processMemoryState.uid, |
| 1801 | processMemoryState.pid); |
| 1802 | if (memoryStat == null) { |
| 1803 | continue; |
| 1804 | } |
| 1805 | StatsEvent e = StatsEvent.newBuilder() |
| 1806 | .setAtomId(atomTag) |
| 1807 | .writeInt(processMemoryState.uid) |
Muhammad Qureshi | 22e52da | 2020-03-30 21:14:21 -0700 | [diff] [blame] | 1808 | .addBooleanAnnotation(ANNOTATION_ID_IS_UID, true) |
Jeffrey Huang | b1c1634 | 2020-01-15 15:36:42 -0800 | [diff] [blame] | 1809 | .writeString(processMemoryState.processName) |
| 1810 | .writeInt(processMemoryState.oomScore) |
| 1811 | .writeLong(memoryStat.pgfault) |
| 1812 | .writeLong(memoryStat.pgmajfault) |
| 1813 | .writeLong(memoryStat.rssInBytes) |
| 1814 | .writeLong(memoryStat.cacheInBytes) |
| 1815 | .writeLong(memoryStat.swapInBytes) |
| 1816 | .writeLong(-1) // unused |
| 1817 | .writeLong(-1) // unused |
| 1818 | .writeInt(-1) // unused |
| 1819 | .build(); |
| 1820 | pulledData.add(e); |
| 1821 | } |
| 1822 | return StatsManager.PULL_SUCCESS; |
| 1823 | } |
| 1824 | |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1825 | private void registerProcessMemoryHighWaterMark() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 1826 | int tagId = FrameworkStatsLog.PROCESS_MEMORY_HIGH_WATER_MARK; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 1827 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | b1c1634 | 2020-01-15 15:36:42 -0800 | [diff] [blame] | 1828 | tagId, |
| 1829 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 1830 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 1831 | mStatsCallbackImpl |
Jeffrey Huang | b1c1634 | 2020-01-15 15:36:42 -0800 | [diff] [blame] | 1832 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1833 | } |
| 1834 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 1835 | int pullProcessMemoryHighWaterMarkLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | b1c1634 | 2020-01-15 15:36:42 -0800 | [diff] [blame] | 1836 | List<ProcessMemoryState> managedProcessList = |
| 1837 | LocalServices.getService(ActivityManagerInternal.class) |
| 1838 | .getMemoryStateForProcesses(); |
| 1839 | for (ProcessMemoryState managedProcess : managedProcessList) { |
| 1840 | final MemorySnapshot snapshot = readMemorySnapshotFromProcfs(managedProcess.pid); |
| 1841 | if (snapshot == null) { |
| 1842 | continue; |
| 1843 | } |
| 1844 | StatsEvent e = StatsEvent.newBuilder() |
| 1845 | .setAtomId(atomTag) |
| 1846 | .writeInt(managedProcess.uid) |
Muhammad Qureshi | 22e52da | 2020-03-30 21:14:21 -0700 | [diff] [blame] | 1847 | .addBooleanAnnotation(ANNOTATION_ID_IS_UID, true) |
Jeffrey Huang | b1c1634 | 2020-01-15 15:36:42 -0800 | [diff] [blame] | 1848 | .writeString(managedProcess.processName) |
| 1849 | // RSS high-water mark in bytes. |
| 1850 | .writeLong(snapshot.rssHighWaterMarkInKilobytes * 1024L) |
| 1851 | .writeInt(snapshot.rssHighWaterMarkInKilobytes) |
| 1852 | .build(); |
| 1853 | pulledData.add(e); |
| 1854 | } |
Ioannis Ilkos | 754eb07 | 2020-01-27 16:42:24 +0000 | [diff] [blame] | 1855 | // Complement the data with native system processes |
| 1856 | SparseArray<String> processCmdlines = getProcessCmdlines(); |
| 1857 | managedProcessList.forEach(managedProcess -> processCmdlines.delete(managedProcess.pid)); |
| 1858 | int size = processCmdlines.size(); |
| 1859 | for (int i = 0; i < size; ++i) { |
| 1860 | final MemorySnapshot snapshot = readMemorySnapshotFromProcfs(processCmdlines.keyAt(i)); |
Ioannis Ilkos | e0ec1f3 | 2020-06-11 14:56:13 +0100 | [diff] [blame] | 1861 | if (snapshot == null) { |
Ioannis Ilkos | 754eb07 | 2020-01-27 16:42:24 +0000 | [diff] [blame] | 1862 | continue; |
Jeffrey Huang | b1c1634 | 2020-01-15 15:36:42 -0800 | [diff] [blame] | 1863 | } |
| 1864 | StatsEvent e = StatsEvent.newBuilder() |
| 1865 | .setAtomId(atomTag) |
| 1866 | .writeInt(snapshot.uid) |
Muhammad Qureshi | 22e52da | 2020-03-30 21:14:21 -0700 | [diff] [blame] | 1867 | .addBooleanAnnotation(ANNOTATION_ID_IS_UID, true) |
Ioannis Ilkos | 754eb07 | 2020-01-27 16:42:24 +0000 | [diff] [blame] | 1868 | .writeString(processCmdlines.valueAt(i)) |
Jeffrey Huang | b1c1634 | 2020-01-15 15:36:42 -0800 | [diff] [blame] | 1869 | // RSS high-water mark in bytes. |
| 1870 | .writeLong(snapshot.rssHighWaterMarkInKilobytes * 1024L) |
| 1871 | .writeInt(snapshot.rssHighWaterMarkInKilobytes) |
| 1872 | .build(); |
| 1873 | pulledData.add(e); |
Ioannis Ilkos | 754eb07 | 2020-01-27 16:42:24 +0000 | [diff] [blame] | 1874 | } |
Jeffrey Huang | b1c1634 | 2020-01-15 15:36:42 -0800 | [diff] [blame] | 1875 | // Invoke rss_hwm_reset binary to reset RSS HWM counters for all processes. |
| 1876 | SystemProperties.set("sys.rss_hwm_reset.on", "1"); |
| 1877 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | private void registerProcessMemorySnapshot() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 1881 | int tagId = FrameworkStatsLog.PROCESS_MEMORY_SNAPSHOT; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 1882 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | b1c1634 | 2020-01-15 15:36:42 -0800 | [diff] [blame] | 1883 | tagId, |
| 1884 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 1885 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 1886 | mStatsCallbackImpl |
Jeffrey Huang | b1c1634 | 2020-01-15 15:36:42 -0800 | [diff] [blame] | 1887 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1888 | } |
| 1889 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 1890 | int pullProcessMemorySnapshotLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | b1c1634 | 2020-01-15 15:36:42 -0800 | [diff] [blame] | 1891 | List<ProcessMemoryState> managedProcessList = |
| 1892 | LocalServices.getService(ActivityManagerInternal.class) |
| 1893 | .getMemoryStateForProcesses(); |
| 1894 | for (ProcessMemoryState managedProcess : managedProcessList) { |
| 1895 | final MemorySnapshot snapshot = readMemorySnapshotFromProcfs(managedProcess.pid); |
| 1896 | if (snapshot == null) { |
| 1897 | continue; |
| 1898 | } |
| 1899 | StatsEvent e = StatsEvent.newBuilder() |
Ioannis Ilkos | da5d881 | 2020-01-27 16:04:10 +0000 | [diff] [blame] | 1900 | .setAtomId(atomTag) |
Jeffrey Huang | b1c1634 | 2020-01-15 15:36:42 -0800 | [diff] [blame] | 1901 | .writeInt(managedProcess.uid) |
Muhammad Qureshi | 22e52da | 2020-03-30 21:14:21 -0700 | [diff] [blame] | 1902 | .addBooleanAnnotation(ANNOTATION_ID_IS_UID, true) |
Jeffrey Huang | b1c1634 | 2020-01-15 15:36:42 -0800 | [diff] [blame] | 1903 | .writeString(managedProcess.processName) |
| 1904 | .writeInt(managedProcess.pid) |
| 1905 | .writeInt(managedProcess.oomScore) |
| 1906 | .writeInt(snapshot.rssInKilobytes) |
| 1907 | .writeInt(snapshot.anonRssInKilobytes) |
| 1908 | .writeInt(snapshot.swapInKilobytes) |
| 1909 | .writeInt(snapshot.anonRssInKilobytes + snapshot.swapInKilobytes) |
| 1910 | .build(); |
| 1911 | pulledData.add(e); |
| 1912 | } |
Ioannis Ilkos | 754eb07 | 2020-01-27 16:42:24 +0000 | [diff] [blame] | 1913 | // Complement the data with native system processes. Given these measurements can be taken |
| 1914 | // in response to LMKs happening, we want to first collect the managed app stats (to |
| 1915 | // maximize the probability that a heavyweight process will be sampled before it dies). |
| 1916 | SparseArray<String> processCmdlines = getProcessCmdlines(); |
| 1917 | managedProcessList.forEach(managedProcess -> processCmdlines.delete(managedProcess.pid)); |
| 1918 | int size = processCmdlines.size(); |
| 1919 | for (int i = 0; i < size; ++i) { |
| 1920 | int pid = processCmdlines.keyAt(i); |
Jeffrey Huang | b1c1634 | 2020-01-15 15:36:42 -0800 | [diff] [blame] | 1921 | final MemorySnapshot snapshot = readMemorySnapshotFromProcfs(pid); |
Ioannis Ilkos | e0ec1f3 | 2020-06-11 14:56:13 +0100 | [diff] [blame] | 1922 | if (snapshot == null) { |
Ioannis Ilkos | 754eb07 | 2020-01-27 16:42:24 +0000 | [diff] [blame] | 1923 | continue; |
Jeffrey Huang | b1c1634 | 2020-01-15 15:36:42 -0800 | [diff] [blame] | 1924 | } |
| 1925 | StatsEvent e = StatsEvent.newBuilder() |
| 1926 | .setAtomId(atomTag) |
| 1927 | .writeInt(snapshot.uid) |
Muhammad Qureshi | 22e52da | 2020-03-30 21:14:21 -0700 | [diff] [blame] | 1928 | .addBooleanAnnotation(ANNOTATION_ID_IS_UID, true) |
Ioannis Ilkos | 754eb07 | 2020-01-27 16:42:24 +0000 | [diff] [blame] | 1929 | .writeString(processCmdlines.valueAt(i)) |
Jeffrey Huang | b1c1634 | 2020-01-15 15:36:42 -0800 | [diff] [blame] | 1930 | .writeInt(pid) |
| 1931 | .writeInt(-1001) // Placeholder for native processes, OOM_SCORE_ADJ_MIN - 1. |
| 1932 | .writeInt(snapshot.rssInKilobytes) |
| 1933 | .writeInt(snapshot.anonRssInKilobytes) |
| 1934 | .writeInt(snapshot.swapInKilobytes) |
| 1935 | .writeInt(snapshot.anonRssInKilobytes + snapshot.swapInKilobytes) |
| 1936 | .build(); |
| 1937 | pulledData.add(e); |
Ioannis Ilkos | 754eb07 | 2020-01-27 16:42:24 +0000 | [diff] [blame] | 1938 | } |
Jeffrey Huang | b1c1634 | 2020-01-15 15:36:42 -0800 | [diff] [blame] | 1939 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | private void registerSystemIonHeapSize() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 1943 | int tagId = FrameworkStatsLog.SYSTEM_ION_HEAP_SIZE; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 1944 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | a0e1cd2 | 2020-01-15 16:05:07 -0800 | [diff] [blame] | 1945 | tagId, |
| 1946 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 1947 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 1948 | mStatsCallbackImpl |
Jeffrey Huang | a0e1cd2 | 2020-01-15 16:05:07 -0800 | [diff] [blame] | 1949 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1950 | } |
| 1951 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 1952 | int pullSystemIonHeapSizeLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | a0e1cd2 | 2020-01-15 16:05:07 -0800 | [diff] [blame] | 1953 | final long systemIonHeapSizeInBytes = readSystemIonHeapSizeFromDebugfs(); |
| 1954 | StatsEvent e = StatsEvent.newBuilder() |
| 1955 | .setAtomId(atomTag) |
| 1956 | .writeLong(systemIonHeapSizeInBytes) |
| 1957 | .build(); |
| 1958 | pulledData.add(e); |
| 1959 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1960 | } |
| 1961 | |
Rafal Slawik | bdd5a50 | 2020-01-14 14:14:29 +0000 | [diff] [blame] | 1962 | private void registerIonHeapSize() { |
Rafal Slawik | ded8305 | 2020-02-28 13:20:13 +0000 | [diff] [blame] | 1963 | if (!new File("/sys/kernel/ion/total_heaps_kb").exists()) { |
| 1964 | return; |
| 1965 | } |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 1966 | int tagId = FrameworkStatsLog.ION_HEAP_SIZE; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 1967 | mStatsManager.setPullAtomCallback( |
Rafal Slawik | bdd5a50 | 2020-01-14 14:14:29 +0000 | [diff] [blame] | 1968 | tagId, |
| 1969 | /* PullAtomMetadata */ null, |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 1970 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 1971 | mStatsCallbackImpl |
Rafal Slawik | bdd5a50 | 2020-01-14 14:14:29 +0000 | [diff] [blame] | 1972 | ); |
| 1973 | } |
| 1974 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 1975 | int pullIonHeapSizeLocked(int atomTag, List<StatsEvent> pulledData) { |
Rafal Slawik | bdd5a50 | 2020-01-14 14:14:29 +0000 | [diff] [blame] | 1976 | int ionHeapSizeInKilobytes = (int) getIonHeapsSizeKb(); |
| 1977 | StatsEvent e = StatsEvent.newBuilder() |
| 1978 | .setAtomId(atomTag) |
| 1979 | .writeInt(ionHeapSizeInKilobytes) |
| 1980 | .build(); |
| 1981 | pulledData.add(e); |
| 1982 | return StatsManager.PULL_SUCCESS; |
| 1983 | } |
| 1984 | |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1985 | private void registerProcessSystemIonHeapSize() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 1986 | int tagId = FrameworkStatsLog.PROCESS_SYSTEM_ION_HEAP_SIZE; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 1987 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | a0e1cd2 | 2020-01-15 16:05:07 -0800 | [diff] [blame] | 1988 | tagId, |
| 1989 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 1990 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 1991 | mStatsCallbackImpl |
Jeffrey Huang | a0e1cd2 | 2020-01-15 16:05:07 -0800 | [diff] [blame] | 1992 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 1993 | } |
| 1994 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 1995 | int pullProcessSystemIonHeapSizeLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | a0e1cd2 | 2020-01-15 16:05:07 -0800 | [diff] [blame] | 1996 | List<IonAllocations> result = readProcessSystemIonHeapSizesFromDebugfs(); |
| 1997 | for (IonAllocations allocations : result) { |
| 1998 | StatsEvent e = StatsEvent.newBuilder() |
| 1999 | .setAtomId(atomTag) |
| 2000 | .writeInt(getUidForPid(allocations.pid)) |
Muhammad Qureshi | 22e52da | 2020-03-30 21:14:21 -0700 | [diff] [blame] | 2001 | .addBooleanAnnotation(ANNOTATION_ID_IS_UID, true) |
Jeffrey Huang | a0e1cd2 | 2020-01-15 16:05:07 -0800 | [diff] [blame] | 2002 | .writeString(readCmdlineFromProcfs(allocations.pid)) |
| 2003 | .writeInt((int) (allocations.totalSizeInBytes / 1024)) |
| 2004 | .writeInt(allocations.count) |
| 2005 | .writeInt((int) (allocations.maxSizeInBytes / 1024)) |
| 2006 | .build(); |
| 2007 | pulledData.add(e); |
| 2008 | } |
| 2009 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2010 | } |
| 2011 | |
| 2012 | private void registerTemperature() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2013 | int tagId = FrameworkStatsLog.TEMPERATURE; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 2014 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | b92de55 | 2020-01-15 16:50:07 -0800 | [diff] [blame] | 2015 | tagId, |
| 2016 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 2017 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 2018 | mStatsCallbackImpl |
Jeffrey Huang | b92de55 | 2020-01-15 16:50:07 -0800 | [diff] [blame] | 2019 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2020 | } |
| 2021 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2022 | int pullTemperatureLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | b92de55 | 2020-01-15 16:50:07 -0800 | [diff] [blame] | 2023 | IThermalService thermalService = getIThermalService(); |
| 2024 | if (thermalService == null) { |
| 2025 | return StatsManager.PULL_SKIP; |
| 2026 | } |
| 2027 | final long callingToken = Binder.clearCallingIdentity(); |
| 2028 | try { |
Chris Ye | 48dbcaa | 2020-02-10 13:29:01 -0800 | [diff] [blame] | 2029 | Temperature temperatures[] = thermalService.getCurrentTemperatures(); |
Jeffrey Huang | b92de55 | 2020-01-15 16:50:07 -0800 | [diff] [blame] | 2030 | for (Temperature temp : temperatures) { |
| 2031 | StatsEvent e = StatsEvent.newBuilder() |
| 2032 | .setAtomId(atomTag) |
| 2033 | .writeInt(temp.getType()) |
| 2034 | .writeString(temp.getName()) |
| 2035 | .writeInt((int) (temp.getValue() * 10)) |
| 2036 | .writeInt(temp.getStatus()) |
| 2037 | .build(); |
| 2038 | pulledData.add(e); |
| 2039 | } |
| 2040 | } catch (RemoteException e) { |
| 2041 | // Should not happen. |
| 2042 | Slog.e(TAG, "Disconnected from thermal service. Cannot pull temperatures."); |
| 2043 | return StatsManager.PULL_SKIP; |
| 2044 | } finally { |
| 2045 | Binder.restoreCallingIdentity(callingToken); |
| 2046 | } |
| 2047 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | private void registerCoolingDevice() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2051 | int tagId = FrameworkStatsLog.COOLING_DEVICE; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 2052 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | b92de55 | 2020-01-15 16:50:07 -0800 | [diff] [blame] | 2053 | tagId, |
| 2054 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 2055 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 2056 | mStatsCallbackImpl |
Jeffrey Huang | b92de55 | 2020-01-15 16:50:07 -0800 | [diff] [blame] | 2057 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2058 | } |
| 2059 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2060 | int pullCooldownDeviceLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | b92de55 | 2020-01-15 16:50:07 -0800 | [diff] [blame] | 2061 | IThermalService thermalService = getIThermalService(); |
| 2062 | if (thermalService == null) { |
| 2063 | return StatsManager.PULL_SKIP; |
| 2064 | } |
| 2065 | final long callingToken = Binder.clearCallingIdentity(); |
| 2066 | try { |
Chris Ye | 48dbcaa | 2020-02-10 13:29:01 -0800 | [diff] [blame] | 2067 | CoolingDevice devices[] = thermalService.getCurrentCoolingDevices(); |
Jeffrey Huang | b92de55 | 2020-01-15 16:50:07 -0800 | [diff] [blame] | 2068 | for (CoolingDevice device : devices) { |
| 2069 | StatsEvent e = StatsEvent.newBuilder() |
| 2070 | .setAtomId(atomTag) |
| 2071 | .writeInt(device.getType()) |
| 2072 | .writeString(device.getName()) |
| 2073 | .writeInt((int) (device.getValue())) |
| 2074 | .build(); |
| 2075 | pulledData.add(e); |
| 2076 | } |
| 2077 | } catch (RemoteException e) { |
| 2078 | // Should not happen. |
| 2079 | Slog.e(TAG, "Disconnected from thermal service. Cannot pull temperatures."); |
| 2080 | return StatsManager.PULL_SKIP; |
| 2081 | } finally { |
| 2082 | Binder.restoreCallingIdentity(callingToken); |
| 2083 | } |
| 2084 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2085 | } |
| 2086 | |
Jeffrey Huang | 877adfe | 2020-01-15 17:16:43 -0800 | [diff] [blame] | 2087 | private void registerBinderCallsStats() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2088 | int tagId = FrameworkStatsLog.BINDER_CALLS; |
Jeffrey Huang | a85d8f9 | 2020-01-24 13:28:45 -0800 | [diff] [blame] | 2089 | PullAtomMetadata metadata = new PullAtomMetadata.Builder() |
Jeffrey Huang | 877adfe | 2020-01-15 17:16:43 -0800 | [diff] [blame] | 2090 | .setAdditiveFields(new int[] {4, 5, 6, 8, 12}) |
| 2091 | .build(); |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 2092 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | 877adfe | 2020-01-15 17:16:43 -0800 | [diff] [blame] | 2093 | tagId, |
| 2094 | metadata, |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 2095 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 2096 | mStatsCallbackImpl |
Jeffrey Huang | 877adfe | 2020-01-15 17:16:43 -0800 | [diff] [blame] | 2097 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2098 | } |
| 2099 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2100 | int pullBinderCallsStatsLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | 877adfe | 2020-01-15 17:16:43 -0800 | [diff] [blame] | 2101 | BinderCallsStatsService.Internal binderStats = |
| 2102 | LocalServices.getService(BinderCallsStatsService.Internal.class); |
| 2103 | if (binderStats == null) { |
| 2104 | Slog.e(TAG, "failed to get binderStats"); |
| 2105 | return StatsManager.PULL_SKIP; |
| 2106 | } |
| 2107 | |
| 2108 | List<ExportedCallStat> callStats = binderStats.getExportedCallStats(); |
| 2109 | binderStats.reset(); |
| 2110 | for (ExportedCallStat callStat : callStats) { |
| 2111 | StatsEvent e = StatsEvent.newBuilder() |
| 2112 | .setAtomId(atomTag) |
| 2113 | .writeInt(callStat.workSourceUid) |
Muhammad Qureshi | 22e52da | 2020-03-30 21:14:21 -0700 | [diff] [blame] | 2114 | .addBooleanAnnotation(ANNOTATION_ID_IS_UID, true) |
Jeffrey Huang | 877adfe | 2020-01-15 17:16:43 -0800 | [diff] [blame] | 2115 | .writeString(callStat.className) |
| 2116 | .writeString(callStat.methodName) |
| 2117 | .writeLong(callStat.callCount) |
| 2118 | .writeLong(callStat.exceptionCount) |
| 2119 | .writeLong(callStat.latencyMicros) |
| 2120 | .writeLong(callStat.maxLatencyMicros) |
| 2121 | .writeLong(callStat.cpuTimeMicros) |
| 2122 | .writeLong(callStat.maxCpuTimeMicros) |
| 2123 | .writeLong(callStat.maxReplySizeBytes) |
| 2124 | .writeLong(callStat.maxRequestSizeBytes) |
| 2125 | .writeLong(callStat.recordedCallCount) |
| 2126 | .writeInt(callStat.screenInteractive ? 1 : 0) |
| 2127 | .writeInt(callStat.callingUid) |
| 2128 | .build(); |
| 2129 | pulledData.add(e); |
| 2130 | } |
| 2131 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2132 | } |
| 2133 | |
Jeffrey Huang | 877adfe | 2020-01-15 17:16:43 -0800 | [diff] [blame] | 2134 | private void registerBinderCallsStatsExceptions() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2135 | int tagId = FrameworkStatsLog.BINDER_CALLS_EXCEPTIONS; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 2136 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | 877adfe | 2020-01-15 17:16:43 -0800 | [diff] [blame] | 2137 | tagId, |
| 2138 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 2139 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 2140 | mStatsCallbackImpl |
Jeffrey Huang | 877adfe | 2020-01-15 17:16:43 -0800 | [diff] [blame] | 2141 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2142 | } |
| 2143 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2144 | int pullBinderCallsStatsExceptionsLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | 877adfe | 2020-01-15 17:16:43 -0800 | [diff] [blame] | 2145 | BinderCallsStatsService.Internal binderStats = |
| 2146 | LocalServices.getService(BinderCallsStatsService.Internal.class); |
| 2147 | if (binderStats == null) { |
| 2148 | Slog.e(TAG, "failed to get binderStats"); |
| 2149 | return StatsManager.PULL_SKIP; |
| 2150 | } |
| 2151 | |
| 2152 | ArrayMap<String, Integer> exceptionStats = binderStats.getExportedExceptionStats(); |
| 2153 | // TODO: decouple binder calls exceptions with the rest of the binder calls data so that we |
| 2154 | // can reset the exception stats. |
| 2155 | for (Map.Entry<String, Integer> entry : exceptionStats.entrySet()) { |
| 2156 | StatsEvent e = StatsEvent.newBuilder() |
| 2157 | .setAtomId(atomTag) |
| 2158 | .writeString(entry.getKey()) |
| 2159 | .writeInt(entry.getValue()) |
| 2160 | .build(); |
| 2161 | pulledData.add(e); |
| 2162 | } |
| 2163 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2164 | } |
| 2165 | |
| 2166 | private void registerLooperStats() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2167 | int tagId = FrameworkStatsLog.LOOPER_STATS; |
Jeffrey Huang | a85d8f9 | 2020-01-24 13:28:45 -0800 | [diff] [blame] | 2168 | PullAtomMetadata metadata = new PullAtomMetadata.Builder() |
Jeffrey Huang | fd037c5 | 2020-01-16 10:42:59 -0800 | [diff] [blame] | 2169 | .setAdditiveFields(new int[] {5, 6, 7, 8, 9}) |
| 2170 | .build(); |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 2171 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | fd037c5 | 2020-01-16 10:42:59 -0800 | [diff] [blame] | 2172 | tagId, |
| 2173 | metadata, |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 2174 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 2175 | mStatsCallbackImpl |
Jeffrey Huang | fd037c5 | 2020-01-16 10:42:59 -0800 | [diff] [blame] | 2176 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2177 | } |
| 2178 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2179 | int pullLooperStatsLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | fd037c5 | 2020-01-16 10:42:59 -0800 | [diff] [blame] | 2180 | LooperStats looperStats = LocalServices.getService(LooperStats.class); |
| 2181 | if (looperStats == null) { |
| 2182 | return StatsManager.PULL_SKIP; |
| 2183 | } |
| 2184 | |
| 2185 | List<LooperStats.ExportedEntry> entries = looperStats.getEntries(); |
| 2186 | looperStats.reset(); |
| 2187 | for (LooperStats.ExportedEntry entry : entries) { |
| 2188 | StatsEvent e = StatsEvent.newBuilder() |
| 2189 | .setAtomId(atomTag) |
| 2190 | .writeInt(entry.workSourceUid) |
Muhammad Qureshi | 22e52da | 2020-03-30 21:14:21 -0700 | [diff] [blame] | 2191 | .addBooleanAnnotation(ANNOTATION_ID_IS_UID, true) |
Jeffrey Huang | fd037c5 | 2020-01-16 10:42:59 -0800 | [diff] [blame] | 2192 | .writeString(entry.handlerClassName) |
| 2193 | .writeString(entry.threadName) |
| 2194 | .writeString(entry.messageName) |
| 2195 | .writeLong(entry.messageCount) |
| 2196 | .writeLong(entry.exceptionCount) |
| 2197 | .writeLong(entry.recordedMessageCount) |
| 2198 | .writeLong(entry.totalLatencyMicros) |
| 2199 | .writeLong(entry.cpuUsageMicros) |
| 2200 | .writeBoolean(entry.isInteractive) |
| 2201 | .writeLong(entry.maxCpuUsageMicros) |
| 2202 | .writeLong(entry.maxLatencyMicros) |
| 2203 | .writeLong(entry.recordedDelayMessageCount) |
| 2204 | .writeLong(entry.delayMillis) |
| 2205 | .writeLong(entry.maxDelayMillis) |
| 2206 | .build(); |
| 2207 | pulledData.add(e); |
| 2208 | } |
| 2209 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | private void registerDiskStats() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2213 | int tagId = FrameworkStatsLog.DISK_STATS; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 2214 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | 95765f0 | 2020-01-16 11:33:58 -0800 | [diff] [blame] | 2215 | tagId, |
| 2216 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 2217 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 2218 | mStatsCallbackImpl |
Jeffrey Huang | 95765f0 | 2020-01-16 11:33:58 -0800 | [diff] [blame] | 2219 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2220 | } |
| 2221 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2222 | int pullDiskStatsLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | 95765f0 | 2020-01-16 11:33:58 -0800 | [diff] [blame] | 2223 | // Run a quick-and-dirty performance test: write 512 bytes |
| 2224 | byte[] junk = new byte[512]; |
| 2225 | for (int i = 0; i < junk.length; i++) junk[i] = (byte) i; // Write nonzero bytes |
| 2226 | |
| 2227 | File tmp = new File(Environment.getDataDirectory(), "system/statsdperftest.tmp"); |
| 2228 | FileOutputStream fos = null; |
| 2229 | IOException error = null; |
| 2230 | |
| 2231 | long before = SystemClock.elapsedRealtime(); |
| 2232 | try { |
| 2233 | fos = new FileOutputStream(tmp); |
| 2234 | fos.write(junk); |
| 2235 | } catch (IOException e) { |
| 2236 | error = e; |
| 2237 | } finally { |
| 2238 | try { |
| 2239 | if (fos != null) fos.close(); |
| 2240 | } catch (IOException e) { |
| 2241 | // Do nothing. |
| 2242 | } |
| 2243 | } |
| 2244 | |
| 2245 | long latency = SystemClock.elapsedRealtime() - before; |
| 2246 | if (tmp.exists()) tmp.delete(); |
| 2247 | |
| 2248 | if (error != null) { |
| 2249 | Slog.e(TAG, "Error performing diskstats latency test"); |
| 2250 | latency = -1; |
| 2251 | } |
| 2252 | // File based encryption. |
| 2253 | boolean fileBased = StorageManager.isFileEncryptedNativeOnly(); |
| 2254 | |
| 2255 | //Recent disk write speed. Binder call to storaged. |
| 2256 | int writeSpeed = -1; |
| 2257 | IStoraged storaged = getIStoragedService(); |
| 2258 | if (storaged == null) { |
| 2259 | return StatsManager.PULL_SKIP; |
| 2260 | } |
| 2261 | try { |
| 2262 | writeSpeed = storaged.getRecentPerf(); |
| 2263 | } catch (RemoteException e) { |
| 2264 | Slog.e(TAG, "storaged not found"); |
| 2265 | } |
| 2266 | |
| 2267 | // Add info pulledData. |
| 2268 | StatsEvent e = StatsEvent.newBuilder() |
| 2269 | .setAtomId(atomTag) |
| 2270 | .writeLong(latency) |
| 2271 | .writeBoolean(fileBased) |
| 2272 | .writeInt(writeSpeed) |
| 2273 | .build(); |
| 2274 | pulledData.add(e); |
| 2275 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2276 | } |
| 2277 | |
| 2278 | private void registerDirectoryUsage() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2279 | int tagId = FrameworkStatsLog.DIRECTORY_USAGE; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 2280 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | 95765f0 | 2020-01-16 11:33:58 -0800 | [diff] [blame] | 2281 | tagId, |
| 2282 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 2283 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 2284 | mStatsCallbackImpl |
Jeffrey Huang | 95765f0 | 2020-01-16 11:33:58 -0800 | [diff] [blame] | 2285 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2286 | } |
| 2287 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2288 | int pullDirectoryUsageLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | 95765f0 | 2020-01-16 11:33:58 -0800 | [diff] [blame] | 2289 | StatFs statFsData = new StatFs(Environment.getDataDirectory().getAbsolutePath()); |
| 2290 | StatFs statFsSystem = new StatFs(Environment.getRootDirectory().getAbsolutePath()); |
| 2291 | StatFs statFsCache = new StatFs(Environment.getDownloadCacheDirectory().getAbsolutePath()); |
| 2292 | |
| 2293 | StatsEvent e = StatsEvent.newBuilder() |
| 2294 | .setAtomId(atomTag) |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2295 | .writeInt(FrameworkStatsLog.DIRECTORY_USAGE__DIRECTORY__DATA) |
Jeffrey Huang | 95765f0 | 2020-01-16 11:33:58 -0800 | [diff] [blame] | 2296 | .writeLong(statFsData.getAvailableBytes()) |
| 2297 | .writeLong(statFsData.getTotalBytes()) |
| 2298 | .build(); |
| 2299 | pulledData.add(e); |
| 2300 | |
| 2301 | e = StatsEvent.newBuilder() |
| 2302 | .setAtomId(atomTag) |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2303 | .writeInt(FrameworkStatsLog.DIRECTORY_USAGE__DIRECTORY__CACHE) |
Jeffrey Huang | 95765f0 | 2020-01-16 11:33:58 -0800 | [diff] [blame] | 2304 | .writeLong(statFsCache.getAvailableBytes()) |
| 2305 | .writeLong(statFsCache.getTotalBytes()) |
| 2306 | .build(); |
| 2307 | pulledData.add(e); |
| 2308 | |
| 2309 | e = StatsEvent.newBuilder() |
| 2310 | .setAtomId(atomTag) |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2311 | .writeInt(FrameworkStatsLog.DIRECTORY_USAGE__DIRECTORY__SYSTEM) |
Jeffrey Huang | 95765f0 | 2020-01-16 11:33:58 -0800 | [diff] [blame] | 2312 | .writeLong(statFsSystem.getAvailableBytes()) |
| 2313 | .writeLong(statFsSystem.getTotalBytes()) |
| 2314 | .build(); |
| 2315 | pulledData.add(e); |
| 2316 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2317 | } |
| 2318 | |
| 2319 | private void registerAppSize() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2320 | int tagId = FrameworkStatsLog.APP_SIZE; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 2321 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | e85d7ad | 2020-01-16 13:02:22 -0800 | [diff] [blame] | 2322 | tagId, |
| 2323 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 2324 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 2325 | mStatsCallbackImpl |
Jeffrey Huang | e85d7ad | 2020-01-16 13:02:22 -0800 | [diff] [blame] | 2326 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2327 | } |
| 2328 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2329 | int pullAppSizeLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | e85d7ad | 2020-01-16 13:02:22 -0800 | [diff] [blame] | 2330 | try { |
| 2331 | String jsonStr = IoUtils.readFileAsString(DiskStatsLoggingService.DUMPSYS_CACHE_PATH); |
| 2332 | JSONObject json = new JSONObject(jsonStr); |
| 2333 | long cache_time = json.optLong(DiskStatsFileLogger.LAST_QUERY_TIMESTAMP_KEY, -1L); |
| 2334 | JSONArray pkg_names = json.getJSONArray(DiskStatsFileLogger.PACKAGE_NAMES_KEY); |
| 2335 | JSONArray app_sizes = json.getJSONArray(DiskStatsFileLogger.APP_SIZES_KEY); |
| 2336 | JSONArray app_data_sizes = json.getJSONArray(DiskStatsFileLogger.APP_DATA_KEY); |
| 2337 | JSONArray app_cache_sizes = json.getJSONArray(DiskStatsFileLogger.APP_CACHES_KEY); |
| 2338 | // Sanity check: Ensure all 4 lists have the same length. |
| 2339 | int length = pkg_names.length(); |
| 2340 | if (app_sizes.length() != length || app_data_sizes.length() != length |
| 2341 | || app_cache_sizes.length() != length) { |
| 2342 | Slog.e(TAG, "formatting error in diskstats cache file!"); |
| 2343 | return StatsManager.PULL_SKIP; |
| 2344 | } |
| 2345 | for (int i = 0; i < length; i++) { |
| 2346 | StatsEvent e = StatsEvent.newBuilder() |
| 2347 | .setAtomId(atomTag) |
| 2348 | .writeString(pkg_names.getString(i)) |
| 2349 | .writeLong(app_sizes.optLong(i, /* fallback */ -1L)) |
| 2350 | .writeLong(app_data_sizes.optLong(i, /* fallback */ -1L)) |
| 2351 | .writeLong(app_cache_sizes.optLong(i, /* fallback */ -1L)) |
| 2352 | .writeLong(cache_time) |
| 2353 | .build(); |
| 2354 | pulledData.add(e); |
| 2355 | } |
| 2356 | } catch (IOException | JSONException e) { |
Ruchir Rastogi | e965f4a | 2020-06-05 08:32:05 -0700 | [diff] [blame] | 2357 | Slog.w(TAG, "Unable to read diskstats cache file within pullAppSize"); |
Jeffrey Huang | e85d7ad | 2020-01-16 13:02:22 -0800 | [diff] [blame] | 2358 | return StatsManager.PULL_SKIP; |
| 2359 | } |
| 2360 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2361 | } |
| 2362 | |
| 2363 | private void registerCategorySize() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2364 | int tagId = FrameworkStatsLog.CATEGORY_SIZE; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 2365 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | e85d7ad | 2020-01-16 13:02:22 -0800 | [diff] [blame] | 2366 | tagId, |
| 2367 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 2368 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 2369 | mStatsCallbackImpl |
Jeffrey Huang | e85d7ad | 2020-01-16 13:02:22 -0800 | [diff] [blame] | 2370 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2371 | } |
| 2372 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2373 | int pullCategorySizeLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | e85d7ad | 2020-01-16 13:02:22 -0800 | [diff] [blame] | 2374 | try { |
| 2375 | String jsonStr = IoUtils.readFileAsString(DiskStatsLoggingService.DUMPSYS_CACHE_PATH); |
| 2376 | JSONObject json = new JSONObject(jsonStr); |
| 2377 | long cacheTime = json.optLong( |
| 2378 | DiskStatsFileLogger.LAST_QUERY_TIMESTAMP_KEY, /* fallback */ -1L); |
| 2379 | |
| 2380 | StatsEvent e = StatsEvent.newBuilder() |
| 2381 | .setAtomId(atomTag) |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2382 | .writeInt(FrameworkStatsLog.CATEGORY_SIZE__CATEGORY__APP_SIZE) |
Jeffrey Huang | e85d7ad | 2020-01-16 13:02:22 -0800 | [diff] [blame] | 2383 | .writeLong(json.optLong( |
| 2384 | DiskStatsFileLogger.APP_SIZE_AGG_KEY, /* fallback */ -1L)) |
| 2385 | .writeLong(cacheTime) |
| 2386 | .build(); |
| 2387 | pulledData.add(e); |
| 2388 | |
| 2389 | e = StatsEvent.newBuilder() |
| 2390 | .setAtomId(atomTag) |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2391 | .writeInt(FrameworkStatsLog.CATEGORY_SIZE__CATEGORY__APP_DATA_SIZE) |
Jeffrey Huang | e85d7ad | 2020-01-16 13:02:22 -0800 | [diff] [blame] | 2392 | .writeLong(json.optLong( |
| 2393 | DiskStatsFileLogger.APP_DATA_SIZE_AGG_KEY, /* fallback */ -1L)) |
| 2394 | .writeLong(cacheTime) |
| 2395 | .build(); |
| 2396 | pulledData.add(e); |
| 2397 | |
| 2398 | e = StatsEvent.newBuilder() |
| 2399 | .setAtomId(atomTag) |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2400 | .writeInt(FrameworkStatsLog.CATEGORY_SIZE__CATEGORY__APP_CACHE_SIZE) |
Jeffrey Huang | e85d7ad | 2020-01-16 13:02:22 -0800 | [diff] [blame] | 2401 | .writeLong(json.optLong( |
| 2402 | DiskStatsFileLogger.APP_CACHE_AGG_KEY, /* fallback */ -1L)) |
| 2403 | .writeLong(cacheTime) |
| 2404 | .build(); |
| 2405 | pulledData.add(e); |
| 2406 | |
| 2407 | e = StatsEvent.newBuilder() |
| 2408 | .setAtomId(atomTag) |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2409 | .writeInt(FrameworkStatsLog.CATEGORY_SIZE__CATEGORY__PHOTOS) |
Jeffrey Huang | e85d7ad | 2020-01-16 13:02:22 -0800 | [diff] [blame] | 2410 | .writeLong(json.optLong( |
| 2411 | DiskStatsFileLogger.PHOTOS_KEY, /* fallback */ -1L)) |
| 2412 | .writeLong(cacheTime) |
| 2413 | .build(); |
| 2414 | pulledData.add(e); |
| 2415 | |
| 2416 | e = StatsEvent.newBuilder() |
| 2417 | .setAtomId(atomTag) |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2418 | .writeInt(FrameworkStatsLog.CATEGORY_SIZE__CATEGORY__VIDEOS) |
Jeffrey Huang | e85d7ad | 2020-01-16 13:02:22 -0800 | [diff] [blame] | 2419 | .writeLong( |
| 2420 | json.optLong(DiskStatsFileLogger.VIDEOS_KEY, /* fallback */ -1L)) |
| 2421 | .writeLong(cacheTime) |
| 2422 | .build(); |
| 2423 | pulledData.add(e); |
| 2424 | |
| 2425 | e = StatsEvent.newBuilder() |
| 2426 | .setAtomId(atomTag) |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2427 | .writeInt(FrameworkStatsLog.CATEGORY_SIZE__CATEGORY__AUDIO) |
Jeffrey Huang | e85d7ad | 2020-01-16 13:02:22 -0800 | [diff] [blame] | 2428 | .writeLong(json.optLong( |
| 2429 | DiskStatsFileLogger.AUDIO_KEY, /* fallback */ -1L)) |
| 2430 | .writeLong(cacheTime) |
| 2431 | .build(); |
| 2432 | pulledData.add(e); |
| 2433 | |
| 2434 | e = StatsEvent.newBuilder() |
| 2435 | .setAtomId(atomTag) |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2436 | .writeInt(FrameworkStatsLog.CATEGORY_SIZE__CATEGORY__DOWNLOADS) |
Jeffrey Huang | e85d7ad | 2020-01-16 13:02:22 -0800 | [diff] [blame] | 2437 | .writeLong( |
| 2438 | json.optLong(DiskStatsFileLogger.DOWNLOADS_KEY, /* fallback */ -1L)) |
| 2439 | .writeLong(cacheTime) |
| 2440 | .build(); |
| 2441 | pulledData.add(e); |
| 2442 | |
| 2443 | e = StatsEvent.newBuilder() |
Tej Singh | 5dd1dc9 | 2020-01-29 20:44:42 -0800 | [diff] [blame] | 2444 | .setAtomId(atomTag) |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2445 | .writeInt(FrameworkStatsLog.CATEGORY_SIZE__CATEGORY__SYSTEM) |
Jeffrey Huang | e85d7ad | 2020-01-16 13:02:22 -0800 | [diff] [blame] | 2446 | .writeLong(json.optLong( |
| 2447 | DiskStatsFileLogger.SYSTEM_KEY, /* fallback */ -1L)) |
| 2448 | .writeLong(cacheTime) |
| 2449 | .build(); |
| 2450 | pulledData.add(e); |
| 2451 | |
| 2452 | e = StatsEvent.newBuilder() |
| 2453 | .setAtomId(atomTag) |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2454 | .writeInt(FrameworkStatsLog.CATEGORY_SIZE__CATEGORY__OTHER) |
Jeffrey Huang | e85d7ad | 2020-01-16 13:02:22 -0800 | [diff] [blame] | 2455 | .writeLong(json.optLong( |
| 2456 | DiskStatsFileLogger.MISC_KEY, /* fallback */ -1L)) |
| 2457 | .writeLong(cacheTime) |
| 2458 | .build(); |
| 2459 | pulledData.add(e); |
| 2460 | } catch (IOException | JSONException e) { |
Ruchir Rastogi | e965f4a | 2020-06-05 08:32:05 -0700 | [diff] [blame] | 2461 | Slog.w(TAG, "Unable to read diskstats cache file within pullCategorySize"); |
Jeffrey Huang | e85d7ad | 2020-01-16 13:02:22 -0800 | [diff] [blame] | 2462 | return StatsManager.PULL_SKIP; |
| 2463 | } |
| 2464 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2465 | } |
| 2466 | |
| 2467 | private void registerNumFingerprintsEnrolled() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2468 | int tagId = FrameworkStatsLog.NUM_FINGERPRINTS_ENROLLED; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 2469 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | 597a886 | 2020-01-16 14:56:37 -0800 | [diff] [blame] | 2470 | tagId, |
| 2471 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 2472 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 2473 | mStatsCallbackImpl |
Jeffrey Huang | 597a886 | 2020-01-16 14:56:37 -0800 | [diff] [blame] | 2474 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2475 | } |
| 2476 | |
| 2477 | private void registerNumFacesEnrolled() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2478 | int tagId = FrameworkStatsLog.NUM_FACES_ENROLLED; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 2479 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | 597a886 | 2020-01-16 14:56:37 -0800 | [diff] [blame] | 2480 | tagId, |
| 2481 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 2482 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 2483 | mStatsCallbackImpl |
Jeffrey Huang | 597a886 | 2020-01-16 14:56:37 -0800 | [diff] [blame] | 2484 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2485 | } |
| 2486 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2487 | private int pullNumBiometricsEnrolledLocked(int modality, int atomTag, |
| 2488 | List<StatsEvent> pulledData) { |
Jeffrey Huang | 597a886 | 2020-01-16 14:56:37 -0800 | [diff] [blame] | 2489 | final PackageManager pm = mContext.getPackageManager(); |
| 2490 | FingerprintManager fingerprintManager = null; |
| 2491 | FaceManager faceManager = null; |
| 2492 | |
| 2493 | if (pm.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) { |
| 2494 | fingerprintManager = mContext.getSystemService(FingerprintManager.class); |
| 2495 | } |
| 2496 | if (pm.hasSystemFeature(PackageManager.FEATURE_FACE)) { |
| 2497 | faceManager = mContext.getSystemService(FaceManager.class); |
| 2498 | } |
| 2499 | |
| 2500 | if (modality == BiometricsProtoEnums.MODALITY_FINGERPRINT && fingerprintManager == null) { |
| 2501 | return StatsManager.PULL_SKIP; |
| 2502 | } |
| 2503 | if (modality == BiometricsProtoEnums.MODALITY_FACE && faceManager == null) { |
| 2504 | return StatsManager.PULL_SKIP; |
| 2505 | } |
| 2506 | UserManager userManager = mContext.getSystemService(UserManager.class); |
| 2507 | if (userManager == null) { |
| 2508 | return StatsManager.PULL_SKIP; |
| 2509 | } |
| 2510 | |
| 2511 | final long token = Binder.clearCallingIdentity(); |
| 2512 | try { |
| 2513 | for (UserInfo user : userManager.getUsers()) { |
| 2514 | final int userId = user.getUserHandle().getIdentifier(); |
| 2515 | int numEnrolled = 0; |
| 2516 | if (modality == BiometricsProtoEnums.MODALITY_FINGERPRINT) { |
| 2517 | numEnrolled = fingerprintManager.getEnrolledFingerprints(userId).size(); |
| 2518 | } else if (modality == BiometricsProtoEnums.MODALITY_FACE) { |
| 2519 | numEnrolled = faceManager.getEnrolledFaces(userId).size(); |
| 2520 | } else { |
| 2521 | return StatsManager.PULL_SKIP; |
| 2522 | } |
| 2523 | StatsEvent e = StatsEvent.newBuilder() |
| 2524 | .setAtomId(atomTag) |
| 2525 | .writeInt(userId) |
| 2526 | .writeInt(numEnrolled) |
| 2527 | .build(); |
| 2528 | pulledData.add(e); |
| 2529 | } |
| 2530 | } finally { |
| 2531 | Binder.restoreCallingIdentity(token); |
| 2532 | } |
| 2533 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2534 | } |
| 2535 | |
Ruchir Rastogi | 92cf9a2 | 2020-01-17 15:55:39 -0800 | [diff] [blame] | 2536 | private void registerProcStats() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2537 | int tagId = FrameworkStatsLog.PROC_STATS; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 2538 | mStatsManager.setPullAtomCallback( |
Ruchir Rastogi | 92cf9a2 | 2020-01-17 15:55:39 -0800 | [diff] [blame] | 2539 | tagId, |
| 2540 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 2541 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 2542 | mStatsCallbackImpl |
Ruchir Rastogi | 92cf9a2 | 2020-01-17 15:55:39 -0800 | [diff] [blame] | 2543 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2544 | } |
| 2545 | |
| 2546 | private void registerProcStatsPkgProc() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2547 | int tagId = FrameworkStatsLog.PROC_STATS_PKG_PROC; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 2548 | mStatsManager.setPullAtomCallback( |
Ruchir Rastogi | 92cf9a2 | 2020-01-17 15:55:39 -0800 | [diff] [blame] | 2549 | tagId, |
| 2550 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 2551 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 2552 | mStatsCallbackImpl |
Ruchir Rastogi | 92cf9a2 | 2020-01-17 15:55:39 -0800 | [diff] [blame] | 2553 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2554 | } |
| 2555 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2556 | private int pullProcStatsLocked(int section, int atomTag, List<StatsEvent> pulledData) { |
Ruchir Rastogi | 92cf9a2 | 2020-01-17 15:55:39 -0800 | [diff] [blame] | 2557 | IProcessStats processStatsService = getIProcessStatsService(); |
| 2558 | if (processStatsService == null) { |
| 2559 | return StatsManager.PULL_SKIP; |
| 2560 | } |
| 2561 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2562 | final long token = Binder.clearCallingIdentity(); |
| 2563 | try { |
| 2564 | // force procstats to flush & combine old files into one store |
| 2565 | long lastHighWaterMark = readProcStatsHighWaterMark(section); |
| 2566 | List<ParcelFileDescriptor> statsFiles = new ArrayList<>(); |
Richard Gaywood | 863a721 | 2020-03-16 17:49:54 +0000 | [diff] [blame] | 2567 | |
Richard Gaywood | 52a2edf | 2020-06-14 17:39:15 +0100 | [diff] [blame] | 2568 | ProtoOutputStream[] protoStreams = new ProtoOutputStream[MAX_PROCSTATS_SHARDS]; |
| 2569 | for (int i = 0; i < protoStreams.length; i++) { |
| 2570 | protoStreams[i] = new ProtoOutputStream(); |
| 2571 | } |
| 2572 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2573 | ProcessStats procStats = new ProcessStats(false); |
Richard Gaywood | 52a2edf | 2020-06-14 17:39:15 +0100 | [diff] [blame] | 2574 | // Force processStatsService to aggregate all in-storage and in-memory data. |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2575 | long highWaterMark = processStatsService.getCommittedStatsMerged( |
| 2576 | lastHighWaterMark, section, true, statsFiles, procStats); |
Richard Gaywood | 52a2edf | 2020-06-14 17:39:15 +0100 | [diff] [blame] | 2577 | procStats.dumpAggregatedProtoForStatsd(protoStreams, MAX_PROCSTATS_RAW_SHARD_SIZE); |
Richard Gaywood | 863a721 | 2020-03-16 17:49:54 +0000 | [diff] [blame] | 2578 | |
Richard Gaywood | 52a2edf | 2020-06-14 17:39:15 +0100 | [diff] [blame] | 2579 | for (ProtoOutputStream proto : protoStreams) { |
| 2580 | if (proto.getBytes().length > 0) { |
| 2581 | StatsEvent e = StatsEvent.newBuilder() |
| 2582 | .setAtomId(atomTag) |
| 2583 | .writeByteArray(proto.getBytes()) |
| 2584 | .build(); |
| 2585 | pulledData.add(e); |
| 2586 | } |
| 2587 | } |
Richard Gaywood | 863a721 | 2020-03-16 17:49:54 +0000 | [diff] [blame] | 2588 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2589 | new File(mBaseDir.getAbsolutePath() + "/" + section + "_" + lastHighWaterMark) |
| 2590 | .delete(); |
| 2591 | new File(mBaseDir.getAbsolutePath() + "/" + section + "_" + highWaterMark) |
| 2592 | .createNewFile(); |
| 2593 | } catch (RemoteException | IOException e) { |
| 2594 | Slog.e(TAG, "Getting procstats failed: ", e); |
| 2595 | return StatsManager.PULL_SKIP; |
| 2596 | } finally { |
| 2597 | Binder.restoreCallingIdentity(token); |
Ruchir Rastogi | 92cf9a2 | 2020-01-17 15:55:39 -0800 | [diff] [blame] | 2598 | } |
Ruchir Rastogi | 92cf9a2 | 2020-01-17 15:55:39 -0800 | [diff] [blame] | 2599 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2600 | } |
| 2601 | |
Ruchir Rastogi | 92cf9a2 | 2020-01-17 15:55:39 -0800 | [diff] [blame] | 2602 | // read high watermark for section |
| 2603 | private long readProcStatsHighWaterMark(int section) { |
| 2604 | try { |
| 2605 | File[] files = mBaseDir.listFiles((d, name) -> { |
| 2606 | return name.toLowerCase().startsWith(String.valueOf(section) + '_'); |
| 2607 | }); |
| 2608 | if (files == null || files.length == 0) { |
| 2609 | return 0; |
| 2610 | } |
| 2611 | if (files.length > 1) { |
| 2612 | Slog.e(TAG, "Only 1 file expected for high water mark. Found " + files.length); |
| 2613 | } |
| 2614 | return Long.valueOf(files[0].getName().split("_")[1]); |
| 2615 | } catch (SecurityException e) { |
| 2616 | Slog.e(TAG, "Failed to get procstats high watermark file.", e); |
| 2617 | } catch (NumberFormatException e) { |
| 2618 | Slog.e(TAG, "Failed to parse file name.", e); |
| 2619 | } |
| 2620 | return 0; |
| 2621 | } |
| 2622 | |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2623 | private void registerDiskIO() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2624 | int tagId = FrameworkStatsLog.DISK_IO; |
Jeffrey Huang | a85d8f9 | 2020-01-24 13:28:45 -0800 | [diff] [blame] | 2625 | PullAtomMetadata metadata = new PullAtomMetadata.Builder() |
Jeffrey Huang | fdc5369 | 2020-01-14 15:17:30 -0800 | [diff] [blame] | 2626 | .setAdditiveFields(new int[] {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}) |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 2627 | .setCoolDownMillis(3 * MILLIS_PER_SEC) |
Jeffrey Huang | fdc5369 | 2020-01-14 15:17:30 -0800 | [diff] [blame] | 2628 | .build(); |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 2629 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | fdc5369 | 2020-01-14 15:17:30 -0800 | [diff] [blame] | 2630 | tagId, |
| 2631 | metadata, |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 2632 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 2633 | mStatsCallbackImpl |
Jeffrey Huang | fdc5369 | 2020-01-14 15:17:30 -0800 | [diff] [blame] | 2634 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2635 | } |
| 2636 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2637 | int pullDiskIOLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | fdc5369 | 2020-01-14 15:17:30 -0800 | [diff] [blame] | 2638 | mStoragedUidIoStatsReader.readAbsolute((uid, fgCharsRead, fgCharsWrite, fgBytesRead, |
| 2639 | fgBytesWrite, bgCharsRead, bgCharsWrite, bgBytesRead, bgBytesWrite, |
| 2640 | fgFsync, bgFsync) -> { |
| 2641 | StatsEvent e = StatsEvent.newBuilder() |
| 2642 | .setAtomId(atomTag) |
| 2643 | .writeInt(uid) |
Muhammad Qureshi | 22e52da | 2020-03-30 21:14:21 -0700 | [diff] [blame] | 2644 | .addBooleanAnnotation(ANNOTATION_ID_IS_UID, true) |
Jeffrey Huang | fdc5369 | 2020-01-14 15:17:30 -0800 | [diff] [blame] | 2645 | .writeLong(fgCharsRead) |
| 2646 | .writeLong(fgCharsWrite) |
| 2647 | .writeLong(fgBytesRead) |
| 2648 | .writeLong(fgBytesWrite) |
| 2649 | .writeLong(bgCharsRead) |
| 2650 | .writeLong(bgCharsWrite) |
| 2651 | .writeLong(bgBytesRead) |
| 2652 | .writeLong(bgBytesWrite) |
| 2653 | .writeLong(fgFsync) |
| 2654 | .writeLong(bgFsync) |
| 2655 | .build(); |
| 2656 | pulledData.add(e); |
| 2657 | }); |
| 2658 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2659 | } |
| 2660 | |
| 2661 | private void registerPowerProfile() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2662 | int tagId = FrameworkStatsLog.POWER_PROFILE; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 2663 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | 1a3935c | 2020-01-13 10:51:17 -0800 | [diff] [blame] | 2664 | tagId, |
| 2665 | /* PullAtomMetadata */ null, |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 2666 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 2667 | mStatsCallbackImpl |
Jeffrey Huang | 1a3935c | 2020-01-13 10:51:17 -0800 | [diff] [blame] | 2668 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2669 | } |
| 2670 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2671 | int pullPowerProfileLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | 1a3935c | 2020-01-13 10:51:17 -0800 | [diff] [blame] | 2672 | PowerProfile powerProfile = new PowerProfile(mContext); |
| 2673 | ProtoOutputStream proto = new ProtoOutputStream(); |
| 2674 | powerProfile.dumpDebug(proto); |
| 2675 | proto.flush(); |
| 2676 | StatsEvent e = StatsEvent.newBuilder() |
| 2677 | .setAtomId(atomTag) |
| 2678 | .writeByteArray(proto.getBytes()) |
| 2679 | .build(); |
| 2680 | pulledData.add(e); |
| 2681 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2682 | } |
| 2683 | |
| 2684 | private void registerProcessCpuTime() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2685 | int tagId = FrameworkStatsLog.PROCESS_CPU_TIME; |
Ruchir Rastogi | 92cf9a2 | 2020-01-17 15:55:39 -0800 | [diff] [blame] | 2686 | // Min cool-down is 5 sec, in line with what ActivityManagerService uses. |
Jeffrey Huang | a85d8f9 | 2020-01-24 13:28:45 -0800 | [diff] [blame] | 2687 | PullAtomMetadata metadata = new PullAtomMetadata.Builder() |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 2688 | .setCoolDownMillis(5 * MILLIS_PER_SEC) |
Jeffrey Huang | 7a9ae13 | 2020-01-14 15:44:58 -0800 | [diff] [blame] | 2689 | .build(); |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 2690 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | 7a9ae13 | 2020-01-14 15:44:58 -0800 | [diff] [blame] | 2691 | tagId, |
| 2692 | metadata, |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 2693 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 2694 | mStatsCallbackImpl |
Jeffrey Huang | 7a9ae13 | 2020-01-14 15:44:58 -0800 | [diff] [blame] | 2695 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2696 | } |
| 2697 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2698 | int pullProcessCpuTimeLocked(int atomTag, List<StatsEvent> pulledData) { |
| 2699 | if (mProcessCpuTracker == null) { |
| 2700 | mProcessCpuTracker = new ProcessCpuTracker(false); |
| 2701 | mProcessCpuTracker.init(); |
| 2702 | } |
| 2703 | mProcessCpuTracker.update(); |
| 2704 | for (int i = 0; i < mProcessCpuTracker.countStats(); i++) { |
| 2705 | ProcessCpuTracker.Stats st = mProcessCpuTracker.getStats(i); |
| 2706 | StatsEvent e = StatsEvent.newBuilder() |
| 2707 | .setAtomId(atomTag) |
| 2708 | .writeInt(st.uid) |
| 2709 | .addBooleanAnnotation(ANNOTATION_ID_IS_UID, true) |
| 2710 | .writeString(st.name) |
| 2711 | .writeLong(st.base_utime) |
| 2712 | .writeLong(st.base_stime) |
| 2713 | .build(); |
| 2714 | pulledData.add(e); |
Jeffrey Huang | 7a9ae13 | 2020-01-14 15:44:58 -0800 | [diff] [blame] | 2715 | } |
| 2716 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2717 | } |
| 2718 | |
| 2719 | private void registerCpuTimePerThreadFreq() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2720 | int tagId = FrameworkStatsLog.CPU_TIME_PER_THREAD_FREQ; |
Jeffrey Huang | a85d8f9 | 2020-01-24 13:28:45 -0800 | [diff] [blame] | 2721 | PullAtomMetadata metadata = new PullAtomMetadata.Builder() |
Jeffrey Huang | bf4eef8 | 2020-01-16 15:38:58 -0800 | [diff] [blame] | 2722 | .setAdditiveFields(new int[] {7, 9, 11, 13, 15, 17, 19, 21}) |
| 2723 | .build(); |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 2724 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | bf4eef8 | 2020-01-16 15:38:58 -0800 | [diff] [blame] | 2725 | tagId, |
| 2726 | metadata, |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 2727 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 2728 | mStatsCallbackImpl |
Jeffrey Huang | bf4eef8 | 2020-01-16 15:38:58 -0800 | [diff] [blame] | 2729 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2730 | } |
| 2731 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2732 | int pullCpuTimePerThreadFreqLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | bf4eef8 | 2020-01-16 15:38:58 -0800 | [diff] [blame] | 2733 | if (this.mKernelCpuThreadReader == null) { |
| 2734 | Slog.e(TAG, "mKernelCpuThreadReader is null"); |
| 2735 | return StatsManager.PULL_SKIP; |
| 2736 | } |
| 2737 | ArrayList<KernelCpuThreadReader.ProcessCpuUsage> processCpuUsages = |
| 2738 | this.mKernelCpuThreadReader.getProcessCpuUsageDiffed(); |
| 2739 | if (processCpuUsages == null) { |
| 2740 | Slog.e(TAG, "processCpuUsages is null"); |
| 2741 | return StatsManager.PULL_SKIP; |
| 2742 | } |
| 2743 | int[] cpuFrequencies = mKernelCpuThreadReader.getCpuFrequenciesKhz(); |
| 2744 | if (cpuFrequencies.length > CPU_TIME_PER_THREAD_FREQ_MAX_NUM_FREQUENCIES) { |
| 2745 | String message = "Expected maximum " + CPU_TIME_PER_THREAD_FREQ_MAX_NUM_FREQUENCIES |
| 2746 | + " frequencies, but got " + cpuFrequencies.length; |
| 2747 | Slog.w(TAG, message); |
| 2748 | return StatsManager.PULL_SKIP; |
| 2749 | } |
| 2750 | for (int i = 0; i < processCpuUsages.size(); i++) { |
| 2751 | KernelCpuThreadReader.ProcessCpuUsage processCpuUsage = processCpuUsages.get(i); |
| 2752 | ArrayList<KernelCpuThreadReader.ThreadCpuUsage> threadCpuUsages = |
| 2753 | processCpuUsage.threadCpuUsages; |
| 2754 | for (int j = 0; j < threadCpuUsages.size(); j++) { |
| 2755 | KernelCpuThreadReader.ThreadCpuUsage threadCpuUsage = threadCpuUsages.get(j); |
| 2756 | if (threadCpuUsage.usageTimesMillis.length != cpuFrequencies.length) { |
| 2757 | String message = "Unexpected number of usage times," |
| 2758 | + " expected " + cpuFrequencies.length |
| 2759 | + " but got " + threadCpuUsage.usageTimesMillis.length; |
| 2760 | Slog.w(TAG, message); |
| 2761 | return StatsManager.PULL_SKIP; |
| 2762 | } |
| 2763 | |
| 2764 | StatsEvent.Builder e = StatsEvent.newBuilder(); |
| 2765 | e.setAtomId(atomTag); |
| 2766 | e.writeInt(processCpuUsage.uid); |
Muhammad Qureshi | 22e52da | 2020-03-30 21:14:21 -0700 | [diff] [blame] | 2767 | e.addBooleanAnnotation(ANNOTATION_ID_IS_UID, true); |
Jeffrey Huang | bf4eef8 | 2020-01-16 15:38:58 -0800 | [diff] [blame] | 2768 | e.writeInt(processCpuUsage.processId); |
| 2769 | e.writeInt(threadCpuUsage.threadId); |
| 2770 | e.writeString(processCpuUsage.processName); |
| 2771 | e.writeString(threadCpuUsage.threadName); |
| 2772 | for (int k = 0; k < CPU_TIME_PER_THREAD_FREQ_MAX_NUM_FREQUENCIES; k++) { |
| 2773 | if (k < cpuFrequencies.length) { |
| 2774 | e.writeInt(cpuFrequencies[k]); |
| 2775 | e.writeInt(threadCpuUsage.usageTimesMillis[k]); |
| 2776 | } else { |
| 2777 | // If we have no more frequencies to write, we still must write empty data. |
| 2778 | // We know that this data is empty (and not just zero) because all |
| 2779 | // frequencies are expected to be greater than zero |
| 2780 | e.writeInt(0); |
| 2781 | e.writeInt(0); |
| 2782 | } |
| 2783 | } |
| 2784 | pulledData.add(e.build()); |
| 2785 | } |
| 2786 | } |
| 2787 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2788 | } |
| 2789 | |
Ruchir Rastogi | 5ea3163 | 2020-01-13 16:42:12 -0800 | [diff] [blame] | 2790 | private BatteryStatsHelper getBatteryStatsHelper() { |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2791 | synchronized (mBatteryStatsHelperLock) { |
| 2792 | if (mBatteryStatsHelper == null) { |
| 2793 | final long callingToken = Binder.clearCallingIdentity(); |
| 2794 | try { |
| 2795 | // clearCallingIdentity required for BatteryStatsHelper.checkWifiOnly(). |
| 2796 | mBatteryStatsHelper = new BatteryStatsHelper(mContext, false); |
| 2797 | } finally { |
| 2798 | Binder.restoreCallingIdentity(callingToken); |
| 2799 | } |
| 2800 | mBatteryStatsHelper.create((Bundle) null); |
Ruchir Rastogi | 5ea3163 | 2020-01-13 16:42:12 -0800 | [diff] [blame] | 2801 | } |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2802 | long currentTime = SystemClock.elapsedRealtime(); |
| 2803 | if (currentTime - mBatteryStatsHelperTimestampMs |
| 2804 | >= MAX_BATTERY_STATS_HELPER_FREQUENCY_MS) { |
| 2805 | // Load BatteryStats and do all the calculations. |
| 2806 | mBatteryStatsHelper.refreshStats(BatteryStats.STATS_SINCE_CHARGED, |
| 2807 | UserHandle.USER_ALL); |
| 2808 | // Calculations are done so we don't need to save the raw BatteryStats data in RAM. |
| 2809 | mBatteryStatsHelper.clearStats(); |
| 2810 | mBatteryStatsHelperTimestampMs = currentTime; |
| 2811 | } |
Ruchir Rastogi | 5ea3163 | 2020-01-13 16:42:12 -0800 | [diff] [blame] | 2812 | } |
| 2813 | return mBatteryStatsHelper; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2814 | } |
| 2815 | |
Ruchir Rastogi | 5ea3163 | 2020-01-13 16:42:12 -0800 | [diff] [blame] | 2816 | private long milliAmpHrsToNanoAmpSecs(double mAh) { |
| 2817 | return (long) (mAh * MILLI_AMP_HR_TO_NANO_AMP_SECS + 0.5); |
| 2818 | } |
| 2819 | |
| 2820 | private void registerDeviceCalculatedPowerUse() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2821 | int tagId = FrameworkStatsLog.DEVICE_CALCULATED_POWER_USE; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 2822 | mStatsManager.setPullAtomCallback( |
Ruchir Rastogi | 5ea3163 | 2020-01-13 16:42:12 -0800 | [diff] [blame] | 2823 | tagId, |
| 2824 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 2825 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 2826 | mStatsCallbackImpl |
Ruchir Rastogi | 5ea3163 | 2020-01-13 16:42:12 -0800 | [diff] [blame] | 2827 | ); |
| 2828 | } |
| 2829 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2830 | int pullDeviceCalculatedPowerUseLocked(int atomTag, List<StatsEvent> pulledData) { |
Ruchir Rastogi | 5ea3163 | 2020-01-13 16:42:12 -0800 | [diff] [blame] | 2831 | BatteryStatsHelper bsHelper = getBatteryStatsHelper(); |
| 2832 | StatsEvent e = StatsEvent.newBuilder() |
| 2833 | .setAtomId(atomTag) |
| 2834 | .writeLong(milliAmpHrsToNanoAmpSecs(bsHelper.getComputedPower())) |
| 2835 | .build(); |
| 2836 | pulledData.add(e); |
| 2837 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2838 | } |
| 2839 | |
| 2840 | private void registerDeviceCalculatedPowerBlameUid() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2841 | int tagId = FrameworkStatsLog.DEVICE_CALCULATED_POWER_BLAME_UID; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 2842 | mStatsManager.setPullAtomCallback( |
Ruchir Rastogi | 5ea3163 | 2020-01-13 16:42:12 -0800 | [diff] [blame] | 2843 | tagId, |
| 2844 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 2845 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 2846 | mStatsCallbackImpl |
Ruchir Rastogi | 5ea3163 | 2020-01-13 16:42:12 -0800 | [diff] [blame] | 2847 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2848 | } |
| 2849 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2850 | int pullDeviceCalculatedPowerBlameUidLocked(int atomTag, List<StatsEvent> pulledData) { |
Ruchir Rastogi | 5ea3163 | 2020-01-13 16:42:12 -0800 | [diff] [blame] | 2851 | final List<BatterySipper> sippers = getBatteryStatsHelper().getUsageList(); |
| 2852 | if (sippers == null) { |
| 2853 | return StatsManager.PULL_SKIP; |
| 2854 | } |
| 2855 | |
| 2856 | for (BatterySipper bs : sippers) { |
| 2857 | if (bs.drainType != bs.drainType.APP) { |
| 2858 | continue; |
| 2859 | } |
| 2860 | StatsEvent e = StatsEvent.newBuilder() |
| 2861 | .setAtomId(atomTag) |
| 2862 | .writeInt(bs.uidObj.getUid()) |
Muhammad Qureshi | 22e52da | 2020-03-30 21:14:21 -0700 | [diff] [blame] | 2863 | .addBooleanAnnotation(ANNOTATION_ID_IS_UID, true) |
Ruchir Rastogi | 5ea3163 | 2020-01-13 16:42:12 -0800 | [diff] [blame] | 2864 | .writeLong(milliAmpHrsToNanoAmpSecs(bs.totalPowerMah)) |
| 2865 | .build(); |
| 2866 | pulledData.add(e); |
| 2867 | } |
| 2868 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2869 | } |
| 2870 | |
| 2871 | private void registerDeviceCalculatedPowerBlameOther() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2872 | int tagId = FrameworkStatsLog.DEVICE_CALCULATED_POWER_BLAME_OTHER; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 2873 | mStatsManager.setPullAtomCallback( |
Ruchir Rastogi | 5ea3163 | 2020-01-13 16:42:12 -0800 | [diff] [blame] | 2874 | tagId, |
| 2875 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 2876 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 2877 | mStatsCallbackImpl |
Ruchir Rastogi | 5ea3163 | 2020-01-13 16:42:12 -0800 | [diff] [blame] | 2878 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2879 | } |
| 2880 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2881 | int pullDeviceCalculatedPowerBlameOtherLocked(int atomTag, List<StatsEvent> pulledData) { |
Ruchir Rastogi | 5ea3163 | 2020-01-13 16:42:12 -0800 | [diff] [blame] | 2882 | final List<BatterySipper> sippers = getBatteryStatsHelper().getUsageList(); |
| 2883 | if (sippers == null) { |
| 2884 | return StatsManager.PULL_SKIP; |
| 2885 | } |
| 2886 | |
| 2887 | for (BatterySipper bs : sippers) { |
| 2888 | if (bs.drainType == bs.drainType.APP) { |
| 2889 | continue; // This is a separate atom; see pullDeviceCalculatedPowerBlameUid(). |
| 2890 | } |
| 2891 | if (bs.drainType == bs.drainType.USER) { |
| 2892 | continue; // This is not supported. We purposefully calculate over USER_ALL. |
| 2893 | } |
| 2894 | StatsEvent e = StatsEvent.newBuilder() |
| 2895 | .setAtomId(atomTag) |
| 2896 | .writeInt(bs.drainType.ordinal()) |
| 2897 | .writeLong(milliAmpHrsToNanoAmpSecs(bs.totalPowerMah)) |
| 2898 | .build(); |
| 2899 | pulledData.add(e); |
| 2900 | } |
| 2901 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2902 | } |
| 2903 | |
| 2904 | private void registerDebugElapsedClock() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2905 | int tagId = FrameworkStatsLog.DEBUG_ELAPSED_CLOCK; |
Jeffrey Huang | a85d8f9 | 2020-01-24 13:28:45 -0800 | [diff] [blame] | 2906 | PullAtomMetadata metadata = new PullAtomMetadata.Builder() |
Ruchir Rastogi | d428dfb | 2020-01-14 16:51:55 -0800 | [diff] [blame] | 2907 | .setAdditiveFields(new int[] {1, 2, 3, 4}) |
| 2908 | .build(); |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 2909 | mStatsManager.setPullAtomCallback( |
Ruchir Rastogi | d428dfb | 2020-01-14 16:51:55 -0800 | [diff] [blame] | 2910 | tagId, |
| 2911 | metadata, |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 2912 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 2913 | mStatsCallbackImpl |
Ruchir Rastogi | d428dfb | 2020-01-14 16:51:55 -0800 | [diff] [blame] | 2914 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2915 | } |
| 2916 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2917 | int pullDebugElapsedClockLocked(int atomTag, List<StatsEvent> pulledData) { |
Ruchir Rastogi | d428dfb | 2020-01-14 16:51:55 -0800 | [diff] [blame] | 2918 | final long elapsedMillis = SystemClock.elapsedRealtime(); |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2919 | final long clockDiffMillis = mDebugElapsedClockPreviousValue == 0 |
| 2920 | ? 0 : elapsedMillis - mDebugElapsedClockPreviousValue; |
Ruchir Rastogi | d428dfb | 2020-01-14 16:51:55 -0800 | [diff] [blame] | 2921 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2922 | StatsEvent e = StatsEvent.newBuilder() |
| 2923 | .setAtomId(atomTag) |
| 2924 | .writeLong(mDebugElapsedClockPullCount) |
| 2925 | .writeLong(elapsedMillis) |
| 2926 | // Log it twice to be able to test multi-value aggregation from ValueMetric. |
| 2927 | .writeLong(elapsedMillis) |
| 2928 | .writeLong(clockDiffMillis) |
| 2929 | .writeInt(1 /* always set */) |
| 2930 | .build(); |
| 2931 | pulledData.add(e); |
Ruchir Rastogi | d428dfb | 2020-01-14 16:51:55 -0800 | [diff] [blame] | 2932 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2933 | if (mDebugElapsedClockPullCount % 2 == 1) { |
| 2934 | StatsEvent e2 = StatsEvent.newBuilder() |
Ruchir Rastogi | d428dfb | 2020-01-14 16:51:55 -0800 | [diff] [blame] | 2935 | .setAtomId(atomTag) |
| 2936 | .writeLong(mDebugElapsedClockPullCount) |
| 2937 | .writeLong(elapsedMillis) |
| 2938 | // Log it twice to be able to test multi-value aggregation from ValueMetric. |
| 2939 | .writeLong(elapsedMillis) |
| 2940 | .writeLong(clockDiffMillis) |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2941 | .writeInt(2 /* set on odd pulls */) |
Ruchir Rastogi | d428dfb | 2020-01-14 16:51:55 -0800 | [diff] [blame] | 2942 | .build(); |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2943 | pulledData.add(e2); |
Ruchir Rastogi | d428dfb | 2020-01-14 16:51:55 -0800 | [diff] [blame] | 2944 | } |
| 2945 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2946 | mDebugElapsedClockPullCount++; |
| 2947 | mDebugElapsedClockPreviousValue = elapsedMillis; |
Ruchir Rastogi | d428dfb | 2020-01-14 16:51:55 -0800 | [diff] [blame] | 2948 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2949 | } |
| 2950 | |
| 2951 | private void registerDebugFailingElapsedClock() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2952 | int tagId = FrameworkStatsLog.DEBUG_FAILING_ELAPSED_CLOCK; |
Jeffrey Huang | a85d8f9 | 2020-01-24 13:28:45 -0800 | [diff] [blame] | 2953 | PullAtomMetadata metadata = new PullAtomMetadata.Builder() |
Ruchir Rastogi | d428dfb | 2020-01-14 16:51:55 -0800 | [diff] [blame] | 2954 | .setAdditiveFields(new int[] {1, 2, 3, 4}) |
| 2955 | .build(); |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 2956 | mStatsManager.setPullAtomCallback( |
Ruchir Rastogi | d428dfb | 2020-01-14 16:51:55 -0800 | [diff] [blame] | 2957 | tagId, |
| 2958 | metadata, |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 2959 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 2960 | mStatsCallbackImpl |
Ruchir Rastogi | d428dfb | 2020-01-14 16:51:55 -0800 | [diff] [blame] | 2961 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2962 | } |
| 2963 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2964 | int pullDebugFailingElapsedClockLocked(int atomTag, List<StatsEvent> pulledData) { |
Ruchir Rastogi | d428dfb | 2020-01-14 16:51:55 -0800 | [diff] [blame] | 2965 | final long elapsedMillis = SystemClock.elapsedRealtime(); |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2966 | // Fails every 5 buckets. |
| 2967 | if (mDebugFailingElapsedClockPullCount++ % 5 == 0) { |
Ruchir Rastogi | d428dfb | 2020-01-14 16:51:55 -0800 | [diff] [blame] | 2968 | mDebugFailingElapsedClockPreviousValue = elapsedMillis; |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2969 | Slog.e(TAG, "Failing debug elapsed clock"); |
| 2970 | return StatsManager.PULL_SKIP; |
Ruchir Rastogi | d428dfb | 2020-01-14 16:51:55 -0800 | [diff] [blame] | 2971 | } |
| 2972 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2973 | StatsEvent e = StatsEvent.newBuilder() |
| 2974 | .setAtomId(atomTag) |
| 2975 | .writeLong(mDebugFailingElapsedClockPullCount) |
| 2976 | .writeLong(elapsedMillis) |
| 2977 | // Log it twice to be able to test multi-value aggregation from ValueMetric. |
| 2978 | .writeLong(elapsedMillis) |
| 2979 | .writeLong(mDebugFailingElapsedClockPreviousValue == 0 |
| 2980 | ? 0 : elapsedMillis - mDebugFailingElapsedClockPreviousValue) |
| 2981 | .build(); |
| 2982 | pulledData.add(e); |
| 2983 | |
| 2984 | mDebugFailingElapsedClockPreviousValue = elapsedMillis; |
Ruchir Rastogi | d428dfb | 2020-01-14 16:51:55 -0800 | [diff] [blame] | 2985 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2986 | } |
| 2987 | |
| 2988 | private void registerBuildInformation() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 2989 | int tagId = FrameworkStatsLog.BUILD_INFORMATION; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 2990 | mStatsManager.setPullAtomCallback( |
Ruchir Rastogi | 425d836 | 2020-01-14 23:51:46 -0800 | [diff] [blame] | 2991 | tagId, |
| 2992 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 2993 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 2994 | mStatsCallbackImpl |
Ruchir Rastogi | 425d836 | 2020-01-14 23:51:46 -0800 | [diff] [blame] | 2995 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 2996 | } |
| 2997 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 2998 | int pullBuildInformationLocked(int atomTag, List<StatsEvent> pulledData) { |
Ruchir Rastogi | 425d836 | 2020-01-14 23:51:46 -0800 | [diff] [blame] | 2999 | StatsEvent e = StatsEvent.newBuilder() |
| 3000 | .setAtomId(atomTag) |
| 3001 | .writeString(Build.FINGERPRINT) |
| 3002 | .writeString(Build.BRAND) |
| 3003 | .writeString(Build.PRODUCT) |
| 3004 | .writeString(Build.DEVICE) |
Dianne Hackborn | db00745 | 2019-10-28 16:34:22 -0700 | [diff] [blame] | 3005 | .writeString(Build.VERSION.RELEASE_OR_CODENAME) |
Ruchir Rastogi | 425d836 | 2020-01-14 23:51:46 -0800 | [diff] [blame] | 3006 | .writeString(Build.ID) |
| 3007 | .writeString(Build.VERSION.INCREMENTAL) |
| 3008 | .writeString(Build.TYPE) |
| 3009 | .writeString(Build.TAGS) |
| 3010 | .build(); |
| 3011 | pulledData.add(e); |
| 3012 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 3013 | } |
| 3014 | |
| 3015 | private void registerRoleHolder() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 3016 | int tagId = FrameworkStatsLog.ROLE_HOLDER; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 3017 | mStatsManager.setPullAtomCallback( |
Ruchir Rastogi | e4fd321 | 2020-01-15 00:39:12 -0800 | [diff] [blame] | 3018 | tagId, |
| 3019 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 3020 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 3021 | mStatsCallbackImpl |
Ruchir Rastogi | e4fd321 | 2020-01-15 00:39:12 -0800 | [diff] [blame] | 3022 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 3023 | } |
| 3024 | |
Ruchir Rastogi | e4fd321 | 2020-01-15 00:39:12 -0800 | [diff] [blame] | 3025 | // Add a RoleHolder atom for each package that holds a role. |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 3026 | int pullRoleHolderLocked(int atomTag, List<StatsEvent> pulledData) { |
Ruchir Rastogi | e4fd321 | 2020-01-15 00:39:12 -0800 | [diff] [blame] | 3027 | long callingToken = Binder.clearCallingIdentity(); |
| 3028 | try { |
| 3029 | PackageManager pm = mContext.getPackageManager(); |
| 3030 | RoleManagerInternal rmi = LocalServices.getService(RoleManagerInternal.class); |
| 3031 | |
| 3032 | List<UserInfo> users = mContext.getSystemService(UserManager.class).getUsers(); |
| 3033 | |
| 3034 | int numUsers = users.size(); |
| 3035 | for (int userNum = 0; userNum < numUsers; userNum++) { |
| 3036 | int userId = users.get(userNum).getUserHandle().getIdentifier(); |
| 3037 | |
| 3038 | ArrayMap<String, ArraySet<String>> roles = rmi.getRolesAndHolders(userId); |
| 3039 | |
| 3040 | int numRoles = roles.size(); |
| 3041 | for (int roleNum = 0; roleNum < numRoles; roleNum++) { |
| 3042 | String roleName = roles.keyAt(roleNum); |
| 3043 | ArraySet<String> holders = roles.valueAt(roleNum); |
| 3044 | |
| 3045 | int numHolders = holders.size(); |
| 3046 | for (int holderNum = 0; holderNum < numHolders; holderNum++) { |
| 3047 | String holderName = holders.valueAt(holderNum); |
| 3048 | |
| 3049 | PackageInfo pkg; |
| 3050 | try { |
| 3051 | pkg = pm.getPackageInfoAsUser(holderName, 0, userId); |
| 3052 | } catch (PackageManager.NameNotFoundException e) { |
| 3053 | Slog.w(TAG, "Role holder " + holderName + " not found"); |
| 3054 | return StatsManager.PULL_SKIP; |
| 3055 | } |
| 3056 | |
| 3057 | StatsEvent e = StatsEvent.newBuilder() |
| 3058 | .setAtomId(atomTag) |
| 3059 | .writeInt(pkg.applicationInfo.uid) |
Muhammad Qureshi | 22e52da | 2020-03-30 21:14:21 -0700 | [diff] [blame] | 3060 | .addBooleanAnnotation(ANNOTATION_ID_IS_UID, true) |
Ruchir Rastogi | e4fd321 | 2020-01-15 00:39:12 -0800 | [diff] [blame] | 3061 | .writeString(holderName) |
| 3062 | .writeString(roleName) |
| 3063 | .build(); |
| 3064 | pulledData.add(e); |
| 3065 | } |
| 3066 | } |
| 3067 | } |
| 3068 | } finally { |
| 3069 | Binder.restoreCallingIdentity(callingToken); |
| 3070 | } |
| 3071 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 3072 | } |
| 3073 | |
| 3074 | private void registerDangerousPermissionState() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 3075 | int tagId = FrameworkStatsLog.DANGEROUS_PERMISSION_STATE; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 3076 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | 7f9fe50 | 2020-01-16 16:47:22 -0800 | [diff] [blame] | 3077 | tagId, |
| 3078 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 3079 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 3080 | mStatsCallbackImpl |
Jeffrey Huang | 7f9fe50 | 2020-01-16 16:47:22 -0800 | [diff] [blame] | 3081 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 3082 | } |
| 3083 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 3084 | int pullDangerousPermissionStateLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | 7f9fe50 | 2020-01-16 16:47:22 -0800 | [diff] [blame] | 3085 | final long token = Binder.clearCallingIdentity(); |
Stanislav Zholnin | a093ae7 | 2020-04-18 19:14:17 +0100 | [diff] [blame] | 3086 | float samplingRate = DeviceConfig.getFloat(DeviceConfig.NAMESPACE_PERMISSIONS, |
| 3087 | DANGEROUS_PERMISSION_STATE_SAMPLE_RATE, 0.02f); |
Jeffrey Huang | 7f9fe50 | 2020-01-16 16:47:22 -0800 | [diff] [blame] | 3088 | Set<Integer> reportedUids = new HashSet<>(); |
| 3089 | try { |
| 3090 | PackageManager pm = mContext.getPackageManager(); |
| 3091 | |
| 3092 | List<UserInfo> users = mContext.getSystemService(UserManager.class).getUsers(); |
| 3093 | |
| 3094 | int numUsers = users.size(); |
| 3095 | for (int userNum = 0; userNum < numUsers; userNum++) { |
| 3096 | UserHandle user = users.get(userNum).getUserHandle(); |
| 3097 | |
| 3098 | List<PackageInfo> pkgs = pm.getInstalledPackagesAsUser( |
| 3099 | PackageManager.GET_PERMISSIONS, user.getIdentifier()); |
| 3100 | |
| 3101 | int numPkgs = pkgs.size(); |
| 3102 | for (int pkgNum = 0; pkgNum < numPkgs; pkgNum++) { |
| 3103 | PackageInfo pkg = pkgs.get(pkgNum); |
| 3104 | |
| 3105 | if (pkg.requestedPermissions == null) { |
| 3106 | continue; |
| 3107 | } |
| 3108 | |
| 3109 | if (reportedUids.contains(pkg.applicationInfo.uid)) { |
| 3110 | // do not report same uid twice |
| 3111 | continue; |
| 3112 | } |
| 3113 | reportedUids.add(pkg.applicationInfo.uid); |
| 3114 | |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 3115 | if (atomTag == FrameworkStatsLog.DANGEROUS_PERMISSION_STATE_SAMPLED |
Stanislav Zholnin | a093ae7 | 2020-04-18 19:14:17 +0100 | [diff] [blame] | 3116 | && ThreadLocalRandom.current().nextFloat() > samplingRate) { |
Jeffrey Huang | 7f9fe50 | 2020-01-16 16:47:22 -0800 | [diff] [blame] | 3117 | continue; |
| 3118 | } |
| 3119 | |
| 3120 | int numPerms = pkg.requestedPermissions.length; |
| 3121 | for (int permNum = 0; permNum < numPerms; permNum++) { |
| 3122 | String permName = pkg.requestedPermissions[permNum]; |
| 3123 | |
| 3124 | PermissionInfo permissionInfo; |
| 3125 | int permissionFlags = 0; |
| 3126 | try { |
| 3127 | permissionInfo = pm.getPermissionInfo(permName, 0); |
| 3128 | permissionFlags = |
| 3129 | pm.getPermissionFlags(permName, pkg.packageName, user); |
| 3130 | } catch (PackageManager.NameNotFoundException ignored) { |
| 3131 | continue; |
| 3132 | } |
| 3133 | |
| 3134 | if (permissionInfo.getProtection() != PROTECTION_DANGEROUS) { |
| 3135 | continue; |
| 3136 | } |
| 3137 | |
Stanislav Zholnin | dbcf85e | 2020-02-24 18:17:55 +0000 | [diff] [blame] | 3138 | if (permName.startsWith(COMMON_PERMISSION_PREFIX)) { |
| 3139 | permName = permName.substring(COMMON_PERMISSION_PREFIX.length()); |
| 3140 | } |
| 3141 | |
Jeffrey Huang | 7f9fe50 | 2020-01-16 16:47:22 -0800 | [diff] [blame] | 3142 | StatsEvent.Builder e = StatsEvent.newBuilder(); |
| 3143 | e.setAtomId(atomTag); |
| 3144 | e.writeString(permName); |
| 3145 | e.writeInt(pkg.applicationInfo.uid); |
Muhammad Qureshi | 22e52da | 2020-03-30 21:14:21 -0700 | [diff] [blame] | 3146 | e.addBooleanAnnotation(ANNOTATION_ID_IS_UID, true); |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 3147 | if (atomTag == FrameworkStatsLog.DANGEROUS_PERMISSION_STATE) { |
Jeffrey Huang | 7f9fe50 | 2020-01-16 16:47:22 -0800 | [diff] [blame] | 3148 | e.writeString(""); |
| 3149 | } |
| 3150 | e.writeBoolean((pkg.requestedPermissionsFlags[permNum] |
| 3151 | & REQUESTED_PERMISSION_GRANTED) != 0); |
| 3152 | e.writeInt(permissionFlags); |
| 3153 | |
| 3154 | pulledData.add(e.build()); |
| 3155 | } |
| 3156 | } |
| 3157 | } |
| 3158 | } catch (Throwable t) { |
| 3159 | Log.e(TAG, "Could not read permissions", t); |
| 3160 | return StatsManager.PULL_SKIP; |
| 3161 | } finally { |
| 3162 | Binder.restoreCallingIdentity(token); |
| 3163 | } |
| 3164 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 3165 | } |
| 3166 | |
| 3167 | private void registerTimeZoneDataInfo() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 3168 | int tagId = FrameworkStatsLog.TIME_ZONE_DATA_INFO; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 3169 | mStatsManager.setPullAtomCallback( |
Ruchir Rastogi | bd2c4ba | 2020-01-15 14:48:36 -0800 | [diff] [blame] | 3170 | tagId, |
| 3171 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 3172 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 3173 | mStatsCallbackImpl |
Ruchir Rastogi | bd2c4ba | 2020-01-15 14:48:36 -0800 | [diff] [blame] | 3174 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 3175 | } |
| 3176 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 3177 | int pullTimeZoneDataInfoLocked(int atomTag, List<StatsEvent> pulledData) { |
Ruchir Rastogi | bd2c4ba | 2020-01-15 14:48:36 -0800 | [diff] [blame] | 3178 | String tzDbVersion = "Unknown"; |
| 3179 | try { |
| 3180 | tzDbVersion = android.icu.util.TimeZone.getTZDataVersion(); |
| 3181 | } catch (MissingResourceException e) { |
| 3182 | Slog.e(TAG, "Getting tzdb version failed: ", e); |
| 3183 | return StatsManager.PULL_SKIP; |
| 3184 | } |
| 3185 | |
| 3186 | StatsEvent e = StatsEvent.newBuilder() |
| 3187 | .setAtomId(atomTag) |
| 3188 | .writeString(tzDbVersion) |
| 3189 | .build(); |
| 3190 | pulledData.add(e); |
| 3191 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 3192 | } |
| 3193 | |
| 3194 | private void registerExternalStorageInfo() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 3195 | int tagId = FrameworkStatsLog.EXTERNAL_STORAGE_INFO; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 3196 | mStatsManager.setPullAtomCallback( |
Ruchir Rastogi | 6fc44a8 | 2020-01-17 10:22:41 -0800 | [diff] [blame] | 3197 | tagId, |
| 3198 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 3199 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 3200 | mStatsCallbackImpl |
Ruchir Rastogi | 6fc44a8 | 2020-01-17 10:22:41 -0800 | [diff] [blame] | 3201 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 3202 | } |
| 3203 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 3204 | int pullExternalStorageInfoLocked(int atomTag, List<StatsEvent> pulledData) { |
Ruchir Rastogi | 6fc44a8 | 2020-01-17 10:22:41 -0800 | [diff] [blame] | 3205 | if (mStorageManager == null) { |
| 3206 | return StatsManager.PULL_SKIP; |
| 3207 | } |
| 3208 | |
| 3209 | List<VolumeInfo> volumes = mStorageManager.getVolumes(); |
| 3210 | for (VolumeInfo vol : volumes) { |
| 3211 | final String envState = VolumeInfo.getEnvironmentForState(vol.getState()); |
| 3212 | final DiskInfo diskInfo = vol.getDisk(); |
| 3213 | if (diskInfo != null && envState.equals(Environment.MEDIA_MOUNTED)) { |
| 3214 | // Get the type of the volume, if it is adoptable or portable. |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 3215 | int volumeType = FrameworkStatsLog.EXTERNAL_STORAGE_INFO__VOLUME_TYPE__OTHER; |
Ruchir Rastogi | 6fc44a8 | 2020-01-17 10:22:41 -0800 | [diff] [blame] | 3216 | if (vol.getType() == TYPE_PUBLIC) { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 3217 | volumeType = FrameworkStatsLog.EXTERNAL_STORAGE_INFO__VOLUME_TYPE__PUBLIC; |
Ruchir Rastogi | 6fc44a8 | 2020-01-17 10:22:41 -0800 | [diff] [blame] | 3218 | } else if (vol.getType() == TYPE_PRIVATE) { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 3219 | volumeType = FrameworkStatsLog.EXTERNAL_STORAGE_INFO__VOLUME_TYPE__PRIVATE; |
Ruchir Rastogi | 6fc44a8 | 2020-01-17 10:22:41 -0800 | [diff] [blame] | 3220 | } |
| 3221 | |
| 3222 | // Get the type of external storage inserted in the device (sd cards, usb, etc.) |
| 3223 | int externalStorageType; |
| 3224 | if (diskInfo.isSd()) { |
| 3225 | externalStorageType = StorageEnums.SD_CARD; |
| 3226 | } else if (diskInfo.isUsb()) { |
| 3227 | externalStorageType = StorageEnums.USB; |
| 3228 | } else { |
| 3229 | externalStorageType = StorageEnums.OTHER; |
| 3230 | } |
| 3231 | |
| 3232 | StatsEvent e = StatsEvent.newBuilder() |
| 3233 | .setAtomId(atomTag) |
| 3234 | .writeInt(externalStorageType) |
| 3235 | .writeInt(volumeType) |
| 3236 | .writeLong(diskInfo.size) |
| 3237 | .build(); |
| 3238 | pulledData.add(e); |
| 3239 | } |
| 3240 | } |
| 3241 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 3242 | } |
| 3243 | |
| 3244 | private void registerAppsOnExternalStorageInfo() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 3245 | int tagId = FrameworkStatsLog.APPS_ON_EXTERNAL_STORAGE_INFO; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 3246 | mStatsManager.setPullAtomCallback( |
Ruchir Rastogi | 6fc44a8 | 2020-01-17 10:22:41 -0800 | [diff] [blame] | 3247 | tagId, |
| 3248 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 3249 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 3250 | mStatsCallbackImpl |
Ruchir Rastogi | 6fc44a8 | 2020-01-17 10:22:41 -0800 | [diff] [blame] | 3251 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 3252 | } |
| 3253 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 3254 | int pullAppsOnExternalStorageInfoLocked(int atomTag, List<StatsEvent> pulledData) { |
Ruchir Rastogi | 6fc44a8 | 2020-01-17 10:22:41 -0800 | [diff] [blame] | 3255 | if (mStorageManager == null) { |
| 3256 | return StatsManager.PULL_SKIP; |
| 3257 | } |
| 3258 | |
| 3259 | PackageManager pm = mContext.getPackageManager(); |
| 3260 | List<ApplicationInfo> apps = pm.getInstalledApplications(/*flags=*/ 0); |
| 3261 | for (ApplicationInfo appInfo : apps) { |
| 3262 | UUID storageUuid = appInfo.storageUuid; |
| 3263 | if (storageUuid == null) { |
| 3264 | continue; |
| 3265 | } |
| 3266 | |
| 3267 | VolumeInfo volumeInfo = mStorageManager.findVolumeByUuid( |
| 3268 | appInfo.storageUuid.toString()); |
| 3269 | if (volumeInfo == null) { |
| 3270 | continue; |
| 3271 | } |
| 3272 | |
| 3273 | DiskInfo diskInfo = volumeInfo.getDisk(); |
| 3274 | if (diskInfo == null) { |
| 3275 | continue; |
| 3276 | } |
| 3277 | |
| 3278 | int externalStorageType = -1; |
| 3279 | if (diskInfo.isSd()) { |
| 3280 | externalStorageType = StorageEnums.SD_CARD; |
| 3281 | } else if (diskInfo.isUsb()) { |
| 3282 | externalStorageType = StorageEnums.USB; |
| 3283 | } else if (appInfo.isExternal()) { |
| 3284 | externalStorageType = StorageEnums.OTHER; |
| 3285 | } |
| 3286 | |
| 3287 | // App is installed on external storage. |
| 3288 | if (externalStorageType != -1) { |
| 3289 | StatsEvent e = StatsEvent.newBuilder() |
| 3290 | .setAtomId(atomTag) |
| 3291 | .writeInt(externalStorageType) |
| 3292 | .writeString(appInfo.packageName) |
| 3293 | .build(); |
| 3294 | pulledData.add(e); |
| 3295 | } |
| 3296 | } |
| 3297 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 3298 | } |
| 3299 | |
| 3300 | private void registerFaceSettings() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 3301 | int tagId = FrameworkStatsLog.FACE_SETTINGS; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 3302 | mStatsManager.setPullAtomCallback( |
Ruchir Rastogi | 329c767 | 2020-01-17 15:33:05 -0800 | [diff] [blame] | 3303 | tagId, |
| 3304 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 3305 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 3306 | mStatsCallbackImpl |
Ruchir Rastogi | 329c767 | 2020-01-17 15:33:05 -0800 | [diff] [blame] | 3307 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 3308 | } |
| 3309 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 3310 | int pullFaceSettingsLocked(int atomTag, List<StatsEvent> pulledData) { |
Ruchir Rastogi | 329c767 | 2020-01-17 15:33:05 -0800 | [diff] [blame] | 3311 | final long callingToken = Binder.clearCallingIdentity(); |
| 3312 | try { |
| 3313 | List<UserInfo> users = mContext.getSystemService(UserManager.class).getUsers(); |
| 3314 | int numUsers = users.size(); |
| 3315 | for (int userNum = 0; userNum < numUsers; userNum++) { |
| 3316 | int userId = users.get(userNum).getUserHandle().getIdentifier(); |
| 3317 | |
| 3318 | int unlockKeyguardEnabled = Settings.Secure.getIntForUser( |
| 3319 | mContext.getContentResolver(), |
| 3320 | Settings.Secure.FACE_UNLOCK_KEYGUARD_ENABLED, 1, userId); |
| 3321 | int unlockDismissesKeyguard = Settings.Secure.getIntForUser( |
| 3322 | mContext.getContentResolver(), |
| 3323 | Settings.Secure.FACE_UNLOCK_DISMISSES_KEYGUARD, 0, userId); |
| 3324 | int unlockAttentionRequired = Settings.Secure.getIntForUser( |
| 3325 | mContext.getContentResolver(), |
| 3326 | Settings.Secure.FACE_UNLOCK_ATTENTION_REQUIRED, 1, userId); |
| 3327 | int unlockAppEnabled = Settings.Secure.getIntForUser( |
| 3328 | mContext.getContentResolver(), |
| 3329 | Settings.Secure.FACE_UNLOCK_APP_ENABLED, 1, userId); |
| 3330 | int unlockAlwaysRequireConfirmation = Settings.Secure.getIntForUser( |
| 3331 | mContext.getContentResolver(), |
| 3332 | Settings.Secure.FACE_UNLOCK_ALWAYS_REQUIRE_CONFIRMATION, 0, userId); |
| 3333 | int unlockDiversityRequired = Settings.Secure.getIntForUser( |
| 3334 | mContext.getContentResolver(), |
| 3335 | Settings.Secure.FACE_UNLOCK_DIVERSITY_REQUIRED, 1, userId); |
| 3336 | |
| 3337 | StatsEvent e = StatsEvent.newBuilder() |
| 3338 | .setAtomId(atomTag) |
| 3339 | .writeBoolean(unlockKeyguardEnabled != 0) |
| 3340 | .writeBoolean(unlockDismissesKeyguard != 0) |
| 3341 | .writeBoolean(unlockAttentionRequired != 0) |
| 3342 | .writeBoolean(unlockAppEnabled != 0) |
| 3343 | .writeBoolean(unlockAlwaysRequireConfirmation != 0) |
| 3344 | .writeBoolean(unlockDiversityRequired != 0) |
| 3345 | .build(); |
| 3346 | pulledData.add(e); |
| 3347 | } |
| 3348 | } finally { |
| 3349 | Binder.restoreCallingIdentity(callingToken); |
| 3350 | } |
| 3351 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 3352 | } |
| 3353 | |
| 3354 | private void registerAppOps() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 3355 | int tagId = FrameworkStatsLog.APP_OPS; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 3356 | mStatsManager.setPullAtomCallback( |
Ruchir Rastogi | 97050e1c | 2020-01-15 17:25:00 -0800 | [diff] [blame] | 3357 | tagId, |
| 3358 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 3359 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 3360 | mStatsCallbackImpl |
Ruchir Rastogi | 97050e1c | 2020-01-15 17:25:00 -0800 | [diff] [blame] | 3361 | ); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 3362 | } |
| 3363 | |
Stanislav Zholnin | df3eeac | 2020-02-17 18:06:07 +0000 | [diff] [blame] | 3364 | private void registerRuntimeAppOpAccessMessage() { |
| 3365 | int tagId = FrameworkStatsLog.RUNTIME_APP_OP_ACCESS; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 3366 | mStatsManager.setPullAtomCallback( |
Stanislav Zholnin | df3eeac | 2020-02-17 18:06:07 +0000 | [diff] [blame] | 3367 | tagId, |
| 3368 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 3369 | DIRECT_EXECUTOR, |
Stanislav Zholnin | df3eeac | 2020-02-17 18:06:07 +0000 | [diff] [blame] | 3370 | mStatsCallbackImpl |
| 3371 | ); |
Stanislav Zholnin | df3eeac | 2020-02-17 18:06:07 +0000 | [diff] [blame] | 3372 | } |
| 3373 | |
Stanislav Zholnin | 841c9ab | 2020-05-24 04:56:18 +0100 | [diff] [blame] | 3374 | private class AppOpEntry { |
| 3375 | public final String mPackageName; |
| 3376 | public final String mAttributionTag; |
| 3377 | public final int mUid; |
| 3378 | public final HistoricalOp mOp; |
| 3379 | public final int mHash; |
| 3380 | |
| 3381 | AppOpEntry(String packageName, @Nullable String attributionTag, HistoricalOp op, int uid) { |
| 3382 | mPackageName = packageName; |
| 3383 | mAttributionTag = attributionTag; |
| 3384 | mUid = uid; |
| 3385 | mOp = op; |
| 3386 | mHash = ((op.getOpCode() * 961 |
| 3387 | + (attributionTag == null ? 0 : attributionTag.hashCode()) * 31 |
| 3388 | + packageName.hashCode() + RANDOM_SEED) & 0x7fffffff) % 100; |
| 3389 | } |
| 3390 | } |
| 3391 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 3392 | int pullAppOpsLocked(int atomTag, List<StatsEvent> pulledData) { |
Ruchir Rastogi | 92cf9a2 | 2020-01-17 15:55:39 -0800 | [diff] [blame] | 3393 | final long token = Binder.clearCallingIdentity(); |
Ruchir Rastogi | 97050e1c | 2020-01-15 17:25:00 -0800 | [diff] [blame] | 3394 | try { |
| 3395 | AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class); |
| 3396 | |
| 3397 | CompletableFuture<HistoricalOps> ops = new CompletableFuture<>(); |
Stanislav Zholnin | 050abc3 | 2020-02-17 14:14:52 +0000 | [diff] [blame] | 3398 | HistoricalOpsRequest histOpsRequest = new HistoricalOpsRequest.Builder(0, |
| 3399 | Long.MAX_VALUE).setFlags(OP_FLAGS_PULLED).build(); |
Stanislav Zholnin | 841c9ab | 2020-05-24 04:56:18 +0100 | [diff] [blame] | 3400 | appOps.getHistoricalOps(histOpsRequest, AsyncTask.THREAD_POOL_EXECUTOR, ops::complete); |
Ruchir Rastogi | 97050e1c | 2020-01-15 17:25:00 -0800 | [diff] [blame] | 3401 | HistoricalOps histOps = ops.get(EXTERNAL_STATS_SYNC_TIMEOUT_MILLIS, |
| 3402 | TimeUnit.MILLISECONDS); |
Stanislav Zholnin | 841c9ab | 2020-05-24 04:56:18 +0100 | [diff] [blame] | 3403 | |
| 3404 | List<AppOpEntry> opsList = processHistoricalOps(histOps, atomTag, 100); |
| 3405 | int samplingRate = sampleAppOps(pulledData, opsList, atomTag, 100); |
| 3406 | if (samplingRate != 100) { |
| 3407 | Slog.e(TAG, "Atom 10060 downsampled - too many dimensions"); |
| 3408 | } |
Ruchir Rastogi | 97050e1c | 2020-01-15 17:25:00 -0800 | [diff] [blame] | 3409 | } catch (Throwable t) { |
| 3410 | // TODO: catch exceptions at a more granular level |
| 3411 | Slog.e(TAG, "Could not read appops", t); |
| 3412 | return StatsManager.PULL_SKIP; |
| 3413 | } finally { |
| 3414 | Binder.restoreCallingIdentity(token); |
| 3415 | } |
| 3416 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 3417 | } |
| 3418 | |
Stanislav Zholnin | 841c9ab | 2020-05-24 04:56:18 +0100 | [diff] [blame] | 3419 | private int sampleAppOps(List<StatsEvent> pulledData, List<AppOpEntry> opsList, int atomTag, |
| 3420 | int samplingRate) { |
| 3421 | int nOps = opsList.size(); |
| 3422 | for (int i = 0; i < nOps; i++) { |
| 3423 | AppOpEntry entry = opsList.get(i); |
| 3424 | if (entry.mHash >= samplingRate) { |
| 3425 | continue; |
| 3426 | } |
| 3427 | StatsEvent.Builder e = StatsEvent.newBuilder(); |
| 3428 | e.setAtomId(atomTag); |
| 3429 | e.writeInt(entry.mUid); |
| 3430 | e.addBooleanAnnotation(ANNOTATION_ID_IS_UID, true); |
| 3431 | e.writeString(entry.mPackageName); |
| 3432 | if (atomTag == FrameworkStatsLog.ATTRIBUTED_APP_OPS) { |
| 3433 | e.writeString(entry.mAttributionTag); |
| 3434 | } |
| 3435 | e.writeInt(entry.mOp.getOpCode()); |
| 3436 | e.writeLong(entry.mOp.getForegroundAccessCount(OP_FLAGS_PULLED)); |
| 3437 | e.writeLong(entry.mOp.getBackgroundAccessCount(OP_FLAGS_PULLED)); |
| 3438 | e.writeLong(entry.mOp.getForegroundRejectCount(OP_FLAGS_PULLED)); |
| 3439 | e.writeLong(entry.mOp.getBackgroundRejectCount(OP_FLAGS_PULLED)); |
| 3440 | e.writeLong(entry.mOp.getForegroundAccessDuration(OP_FLAGS_PULLED)); |
| 3441 | e.writeLong(entry.mOp.getBackgroundAccessDuration(OP_FLAGS_PULLED)); |
| 3442 | e.writeBoolean(mDangerousAppOpsList.contains(entry.mOp.getOpCode())); |
| 3443 | |
| 3444 | if (atomTag == FrameworkStatsLog.ATTRIBUTED_APP_OPS) { |
| 3445 | e.writeInt(samplingRate); |
| 3446 | } |
| 3447 | pulledData.add(e.build()); |
| 3448 | } |
| 3449 | if (pulledData.size() > DIMENSION_KEY_SIZE_HARD_LIMIT) { |
| 3450 | int adjustedSamplingRate = constrain( |
| 3451 | samplingRate * DIMENSION_KEY_SIZE_SOFT_LIMIT / pulledData.size(), 0, |
| 3452 | samplingRate - 1); |
| 3453 | pulledData.clear(); |
| 3454 | return sampleAppOps(pulledData, opsList, atomTag, adjustedSamplingRate); |
| 3455 | } |
| 3456 | return samplingRate; |
| 3457 | } |
| 3458 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3459 | private void registerAttributedAppOps() { |
| 3460 | int tagId = FrameworkStatsLog.ATTRIBUTED_APP_OPS; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 3461 | mStatsManager.setPullAtomCallback( |
Stanislav Zholnin | 7dcc51d | 2020-01-29 16:53:24 +0000 | [diff] [blame] | 3462 | tagId, |
| 3463 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 3464 | DIRECT_EXECUTOR, |
Stanislav Zholnin | 7dcc51d | 2020-01-29 16:53:24 +0000 | [diff] [blame] | 3465 | mStatsCallbackImpl |
| 3466 | ); |
| 3467 | } |
| 3468 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 3469 | int pullAttributedAppOpsLocked(int atomTag, List<StatsEvent> pulledData) { |
Stanislav Zholnin | 7dcc51d | 2020-01-29 16:53:24 +0000 | [diff] [blame] | 3470 | final long token = Binder.clearCallingIdentity(); |
| 3471 | try { |
| 3472 | AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class); |
Stanislav Zholnin | 7dcc51d | 2020-01-29 16:53:24 +0000 | [diff] [blame] | 3473 | CompletableFuture<HistoricalOps> ops = new CompletableFuture<>(); |
| 3474 | HistoricalOpsRequest histOpsRequest = |
| 3475 | new HistoricalOpsRequest.Builder(0, Long.MAX_VALUE).setFlags( |
| 3476 | OP_FLAGS_PULLED).build(); |
Stanislav Zholnin | 841c9ab | 2020-05-24 04:56:18 +0100 | [diff] [blame] | 3477 | |
| 3478 | appOps.getHistoricalOps(histOpsRequest, AsyncTask.THREAD_POOL_EXECUTOR, ops::complete); |
Stanislav Zholnin | 7dcc51d | 2020-01-29 16:53:24 +0000 | [diff] [blame] | 3479 | HistoricalOps histOps = ops.get(EXTERNAL_STATS_SYNC_TIMEOUT_MILLIS, |
| 3480 | TimeUnit.MILLISECONDS); |
Stanislav Zholnin | 841c9ab | 2020-05-24 04:56:18 +0100 | [diff] [blame] | 3481 | |
| 3482 | synchronized (mAppOpsSamplingRateLock) { |
| 3483 | if (mAppOpsSamplingRate == 0) { |
| 3484 | mContext.getMainThreadHandler().postDelayed(new Runnable() { |
| 3485 | @Override |
| 3486 | public void run() { |
| 3487 | try { |
| 3488 | estimateAppOpsSamplingRate(); |
Stanislav Zholnin | 74b048a | 2020-05-31 13:17:16 +0100 | [diff] [blame] | 3489 | } catch (Throwable e) { |
| 3490 | Slog.e(TAG, "AppOps sampling ratio estimation failed: ", e); |
Stanislav Zholnin | 841c9ab | 2020-05-24 04:56:18 +0100 | [diff] [blame] | 3491 | synchronized (mAppOpsSamplingRateLock) { |
| 3492 | mAppOpsSamplingRate = min(mAppOpsSamplingRate, 10); |
| 3493 | } |
| 3494 | } |
| 3495 | } |
| 3496 | }, APP_OPS_SAMPLING_INITIALIZATION_DELAY_MILLIS); |
| 3497 | mAppOpsSamplingRate = 100; |
| 3498 | } |
Stanislav Zholnin | 7dcc51d | 2020-01-29 16:53:24 +0000 | [diff] [blame] | 3499 | } |
Stanislav Zholnin | 841c9ab | 2020-05-24 04:56:18 +0100 | [diff] [blame] | 3500 | |
| 3501 | List<AppOpEntry> opsList = |
| 3502 | processHistoricalOps(histOps, atomTag, mAppOpsSamplingRate); |
| 3503 | |
| 3504 | int newSamplingRate = sampleAppOps(pulledData, opsList, atomTag, mAppOpsSamplingRate); |
| 3505 | |
| 3506 | synchronized (mAppOpsSamplingRateLock) { |
| 3507 | mAppOpsSamplingRate = min(mAppOpsSamplingRate, newSamplingRate); |
| 3508 | } |
Stanislav Zholnin | 7dcc51d | 2020-01-29 16:53:24 +0000 | [diff] [blame] | 3509 | } catch (Throwable t) { |
| 3510 | // TODO: catch exceptions at a more granular level |
| 3511 | Slog.e(TAG, "Could not read appops", t); |
| 3512 | return StatsManager.PULL_SKIP; |
| 3513 | } finally { |
| 3514 | Binder.restoreCallingIdentity(token); |
| 3515 | } |
| 3516 | return StatsManager.PULL_SUCCESS; |
| 3517 | } |
| 3518 | |
Stanislav Zholnin | 841c9ab | 2020-05-24 04:56:18 +0100 | [diff] [blame] | 3519 | private void estimateAppOpsSamplingRate() throws Exception { |
| 3520 | int appOpsTargetCollectionSize = DeviceConfig.getInt( |
| 3521 | DeviceConfig.NAMESPACE_PERMISSIONS, APP_OPS_TARGET_COLLECTION_SIZE, |
| 3522 | APP_OPS_SIZE_ESTIMATE); |
Stanislav Zholnin | 5919913 | 2020-03-06 14:19:25 +0000 | [diff] [blame] | 3523 | AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class); |
| 3524 | |
| 3525 | CompletableFuture<HistoricalOps> ops = new CompletableFuture<>(); |
| 3526 | HistoricalOpsRequest histOpsRequest = |
| 3527 | new HistoricalOpsRequest.Builder( |
Hai Zhang | 4d0bf0f | 2020-06-08 16:42:53 -0700 | [diff] [blame] | 3528 | Math.max(Instant.now().minus(1, ChronoUnit.DAYS).toEpochMilli(), 0), |
Stanislav Zholnin | 5919913 | 2020-03-06 14:19:25 +0000 | [diff] [blame] | 3529 | Long.MAX_VALUE).setFlags( |
| 3530 | OP_FLAGS_PULLED).build(); |
Stanislav Zholnin | 74b048a | 2020-05-31 13:17:16 +0100 | [diff] [blame] | 3531 | appOps.getHistoricalOps(histOpsRequest, AsyncTask.THREAD_POOL_EXECUTOR, ops::complete); |
Stanislav Zholnin | 5919913 | 2020-03-06 14:19:25 +0000 | [diff] [blame] | 3532 | HistoricalOps histOps = ops.get(EXTERNAL_STATS_SYNC_TIMEOUT_MILLIS, |
| 3533 | TimeUnit.MILLISECONDS); |
Stanislav Zholnin | 841c9ab | 2020-05-24 04:56:18 +0100 | [diff] [blame] | 3534 | List<AppOpEntry> opsList = |
| 3535 | processHistoricalOps(histOps, FrameworkStatsLog.ATTRIBUTED_APP_OPS, 100); |
| 3536 | |
| 3537 | long estimatedSize = 0; |
| 3538 | int nOps = opsList.size(); |
| 3539 | for (int i = 0; i < nOps; i++) { |
| 3540 | AppOpEntry entry = opsList.get(i); |
| 3541 | estimatedSize += 32 + entry.mPackageName.length() + (entry.mAttributionTag == null ? 1 |
| 3542 | : entry.mAttributionTag.length()); |
| 3543 | |
| 3544 | } |
| 3545 | int estimatedSamplingRate = (int) constrain( |
| 3546 | appOpsTargetCollectionSize * 100 / estimatedSize, 0, 100); |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 3547 | synchronized (mAppOpsSamplingRateLock) { |
| 3548 | mAppOpsSamplingRate = min(mAppOpsSamplingRate, estimatedSamplingRate); |
| 3549 | } |
Stanislav Zholnin | 5919913 | 2020-03-06 14:19:25 +0000 | [diff] [blame] | 3550 | } |
| 3551 | |
Stanislav Zholnin | 841c9ab | 2020-05-24 04:56:18 +0100 | [diff] [blame] | 3552 | private List<AppOpEntry> processHistoricalOps( |
| 3553 | HistoricalOps histOps, int atomTag, int samplingRatio) { |
| 3554 | List<AppOpEntry> opsList = new ArrayList<>(); |
Stanislav Zholnin | 5919913 | 2020-03-06 14:19:25 +0000 | [diff] [blame] | 3555 | for (int uidIdx = 0; uidIdx < histOps.getUidCount(); uidIdx++) { |
| 3556 | final HistoricalUidOps uidOps = histOps.getUidOpsAt(uidIdx); |
| 3557 | final int uid = uidOps.getUid(); |
| 3558 | for (int pkgIdx = 0; pkgIdx < uidOps.getPackageCount(); pkgIdx++) { |
| 3559 | final HistoricalPackageOps packageOps = uidOps.getPackageOpsAt(pkgIdx); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3560 | if (atomTag == FrameworkStatsLog.ATTRIBUTED_APP_OPS) { |
| 3561 | for (int attributionIdx = 0; |
| 3562 | attributionIdx < packageOps.getAttributedOpsCount(); attributionIdx++) { |
| 3563 | final AppOpsManager.AttributedHistoricalOps attributedOps = |
| 3564 | packageOps.getAttributedOpsAt(attributionIdx); |
| 3565 | for (int opIdx = 0; opIdx < attributedOps.getOpCount(); opIdx++) { |
| 3566 | final AppOpsManager.HistoricalOp op = attributedOps.getOpAt(opIdx); |
Stanislav Zholnin | 841c9ab | 2020-05-24 04:56:18 +0100 | [diff] [blame] | 3567 | processHistoricalOp(op, opsList, uid, samplingRatio, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3568 | packageOps.getPackageName(), attributedOps.getTag()); |
Stanislav Zholnin | 5919913 | 2020-03-06 14:19:25 +0000 | [diff] [blame] | 3569 | } |
| 3570 | } |
| 3571 | } else if (atomTag == FrameworkStatsLog.APP_OPS) { |
| 3572 | for (int opIdx = 0; opIdx < packageOps.getOpCount(); opIdx++) { |
| 3573 | final AppOpsManager.HistoricalOp op = packageOps.getOpAt(opIdx); |
Stanislav Zholnin | 841c9ab | 2020-05-24 04:56:18 +0100 | [diff] [blame] | 3574 | processHistoricalOp(op, opsList, uid, samplingRatio, |
Stanislav Zholnin | 5919913 | 2020-03-06 14:19:25 +0000 | [diff] [blame] | 3575 | packageOps.getPackageName(), null); |
| 3576 | } |
| 3577 | } |
| 3578 | } |
| 3579 | } |
Stanislav Zholnin | 841c9ab | 2020-05-24 04:56:18 +0100 | [diff] [blame] | 3580 | return opsList; |
Stanislav Zholnin | 5919913 | 2020-03-06 14:19:25 +0000 | [diff] [blame] | 3581 | } |
| 3582 | |
Stanislav Zholnin | 841c9ab | 2020-05-24 04:56:18 +0100 | [diff] [blame] | 3583 | private void processHistoricalOp(AppOpsManager.HistoricalOp op, |
| 3584 | List<AppOpEntry> opsList, int uid, int samplingRatio, String packageName, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3585 | @Nullable String attributionTag) { |
Stanislav Zholnin | 044b179 | 2020-06-03 17:53:53 +0100 | [diff] [blame] | 3586 | int firstChar = 0; |
| 3587 | if (attributionTag != null && attributionTag.startsWith(packageName)) { |
| 3588 | firstChar = packageName.length(); |
| 3589 | if (firstChar < attributionTag.length() && attributionTag.charAt(firstChar) == '.') { |
| 3590 | firstChar++; |
| 3591 | } |
| 3592 | } |
| 3593 | AppOpEntry entry = new AppOpEntry(packageName, |
| 3594 | attributionTag == null ? null : attributionTag.substring(firstChar), op, |
| 3595 | uid); |
Stanislav Zholnin | 841c9ab | 2020-05-24 04:56:18 +0100 | [diff] [blame] | 3596 | if (entry.mHash < samplingRatio) { |
| 3597 | opsList.add(entry); |
Stanislav Zholnin | 5919913 | 2020-03-06 14:19:25 +0000 | [diff] [blame] | 3598 | } |
Stanislav Zholnin | 5919913 | 2020-03-06 14:19:25 +0000 | [diff] [blame] | 3599 | } |
| 3600 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 3601 | int pullRuntimeAppOpAccessMessageLocked(int atomTag, List<StatsEvent> pulledData) { |
Stanislav Zholnin | df3eeac | 2020-02-17 18:06:07 +0000 | [diff] [blame] | 3602 | final long token = Binder.clearCallingIdentity(); |
| 3603 | try { |
| 3604 | AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class); |
| 3605 | |
| 3606 | RuntimeAppOpAccessMessage message = appOps.collectRuntimeAppOpAccessMessage(); |
| 3607 | if (message == null) { |
| 3608 | Slog.i(TAG, "No runtime appop access message collected"); |
| 3609 | return StatsManager.PULL_SUCCESS; |
| 3610 | } |
| 3611 | |
| 3612 | StatsEvent.Builder e = StatsEvent.newBuilder(); |
| 3613 | e.setAtomId(atomTag); |
| 3614 | e.writeInt(message.getUid()); |
Muhammad Qureshi | 22e52da | 2020-03-30 21:14:21 -0700 | [diff] [blame] | 3615 | e.addBooleanAnnotation(ANNOTATION_ID_IS_UID, true); |
Stanislav Zholnin | df3eeac | 2020-02-17 18:06:07 +0000 | [diff] [blame] | 3616 | e.writeString(message.getPackageName()); |
Stanislav Zholnin | 3a2a6e4 | 2020-03-30 10:44:51 +0100 | [diff] [blame] | 3617 | e.writeString(""); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3618 | if (message.getAttributionTag() == null) { |
Stanislav Zholnin | df3eeac | 2020-02-17 18:06:07 +0000 | [diff] [blame] | 3619 | e.writeString(""); |
| 3620 | } else { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3621 | e.writeString(message.getAttributionTag()); |
Stanislav Zholnin | df3eeac | 2020-02-17 18:06:07 +0000 | [diff] [blame] | 3622 | } |
| 3623 | e.writeString(message.getMessage()); |
| 3624 | e.writeInt(message.getSamplingStrategy()); |
Stanislav Zholnin | 3a2a6e4 | 2020-03-30 10:44:51 +0100 | [diff] [blame] | 3625 | e.writeInt(AppOpsManager.strOpToOp(message.getOp())); |
Stanislav Zholnin | df3eeac | 2020-02-17 18:06:07 +0000 | [diff] [blame] | 3626 | |
| 3627 | pulledData.add(e.build()); |
| 3628 | } catch (Throwable t) { |
| 3629 | // TODO: catch exceptions at a more granular level |
| 3630 | Slog.e(TAG, "Could not read runtime appop access message", t); |
| 3631 | return StatsManager.PULL_SKIP; |
| 3632 | } finally { |
| 3633 | Binder.restoreCallingIdentity(token); |
| 3634 | } |
| 3635 | return StatsManager.PULL_SUCCESS; |
| 3636 | } |
| 3637 | |
Jeffrey Huang | 730bf96 | 2020-01-16 15:59:52 -0800 | [diff] [blame] | 3638 | static void unpackStreamedData(int atomTag, List<StatsEvent> pulledData, |
| 3639 | List<ParcelFileDescriptor> statsFiles) throws IOException { |
| 3640 | InputStream stream = new ParcelFileDescriptor.AutoCloseInputStream(statsFiles.get(0)); |
| 3641 | int[] len = new int[1]; |
| 3642 | byte[] stats = readFully(stream, len); |
| 3643 | StatsEvent e = StatsEvent.newBuilder() |
| 3644 | .setAtomId(atomTag) |
| 3645 | .writeByteArray(Arrays.copyOf(stats, len[0])) |
| 3646 | .build(); |
| 3647 | pulledData.add(e); |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 3648 | } |
| 3649 | |
Jeffrey Huang | 730bf96 | 2020-01-16 15:59:52 -0800 | [diff] [blame] | 3650 | static byte[] readFully(InputStream stream, int[] outLen) throws IOException { |
| 3651 | int pos = 0; |
| 3652 | final int initialAvail = stream.available(); |
| 3653 | byte[] data = new byte[initialAvail > 0 ? (initialAvail + 1) : 16384]; |
| 3654 | while (true) { |
| 3655 | int amt = stream.read(data, pos, data.length - pos); |
| 3656 | if (DEBUG) { |
| 3657 | Slog.i(TAG, "Read " + amt + " bytes at " + pos + " of avail " + data.length); |
| 3658 | } |
| 3659 | if (amt < 0) { |
| 3660 | if (DEBUG) { |
| 3661 | Slog.i(TAG, "**** FINISHED READING: pos=" + pos + " len=" + data.length); |
| 3662 | } |
| 3663 | outLen[0] = pos; |
| 3664 | return data; |
| 3665 | } |
| 3666 | pos += amt; |
| 3667 | if (pos >= data.length) { |
| 3668 | byte[] newData = new byte[pos + 16384]; |
| 3669 | if (DEBUG) { |
| 3670 | Slog.i(TAG, "Copying " + pos + " bytes to new array len " + newData.length); |
| 3671 | } |
| 3672 | System.arraycopy(data, 0, newData, 0, pos); |
| 3673 | data = newData; |
| 3674 | } |
| 3675 | } |
| 3676 | } |
| 3677 | |
| 3678 | private void registerNotificationRemoteViews() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 3679 | int tagId = FrameworkStatsLog.NOTIFICATION_REMOTE_VIEWS; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 3680 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | 730bf96 | 2020-01-16 15:59:52 -0800 | [diff] [blame] | 3681 | tagId, |
| 3682 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 3683 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 3684 | mStatsCallbackImpl |
Jeffrey Huang | 730bf96 | 2020-01-16 15:59:52 -0800 | [diff] [blame] | 3685 | ); |
| 3686 | } |
| 3687 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 3688 | int pullNotificationRemoteViewsLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | 730bf96 | 2020-01-16 15:59:52 -0800 | [diff] [blame] | 3689 | INotificationManager notificationManagerService = getINotificationManagerService(); |
| 3690 | if (notificationManagerService == null) { |
| 3691 | return StatsManager.PULL_SKIP; |
| 3692 | } |
| 3693 | final long callingToken = Binder.clearCallingIdentity(); |
| 3694 | try { |
Ruchir Rastogi | 92cf9a2 | 2020-01-17 15:55:39 -0800 | [diff] [blame] | 3695 | // determine last pull tine. Copy file trick from pullProcStats? |
Jeffrey Huang | 730bf96 | 2020-01-16 15:59:52 -0800 | [diff] [blame] | 3696 | long wallClockNanos = SystemClock.currentTimeMicro() * 1000L; |
| 3697 | long lastNotificationStatsNs = wallClockNanos - |
| 3698 | TimeUnit.NANOSECONDS.convert(1, TimeUnit.DAYS); |
| 3699 | |
| 3700 | List<ParcelFileDescriptor> statsFiles = new ArrayList<>(); |
| 3701 | notificationManagerService.pullStats(lastNotificationStatsNs, |
| 3702 | NotificationManagerService.REPORT_REMOTE_VIEWS, true, statsFiles); |
| 3703 | if (statsFiles.size() != 1) { |
| 3704 | return StatsManager.PULL_SKIP; |
| 3705 | } |
| 3706 | unpackStreamedData(atomTag, pulledData, statsFiles); |
| 3707 | } catch (IOException e) { |
| 3708 | Slog.e(TAG, "Getting notistats failed: ", e); |
| 3709 | return StatsManager.PULL_SKIP; |
| 3710 | } catch (RemoteException e) { |
| 3711 | Slog.e(TAG, "Getting notistats failed: ", e); |
| 3712 | return StatsManager.PULL_SKIP; |
| 3713 | } catch (SecurityException e) { |
| 3714 | Slog.e(TAG, "Getting notistats failed: ", e); |
| 3715 | return StatsManager.PULL_SKIP; |
| 3716 | } finally { |
| 3717 | Binder.restoreCallingIdentity(callingToken); |
| 3718 | } |
| 3719 | return StatsManager.PULL_SUCCESS; |
Jeffrey Huang | fdd3c7c | 2020-01-09 13:54:24 -0800 | [diff] [blame] | 3720 | } |
| 3721 | |
| 3722 | private void registerDangerousPermissionStateSampled() { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 3723 | int tagId = FrameworkStatsLog.DANGEROUS_PERMISSION_STATE_SAMPLED; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 3724 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | 7f9fe50 | 2020-01-16 16:47:22 -0800 | [diff] [blame] | 3725 | tagId, |
| 3726 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 3727 | DIRECT_EXECUTOR, |
Jeffrey Huang | 8fb5b83 | 2020-01-27 13:20:16 -0800 | [diff] [blame] | 3728 | mStatsCallbackImpl |
Jeffrey Huang | 7f9fe50 | 2020-01-16 16:47:22 -0800 | [diff] [blame] | 3729 | ); |
Tej Singh | 953ad86 | 2020-01-03 12:47:07 -0800 | [diff] [blame] | 3730 | } |
Jeffrey Huang | bf13083 | 2020-01-22 15:48:26 -0800 | [diff] [blame] | 3731 | |
Jeffrey Huang | 9a3c1b6 | 2020-01-27 17:33:13 -0800 | [diff] [blame] | 3732 | private void registerBatteryLevel() { |
| 3733 | int tagId = FrameworkStatsLog.BATTERY_LEVEL; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 3734 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | 9a3c1b6 | 2020-01-27 17:33:13 -0800 | [diff] [blame] | 3735 | tagId, |
| 3736 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 3737 | DIRECT_EXECUTOR, |
Jeffrey Huang | 9a3c1b6 | 2020-01-27 17:33:13 -0800 | [diff] [blame] | 3738 | mStatsCallbackImpl |
| 3739 | ); |
| 3740 | } |
| 3741 | |
Jeffrey Huang | 43f4d26 | 2020-01-30 14:03:31 -0800 | [diff] [blame] | 3742 | private void registerRemainingBatteryCapacity() { |
| 3743 | int tagId = FrameworkStatsLog.REMAINING_BATTERY_CAPACITY; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 3744 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | 43f4d26 | 2020-01-30 14:03:31 -0800 | [diff] [blame] | 3745 | tagId, |
| 3746 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 3747 | DIRECT_EXECUTOR, |
Jeffrey Huang | 43f4d26 | 2020-01-30 14:03:31 -0800 | [diff] [blame] | 3748 | mStatsCallbackImpl |
| 3749 | ); |
| 3750 | } |
| 3751 | |
| 3752 | private void registerFullBatteryCapacity() { |
| 3753 | int tagId = FrameworkStatsLog.FULL_BATTERY_CAPACITY; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 3754 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | 43f4d26 | 2020-01-30 14:03:31 -0800 | [diff] [blame] | 3755 | tagId, |
| 3756 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 3757 | DIRECT_EXECUTOR, |
Jeffrey Huang | 43f4d26 | 2020-01-30 14:03:31 -0800 | [diff] [blame] | 3758 | mStatsCallbackImpl |
| 3759 | ); |
| 3760 | } |
| 3761 | |
| 3762 | private void registerBatteryVoltage() { |
| 3763 | int tagId = FrameworkStatsLog.BATTERY_VOLTAGE; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 3764 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | 43f4d26 | 2020-01-30 14:03:31 -0800 | [diff] [blame] | 3765 | tagId, |
| 3766 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 3767 | DIRECT_EXECUTOR, |
Jeffrey Huang | 43f4d26 | 2020-01-30 14:03:31 -0800 | [diff] [blame] | 3768 | mStatsCallbackImpl |
| 3769 | ); |
| 3770 | } |
| 3771 | |
| 3772 | private void registerBatteryCycleCount() { |
| 3773 | int tagId = FrameworkStatsLog.BATTERY_CYCLE_COUNT; |
Tej Singh | 72a70a8 | 2020-02-26 23:46:29 -0800 | [diff] [blame] | 3774 | mStatsManager.setPullAtomCallback( |
Jeffrey Huang | 43f4d26 | 2020-01-30 14:03:31 -0800 | [diff] [blame] | 3775 | tagId, |
| 3776 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 3777 | DIRECT_EXECUTOR, |
Jeffrey Huang | 43f4d26 | 2020-01-30 14:03:31 -0800 | [diff] [blame] | 3778 | mStatsCallbackImpl |
| 3779 | ); |
| 3780 | } |
| 3781 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 3782 | int pullHealthHalLocked(int atomTag, List<StatsEvent> pulledData) { |
Jeffrey Huang | 9a3c1b6 | 2020-01-27 17:33:13 -0800 | [diff] [blame] | 3783 | IHealth healthService = mHealthService.getLastService(); |
| 3784 | if (healthService == null) { |
| 3785 | return StatsManager.PULL_SKIP; |
| 3786 | } |
| 3787 | try { |
| 3788 | healthService.getHealthInfo((result, value) -> { |
Jeffrey Huang | 43f4d26 | 2020-01-30 14:03:31 -0800 | [diff] [blame] | 3789 | int pulledValue; |
| 3790 | switch(atomTag) { |
| 3791 | case FrameworkStatsLog.BATTERY_LEVEL: |
| 3792 | pulledValue = value.legacy.batteryLevel; |
| 3793 | break; |
| 3794 | case FrameworkStatsLog.REMAINING_BATTERY_CAPACITY: |
| 3795 | pulledValue = value.legacy.batteryChargeCounter; |
| 3796 | break; |
| 3797 | case FrameworkStatsLog.FULL_BATTERY_CAPACITY: |
| 3798 | pulledValue = value.legacy.batteryFullCharge; |
| 3799 | break; |
| 3800 | case FrameworkStatsLog.BATTERY_VOLTAGE: |
| 3801 | pulledValue = value.legacy.batteryVoltage; |
| 3802 | break; |
| 3803 | case FrameworkStatsLog.BATTERY_CYCLE_COUNT: |
| 3804 | pulledValue = value.legacy.batteryCycleCount; |
| 3805 | break; |
| 3806 | default: |
| 3807 | throw new IllegalStateException("Invalid atomTag in healthHal puller: " |
| 3808 | + atomTag); |
| 3809 | } |
Jeffrey Huang | 9a3c1b6 | 2020-01-27 17:33:13 -0800 | [diff] [blame] | 3810 | StatsEvent e = StatsEvent.newBuilder() |
| 3811 | .setAtomId(atomTag) |
Jeffrey Huang | 43f4d26 | 2020-01-30 14:03:31 -0800 | [diff] [blame] | 3812 | .writeInt(pulledValue) |
Jeffrey Huang | 9a3c1b6 | 2020-01-27 17:33:13 -0800 | [diff] [blame] | 3813 | .build(); |
| 3814 | pulledData.add(e); |
| 3815 | }); |
Jeffrey Huang | 43f4d26 | 2020-01-30 14:03:31 -0800 | [diff] [blame] | 3816 | } catch (RemoteException | IllegalStateException e) { |
Jeffrey Huang | 9a3c1b6 | 2020-01-27 17:33:13 -0800 | [diff] [blame] | 3817 | return StatsManager.PULL_SKIP; |
| 3818 | } |
| 3819 | return StatsManager.PULL_SUCCESS; |
| 3820 | } |
Jeffrey Huang | bf13083 | 2020-01-22 15:48:26 -0800 | [diff] [blame] | 3821 | |
Raff Tsai | 87cefd4 | 2020-04-07 14:25:02 +0800 | [diff] [blame] | 3822 | private void registerSettingsStats() { |
| 3823 | int tagId = FrameworkStatsLog.SETTING_SNAPSHOT; |
| 3824 | mStatsManager.setPullAtomCallback( |
| 3825 | tagId, |
| 3826 | null, // use default PullAtomMetadata values |
Tej Singh | e19799f | 2020-05-20 19:14:26 -0700 | [diff] [blame] | 3827 | DIRECT_EXECUTOR, |
Raff Tsai | 87cefd4 | 2020-04-07 14:25:02 +0800 | [diff] [blame] | 3828 | mStatsCallbackImpl |
| 3829 | ); |
| 3830 | } |
| 3831 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 3832 | int pullSettingsStatsLocked(int atomTag, List<StatsEvent> pulledData) { |
Raff Tsai | 87cefd4 | 2020-04-07 14:25:02 +0800 | [diff] [blame] | 3833 | UserManager userManager = mContext.getSystemService(UserManager.class); |
| 3834 | if (userManager == null) { |
| 3835 | return StatsManager.PULL_SKIP; |
| 3836 | } |
| 3837 | |
| 3838 | final long token = Binder.clearCallingIdentity(); |
| 3839 | try { |
| 3840 | for (UserInfo user : userManager.getUsers()) { |
| 3841 | final int userId = user.getUserHandle().getIdentifier(); |
| 3842 | |
| 3843 | if (userId == UserHandle.USER_SYSTEM) { |
| 3844 | pulledData.addAll(SettingsStatsUtil.logGlobalSettings(mContext, atomTag, |
| 3845 | UserHandle.USER_SYSTEM)); |
| 3846 | } |
| 3847 | pulledData.addAll(SettingsStatsUtil.logSystemSettings(mContext, atomTag, userId)); |
| 3848 | pulledData.addAll(SettingsStatsUtil.logSecureSettings(mContext, atomTag, userId)); |
| 3849 | } |
| 3850 | } catch (Exception e) { |
| 3851 | Slog.e(TAG, "failed to pullSettingsStats", e); |
| 3852 | return StatsManager.PULL_SKIP; |
| 3853 | } finally { |
| 3854 | Binder.restoreCallingIdentity(token); |
| 3855 | } |
| 3856 | return StatsManager.PULL_SUCCESS; |
| 3857 | } |
| 3858 | |
Jeffrey Huang | bf13083 | 2020-01-22 15:48:26 -0800 | [diff] [blame] | 3859 | // Thermal event received from vendor thermal management subsystem |
| 3860 | private static final class ThermalEventListener extends IThermalEventListener.Stub { |
| 3861 | @Override |
| 3862 | public void notifyThrottling(Temperature temp) { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 3863 | FrameworkStatsLog.write(FrameworkStatsLog.THERMAL_THROTTLING_SEVERITY_STATE_CHANGED, |
| 3864 | temp.getType(), temp.getName(), (int) (temp.getValue() * 10), temp.getStatus()); |
Jeffrey Huang | bf13083 | 2020-01-22 15:48:26 -0800 | [diff] [blame] | 3865 | } |
| 3866 | } |
| 3867 | |
| 3868 | private static final class ConnectivityStatsCallback extends |
| 3869 | ConnectivityManager.NetworkCallback { |
| 3870 | @Override |
| 3871 | public void onAvailable(Network network) { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 3872 | FrameworkStatsLog.write(FrameworkStatsLog.CONNECTIVITY_STATE_CHANGED, network.netId, |
| 3873 | FrameworkStatsLog.CONNECTIVITY_STATE_CHANGED__STATE__CONNECTED); |
Jeffrey Huang | bf13083 | 2020-01-22 15:48:26 -0800 | [diff] [blame] | 3874 | } |
| 3875 | |
| 3876 | @Override |
| 3877 | public void onLost(Network network) { |
Muhammad Qureshi | 4e4c44f | 2020-01-27 17:18:52 -0800 | [diff] [blame] | 3878 | FrameworkStatsLog.write(FrameworkStatsLog.CONNECTIVITY_STATE_CHANGED, network.netId, |
| 3879 | FrameworkStatsLog.CONNECTIVITY_STATE_CHANGED__STATE__DISCONNECTED); |
Jeffrey Huang | bf13083 | 2020-01-22 15:48:26 -0800 | [diff] [blame] | 3880 | } |
| 3881 | } |
junyulai | aa31d3c | 2020-04-10 15:11:54 +0800 | [diff] [blame] | 3882 | |
| 3883 | private final class StatsSubscriptionsListener |
| 3884 | extends SubscriptionManager.OnSubscriptionsChangedListener { |
| 3885 | @NonNull |
| 3886 | private final SubscriptionManager mSm; |
| 3887 | |
| 3888 | StatsSubscriptionsListener(@NonNull SubscriptionManager sm) { |
| 3889 | mSm = sm; |
| 3890 | } |
| 3891 | |
| 3892 | @Override |
| 3893 | public void onSubscriptionsChanged() { |
| 3894 | final List<SubscriptionInfo> currentSubs = mSm.getCompleteActiveSubscriptionInfoList(); |
| 3895 | for (final SubscriptionInfo sub : currentSubs) { |
| 3896 | final SubInfo match = CollectionUtils.find(mHistoricalSubs, |
| 3897 | (SubInfo it) -> it.subId == sub.getSubscriptionId()); |
| 3898 | // SubInfo exists, ignore. |
| 3899 | if (match != null) continue; |
| 3900 | |
| 3901 | // Ignore if no valid mcc, mnc, imsi, carrierId. |
| 3902 | final int subId = sub.getSubscriptionId(); |
| 3903 | final String mcc = sub.getMccString(); |
| 3904 | final String mnc = sub.getMncString(); |
| 3905 | final String subscriberId = mTelephony.getSubscriberId(subId); |
| 3906 | if (TextUtils.isEmpty(subscriberId) || TextUtils.isEmpty(mcc) |
| 3907 | || TextUtils.isEmpty(mnc) || sub.getCarrierId() == UNKNOWN_CARRIER_ID) { |
| 3908 | Slog.e(TAG, "subInfo of subId " + subId + " is invalid, ignored."); |
| 3909 | continue; |
| 3910 | } |
| 3911 | |
| 3912 | final SubInfo subInfo = new SubInfo(subId, sub.getCarrierId(), mcc, mnc, |
| 3913 | subscriberId, sub.isOpportunistic()); |
| 3914 | Slog.i(TAG, "subId " + subId + " added into historical sub list"); |
junyulai | aa31d3c | 2020-04-10 15:11:54 +0800 | [diff] [blame] | 3915 | |
Ruchir Rastogi | bba70a6 | 2020-06-01 10:43:31 -0700 | [diff] [blame] | 3916 | synchronized (mDataBytesTransferLock) { |
| 3917 | mHistoricalSubs.add(subInfo); |
| 3918 | // Since getting snapshot when pulling will also include data before boot, |
| 3919 | // query stats as baseline to prevent double count is needed. |
| 3920 | mNetworkStatsBaselines.addAll(getDataUsageBytesTransferSnapshotForSub(subInfo)); |
| 3921 | } |
junyulai | aa31d3c | 2020-04-10 15:11:54 +0800 | [diff] [blame] | 3922 | } |
| 3923 | } |
| 3924 | } |
| 3925 | |
Tej Singh | 953ad86 | 2020-01-03 12:47:07 -0800 | [diff] [blame] | 3926 | } |