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