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