blob: ca73059d8b8ed4e7a59c7c1e66f103bccc5fc278 [file] [log] [blame]
Chenjie Yu1a317ba2017-10-05 16:05:32 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Chenjie Yu80f91122018-01-31 20:24:50 -080017#define DEBUG false
Joe Onorato9fc9edf2017-10-15 20:08:52 -070018#include "Log.h"
Chenjie Yu1a317ba2017-10-05 16:05:32 -070019
Chenjie Yu1a317ba2017-10-05 16:05:32 -070020#include <android/os/IStatsCompanionService.h>
Tej Singha0c89dd2019-01-25 16:39:18 -080021#include <android/os/IStatsPullerCallback.h>
David Chen1481fe12017-10-16 13:16:34 -070022#include <cutils/log.h>
Chenjie Yu1a0a9412018-03-28 10:07:22 -070023#include <math.h>
Chenjie Yu3b3adcd2018-04-18 16:25:36 -070024#include <stdint.h>
David Chen1481fe12017-10-16 13:16:34 -070025#include <algorithm>
Chenjie Yu1a0a9412018-03-28 10:07:22 -070026#include "../StatsService.h"
Chenjie Yuaa5b2012018-03-21 13:53:15 -070027#include "../logd/LogEvent.h"
28#include "../stats_log_util.h"
29#include "../statscompanion_util.h"
Yiwei Zhang7e633032019-03-01 17:25:27 -080030#include "GpuStatsPuller.h"
Bookatz92da2832018-11-01 18:10:03 -070031#include "PowerStatsPuller.h"
Tej Singh40298312018-02-16 00:15:09 -080032#include "ResourceHealthManagerPuller.h"
Tej Singha0c89dd2019-01-25 16:39:18 -080033#include "StatsCallbackPuller.h"
Chenjie Yu97dbb202019-02-13 16:42:04 -080034#include "StatsCompanionServicePuller.h"
Chenjie Yue2219202018-06-08 10:07:51 -070035#include "StatsPullerManager.h"
Tej Singhbf972d92018-01-10 20:51:13 -080036#include "SubsystemSleepStatePuller.h"
Chenjie Yu97dbb202019-02-13 16:42:04 -080037#include "TrainInfoPuller.h"
Chenjie Yu5305e1d2017-10-31 13:49:36 -070038#include "statslog.h"
David Chen1481fe12017-10-16 13:16:34 -070039
40#include <iostream>
Chenjie Yu1a317ba2017-10-05 16:05:32 -070041
Yao Chen93fe3a32017-11-02 13:52:59 -070042using std::make_shared;
Chenjie Yu5305e1d2017-10-31 13:49:36 -070043using std::map;
Yao Chen93fe3a32017-11-02 13:52:59 -070044using std::shared_ptr;
Chenjie Yub3dda412017-10-24 13:41:59 -070045using std::string;
46using std::vector;
Chenjie Yu6736c892017-11-09 10:50:09 -080047using std::list;
Chenjie Yu1a317ba2017-10-05 16:05:32 -070048
49namespace android {
50namespace os {
51namespace statsd {
52
Chenjie Yu3b3adcd2018-04-18 16:25:36 -070053// Values smaller than this may require to update the alarm.
54const int64_t NO_ALARM_UPDATE = INT64_MAX;
55
Tej Singha0c89dd2019-01-25 16:39:18 -080056std::map<int, PullAtomInfo> StatsPullerManager::kAllPullAtomInfo = {
Chenjie Yufeba3092018-02-08 14:33:37 -080057 // wifi_bytes_transfer
58 {android::util::WIFI_BYTES_TRANSFER,
Chenjie Yu0bd73db2018-12-16 07:37:04 -080059 {.additiveFields = {2, 3, 4, 5},
60 .puller = new StatsCompanionServicePuller(android::util::WIFI_BYTES_TRANSFER)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080061 // wifi_bytes_transfer_by_fg_bg
62 {android::util::WIFI_BYTES_TRANSFER_BY_FG_BG,
Chenjie Yu0bd73db2018-12-16 07:37:04 -080063 {.additiveFields = {3, 4, 5, 6},
64 .puller = new StatsCompanionServicePuller(android::util::WIFI_BYTES_TRANSFER_BY_FG_BG)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080065 // mobile_bytes_transfer
66 {android::util::MOBILE_BYTES_TRANSFER,
Chenjie Yu0bd73db2018-12-16 07:37:04 -080067 {.additiveFields = {2, 3, 4, 5},
68 .puller = new StatsCompanionServicePuller(android::util::MOBILE_BYTES_TRANSFER)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080069 // mobile_bytes_transfer_by_fg_bg
70 {android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG,
Chenjie Yu0bd73db2018-12-16 07:37:04 -080071 {.additiveFields = {3, 4, 5, 6},
72 .puller =
73 new StatsCompanionServicePuller(android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080074 // bluetooth_bytes_transfer
75 {android::util::BLUETOOTH_BYTES_TRANSFER,
Chenjie Yu0bd73db2018-12-16 07:37:04 -080076 {.additiveFields = {2, 3},
77 .puller = new StatsCompanionServicePuller(android::util::BLUETOOTH_BYTES_TRANSFER)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080078 // kernel_wakelock
79 {android::util::KERNEL_WAKELOCK,
Chenjie Yu0bd73db2018-12-16 07:37:04 -080080 {.puller = new StatsCompanionServicePuller(android::util::KERNEL_WAKELOCK)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080081 // subsystem_sleep_state
Chenjie Yu0bd73db2018-12-16 07:37:04 -080082 {android::util::SUBSYSTEM_SLEEP_STATE, {.puller = new SubsystemSleepStatePuller()}},
Bookatz92da2832018-11-01 18:10:03 -070083 // on_device_power_measurement
Chenjie Yu0bd73db2018-12-16 07:37:04 -080084 {android::util::ON_DEVICE_POWER_MEASUREMENT, {.puller = new PowerStatsPuller()}},
Chenjie Yufeba3092018-02-08 14:33:37 -080085 // cpu_time_per_freq
86 {android::util::CPU_TIME_PER_FREQ,
Chenjie Yu0bd73db2018-12-16 07:37:04 -080087 {.additiveFields = {3},
88 .puller = new StatsCompanionServicePuller(android::util::CPU_TIME_PER_FREQ)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080089 // cpu_time_per_uid
Chenjie Yuec676612018-03-07 09:19:17 -080090 {android::util::CPU_TIME_PER_UID,
Chenjie Yu0bd73db2018-12-16 07:37:04 -080091 {.additiveFields = {2, 3},
92 .puller = new StatsCompanionServicePuller(android::util::CPU_TIME_PER_UID)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080093 // cpu_time_per_uid_freq
Chenjie Yu1a0a9412018-03-28 10:07:22 -070094 // the throttling is 3sec, handled in
95 // frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader
Chenjie Yuec676612018-03-07 09:19:17 -080096 {android::util::CPU_TIME_PER_UID_FREQ,
Chenjie Yu0bd73db2018-12-16 07:37:04 -080097 {.additiveFields = {4},
98 .puller = new StatsCompanionServicePuller(android::util::CPU_TIME_PER_UID_FREQ)}},
Chenjie Yuec676612018-03-07 09:19:17 -080099 // cpu_active_time
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700100 // the throttling is 3sec, handled in
101 // frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader
Chenjie Yuec676612018-03-07 09:19:17 -0800102 {android::util::CPU_ACTIVE_TIME,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800103 {.additiveFields = {2},
104 .puller = new StatsCompanionServicePuller(android::util::CPU_ACTIVE_TIME)}},
Chenjie Yuec676612018-03-07 09:19:17 -0800105 // cpu_cluster_time
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700106 // the throttling is 3sec, handled in
107 // frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader
Chenjie Yuec676612018-03-07 09:19:17 -0800108 {android::util::CPU_CLUSTER_TIME,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800109 {.additiveFields = {3},
110 .puller = new StatsCompanionServicePuller(android::util::CPU_CLUSTER_TIME)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800111 // wifi_activity_energy_info
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800112 {android::util::WIFI_ACTIVITY_INFO,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800113 {.puller = new StatsCompanionServicePuller(android::util::WIFI_ACTIVITY_INFO)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800114 // modem_activity_info
115 {android::util::MODEM_ACTIVITY_INFO,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800116 {.puller = new StatsCompanionServicePuller(android::util::MODEM_ACTIVITY_INFO)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800117 // bluetooth_activity_info
118 {android::util::BLUETOOTH_ACTIVITY_INFO,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800119 {.puller = new StatsCompanionServicePuller(android::util::BLUETOOTH_ACTIVITY_INFO)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800120 // system_elapsed_realtime
121 {android::util::SYSTEM_ELAPSED_REALTIME,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800122 {.pullTimeoutNs = NS_PER_SEC / 2,
123 .coolDownNs = NS_PER_SEC,
124 .puller = new StatsCompanionServicePuller(android::util::SYSTEM_ELAPSED_REALTIME)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800125 // system_uptime
126 {android::util::SYSTEM_UPTIME,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800127 {.puller = new StatsCompanionServicePuller(android::util::SYSTEM_UPTIME)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800128 // remaining_battery_capacity
129 {android::util::REMAINING_BATTERY_CAPACITY,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800130 {.puller = new ResourceHealthManagerPuller(android::util::REMAINING_BATTERY_CAPACITY)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800131 // full_battery_capacity
132 {android::util::FULL_BATTERY_CAPACITY,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800133 {.puller = new ResourceHealthManagerPuller(android::util::FULL_BATTERY_CAPACITY)}},
Bookatz17f0d8a2018-09-13 12:56:32 -0700134 // battery_voltage
135 {android::util::BATTERY_VOLTAGE,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800136 {.puller = new ResourceHealthManagerPuller(android::util::BATTERY_VOLTAGE)}},
Maggie White8735b852019-01-18 11:40:49 -0800137 // battery_level
Tej Singhb1dbc8b2018-11-19 15:49:47 -0800138 {android::util::BATTERY_LEVEL,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800139 {.puller = new ResourceHealthManagerPuller(android::util::BATTERY_LEVEL)}},
Maggie White8735b852019-01-18 11:40:49 -0800140 // battery_cycle_count
141 {android::util::BATTERY_CYCLE_COUNT,
142 {.puller = new ResourceHealthManagerPuller(android::util::BATTERY_CYCLE_COUNT)}},
Rajeev Kumar22d92b72018-02-07 18:38:36 -0800143 // process_memory_state
144 {android::util::PROCESS_MEMORY_STATE,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800145 {.additiveFields = {4, 5, 6, 7, 8, 9},
146 .puller = new StatsCompanionServicePuller(android::util::PROCESS_MEMORY_STATE)}},
Rafal Slawik08621582018-10-15 14:53:07 +0100147 // native_process_memory_state
148 {android::util::NATIVE_PROCESS_MEMORY_STATE,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800149 {.additiveFields = {3, 4, 5, 6},
150 .puller = new StatsCompanionServicePuller(android::util::NATIVE_PROCESS_MEMORY_STATE)}},
Rafal Slawikd4e87572019-03-12 13:08:38 +0000151 // process_memory_high_water_mark
Rafal Slawik3bea8952018-11-15 12:39:33 +0000152 {android::util::PROCESS_MEMORY_HIGH_WATER_MARK,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800153 {.additiveFields = {3},
154 .puller =
155 new StatsCompanionServicePuller(android::util::PROCESS_MEMORY_HIGH_WATER_MARK)}},
Rafal Slawikd4e87572019-03-12 13:08:38 +0000156 // system_ion_heap_size
157 {android::util::SYSTEM_ION_HEAP_SIZE,
158 {.puller = new StatsCompanionServicePuller(android::util::SYSTEM_ION_HEAP_SIZE)}},
Tej Singh40298312018-02-16 00:15:09 -0800159 // temperature
Tej Singh9876a3b2019-01-11 14:28:35 -0800160 {android::util::TEMPERATURE,
Chenjie Yu97dbb202019-02-13 16:42:04 -0800161 {.puller = new StatsCompanionServicePuller(android::util::TEMPERATURE)}},
Maggie White442b6312019-04-05 16:26:44 -0700162 // cooling_device
163 {android::util::COOLING_DEVICE,
164 {.puller = new StatsCompanionServicePuller(android::util::COOLING_DEVICE)}},
Olivier Gaillard00bfb1b2018-07-10 11:25:09 +0100165 // binder_calls
166 {android::util::BINDER_CALLS,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800167 {.additiveFields = {4, 5, 6, 8, 12},
168 .puller = new StatsCompanionServicePuller(android::util::BINDER_CALLS)}},
Olivier Gaillard6f52d152018-07-25 12:13:12 +0100169 // binder_calls_exceptions
170 {android::util::BINDER_CALLS_EXCEPTIONS,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800171 {.puller = new StatsCompanionServicePuller(android::util::BINDER_CALLS_EXCEPTIONS)}},
Marcin Oczeretkod8cc8592018-08-22 16:07:36 +0100172 // looper_stats
173 {android::util::LOOPER_STATS,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800174 {.additiveFields = {5, 6, 7, 8, 9},
175 .puller = new StatsCompanionServicePuller(android::util::LOOPER_STATS)}},
Tej Singh86dc9db2018-09-06 00:39:57 +0000176 // Disk Stats
177 {android::util::DISK_STATS,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800178 {.puller = new StatsCompanionServicePuller(android::util::DISK_STATS)}},
Tej Singh86dc9db2018-09-06 00:39:57 +0000179 // Directory usage
180 {android::util::DIRECTORY_USAGE,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800181 {.puller = new StatsCompanionServicePuller(android::util::DIRECTORY_USAGE)}},
Tej Singh86dc9db2018-09-06 00:39:57 +0000182 // Size of app's code, data, and cache
183 {android::util::APP_SIZE,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800184 {.puller = new StatsCompanionServicePuller(android::util::APP_SIZE)}},
Tej Singh86dc9db2018-09-06 00:39:57 +0000185 // Size of specific categories of files. Eg. Music.
186 {android::util::CATEGORY_SIZE,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800187 {.puller = new StatsCompanionServicePuller(android::util::CATEGORY_SIZE)}},
Kevin Chyn6ecc5702019-01-28 12:00:37 -0800188 // Number of fingerprints enrolled for each user.
Kevin Chyn1741a072019-01-17 11:54:40 -0800189 {android::util::NUM_FINGERPRINTS_ENROLLED,
190 {.puller = new StatsCompanionServicePuller(android::util::NUM_FINGERPRINTS_ENROLLED)}},
Kevin Chyn6ecc5702019-01-28 12:00:37 -0800191 // Number of faces enrolled for each user.
192 {android::util::NUM_FACES_ENROLLED,
193 {.puller = new StatsCompanionServicePuller(android::util::NUM_FACES_ENROLLED)}},
Chenjie Yu2c0d83d2018-09-02 06:37:08 -0700194 // ProcStats.
195 {android::util::PROC_STATS,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800196 {.puller = new StatsCompanionServicePuller(android::util::PROC_STATS)}},
Chenjie Yub52779e2018-10-05 12:03:36 -0700197 // ProcStatsPkgProc.
198 {android::util::PROC_STATS_PKG_PROC,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800199 {.puller = new StatsCompanionServicePuller(android::util::PROC_STATS_PKG_PROC)}},
Tej Singhe7726dc2018-09-21 11:42:12 -0700200 // Disk I/O stats per uid.
201 {android::util::DISK_IO,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800202 {.additiveFields = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
203 .coolDownNs = 3 * NS_PER_SEC,
204 .puller = new StatsCompanionServicePuller(android::util::DISK_IO)}},
Chenjie Yuab530202018-09-26 12:39:20 -0700205 // PowerProfile constants for power model calculations.
206 {android::util::POWER_PROFILE,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800207 {.puller = new StatsCompanionServicePuller(android::util::POWER_PROFILE)}},
Yangster-mac308ea0c2018-10-22 13:10:25 -0700208 // Process cpu stats. Min cool-down is 5 sec, inline with what AcitivityManagerService uses.
209 {android::util::PROCESS_CPU_TIME,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800210 {.coolDownNs = 5 * NS_PER_SEC /* min cool-down in seconds*/,
211 .puller = new StatsCompanionServicePuller(android::util::PROCESS_CPU_TIME)}},
Misha Wagner5a51e002018-10-03 15:04:09 +0100212 {android::util::CPU_TIME_PER_THREAD_FREQ,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800213 {.additiveFields = {7, 9, 11, 13, 15, 17, 19, 21},
214 .puller = new StatsCompanionServicePuller(android::util::CPU_TIME_PER_THREAD_FREQ)}},
Bookatz75ee60422018-11-09 12:27:37 -0800215 // DeviceCalculatedPowerUse.
216 {android::util::DEVICE_CALCULATED_POWER_USE,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800217 {.puller = new StatsCompanionServicePuller(android::util::DEVICE_CALCULATED_POWER_USE)}},
Bookatz75ee60422018-11-09 12:27:37 -0800218 // DeviceCalculatedPowerBlameUid.
219 {android::util::DEVICE_CALCULATED_POWER_BLAME_UID,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800220 {.puller = new StatsCompanionServicePuller(
221 android::util::DEVICE_CALCULATED_POWER_BLAME_UID)}},
Bookatz75ee60422018-11-09 12:27:37 -0800222 // DeviceCalculatedPowerBlameOther.
223 {android::util::DEVICE_CALCULATED_POWER_BLAME_OTHER,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800224 {.puller = new StatsCompanionServicePuller(
225 android::util::DEVICE_CALCULATED_POWER_BLAME_OTHER)}},
Olivier Gaillard18d452202019-01-22 15:03:41 +0000226 // DebugElapsedClock.
227 {android::util::DEBUG_ELAPSED_CLOCK,
228 {.additiveFields = {1, 2, 3, 4},
229 .puller = new StatsCompanionServicePuller(android::util::DEBUG_ELAPSED_CLOCK)}},
230 // DebugFailingElapsedClock.
231 {android::util::DEBUG_FAILING_ELAPSED_CLOCK,
232 {.additiveFields = {1, 2, 3, 4},
233 .puller = new StatsCompanionServicePuller(android::util::DEBUG_FAILING_ELAPSED_CLOCK)}},
Bookatz366a4432018-11-20 09:42:33 -0800234 // BuildInformation.
235 {android::util::BUILD_INFORMATION,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800236 {.puller = new StatsCompanionServicePuller(android::util::BUILD_INFORMATION)}},
Philip P. Moltmann70b42ae2019-01-29 16:24:19 -0800237 // RoleHolder.
238 {android::util::ROLE_HOLDER,
239 {.puller = new StatsCompanionServicePuller(android::util::ROLE_HOLDER)}},
Philip P. Moltmannf21a3502019-01-30 09:53:04 -0800240 // PermissionState.
241 {android::util::DANGEROUS_PERMISSION_STATE,
242 {.puller = new StatsCompanionServicePuller(android::util::DANGEROUS_PERMISSION_STATE)}},
Chenjie Yu97dbb202019-02-13 16:42:04 -0800243 // TrainInfo.
244 {android::util::TRAIN_INFO, {.puller = new TrainInfoPuller()}},
Neil Fullerdb14b482019-02-05 19:44:44 +0000245 // TimeZoneDataInfo.
246 {android::util::TIME_ZONE_DATA_INFO,
247 {.puller = new StatsCompanionServicePuller(android::util::TIME_ZONE_DATA_INFO)}},
Carmen Agimof12dec122019-03-06 16:14:38 +0000248 // ExternalStorageInfo
249 {android::util::EXTERNAL_STORAGE_INFO,
250 {.puller = new StatsCompanionServicePuller(android::util::EXTERNAL_STORAGE_INFO)}},
Yiwei Zhang7e633032019-03-01 17:25:27 -0800251 // GpuStatsGlobalInfo
252 {android::util::GPU_STATS_GLOBAL_INFO,
253 {.puller = new GpuStatsPuller(android::util::GPU_STATS_GLOBAL_INFO)}},
Yiwei Zhang4fb92892019-03-04 14:29:25 -0800254 // GpuStatsAppInfo
255 {android::util::GPU_STATS_APP_INFO,
256 {.puller = new GpuStatsPuller(android::util::GPU_STATS_APP_INFO)}},
Carmen Agimofa769c152019-03-04 14:39:14 +0000257 // AppsOnExternalStorageInfo
258 {android::util::APPS_ON_EXTERNAL_STORAGE_INFO,
259 {.puller = new StatsCompanionServicePuller(android::util::APPS_ON_EXTERNAL_STORAGE_INFO)}},
joshmccloskey34c1f8e2019-04-11 14:06:43 -0700260 // Face Settings
261 {android::util::FACE_SETTINGS,
262 {.puller = new StatsCompanionServicePuller(android::util::FACE_SETTINGS)}},
Chenjie Yu2c0d83d2018-09-02 06:37:08 -0700263};
Chenjie Yu80f91122018-01-31 20:24:50 -0800264
Chenjie Yue2219202018-06-08 10:07:51 -0700265StatsPullerManager::StatsPullerManager() : mNextPullTimeNs(NO_ALARM_UPDATE) {
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700266}
267
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800268bool StatsPullerManager::Pull(int tagId, vector<shared_ptr<LogEvent>>* data) {
Tej Singh484524a2018-02-01 15:10:05 -0800269 VLOG("Initiating pulling %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700270
Chenjie Yu80f91122018-01-31 20:24:50 -0800271 if (kAllPullAtomInfo.find(tagId) != kAllPullAtomInfo.end()) {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800272 bool ret = kAllPullAtomInfo.find(tagId)->second.puller->Pull(data);
Tej Singh484524a2018-02-01 15:10:05 -0800273 VLOG("pulled %d items", (int)data->size());
Misha Wagner1eee2212019-01-22 11:47:11 +0000274 if (!ret) {
275 StatsdStats::getInstance().notePullFailed(tagId);
276 }
Tej Singh484524a2018-02-01 15:10:05 -0800277 return ret;
Yao Chen93fe3a32017-11-02 13:52:59 -0700278 } else {
Tej Singh484524a2018-02-01 15:10:05 -0800279 VLOG("Unknown tagId %d", tagId);
Yao Chen93fe3a32017-11-02 13:52:59 -0700280 return false; // Return early since we don't know what to pull.
281 }
282}
Chenjie Yub3dda412017-10-24 13:41:59 -0700283
Chenjie Yue2219202018-06-08 10:07:51 -0700284bool StatsPullerManager::PullerForMatcherExists(int tagId) const {
Tej Singhde473b512019-04-10 19:25:03 -0700285 // Vendor pulled atoms might be registered after we parse the config.
286 return isVendorPulledAtom(tagId) || kAllPullAtomInfo.find(tagId) != kAllPullAtomInfo.end();
Chenjie Yub3dda412017-10-24 13:41:59 -0700287}
288
Chenjie Yue2219202018-06-08 10:07:51 -0700289void StatsPullerManager::updateAlarmLocked() {
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700290 if (mNextPullTimeNs == NO_ALARM_UPDATE) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700291 VLOG("No need to set alarms. Skipping");
292 return;
293 }
294
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700295 sp<IStatsCompanionService> statsCompanionServiceCopy = mStatsCompanionService;
296 if (statsCompanionServiceCopy != nullptr) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700297 statsCompanionServiceCopy->setPullingAlarm(mNextPullTimeNs / 1000000);
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700298 } else {
299 VLOG("StatsCompanionService not available. Alarm not set.");
300 }
301 return;
302}
303
Chenjie Yue2219202018-06-08 10:07:51 -0700304void StatsPullerManager::SetStatsCompanionService(
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700305 sp<IStatsCompanionService> statsCompanionService) {
306 AutoMutex _l(mLock);
307 sp<IStatsCompanionService> tmpForLock = mStatsCompanionService;
308 mStatsCompanionService = statsCompanionService;
309 for (const auto& pulledAtom : kAllPullAtomInfo) {
310 pulledAtom.second.puller->SetStatsCompanionService(statsCompanionService);
311 }
312 if (mStatsCompanionService != nullptr) {
313 updateAlarmLocked();
314 }
315}
316
Chenjie Yue2219202018-06-08 10:07:51 -0700317void StatsPullerManager::RegisterReceiver(int tagId, wp<PullDataReceiver> receiver,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700318 int64_t nextPullTimeNs, int64_t intervalNs) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700319 AutoMutex _l(mLock);
Chenjie Yu6736c892017-11-09 10:50:09 -0800320 auto& receivers = mReceivers[tagId];
Chenjie Yub3dda412017-10-24 13:41:59 -0700321 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800322 if (it->receiver == receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700323 VLOG("Receiver already registered of %d", (int)receivers.size());
324 return;
325 }
326 }
327 ReceiverInfo receiverInfo;
328 receiverInfo.receiver = receiver;
Chenjie Yub3dda412017-10-24 13:41:59 -0700329
Chenjie Yu85ed8382017-12-14 16:48:54 -0800330 // Round it to the nearest minutes. This is the limit of alarm manager.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700331 // In practice, we should always have larger buckets.
332 int64_t roundedIntervalNs = intervalNs / NS_PER_SEC / 60 * NS_PER_SEC * 60;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700333 // Scheduled pulling should be at least 1 min apart.
334 // This can be lower in cts tests, in which case we round it to 1 min.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700335 if (roundedIntervalNs < 60 * (int64_t)NS_PER_SEC) {
336 roundedIntervalNs = 60 * (int64_t)NS_PER_SEC;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700337 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700338
339 receiverInfo.intervalNs = roundedIntervalNs;
340 receiverInfo.nextPullTimeNs = nextPullTimeNs;
341 receivers.push_back(receiverInfo);
342
Chenjie Yub3dda412017-10-24 13:41:59 -0700343 // There is only one alarm for all pulled events. So only set it to the smallest denom.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700344 if (nextPullTimeNs < mNextPullTimeNs) {
345 VLOG("Updating next pull time %lld", (long long)mNextPullTimeNs);
346 mNextPullTimeNs = nextPullTimeNs;
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700347 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700348 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700349 VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700350}
351
Chenjie Yue2219202018-06-08 10:07:51 -0700352void StatsPullerManager::UnRegisterReceiver(int tagId, wp<PullDataReceiver> receiver) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700353 AutoMutex _l(mLock);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700354 if (mReceivers.find(tagId) == mReceivers.end()) {
355 VLOG("Unknown pull code or no receivers: %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700356 return;
357 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700358 auto& receivers = mReceivers.find(tagId)->second;
Chenjie Yub3dda412017-10-24 13:41:59 -0700359 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800360 if (receiver == it->receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700361 receivers.erase(it);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700362 VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700363 return;
364 }
365 }
366}
367
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800368void StatsPullerManager::OnAlarmFired(int64_t elapsedTimeNs) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700369 AutoMutex _l(mLock);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800370 int64_t wallClockNs = getWallClockNs();
Chenjie Yub3dda412017-10-24 13:41:59 -0700371
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700372 int64_t minNextPullTimeNs = NO_ALARM_UPDATE;
Chenjie Yub3dda412017-10-24 13:41:59 -0700373
374 vector<pair<int, vector<ReceiverInfo*>>> needToPull =
375 vector<pair<int, vector<ReceiverInfo*>>>();
376 for (auto& pair : mReceivers) {
377 vector<ReceiverInfo*> receivers = vector<ReceiverInfo*>();
Yao Chen93fe3a32017-11-02 13:52:59 -0700378 if (pair.second.size() != 0) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700379 for (ReceiverInfo& receiverInfo : pair.second) {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800380 if (receiverInfo.nextPullTimeNs <= elapsedTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700381 receivers.push_back(&receiverInfo);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700382 } else {
383 if (receiverInfo.nextPullTimeNs < minNextPullTimeNs) {
384 minNextPullTimeNs = receiverInfo.nextPullTimeNs;
385 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700386 }
387 }
388 if (receivers.size() > 0) {
389 needToPull.push_back(make_pair(pair.first, receivers));
390 }
391 }
392 }
393
394 for (const auto& pullInfo : needToPull) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700395 vector<shared_ptr<LogEvent>> data;
Olivier Gaillardc5f11c42019-02-05 12:44:58 +0000396 bool pullSuccess = Pull(pullInfo.first, &data);
397 if (pullSuccess) {
398 StatsdStats::getInstance().notePullDelay(
399 pullInfo.first, getElapsedRealtimeNs() - elapsedTimeNs);
400 } else {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800401 VLOG("pull failed at %lld, will try again later", (long long)elapsedTimeNs);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800402 }
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800403
404 // Convention is to mark pull atom timestamp at request time.
405 // If we pull at t0, puller starts at t1, finishes at t2, and send back
406 // at t3, we mark t0 as its timestamp, which should correspond to its
407 // triggering event, such as condition change at t0.
408 // Here the triggering event is alarm fired from AlarmManager.
409 // In ValueMetricProducer and GaugeMetricProducer we do same thing
410 // when pull on condition change, etc.
411 for (auto& event : data) {
412 event->setElapsedTimestampNs(elapsedTimeNs);
413 event->setLogdWallClockTimestampNs(wallClockNs);
414 }
415
416 for (const auto& receiverInfo : pullInfo.second) {
417 sp<PullDataReceiver> receiverPtr = receiverInfo->receiver.promote();
418 if (receiverPtr != nullptr) {
Olivier Gaillard11203df2019-02-06 13:18:09 +0000419 receiverPtr->onDataPulled(data, pullSuccess, elapsedTimeNs);
Olivier Gaillardc5f11c42019-02-05 12:44:58 +0000420 // We may have just come out of a coma, compute next pull time.
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800421 int numBucketsAhead =
422 (elapsedTimeNs - receiverInfo->nextPullTimeNs) / receiverInfo->intervalNs;
423 receiverInfo->nextPullTimeNs += (numBucketsAhead + 1) * receiverInfo->intervalNs;
424 if (receiverInfo->nextPullTimeNs < minNextPullTimeNs) {
425 minNextPullTimeNs = receiverInfo->nextPullTimeNs;
Chenjie Yu6736c892017-11-09 10:50:09 -0800426 }
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800427 } else {
428 VLOG("receiver already gone.");
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700429 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700430 }
431 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700432
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700433 VLOG("mNextPullTimeNs: %lld updated to %lld", (long long)mNextPullTimeNs,
434 (long long)minNextPullTimeNs);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700435 mNextPullTimeNs = minNextPullTimeNs;
436 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700437}
438
Chenjie Yue2219202018-06-08 10:07:51 -0700439int StatsPullerManager::ForceClearPullerCache() {
Chenjie Yufa22d652018-02-05 14:37:48 -0800440 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800441 for (const auto& pulledAtom : kAllPullAtomInfo) {
442 totalCleared += pulledAtom.second.puller->ForceClearCache();
Chenjie Yue72252b2018-02-01 13:19:35 -0800443 }
Chenjie Yufa22d652018-02-05 14:37:48 -0800444 return totalCleared;
445}
446
Chenjie Yue2219202018-06-08 10:07:51 -0700447int StatsPullerManager::ClearPullerCacheIfNecessary(int64_t timestampNs) {
Chenjie Yufa22d652018-02-05 14:37:48 -0800448 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800449 for (const auto& pulledAtom : kAllPullAtomInfo) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700450 totalCleared += pulledAtom.second.puller->ClearCacheIfNecessary(timestampNs);
Chenjie Yufa22d652018-02-05 14:37:48 -0800451 }
452 return totalCleared;
Chenjie Yue72252b2018-02-01 13:19:35 -0800453}
454
Tej Singha0c89dd2019-01-25 16:39:18 -0800455void StatsPullerManager::RegisterPullerCallback(int32_t atomTag,
456 const sp<IStatsPullerCallback>& callback) {
457 AutoMutex _l(mLock);
458 // Platform pullers cannot be changed.
Tej Singhde473b512019-04-10 19:25:03 -0700459 if (!isVendorPulledAtom(atomTag)) {
460 VLOG("RegisterPullerCallback: atom tag %d is not vendor pulled", atomTag);
Tej Singha0c89dd2019-01-25 16:39:18 -0800461 return;
462 }
463 VLOG("RegisterPullerCallback: adding puller for tag %d", atomTag);
464 StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/true);
465 kAllPullAtomInfo[atomTag] = {.puller = new StatsCallbackPuller(atomTag, callback)};
466}
467
468void StatsPullerManager::UnregisterPullerCallback(int32_t atomTag) {
469 AutoMutex _l(mLock);
470 // Platform pullers cannot be changed.
Tej Singhde473b512019-04-10 19:25:03 -0700471 if (!isVendorPulledAtom(atomTag)) {
Tej Singha0c89dd2019-01-25 16:39:18 -0800472 return;
473 }
474 StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/false);
475 kAllPullAtomInfo.erase(atomTag);
476}
477
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700478} // namespace statsd
479} // namespace os
480} // namespace android