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