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 | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 17 | #define DEBUG true |
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 | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 24 | #include "ResourcePowerManagerPuller.h" |
| 25 | #include "StatsCompanionServicePuller.h" |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 26 | #include "StatsPullerManager.h" |
| 27 | #include "StatsService.h" |
| 28 | #include "logd/LogEvent.h" |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 29 | #include "statslog.h" |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 30 | |
| 31 | #include <iostream> |
Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 32 | |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 33 | using std::map; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 34 | using std::string; |
| 35 | using std::vector; |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 36 | using std::make_shared; |
| 37 | using std::shared_ptr; |
Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 38 | |
| 39 | namespace android { |
| 40 | namespace os { |
| 41 | namespace statsd { |
| 42 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 43 | StatsPullerManager::StatsPullerManager() |
| 44 | : mCurrentPullingInterval(LONG_MAX), mPullStartTimeMs(get_pull_start_time_ms()) { |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 45 | shared_ptr<StatsPuller> statsCompanionServicePuller = make_shared<StatsCompanionServicePuller>(); |
| 46 | shared_ptr <StatsPuller> |
| 47 | resourcePowerManagerPuller = make_shared<ResourcePowerManagerPuller>(); |
| 48 | |
| 49 | mPullers.insert({android::util::KERNEL_WAKELOCK_PULLED, |
| 50 | statsCompanionServicePuller}); |
| 51 | mPullers.insert({android::util::WIFI_BYTES_TRANSFERRED, |
| 52 | statsCompanionServicePuller}); |
| 53 | mPullers.insert({android::util::MOBILE_BYTES_TRANSFERRED, |
| 54 | statsCompanionServicePuller}); |
| 55 | mPullers.insert({android::util::WIFI_BYTES_TRANSFERRED_BY_FG_BG, |
| 56 | statsCompanionServicePuller}); |
| 57 | mPullers.insert({android::util::MOBILE_BYTES_TRANSFERRED_BY_FG_BG, |
| 58 | statsCompanionServicePuller}); |
| 59 | mPullers.insert({android::util::POWER_STATE_PLATFORM_SLEEP_STATE_PULLED, |
| 60 | resourcePowerManagerPuller}); |
| 61 | mPullers.insert({android::util::POWER_STATE_VOTER_PULLED, |
| 62 | resourcePowerManagerPuller}); |
| 63 | mPullers.insert({android::util::POWER_STATE_SUBSYSTEM_SLEEP_STATE_PULLED, |
| 64 | resourcePowerManagerPuller}); |
| 65 | |
| 66 | mStatsCompanionService = StatsService::getStatsCompanionService(); |
Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 69 | bool StatsPullerManager::Pull(int tagId, vector<shared_ptr<LogEvent>>* data) { |
| 70 | if (DEBUG) ALOGD("Initiating pulling %d", tagId); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 71 | |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 72 | if (mPullers.find(tagId) != mPullers.end()) { |
| 73 | return mPullers.find(tagId)->second->Pull(tagId, data); |
| 74 | } else { |
| 75 | ALOGD("Unknown tagId %d", tagId); |
| 76 | return false; // Return early since we don't know what to pull. |
| 77 | } |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 78 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 79 | |
| 80 | StatsPullerManager& StatsPullerManager::GetInstance() { |
| 81 | static StatsPullerManager instance; |
| 82 | return instance; |
| 83 | } |
| 84 | |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 85 | bool StatsPullerManager::PullerForMatcherExists(int tagId) { |
| 86 | return mPullers.find(tagId) != mPullers.end(); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | long StatsPullerManager::get_pull_start_time_ms() { |
| 90 | // TODO: limit and align pull intervals to 10min boundaries if this turns out to be a problem |
| 91 | return time(nullptr) * 1000; |
| 92 | } |
| 93 | |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 94 | void StatsPullerManager::RegisterReceiver(int tagId, sp<PullDataReceiver> receiver, long intervalMs) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 95 | AutoMutex _l(mReceiversLock); |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 96 | vector<ReceiverInfo>& receivers = mReceivers[tagId]; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 97 | for (auto it = receivers.begin(); it != receivers.end(); it++) { |
| 98 | if (it->receiver.get() == receiver.get()) { |
| 99 | VLOG("Receiver already registered of %d", (int)receivers.size()); |
| 100 | return; |
| 101 | } |
| 102 | } |
| 103 | ReceiverInfo receiverInfo; |
| 104 | receiverInfo.receiver = receiver; |
| 105 | receiverInfo.timeInfo.first = intervalMs; |
| 106 | receivers.push_back(receiverInfo); |
| 107 | |
| 108 | // There is only one alarm for all pulled events. So only set it to the smallest denom. |
| 109 | if (intervalMs < mCurrentPullingInterval) { |
| 110 | VLOG("Updating pulling interval %ld", intervalMs); |
| 111 | mCurrentPullingInterval = intervalMs; |
| 112 | if (mStatsCompanionService != nullptr) { |
| 113 | mStatsCompanionService->setPullingAlarms(mPullStartTimeMs, mCurrentPullingInterval); |
| 114 | } else { |
| 115 | VLOG("Failed to update pulling interval"); |
| 116 | } |
| 117 | } |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 118 | VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size()); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 121 | void StatsPullerManager::UnRegisterReceiver(int tagId, sp<PullDataReceiver> receiver) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 122 | AutoMutex _l(mReceiversLock); |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 123 | if (mReceivers.find(tagId) == mReceivers.end()) { |
| 124 | VLOG("Unknown pull code or no receivers: %d", tagId); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 125 | return; |
| 126 | } |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 127 | auto& receivers = mReceivers.find(tagId)->second; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 128 | for (auto it = receivers.begin(); it != receivers.end(); it++) { |
| 129 | if (receiver.get() == it->receiver.get()) { |
| 130 | receivers.erase(it); |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 131 | VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size()); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 132 | return; |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void StatsPullerManager::OnAlarmFired() { |
| 138 | AutoMutex _l(mReceiversLock); |
| 139 | |
| 140 | uint64_t currentTimeMs = time(nullptr) * 1000; |
| 141 | |
| 142 | vector<pair<int, vector<ReceiverInfo*>>> needToPull = |
| 143 | vector<pair<int, vector<ReceiverInfo*>>>(); |
| 144 | for (auto& pair : mReceivers) { |
| 145 | vector<ReceiverInfo*> receivers = vector<ReceiverInfo*>(); |
| 146 | if (pair.second.size() != 0){ |
| 147 | for(auto& receiverInfo : pair.second) { |
| 148 | if (receiverInfo.timeInfo.first + receiverInfo.timeInfo.second > currentTimeMs) { |
| 149 | receivers.push_back(&receiverInfo); |
| 150 | } |
| 151 | } |
| 152 | if (receivers.size() > 0) { |
| 153 | needToPull.push_back(make_pair(pair.first, receivers)); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | for (const auto& pullInfo : needToPull) { |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 159 | vector<shared_ptr<LogEvent>> data; |
| 160 | if (Pull(pullInfo.first, &data)) { |
| 161 | for (const auto& receiverInfo : pullInfo.second) { |
| 162 | receiverInfo->receiver->onDataPulled(data); |
| 163 | receiverInfo->timeInfo.second = currentTimeMs; |
| 164 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 169 | } // namespace statsd |
| 170 | } // namespace os |
| 171 | } // namespace android |