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