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