blob: f2cc2f73058337697f554ce93c2b7987659a01c5 [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
Alec Mouri1dc5f1e2019-09-18 21:13:01 -070020#include "StatsPullerManager.h"
21
Tej Singh6a5c9432019-10-11 11:07:06 -070022#include <android/os/IPullAtomCallback.h>
Chenjie Yu1a317ba2017-10-05 16:05:32 -070023#include <android/os/IStatsCompanionService.h>
Tej Singha0c89dd2019-01-25 16:39:18 -080024#include <android/os/IStatsPullerCallback.h>
David Chen1481fe12017-10-16 13:16:34 -070025#include <cutils/log.h>
Chenjie Yu1a0a9412018-03-28 10:07:22 -070026#include <math.h>
Chenjie Yu3b3adcd2018-04-18 16:25:36 -070027#include <stdint.h>
Alec Mouri1dc5f1e2019-09-18 21:13:01 -070028
David Chen1481fe12017-10-16 13:16:34 -070029#include <algorithm>
Alec Mouri1dc5f1e2019-09-18 21:13:01 -070030#include <iostream>
31
Chenjie Yu1a0a9412018-03-28 10:07:22 -070032#include "../StatsService.h"
Chenjie Yuaa5b2012018-03-21 13:53:15 -070033#include "../logd/LogEvent.h"
34#include "../stats_log_util.h"
35#include "../statscompanion_util.h"
Mark Tabry9dc13572019-11-06 13:42:30 -080036#include "CarStatsPuller.h"
Yiwei Zhang7e633032019-03-01 17:25:27 -080037#include "GpuStatsPuller.h"
Bookatz92da2832018-11-01 18:10:03 -070038#include "PowerStatsPuller.h"
Tej Singh40298312018-02-16 00:15:09 -080039#include "ResourceHealthManagerPuller.h"
Tej Singha0c89dd2019-01-25 16:39:18 -080040#include "StatsCallbackPuller.h"
Tej Singh6a5c9432019-10-11 11:07:06 -070041#include "StatsCallbackPullerDeprecated.h"
Chenjie Yu97dbb202019-02-13 16:42:04 -080042#include "StatsCompanionServicePuller.h"
Tej Singhbf972d92018-01-10 20:51:13 -080043#include "SubsystemSleepStatePuller.h"
Chenjie Yu97dbb202019-02-13 16:42:04 -080044#include "TrainInfoPuller.h"
Chenjie Yu5305e1d2017-10-31 13:49:36 -070045#include "statslog.h"
David Chen1481fe12017-10-16 13:16:34 -070046
Yao Chen93fe3a32017-11-02 13:52:59 -070047using std::make_shared;
Chenjie Yu5305e1d2017-10-31 13:49:36 -070048using std::map;
Yao Chen93fe3a32017-11-02 13:52:59 -070049using std::shared_ptr;
Chenjie Yub3dda412017-10-24 13:41:59 -070050using std::string;
51using std::vector;
Chenjie Yu6736c892017-11-09 10:50:09 -080052using std::list;
Chenjie Yu1a317ba2017-10-05 16:05:32 -070053
54namespace android {
55namespace os {
56namespace statsd {
57
Chenjie Yu3b3adcd2018-04-18 16:25:36 -070058// Values smaller than this may require to update the alarm.
59const int64_t NO_ALARM_UPDATE = INT64_MAX;
60
Tej Singh6a5c9432019-10-11 11:07:06 -070061std::map<PullerKey, PullAtomInfo> StatsPullerManager::kAllPullAtomInfo = {
Chenjie Yufeba3092018-02-08 14:33:37 -080062 // wifi_bytes_transfer
Tej Singh6a5c9432019-10-11 11:07:06 -070063 {{.atomTag = android::util::WIFI_BYTES_TRANSFER},
Chenjie Yu0bd73db2018-12-16 07:37:04 -080064 {.additiveFields = {2, 3, 4, 5},
65 .puller = new StatsCompanionServicePuller(android::util::WIFI_BYTES_TRANSFER)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -080066
Chenjie Yufeba3092018-02-08 14:33:37 -080067 // wifi_bytes_transfer_by_fg_bg
Tej Singh6a5c9432019-10-11 11:07:06 -070068 {{.atomTag = android::util::WIFI_BYTES_TRANSFER_BY_FG_BG},
Chenjie Yu0bd73db2018-12-16 07:37:04 -080069 {.additiveFields = {3, 4, 5, 6},
70 .puller = new StatsCompanionServicePuller(android::util::WIFI_BYTES_TRANSFER_BY_FG_BG)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -080071
Chenjie Yufeba3092018-02-08 14:33:37 -080072 // mobile_bytes_transfer
Tej Singh6a5c9432019-10-11 11:07:06 -070073 {{.atomTag = android::util::MOBILE_BYTES_TRANSFER},
Chenjie Yu0bd73db2018-12-16 07:37:04 -080074 {.additiveFields = {2, 3, 4, 5},
75 .puller = new StatsCompanionServicePuller(android::util::MOBILE_BYTES_TRANSFER)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -080076
Chenjie Yufeba3092018-02-08 14:33:37 -080077 // mobile_bytes_transfer_by_fg_bg
Tej Singh6a5c9432019-10-11 11:07:06 -070078 {{.atomTag = android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG},
Chenjie Yu0bd73db2018-12-16 07:37:04 -080079 {.additiveFields = {3, 4, 5, 6},
80 .puller =
81 new StatsCompanionServicePuller(android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -080082
Chenjie Yufeba3092018-02-08 14:33:37 -080083 // bluetooth_bytes_transfer
Tej Singh6a5c9432019-10-11 11:07:06 -070084 {{.atomTag = android::util::BLUETOOTH_BYTES_TRANSFER},
Chenjie Yu0bd73db2018-12-16 07:37:04 -080085 {.additiveFields = {2, 3},
86 .puller = new StatsCompanionServicePuller(android::util::BLUETOOTH_BYTES_TRANSFER)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -080087
Chenjie Yufeba3092018-02-08 14:33:37 -080088 // kernel_wakelock
Tej Singh6a5c9432019-10-11 11:07:06 -070089 {{.atomTag = android::util::KERNEL_WAKELOCK},
Chenjie Yu0bd73db2018-12-16 07:37:04 -080090 {.puller = new StatsCompanionServicePuller(android::util::KERNEL_WAKELOCK)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -080091
Chenjie Yufeba3092018-02-08 14:33:37 -080092 // subsystem_sleep_state
Tej Singh6a5c9432019-10-11 11:07:06 -070093 {{.atomTag = android::util::SUBSYSTEM_SLEEP_STATE},
94 {.puller = new SubsystemSleepStatePuller()}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -080095
Bookatz92da2832018-11-01 18:10:03 -070096 // on_device_power_measurement
Tej Singh6a5c9432019-10-11 11:07:06 -070097 {{.atomTag = android::util::ON_DEVICE_POWER_MEASUREMENT},
98 {.puller = new PowerStatsPuller()}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -080099
Chenjie Yufeba3092018-02-08 14:33:37 -0800100 // cpu_time_per_freq
Tej Singh6a5c9432019-10-11 11:07:06 -0700101 {{.atomTag = android::util::CPU_TIME_PER_FREQ},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800102 {.additiveFields = {3},
103 .puller = new StatsCompanionServicePuller(android::util::CPU_TIME_PER_FREQ)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800104
Chenjie Yufeba3092018-02-08 14:33:37 -0800105 // cpu_time_per_uid
Tej Singh6a5c9432019-10-11 11:07:06 -0700106 {{.atomTag = android::util::CPU_TIME_PER_UID},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800107 {.additiveFields = {2, 3},
108 .puller = new StatsCompanionServicePuller(android::util::CPU_TIME_PER_UID)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800109
Chenjie Yufeba3092018-02-08 14:33:37 -0800110 // cpu_time_per_uid_freq
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700111 // the throttling is 3sec, handled in
112 // frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader
Tej Singh6a5c9432019-10-11 11:07:06 -0700113 {{.atomTag = android::util::CPU_TIME_PER_UID_FREQ},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800114 {.additiveFields = {4},
115 .puller = new StatsCompanionServicePuller(android::util::CPU_TIME_PER_UID_FREQ)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800116
Chenjie Yuec676612018-03-07 09:19:17 -0800117 // cpu_active_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
Tej Singh6a5c9432019-10-11 11:07:06 -0700120 {{.atomTag = android::util::CPU_ACTIVE_TIME},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800121 {.additiveFields = {2},
122 .puller = new StatsCompanionServicePuller(android::util::CPU_ACTIVE_TIME)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800123
Chenjie Yuec676612018-03-07 09:19:17 -0800124 // cpu_cluster_time
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700125 // the throttling is 3sec, handled in
126 // frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader
Tej Singh6a5c9432019-10-11 11:07:06 -0700127 {{.atomTag = android::util::CPU_CLUSTER_TIME},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800128 {.additiveFields = {3},
129 .puller = new StatsCompanionServicePuller(android::util::CPU_CLUSTER_TIME)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800130
Chenjie Yufeba3092018-02-08 14:33:37 -0800131 // wifi_activity_energy_info
Tej Singh6a5c9432019-10-11 11:07:06 -0700132 {{.atomTag = android::util::WIFI_ACTIVITY_INFO},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800133 {.puller = new StatsCompanionServicePuller(android::util::WIFI_ACTIVITY_INFO)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800134
Chenjie Yufeba3092018-02-08 14:33:37 -0800135 // modem_activity_info
Tej Singh6a5c9432019-10-11 11:07:06 -0700136 {{.atomTag = android::util::MODEM_ACTIVITY_INFO},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800137 {.puller = new StatsCompanionServicePuller(android::util::MODEM_ACTIVITY_INFO)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800138
Chenjie Yufeba3092018-02-08 14:33:37 -0800139 // bluetooth_activity_info
Tej Singh6a5c9432019-10-11 11:07:06 -0700140 {{.atomTag = android::util::BLUETOOTH_ACTIVITY_INFO},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800141 {.puller = new StatsCompanionServicePuller(android::util::BLUETOOTH_ACTIVITY_INFO)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800142
Chenjie Yufeba3092018-02-08 14:33:37 -0800143 // system_elapsed_realtime
Tej Singh6a5c9432019-10-11 11:07:06 -0700144 {{.atomTag = android::util::SYSTEM_ELAPSED_REALTIME},
Nick Desaulniers691e1a02019-10-10 13:08:34 -0700145 {.coolDownNs = NS_PER_SEC,
146 .puller = new StatsCompanionServicePuller(android::util::SYSTEM_ELAPSED_REALTIME),
147 .pullTimeoutNs = NS_PER_SEC / 2,
148 }},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800149
Chenjie Yufeba3092018-02-08 14:33:37 -0800150 // system_uptime
Tej Singh6a5c9432019-10-11 11:07:06 -0700151 {{.atomTag = android::util::SYSTEM_UPTIME},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800152 {.puller = new StatsCompanionServicePuller(android::util::SYSTEM_UPTIME)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800153
Chenjie Yufeba3092018-02-08 14:33:37 -0800154 // remaining_battery_capacity
Tej Singh6a5c9432019-10-11 11:07:06 -0700155 {{.atomTag = android::util::REMAINING_BATTERY_CAPACITY},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800156 {.puller = new ResourceHealthManagerPuller(android::util::REMAINING_BATTERY_CAPACITY)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800157
Chenjie Yufeba3092018-02-08 14:33:37 -0800158 // full_battery_capacity
Tej Singh6a5c9432019-10-11 11:07:06 -0700159 {{.atomTag = android::util::FULL_BATTERY_CAPACITY},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800160 {.puller = new ResourceHealthManagerPuller(android::util::FULL_BATTERY_CAPACITY)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800161
Bookatz17f0d8a2018-09-13 12:56:32 -0700162 // battery_voltage
Tej Singh6a5c9432019-10-11 11:07:06 -0700163 {{.atomTag = android::util::BATTERY_VOLTAGE},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800164 {.puller = new ResourceHealthManagerPuller(android::util::BATTERY_VOLTAGE)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800165
Maggie White8735b852019-01-18 11:40:49 -0800166 // battery_level
Tej Singh6a5c9432019-10-11 11:07:06 -0700167 {{.atomTag = android::util::BATTERY_LEVEL},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800168 {.puller = new ResourceHealthManagerPuller(android::util::BATTERY_LEVEL)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800169
Maggie White8735b852019-01-18 11:40:49 -0800170 // battery_cycle_count
Tej Singh6a5c9432019-10-11 11:07:06 -0700171 {{.atomTag = android::util::BATTERY_CYCLE_COUNT},
Maggie White8735b852019-01-18 11:40:49 -0800172 {.puller = new ResourceHealthManagerPuller(android::util::BATTERY_CYCLE_COUNT)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800173
Rajeev Kumar22d92b72018-02-07 18:38:36 -0800174 // process_memory_state
Tej Singh6a5c9432019-10-11 11:07:06 -0700175 {{.atomTag = android::util::PROCESS_MEMORY_STATE},
Rafal Slawik203c3db2019-09-25 19:53:01 +0100176 {.additiveFields = {4, 5, 6, 7, 8},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800177 .puller = new StatsCompanionServicePuller(android::util::PROCESS_MEMORY_STATE)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800178
Rafal Slawikd4e87572019-03-12 13:08:38 +0000179 // process_memory_high_water_mark
Tej Singh6a5c9432019-10-11 11:07:06 -0700180 {{.atomTag = android::util::PROCESS_MEMORY_HIGH_WATER_MARK},
Rafal Slawik203c3db2019-09-25 19:53:01 +0100181 {.puller =
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800182 new StatsCompanionServicePuller(android::util::PROCESS_MEMORY_HIGH_WATER_MARK)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800183
Rafal Slawik7efcefb2019-09-20 11:16:20 +0100184 // process_memory_snapshot
Tej Singh6a5c9432019-10-11 11:07:06 -0700185 {{.atomTag = android::util::PROCESS_MEMORY_SNAPSHOT},
Rafal Slawik7efcefb2019-09-20 11:16:20 +0100186 {.puller = new StatsCompanionServicePuller(android::util::PROCESS_MEMORY_SNAPSHOT)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800187
Rafal Slawikd4e87572019-03-12 13:08:38 +0000188 // system_ion_heap_size
Tej Singh6a5c9432019-10-11 11:07:06 -0700189 {{.atomTag = android::util::SYSTEM_ION_HEAP_SIZE},
Rafal Slawikd4e87572019-03-12 13:08:38 +0000190 {.puller = new StatsCompanionServicePuller(android::util::SYSTEM_ION_HEAP_SIZE)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800191
Rafal Slawik77987ed2019-06-17 12:28:23 +0100192 // process_system_ion_heap_size
Tej Singh6a5c9432019-10-11 11:07:06 -0700193 {{.atomTag = android::util::PROCESS_SYSTEM_ION_HEAP_SIZE},
Rafal Slawik77987ed2019-06-17 12:28:23 +0100194 {.puller = new StatsCompanionServicePuller(android::util::PROCESS_SYSTEM_ION_HEAP_SIZE)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800195
Tej Singh40298312018-02-16 00:15:09 -0800196 // temperature
Tej Singh6a5c9432019-10-11 11:07:06 -0700197 {{.atomTag = android::util::TEMPERATURE},
Chenjie Yu97dbb202019-02-13 16:42:04 -0800198 {.puller = new StatsCompanionServicePuller(android::util::TEMPERATURE)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800199
Maggie White442b6312019-04-05 16:26:44 -0700200 // cooling_device
Tej Singh6a5c9432019-10-11 11:07:06 -0700201 {{.atomTag = android::util::COOLING_DEVICE},
Maggie White442b6312019-04-05 16:26:44 -0700202 {.puller = new StatsCompanionServicePuller(android::util::COOLING_DEVICE)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800203
Olivier Gaillard00bfb1b2018-07-10 11:25:09 +0100204 // binder_calls
Tej Singh6a5c9432019-10-11 11:07:06 -0700205 {{.atomTag = android::util::BINDER_CALLS},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800206 {.additiveFields = {4, 5, 6, 8, 12},
207 .puller = new StatsCompanionServicePuller(android::util::BINDER_CALLS)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800208
Olivier Gaillard6f52d152018-07-25 12:13:12 +0100209 // binder_calls_exceptions
Tej Singh6a5c9432019-10-11 11:07:06 -0700210 {{.atomTag = android::util::BINDER_CALLS_EXCEPTIONS},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800211 {.puller = new StatsCompanionServicePuller(android::util::BINDER_CALLS_EXCEPTIONS)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800212
Marcin Oczeretkod8cc8592018-08-22 16:07:36 +0100213 // looper_stats
Tej Singh6a5c9432019-10-11 11:07:06 -0700214 {{.atomTag = android::util::LOOPER_STATS},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800215 {.additiveFields = {5, 6, 7, 8, 9},
216 .puller = new StatsCompanionServicePuller(android::util::LOOPER_STATS)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800217
Tej Singh86dc9db2018-09-06 00:39:57 +0000218 // Disk Stats
Tej Singh6a5c9432019-10-11 11:07:06 -0700219 {{.atomTag = android::util::DISK_STATS},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800220 {.puller = new StatsCompanionServicePuller(android::util::DISK_STATS)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800221
Tej Singh86dc9db2018-09-06 00:39:57 +0000222 // Directory usage
Tej Singh6a5c9432019-10-11 11:07:06 -0700223 {{.atomTag = android::util::DIRECTORY_USAGE},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800224 {.puller = new StatsCompanionServicePuller(android::util::DIRECTORY_USAGE)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800225
Tej Singh86dc9db2018-09-06 00:39:57 +0000226 // Size of app's code, data, and cache
Tej Singh6a5c9432019-10-11 11:07:06 -0700227 {{.atomTag = android::util::APP_SIZE},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800228 {.puller = new StatsCompanionServicePuller(android::util::APP_SIZE)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800229
Tej Singh86dc9db2018-09-06 00:39:57 +0000230 // Size of specific categories of files. Eg. Music.
Tej Singh6a5c9432019-10-11 11:07:06 -0700231 {{.atomTag = android::util::CATEGORY_SIZE},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800232 {.puller = new StatsCompanionServicePuller(android::util::CATEGORY_SIZE)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800233
Kevin Chyn6ecc5702019-01-28 12:00:37 -0800234 // Number of fingerprints enrolled for each user.
Tej Singh6a5c9432019-10-11 11:07:06 -0700235 {{.atomTag = android::util::NUM_FINGERPRINTS_ENROLLED},
Kevin Chyn1741a072019-01-17 11:54:40 -0800236 {.puller = new StatsCompanionServicePuller(android::util::NUM_FINGERPRINTS_ENROLLED)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800237
Kevin Chyn6ecc5702019-01-28 12:00:37 -0800238 // Number of faces enrolled for each user.
Tej Singh6a5c9432019-10-11 11:07:06 -0700239 {{.atomTag = android::util::NUM_FACES_ENROLLED},
Kevin Chyn6ecc5702019-01-28 12:00:37 -0800240 {.puller = new StatsCompanionServicePuller(android::util::NUM_FACES_ENROLLED)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800241
Chenjie Yu2c0d83d2018-09-02 06:37:08 -0700242 // ProcStats.
Tej Singh6a5c9432019-10-11 11:07:06 -0700243 {{.atomTag = android::util::PROC_STATS},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800244 {.puller = new StatsCompanionServicePuller(android::util::PROC_STATS)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800245
Chenjie Yub52779e2018-10-05 12:03:36 -0700246 // ProcStatsPkgProc.
Tej Singh6a5c9432019-10-11 11:07:06 -0700247 {{.atomTag = android::util::PROC_STATS_PKG_PROC},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800248 {.puller = new StatsCompanionServicePuller(android::util::PROC_STATS_PKG_PROC)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800249
Tej Singhe7726dc2018-09-21 11:42:12 -0700250 // Disk I/O stats per uid.
Tej Singh6a5c9432019-10-11 11:07:06 -0700251 {{.atomTag = android::util::DISK_IO},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800252 {.additiveFields = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
253 .coolDownNs = 3 * NS_PER_SEC,
254 .puller = new StatsCompanionServicePuller(android::util::DISK_IO)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800255
Chenjie Yuab530202018-09-26 12:39:20 -0700256 // PowerProfile constants for power model calculations.
Tej Singh6a5c9432019-10-11 11:07:06 -0700257 {{.atomTag = android::util::POWER_PROFILE},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800258 {.puller = new StatsCompanionServicePuller(android::util::POWER_PROFILE)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800259
Yangster-mac308ea0c2018-10-22 13:10:25 -0700260 // Process cpu stats. Min cool-down is 5 sec, inline with what AcitivityManagerService uses.
Tej Singh6a5c9432019-10-11 11:07:06 -0700261 {{.atomTag = android::util::PROCESS_CPU_TIME},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800262 {.coolDownNs = 5 * NS_PER_SEC /* min cool-down in seconds*/,
263 .puller = new StatsCompanionServicePuller(android::util::PROCESS_CPU_TIME)}},
Tej Singh6a5c9432019-10-11 11:07:06 -0700264 {{.atomTag = android::util::CPU_TIME_PER_THREAD_FREQ},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800265 {.additiveFields = {7, 9, 11, 13, 15, 17, 19, 21},
266 .puller = new StatsCompanionServicePuller(android::util::CPU_TIME_PER_THREAD_FREQ)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800267
Bookatz75ee60422018-11-09 12:27:37 -0800268 // DeviceCalculatedPowerUse.
Tej Singh6a5c9432019-10-11 11:07:06 -0700269 {{.atomTag = android::util::DEVICE_CALCULATED_POWER_USE},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800270 {.puller = new StatsCompanionServicePuller(android::util::DEVICE_CALCULATED_POWER_USE)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800271
Bookatz75ee60422018-11-09 12:27:37 -0800272 // DeviceCalculatedPowerBlameUid.
Tej Singh6a5c9432019-10-11 11:07:06 -0700273 {{.atomTag = android::util::DEVICE_CALCULATED_POWER_BLAME_UID},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800274 {.puller = new StatsCompanionServicePuller(
275 android::util::DEVICE_CALCULATED_POWER_BLAME_UID)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800276
Bookatz75ee60422018-11-09 12:27:37 -0800277 // DeviceCalculatedPowerBlameOther.
Tej Singh6a5c9432019-10-11 11:07:06 -0700278 {{.atomTag = android::util::DEVICE_CALCULATED_POWER_BLAME_OTHER},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800279 {.puller = new StatsCompanionServicePuller(
280 android::util::DEVICE_CALCULATED_POWER_BLAME_OTHER)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800281
Olivier Gaillard18d452202019-01-22 15:03:41 +0000282 // DebugElapsedClock.
Tej Singh6a5c9432019-10-11 11:07:06 -0700283 {{.atomTag = android::util::DEBUG_ELAPSED_CLOCK},
Olivier Gaillard18d452202019-01-22 15:03:41 +0000284 {.additiveFields = {1, 2, 3, 4},
285 .puller = new StatsCompanionServicePuller(android::util::DEBUG_ELAPSED_CLOCK)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800286
Olivier Gaillard18d452202019-01-22 15:03:41 +0000287 // DebugFailingElapsedClock.
Tej Singh6a5c9432019-10-11 11:07:06 -0700288 {{.atomTag = android::util::DEBUG_FAILING_ELAPSED_CLOCK},
Olivier Gaillard18d452202019-01-22 15:03:41 +0000289 {.additiveFields = {1, 2, 3, 4},
290 .puller = new StatsCompanionServicePuller(android::util::DEBUG_FAILING_ELAPSED_CLOCK)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800291
Bookatz366a4432018-11-20 09:42:33 -0800292 // BuildInformation.
Tej Singh6a5c9432019-10-11 11:07:06 -0700293 {{.atomTag = android::util::BUILD_INFORMATION},
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800294 {.puller = new StatsCompanionServicePuller(android::util::BUILD_INFORMATION)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800295
Philip P. Moltmann70b42ae2019-01-29 16:24:19 -0800296 // RoleHolder.
Tej Singh6a5c9432019-10-11 11:07:06 -0700297 {{.atomTag = android::util::ROLE_HOLDER},
Philip P. Moltmann70b42ae2019-01-29 16:24:19 -0800298 {.puller = new StatsCompanionServicePuller(android::util::ROLE_HOLDER)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800299
Philip P. Moltmannf21a3502019-01-30 09:53:04 -0800300 // PermissionState.
Tej Singh6a5c9432019-10-11 11:07:06 -0700301 {{.atomTag = android::util::DANGEROUS_PERMISSION_STATE},
Philip P. Moltmannf21a3502019-01-30 09:53:04 -0800302 {.puller = new StatsCompanionServicePuller(android::util::DANGEROUS_PERMISSION_STATE)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800303
Chenjie Yu97dbb202019-02-13 16:42:04 -0800304 // TrainInfo.
Tej Singh6a5c9432019-10-11 11:07:06 -0700305 {{.atomTag = android::util::TRAIN_INFO}, {.puller = new TrainInfoPuller()}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800306
Neil Fullerdb14b482019-02-05 19:44:44 +0000307 // TimeZoneDataInfo.
Tej Singh6a5c9432019-10-11 11:07:06 -0700308 {{.atomTag = android::util::TIME_ZONE_DATA_INFO},
Neil Fullerdb14b482019-02-05 19:44:44 +0000309 {.puller = new StatsCompanionServicePuller(android::util::TIME_ZONE_DATA_INFO)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800310
Carmen Agimof12dec122019-03-06 16:14:38 +0000311 // ExternalStorageInfo
Tej Singh6a5c9432019-10-11 11:07:06 -0700312 {{.atomTag = android::util::EXTERNAL_STORAGE_INFO},
Carmen Agimof12dec122019-03-06 16:14:38 +0000313 {.puller = new StatsCompanionServicePuller(android::util::EXTERNAL_STORAGE_INFO)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800314
Yiwei Zhang7e633032019-03-01 17:25:27 -0800315 // GpuStatsGlobalInfo
Tej Singh6a5c9432019-10-11 11:07:06 -0700316 {{.atomTag = android::util::GPU_STATS_GLOBAL_INFO},
Yiwei Zhang7e633032019-03-01 17:25:27 -0800317 {.puller = new GpuStatsPuller(android::util::GPU_STATS_GLOBAL_INFO)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800318
Yiwei Zhang4fb92892019-03-04 14:29:25 -0800319 // GpuStatsAppInfo
Tej Singh6a5c9432019-10-11 11:07:06 -0700320 {{.atomTag = android::util::GPU_STATS_APP_INFO},
Yiwei Zhang4fb92892019-03-04 14:29:25 -0800321 {.puller = new GpuStatsPuller(android::util::GPU_STATS_APP_INFO)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800322
Carmen Agimofa769c152019-03-04 14:39:14 +0000323 // AppsOnExternalStorageInfo
Tej Singh6a5c9432019-10-11 11:07:06 -0700324 {{.atomTag = android::util::APPS_ON_EXTERNAL_STORAGE_INFO},
Carmen Agimofa769c152019-03-04 14:39:14 +0000325 {.puller = new StatsCompanionServicePuller(android::util::APPS_ON_EXTERNAL_STORAGE_INFO)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800326
joshmccloskey34c1f8e2019-04-11 14:06:43 -0700327 // Face Settings
Tej Singh6a5c9432019-10-11 11:07:06 -0700328 {{.atomTag = android::util::FACE_SETTINGS},
joshmccloskey34c1f8e2019-04-11 14:06:43 -0700329 {.puller = new StatsCompanionServicePuller(android::util::FACE_SETTINGS)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800330
Martijn Coenencd95d462019-04-25 17:09:45 +0200331 // App ops
Tej Singh6a5c9432019-10-11 11:07:06 -0700332 {{.atomTag = android::util::APP_OPS},
Martijn Coenencd95d462019-04-25 17:09:45 +0200333 {.puller = new StatsCompanionServicePuller(android::util::APP_OPS)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800334
Mark Tabry9dc13572019-11-06 13:42:30 -0800335 // VmsClientStats
Tej Singh6a5c9432019-10-11 11:07:06 -0700336 {{.atomTag = android::util::VMS_CLIENT_STATS},
Mark Tabry9dc13572019-11-06 13:42:30 -0800337 {.additiveFields = {5, 6, 7, 8, 9, 10},
338 .puller = new CarStatsPuller(android::util::VMS_CLIENT_STATS)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800339
Chris Wrencdea3b72019-11-22 16:58:17 -0500340 // NotiifcationRemoteViews.
341 {{.atomTag = android::util::NOTIFICATION_REMOTE_VIEWS},
342 {.puller = new StatsCompanionServicePuller(android::util::NOTIFICATION_REMOTE_VIEWS)}},
Jeffrey Huangfdd3c7c2020-01-09 13:54:24 -0800343
Stanislav Zholninccb82e42019-11-29 18:59:49 +0000344 // PermissionStateSampled.
345 {{.atomTag = android::util::DANGEROUS_PERMISSION_STATE_SAMPLED},
346 {.puller =
347 new StatsCompanionServicePuller(android::util::DANGEROUS_PERMISSION_STATE_SAMPLED)}},
Chenjie Yu2c0d83d2018-09-02 06:37:08 -0700348};
Chenjie Yu80f91122018-01-31 20:24:50 -0800349
Chenjie Yue2219202018-06-08 10:07:51 -0700350StatsPullerManager::StatsPullerManager() : mNextPullTimeNs(NO_ALARM_UPDATE) {
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700351}
352
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800353bool StatsPullerManager::Pull(int tagId, vector<shared_ptr<LogEvent>>* data) {
Tej Singhfa1c1372019-12-05 20:36:54 -0800354 AutoMutex _l(mLock);
355 return PullLocked(tagId, data);
356}
357
358bool StatsPullerManager::PullLocked(int tagId, vector<shared_ptr<LogEvent>>* data) {
Tej Singh484524a2018-02-01 15:10:05 -0800359 VLOG("Initiating pulling %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700360
Tej Singh6a5c9432019-10-11 11:07:06 -0700361 if (kAllPullAtomInfo.find({.atomTag = tagId}) != kAllPullAtomInfo.end()) {
362 bool ret = kAllPullAtomInfo.find({.atomTag = tagId})->second.puller->Pull(data);
Tej Singh484524a2018-02-01 15:10:05 -0800363 VLOG("pulled %d items", (int)data->size());
Misha Wagner1eee2212019-01-22 11:47:11 +0000364 if (!ret) {
365 StatsdStats::getInstance().notePullFailed(tagId);
366 }
Tej Singh484524a2018-02-01 15:10:05 -0800367 return ret;
Yao Chen93fe3a32017-11-02 13:52:59 -0700368 } else {
Tej Singh484524a2018-02-01 15:10:05 -0800369 VLOG("Unknown tagId %d", tagId);
Yao Chen93fe3a32017-11-02 13:52:59 -0700370 return false; // Return early since we don't know what to pull.
371 }
372}
Chenjie Yub3dda412017-10-24 13:41:59 -0700373
Chenjie Yue2219202018-06-08 10:07:51 -0700374bool StatsPullerManager::PullerForMatcherExists(int tagId) const {
Tej Singhde473b512019-04-10 19:25:03 -0700375 // Vendor pulled atoms might be registered after we parse the config.
Tej Singh6a5c9432019-10-11 11:07:06 -0700376 return isVendorPulledAtom(tagId) ||
377 kAllPullAtomInfo.find({.atomTag = tagId}) != kAllPullAtomInfo.end();
Chenjie Yub3dda412017-10-24 13:41:59 -0700378}
379
Chenjie Yue2219202018-06-08 10:07:51 -0700380void StatsPullerManager::updateAlarmLocked() {
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700381 if (mNextPullTimeNs == NO_ALARM_UPDATE) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700382 VLOG("No need to set alarms. Skipping");
383 return;
384 }
385
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700386 sp<IStatsCompanionService> statsCompanionServiceCopy = mStatsCompanionService;
387 if (statsCompanionServiceCopy != nullptr) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700388 statsCompanionServiceCopy->setPullingAlarm(mNextPullTimeNs / 1000000);
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700389 } else {
390 VLOG("StatsCompanionService not available. Alarm not set.");
391 }
392 return;
393}
394
Chenjie Yue2219202018-06-08 10:07:51 -0700395void StatsPullerManager::SetStatsCompanionService(
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700396 sp<IStatsCompanionService> statsCompanionService) {
397 AutoMutex _l(mLock);
398 sp<IStatsCompanionService> tmpForLock = mStatsCompanionService;
399 mStatsCompanionService = statsCompanionService;
400 for (const auto& pulledAtom : kAllPullAtomInfo) {
401 pulledAtom.second.puller->SetStatsCompanionService(statsCompanionService);
402 }
403 if (mStatsCompanionService != nullptr) {
404 updateAlarmLocked();
405 }
406}
407
Chenjie Yue2219202018-06-08 10:07:51 -0700408void StatsPullerManager::RegisterReceiver(int tagId, wp<PullDataReceiver> receiver,
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700409 int64_t nextPullTimeNs, int64_t intervalNs) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700410 AutoMutex _l(mLock);
Chenjie Yu6736c892017-11-09 10:50:09 -0800411 auto& receivers = mReceivers[tagId];
Chenjie Yub3dda412017-10-24 13:41:59 -0700412 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800413 if (it->receiver == receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700414 VLOG("Receiver already registered of %d", (int)receivers.size());
415 return;
416 }
417 }
418 ReceiverInfo receiverInfo;
419 receiverInfo.receiver = receiver;
Chenjie Yub3dda412017-10-24 13:41:59 -0700420
Chenjie Yu85ed8382017-12-14 16:48:54 -0800421 // Round it to the nearest minutes. This is the limit of alarm manager.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700422 // In practice, we should always have larger buckets.
423 int64_t roundedIntervalNs = intervalNs / NS_PER_SEC / 60 * NS_PER_SEC * 60;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700424 // Scheduled pulling should be at least 1 min apart.
425 // This can be lower in cts tests, in which case we round it to 1 min.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700426 if (roundedIntervalNs < 60 * (int64_t)NS_PER_SEC) {
427 roundedIntervalNs = 60 * (int64_t)NS_PER_SEC;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700428 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700429
430 receiverInfo.intervalNs = roundedIntervalNs;
431 receiverInfo.nextPullTimeNs = nextPullTimeNs;
432 receivers.push_back(receiverInfo);
433
Chenjie Yub3dda412017-10-24 13:41:59 -0700434 // There is only one alarm for all pulled events. So only set it to the smallest denom.
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700435 if (nextPullTimeNs < mNextPullTimeNs) {
436 VLOG("Updating next pull time %lld", (long long)mNextPullTimeNs);
437 mNextPullTimeNs = nextPullTimeNs;
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700438 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700439 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700440 VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700441}
442
Chenjie Yue2219202018-06-08 10:07:51 -0700443void StatsPullerManager::UnRegisterReceiver(int tagId, wp<PullDataReceiver> receiver) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700444 AutoMutex _l(mLock);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700445 if (mReceivers.find(tagId) == mReceivers.end()) {
446 VLOG("Unknown pull code or no receivers: %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700447 return;
448 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700449 auto& receivers = mReceivers.find(tagId)->second;
Chenjie Yub3dda412017-10-24 13:41:59 -0700450 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800451 if (receiver == it->receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700452 receivers.erase(it);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700453 VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700454 return;
455 }
456 }
457}
458
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800459void StatsPullerManager::OnAlarmFired(int64_t elapsedTimeNs) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700460 AutoMutex _l(mLock);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800461 int64_t wallClockNs = getWallClockNs();
Chenjie Yub3dda412017-10-24 13:41:59 -0700462
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700463 int64_t minNextPullTimeNs = NO_ALARM_UPDATE;
Chenjie Yub3dda412017-10-24 13:41:59 -0700464
465 vector<pair<int, vector<ReceiverInfo*>>> needToPull =
466 vector<pair<int, vector<ReceiverInfo*>>>();
467 for (auto& pair : mReceivers) {
468 vector<ReceiverInfo*> receivers = vector<ReceiverInfo*>();
Yao Chen93fe3a32017-11-02 13:52:59 -0700469 if (pair.second.size() != 0) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700470 for (ReceiverInfo& receiverInfo : pair.second) {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800471 if (receiverInfo.nextPullTimeNs <= elapsedTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700472 receivers.push_back(&receiverInfo);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700473 } else {
474 if (receiverInfo.nextPullTimeNs < minNextPullTimeNs) {
475 minNextPullTimeNs = receiverInfo.nextPullTimeNs;
476 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700477 }
478 }
479 if (receivers.size() > 0) {
480 needToPull.push_back(make_pair(pair.first, receivers));
481 }
482 }
483 }
484
485 for (const auto& pullInfo : needToPull) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700486 vector<shared_ptr<LogEvent>> data;
Tej Singhfa1c1372019-12-05 20:36:54 -0800487 bool pullSuccess = PullLocked(pullInfo.first, &data);
Olivier Gaillardc5f11c42019-02-05 12:44:58 +0000488 if (pullSuccess) {
489 StatsdStats::getInstance().notePullDelay(
490 pullInfo.first, getElapsedRealtimeNs() - elapsedTimeNs);
491 } else {
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800492 VLOG("pull failed at %lld, will try again later", (long long)elapsedTimeNs);
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800493 }
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800494
495 // Convention is to mark pull atom timestamp at request time.
496 // If we pull at t0, puller starts at t1, finishes at t2, and send back
497 // at t3, we mark t0 as its timestamp, which should correspond to its
498 // triggering event, such as condition change at t0.
499 // Here the triggering event is alarm fired from AlarmManager.
500 // In ValueMetricProducer and GaugeMetricProducer we do same thing
501 // when pull on condition change, etc.
502 for (auto& event : data) {
503 event->setElapsedTimestampNs(elapsedTimeNs);
504 event->setLogdWallClockTimestampNs(wallClockNs);
505 }
506
507 for (const auto& receiverInfo : pullInfo.second) {
508 sp<PullDataReceiver> receiverPtr = receiverInfo->receiver.promote();
509 if (receiverPtr != nullptr) {
Olivier Gaillard11203df2019-02-06 13:18:09 +0000510 receiverPtr->onDataPulled(data, pullSuccess, elapsedTimeNs);
Olivier Gaillardc5f11c42019-02-05 12:44:58 +0000511 // We may have just come out of a coma, compute next pull time.
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800512 int numBucketsAhead =
513 (elapsedTimeNs - receiverInfo->nextPullTimeNs) / receiverInfo->intervalNs;
514 receiverInfo->nextPullTimeNs += (numBucketsAhead + 1) * receiverInfo->intervalNs;
515 if (receiverInfo->nextPullTimeNs < minNextPullTimeNs) {
516 minNextPullTimeNs = receiverInfo->nextPullTimeNs;
Chenjie Yu6736c892017-11-09 10:50:09 -0800517 }
Chenjie Yu0bd73db2018-12-16 07:37:04 -0800518 } else {
519 VLOG("receiver already gone.");
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700520 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700521 }
522 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700523
Chenjie Yu3b3adcd2018-04-18 16:25:36 -0700524 VLOG("mNextPullTimeNs: %lld updated to %lld", (long long)mNextPullTimeNs,
525 (long long)minNextPullTimeNs);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700526 mNextPullTimeNs = minNextPullTimeNs;
527 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700528}
529
Chenjie Yue2219202018-06-08 10:07:51 -0700530int StatsPullerManager::ForceClearPullerCache() {
Chenjie Yufa22d652018-02-05 14:37:48 -0800531 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800532 for (const auto& pulledAtom : kAllPullAtomInfo) {
533 totalCleared += pulledAtom.second.puller->ForceClearCache();
Chenjie Yue72252b2018-02-01 13:19:35 -0800534 }
Chenjie Yufa22d652018-02-05 14:37:48 -0800535 return totalCleared;
536}
537
Chenjie Yue2219202018-06-08 10:07:51 -0700538int StatsPullerManager::ClearPullerCacheIfNecessary(int64_t timestampNs) {
Chenjie Yufa22d652018-02-05 14:37:48 -0800539 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800540 for (const auto& pulledAtom : kAllPullAtomInfo) {
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700541 totalCleared += pulledAtom.second.puller->ClearCacheIfNecessary(timestampNs);
Chenjie Yufa22d652018-02-05 14:37:48 -0800542 }
543 return totalCleared;
Chenjie Yue72252b2018-02-01 13:19:35 -0800544}
545
Tej Singh6a5c9432019-10-11 11:07:06 -0700546// Deprecated, remove after puller API is complete.
Tej Singha0c89dd2019-01-25 16:39:18 -0800547void StatsPullerManager::RegisterPullerCallback(int32_t atomTag,
548 const sp<IStatsPullerCallback>& callback) {
549 AutoMutex _l(mLock);
550 // Platform pullers cannot be changed.
Tej Singhde473b512019-04-10 19:25:03 -0700551 if (!isVendorPulledAtom(atomTag)) {
552 VLOG("RegisterPullerCallback: atom tag %d is not vendor pulled", atomTag);
Tej Singha0c89dd2019-01-25 16:39:18 -0800553 return;
554 }
555 VLOG("RegisterPullerCallback: adding puller for tag %d", atomTag);
556 StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/true);
Tej Singh6a5c9432019-10-11 11:07:06 -0700557 kAllPullAtomInfo[{.atomTag = atomTag}] = {
558 .puller = new StatsCallbackPullerDeprecated(atomTag, callback)};
559}
560
561void StatsPullerManager::RegisterPullAtomCallback(const int uid, const int32_t atomTag,
562 const int64_t coolDownNs, const int64_t timeoutNs,
563 const vector<int32_t>& additiveFields,
564 const sp<IPullAtomCallback>& callback) {
565 AutoMutex _l(mLock);
566 VLOG("RegisterPullerCallback: adding puller for tag %d", atomTag);
567 // TODO: linkToDeath with the callback so that we can remove it and delete the puller.
568 StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/true);
Tej Singh89817632019-12-09 16:58:08 -0800569 kAllPullAtomInfo[{.atomTag = atomTag}] = {
570 .additiveFields = additiveFields,
571 .coolDownNs = coolDownNs,
572 .puller = new StatsCallbackPuller(atomTag, callback, timeoutNs),
573 .pullTimeoutNs = timeoutNs,
Nick Desaulniersfceda702019-11-21 12:23:57 -0800574 };
Tej Singha0c89dd2019-01-25 16:39:18 -0800575}
576
577void StatsPullerManager::UnregisterPullerCallback(int32_t atomTag) {
578 AutoMutex _l(mLock);
579 // Platform pullers cannot be changed.
Tej Singhde473b512019-04-10 19:25:03 -0700580 if (!isVendorPulledAtom(atomTag)) {
Tej Singha0c89dd2019-01-25 16:39:18 -0800581 return;
582 }
583 StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/false);
Tej Singh6a5c9432019-10-11 11:07:06 -0700584 kAllPullAtomInfo.erase({.atomTag = atomTag});
Tej Singha0c89dd2019-01-25 16:39:18 -0800585}
586
Tej Singhfa1c1372019-12-05 20:36:54 -0800587void StatsPullerManager::UnregisterPullAtomCallback(const int uid, const int32_t atomTag) {
588 AutoMutex _l(mLock);
589 StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/false);
590 kAllPullAtomInfo.erase({.atomTag = atomTag});
591}
592
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700593} // namespace statsd
594} // namespace os
595} // namespace android