| /* |
| * Copyright (C) 2017 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| #define DEBUG false |
| #include "Log.h" |
| |
| #include "StatsPullerManager.h" |
| |
| #include <cutils/log.h> |
| #include <math.h> |
| #include <stdint.h> |
| |
| #include <algorithm> |
| #include <iostream> |
| |
| #include "../StatsService.h" |
| #include "../logd/LogEvent.h" |
| #include "../stats_log_util.h" |
| #include "../statscompanion_util.h" |
| #include "StatsCallbackPuller.h" |
| #include "TrainInfoPuller.h" |
| #include "statslog_statsd.h" |
| |
| using std::shared_ptr; |
| using std::vector; |
| |
| namespace android { |
| namespace os { |
| namespace statsd { |
| |
| // Values smaller than this may require to update the alarm. |
| const int64_t NO_ALARM_UPDATE = INT64_MAX; |
| |
| StatsPullerManager::StatsPullerManager() |
| : kAllPullAtomInfo({ |
| // TrainInfo. |
| {{.atomTag = util::TRAIN_INFO}, new TrainInfoPuller()}, |
| }), |
| mNextPullTimeNs(NO_ALARM_UPDATE) { |
| } |
| |
| bool StatsPullerManager::Pull(int tagId, vector<shared_ptr<LogEvent>>* data) { |
| AutoMutex _l(mLock); |
| return PullLocked(tagId, data); |
| } |
| |
| bool StatsPullerManager::PullLocked(int tagId, vector<shared_ptr<LogEvent>>* data) { |
| VLOG("Initiating pulling %d", tagId); |
| |
| if (kAllPullAtomInfo.find({.atomTag = tagId}) != kAllPullAtomInfo.end()) { |
| bool ret = kAllPullAtomInfo.find({.atomTag = tagId})->second->Pull(data); |
| VLOG("pulled %d items", (int)data->size()); |
| if (!ret) { |
| StatsdStats::getInstance().notePullFailed(tagId); |
| } |
| return ret; |
| } else { |
| ALOGW("StatsPullerManager: Unknown tagId %d", tagId); |
| return false; // Return early since we don't know what to pull. |
| } |
| } |
| |
| bool StatsPullerManager::PullerForMatcherExists(int tagId) const { |
| // Pulled atoms might be registered after we parse the config, so just make sure the id is in |
| // an appropriate range. |
| return isVendorPulledAtom(tagId) || isPulledAtom(tagId); |
| } |
| |
| void StatsPullerManager::updateAlarmLocked() { |
| if (mNextPullTimeNs == NO_ALARM_UPDATE) { |
| VLOG("No need to set alarms. Skipping"); |
| return; |
| } |
| |
| // TODO(b/151045771): do not hold a lock while making a binder call |
| if (mStatsCompanionService != nullptr) { |
| mStatsCompanionService->setPullingAlarm(mNextPullTimeNs / 1000000); |
| } else { |
| VLOG("StatsCompanionService not available. Alarm not set."); |
| } |
| return; |
| } |
| |
| void StatsPullerManager::SetStatsCompanionService( |
| shared_ptr<IStatsCompanionService> statsCompanionService) { |
| AutoMutex _l(mLock); |
| shared_ptr<IStatsCompanionService> tmpForLock = mStatsCompanionService; |
| mStatsCompanionService = statsCompanionService; |
| for (const auto& pulledAtom : kAllPullAtomInfo) { |
| pulledAtom.second->SetStatsCompanionService(statsCompanionService); |
| } |
| if (mStatsCompanionService != nullptr) { |
| updateAlarmLocked(); |
| } |
| } |
| |
| void StatsPullerManager::RegisterReceiver(int tagId, wp<PullDataReceiver> receiver, |
| int64_t nextPullTimeNs, int64_t intervalNs) { |
| AutoMutex _l(mLock); |
| auto& receivers = mReceivers[tagId]; |
| for (auto it = receivers.begin(); it != receivers.end(); it++) { |
| if (it->receiver == receiver) { |
| VLOG("Receiver already registered of %d", (int)receivers.size()); |
| return; |
| } |
| } |
| ReceiverInfo receiverInfo; |
| receiverInfo.receiver = receiver; |
| |
| // Round it to the nearest minutes. This is the limit of alarm manager. |
| // In practice, we should always have larger buckets. |
| int64_t roundedIntervalNs = intervalNs / NS_PER_SEC / 60 * NS_PER_SEC * 60; |
| // Scheduled pulling should be at least 1 min apart. |
| // This can be lower in cts tests, in which case we round it to 1 min. |
| if (roundedIntervalNs < 60 * (int64_t)NS_PER_SEC) { |
| roundedIntervalNs = 60 * (int64_t)NS_PER_SEC; |
| } |
| |
| receiverInfo.intervalNs = roundedIntervalNs; |
| receiverInfo.nextPullTimeNs = nextPullTimeNs; |
| receivers.push_back(receiverInfo); |
| |
| // There is only one alarm for all pulled events. So only set it to the smallest denom. |
| if (nextPullTimeNs < mNextPullTimeNs) { |
| VLOG("Updating next pull time %lld", (long long)mNextPullTimeNs); |
| mNextPullTimeNs = nextPullTimeNs; |
| updateAlarmLocked(); |
| } |
| VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size()); |
| } |
| |
| void StatsPullerManager::UnRegisterReceiver(int tagId, wp<PullDataReceiver> receiver) { |
| AutoMutex _l(mLock); |
| if (mReceivers.find(tagId) == mReceivers.end()) { |
| VLOG("Unknown pull code or no receivers: %d", tagId); |
| return; |
| } |
| auto& receivers = mReceivers.find(tagId)->second; |
| for (auto it = receivers.begin(); it != receivers.end(); it++) { |
| if (receiver == it->receiver) { |
| receivers.erase(it); |
| VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size()); |
| return; |
| } |
| } |
| } |
| |
| void StatsPullerManager::OnAlarmFired(int64_t elapsedTimeNs) { |
| AutoMutex _l(mLock); |
| int64_t wallClockNs = getWallClockNs(); |
| |
| int64_t minNextPullTimeNs = NO_ALARM_UPDATE; |
| |
| vector<pair<int, vector<ReceiverInfo*>>> needToPull = |
| vector<pair<int, vector<ReceiverInfo*>>>(); |
| for (auto& pair : mReceivers) { |
| vector<ReceiverInfo*> receivers = vector<ReceiverInfo*>(); |
| if (pair.second.size() != 0) { |
| for (ReceiverInfo& receiverInfo : pair.second) { |
| if (receiverInfo.nextPullTimeNs <= elapsedTimeNs) { |
| receivers.push_back(&receiverInfo); |
| } else { |
| if (receiverInfo.nextPullTimeNs < minNextPullTimeNs) { |
| minNextPullTimeNs = receiverInfo.nextPullTimeNs; |
| } |
| } |
| } |
| if (receivers.size() > 0) { |
| needToPull.push_back(make_pair(pair.first, receivers)); |
| } |
| } |
| } |
| |
| for (const auto& pullInfo : needToPull) { |
| vector<shared_ptr<LogEvent>> data; |
| bool pullSuccess = PullLocked(pullInfo.first, &data); |
| if (pullSuccess) { |
| StatsdStats::getInstance().notePullDelay( |
| pullInfo.first, getElapsedRealtimeNs() - elapsedTimeNs); |
| } else { |
| VLOG("pull failed at %lld, will try again later", (long long)elapsedTimeNs); |
| } |
| |
| // Convention is to mark pull atom timestamp at request time. |
| // If we pull at t0, puller starts at t1, finishes at t2, and send back |
| // at t3, we mark t0 as its timestamp, which should correspond to its |
| // triggering event, such as condition change at t0. |
| // Here the triggering event is alarm fired from AlarmManager. |
| // In ValueMetricProducer and GaugeMetricProducer we do same thing |
| // when pull on condition change, etc. |
| for (auto& event : data) { |
| event->setElapsedTimestampNs(elapsedTimeNs); |
| event->setLogdWallClockTimestampNs(wallClockNs); |
| } |
| |
| for (const auto& receiverInfo : pullInfo.second) { |
| sp<PullDataReceiver> receiverPtr = receiverInfo->receiver.promote(); |
| if (receiverPtr != nullptr) { |
| receiverPtr->onDataPulled(data, pullSuccess, elapsedTimeNs); |
| // We may have just come out of a coma, compute next pull time. |
| int numBucketsAhead = |
| (elapsedTimeNs - receiverInfo->nextPullTimeNs) / receiverInfo->intervalNs; |
| receiverInfo->nextPullTimeNs += (numBucketsAhead + 1) * receiverInfo->intervalNs; |
| if (receiverInfo->nextPullTimeNs < minNextPullTimeNs) { |
| minNextPullTimeNs = receiverInfo->nextPullTimeNs; |
| } |
| } else { |
| VLOG("receiver already gone."); |
| } |
| } |
| } |
| |
| VLOG("mNextPullTimeNs: %lld updated to %lld", (long long)mNextPullTimeNs, |
| (long long)minNextPullTimeNs); |
| mNextPullTimeNs = minNextPullTimeNs; |
| updateAlarmLocked(); |
| } |
| |
| int StatsPullerManager::ForceClearPullerCache() { |
| int totalCleared = 0; |
| for (const auto& pulledAtom : kAllPullAtomInfo) { |
| totalCleared += pulledAtom.second->ForceClearCache(); |
| } |
| return totalCleared; |
| } |
| |
| int StatsPullerManager::ClearPullerCacheIfNecessary(int64_t timestampNs) { |
| int totalCleared = 0; |
| for (const auto& pulledAtom : kAllPullAtomInfo) { |
| totalCleared += pulledAtom.second->ClearCacheIfNecessary(timestampNs); |
| } |
| return totalCleared; |
| } |
| |
| void StatsPullerManager::RegisterPullAtomCallback(const int uid, const int32_t atomTag, |
| const int64_t coolDownNs, const int64_t timeoutNs, |
| const vector<int32_t>& additiveFields, |
| const shared_ptr<IPullAtomCallback>& callback) { |
| AutoMutex _l(mLock); |
| VLOG("RegisterPullerCallback: adding puller for tag %d", atomTag); |
| // TODO(b/146439412): linkToDeath with the callback so that we can remove it |
| // and delete the puller. |
| StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/true); |
| kAllPullAtomInfo[{.atomTag = atomTag}] = |
| new StatsCallbackPuller(atomTag, callback, coolDownNs, timeoutNs, additiveFields); |
| } |
| |
| void StatsPullerManager::UnregisterPullAtomCallback(const int uid, const int32_t atomTag) { |
| AutoMutex _l(mLock); |
| StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/false); |
| kAllPullAtomInfo.erase({.atomTag = atomTag}); |
| } |
| |
| } // namespace statsd |
| } // namespace os |
| } // namespace android |