| 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> |
| 22 | #include <algorithm> |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 23 | #include <climits> |
| Chenjie Yu | e33bc3b | 2017-11-06 17:56:44 -0800 | [diff] [blame] | 24 | #include "CpuTimePerUidFreqPuller.h" |
| 25 | #include "CpuTimePerUidPuller.h" |
| Tej Singh | bf972d9 | 2018-01-10 20:51:13 -0800 | [diff] [blame] | 26 | #include "ResourceHealthManagerPuller.h" |
| Chenjie Yu | 80f9112 | 2018-01-31 20:24:50 -0800 | [diff] [blame] | 27 | #include "KernelUidCpuActiveTimeReader.h" |
| 28 | #include "KernelUidCpuClusterTimeReader.h" |
| 29 | #include "SubsystemSleepStatePuller.h" |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 30 | #include "StatsCompanionServicePuller.h" |
| Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 31 | #include "StatsPullerManagerImpl.h" |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 32 | #include "StatsService.h" |
| Tej Singh | bf972d9 | 2018-01-10 20:51:13 -0800 | [diff] [blame] | 33 | #include "SubsystemSleepStatePuller.h" |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 34 | #include "logd/LogEvent.h" |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 35 | #include "statslog.h" |
| David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 36 | |
| 37 | #include <iostream> |
| Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 38 | |
| Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 39 | using std::make_shared; |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 40 | using std::map; |
| Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 41 | using std::shared_ptr; |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 42 | using std::string; |
| 43 | using std::vector; |
| Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 44 | using std::list; |
| Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 45 | |
| 46 | namespace android { |
| 47 | namespace os { |
| 48 | namespace statsd { |
| 49 | |
| Chenjie Yu | 80f9112 | 2018-01-31 20:24:50 -0800 | [diff] [blame] | 50 | const std::map<int, PullAtomInfo> StatsPullerManagerImpl::kAllPullAtomInfo = { |
| 51 | // wifi_bytes_transfer |
| 52 | {android::util::WIFI_BYTES_TRANSFER, |
| 53 | {{2, 3, 4, 5}, |
| 54 | {}, |
| 55 | 1, |
| 56 | new StatsCompanionServicePuller(android::util::WIFI_BYTES_TRANSFER)}}, |
| 57 | // wifi_bytes_transfer_by_fg_bg |
| 58 | {android::util::WIFI_BYTES_TRANSFER_BY_FG_BG, |
| 59 | {{3, 4, 5, 6}, |
| 60 | {2}, |
| 61 | 1, |
| 62 | new StatsCompanionServicePuller( |
| 63 | android::util::WIFI_BYTES_TRANSFER_BY_FG_BG)}}, |
| 64 | // mobile_bytes_transfer |
| 65 | {android::util::MOBILE_BYTES_TRANSFER, |
| 66 | {{2, 3, 4, 5}, |
| 67 | {}, |
| 68 | 1, |
| 69 | new StatsCompanionServicePuller(android::util::MOBILE_BYTES_TRANSFER)}}, |
| 70 | // mobile_bytes_transfer_by_fg_bg |
| 71 | {android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG, |
| 72 | {{3, 4, 5, 6}, |
| 73 | {2}, |
| 74 | 1, |
| 75 | new StatsCompanionServicePuller( |
| 76 | android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG)}}, |
| 77 | // bluetooth_bytes_transfer |
| 78 | {android::util::BLUETOOTH_BYTES_TRANSFER, |
| 79 | {{2, 3}, |
| 80 | {}, |
| 81 | 1, |
| 82 | new StatsCompanionServicePuller( |
| 83 | android::util::BLUETOOTH_BYTES_TRANSFER)}}, |
| 84 | // kernel_wakelock |
| 85 | {android::util::KERNEL_WAKELOCK, |
| 86 | {{}, |
| 87 | {}, |
| 88 | 1, |
| 89 | new StatsCompanionServicePuller(android::util::KERNEL_WAKELOCK)}}, |
| 90 | // subsystem_sleep_state |
| 91 | {android::util::SUBSYSTEM_SLEEP_STATE, |
| 92 | {{}, |
| 93 | {}, |
| 94 | 1, |
| 95 | new StatsCompanionServicePuller(android::util::SUBSYSTEM_SLEEP_STATE)}}, |
| 96 | // cpu_time_per_freq |
| 97 | {android::util::CPU_TIME_PER_FREQ, |
| 98 | {{3}, |
| 99 | {2}, |
| 100 | 1, |
| 101 | new StatsCompanionServicePuller(android::util::CPU_TIME_PER_FREQ)}}, |
| 102 | // cpu_time_per_uid |
| 103 | {android::util::CPU_TIME_PER_UID, |
| 104 | {{2, 3}, {}, 1, new CpuTimePerUidPuller()}}, |
| 105 | // cpu_time_per_uid_freq |
| 106 | {android::util::CPU_TIME_PER_UID_FREQ, |
| 107 | {{3}, {2}, 1, new CpuTimePerUidFreqPuller()}}, |
| 108 | // wifi_activity_energy_info |
| 109 | {android::util::WIFI_ACTIVITY_ENERGY_INFO, |
| 110 | {{}, |
| 111 | {}, |
| 112 | 1, |
| 113 | new StatsCompanionServicePuller( |
| 114 | android::util::WIFI_ACTIVITY_ENERGY_INFO)}}, |
| 115 | // modem_activity_info |
| 116 | {android::util::MODEM_ACTIVITY_INFO, |
| 117 | {{}, |
| 118 | {}, |
| 119 | 1, |
| 120 | new StatsCompanionServicePuller(android::util::MODEM_ACTIVITY_INFO)}}, |
| 121 | // bluetooth_activity_info |
| 122 | {android::util::BLUETOOTH_ACTIVITY_INFO, |
| 123 | {{}, |
| 124 | {}, |
| 125 | 1, |
| 126 | new StatsCompanionServicePuller(android::util::BLUETOOTH_ACTIVITY_INFO)}}, |
| 127 | // system_elapsed_realtime |
| 128 | {android::util::SYSTEM_ELAPSED_REALTIME, |
| 129 | {{}, |
| 130 | {}, |
| 131 | 1, |
| 132 | new StatsCompanionServicePuller(android::util::SYSTEM_ELAPSED_REALTIME)}}, |
| 133 | // system_uptime |
| 134 | {android::util::SYSTEM_UPTIME, |
| 135 | {{}, |
| 136 | {}, |
| 137 | 1, |
| 138 | new StatsCompanionServicePuller(android::util::SYSTEM_UPTIME)}}, |
| 139 | // cpu_active_time |
| 140 | {android::util::CPU_ACTIVE_TIME, |
| 141 | {{3}, {2}, 1, new KernelUidCpuActiveTimeReader()}}, |
| 142 | // cpu_cluster_time |
| 143 | {android::util::CPU_CLUSTER_TIME, |
| 144 | {{3}, {2}, 1, new KernelUidCpuClusterTimeReader()}}, |
| 145 | // disk_space |
| 146 | {android::util::DISK_SPACE, |
| 147 | {{}, {}, 1, new StatsCompanionServicePuller(android::util::DISK_SPACE)}}, |
| 148 | // remaining_battery_capacity |
| 149 | {android::util::REMAINING_BATTERY_CAPACITY, |
| 150 | {{}, |
| 151 | {}, |
| 152 | 1, |
| 153 | new ResourceHealthManagerPuller( |
| 154 | android::util::REMAINING_BATTERY_CAPACITY)}}, |
| 155 | // full_battery_capacity |
| 156 | {android::util::FULL_BATTERY_CAPACITY, |
| 157 | {{}, |
| 158 | {}, |
| 159 | 1, |
| 160 | new ResourceHealthManagerPuller(android::util::FULL_BATTERY_CAPACITY)}}}; |
| 161 | |
| Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 162 | StatsPullerManagerImpl::StatsPullerManagerImpl() |
| Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 163 | : mCurrentPullingInterval(LONG_MAX) { |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 164 | mStatsCompanionService = StatsService::getStatsCompanionService(); |
| Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 167 | bool StatsPullerManagerImpl::Pull(int tagId, vector<shared_ptr<LogEvent>>* data) { |
| Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 168 | if (DEBUG) ALOGD("Initiating pulling %d", tagId); |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 169 | |
| Chenjie Yu | 80f9112 | 2018-01-31 20:24:50 -0800 | [diff] [blame] | 170 | if (kAllPullAtomInfo.find(tagId) != kAllPullAtomInfo.end()) { |
| 171 | bool ret = kAllPullAtomInfo.find(tagId)->second.puller->Pull(data); |
| 172 | ALOGD("pulled %d items", (int)data->size()); |
| 173 | return ret; |
| Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 174 | } else { |
| 175 | ALOGD("Unknown tagId %d", tagId); |
| 176 | return false; // Return early since we don't know what to pull. |
| 177 | } |
| 178 | } |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 179 | |
| Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 180 | StatsPullerManagerImpl& StatsPullerManagerImpl::GetInstance() { |
| 181 | static StatsPullerManagerImpl instance; |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 182 | return instance; |
| 183 | } |
| 184 | |
| Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 185 | bool StatsPullerManagerImpl::PullerForMatcherExists(int tagId) const { |
| Chenjie Yu | 80f9112 | 2018-01-31 20:24:50 -0800 | [diff] [blame] | 186 | return kAllPullAtomInfo.find(tagId) != kAllPullAtomInfo.end(); |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 189 | void StatsPullerManagerImpl::RegisterReceiver(int tagId, wp<PullDataReceiver> receiver, |
| 190 | long intervalMs) { |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 191 | AutoMutex _l(mReceiversLock); |
| Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 192 | auto& receivers = mReceivers[tagId]; |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 193 | for (auto it = receivers.begin(); it != receivers.end(); it++) { |
| Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 194 | if (it->receiver == receiver) { |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 195 | VLOG("Receiver already registered of %d", (int)receivers.size()); |
| 196 | return; |
| 197 | } |
| 198 | } |
| 199 | ReceiverInfo receiverInfo; |
| 200 | receiverInfo.receiver = receiver; |
| 201 | receiverInfo.timeInfo.first = intervalMs; |
| 202 | receivers.push_back(receiverInfo); |
| 203 | |
| Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 204 | // Round it to the nearest minutes. This is the limit of alarm manager. |
| 205 | // In practice, we should limit it higher. |
| 206 | long roundedIntervalMs = intervalMs/1000/60 * 1000 * 60; |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 207 | // There is only one alarm for all pulled events. So only set it to the smallest denom. |
| Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 208 | if (roundedIntervalMs < mCurrentPullingInterval) { |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 209 | VLOG("Updating pulling interval %ld", intervalMs); |
| Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 210 | mCurrentPullingInterval = roundedIntervalMs; |
| 211 | long currentTimeMs = time(nullptr) * 1000; |
| 212 | long nextAlarmTimeMs = currentTimeMs + mCurrentPullingInterval - (currentTimeMs - mTimeBaseSec * 1000) % mCurrentPullingInterval; |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 213 | if (mStatsCompanionService != nullptr) { |
| Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 214 | mStatsCompanionService->setPullingAlarms(nextAlarmTimeMs, mCurrentPullingInterval); |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 215 | } else { |
| 216 | VLOG("Failed to update pulling interval"); |
| 217 | } |
| 218 | } |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 219 | VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size()); |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 222 | void StatsPullerManagerImpl::UnRegisterReceiver(int tagId, wp<PullDataReceiver> receiver) { |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 223 | AutoMutex _l(mReceiversLock); |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 224 | if (mReceivers.find(tagId) == mReceivers.end()) { |
| 225 | VLOG("Unknown pull code or no receivers: %d", tagId); |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 226 | return; |
| 227 | } |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 228 | auto& receivers = mReceivers.find(tagId)->second; |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 229 | for (auto it = receivers.begin(); it != receivers.end(); it++) { |
| Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 230 | if (receiver == it->receiver) { |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 231 | receivers.erase(it); |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 232 | VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size()); |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 233 | return; |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 238 | void StatsPullerManagerImpl::OnAlarmFired() { |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 239 | AutoMutex _l(mReceiversLock); |
| 240 | |
| Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 241 | uint64_t currentTimeMs = time(nullptr) /60 * 60 * 1000; |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 242 | |
| 243 | vector<pair<int, vector<ReceiverInfo*>>> needToPull = |
| 244 | vector<pair<int, vector<ReceiverInfo*>>>(); |
| 245 | for (auto& pair : mReceivers) { |
| 246 | vector<ReceiverInfo*> receivers = vector<ReceiverInfo*>(); |
| Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 247 | if (pair.second.size() != 0) { |
| 248 | for (auto& receiverInfo : pair.second) { |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 249 | if (receiverInfo.timeInfo.first + receiverInfo.timeInfo.second > currentTimeMs) { |
| 250 | receivers.push_back(&receiverInfo); |
| 251 | } |
| 252 | } |
| 253 | if (receivers.size() > 0) { |
| 254 | needToPull.push_back(make_pair(pair.first, receivers)); |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | for (const auto& pullInfo : needToPull) { |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 260 | vector<shared_ptr<LogEvent>> data; |
| 261 | if (Pull(pullInfo.first, &data)) { |
| 262 | for (const auto& receiverInfo : pullInfo.second) { |
| Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 263 | sp<PullDataReceiver> receiverPtr = receiverInfo->receiver.promote(); |
| 264 | if (receiverPtr != nullptr) { |
| 265 | receiverPtr->onDataPulled(data); |
| 266 | receiverInfo->timeInfo.second = currentTimeMs; |
| 267 | } else { |
| 268 | VLOG("receiver already gone."); |
| 269 | } |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 270 | } |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 275 | int StatsPullerManagerImpl::ForceClearPullerCache() { |
| 276 | int totalCleared = 0; |
| Chenjie Yu | 80f9112 | 2018-01-31 20:24:50 -0800 | [diff] [blame] | 277 | for (const auto& pulledAtom : kAllPullAtomInfo) { |
| 278 | totalCleared += pulledAtom.second.puller->ForceClearCache(); |
| Chenjie Yu | e72252b | 2018-02-01 13:19:35 -0800 | [diff] [blame] | 279 | } |
| Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 280 | return totalCleared; |
| 281 | } |
| 282 | |
| 283 | int StatsPullerManagerImpl::ClearPullerCacheIfNecessary(long timestampSec) { |
| 284 | int totalCleared = 0; |
| Chenjie Yu | 80f9112 | 2018-01-31 20:24:50 -0800 | [diff] [blame] | 285 | for (const auto& pulledAtom : kAllPullAtomInfo) { |
| 286 | totalCleared += pulledAtom.second.puller->ClearCacheIfNecessary(timestampSec); |
| Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 287 | } |
| 288 | return totalCleared; |
| Chenjie Yu | e72252b | 2018-02-01 13:19:35 -0800 | [diff] [blame] | 289 | } |
| 290 | |
| Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 291 | } // namespace statsd |
| 292 | } // namespace os |
| 293 | } // namespace android |