blob: 1513834b67244ae74e88fcc0706d368582538491 [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"
Bookatz92da2832018-11-01 18:10:03 -070030#include "PowerStatsPuller.h"
Tej Singh40298312018-02-16 00:15:09 -080031#include "ResourceHealthManagerPuller.h"
Tej Singha0c89dd2019-01-25 16:39:18 -080032#include "StatsCallbackPuller.h"
Chenjie Yu97dbb202019-02-13 16:42:04 -080033#include "StatsCompanionServicePuller.h"
Chenjie Yue2219202018-06-08 10:07:51 -070034#include "StatsPullerManager.h"
Tej Singhbf972d92018-01-10 20:51:13 -080035#include "SubsystemSleepStatePuller.h"
Chenjie Yu97dbb202019-02-13 16:42:04 -080036#include "TrainInfoPuller.h"
Chenjie Yu5305e1d2017-10-31 13:49:36 -070037#include "statslog.h"
David Chen1481fe12017-10-16 13:16:34 -070038
39#include <iostream>
Chenjie Yu1a317ba2017-10-05 16:05:32 -070040
Yao Chen93fe3a32017-11-02 13:52:59 -070041using std::make_shared;
Chenjie Yu5305e1d2017-10-31 13:49:36 -070042using std::map;
Yao Chen93fe3a32017-11-02 13:52:59 -070043using std::shared_ptr;
Chenjie Yub3dda412017-10-24 13:41:59 -070044using std::string;
45using std::vector;
Chenjie Yu6736c892017-11-09 10:50:09 -080046using std::list;
Chenjie Yu1a317ba2017-10-05 16:05:32 -070047
48namespace android {
49namespace os {
50namespace statsd {
51
Chenjie Yu3b3adcd2018-04-18 16:25:36 -070052// Values smaller than this may require to update the alarm.
53const int64_t NO_ALARM_UPDATE = INT64_MAX;
54
Tej Singha0c89dd2019-01-25 16:39:18 -080055std::map<int, PullAtomInfo> StatsPullerManager::kAllPullAtomInfo = {
Chenjie Yufeba3092018-02-08 14:33:37 -080056 // wifi_bytes_transfer
57 {android::util::WIFI_BYTES_TRANSFER,
Chenjie Yu0bd73db2018-12-16 07:37:04 -080058 {.additiveFields = {2, 3, 4, 5},
59 .puller = new StatsCompanionServicePuller(android::util::WIFI_BYTES_TRANSFER)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080060 // wifi_bytes_transfer_by_fg_bg
61 {android::util::WIFI_BYTES_TRANSFER_BY_FG_BG,
Chenjie Yu0bd73db2018-12-16 07:37:04 -080062 {.additiveFields = {3, 4, 5, 6},
63 .puller = new StatsCompanionServicePuller(android::util::WIFI_BYTES_TRANSFER_BY_FG_BG)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080064 // mobile_bytes_transfer
65 {android::util::MOBILE_BYTES_TRANSFER,
Chenjie Yu0bd73db2018-12-16 07:37:04 -080066 {.additiveFields = {2, 3, 4, 5},
67 .puller = new StatsCompanionServicePuller(android::util::MOBILE_BYTES_TRANSFER)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080068 // mobile_bytes_transfer_by_fg_bg
69 {android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG,
Chenjie Yu0bd73db2018-12-16 07:37:04 -080070 {.additiveFields = {3, 4, 5, 6},
71 .puller =
72 new StatsCompanionServicePuller(android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080073 // bluetooth_bytes_transfer
74 {android::util::BLUETOOTH_BYTES_TRANSFER,
Chenjie Yu0bd73db2018-12-16 07:37:04 -080075 {.additiveFields = {2, 3},
76 .puller = new StatsCompanionServicePuller(android::util::BLUETOOTH_BYTES_TRANSFER)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080077 // kernel_wakelock
78 {android::util::KERNEL_WAKELOCK,
Chenjie Yu0bd73db2018-12-16 07:37:04 -080079 {.puller = new StatsCompanionServicePuller(android::util::KERNEL_WAKELOCK)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080080 // subsystem_sleep_state
Chenjie Yu0bd73db2018-12-16 07:37:04 -080081 {android::util::SUBSYSTEM_SLEEP_STATE, {.puller = new SubsystemSleepStatePuller()}},
Bookatz92da2832018-11-01 18:10:03 -070082 // on_device_power_measurement
Chenjie Yu0bd73db2018-12-16 07:37:04 -080083 {android::util::ON_DEVICE_POWER_MEASUREMENT, {.puller = new PowerStatsPuller()}},
Chenjie Yufeba3092018-02-08 14:33:37 -080084 // cpu_time_per_freq
85 {android::util::CPU_TIME_PER_FREQ,
Chenjie Yu0bd73db2018-12-16 07:37:04 -080086 {.additiveFields = {3},
87 .puller = new StatsCompanionServicePuller(android::util::CPU_TIME_PER_FREQ)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080088 // cpu_time_per_uid
Chenjie Yuec676612018-03-07 09:19:17 -080089 {android::util::CPU_TIME_PER_UID,
Chenjie Yu0bd73db2018-12-16 07:37:04 -080090 {.additiveFields = {2, 3},
91 .puller = new StatsCompanionServicePuller(android::util::CPU_TIME_PER_UID)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080092 // cpu_time_per_uid_freq
Chenjie Yu1a0a9412018-03-28 10:07:22 -070093 // the throttling is 3sec, handled in
94 // frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader
Chenjie Yuec676612018-03-07 09:19:17 -080095 {android::util::CPU_TIME_PER_UID_FREQ,
Chenjie Yu0bd73db2018-12-16 07:37:04 -080096 {.additiveFields = {4},
97 .puller = new StatsCompanionServicePuller(android::util::CPU_TIME_PER_UID_FREQ)}},
Chenjie Yuec676612018-03-07 09:19:17 -080098 // cpu_active_time
Chenjie Yu1a0a9412018-03-28 10:07:22 -070099 // the throttling is 3sec, handled in
100 // frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader
Chenjie Yuec676612018-03-07 09:19:17 -0800101 {android::util::CPU_ACTIVE_TIME,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800102 {.additiveFields = {2},
103 .puller = new StatsCompanionServicePuller(android::util::CPU_ACTIVE_TIME)}},
Chenjie Yuec676612018-03-07 09:19:17 -0800104 // cpu_cluster_time
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700105 // the throttling is 3sec, handled in
106 // frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader
Chenjie Yuec676612018-03-07 09:19:17 -0800107 {android::util::CPU_CLUSTER_TIME,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800108 {.additiveFields = {3},
109 .puller = new StatsCompanionServicePuller(android::util::CPU_CLUSTER_TIME)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800110 // wifi_activity_energy_info
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800111 {android::util::WIFI_ACTIVITY_INFO,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800112 {.puller = new StatsCompanionServicePuller(android::util::WIFI_ACTIVITY_INFO)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800113 // modem_activity_info
114 {android::util::MODEM_ACTIVITY_INFO,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800115 {.puller = new StatsCompanionServicePuller(android::util::MODEM_ACTIVITY_INFO)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800116 // bluetooth_activity_info
117 {android::util::BLUETOOTH_ACTIVITY_INFO,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800118 {.puller = new StatsCompanionServicePuller(android::util::BLUETOOTH_ACTIVITY_INFO)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800119 // system_elapsed_realtime
120 {android::util::SYSTEM_ELAPSED_REALTIME,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800121 {.pullTimeoutNs = NS_PER_SEC / 2,
122 .coolDownNs = NS_PER_SEC,
123 .puller = new StatsCompanionServicePuller(android::util::SYSTEM_ELAPSED_REALTIME)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800124 // system_uptime
125 {android::util::SYSTEM_UPTIME,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800126 {.puller = new StatsCompanionServicePuller(android::util::SYSTEM_UPTIME)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800127 // remaining_battery_capacity
128 {android::util::REMAINING_BATTERY_CAPACITY,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800129 {.puller = new ResourceHealthManagerPuller(android::util::REMAINING_BATTERY_CAPACITY)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800130 // full_battery_capacity
131 {android::util::FULL_BATTERY_CAPACITY,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800132 {.puller = new ResourceHealthManagerPuller(android::util::FULL_BATTERY_CAPACITY)}},
Bookatz17f0d8a2018-09-13 12:56:32 -0700133 // battery_voltage
134 {android::util::BATTERY_VOLTAGE,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800135 {.puller = new ResourceHealthManagerPuller(android::util::BATTERY_VOLTAGE)}},
Maggie White8735b852019-01-18 11:40:49 -0800136 // battery_level
Tej Singhb1dbc8b2018-11-19 15:49:47 -0800137 {android::util::BATTERY_LEVEL,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800138 {.puller = new ResourceHealthManagerPuller(android::util::BATTERY_LEVEL)}},
Maggie White8735b852019-01-18 11:40:49 -0800139 // battery_cycle_count
140 {android::util::BATTERY_CYCLE_COUNT,
141 {.puller = new ResourceHealthManagerPuller(android::util::BATTERY_CYCLE_COUNT)}},
Rajeev Kumar22d92b72018-02-07 18:38:36 -0800142 // process_memory_state
143 {android::util::PROCESS_MEMORY_STATE,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800144 {.additiveFields = {4, 5, 6, 7, 8, 9},
145 .puller = new StatsCompanionServicePuller(android::util::PROCESS_MEMORY_STATE)}},
Rafal Slawik08621582018-10-15 14:53:07 +0100146 // native_process_memory_state
147 {android::util::NATIVE_PROCESS_MEMORY_STATE,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800148 {.additiveFields = {3, 4, 5, 6},
149 .puller = new StatsCompanionServicePuller(android::util::NATIVE_PROCESS_MEMORY_STATE)}},
Rafal Slawik3bea8952018-11-15 12:39:33 +0000150 {android::util::PROCESS_MEMORY_HIGH_WATER_MARK,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800151 {.additiveFields = {3},
152 .puller =
153 new StatsCompanionServicePuller(android::util::PROCESS_MEMORY_HIGH_WATER_MARK)}},
Tej Singh40298312018-02-16 00:15:09 -0800154 // temperature
Tej Singh9876a3b2019-01-11 14:28:35 -0800155 {android::util::TEMPERATURE,
Chenjie Yu97dbb202019-02-13 16:42:04 -0800156 {.puller = new StatsCompanionServicePuller(android::util::TEMPERATURE)}},
Olivier Gaillard00bfb1b2018-07-10 11:25:09 +0100157 // binder_calls
158 {android::util::BINDER_CALLS,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800159 {.additiveFields = {4, 5, 6, 8, 12},
160 .puller = new StatsCompanionServicePuller(android::util::BINDER_CALLS)}},
Olivier Gaillard6f52d152018-07-25 12:13:12 +0100161 // binder_calls_exceptions
162 {android::util::BINDER_CALLS_EXCEPTIONS,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800163 {.puller = new StatsCompanionServicePuller(android::util::BINDER_CALLS_EXCEPTIONS)}},
Marcin Oczeretkod8cc8592018-08-22 16:07:36 +0100164 // looper_stats
165 {android::util::LOOPER_STATS,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800166 {.additiveFields = {5, 6, 7, 8, 9},
167 .puller = new StatsCompanionServicePuller(android::util::LOOPER_STATS)}},
Tej Singh86dc9db2018-09-06 00:39:57 +0000168 // Disk Stats
169 {android::util::DISK_STATS,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800170 {.puller = new StatsCompanionServicePuller(android::util::DISK_STATS)}},
Tej Singh86dc9db2018-09-06 00:39:57 +0000171 // Directory usage
172 {android::util::DIRECTORY_USAGE,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800173 {.puller = new StatsCompanionServicePuller(android::util::DIRECTORY_USAGE)}},
Tej Singh86dc9db2018-09-06 00:39:57 +0000174 // Size of app's code, data, and cache
175 {android::util::APP_SIZE,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800176 {.puller = new StatsCompanionServicePuller(android::util::APP_SIZE)}},
Tej Singh86dc9db2018-09-06 00:39:57 +0000177 // Size of specific categories of files. Eg. Music.
178 {android::util::CATEGORY_SIZE,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800179 {.puller = new StatsCompanionServicePuller(android::util::CATEGORY_SIZE)}},
Kevin Chyn6ecc5702019-01-28 12:00:37 -0800180 // Number of fingerprints enrolled for each user.
Kevin Chyn1741a072019-01-17 11:54:40 -0800181 {android::util::NUM_FINGERPRINTS_ENROLLED,
182 {.puller = new StatsCompanionServicePuller(android::util::NUM_FINGERPRINTS_ENROLLED)}},
Kevin Chyn6ecc5702019-01-28 12:00:37 -0800183 // Number of faces enrolled for each user.
184 {android::util::NUM_FACES_ENROLLED,
185 {.puller = new StatsCompanionServicePuller(android::util::NUM_FACES_ENROLLED)}},
Chenjie Yu2c0d83d2018-09-02 06:37:08 -0700186 // ProcStats.
187 {android::util::PROC_STATS,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800188 {.puller = new StatsCompanionServicePuller(android::util::PROC_STATS)}},
Chenjie Yub52779e2018-10-05 12:03:36 -0700189 // ProcStatsPkgProc.
190 {android::util::PROC_STATS_PKG_PROC,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800191 {.puller = new StatsCompanionServicePuller(android::util::PROC_STATS_PKG_PROC)}},
Tej Singhe7726dc2018-09-21 11:42:12 -0700192 // Disk I/O stats per uid.
193 {android::util::DISK_IO,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800194 {.additiveFields = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
195 .coolDownNs = 3 * NS_PER_SEC,
196 .puller = new StatsCompanionServicePuller(android::util::DISK_IO)}},
Chenjie Yuab530202018-09-26 12:39:20 -0700197 // PowerProfile constants for power model calculations.
198 {android::util::POWER_PROFILE,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800199 {.puller = new StatsCompanionServicePuller(android::util::POWER_PROFILE)}},
Yangster-mac308ea0c2018-10-22 13:10:25 -0700200 // Process cpu stats. Min cool-down is 5 sec, inline with what AcitivityManagerService uses.
201 {android::util::PROCESS_CPU_TIME,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800202 {.coolDownNs = 5 * NS_PER_SEC /* min cool-down in seconds*/,
203 .puller = new StatsCompanionServicePuller(android::util::PROCESS_CPU_TIME)}},
Misha Wagner5a51e002018-10-03 15:04:09 +0100204 {android::util::CPU_TIME_PER_THREAD_FREQ,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800205 {.additiveFields = {7, 9, 11, 13, 15, 17, 19, 21},
206 .puller = new StatsCompanionServicePuller(android::util::CPU_TIME_PER_THREAD_FREQ)}},
Bookatz75ee6042018-11-09 12:27:37 -0800207 // DeviceCalculatedPowerUse.
208 {android::util::DEVICE_CALCULATED_POWER_USE,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800209 {.puller = new StatsCompanionServicePuller(android::util::DEVICE_CALCULATED_POWER_USE)}},
Bookatz75ee6042018-11-09 12:27:37 -0800210 // DeviceCalculatedPowerBlameUid.
211 {android::util::DEVICE_CALCULATED_POWER_BLAME_UID,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800212 {.puller = new StatsCompanionServicePuller(
213 android::util::DEVICE_CALCULATED_POWER_BLAME_UID)}},
Bookatz75ee6042018-11-09 12:27:37 -0800214 // DeviceCalculatedPowerBlameOther.
215 {android::util::DEVICE_CALCULATED_POWER_BLAME_OTHER,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800216 {.puller = new StatsCompanionServicePuller(
217 android::util::DEVICE_CALCULATED_POWER_BLAME_OTHER)}},
Olivier Gaillard18d452202019-01-22 15:03:41 +0000218 // DebugElapsedClock.
219 {android::util::DEBUG_ELAPSED_CLOCK,
220 {.additiveFields = {1, 2, 3, 4},
221 .puller = new StatsCompanionServicePuller(android::util::DEBUG_ELAPSED_CLOCK)}},
222 // DebugFailingElapsedClock.
223 {android::util::DEBUG_FAILING_ELAPSED_CLOCK,
224 {.additiveFields = {1, 2, 3, 4},
225 .puller = new StatsCompanionServicePuller(android::util::DEBUG_FAILING_ELAPSED_CLOCK)}},
Bookatz366a4432018-11-20 09:42:33 -0800226 // BuildInformation.
227 {android::util::BUILD_INFORMATION,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800228 {.puller = new StatsCompanionServicePuller(android::util::BUILD_INFORMATION)}},
Philip P. Moltmann70b42ae2019-01-29 16:24:19 -0800229 // RoleHolder.
230 {android::util::ROLE_HOLDER,
231 {.puller = new StatsCompanionServicePuller(android::util::ROLE_HOLDER)}},
Philip P. Moltmannf21a3502019-01-30 09:53:04 -0800232 // PermissionState.
233 {android::util::DANGEROUS_PERMISSION_STATE,
234 {.puller = new StatsCompanionServicePuller(android::util::DANGEROUS_PERMISSION_STATE)}},
Chenjie Yu97dbb202019-02-13 16:42:04 -0800235 // TrainInfo.
236 {android::util::TRAIN_INFO, {.puller = new TrainInfoPuller()}},
Neil Fullerdb14b482019-02-05 19:44:44 +0000237 // TimeZoneDataInfo.
238 {android::util::TIME_ZONE_DATA_INFO,
239 {.puller = new StatsCompanionServicePuller(android::util::TIME_ZONE_DATA_INFO)}},
Carmen Agimof35f7f6f2019-02-05 18:19:21 +0000240 // SDCardInfo
241 {android::util::SDCARD_INFO,
242 {.puller = new StatsCompanionServicePuller(android::util::SDCARD_INFO)}},
Chenjie Yu2c0d83d2018-09-02 06:37:08 -0700243};
Chenjie Yu80f91122018-01-31 20:24:50 -0800244
Chenjie Yue2219202018-06-08 10:07:51 -0700245StatsPullerManager::StatsPullerManager() : mNextPullTimeNs(NO_ALARM_UPDATE) {
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700246}
247
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800248bool StatsPullerManager::Pull(int tagId, vector<shared_ptr<LogEvent>>* data) {
Tej Singh484524a2018-02-01 15:10:05 -0800249 VLOG("Initiating pulling %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700250
Chenjie Yu80f91122018-01-31 20:24:50 -0800251 if (kAllPullAtomInfo.find(tagId) != kAllPullAtomInfo.end()) {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800252 bool ret = kAllPullAtomInfo.find(tagId)->second.puller->Pull(data);
Tej Singh484524a2018-02-01 15:10:05 -0800253 VLOG("pulled %d items", (int)data->size());
Misha Wagner1eee2212019-01-22 11:47:11 +0000254 if (!ret) {
255 StatsdStats::getInstance().notePullFailed(tagId);
256 }
Tej Singh484524a2018-02-01 15:10:05 -0800257 return ret;
Yao Chen93fe3a32017-11-02 13:52:59 -0700258 } else {
Tej Singh484524a2018-02-01 15:10:05 -0800259 VLOG("Unknown tagId %d", tagId);
Yao Chen93fe3a32017-11-02 13:52:59 -0700260 return false; // Return early since we don't know what to pull.
261 }
262}
Chenjie Yub3dda412017-10-24 13:41:59 -0700263
Chenjie Yue2219202018-06-08 10:07:51 -0700264bool StatsPullerManager::PullerForMatcherExists(int tagId) const {
Chenjie Yufeba3092018-02-08 14:33:37 -0800265 return kAllPullAtomInfo.find(tagId) != kAllPullAtomInfo.end();
Chenjie Yub3dda412017-10-24 13:41:59 -0700266}
267
Chenjie Yue2219202018-06-08 10:07:51 -0700268void StatsPullerManager::updateAlarmLocked() {
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700269 if (mNextPullTimeNs == NO_ALARM_UPDATE) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700270 VLOG("No need to set alarms. Skipping");
271 return;
272 }
273
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700274 sp<IStatsCompanionService> statsCompanionServiceCopy = mStatsCompanionService;
275 if (statsCompanionServiceCopy != nullptr) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700276 statsCompanionServiceCopy->setPullingAlarm(mNextPullTimeNs / 1000000);
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700277 } else {
278 VLOG("StatsCompanionService not available. Alarm not set.");
279 }
280 return;
281}
282
Chenjie Yue2219202018-06-08 10:07:51 -0700283void StatsPullerManager::SetStatsCompanionService(
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700284 sp<IStatsCompanionService> statsCompanionService) {
285 AutoMutex _l(mLock);
286 sp<IStatsCompanionService> tmpForLock = mStatsCompanionService;
287 mStatsCompanionService = statsCompanionService;
288 for (const auto& pulledAtom : kAllPullAtomInfo) {
289 pulledAtom.second.puller->SetStatsCompanionService(statsCompanionService);
290 }
291 if (mStatsCompanionService != nullptr) {
292 updateAlarmLocked();
293 }
294}
295
Chenjie Yue2219202018-06-08 10:07:51 -0700296void StatsPullerManager::RegisterReceiver(int tagId, wp<PullDataReceiver> receiver,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700297 int64_t nextPullTimeNs, int64_t intervalNs) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700298 AutoMutex _l(mLock);
Chenjie Yu6736c892017-11-09 10:50:09 -0800299 auto& receivers = mReceivers[tagId];
Chenjie Yub3dda412017-10-24 13:41:59 -0700300 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800301 if (it->receiver == receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700302 VLOG("Receiver already registered of %d", (int)receivers.size());
303 return;
304 }
305 }
306 ReceiverInfo receiverInfo;
307 receiverInfo.receiver = receiver;
Chenjie Yub3dda412017-10-24 13:41:59 -0700308
Chenjie Yu85ed8382017-12-14 16:48:54 -0800309 // Round it to the nearest minutes. This is the limit of alarm manager.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700310 // In practice, we should always have larger buckets.
311 int64_t roundedIntervalNs = intervalNs / NS_PER_SEC / 60 * NS_PER_SEC * 60;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700312 // Scheduled pulling should be at least 1 min apart.
313 // This can be lower in cts tests, in which case we round it to 1 min.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700314 if (roundedIntervalNs < 60 * (int64_t)NS_PER_SEC) {
315 roundedIntervalNs = 60 * (int64_t)NS_PER_SEC;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700316 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700317
318 receiverInfo.intervalNs = roundedIntervalNs;
319 receiverInfo.nextPullTimeNs = nextPullTimeNs;
320 receivers.push_back(receiverInfo);
321
Chenjie Yub3dda412017-10-24 13:41:59 -0700322 // There is only one alarm for all pulled events. So only set it to the smallest denom.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700323 if (nextPullTimeNs < mNextPullTimeNs) {
324 VLOG("Updating next pull time %lld", (long long)mNextPullTimeNs);
325 mNextPullTimeNs = nextPullTimeNs;
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700326 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700327 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700328 VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700329}
330
Chenjie Yue2219202018-06-08 10:07:51 -0700331void StatsPullerManager::UnRegisterReceiver(int tagId, wp<PullDataReceiver> receiver) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700332 AutoMutex _l(mLock);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700333 if (mReceivers.find(tagId) == mReceivers.end()) {
334 VLOG("Unknown pull code or no receivers: %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700335 return;
336 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700337 auto& receivers = mReceivers.find(tagId)->second;
Chenjie Yub3dda412017-10-24 13:41:59 -0700338 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800339 if (receiver == it->receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700340 receivers.erase(it);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700341 VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700342 return;
343 }
344 }
345}
346
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800347void StatsPullerManager::OnAlarmFired(int64_t elapsedTimeNs) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700348 AutoMutex _l(mLock);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800349 int64_t wallClockNs = getWallClockNs();
Chenjie Yub3dda412017-10-24 13:41:59 -0700350
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700351 int64_t minNextPullTimeNs = NO_ALARM_UPDATE;
Chenjie Yub3dda412017-10-24 13:41:59 -0700352
353 vector<pair<int, vector<ReceiverInfo*>>> needToPull =
354 vector<pair<int, vector<ReceiverInfo*>>>();
355 for (auto& pair : mReceivers) {
356 vector<ReceiverInfo*> receivers = vector<ReceiverInfo*>();
Yao Chen93fe3a32017-11-02 13:52:59 -0700357 if (pair.second.size() != 0) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700358 for (ReceiverInfo& receiverInfo : pair.second) {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800359 if (receiverInfo.nextPullTimeNs <= elapsedTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700360 receivers.push_back(&receiverInfo);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700361 } else {
362 if (receiverInfo.nextPullTimeNs < minNextPullTimeNs) {
363 minNextPullTimeNs = receiverInfo.nextPullTimeNs;
364 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700365 }
366 }
367 if (receivers.size() > 0) {
368 needToPull.push_back(make_pair(pair.first, receivers));
369 }
370 }
371 }
372
373 for (const auto& pullInfo : needToPull) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700374 vector<shared_ptr<LogEvent>> data;
Olivier Gaillardc5f11c42019-02-05 12:44:58 +0000375 bool pullSuccess = Pull(pullInfo.first, &data);
376 if (pullSuccess) {
377 StatsdStats::getInstance().notePullDelay(
378 pullInfo.first, getElapsedRealtimeNs() - elapsedTimeNs);
379 } else {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800380 VLOG("pull failed at %lld, will try again later", (long long)elapsedTimeNs);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800381 }
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800382
383 // Convention is to mark pull atom timestamp at request time.
384 // If we pull at t0, puller starts at t1, finishes at t2, and send back
385 // at t3, we mark t0 as its timestamp, which should correspond to its
386 // triggering event, such as condition change at t0.
387 // Here the triggering event is alarm fired from AlarmManager.
388 // In ValueMetricProducer and GaugeMetricProducer we do same thing
389 // when pull on condition change, etc.
390 for (auto& event : data) {
391 event->setElapsedTimestampNs(elapsedTimeNs);
392 event->setLogdWallClockTimestampNs(wallClockNs);
393 }
394
395 for (const auto& receiverInfo : pullInfo.second) {
396 sp<PullDataReceiver> receiverPtr = receiverInfo->receiver.promote();
397 if (receiverPtr != nullptr) {
Olivier Gaillard11203df2019-02-06 13:18:09 +0000398 receiverPtr->onDataPulled(data, pullSuccess, elapsedTimeNs);
Olivier Gaillardc5f11c42019-02-05 12:44:58 +0000399 // We may have just come out of a coma, compute next pull time.
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800400 int numBucketsAhead =
401 (elapsedTimeNs - receiverInfo->nextPullTimeNs) / receiverInfo->intervalNs;
402 receiverInfo->nextPullTimeNs += (numBucketsAhead + 1) * receiverInfo->intervalNs;
403 if (receiverInfo->nextPullTimeNs < minNextPullTimeNs) {
404 minNextPullTimeNs = receiverInfo->nextPullTimeNs;
Chenjie Yu6736c892017-11-09 10:50:09 -0800405 }
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800406 } else {
407 VLOG("receiver already gone.");
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700408 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700409 }
410 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700411
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700412 VLOG("mNextPullTimeNs: %lld updated to %lld", (long long)mNextPullTimeNs,
413 (long long)minNextPullTimeNs);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700414 mNextPullTimeNs = minNextPullTimeNs;
415 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700416}
417
Chenjie Yue2219202018-06-08 10:07:51 -0700418int StatsPullerManager::ForceClearPullerCache() {
Chenjie Yufa22d652018-02-05 14:37:48 -0800419 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800420 for (const auto& pulledAtom : kAllPullAtomInfo) {
421 totalCleared += pulledAtom.second.puller->ForceClearCache();
Chenjie Yue72252b2018-02-01 13:19:35 -0800422 }
Chenjie Yufa22d652018-02-05 14:37:48 -0800423 return totalCleared;
424}
425
Chenjie Yue2219202018-06-08 10:07:51 -0700426int StatsPullerManager::ClearPullerCacheIfNecessary(int64_t timestampNs) {
Chenjie Yufa22d652018-02-05 14:37:48 -0800427 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800428 for (const auto& pulledAtom : kAllPullAtomInfo) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700429 totalCleared += pulledAtom.second.puller->ClearCacheIfNecessary(timestampNs);
Chenjie Yufa22d652018-02-05 14:37:48 -0800430 }
431 return totalCleared;
Chenjie Yue72252b2018-02-01 13:19:35 -0800432}
433
Tej Singha0c89dd2019-01-25 16:39:18 -0800434void StatsPullerManager::RegisterPullerCallback(int32_t atomTag,
435 const sp<IStatsPullerCallback>& callback) {
436 AutoMutex _l(mLock);
437 // Platform pullers cannot be changed.
438 if (atomTag < StatsdStats::kMaxPlatformAtomTag) {
439 VLOG("RegisterPullerCallback: atom tag %d is less than min tag %d",
440 atomTag, StatsdStats::kMaxPlatformAtomTag);
441 return;
442 }
443 VLOG("RegisterPullerCallback: adding puller for tag %d", atomTag);
444 StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/true);
445 kAllPullAtomInfo[atomTag] = {.puller = new StatsCallbackPuller(atomTag, callback)};
446}
447
448void StatsPullerManager::UnregisterPullerCallback(int32_t atomTag) {
449 AutoMutex _l(mLock);
450 // Platform pullers cannot be changed.
451 if (atomTag < StatsdStats::kMaxPlatformAtomTag) {
452 return;
453 }
454 StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/false);
455 kAllPullAtomInfo.erase(atomTag);
456}
457
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700458} // namespace statsd
459} // namespace os
460} // namespace android