Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 1 | /* |
| 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 Yu | 80f9112 | 2018-01-31 20:24:50 -0800 | [diff] [blame] | 17 | #define DEBUG false |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 18 | #include "Log.h" |
Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 19 | |
Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 20 | #include <android/os/IStatsCompanionService.h> |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 21 | #include <cutils/log.h> |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 22 | #include <math.h> |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 23 | #include <algorithm> |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 24 | #include <climits> |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 25 | #include "../StatsService.h" |
Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 26 | #include "../logd/LogEvent.h" |
| 27 | #include "../stats_log_util.h" |
| 28 | #include "../statscompanion_util.h" |
Tej Singh | 4029831 | 2018-02-16 00:15:09 -0800 | [diff] [blame] | 29 | #include "ResourceHealthManagerPuller.h" |
| 30 | #include "ResourceThermalManagerPuller.h" |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 31 | #include "StatsCompanionServicePuller.h" |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 32 | #include "StatsPullerManagerImpl.h" |
Tej Singh | bf972d9 | 2018-01-10 20:51:13 -0800 | [diff] [blame] | 33 | #include "SubsystemSleepStatePuller.h" |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 34 | #include "statslog.h" |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 35 | |
| 36 | #include <iostream> |
Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 37 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 38 | using std::make_shared; |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 39 | using std::map; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 40 | using std::shared_ptr; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 41 | using std::string; |
| 42 | using std::vector; |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 43 | using std::list; |
Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 44 | |
| 45 | namespace android { |
| 46 | namespace os { |
| 47 | namespace statsd { |
| 48 | |
Chenjie Yu | 80f9112 | 2018-01-31 20:24:50 -0800 | [diff] [blame] | 49 | const std::map<int, PullAtomInfo> StatsPullerManagerImpl::kAllPullAtomInfo = { |
Chenjie Yu | feba309 | 2018-02-08 14:33:37 -0800 | [diff] [blame] | 50 | // wifi_bytes_transfer |
| 51 | {android::util::WIFI_BYTES_TRANSFER, |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 52 | {{2, 3, 4, 5}, |
| 53 | {}, |
| 54 | 1 * NS_PER_SEC, |
Chenjie Yu | feba309 | 2018-02-08 14:33:37 -0800 | [diff] [blame] | 55 | new StatsCompanionServicePuller(android::util::WIFI_BYTES_TRANSFER)}}, |
| 56 | // wifi_bytes_transfer_by_fg_bg |
| 57 | {android::util::WIFI_BYTES_TRANSFER_BY_FG_BG, |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 58 | {{3, 4, 5, 6}, |
| 59 | {2}, |
| 60 | 1 * NS_PER_SEC, |
Chenjie Yu | feba309 | 2018-02-08 14:33:37 -0800 | [diff] [blame] | 61 | new StatsCompanionServicePuller(android::util::WIFI_BYTES_TRANSFER_BY_FG_BG)}}, |
| 62 | // mobile_bytes_transfer |
| 63 | {android::util::MOBILE_BYTES_TRANSFER, |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 64 | {{2, 3, 4, 5}, |
| 65 | {}, |
| 66 | 1 * NS_PER_SEC, |
Chenjie Yu | feba309 | 2018-02-08 14:33:37 -0800 | [diff] [blame] | 67 | new StatsCompanionServicePuller(android::util::MOBILE_BYTES_TRANSFER)}}, |
| 68 | // mobile_bytes_transfer_by_fg_bg |
| 69 | {android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG, |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 70 | {{3, 4, 5, 6}, |
| 71 | {2}, |
| 72 | 1 * NS_PER_SEC, |
Chenjie Yu | feba309 | 2018-02-08 14:33:37 -0800 | [diff] [blame] | 73 | new StatsCompanionServicePuller(android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG)}}, |
| 74 | // bluetooth_bytes_transfer |
| 75 | {android::util::BLUETOOTH_BYTES_TRANSFER, |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 76 | {{2, 3}, |
| 77 | {}, |
| 78 | 1 * NS_PER_SEC, |
| 79 | new StatsCompanionServicePuller(android::util::BLUETOOTH_BYTES_TRANSFER)}}, |
Chenjie Yu | feba309 | 2018-02-08 14:33:37 -0800 | [diff] [blame] | 80 | // kernel_wakelock |
| 81 | {android::util::KERNEL_WAKELOCK, |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 82 | {{}, {}, 1 * NS_PER_SEC, new StatsCompanionServicePuller(android::util::KERNEL_WAKELOCK)}}, |
Chenjie Yu | feba309 | 2018-02-08 14:33:37 -0800 | [diff] [blame] | 83 | // subsystem_sleep_state |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 84 | {android::util::SUBSYSTEM_SLEEP_STATE, |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 85 | {{}, {}, 1 * NS_PER_SEC, new SubsystemSleepStatePuller()}}, |
Chenjie Yu | feba309 | 2018-02-08 14:33:37 -0800 | [diff] [blame] | 86 | // cpu_time_per_freq |
| 87 | {android::util::CPU_TIME_PER_FREQ, |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 88 | {{3}, |
| 89 | {2}, |
| 90 | 1 * NS_PER_SEC, |
| 91 | new StatsCompanionServicePuller(android::util::CPU_TIME_PER_FREQ)}}, |
Chenjie Yu | feba309 | 2018-02-08 14:33:37 -0800 | [diff] [blame] | 92 | // cpu_time_per_uid |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 93 | {android::util::CPU_TIME_PER_UID, |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 94 | {{2, 3}, |
| 95 | {}, |
| 96 | 1 * NS_PER_SEC, |
| 97 | new StatsCompanionServicePuller(android::util::CPU_TIME_PER_UID)}}, |
Chenjie Yu | feba309 | 2018-02-08 14:33:37 -0800 | [diff] [blame] | 98 | // cpu_time_per_uid_freq |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 99 | // the throttling is 3sec, handled in |
| 100 | // frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 101 | {android::util::CPU_TIME_PER_UID_FREQ, |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 102 | {{4}, |
| 103 | {2, 3}, |
| 104 | 1 * NS_PER_SEC, |
| 105 | new StatsCompanionServicePuller(android::util::CPU_TIME_PER_UID_FREQ)}}, |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 106 | // cpu_active_time |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 107 | // the throttling is 3sec, handled in |
| 108 | // frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 109 | {android::util::CPU_ACTIVE_TIME, |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 110 | {{2}, |
| 111 | {}, |
| 112 | 1 * NS_PER_SEC, |
| 113 | new StatsCompanionServicePuller(android::util::CPU_ACTIVE_TIME)}}, |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 114 | // cpu_cluster_time |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 115 | // the throttling is 3sec, handled in |
| 116 | // frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 117 | {android::util::CPU_CLUSTER_TIME, |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 118 | {{3}, |
| 119 | {2}, |
| 120 | 1 * NS_PER_SEC, |
| 121 | new StatsCompanionServicePuller(android::util::CPU_CLUSTER_TIME)}}, |
Chenjie Yu | feba309 | 2018-02-08 14:33:37 -0800 | [diff] [blame] | 122 | // wifi_activity_energy_info |
Chenjie Yu | 5caaa9d | 2018-03-06 15:48:54 -0800 | [diff] [blame^] | 123 | {android::util::WIFI_ACTIVITY_INFO, |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 124 | {{}, |
| 125 | {}, |
| 126 | 1 * NS_PER_SEC, |
Chenjie Yu | 5caaa9d | 2018-03-06 15:48:54 -0800 | [diff] [blame^] | 127 | new StatsCompanionServicePuller(android::util::WIFI_ACTIVITY_INFO)}}, |
Chenjie Yu | feba309 | 2018-02-08 14:33:37 -0800 | [diff] [blame] | 128 | // modem_activity_info |
| 129 | {android::util::MODEM_ACTIVITY_INFO, |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 130 | {{}, |
| 131 | {}, |
| 132 | 1 * NS_PER_SEC, |
| 133 | new StatsCompanionServicePuller(android::util::MODEM_ACTIVITY_INFO)}}, |
Chenjie Yu | feba309 | 2018-02-08 14:33:37 -0800 | [diff] [blame] | 134 | // bluetooth_activity_info |
| 135 | {android::util::BLUETOOTH_ACTIVITY_INFO, |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 136 | {{}, |
| 137 | {}, |
| 138 | 1 * NS_PER_SEC, |
| 139 | new StatsCompanionServicePuller(android::util::BLUETOOTH_ACTIVITY_INFO)}}, |
Chenjie Yu | feba309 | 2018-02-08 14:33:37 -0800 | [diff] [blame] | 140 | // system_elapsed_realtime |
| 141 | {android::util::SYSTEM_ELAPSED_REALTIME, |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 142 | {{}, |
| 143 | {}, |
| 144 | 1 * NS_PER_SEC, |
| 145 | new StatsCompanionServicePuller(android::util::SYSTEM_ELAPSED_REALTIME)}}, |
Chenjie Yu | feba309 | 2018-02-08 14:33:37 -0800 | [diff] [blame] | 146 | // system_uptime |
| 147 | {android::util::SYSTEM_UPTIME, |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 148 | {{}, {}, 1 * NS_PER_SEC, new StatsCompanionServicePuller(android::util::SYSTEM_UPTIME)}}, |
Chenjie Yu | feba309 | 2018-02-08 14:33:37 -0800 | [diff] [blame] | 149 | // disk_space |
| 150 | {android::util::DISK_SPACE, |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 151 | {{}, {}, 1 * NS_PER_SEC, new StatsCompanionServicePuller(android::util::DISK_SPACE)}}, |
Chenjie Yu | feba309 | 2018-02-08 14:33:37 -0800 | [diff] [blame] | 152 | // remaining_battery_capacity |
| 153 | {android::util::REMAINING_BATTERY_CAPACITY, |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 154 | {{}, |
| 155 | {}, |
| 156 | 1 * NS_PER_SEC, |
| 157 | new ResourceHealthManagerPuller(android::util::REMAINING_BATTERY_CAPACITY)}}, |
Chenjie Yu | feba309 | 2018-02-08 14:33:37 -0800 | [diff] [blame] | 158 | // full_battery_capacity |
| 159 | {android::util::FULL_BATTERY_CAPACITY, |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 160 | {{}, |
| 161 | {}, |
| 162 | 1 * NS_PER_SEC, |
| 163 | new ResourceHealthManagerPuller(android::util::FULL_BATTERY_CAPACITY)}}, |
Rajeev Kumar | 22d92b7 | 2018-02-07 18:38:36 -0800 | [diff] [blame] | 164 | // process_memory_state |
| 165 | {android::util::PROCESS_MEMORY_STATE, |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 166 | {{4, 5, 6, 7, 8}, |
| 167 | {2, 3}, |
| 168 | 1 * NS_PER_SEC, |
Chenjie Yu | ec67661 | 2018-03-07 09:19:17 -0800 | [diff] [blame] | 169 | new StatsCompanionServicePuller(android::util::PROCESS_MEMORY_STATE)}}, |
Tej Singh | 4029831 | 2018-02-16 00:15:09 -0800 | [diff] [blame] | 170 | // temperature |
| 171 | {android::util::TEMPERATURE, {{}, {}, 1, new ResourceThermalManagerPuller()}}}; |
Chenjie Yu | 80f9112 | 2018-01-31 20:24:50 -0800 | [diff] [blame] | 172 | |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 173 | StatsPullerManagerImpl::StatsPullerManagerImpl() : mNextPullTimeNs(LONG_MAX) { |
Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 176 | bool StatsPullerManagerImpl::Pull(const int tagId, const int64_t timeNs, |
| 177 | vector<shared_ptr<LogEvent>>* data) { |
Tej Singh | 484524a | 2018-02-01 15:10:05 -0800 | [diff] [blame] | 178 | VLOG("Initiating pulling %d", tagId); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 179 | |
Chenjie Yu | 80f9112 | 2018-01-31 20:24:50 -0800 | [diff] [blame] | 180 | if (kAllPullAtomInfo.find(tagId) != kAllPullAtomInfo.end()) { |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 181 | bool ret = kAllPullAtomInfo.find(tagId)->second.puller->Pull(timeNs, data); |
Tej Singh | 484524a | 2018-02-01 15:10:05 -0800 | [diff] [blame] | 182 | VLOG("pulled %d items", (int)data->size()); |
| 183 | return ret; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 184 | } else { |
Tej Singh | 484524a | 2018-02-01 15:10:05 -0800 | [diff] [blame] | 185 | VLOG("Unknown tagId %d", tagId); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 186 | return false; // Return early since we don't know what to pull. |
| 187 | } |
| 188 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 189 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 190 | StatsPullerManagerImpl& StatsPullerManagerImpl::GetInstance() { |
| 191 | static StatsPullerManagerImpl instance; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 192 | return instance; |
| 193 | } |
| 194 | |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 195 | bool StatsPullerManagerImpl::PullerForMatcherExists(int tagId) const { |
Chenjie Yu | feba309 | 2018-02-08 14:33:37 -0800 | [diff] [blame] | 196 | return kAllPullAtomInfo.find(tagId) != kAllPullAtomInfo.end(); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 199 | void StatsPullerManagerImpl::updateAlarmLocked() { |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 200 | if (mNextPullTimeNs == LONG_MAX) { |
| 201 | VLOG("No need to set alarms. Skipping"); |
| 202 | return; |
| 203 | } |
| 204 | |
Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 205 | sp<IStatsCompanionService> statsCompanionServiceCopy = mStatsCompanionService; |
| 206 | if (statsCompanionServiceCopy != nullptr) { |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 207 | statsCompanionServiceCopy->setPullingAlarm(mNextPullTimeNs / 1000000); |
Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 208 | } else { |
| 209 | VLOG("StatsCompanionService not available. Alarm not set."); |
| 210 | } |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | void StatsPullerManagerImpl::SetStatsCompanionService( |
| 215 | sp<IStatsCompanionService> statsCompanionService) { |
| 216 | AutoMutex _l(mLock); |
| 217 | sp<IStatsCompanionService> tmpForLock = mStatsCompanionService; |
| 218 | mStatsCompanionService = statsCompanionService; |
| 219 | for (const auto& pulledAtom : kAllPullAtomInfo) { |
| 220 | pulledAtom.second.puller->SetStatsCompanionService(statsCompanionService); |
| 221 | } |
| 222 | if (mStatsCompanionService != nullptr) { |
| 223 | updateAlarmLocked(); |
| 224 | } |
| 225 | } |
| 226 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 227 | void StatsPullerManagerImpl::RegisterReceiver(int tagId, wp<PullDataReceiver> receiver, |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 228 | int64_t nextPullTimeNs, int64_t intervalNs) { |
Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 229 | AutoMutex _l(mLock); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 230 | auto& receivers = mReceivers[tagId]; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 231 | for (auto it = receivers.begin(); it != receivers.end(); it++) { |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 232 | if (it->receiver == receiver) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 233 | VLOG("Receiver already registered of %d", (int)receivers.size()); |
| 234 | return; |
| 235 | } |
| 236 | } |
| 237 | ReceiverInfo receiverInfo; |
| 238 | receiverInfo.receiver = receiver; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 239 | |
Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 240 | // Round it to the nearest minutes. This is the limit of alarm manager. |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 241 | // In practice, we should always have larger buckets. |
| 242 | int64_t roundedIntervalNs = intervalNs / NS_PER_SEC / 60 * NS_PER_SEC * 60; |
Chenjie Yu | 83baaa1 | 2018-03-19 10:41:35 -0700 | [diff] [blame] | 243 | // Scheduled pulling should be at least 1 min apart. |
| 244 | // This can be lower in cts tests, in which case we round it to 1 min. |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 245 | if (roundedIntervalNs < 60 * (int64_t)NS_PER_SEC) { |
| 246 | roundedIntervalNs = 60 * (int64_t)NS_PER_SEC; |
Chenjie Yu | 83baaa1 | 2018-03-19 10:41:35 -0700 | [diff] [blame] | 247 | } |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 248 | |
| 249 | receiverInfo.intervalNs = roundedIntervalNs; |
| 250 | receiverInfo.nextPullTimeNs = nextPullTimeNs; |
| 251 | receivers.push_back(receiverInfo); |
| 252 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 253 | // There is only one alarm for all pulled events. So only set it to the smallest denom. |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 254 | if (nextPullTimeNs < mNextPullTimeNs) { |
| 255 | VLOG("Updating next pull time %lld", (long long)mNextPullTimeNs); |
| 256 | mNextPullTimeNs = nextPullTimeNs; |
Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 257 | updateAlarmLocked(); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 258 | } |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 259 | VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size()); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 262 | void StatsPullerManagerImpl::UnRegisterReceiver(int tagId, wp<PullDataReceiver> receiver) { |
Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 263 | AutoMutex _l(mLock); |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 264 | if (mReceivers.find(tagId) == mReceivers.end()) { |
| 265 | VLOG("Unknown pull code or no receivers: %d", tagId); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 266 | return; |
| 267 | } |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 268 | auto& receivers = mReceivers.find(tagId)->second; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 269 | for (auto it = receivers.begin(); it != receivers.end(); it++) { |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 270 | if (receiver == it->receiver) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 271 | receivers.erase(it); |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 272 | VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size()); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 273 | return; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 278 | void StatsPullerManagerImpl::OnAlarmFired() { |
Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 279 | AutoMutex _l(mLock); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 280 | |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 281 | int64_t currentTimeNs = getElapsedRealtimeNs(); |
| 282 | |
| 283 | int64_t minNextPullTimeNs = LONG_MAX; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 284 | |
| 285 | vector<pair<int, vector<ReceiverInfo*>>> needToPull = |
| 286 | vector<pair<int, vector<ReceiverInfo*>>>(); |
| 287 | for (auto& pair : mReceivers) { |
| 288 | vector<ReceiverInfo*> receivers = vector<ReceiverInfo*>(); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 289 | if (pair.second.size() != 0) { |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 290 | for (ReceiverInfo& receiverInfo : pair.second) { |
| 291 | if (receiverInfo.nextPullTimeNs < currentTimeNs) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 292 | receivers.push_back(&receiverInfo); |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 293 | } else { |
| 294 | if (receiverInfo.nextPullTimeNs < minNextPullTimeNs) { |
| 295 | minNextPullTimeNs = receiverInfo.nextPullTimeNs; |
| 296 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | if (receivers.size() > 0) { |
| 300 | needToPull.push_back(make_pair(pair.first, receivers)); |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | for (const auto& pullInfo : needToPull) { |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 306 | vector<shared_ptr<LogEvent>> data; |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 307 | if (Pull(pullInfo.first, currentTimeNs, &data)) { |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 308 | for (const auto& receiverInfo : pullInfo.second) { |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 309 | sp<PullDataReceiver> receiverPtr = receiverInfo->receiver.promote(); |
| 310 | if (receiverPtr != nullptr) { |
| 311 | receiverPtr->onDataPulled(data); |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 312 | // we may have just come out of a coma, compute next pull time |
| 313 | receiverInfo->nextPullTimeNs = |
| 314 | ceil((double_t)(currentTimeNs - receiverInfo->nextPullTimeNs) / |
| 315 | receiverInfo->intervalNs) * |
| 316 | receiverInfo->intervalNs + |
| 317 | receiverInfo->nextPullTimeNs; |
| 318 | if (receiverInfo->nextPullTimeNs < minNextPullTimeNs) { |
| 319 | minNextPullTimeNs = receiverInfo->nextPullTimeNs; |
| 320 | } |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 321 | } else { |
| 322 | VLOG("receiver already gone."); |
| 323 | } |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 324 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 325 | } |
| 326 | } |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 327 | |
| 328 | mNextPullTimeNs = minNextPullTimeNs; |
| 329 | updateAlarmLocked(); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 332 | int StatsPullerManagerImpl::ForceClearPullerCache() { |
| 333 | int totalCleared = 0; |
Chenjie Yu | 80f9112 | 2018-01-31 20:24:50 -0800 | [diff] [blame] | 334 | for (const auto& pulledAtom : kAllPullAtomInfo) { |
| 335 | totalCleared += pulledAtom.second.puller->ForceClearCache(); |
Chenjie Yu | e72252b | 2018-02-01 13:19:35 -0800 | [diff] [blame] | 336 | } |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 337 | return totalCleared; |
| 338 | } |
| 339 | |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 340 | int StatsPullerManagerImpl::ClearPullerCacheIfNecessary(int64_t timestampNs) { |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 341 | int totalCleared = 0; |
Chenjie Yu | 80f9112 | 2018-01-31 20:24:50 -0800 | [diff] [blame] | 342 | for (const auto& pulledAtom : kAllPullAtomInfo) { |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 343 | totalCleared += pulledAtom.second.puller->ClearCacheIfNecessary(timestampNs); |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 344 | } |
| 345 | return totalCleared; |
Chenjie Yu | e72252b | 2018-02-01 13:19:35 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 348 | } // namespace statsd |
| 349 | } // namespace os |
| 350 | } // namespace android |