blob: 0e131cb6c8c98f0905f185c125cd9e14b6e02827 [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"
31#include "ResourceThermalManagerPuller.h"
Chenjie Yu5305e1d2017-10-31 13:49:36 -070032#include "StatsCompanionServicePuller.h"
Chenjie Yue2219202018-06-08 10:07:51 -070033#include "StatsPullerManager.h"
Tej Singhbf972d92018-01-10 20:51:13 -080034#include "SubsystemSleepStatePuller.h"
Chenjie Yu5305e1d2017-10-31 13:49:36 -070035#include "statslog.h"
David Chen1481fe12017-10-16 13:16:34 -070036
37#include <iostream>
Chenjie Yu1a317ba2017-10-05 16:05:32 -070038
Yao Chen93fe3a32017-11-02 13:52:59 -070039using std::make_shared;
Chenjie Yu5305e1d2017-10-31 13:49:36 -070040using std::map;
Yao Chen93fe3a32017-11-02 13:52:59 -070041using std::shared_ptr;
Chenjie Yub3dda412017-10-24 13:41:59 -070042using std::string;
43using std::vector;
Chenjie Yu6736c892017-11-09 10:50:09 -080044using std::list;
Chenjie Yu1a317ba2017-10-05 16:05:32 -070045
46namespace android {
47namespace os {
48namespace statsd {
49
Chenjie Yu3b3adcd2018-04-18 16:25:36 -070050// Values smaller than this may require to update the alarm.
51const int64_t NO_ALARM_UPDATE = INT64_MAX;
52
Chenjie Yue2219202018-06-08 10:07:51 -070053const std::map<int, PullAtomInfo> StatsPullerManager::kAllPullAtomInfo = {
Chenjie Yufeba3092018-02-08 14:33:37 -080054 // wifi_bytes_transfer
55 {android::util::WIFI_BYTES_TRANSFER,
Chenjie Yu1a0a9412018-03-28 10:07:22 -070056 {{2, 3, 4, 5},
57 {},
58 1 * NS_PER_SEC,
Chenjie Yufeba3092018-02-08 14:33:37 -080059 new StatsCompanionServicePuller(android::util::WIFI_BYTES_TRANSFER)}},
60 // wifi_bytes_transfer_by_fg_bg
61 {android::util::WIFI_BYTES_TRANSFER_BY_FG_BG,
Chenjie Yu1a0a9412018-03-28 10:07:22 -070062 {{3, 4, 5, 6},
63 {2},
64 1 * NS_PER_SEC,
Chenjie Yufeba3092018-02-08 14:33:37 -080065 new StatsCompanionServicePuller(android::util::WIFI_BYTES_TRANSFER_BY_FG_BG)}},
66 // mobile_bytes_transfer
67 {android::util::MOBILE_BYTES_TRANSFER,
Chenjie Yu1a0a9412018-03-28 10:07:22 -070068 {{2, 3, 4, 5},
69 {},
70 1 * NS_PER_SEC,
Chenjie Yufeba3092018-02-08 14:33:37 -080071 new StatsCompanionServicePuller(android::util::MOBILE_BYTES_TRANSFER)}},
72 // mobile_bytes_transfer_by_fg_bg
73 {android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG,
Chenjie Yu1a0a9412018-03-28 10:07:22 -070074 {{3, 4, 5, 6},
75 {2},
76 1 * NS_PER_SEC,
Chenjie Yufeba3092018-02-08 14:33:37 -080077 new StatsCompanionServicePuller(android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG)}},
78 // bluetooth_bytes_transfer
79 {android::util::BLUETOOTH_BYTES_TRANSFER,
Chenjie Yu1a0a9412018-03-28 10:07:22 -070080 {{2, 3},
81 {},
82 1 * NS_PER_SEC,
83 new StatsCompanionServicePuller(android::util::BLUETOOTH_BYTES_TRANSFER)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080084 // kernel_wakelock
85 {android::util::KERNEL_WAKELOCK,
Chenjie Yu1a0a9412018-03-28 10:07:22 -070086 {{}, {}, 1 * NS_PER_SEC, new StatsCompanionServicePuller(android::util::KERNEL_WAKELOCK)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080087 // subsystem_sleep_state
Chenjie Yuec676612018-03-07 09:19:17 -080088 {android::util::SUBSYSTEM_SLEEP_STATE,
Chenjie Yu1a0a9412018-03-28 10:07:22 -070089 {{}, {}, 1 * NS_PER_SEC, new SubsystemSleepStatePuller()}},
Bookatz92da2832018-11-01 18:10:03 -070090 // on_device_power_measurement
91 {android::util::ON_DEVICE_POWER_MEASUREMENT,
92 {{}, {}, 1 * NS_PER_SEC, new PowerStatsPuller()}},
Chenjie Yufeba3092018-02-08 14:33:37 -080093 // cpu_time_per_freq
94 {android::util::CPU_TIME_PER_FREQ,
Chenjie Yu1a0a9412018-03-28 10:07:22 -070095 {{3},
96 {2},
97 1 * NS_PER_SEC,
98 new StatsCompanionServicePuller(android::util::CPU_TIME_PER_FREQ)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080099 // cpu_time_per_uid
Chenjie Yuec676612018-03-07 09:19:17 -0800100 {android::util::CPU_TIME_PER_UID,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700101 {{2, 3},
102 {},
103 1 * NS_PER_SEC,
104 new StatsCompanionServicePuller(android::util::CPU_TIME_PER_UID)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800105 // cpu_time_per_uid_freq
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_TIME_PER_UID_FREQ,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700109 {{4},
110 {2, 3},
111 1 * NS_PER_SEC,
112 new StatsCompanionServicePuller(android::util::CPU_TIME_PER_UID_FREQ)}},
Chenjie Yuec676612018-03-07 09:19:17 -0800113 // cpu_active_time
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700114 // the throttling is 3sec, handled in
115 // frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader
Chenjie Yuec676612018-03-07 09:19:17 -0800116 {android::util::CPU_ACTIVE_TIME,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700117 {{2},
118 {},
119 1 * NS_PER_SEC,
120 new StatsCompanionServicePuller(android::util::CPU_ACTIVE_TIME)}},
Chenjie Yuec676612018-03-07 09:19:17 -0800121 // cpu_cluster_time
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700122 // the throttling is 3sec, handled in
123 // frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader
Chenjie Yuec676612018-03-07 09:19:17 -0800124 {android::util::CPU_CLUSTER_TIME,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700125 {{3},
126 {2},
127 1 * NS_PER_SEC,
128 new StatsCompanionServicePuller(android::util::CPU_CLUSTER_TIME)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800129 // wifi_activity_energy_info
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800130 {android::util::WIFI_ACTIVITY_INFO,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700131 {{},
132 {},
133 1 * NS_PER_SEC,
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800134 new StatsCompanionServicePuller(android::util::WIFI_ACTIVITY_INFO)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800135 // modem_activity_info
136 {android::util::MODEM_ACTIVITY_INFO,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700137 {{},
138 {},
139 1 * NS_PER_SEC,
140 new StatsCompanionServicePuller(android::util::MODEM_ACTIVITY_INFO)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800141 // bluetooth_activity_info
142 {android::util::BLUETOOTH_ACTIVITY_INFO,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700143 {{},
144 {},
145 1 * NS_PER_SEC,
146 new StatsCompanionServicePuller(android::util::BLUETOOTH_ACTIVITY_INFO)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800147 // system_elapsed_realtime
148 {android::util::SYSTEM_ELAPSED_REALTIME,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700149 {{},
150 {},
151 1 * NS_PER_SEC,
152 new StatsCompanionServicePuller(android::util::SYSTEM_ELAPSED_REALTIME)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800153 // system_uptime
154 {android::util::SYSTEM_UPTIME,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700155 {{}, {}, 1 * NS_PER_SEC, new StatsCompanionServicePuller(android::util::SYSTEM_UPTIME)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800156 // remaining_battery_capacity
157 {android::util::REMAINING_BATTERY_CAPACITY,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700158 {{},
159 {},
160 1 * NS_PER_SEC,
161 new ResourceHealthManagerPuller(android::util::REMAINING_BATTERY_CAPACITY)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800162 // full_battery_capacity
163 {android::util::FULL_BATTERY_CAPACITY,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700164 {{},
165 {},
166 1 * NS_PER_SEC,
167 new ResourceHealthManagerPuller(android::util::FULL_BATTERY_CAPACITY)}},
Bookatz17f0d8a2018-09-13 12:56:32 -0700168 // battery_voltage
169 {android::util::BATTERY_VOLTAGE,
Chenjie Yu2c0d83d2018-09-02 06:37:08 -0700170 {{}, {}, 1 * NS_PER_SEC, new ResourceHealthManagerPuller(android::util::BATTERY_VOLTAGE)}},
Rajeev Kumar22d92b72018-02-07 18:38:36 -0800171 // process_memory_state
172 {android::util::PROCESS_MEMORY_STATE,
Rafal Slawikaaf60892018-09-12 13:04:30 +0100173 {{4, 5, 6, 7, 8, 9},
Rafal Slawik272a8162018-11-01 15:12:28 +0000174 {2, 3, 10},
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700175 1 * NS_PER_SEC,
Chenjie Yuec676612018-03-07 09:19:17 -0800176 new StatsCompanionServicePuller(android::util::PROCESS_MEMORY_STATE)}},
Rafal Slawik08621582018-10-15 14:53:07 +0100177 // native_process_memory_state
178 {android::util::NATIVE_PROCESS_MEMORY_STATE,
Rafal Slawik272a8162018-11-01 15:12:28 +0000179 {{3, 4, 5, 6},
180 {2, 7},
Rafal Slawik08621582018-10-15 14:53:07 +0100181 1 * NS_PER_SEC,
182 new StatsCompanionServicePuller(android::util::NATIVE_PROCESS_MEMORY_STATE)}},
Tej Singh40298312018-02-16 00:15:09 -0800183 // temperature
Wei Wangf0f01172018-09-13 16:16:02 -0700184 {android::util::TEMPERATURE, {{}, {}, 1 * NS_PER_SEC, new ResourceThermalManagerPuller()}},
Olivier Gaillard00bfb1b2018-07-10 11:25:09 +0100185 // binder_calls
186 {android::util::BINDER_CALLS,
Olivier Gaillard6f52d152018-07-25 12:13:12 +0100187 {{4, 5, 6, 8, 12},
188 {2, 3, 7, 9, 10, 11, 13},
Olivier Gaillard00bfb1b2018-07-10 11:25:09 +0100189 1 * NS_PER_SEC,
Olivier Gaillard6f52d152018-07-25 12:13:12 +0100190 new StatsCompanionServicePuller(android::util::BINDER_CALLS)}},
191 // binder_calls_exceptions
192 {android::util::BINDER_CALLS_EXCEPTIONS,
193 {{},
194 {},
195 1 * NS_PER_SEC,
Marcin Oczeretkod8cc8592018-08-22 16:07:36 +0100196 new StatsCompanionServicePuller(android::util::BINDER_CALLS_EXCEPTIONS)}},
197 // looper_stats
198 {android::util::LOOPER_STATS,
199 {{5, 6, 7, 8, 9},
Marcin Oczeretko3e6494e2018-09-10 18:06:52 +0100200 {2, 3, 4, 10},
Marcin Oczeretkod8cc8592018-08-22 16:07:36 +0100201 1 * NS_PER_SEC,
Tej Singh86dc9db2018-09-06 00:39:57 +0000202 new StatsCompanionServicePuller(android::util::LOOPER_STATS)}},
203 // Disk Stats
204 {android::util::DISK_STATS,
Chenjie Yu12e5e672018-09-14 15:54:59 -0700205 {{}, {}, 1 * NS_PER_SEC, new StatsCompanionServicePuller(android::util::DISK_STATS)}},
Tej Singh86dc9db2018-09-06 00:39:57 +0000206 // Directory usage
207 {android::util::DIRECTORY_USAGE,
Chenjie Yu12e5e672018-09-14 15:54:59 -0700208 {{}, {}, 1 * NS_PER_SEC, new StatsCompanionServicePuller(android::util::DIRECTORY_USAGE)}},
Tej Singh86dc9db2018-09-06 00:39:57 +0000209 // Size of app's code, data, and cache
210 {android::util::APP_SIZE,
Chenjie Yu12e5e672018-09-14 15:54:59 -0700211 {{}, {}, 1 * NS_PER_SEC, new StatsCompanionServicePuller(android::util::APP_SIZE)}},
Tej Singh86dc9db2018-09-06 00:39:57 +0000212 // Size of specific categories of files. Eg. Music.
213 {android::util::CATEGORY_SIZE,
Chenjie Yu2c0d83d2018-09-02 06:37:08 -0700214 {{}, {}, 1 * NS_PER_SEC, new StatsCompanionServicePuller(android::util::CATEGORY_SIZE)}},
Tej Singhd6d6d772018-09-05 17:41:25 -0700215 // Number of fingerprints registered to each user.
216 {android::util::NUM_FINGERPRINTS,
217 {{},
218 {},
219 1 * NS_PER_SEC,
220 new StatsCompanionServicePuller(android::util::NUM_FINGERPRINTS)}},
Chenjie Yu2c0d83d2018-09-02 06:37:08 -0700221 // ProcStats.
222 {android::util::PROC_STATS,
223 {{}, {}, 1 * NS_PER_SEC, new StatsCompanionServicePuller(android::util::PROC_STATS)}},
Chenjie Yub52779e2018-10-05 12:03:36 -0700224 // ProcStatsPkgProc.
225 {android::util::PROC_STATS_PKG_PROC,
226 {{}, {}, 1 * NS_PER_SEC, new StatsCompanionServicePuller(android::util::PROC_STATS_PKG_PROC)}},
Tej Singhe7726dc2018-09-21 11:42:12 -0700227 // Disk I/O stats per uid.
228 {android::util::DISK_IO,
229 {{2,3,4,5,6,7,8,9,10,11},
230 {},
231 3 * NS_PER_SEC,
232 new StatsCompanionServicePuller(android::util::DISK_IO)}},
Chenjie Yuab530202018-09-26 12:39:20 -0700233 // PowerProfile constants for power model calculations.
234 {android::util::POWER_PROFILE,
235 {{}, {}, 1 * NS_PER_SEC, new StatsCompanionServicePuller(android::util::POWER_PROFILE)}},
Yangster-mac308ea0c2018-10-22 13:10:25 -0700236 // Process cpu stats. Min cool-down is 5 sec, inline with what AcitivityManagerService uses.
237 {android::util::PROCESS_CPU_TIME,
238 {{} /* additive fields */, {} /* non additive fields */,
239 5 * NS_PER_SEC /* min cool-down in seconds*/,
240 new StatsCompanionServicePuller(android::util::PROCESS_CPU_TIME)}},
Misha Wagner5a51e002018-10-03 15:04:09 +0100241 {android::util::CPU_TIME_PER_THREAD_FREQ,
242 {{7},
243 {2, 3, 4, 5, 6},
244 1 * NS_PER_SEC,
245 new StatsCompanionServicePuller(android::util::CPU_TIME_PER_THREAD_FREQ)}},
Bookatz75ee60422018-11-09 12:27:37 -0800246 // DeviceCalculatedPowerUse.
247 {android::util::DEVICE_CALCULATED_POWER_USE,
248 {{}, {}, 1 * NS_PER_SEC,
249 new StatsCompanionServicePuller(android::util::DEVICE_CALCULATED_POWER_USE)}},
250 // DeviceCalculatedPowerBlameUid.
251 {android::util::DEVICE_CALCULATED_POWER_BLAME_UID,
252 {{}, {}, // BatteryStats already merged isolated with host ids so it's unnecessary here.
253 1 * NS_PER_SEC,
254 new StatsCompanionServicePuller(android::util::DEVICE_CALCULATED_POWER_BLAME_UID)}},
255 // DeviceCalculatedPowerBlameOther.
256 {android::util::DEVICE_CALCULATED_POWER_BLAME_OTHER,
257 {{}, {},
258 1 * NS_PER_SEC,
259 new StatsCompanionServicePuller(android::util::DEVICE_CALCULATED_POWER_BLAME_OTHER)}},
Chenjie Yu2c0d83d2018-09-02 06:37:08 -0700260};
Chenjie Yu80f91122018-01-31 20:24:50 -0800261
Chenjie Yue2219202018-06-08 10:07:51 -0700262StatsPullerManager::StatsPullerManager() : mNextPullTimeNs(NO_ALARM_UPDATE) {
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700263}
264
Chenjie Yue2219202018-06-08 10:07:51 -0700265bool StatsPullerManager::Pull(const int tagId, const int64_t timeNs,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700266 vector<shared_ptr<LogEvent>>* data) {
Tej Singh484524a2018-02-01 15:10:05 -0800267 VLOG("Initiating pulling %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700268
Chenjie Yu80f91122018-01-31 20:24:50 -0800269 if (kAllPullAtomInfo.find(tagId) != kAllPullAtomInfo.end()) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700270 bool ret = kAllPullAtomInfo.find(tagId)->second.puller->Pull(timeNs, data);
Tej Singh484524a2018-02-01 15:10:05 -0800271 VLOG("pulled %d items", (int)data->size());
272 return ret;
Yao Chen93fe3a32017-11-02 13:52:59 -0700273 } else {
Tej Singh484524a2018-02-01 15:10:05 -0800274 VLOG("Unknown tagId %d", tagId);
Yao Chen93fe3a32017-11-02 13:52:59 -0700275 return false; // Return early since we don't know what to pull.
276 }
277}
Chenjie Yub3dda412017-10-24 13:41:59 -0700278
Chenjie Yue2219202018-06-08 10:07:51 -0700279bool StatsPullerManager::PullerForMatcherExists(int tagId) const {
Chenjie Yufeba3092018-02-08 14:33:37 -0800280 return kAllPullAtomInfo.find(tagId) != kAllPullAtomInfo.end();
Chenjie Yub3dda412017-10-24 13:41:59 -0700281}
282
Chenjie Yue2219202018-06-08 10:07:51 -0700283void StatsPullerManager::updateAlarmLocked() {
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700284 if (mNextPullTimeNs == NO_ALARM_UPDATE) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700285 VLOG("No need to set alarms. Skipping");
286 return;
287 }
288
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700289 sp<IStatsCompanionService> statsCompanionServiceCopy = mStatsCompanionService;
290 if (statsCompanionServiceCopy != nullptr) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700291 statsCompanionServiceCopy->setPullingAlarm(mNextPullTimeNs / 1000000);
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700292 } else {
293 VLOG("StatsCompanionService not available. Alarm not set.");
294 }
295 return;
296}
297
Chenjie Yue2219202018-06-08 10:07:51 -0700298void StatsPullerManager::SetStatsCompanionService(
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700299 sp<IStatsCompanionService> statsCompanionService) {
300 AutoMutex _l(mLock);
301 sp<IStatsCompanionService> tmpForLock = mStatsCompanionService;
302 mStatsCompanionService = statsCompanionService;
303 for (const auto& pulledAtom : kAllPullAtomInfo) {
304 pulledAtom.second.puller->SetStatsCompanionService(statsCompanionService);
305 }
306 if (mStatsCompanionService != nullptr) {
307 updateAlarmLocked();
308 }
309}
310
Chenjie Yue2219202018-06-08 10:07:51 -0700311void StatsPullerManager::RegisterReceiver(int tagId, wp<PullDataReceiver> receiver,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700312 int64_t nextPullTimeNs, int64_t intervalNs) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700313 AutoMutex _l(mLock);
Chenjie Yu6736c892017-11-09 10:50:09 -0800314 auto& receivers = mReceivers[tagId];
Chenjie Yub3dda412017-10-24 13:41:59 -0700315 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800316 if (it->receiver == receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700317 VLOG("Receiver already registered of %d", (int)receivers.size());
318 return;
319 }
320 }
321 ReceiverInfo receiverInfo;
322 receiverInfo.receiver = receiver;
Chenjie Yub3dda412017-10-24 13:41:59 -0700323
Chenjie Yu85ed8382017-12-14 16:48:54 -0800324 // Round it to the nearest minutes. This is the limit of alarm manager.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700325 // In practice, we should always have larger buckets.
326 int64_t roundedIntervalNs = intervalNs / NS_PER_SEC / 60 * NS_PER_SEC * 60;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700327 // Scheduled pulling should be at least 1 min apart.
328 // This can be lower in cts tests, in which case we round it to 1 min.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700329 if (roundedIntervalNs < 60 * (int64_t)NS_PER_SEC) {
330 roundedIntervalNs = 60 * (int64_t)NS_PER_SEC;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700331 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700332
333 receiverInfo.intervalNs = roundedIntervalNs;
334 receiverInfo.nextPullTimeNs = nextPullTimeNs;
335 receivers.push_back(receiverInfo);
336
Chenjie Yub3dda412017-10-24 13:41:59 -0700337 // There is only one alarm for all pulled events. So only set it to the smallest denom.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700338 if (nextPullTimeNs < mNextPullTimeNs) {
339 VLOG("Updating next pull time %lld", (long long)mNextPullTimeNs);
340 mNextPullTimeNs = nextPullTimeNs;
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700341 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700342 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700343 VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700344}
345
Chenjie Yue2219202018-06-08 10:07:51 -0700346void StatsPullerManager::UnRegisterReceiver(int tagId, wp<PullDataReceiver> receiver) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700347 AutoMutex _l(mLock);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700348 if (mReceivers.find(tagId) == mReceivers.end()) {
349 VLOG("Unknown pull code or no receivers: %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700350 return;
351 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700352 auto& receivers = mReceivers.find(tagId)->second;
Chenjie Yub3dda412017-10-24 13:41:59 -0700353 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800354 if (receiver == it->receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700355 receivers.erase(it);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700356 VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700357 return;
358 }
359 }
360}
361
Chenjie Yue2219202018-06-08 10:07:51 -0700362void StatsPullerManager::OnAlarmFired(const int64_t currentTimeNs) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700363 AutoMutex _l(mLock);
Chenjie Yub3dda412017-10-24 13:41:59 -0700364
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700365 int64_t minNextPullTimeNs = NO_ALARM_UPDATE;
Chenjie Yub3dda412017-10-24 13:41:59 -0700366
367 vector<pair<int, vector<ReceiverInfo*>>> needToPull =
368 vector<pair<int, vector<ReceiverInfo*>>>();
369 for (auto& pair : mReceivers) {
370 vector<ReceiverInfo*> receivers = vector<ReceiverInfo*>();
Yao Chen93fe3a32017-11-02 13:52:59 -0700371 if (pair.second.size() != 0) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700372 for (ReceiverInfo& receiverInfo : pair.second) {
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700373 if (receiverInfo.nextPullTimeNs <= currentTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700374 receivers.push_back(&receiverInfo);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700375 } else {
376 if (receiverInfo.nextPullTimeNs < minNextPullTimeNs) {
377 minNextPullTimeNs = receiverInfo.nextPullTimeNs;
378 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700379 }
380 }
381 if (receivers.size() > 0) {
382 needToPull.push_back(make_pair(pair.first, receivers));
383 }
384 }
385 }
386
387 for (const auto& pullInfo : needToPull) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700388 vector<shared_ptr<LogEvent>> data;
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700389 if (Pull(pullInfo.first, currentTimeNs, &data)) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700390 for (const auto& receiverInfo : pullInfo.second) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800391 sp<PullDataReceiver> receiverPtr = receiverInfo->receiver.promote();
392 if (receiverPtr != nullptr) {
393 receiverPtr->onDataPulled(data);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700394 // we may have just come out of a coma, compute next pull time
395 receiverInfo->nextPullTimeNs =
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700396 (currentTimeNs - receiverInfo->nextPullTimeNs) /
397 receiverInfo->intervalNs * receiverInfo->intervalNs +
398 receiverInfo->intervalNs + receiverInfo->nextPullTimeNs;
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700399 if (receiverInfo->nextPullTimeNs < minNextPullTimeNs) {
400 minNextPullTimeNs = receiverInfo->nextPullTimeNs;
401 }
Chenjie Yu6736c892017-11-09 10:50:09 -0800402 } else {
403 VLOG("receiver already gone.");
404 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700405 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700406 }
407 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700408
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700409 VLOG("mNextPullTimeNs: %lld updated to %lld", (long long)mNextPullTimeNs,
410 (long long)minNextPullTimeNs);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700411 mNextPullTimeNs = minNextPullTimeNs;
412 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700413}
414
Chenjie Yue2219202018-06-08 10:07:51 -0700415int StatsPullerManager::ForceClearPullerCache() {
Chenjie Yufa22d652018-02-05 14:37:48 -0800416 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800417 for (const auto& pulledAtom : kAllPullAtomInfo) {
418 totalCleared += pulledAtom.second.puller->ForceClearCache();
Chenjie Yue72252b2018-02-01 13:19:35 -0800419 }
Chenjie Yufa22d652018-02-05 14:37:48 -0800420 return totalCleared;
421}
422
Chenjie Yue2219202018-06-08 10:07:51 -0700423int StatsPullerManager::ClearPullerCacheIfNecessary(int64_t timestampNs) {
Chenjie Yufa22d652018-02-05 14:37:48 -0800424 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800425 for (const auto& pulledAtom : kAllPullAtomInfo) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700426 totalCleared += pulledAtom.second.puller->ClearCacheIfNecessary(timestampNs);
Chenjie Yufa22d652018-02-05 14:37:48 -0800427 }
428 return totalCleared;
Chenjie Yue72252b2018-02-01 13:19:35 -0800429}
430
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700431} // namespace statsd
432} // namespace os
433} // namespace android