blob: 9b0fbef7066622f74a0d16da0d43cd9be374c6f0 [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>
David Chen1481fe12017-10-16 13:16:34 -070021#include <cutils/log.h>
Chenjie Yu1a0a9412018-03-28 10:07:22 -070022#include <math.h>
Chenjie Yu3b3adcd2018-04-18 16:25:36 -070023#include <stdint.h>
David Chen1481fe12017-10-16 13:16:34 -070024#include <algorithm>
Chenjie Yu1a0a9412018-03-28 10:07:22 -070025#include "../StatsService.h"
Chenjie Yuaa5b2012018-03-21 13:53:15 -070026#include "../logd/LogEvent.h"
27#include "../stats_log_util.h"
28#include "../statscompanion_util.h"
Bookatz92da2832018-11-01 18:10:03 -070029#include "PowerStatsPuller.h"
Tej Singh40298312018-02-16 00:15:09 -080030#include "ResourceHealthManagerPuller.h"
Chenjie Yu5305e1d2017-10-31 13:49:36 -070031#include "StatsCompanionServicePuller.h"
Chenjie Yue2219202018-06-08 10:07:51 -070032#include "StatsPullerManager.h"
Tej Singhbf972d92018-01-10 20:51:13 -080033#include "SubsystemSleepStatePuller.h"
Chenjie Yu5305e1d2017-10-31 13:49:36 -070034#include "statslog.h"
David Chen1481fe12017-10-16 13:16:34 -070035
36#include <iostream>
Chenjie Yu1a317ba2017-10-05 16:05:32 -070037
Yao Chen93fe3a32017-11-02 13:52:59 -070038using std::make_shared;
Chenjie Yu5305e1d2017-10-31 13:49:36 -070039using std::map;
Yao Chen93fe3a32017-11-02 13:52:59 -070040using std::shared_ptr;
Chenjie Yub3dda412017-10-24 13:41:59 -070041using std::string;
42using std::vector;
Chenjie Yu6736c892017-11-09 10:50:09 -080043using std::list;
Chenjie Yu1a317ba2017-10-05 16:05:32 -070044
45namespace android {
46namespace os {
47namespace statsd {
48
Chenjie Yu3b3adcd2018-04-18 16:25:36 -070049// Values smaller than this may require to update the alarm.
50const int64_t NO_ALARM_UPDATE = INT64_MAX;
51
Chenjie Yue2219202018-06-08 10:07:51 -070052const std::map<int, PullAtomInfo> StatsPullerManager::kAllPullAtomInfo = {
Chenjie Yufeba3092018-02-08 14:33:37 -080053 // wifi_bytes_transfer
54 {android::util::WIFI_BYTES_TRANSFER,
Chenjie Yu0bd73db2018-12-16 07:37:04 -080055 {.additiveFields = {2, 3, 4, 5},
56 .puller = new StatsCompanionServicePuller(android::util::WIFI_BYTES_TRANSFER)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080057 // wifi_bytes_transfer_by_fg_bg
58 {android::util::WIFI_BYTES_TRANSFER_BY_FG_BG,
Chenjie Yu0bd73db2018-12-16 07:37:04 -080059 {.additiveFields = {3, 4, 5, 6},
60 .puller = new StatsCompanionServicePuller(android::util::WIFI_BYTES_TRANSFER_BY_FG_BG)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080061 // mobile_bytes_transfer
62 {android::util::MOBILE_BYTES_TRANSFER,
Chenjie Yu0bd73db2018-12-16 07:37:04 -080063 {.additiveFields = {2, 3, 4, 5},
64 .puller = new StatsCompanionServicePuller(android::util::MOBILE_BYTES_TRANSFER)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080065 // mobile_bytes_transfer_by_fg_bg
66 {android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG,
Chenjie Yu0bd73db2018-12-16 07:37:04 -080067 {.additiveFields = {3, 4, 5, 6},
68 .puller =
69 new StatsCompanionServicePuller(android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080070 // bluetooth_bytes_transfer
71 {android::util::BLUETOOTH_BYTES_TRANSFER,
Chenjie Yu0bd73db2018-12-16 07:37:04 -080072 {.additiveFields = {2, 3},
73 .puller = new StatsCompanionServicePuller(android::util::BLUETOOTH_BYTES_TRANSFER)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080074 // kernel_wakelock
75 {android::util::KERNEL_WAKELOCK,
Chenjie Yu0bd73db2018-12-16 07:37:04 -080076 {.puller = new StatsCompanionServicePuller(android::util::KERNEL_WAKELOCK)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080077 // subsystem_sleep_state
Chenjie Yu0bd73db2018-12-16 07:37:04 -080078 {android::util::SUBSYSTEM_SLEEP_STATE, {.puller = new SubsystemSleepStatePuller()}},
Bookatz92da2832018-11-01 18:10:03 -070079 // on_device_power_measurement
Chenjie Yu0bd73db2018-12-16 07:37:04 -080080 {android::util::ON_DEVICE_POWER_MEASUREMENT, {.puller = new PowerStatsPuller()}},
Chenjie Yufeba3092018-02-08 14:33:37 -080081 // cpu_time_per_freq
82 {android::util::CPU_TIME_PER_FREQ,
Chenjie Yu0bd73db2018-12-16 07:37:04 -080083 {.additiveFields = {3},
84 .puller = new StatsCompanionServicePuller(android::util::CPU_TIME_PER_FREQ)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080085 // cpu_time_per_uid
Chenjie Yuec676612018-03-07 09:19:17 -080086 {android::util::CPU_TIME_PER_UID,
Chenjie Yu0bd73db2018-12-16 07:37:04 -080087 {.additiveFields = {2, 3},
88 .puller = new StatsCompanionServicePuller(android::util::CPU_TIME_PER_UID)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080089 // cpu_time_per_uid_freq
Chenjie Yu1a0a9412018-03-28 10:07:22 -070090 // the throttling is 3sec, handled in
91 // frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader
Chenjie Yuec676612018-03-07 09:19:17 -080092 {android::util::CPU_TIME_PER_UID_FREQ,
Chenjie Yu0bd73db2018-12-16 07:37:04 -080093 {.additiveFields = {4},
94 .puller = new StatsCompanionServicePuller(android::util::CPU_TIME_PER_UID_FREQ)}},
Chenjie Yuec676612018-03-07 09:19:17 -080095 // cpu_active_time
Chenjie Yu1a0a9412018-03-28 10:07:22 -070096 // the throttling is 3sec, handled in
97 // frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader
Chenjie Yuec676612018-03-07 09:19:17 -080098 {android::util::CPU_ACTIVE_TIME,
Chenjie Yu0bd73db2018-12-16 07:37:04 -080099 {.additiveFields = {2},
100 .puller = new StatsCompanionServicePuller(android::util::CPU_ACTIVE_TIME)}},
Chenjie Yuec676612018-03-07 09:19:17 -0800101 // cpu_cluster_time
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700102 // the throttling is 3sec, handled in
103 // frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader
Chenjie Yuec676612018-03-07 09:19:17 -0800104 {android::util::CPU_CLUSTER_TIME,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800105 {.additiveFields = {3},
106 .puller = new StatsCompanionServicePuller(android::util::CPU_CLUSTER_TIME)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800107 // wifi_activity_energy_info
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800108 {android::util::WIFI_ACTIVITY_INFO,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800109 {.puller = new StatsCompanionServicePuller(android::util::WIFI_ACTIVITY_INFO)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800110 // modem_activity_info
111 {android::util::MODEM_ACTIVITY_INFO,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800112 {.puller = new StatsCompanionServicePuller(android::util::MODEM_ACTIVITY_INFO)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800113 // bluetooth_activity_info
114 {android::util::BLUETOOTH_ACTIVITY_INFO,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800115 {.puller = new StatsCompanionServicePuller(android::util::BLUETOOTH_ACTIVITY_INFO)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800116 // system_elapsed_realtime
117 {android::util::SYSTEM_ELAPSED_REALTIME,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800118 {.pullTimeoutNs = NS_PER_SEC / 2,
119 .coolDownNs = NS_PER_SEC,
120 .puller = new StatsCompanionServicePuller(android::util::SYSTEM_ELAPSED_REALTIME)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800121 // system_uptime
122 {android::util::SYSTEM_UPTIME,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800123 {.puller = new StatsCompanionServicePuller(android::util::SYSTEM_UPTIME)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800124 // remaining_battery_capacity
125 {android::util::REMAINING_BATTERY_CAPACITY,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800126 {.puller = new ResourceHealthManagerPuller(android::util::REMAINING_BATTERY_CAPACITY)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800127 // full_battery_capacity
128 {android::util::FULL_BATTERY_CAPACITY,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800129 {.puller = new ResourceHealthManagerPuller(android::util::FULL_BATTERY_CAPACITY)}},
Bookatz17f0d8a2018-09-13 12:56:32 -0700130 // battery_voltage
131 {android::util::BATTERY_VOLTAGE,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800132 {.puller = new ResourceHealthManagerPuller(android::util::BATTERY_VOLTAGE)}},
Maggie White8735b852019-01-18 11:40:49 -0800133 // battery_level
Tej Singhb1dbc8b2018-11-19 15:49:47 -0800134 {android::util::BATTERY_LEVEL,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800135 {.puller = new ResourceHealthManagerPuller(android::util::BATTERY_LEVEL)}},
Maggie White8735b852019-01-18 11:40:49 -0800136 // battery_cycle_count
137 {android::util::BATTERY_CYCLE_COUNT,
138 {.puller = new ResourceHealthManagerPuller(android::util::BATTERY_CYCLE_COUNT)}},
Rajeev Kumar22d92b72018-02-07 18:38:36 -0800139 // process_memory_state
140 {android::util::PROCESS_MEMORY_STATE,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800141 {.additiveFields = {4, 5, 6, 7, 8, 9},
142 .puller = new StatsCompanionServicePuller(android::util::PROCESS_MEMORY_STATE)}},
Rafal Slawik08621582018-10-15 14:53:07 +0100143 // native_process_memory_state
144 {android::util::NATIVE_PROCESS_MEMORY_STATE,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800145 {.additiveFields = {3, 4, 5, 6},
146 .puller = new StatsCompanionServicePuller(android::util::NATIVE_PROCESS_MEMORY_STATE)}},
Rafal Slawik3bea8952018-11-15 12:39:33 +0000147 {android::util::PROCESS_MEMORY_HIGH_WATER_MARK,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800148 {.additiveFields = {3},
149 .puller =
150 new StatsCompanionServicePuller(android::util::PROCESS_MEMORY_HIGH_WATER_MARK)}},
Tej Singh40298312018-02-16 00:15:09 -0800151 // temperature
Tej Singh9876a3b2019-01-11 14:28:35 -0800152 {android::util::TEMPERATURE,
153 {.puller = new StatsCompanionServicePuller(android::util::TEMPERATURE)}},
Olivier Gaillard00bfb1b2018-07-10 11:25:09 +0100154 // binder_calls
155 {android::util::BINDER_CALLS,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800156 {.additiveFields = {4, 5, 6, 8, 12},
157 .puller = new StatsCompanionServicePuller(android::util::BINDER_CALLS)}},
Olivier Gaillard6f52d152018-07-25 12:13:12 +0100158 // binder_calls_exceptions
159 {android::util::BINDER_CALLS_EXCEPTIONS,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800160 {.puller = new StatsCompanionServicePuller(android::util::BINDER_CALLS_EXCEPTIONS)}},
Marcin Oczeretkod8cc8592018-08-22 16:07:36 +0100161 // looper_stats
162 {android::util::LOOPER_STATS,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800163 {.additiveFields = {5, 6, 7, 8, 9},
164 .puller = new StatsCompanionServicePuller(android::util::LOOPER_STATS)}},
Tej Singh86dc9db2018-09-06 00:39:57 +0000165 // Disk Stats
166 {android::util::DISK_STATS,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800167 {.puller = new StatsCompanionServicePuller(android::util::DISK_STATS)}},
Tej Singh86dc9db2018-09-06 00:39:57 +0000168 // Directory usage
169 {android::util::DIRECTORY_USAGE,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800170 {.puller = new StatsCompanionServicePuller(android::util::DIRECTORY_USAGE)}},
Tej Singh86dc9db2018-09-06 00:39:57 +0000171 // Size of app's code, data, and cache
172 {android::util::APP_SIZE,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800173 {.puller = new StatsCompanionServicePuller(android::util::APP_SIZE)}},
Tej Singh86dc9db2018-09-06 00:39:57 +0000174 // Size of specific categories of files. Eg. Music.
175 {android::util::CATEGORY_SIZE,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800176 {.puller = new StatsCompanionServicePuller(android::util::CATEGORY_SIZE)}},
Tej Singhd6d6d772018-09-05 17:41:25 -0700177 // Number of fingerprints registered to each user.
Kevin Chyn1741a072019-01-17 11:54:40 -0800178 {android::util::NUM_FINGERPRINTS_ENROLLED,
179 {.puller = new StatsCompanionServicePuller(android::util::NUM_FINGERPRINTS_ENROLLED)}},
Chenjie Yu2c0d83d2018-09-02 06:37:08 -0700180 // ProcStats.
181 {android::util::PROC_STATS,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800182 {.puller = new StatsCompanionServicePuller(android::util::PROC_STATS)}},
Chenjie Yub52779e2018-10-05 12:03:36 -0700183 // ProcStatsPkgProc.
184 {android::util::PROC_STATS_PKG_PROC,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800185 {.puller = new StatsCompanionServicePuller(android::util::PROC_STATS_PKG_PROC)}},
Tej Singhe7726dc2018-09-21 11:42:12 -0700186 // Disk I/O stats per uid.
187 {android::util::DISK_IO,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800188 {.additiveFields = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
189 .coolDownNs = 3 * NS_PER_SEC,
190 .puller = new StatsCompanionServicePuller(android::util::DISK_IO)}},
Chenjie Yuab530202018-09-26 12:39:20 -0700191 // PowerProfile constants for power model calculations.
192 {android::util::POWER_PROFILE,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800193 {.puller = new StatsCompanionServicePuller(android::util::POWER_PROFILE)}},
Yangster-mac308ea0c2018-10-22 13:10:25 -0700194 // Process cpu stats. Min cool-down is 5 sec, inline with what AcitivityManagerService uses.
195 {android::util::PROCESS_CPU_TIME,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800196 {.coolDownNs = 5 * NS_PER_SEC /* min cool-down in seconds*/,
197 .puller = new StatsCompanionServicePuller(android::util::PROCESS_CPU_TIME)}},
Misha Wagner5a51e002018-10-03 15:04:09 +0100198 {android::util::CPU_TIME_PER_THREAD_FREQ,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800199 {.additiveFields = {7, 9, 11, 13, 15, 17, 19, 21},
200 .puller = new StatsCompanionServicePuller(android::util::CPU_TIME_PER_THREAD_FREQ)}},
Bookatz75ee6042018-11-09 12:27:37 -0800201 // DeviceCalculatedPowerUse.
202 {android::util::DEVICE_CALCULATED_POWER_USE,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800203 {.puller = new StatsCompanionServicePuller(android::util::DEVICE_CALCULATED_POWER_USE)}},
Bookatz75ee6042018-11-09 12:27:37 -0800204 // DeviceCalculatedPowerBlameUid.
205 {android::util::DEVICE_CALCULATED_POWER_BLAME_UID,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800206 {.puller = new StatsCompanionServicePuller(
207 android::util::DEVICE_CALCULATED_POWER_BLAME_UID)}},
Bookatz75ee6042018-11-09 12:27:37 -0800208 // DeviceCalculatedPowerBlameOther.
209 {android::util::DEVICE_CALCULATED_POWER_BLAME_OTHER,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800210 {.puller = new StatsCompanionServicePuller(
211 android::util::DEVICE_CALCULATED_POWER_BLAME_OTHER)}},
Olivier Gaillard18d452202019-01-22 15:03:41 +0000212 // DebugElapsedClock.
213 {android::util::DEBUG_ELAPSED_CLOCK,
214 {.additiveFields = {1, 2, 3, 4},
215 .puller = new StatsCompanionServicePuller(android::util::DEBUG_ELAPSED_CLOCK)}},
216 // DebugFailingElapsedClock.
217 {android::util::DEBUG_FAILING_ELAPSED_CLOCK,
218 {.additiveFields = {1, 2, 3, 4},
219 .puller = new StatsCompanionServicePuller(android::util::DEBUG_FAILING_ELAPSED_CLOCK)}},
Bookatz366a4432018-11-20 09:42:33 -0800220 // BuildInformation.
221 {android::util::BUILD_INFORMATION,
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800222 {.puller = new StatsCompanionServicePuller(android::util::BUILD_INFORMATION)}},
Philip P. Moltmann70b42ae2019-01-29 16:24:19 -0800223 // RoleHolder.
224 {android::util::ROLE_HOLDER,
225 {.puller = new StatsCompanionServicePuller(android::util::ROLE_HOLDER)}},
Chenjie Yu2c0d83d2018-09-02 06:37:08 -0700226};
Chenjie Yu80f91122018-01-31 20:24:50 -0800227
Chenjie Yue2219202018-06-08 10:07:51 -0700228StatsPullerManager::StatsPullerManager() : mNextPullTimeNs(NO_ALARM_UPDATE) {
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700229}
230
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800231bool StatsPullerManager::Pull(int tagId, vector<shared_ptr<LogEvent>>* data) {
Tej Singh484524a2018-02-01 15:10:05 -0800232 VLOG("Initiating pulling %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700233
Chenjie Yu80f91122018-01-31 20:24:50 -0800234 if (kAllPullAtomInfo.find(tagId) != kAllPullAtomInfo.end()) {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800235 bool ret = kAllPullAtomInfo.find(tagId)->second.puller->Pull(data);
Tej Singh484524a2018-02-01 15:10:05 -0800236 VLOG("pulled %d items", (int)data->size());
237 return ret;
Yao Chen93fe3a32017-11-02 13:52:59 -0700238 } else {
Tej Singh484524a2018-02-01 15:10:05 -0800239 VLOG("Unknown tagId %d", tagId);
Yao Chen93fe3a32017-11-02 13:52:59 -0700240 return false; // Return early since we don't know what to pull.
241 }
242}
Chenjie Yub3dda412017-10-24 13:41:59 -0700243
Chenjie Yue2219202018-06-08 10:07:51 -0700244bool StatsPullerManager::PullerForMatcherExists(int tagId) const {
Chenjie Yufeba3092018-02-08 14:33:37 -0800245 return kAllPullAtomInfo.find(tagId) != kAllPullAtomInfo.end();
Chenjie Yub3dda412017-10-24 13:41:59 -0700246}
247
Chenjie Yue2219202018-06-08 10:07:51 -0700248void StatsPullerManager::updateAlarmLocked() {
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700249 if (mNextPullTimeNs == NO_ALARM_UPDATE) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700250 VLOG("No need to set alarms. Skipping");
251 return;
252 }
253
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700254 sp<IStatsCompanionService> statsCompanionServiceCopy = mStatsCompanionService;
255 if (statsCompanionServiceCopy != nullptr) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700256 statsCompanionServiceCopy->setPullingAlarm(mNextPullTimeNs / 1000000);
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700257 } else {
258 VLOG("StatsCompanionService not available. Alarm not set.");
259 }
260 return;
261}
262
Chenjie Yue2219202018-06-08 10:07:51 -0700263void StatsPullerManager::SetStatsCompanionService(
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700264 sp<IStatsCompanionService> statsCompanionService) {
265 AutoMutex _l(mLock);
266 sp<IStatsCompanionService> tmpForLock = mStatsCompanionService;
267 mStatsCompanionService = statsCompanionService;
268 for (const auto& pulledAtom : kAllPullAtomInfo) {
269 pulledAtom.second.puller->SetStatsCompanionService(statsCompanionService);
270 }
271 if (mStatsCompanionService != nullptr) {
272 updateAlarmLocked();
273 }
274}
275
Chenjie Yue2219202018-06-08 10:07:51 -0700276void StatsPullerManager::RegisterReceiver(int tagId, wp<PullDataReceiver> receiver,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700277 int64_t nextPullTimeNs, int64_t intervalNs) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700278 AutoMutex _l(mLock);
Chenjie Yu6736c892017-11-09 10:50:09 -0800279 auto& receivers = mReceivers[tagId];
Chenjie Yub3dda412017-10-24 13:41:59 -0700280 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800281 if (it->receiver == receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700282 VLOG("Receiver already registered of %d", (int)receivers.size());
283 return;
284 }
285 }
286 ReceiverInfo receiverInfo;
287 receiverInfo.receiver = receiver;
Chenjie Yub3dda412017-10-24 13:41:59 -0700288
Chenjie Yu85ed8382017-12-14 16:48:54 -0800289 // Round it to the nearest minutes. This is the limit of alarm manager.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700290 // In practice, we should always have larger buckets.
291 int64_t roundedIntervalNs = intervalNs / NS_PER_SEC / 60 * NS_PER_SEC * 60;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700292 // Scheduled pulling should be at least 1 min apart.
293 // This can be lower in cts tests, in which case we round it to 1 min.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700294 if (roundedIntervalNs < 60 * (int64_t)NS_PER_SEC) {
295 roundedIntervalNs = 60 * (int64_t)NS_PER_SEC;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700296 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700297
298 receiverInfo.intervalNs = roundedIntervalNs;
299 receiverInfo.nextPullTimeNs = nextPullTimeNs;
300 receivers.push_back(receiverInfo);
301
Chenjie Yub3dda412017-10-24 13:41:59 -0700302 // There is only one alarm for all pulled events. So only set it to the smallest denom.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700303 if (nextPullTimeNs < mNextPullTimeNs) {
304 VLOG("Updating next pull time %lld", (long long)mNextPullTimeNs);
305 mNextPullTimeNs = nextPullTimeNs;
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700306 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700307 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700308 VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700309}
310
Chenjie Yue2219202018-06-08 10:07:51 -0700311void StatsPullerManager::UnRegisterReceiver(int tagId, wp<PullDataReceiver> receiver) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700312 AutoMutex _l(mLock);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700313 if (mReceivers.find(tagId) == mReceivers.end()) {
314 VLOG("Unknown pull code or no receivers: %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700315 return;
316 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700317 auto& receivers = mReceivers.find(tagId)->second;
Chenjie Yub3dda412017-10-24 13:41:59 -0700318 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800319 if (receiver == it->receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700320 receivers.erase(it);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700321 VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700322 return;
323 }
324 }
325}
326
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800327void StatsPullerManager::OnAlarmFired(int64_t elapsedTimeNs) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700328 AutoMutex _l(mLock);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800329 int64_t wallClockNs = getWallClockNs();
Chenjie Yub3dda412017-10-24 13:41:59 -0700330
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700331 int64_t minNextPullTimeNs = NO_ALARM_UPDATE;
Chenjie Yub3dda412017-10-24 13:41:59 -0700332
333 vector<pair<int, vector<ReceiverInfo*>>> needToPull =
334 vector<pair<int, vector<ReceiverInfo*>>>();
335 for (auto& pair : mReceivers) {
336 vector<ReceiverInfo*> receivers = vector<ReceiverInfo*>();
Yao Chen93fe3a32017-11-02 13:52:59 -0700337 if (pair.second.size() != 0) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700338 for (ReceiverInfo& receiverInfo : pair.second) {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800339 if (receiverInfo.nextPullTimeNs <= elapsedTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700340 receivers.push_back(&receiverInfo);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700341 } else {
342 if (receiverInfo.nextPullTimeNs < minNextPullTimeNs) {
343 minNextPullTimeNs = receiverInfo.nextPullTimeNs;
344 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700345 }
346 }
347 if (receivers.size() > 0) {
348 needToPull.push_back(make_pair(pair.first, receivers));
349 }
350 }
351 }
352
353 for (const auto& pullInfo : needToPull) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700354 vector<shared_ptr<LogEvent>> data;
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800355 if (!Pull(pullInfo.first, &data)) {
356 VLOG("pull failed at %lld, will try again later", (long long)elapsedTimeNs);
357 continue;
358 }
359 StatsdStats::getInstance().notePullDelay(pullInfo.first,
360 getElapsedRealtimeNs() - elapsedTimeNs);
361
362 // Convention is to mark pull atom timestamp at request time.
363 // If we pull at t0, puller starts at t1, finishes at t2, and send back
364 // at t3, we mark t0 as its timestamp, which should correspond to its
365 // triggering event, such as condition change at t0.
366 // Here the triggering event is alarm fired from AlarmManager.
367 // In ValueMetricProducer and GaugeMetricProducer we do same thing
368 // when pull on condition change, etc.
369 for (auto& event : data) {
370 event->setElapsedTimestampNs(elapsedTimeNs);
371 event->setLogdWallClockTimestampNs(wallClockNs);
372 }
373
374 for (const auto& receiverInfo : pullInfo.second) {
375 sp<PullDataReceiver> receiverPtr = receiverInfo->receiver.promote();
376 if (receiverPtr != nullptr) {
377 receiverPtr->onDataPulled(data);
378 // we may have just come out of a coma, compute next pull time
379 int numBucketsAhead =
380 (elapsedTimeNs - receiverInfo->nextPullTimeNs) / receiverInfo->intervalNs;
381 receiverInfo->nextPullTimeNs += (numBucketsAhead + 1) * receiverInfo->intervalNs;
382 if (receiverInfo->nextPullTimeNs < minNextPullTimeNs) {
383 minNextPullTimeNs = receiverInfo->nextPullTimeNs;
Chenjie Yu6736c892017-11-09 10:50:09 -0800384 }
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800385 } else {
386 VLOG("receiver already gone.");
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700387 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700388 }
389 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700390
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700391 VLOG("mNextPullTimeNs: %lld updated to %lld", (long long)mNextPullTimeNs,
392 (long long)minNextPullTimeNs);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700393 mNextPullTimeNs = minNextPullTimeNs;
394 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700395}
396
Chenjie Yue2219202018-06-08 10:07:51 -0700397int StatsPullerManager::ForceClearPullerCache() {
Chenjie Yufa22d652018-02-05 14:37:48 -0800398 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800399 for (const auto& pulledAtom : kAllPullAtomInfo) {
400 totalCleared += pulledAtom.second.puller->ForceClearCache();
Chenjie Yue72252b2018-02-01 13:19:35 -0800401 }
Chenjie Yufa22d652018-02-05 14:37:48 -0800402 return totalCleared;
403}
404
Chenjie Yue2219202018-06-08 10:07:51 -0700405int StatsPullerManager::ClearPullerCacheIfNecessary(int64_t timestampNs) {
Chenjie Yufa22d652018-02-05 14:37:48 -0800406 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800407 for (const auto& pulledAtom : kAllPullAtomInfo) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700408 totalCleared += pulledAtom.second.puller->ClearCacheIfNecessary(timestampNs);
Chenjie Yufa22d652018-02-05 14:37:48 -0800409 }
410 return totalCleared;
Chenjie Yue72252b2018-02-01 13:19:35 -0800411}
412
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700413} // namespace statsd
414} // namespace os
415} // namespace android