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