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 | e33bc3b | 2017-11-06 17:56:44 -0800 | [diff] [blame] | 24 | #include "CpuTimePerUidFreqPuller.h" |
| 25 | #include "CpuTimePerUidPuller.h" |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 26 | #include "ResourcePowerManagerPuller.h" |
| 27 | #include "StatsCompanionServicePuller.h" |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 28 | #include "StatsPullerManagerImpl.h" |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 29 | #include "StatsService.h" |
| 30 | #include "logd/LogEvent.h" |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 31 | #include "statslog.h" |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 32 | |
| 33 | #include <iostream> |
Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 34 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 35 | using std::make_shared; |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 36 | using std::map; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 37 | using std::shared_ptr; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 38 | using std::string; |
| 39 | using std::vector; |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 40 | using std::list; |
Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 41 | |
| 42 | namespace android { |
| 43 | namespace os { |
| 44 | namespace statsd { |
| 45 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 46 | StatsPullerManagerImpl::StatsPullerManagerImpl() |
Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 47 | : mCurrentPullingInterval(LONG_MAX) { |
Chenjie Yu | 31d14d7 | 2017-12-12 17:54:33 -0800 | [diff] [blame] | 48 | mPullers.insert({android::util::KERNEL_WAKELOCK, |
Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame] | 49 | make_shared<StatsCompanionServicePuller>(android::util::KERNEL_WAKELOCK)}); |
Chenjie Yu | 31d14d7 | 2017-12-12 17:54:33 -0800 | [diff] [blame] | 50 | mPullers.insert({android::util::WIFI_BYTES_TRANSFER, |
Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame] | 51 | make_shared<StatsCompanionServicePuller>(android::util::WIFI_BYTES_TRANSFER)}); |
| 52 | mPullers.insert( |
| 53 | {android::util::MOBILE_BYTES_TRANSFER, |
| 54 | make_shared<StatsCompanionServicePuller>(android::util::MOBILE_BYTES_TRANSFER)}); |
Chenjie Yu | 31d14d7 | 2017-12-12 17:54:33 -0800 | [diff] [blame] | 55 | mPullers.insert({android::util::WIFI_BYTES_TRANSFER_BY_FG_BG, |
Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame] | 56 | make_shared<StatsCompanionServicePuller>( |
| 57 | android::util::WIFI_BYTES_TRANSFER_BY_FG_BG)}); |
Chenjie Yu | 31d14d7 | 2017-12-12 17:54:33 -0800 | [diff] [blame] | 58 | mPullers.insert({android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG, |
Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame] | 59 | make_shared<StatsCompanionServicePuller>( |
| 60 | android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG)}); |
Chenjie Yu | 31d14d7 | 2017-12-12 17:54:33 -0800 | [diff] [blame] | 61 | mPullers.insert({android::util::PLATFORM_SLEEP_STATE, |
Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame] | 62 | make_shared<ResourcePowerManagerPuller>(android::util::PLATFORM_SLEEP_STATE)}); |
Chenjie Yu | 31d14d7 | 2017-12-12 17:54:33 -0800 | [diff] [blame] | 63 | mPullers.insert({android::util::SLEEP_STATE_VOTER, |
Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame] | 64 | make_shared<ResourcePowerManagerPuller>(android::util::SLEEP_STATE_VOTER)}); |
| 65 | mPullers.insert( |
| 66 | {android::util::SUBSYSTEM_SLEEP_STATE, |
| 67 | make_shared<ResourcePowerManagerPuller>(android::util::SUBSYSTEM_SLEEP_STATE)}); |
| 68 | mPullers.insert({android::util::CPU_TIME_PER_FREQ, make_shared<StatsCompanionServicePuller>(android::util::CPU_TIME_PER_FREQ)}); |
| 69 | mPullers.insert({android::util::CPU_TIME_PER_UID, make_shared<CpuTimePerUidPuller>()}); |
| 70 | mPullers.insert({android::util::CPU_TIME_PER_UID_FREQ, make_shared<CpuTimePerUidFreqPuller>()}); |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 71 | |
| 72 | mStatsCompanionService = StatsService::getStatsCompanionService(); |
Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 75 | bool StatsPullerManagerImpl::Pull(int tagId, vector<shared_ptr<LogEvent>>* data) { |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 76 | if (DEBUG) ALOGD("Initiating pulling %d", tagId); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 77 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 78 | if (mPullers.find(tagId) != mPullers.end()) { |
Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame] | 79 | bool ret = mPullers.find(tagId)->second->Pull(data); |
Chenjie Yu | 032fefc | 2017-12-01 23:30:59 -0800 | [diff] [blame] | 80 | ALOGD("pulled %d items", (int)data->size()); |
| 81 | return ret; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 82 | } else { |
| 83 | ALOGD("Unknown tagId %d", tagId); |
| 84 | return false; // Return early since we don't know what to pull. |
| 85 | } |
| 86 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 87 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 88 | StatsPullerManagerImpl& StatsPullerManagerImpl::GetInstance() { |
| 89 | static StatsPullerManagerImpl instance; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 90 | return instance; |
| 91 | } |
| 92 | |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 93 | bool StatsPullerManagerImpl::PullerForMatcherExists(int tagId) const { |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 94 | return mPullers.find(tagId) != mPullers.end(); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 97 | void StatsPullerManagerImpl::RegisterReceiver(int tagId, wp<PullDataReceiver> receiver, |
| 98 | long intervalMs) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 99 | AutoMutex _l(mReceiversLock); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 100 | auto& receivers = mReceivers[tagId]; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 101 | for (auto it = receivers.begin(); it != receivers.end(); it++) { |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 102 | if (it->receiver == receiver) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 103 | VLOG("Receiver already registered of %d", (int)receivers.size()); |
| 104 | return; |
| 105 | } |
| 106 | } |
| 107 | ReceiverInfo receiverInfo; |
| 108 | receiverInfo.receiver = receiver; |
| 109 | receiverInfo.timeInfo.first = intervalMs; |
| 110 | receivers.push_back(receiverInfo); |
| 111 | |
Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 112 | // Round it to the nearest minutes. This is the limit of alarm manager. |
| 113 | // In practice, we should limit it higher. |
| 114 | long roundedIntervalMs = intervalMs/1000/60 * 1000 * 60; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 115 | // 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] | 116 | if (roundedIntervalMs < mCurrentPullingInterval) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 117 | VLOG("Updating pulling interval %ld", intervalMs); |
Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 118 | mCurrentPullingInterval = roundedIntervalMs; |
| 119 | long currentTimeMs = time(nullptr) * 1000; |
| 120 | long nextAlarmTimeMs = currentTimeMs + mCurrentPullingInterval - (currentTimeMs - mTimeBaseSec * 1000) % mCurrentPullingInterval; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 121 | if (mStatsCompanionService != nullptr) { |
Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 122 | mStatsCompanionService->setPullingAlarms(nextAlarmTimeMs, mCurrentPullingInterval); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 123 | } else { |
| 124 | VLOG("Failed to update pulling interval"); |
| 125 | } |
| 126 | } |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 127 | VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size()); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 130 | void StatsPullerManagerImpl::UnRegisterReceiver(int tagId, wp<PullDataReceiver> receiver) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 131 | AutoMutex _l(mReceiversLock); |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 132 | if (mReceivers.find(tagId) == mReceivers.end()) { |
| 133 | VLOG("Unknown pull code or no receivers: %d", tagId); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 134 | return; |
| 135 | } |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 136 | auto& receivers = mReceivers.find(tagId)->second; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 137 | for (auto it = receivers.begin(); it != receivers.end(); it++) { |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 138 | if (receiver == it->receiver) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 139 | receivers.erase(it); |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 140 | VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size()); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 141 | return; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 146 | void StatsPullerManagerImpl::OnAlarmFired() { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 147 | AutoMutex _l(mReceiversLock); |
| 148 | |
Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 149 | uint64_t currentTimeMs = time(nullptr) /60 * 60 * 1000; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 150 | |
| 151 | vector<pair<int, vector<ReceiverInfo*>>> needToPull = |
| 152 | vector<pair<int, vector<ReceiverInfo*>>>(); |
| 153 | for (auto& pair : mReceivers) { |
| 154 | vector<ReceiverInfo*> receivers = vector<ReceiverInfo*>(); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 155 | if (pair.second.size() != 0) { |
| 156 | for (auto& receiverInfo : pair.second) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 157 | if (receiverInfo.timeInfo.first + receiverInfo.timeInfo.second > currentTimeMs) { |
| 158 | receivers.push_back(&receiverInfo); |
| 159 | } |
| 160 | } |
| 161 | if (receivers.size() > 0) { |
| 162 | needToPull.push_back(make_pair(pair.first, receivers)); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | for (const auto& pullInfo : needToPull) { |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 168 | vector<shared_ptr<LogEvent>> data; |
| 169 | if (Pull(pullInfo.first, &data)) { |
| 170 | for (const auto& receiverInfo : pullInfo.second) { |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 171 | sp<PullDataReceiver> receiverPtr = receiverInfo->receiver.promote(); |
| 172 | if (receiverPtr != nullptr) { |
| 173 | receiverPtr->onDataPulled(data); |
| 174 | receiverInfo->timeInfo.second = currentTimeMs; |
| 175 | } else { |
| 176 | VLOG("receiver already gone."); |
| 177 | } |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 178 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 183 | } // namespace statsd |
| 184 | } // namespace os |
| 185 | } // namespace android |