blob: dab64cacb67903b030ec3e1c26a2c6b3a938c643 [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"
Tej Singh40298312018-02-16 00:15:09 -080029#include "ResourceHealthManagerPuller.h"
30#include "ResourceThermalManagerPuller.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 Yu1a0a9412018-03-28 10:07:22 -070055 {{2, 3, 4, 5},
56 {},
57 1 * NS_PER_SEC,
Chenjie Yufeba3092018-02-08 14:33:37 -080058 new StatsCompanionServicePuller(android::util::WIFI_BYTES_TRANSFER)}},
59 // wifi_bytes_transfer_by_fg_bg
60 {android::util::WIFI_BYTES_TRANSFER_BY_FG_BG,
Chenjie Yu1a0a9412018-03-28 10:07:22 -070061 {{3, 4, 5, 6},
62 {2},
63 1 * NS_PER_SEC,
Chenjie Yufeba3092018-02-08 14:33:37 -080064 new StatsCompanionServicePuller(android::util::WIFI_BYTES_TRANSFER_BY_FG_BG)}},
65 // mobile_bytes_transfer
66 {android::util::MOBILE_BYTES_TRANSFER,
Chenjie Yu1a0a9412018-03-28 10:07:22 -070067 {{2, 3, 4, 5},
68 {},
69 1 * NS_PER_SEC,
Chenjie Yufeba3092018-02-08 14:33:37 -080070 new StatsCompanionServicePuller(android::util::MOBILE_BYTES_TRANSFER)}},
71 // mobile_bytes_transfer_by_fg_bg
72 {android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG,
Chenjie Yu1a0a9412018-03-28 10:07:22 -070073 {{3, 4, 5, 6},
74 {2},
75 1 * NS_PER_SEC,
Chenjie Yufeba3092018-02-08 14:33:37 -080076 new StatsCompanionServicePuller(android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG)}},
77 // bluetooth_bytes_transfer
78 {android::util::BLUETOOTH_BYTES_TRANSFER,
Chenjie Yu1a0a9412018-03-28 10:07:22 -070079 {{2, 3},
80 {},
81 1 * NS_PER_SEC,
82 new StatsCompanionServicePuller(android::util::BLUETOOTH_BYTES_TRANSFER)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080083 // kernel_wakelock
84 {android::util::KERNEL_WAKELOCK,
Chenjie Yu1a0a9412018-03-28 10:07:22 -070085 {{}, {}, 1 * NS_PER_SEC, new StatsCompanionServicePuller(android::util::KERNEL_WAKELOCK)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080086 // subsystem_sleep_state
Chenjie Yuec676612018-03-07 09:19:17 -080087 {android::util::SUBSYSTEM_SLEEP_STATE,
Chenjie Yu1a0a9412018-03-28 10:07:22 -070088 {{}, {}, 1 * NS_PER_SEC, new SubsystemSleepStatePuller()}},
Chenjie Yufeba3092018-02-08 14:33:37 -080089 // cpu_time_per_freq
90 {android::util::CPU_TIME_PER_FREQ,
Chenjie Yu1a0a9412018-03-28 10:07:22 -070091 {{3},
92 {2},
93 1 * NS_PER_SEC,
94 new StatsCompanionServicePuller(android::util::CPU_TIME_PER_FREQ)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080095 // cpu_time_per_uid
Chenjie Yuec676612018-03-07 09:19:17 -080096 {android::util::CPU_TIME_PER_UID,
Chenjie Yu1a0a9412018-03-28 10:07:22 -070097 {{2, 3},
98 {},
99 1 * NS_PER_SEC,
100 new StatsCompanionServicePuller(android::util::CPU_TIME_PER_UID)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800101 // cpu_time_per_uid_freq
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_TIME_PER_UID_FREQ,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700105 {{4},
106 {2, 3},
107 1 * NS_PER_SEC,
108 new StatsCompanionServicePuller(android::util::CPU_TIME_PER_UID_FREQ)}},
Chenjie Yuec676612018-03-07 09:19:17 -0800109 // cpu_active_time
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700110 // the throttling is 3sec, handled in
111 // frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader
Chenjie Yuec676612018-03-07 09:19:17 -0800112 {android::util::CPU_ACTIVE_TIME,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700113 {{2},
114 {},
115 1 * NS_PER_SEC,
116 new StatsCompanionServicePuller(android::util::CPU_ACTIVE_TIME)}},
Chenjie Yuec676612018-03-07 09:19:17 -0800117 // cpu_cluster_time
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700118 // the throttling is 3sec, handled in
119 // frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader
Chenjie Yuec676612018-03-07 09:19:17 -0800120 {android::util::CPU_CLUSTER_TIME,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700121 {{3},
122 {2},
123 1 * NS_PER_SEC,
124 new StatsCompanionServicePuller(android::util::CPU_CLUSTER_TIME)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800125 // wifi_activity_energy_info
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800126 {android::util::WIFI_ACTIVITY_INFO,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700127 {{},
128 {},
129 1 * NS_PER_SEC,
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800130 new StatsCompanionServicePuller(android::util::WIFI_ACTIVITY_INFO)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800131 // modem_activity_info
132 {android::util::MODEM_ACTIVITY_INFO,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700133 {{},
134 {},
135 1 * NS_PER_SEC,
136 new StatsCompanionServicePuller(android::util::MODEM_ACTIVITY_INFO)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800137 // bluetooth_activity_info
138 {android::util::BLUETOOTH_ACTIVITY_INFO,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700139 {{},
140 {},
141 1 * NS_PER_SEC,
142 new StatsCompanionServicePuller(android::util::BLUETOOTH_ACTIVITY_INFO)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800143 // system_elapsed_realtime
144 {android::util::SYSTEM_ELAPSED_REALTIME,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700145 {{},
146 {},
147 1 * NS_PER_SEC,
148 new StatsCompanionServicePuller(android::util::SYSTEM_ELAPSED_REALTIME)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800149 // system_uptime
150 {android::util::SYSTEM_UPTIME,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700151 {{}, {}, 1 * NS_PER_SEC, new StatsCompanionServicePuller(android::util::SYSTEM_UPTIME)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800152 // remaining_battery_capacity
153 {android::util::REMAINING_BATTERY_CAPACITY,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700154 {{},
155 {},
156 1 * NS_PER_SEC,
157 new ResourceHealthManagerPuller(android::util::REMAINING_BATTERY_CAPACITY)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800158 // full_battery_capacity
159 {android::util::FULL_BATTERY_CAPACITY,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700160 {{},
161 {},
162 1 * NS_PER_SEC,
163 new ResourceHealthManagerPuller(android::util::FULL_BATTERY_CAPACITY)}},
Bookatz17f0d8a2018-09-13 12:56:32 -0700164 // battery_voltage
165 {android::util::BATTERY_VOLTAGE,
Chenjie Yu2c0d83d2018-09-02 06:37:08 -0700166 {{}, {}, 1 * NS_PER_SEC, new ResourceHealthManagerPuller(android::util::BATTERY_VOLTAGE)}},
Rajeev Kumar22d92b72018-02-07 18:38:36 -0800167 // process_memory_state
168 {android::util::PROCESS_MEMORY_STATE,
Rafal Slawikaaf60892018-09-12 13:04:30 +0100169 {{4, 5, 6, 7, 8, 9},
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700170 {2, 3},
171 1 * NS_PER_SEC,
Chenjie Yuec676612018-03-07 09:19:17 -0800172 new StatsCompanionServicePuller(android::util::PROCESS_MEMORY_STATE)}},
Tej Singh40298312018-02-16 00:15:09 -0800173 // temperature
Wei Wangf0f01172018-09-13 16:16:02 -0700174 {android::util::TEMPERATURE, {{}, {}, 1 * NS_PER_SEC, new ResourceThermalManagerPuller()}},
Olivier Gaillard00bfb1b2018-07-10 11:25:09 +0100175 // binder_calls
176 {android::util::BINDER_CALLS,
Olivier Gaillard6f52d152018-07-25 12:13:12 +0100177 {{4, 5, 6, 8, 12},
178 {2, 3, 7, 9, 10, 11, 13},
Olivier Gaillard00bfb1b2018-07-10 11:25:09 +0100179 1 * NS_PER_SEC,
Olivier Gaillard6f52d152018-07-25 12:13:12 +0100180 new StatsCompanionServicePuller(android::util::BINDER_CALLS)}},
181 // binder_calls_exceptions
182 {android::util::BINDER_CALLS_EXCEPTIONS,
183 {{},
184 {},
185 1 * NS_PER_SEC,
Marcin Oczeretkod8cc8592018-08-22 16:07:36 +0100186 new StatsCompanionServicePuller(android::util::BINDER_CALLS_EXCEPTIONS)}},
187 // looper_stats
188 {android::util::LOOPER_STATS,
189 {{5, 6, 7, 8, 9},
Marcin Oczeretko3e6494e2018-09-10 18:06:52 +0100190 {2, 3, 4, 10},
Marcin Oczeretkod8cc8592018-08-22 16:07:36 +0100191 1 * NS_PER_SEC,
Tej Singh86dc9db2018-09-06 00:39:57 +0000192 new StatsCompanionServicePuller(android::util::LOOPER_STATS)}},
193 // Disk Stats
194 {android::util::DISK_STATS,
Chenjie Yu12e5e672018-09-14 15:54:59 -0700195 {{}, {}, 1 * NS_PER_SEC, new StatsCompanionServicePuller(android::util::DISK_STATS)}},
Tej Singh86dc9db2018-09-06 00:39:57 +0000196 // Directory usage
197 {android::util::DIRECTORY_USAGE,
Chenjie Yu12e5e672018-09-14 15:54:59 -0700198 {{}, {}, 1 * NS_PER_SEC, new StatsCompanionServicePuller(android::util::DIRECTORY_USAGE)}},
Tej Singh86dc9db2018-09-06 00:39:57 +0000199 // Size of app's code, data, and cache
200 {android::util::APP_SIZE,
Chenjie Yu12e5e672018-09-14 15:54:59 -0700201 {{}, {}, 1 * NS_PER_SEC, new StatsCompanionServicePuller(android::util::APP_SIZE)}},
Tej Singh86dc9db2018-09-06 00:39:57 +0000202 // Size of specific categories of files. Eg. Music.
203 {android::util::CATEGORY_SIZE,
Chenjie Yu2c0d83d2018-09-02 06:37:08 -0700204 {{}, {}, 1 * NS_PER_SEC, new StatsCompanionServicePuller(android::util::CATEGORY_SIZE)}},
Tej Singhd6d6d772018-09-05 17:41:25 -0700205 // Number of fingerprints registered to each user.
206 {android::util::NUM_FINGERPRINTS,
207 {{},
208 {},
209 1 * NS_PER_SEC,
210 new StatsCompanionServicePuller(android::util::NUM_FINGERPRINTS)}},
Chenjie Yu2c0d83d2018-09-02 06:37:08 -0700211 // ProcStats.
212 {android::util::PROC_STATS,
213 {{}, {}, 1 * NS_PER_SEC, new StatsCompanionServicePuller(android::util::PROC_STATS)}},
Chenjie Yub52779e2018-10-05 12:03:36 -0700214 // ProcStatsPkgProc.
215 {android::util::PROC_STATS_PKG_PROC,
216 {{}, {}, 1 * NS_PER_SEC, new StatsCompanionServicePuller(android::util::PROC_STATS_PKG_PROC)}},
Tej Singhe7726dc2018-09-21 11:42:12 -0700217 // Disk I/O stats per uid.
218 {android::util::DISK_IO,
219 {{2,3,4,5,6,7,8,9,10,11},
220 {},
221 3 * NS_PER_SEC,
222 new StatsCompanionServicePuller(android::util::DISK_IO)}},
Chenjie Yuab530202018-09-26 12:39:20 -0700223 // PowerProfile constants for power model calculations.
224 {android::util::POWER_PROFILE,
225 {{}, {}, 1 * NS_PER_SEC, new StatsCompanionServicePuller(android::util::POWER_PROFILE)}},
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 Yue2219202018-06-08 10:07:51 -0700231bool StatsPullerManager::Pull(const int tagId, const int64_t timeNs,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700232 vector<shared_ptr<LogEvent>>* data) {
Tej Singh484524a2018-02-01 15:10:05 -0800233 VLOG("Initiating pulling %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700234
Chenjie Yu80f91122018-01-31 20:24:50 -0800235 if (kAllPullAtomInfo.find(tagId) != kAllPullAtomInfo.end()) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700236 bool ret = kAllPullAtomInfo.find(tagId)->second.puller->Pull(timeNs, data);
Tej Singh484524a2018-02-01 15:10:05 -0800237 VLOG("pulled %d items", (int)data->size());
238 return ret;
Yao Chen93fe3a32017-11-02 13:52:59 -0700239 } else {
Tej Singh484524a2018-02-01 15:10:05 -0800240 VLOG("Unknown tagId %d", tagId);
Yao Chen93fe3a32017-11-02 13:52:59 -0700241 return false; // Return early since we don't know what to pull.
242 }
243}
Chenjie Yub3dda412017-10-24 13:41:59 -0700244
Chenjie Yue2219202018-06-08 10:07:51 -0700245bool StatsPullerManager::PullerForMatcherExists(int tagId) const {
Chenjie Yufeba3092018-02-08 14:33:37 -0800246 return kAllPullAtomInfo.find(tagId) != kAllPullAtomInfo.end();
Chenjie Yub3dda412017-10-24 13:41:59 -0700247}
248
Chenjie Yue2219202018-06-08 10:07:51 -0700249void StatsPullerManager::updateAlarmLocked() {
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700250 if (mNextPullTimeNs == NO_ALARM_UPDATE) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700251 VLOG("No need to set alarms. Skipping");
252 return;
253 }
254
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700255 sp<IStatsCompanionService> statsCompanionServiceCopy = mStatsCompanionService;
256 if (statsCompanionServiceCopy != nullptr) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700257 statsCompanionServiceCopy->setPullingAlarm(mNextPullTimeNs / 1000000);
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700258 } else {
259 VLOG("StatsCompanionService not available. Alarm not set.");
260 }
261 return;
262}
263
Chenjie Yue2219202018-06-08 10:07:51 -0700264void StatsPullerManager::SetStatsCompanionService(
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700265 sp<IStatsCompanionService> statsCompanionService) {
266 AutoMutex _l(mLock);
267 sp<IStatsCompanionService> tmpForLock = mStatsCompanionService;
268 mStatsCompanionService = statsCompanionService;
269 for (const auto& pulledAtom : kAllPullAtomInfo) {
270 pulledAtom.second.puller->SetStatsCompanionService(statsCompanionService);
271 }
272 if (mStatsCompanionService != nullptr) {
273 updateAlarmLocked();
274 }
275}
276
Chenjie Yue2219202018-06-08 10:07:51 -0700277void StatsPullerManager::RegisterReceiver(int tagId, wp<PullDataReceiver> receiver,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700278 int64_t nextPullTimeNs, int64_t intervalNs) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700279 AutoMutex _l(mLock);
Chenjie Yu6736c892017-11-09 10:50:09 -0800280 auto& receivers = mReceivers[tagId];
Chenjie Yub3dda412017-10-24 13:41:59 -0700281 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800282 if (it->receiver == receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700283 VLOG("Receiver already registered of %d", (int)receivers.size());
284 return;
285 }
286 }
287 ReceiverInfo receiverInfo;
288 receiverInfo.receiver = receiver;
Chenjie Yub3dda412017-10-24 13:41:59 -0700289
Chenjie Yu85ed8382017-12-14 16:48:54 -0800290 // Round it to the nearest minutes. This is the limit of alarm manager.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700291 // In practice, we should always have larger buckets.
292 int64_t roundedIntervalNs = intervalNs / NS_PER_SEC / 60 * NS_PER_SEC * 60;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700293 // Scheduled pulling should be at least 1 min apart.
294 // This can be lower in cts tests, in which case we round it to 1 min.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700295 if (roundedIntervalNs < 60 * (int64_t)NS_PER_SEC) {
296 roundedIntervalNs = 60 * (int64_t)NS_PER_SEC;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700297 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700298
299 receiverInfo.intervalNs = roundedIntervalNs;
300 receiverInfo.nextPullTimeNs = nextPullTimeNs;
301 receivers.push_back(receiverInfo);
302
Chenjie Yub3dda412017-10-24 13:41:59 -0700303 // There is only one alarm for all pulled events. So only set it to the smallest denom.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700304 if (nextPullTimeNs < mNextPullTimeNs) {
305 VLOG("Updating next pull time %lld", (long long)mNextPullTimeNs);
306 mNextPullTimeNs = nextPullTimeNs;
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700307 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700308 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700309 VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700310}
311
Chenjie Yue2219202018-06-08 10:07:51 -0700312void StatsPullerManager::UnRegisterReceiver(int tagId, wp<PullDataReceiver> receiver) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700313 AutoMutex _l(mLock);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700314 if (mReceivers.find(tagId) == mReceivers.end()) {
315 VLOG("Unknown pull code or no receivers: %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700316 return;
317 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700318 auto& receivers = mReceivers.find(tagId)->second;
Chenjie Yub3dda412017-10-24 13:41:59 -0700319 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800320 if (receiver == it->receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700321 receivers.erase(it);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700322 VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700323 return;
324 }
325 }
326}
327
Chenjie Yue2219202018-06-08 10:07:51 -0700328void StatsPullerManager::OnAlarmFired(const int64_t currentTimeNs) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700329 AutoMutex _l(mLock);
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) {
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700339 if (receiverInfo.nextPullTimeNs <= currentTimeNs) {
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 Yu1a0a9412018-03-28 10:07:22 -0700355 if (Pull(pullInfo.first, currentTimeNs, &data)) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700356 for (const auto& receiverInfo : pullInfo.second) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800357 sp<PullDataReceiver> receiverPtr = receiverInfo->receiver.promote();
358 if (receiverPtr != nullptr) {
359 receiverPtr->onDataPulled(data);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700360 // we may have just come out of a coma, compute next pull time
361 receiverInfo->nextPullTimeNs =
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700362 (currentTimeNs - receiverInfo->nextPullTimeNs) /
363 receiverInfo->intervalNs * receiverInfo->intervalNs +
364 receiverInfo->intervalNs + receiverInfo->nextPullTimeNs;
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700365 if (receiverInfo->nextPullTimeNs < minNextPullTimeNs) {
366 minNextPullTimeNs = receiverInfo->nextPullTimeNs;
367 }
Chenjie Yu6736c892017-11-09 10:50:09 -0800368 } else {
369 VLOG("receiver already gone.");
370 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700371 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700372 }
373 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700374
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700375 VLOG("mNextPullTimeNs: %lld updated to %lld", (long long)mNextPullTimeNs,
376 (long long)minNextPullTimeNs);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700377 mNextPullTimeNs = minNextPullTimeNs;
378 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700379}
380
Chenjie Yue2219202018-06-08 10:07:51 -0700381int StatsPullerManager::ForceClearPullerCache() {
Chenjie Yufa22d652018-02-05 14:37:48 -0800382 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800383 for (const auto& pulledAtom : kAllPullAtomInfo) {
384 totalCleared += pulledAtom.second.puller->ForceClearCache();
Chenjie Yue72252b2018-02-01 13:19:35 -0800385 }
Chenjie Yufa22d652018-02-05 14:37:48 -0800386 return totalCleared;
387}
388
Chenjie Yue2219202018-06-08 10:07:51 -0700389int StatsPullerManager::ClearPullerCacheIfNecessary(int64_t timestampNs) {
Chenjie Yufa22d652018-02-05 14:37:48 -0800390 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800391 for (const auto& pulledAtom : kAllPullAtomInfo) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700392 totalCleared += pulledAtom.second.puller->ClearCacheIfNecessary(timestampNs);
Chenjie Yufa22d652018-02-05 14:37:48 -0800393 }
394 return totalCleared;
Chenjie Yue72252b2018-02-01 13:19:35 -0800395}
396
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700397} // namespace statsd
398} // namespace os
399} // namespace android