From e22192071d0abccd52091eb3bff07176f4bfb84c Mon Sep 17 00:00:00 2001 From: Chenjie Yu Date: Fri, 8 Jun 2018 10:07:51 -0700 Subject: [PATCH] StatsPullerManager not use singleton This is to be consistent with other patterns such as UidMap. This also makes unit test simpler. Change-Id: I1558cd609e470481f269ecf2ae616277a95cfbf0 Bug: 72722120 Test: unit test --- cmds/statsd/Android.mk | 2 +- cmds/statsd/benchmark/metric_util.cpp | 7 +- cmds/statsd/src/StatsLogProcessor.cpp | 14 +-- cmds/statsd/src/StatsLogProcessor.h | 5 +- cmds/statsd/src/StatsService.cpp | 43 ++++---- cmds/statsd/src/StatsService.h | 4 +- cmds/statsd/src/external/StatsPuller.cpp | 4 +- ...ManagerImpl.cpp => StatsPullerManager.cpp} | 29 +++-- cmds/statsd/src/external/StatsPullerManager.h | 97 +++++++++++------ .../src/external/StatsPullerManagerImpl.h | 102 ------------------ cmds/statsd/src/external/puller_util.cpp | 10 +- .../src/metrics/GaugeMetricProducer.cpp | 21 ++-- cmds/statsd/src/metrics/GaugeMetricProducer.h | 12 +-- cmds/statsd/src/metrics/MetricsManager.cpp | 17 +-- cmds/statsd/src/metrics/MetricsManager.h | 8 +- .../src/metrics/ValueMetricProducer.cpp | 21 ++-- cmds/statsd/src/metrics/ValueMetricProducer.h | 15 +-- .../src/metrics/metrics_manager_util.cpp | 29 ++--- .../statsd/src/metrics/metrics_manager_util.h | 13 ++- cmds/statsd/tests/MetricsManager_test.cpp | 91 ++++++++-------- cmds/statsd/tests/StatsLogProcessor_test.cpp | 67 ++++++++---- cmds/statsd/tests/UidMap_test.cpp | 5 +- .../tests/e2e/GaugeMetric_e2e_pull_test.cpp | 24 ++--- .../tests/e2e/ValueMetric_pull_e2e_test.cpp | 22 ++-- .../metrics/GaugeMetricProducer_test.cpp | 29 ++--- .../metrics/ValueMetricProducer_test.cpp | 43 +++----- cmds/statsd/tests/statsd_test_util.cpp | 6 +- 27 files changed, 322 insertions(+), 418 deletions(-) rename cmds/statsd/src/external/{StatsPullerManagerImpl.cpp => StatsPullerManager.cpp} (92%) delete mode 100644 cmds/statsd/src/external/StatsPullerManagerImpl.h diff --git a/cmds/statsd/Android.mk b/cmds/statsd/Android.mk index 5e3b85bac74b3..0b5e358fde1f4 100644 --- a/cmds/statsd/Android.mk +++ b/cmds/statsd/Android.mk @@ -41,7 +41,7 @@ statsd_common_src := \ src/external/SubsystemSleepStatePuller.cpp \ src/external/ResourceHealthManagerPuller.cpp \ src/external/ResourceThermalManagerPuller.cpp \ - src/external/StatsPullerManagerImpl.cpp \ + src/external/StatsPullerManager.cpp \ src/external/puller_util.cpp \ src/logd/LogEvent.cpp \ src/logd/LogListener.cpp \ diff --git a/cmds/statsd/benchmark/metric_util.cpp b/cmds/statsd/benchmark/metric_util.cpp index 50ed18d3e2b0d..067b6eddf2541 100644 --- a/cmds/statsd/benchmark/metric_util.cpp +++ b/cmds/statsd/benchmark/metric_util.cpp @@ -362,11 +362,12 @@ std::unique_ptr CreateSyncEndEvent( sp CreateStatsLogProcessor(const long timeBaseSec, const StatsdConfig& config, const ConfigKey& key) { sp uidMap = new UidMap(); + sp pullerManager = new StatsPullerManager(); sp anomalyAlarmMonitor; sp periodicAlarmMonitor; - sp processor = new StatsLogProcessor( - uidMap, anomalyAlarmMonitor, periodicAlarmMonitor, timeBaseSec * NS_PER_SEC, - [](const ConfigKey&){return true;}); + sp processor = + new StatsLogProcessor(uidMap, pullerManager, anomalyAlarmMonitor, periodicAlarmMonitor, + timeBaseSec * NS_PER_SEC, [](const ConfigKey&) { return true; }); processor->OnConfigUpdated(timeBaseSec * NS_PER_SEC, key, config); return processor; } diff --git a/cmds/statsd/src/StatsLogProcessor.cpp b/cmds/statsd/src/StatsLogProcessor.cpp index 1378ce50bf18b..ab0aa25103e92 100644 --- a/cmds/statsd/src/StatsLogProcessor.cpp +++ b/cmds/statsd/src/StatsLogProcessor.cpp @@ -72,18 +72,20 @@ const int FIELD_ID_STRINGS = 9; #define STATS_DATA_DIR "/data/misc/stats-data" StatsLogProcessor::StatsLogProcessor(const sp& uidMap, + const sp& pullerManager, const sp& anomalyAlarmMonitor, const sp& periodicAlarmMonitor, const int64_t timeBaseNs, const std::function& sendBroadcast) : mUidMap(uidMap), + mPullerManager(pullerManager), mAnomalyAlarmMonitor(anomalyAlarmMonitor), mPeriodicAlarmMonitor(periodicAlarmMonitor), mSendBroadcast(sendBroadcast), mTimeBaseNs(timeBaseNs), mLargestTimestampSeen(0), mLastTimestampSeen(0) { - mStatsPullerManager.ForceClearPullerCache(); + mPullerManager->ForceClearPullerCache(); } StatsLogProcessor::~StatsLogProcessor() { @@ -238,7 +240,7 @@ void StatsLogProcessor::OnLogEvent(LogEvent* event, bool reconnected) { int64_t curTimeSec = getElapsedRealtimeSec(); if (curTimeSec - mLastPullerCacheClearTimeSec > StatsdStats::kPullerCacheClearIntervalSec) { - mStatsPullerManager.ClearPullerCacheIfNecessary(curTimeSec * NS_PER_SEC); + mPullerManager->ClearPullerCacheIfNecessary(curTimeSec * NS_PER_SEC); mLastPullerCacheClearTimeSec = curTimeSec; } @@ -266,8 +268,8 @@ void StatsLogProcessor::OnConfigUpdatedLocked( const int64_t timestampNs, const ConfigKey& key, const StatsdConfig& config) { VLOG("Updated configuration for key %s", key.ToString().c_str()); sp newMetricsManager = - new MetricsManager(key, config, mTimeBaseNs, timestampNs, mUidMap, - mAnomalyAlarmMonitor, mPeriodicAlarmMonitor); + new MetricsManager(key, config, mTimeBaseNs, timestampNs, mUidMap, mPullerManager, + mAnomalyAlarmMonitor, mPeriodicAlarmMonitor); if (newMetricsManager->isConfigValid()) { mUidMap->OnConfigUpdated(key); if (newMetricsManager->shouldAddUidMapListener()) { @@ -453,7 +455,7 @@ void StatsLogProcessor::OnConfigRemoved(const ConfigKey& key) { mLastBroadcastTimes.erase(key); if (mMetricsManagers.empty()) { - mStatsPullerManager.ForceClearPullerCache(); + mPullerManager->ForceClearPullerCache(); } } @@ -538,7 +540,7 @@ void StatsLogProcessor::WriteDataToDisk(const DumpReportReason dumpReportReason) void StatsLogProcessor::informPullAlarmFired(const int64_t timestampNs) { std::lock_guard lock(mMetricsMutex); - mStatsPullerManager.OnAlarmFired(timestampNs); + mPullerManager->OnAlarmFired(timestampNs); } int64_t StatsLogProcessor::getLastReportTimeNs(const ConfigKey& key) { diff --git a/cmds/statsd/src/StatsLogProcessor.h b/cmds/statsd/src/StatsLogProcessor.h index b175b3c544b5d..05cf0c1fc764c 100644 --- a/cmds/statsd/src/StatsLogProcessor.h +++ b/cmds/statsd/src/StatsLogProcessor.h @@ -45,7 +45,8 @@ enum DumpReportReason { class StatsLogProcessor : public ConfigListener { public: - StatsLogProcessor(const sp& uidMap, const sp& anomalyAlarmMonitor, + StatsLogProcessor(const sp& uidMap, const sp& pullerManager, + const sp& anomalyAlarmMonitor, const sp& subscriberTriggerAlarmMonitor, const int64_t timeBaseNs, const std::function& sendBroadcast); @@ -126,7 +127,7 @@ private: sp mUidMap; // Reference to the UidMap to lookup app name and version for each uid. - StatsPullerManager mStatsPullerManager; + sp mPullerManager; // Reference to StatsPullerManager sp mAnomalyAlarmMonitor; diff --git a/cmds/statsd/src/StatsService.cpp b/cmds/statsd/src/StatsService.cpp index 10c04f67ca051..ae44ee917d690 100644 --- a/cmds/statsd/src/StatsService.cpp +++ b/cmds/statsd/src/StatsService.cpp @@ -150,25 +150,26 @@ StatsService::StatsService(const sp& handlerLooper) })) { mUidMap = new UidMap(); + mPullerManager = new StatsPullerManager(); StatsPuller::SetUidMap(mUidMap); mConfigManager = new ConfigManager(); - mProcessor = new StatsLogProcessor(mUidMap, mAnomalyAlarmMonitor, mPeriodicAlarmMonitor, - getElapsedRealtimeNs(), [this](const ConfigKey& key) { - sp sc = getStatsCompanionService(); - auto receiver = mConfigManager->GetConfigReceiver(key); - if (sc == nullptr) { - VLOG("Could not find StatsCompanionService"); - return false; - } else if (receiver == nullptr) { - VLOG("Statscompanion could not find a broadcast receiver for %s", - key.ToString().c_str()); - return false; - } else { - sc->sendDataBroadcast(receiver, mProcessor->getLastReportTimeNs(key)); - return true; - } - } - ); + mProcessor = new StatsLogProcessor( + mUidMap, mPullerManager, mAnomalyAlarmMonitor, mPeriodicAlarmMonitor, + getElapsedRealtimeNs(), [this](const ConfigKey& key) { + sp sc = getStatsCompanionService(); + auto receiver = mConfigManager->GetConfigReceiver(key); + if (sc == nullptr) { + VLOG("Could not find StatsCompanionService"); + return false; + } else if (receiver == nullptr) { + VLOG("Statscompanion could not find a broadcast receiver for %s", + key.ToString().c_str()); + return false; + } else { + sc->sendDataBroadcast(receiver, mProcessor->getLastReportTimeNs(key)); + return true; + } + }); mConfigManager->AddListener(mProcessor); @@ -711,7 +712,7 @@ status_t StatsService::cmd_log_app_breadcrumb(FILE* out, const Vector& status_t StatsService::cmd_print_pulled_metrics(FILE* out, const Vector& args) { int s = atoi(args[1].c_str()); vector > stats; - if (mStatsPullerManager.Pull(s, getElapsedRealtimeNs(), &stats)) { + if (mPullerManager->Pull(s, getElapsedRealtimeNs(), &stats)) { for (const auto& it : stats) { fprintf(out, "Pull from %d: %s\n", s, it->ToString().c_str()); } @@ -739,7 +740,7 @@ status_t StatsService::cmd_clear_puller_cache(FILE* out) { VLOG("StatsService::cmd_clear_puller_cache with Pid %i, Uid %i", ipc->getCallingPid(), ipc->getCallingUid()); if (checkCallingPermission(String16(kPermissionDump))) { - int cleared = mStatsPullerManager.ForceClearPullerCache(); + int cleared = mPullerManager->ForceClearPullerCache(); fprintf(out, "Puller removed %d cached data!\n", cleared); return NO_ERROR; } else { @@ -870,7 +871,7 @@ Status StatsService::statsCompanionReady() { } VLOG("StatsService::statsCompanionReady linking to statsCompanion."); IInterface::asBinder(statsCompanion)->linkToDeath(this); - mStatsPullerManager.SetStatsCompanionService(statsCompanion); + mPullerManager->SetStatsCompanionService(statsCompanion); mAnomalyAlarmMonitor->setStatsCompanionService(statsCompanion); mPeriodicAlarmMonitor->setStatsCompanionService(statsCompanion); SubscriberReporter::getInstance().setStatsCompanionService(statsCompanion); @@ -1014,7 +1015,7 @@ void StatsService::binderDied(const wp & who) { mAnomalyAlarmMonitor->setStatsCompanionService(nullptr); mPeriodicAlarmMonitor->setStatsCompanionService(nullptr); SubscriberReporter::getInstance().setStatsCompanionService(nullptr); - mStatsPullerManager.SetStatsCompanionService(nullptr); + mPullerManager->SetStatsCompanionService(nullptr); } } // namespace statsd diff --git a/cmds/statsd/src/StatsService.h b/cmds/statsd/src/StatsService.h index b3a477645b73c..0a11f7cbad8fa 100644 --- a/cmds/statsd/src/StatsService.h +++ b/cmds/statsd/src/StatsService.h @@ -246,9 +246,9 @@ private: sp mUidMap; /** - * Fetches external metrics. + * Fetches external metrics */ - StatsPullerManager mStatsPullerManager; + sp mPullerManager; /** * Tracks the configurations that have been passed to statsd. diff --git a/cmds/statsd/src/external/StatsPuller.cpp b/cmds/statsd/src/external/StatsPuller.cpp index b29e979b52366..436a8801896ff 100644 --- a/cmds/statsd/src/external/StatsPuller.cpp +++ b/cmds/statsd/src/external/StatsPuller.cpp @@ -18,10 +18,10 @@ #include "Log.h" #include "StatsPuller.h" +#include "StatsPullerManager.h" #include "guardrail/StatsdStats.h" #include "puller_util.h" #include "stats_log_util.h" -#include "StatsPullerManagerImpl.h" namespace android { namespace os { @@ -35,7 +35,7 @@ void StatsPuller::SetUidMap(const sp& uidMap) { mUidMap = uidMap; } // ValueMetric has a minimum bucket size of 10min so that we don't pull too frequently StatsPuller::StatsPuller(const int tagId) : mTagId(tagId) { - mCoolDownNs = StatsPullerManagerImpl::kAllPullAtomInfo.find(tagId)->second.coolDownNs; + mCoolDownNs = StatsPullerManager::kAllPullAtomInfo.find(tagId)->second.coolDownNs; VLOG("Puller for tag %d created. Cooldown set to %lld", mTagId, (long long)mCoolDownNs); } diff --git a/cmds/statsd/src/external/StatsPullerManagerImpl.cpp b/cmds/statsd/src/external/StatsPullerManager.cpp similarity index 92% rename from cmds/statsd/src/external/StatsPullerManagerImpl.cpp rename to cmds/statsd/src/external/StatsPullerManager.cpp index c020f9c12b879..06edff9e498c7 100644 --- a/cmds/statsd/src/external/StatsPullerManagerImpl.cpp +++ b/cmds/statsd/src/external/StatsPullerManager.cpp @@ -29,7 +29,7 @@ #include "ResourceHealthManagerPuller.h" #include "ResourceThermalManagerPuller.h" #include "StatsCompanionServicePuller.h" -#include "StatsPullerManagerImpl.h" +#include "StatsPullerManager.h" #include "SubsystemSleepStatePuller.h" #include "statslog.h" @@ -49,7 +49,7 @@ namespace statsd { // Values smaller than this may require to update the alarm. const int64_t NO_ALARM_UPDATE = INT64_MAX; -const std::map StatsPullerManagerImpl::kAllPullAtomInfo = { +const std::map StatsPullerManager::kAllPullAtomInfo = { // wifi_bytes_transfer {android::util::WIFI_BYTES_TRANSFER, {{2, 3, 4, 5}, @@ -173,10 +173,10 @@ const std::map StatsPullerManagerImpl::kAllPullAtomInfo = { // temperature {android::util::TEMPERATURE, {{}, {}, 1, new ResourceThermalManagerPuller()}}}; -StatsPullerManagerImpl::StatsPullerManagerImpl() : mNextPullTimeNs(NO_ALARM_UPDATE) { +StatsPullerManager::StatsPullerManager() : mNextPullTimeNs(NO_ALARM_UPDATE) { } -bool StatsPullerManagerImpl::Pull(const int tagId, const int64_t timeNs, +bool StatsPullerManager::Pull(const int tagId, const int64_t timeNs, vector>* data) { VLOG("Initiating pulling %d", tagId); @@ -190,16 +190,11 @@ bool StatsPullerManagerImpl::Pull(const int tagId, const int64_t timeNs, } } -StatsPullerManagerImpl& StatsPullerManagerImpl::GetInstance() { - static StatsPullerManagerImpl instance; - return instance; -} - -bool StatsPullerManagerImpl::PullerForMatcherExists(int tagId) const { +bool StatsPullerManager::PullerForMatcherExists(int tagId) const { return kAllPullAtomInfo.find(tagId) != kAllPullAtomInfo.end(); } -void StatsPullerManagerImpl::updateAlarmLocked() { +void StatsPullerManager::updateAlarmLocked() { if (mNextPullTimeNs == NO_ALARM_UPDATE) { VLOG("No need to set alarms. Skipping"); return; @@ -214,7 +209,7 @@ void StatsPullerManagerImpl::updateAlarmLocked() { return; } -void StatsPullerManagerImpl::SetStatsCompanionService( +void StatsPullerManager::SetStatsCompanionService( sp statsCompanionService) { AutoMutex _l(mLock); sp tmpForLock = mStatsCompanionService; @@ -227,7 +222,7 @@ void StatsPullerManagerImpl::SetStatsCompanionService( } } -void StatsPullerManagerImpl::RegisterReceiver(int tagId, wp receiver, +void StatsPullerManager::RegisterReceiver(int tagId, wp receiver, int64_t nextPullTimeNs, int64_t intervalNs) { AutoMutex _l(mLock); auto& receivers = mReceivers[tagId]; @@ -262,7 +257,7 @@ void StatsPullerManagerImpl::RegisterReceiver(int tagId, wp re VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size()); } -void StatsPullerManagerImpl::UnRegisterReceiver(int tagId, wp receiver) { +void StatsPullerManager::UnRegisterReceiver(int tagId, wp receiver) { AutoMutex _l(mLock); if (mReceivers.find(tagId) == mReceivers.end()) { VLOG("Unknown pull code or no receivers: %d", tagId); @@ -278,7 +273,7 @@ void StatsPullerManagerImpl::UnRegisterReceiver(int tagId, wp } } -void StatsPullerManagerImpl::OnAlarmFired(const int64_t currentTimeNs) { +void StatsPullerManager::OnAlarmFired(const int64_t currentTimeNs) { AutoMutex _l(mLock); int64_t minNextPullTimeNs = NO_ALARM_UPDATE; @@ -331,7 +326,7 @@ void StatsPullerManagerImpl::OnAlarmFired(const int64_t currentTimeNs) { updateAlarmLocked(); } -int StatsPullerManagerImpl::ForceClearPullerCache() { +int StatsPullerManager::ForceClearPullerCache() { int totalCleared = 0; for (const auto& pulledAtom : kAllPullAtomInfo) { totalCleared += pulledAtom.second.puller->ForceClearCache(); @@ -339,7 +334,7 @@ int StatsPullerManagerImpl::ForceClearPullerCache() { return totalCleared; } -int StatsPullerManagerImpl::ClearPullerCacheIfNecessary(int64_t timestampNs) { +int StatsPullerManager::ClearPullerCacheIfNecessary(int64_t timestampNs) { int totalCleared = 0; for (const auto& pulledAtom : kAllPullAtomInfo) { totalCleared += pulledAtom.second.puller->ClearCacheIfNecessary(timestampNs); diff --git a/cmds/statsd/src/external/StatsPullerManager.h b/cmds/statsd/src/external/StatsPullerManager.h index 50ffe17549c61..45efc4a8bea03 100644 --- a/cmds/statsd/src/external/StatsPullerManager.h +++ b/cmds/statsd/src/external/StatsPullerManager.h @@ -16,54 +16,87 @@ #pragma once -#include "StatsPullerManagerImpl.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include "PullDataReceiver.h" +#include "StatsPuller.h" +#include "logd/LogEvent.h" namespace android { namespace os { namespace statsd { -class StatsPullerManager { - public: - virtual ~StatsPullerManager() {} +typedef struct { + // The field numbers of the fields that need to be summed when merging + // isolated uid with host uid. + std::vector additiveFields; + // The field numbers of the fields that can't be merged when merging + // data belong to isolated uid and host uid. + std::vector nonAdditiveFields; + // How long should the puller wait before doing an actual pull again. Default + // 1 sec. Set this to 0 if this is handled elsewhere. + int64_t coolDownNs = 1 * NS_PER_SEC; + // The actual puller + sp puller; +} PullAtomInfo; + +class StatsPullerManager : public virtual RefBase { +public: + StatsPullerManager(); + + virtual ~StatsPullerManager() { + } virtual void RegisterReceiver(int tagId, wp receiver, int64_t nextPullTimeNs, - int64_t intervalNs) { - mPullerManager.RegisterReceiver(tagId, receiver, nextPullTimeNs, intervalNs); - }; + int64_t intervalNs); - virtual void UnRegisterReceiver(int tagId, wp receiver) { - mPullerManager.UnRegisterReceiver(tagId, receiver); - }; + virtual void UnRegisterReceiver(int tagId, wp receiver); // Verify if we know how to pull for this matcher - bool PullerForMatcherExists(int tagId) { - return mPullerManager.PullerForMatcherExists(tagId); - } + bool PullerForMatcherExists(int tagId) const; - void OnAlarmFired(const int64_t currentTimeNs) { - mPullerManager.OnAlarmFired(currentTimeNs); - } + void OnAlarmFired(const int64_t timeNs); - virtual bool Pull(const int tagId, const int64_t timesNs, - vector>* data) { - return mPullerManager.Pull(tagId, timesNs, data); - } + virtual bool Pull(const int tagId, const int64_t timeNs, + vector>* data); - int ForceClearPullerCache() { - return mPullerManager.ForceClearPullerCache(); - } + int ForceClearPullerCache(); - void SetStatsCompanionService(sp statsCompanionService) { - mPullerManager.SetStatsCompanionService(statsCompanionService); - } + int ClearPullerCacheIfNecessary(int64_t timestampNs); - int ClearPullerCacheIfNecessary(int64_t timestampNs) { - return mPullerManager.ClearPullerCacheIfNecessary(timestampNs); - } + void SetStatsCompanionService(sp statsCompanionService); - private: - StatsPullerManagerImpl - & mPullerManager = StatsPullerManagerImpl::GetInstance(); + const static std::map kAllPullAtomInfo; + +private: + sp mStatsCompanionService = nullptr; + + typedef struct { + int64_t nextPullTimeNs; + int64_t intervalNs; + wp receiver; + } ReceiverInfo; + + // mapping from simple matcher tagId to receivers + std::map> mReceivers; + + // locks for data receiver and StatsCompanionService changes + Mutex mLock; + + void updateAlarmLocked(); + + int64_t mNextPullTimeNs; + + FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvents); + FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvent_LateAlarm); + FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents); + FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents_LateAlarm); }; } // namespace statsd diff --git a/cmds/statsd/src/external/StatsPullerManagerImpl.h b/cmds/statsd/src/external/StatsPullerManagerImpl.h deleted file mode 100644 index 56d04b41c5d55..0000000000000 --- a/cmds/statsd/src/external/StatsPullerManagerImpl.h +++ /dev/null @@ -1,102 +0,0 @@ -/* - * 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. - */ - -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include -#include "PullDataReceiver.h" -#include "StatsPuller.h" -#include "logd/LogEvent.h" - -namespace android { -namespace os { -namespace statsd { - -typedef struct { - // The field numbers of the fields that need to be summed when merging - // isolated uid with host uid. - std::vector additiveFields; - // The field numbers of the fields that can't be merged when merging - // data belong to isolated uid and host uid. - std::vector nonAdditiveFields; - // How long should the puller wait before doing an actual pull again. Default - // 1 sec. Set this to 0 if this is handled elsewhere. - int64_t coolDownNs = 1 * NS_PER_SEC; - // The actual puller - sp puller; -} PullAtomInfo; - -class StatsPullerManagerImpl : public virtual RefBase { -public: - static StatsPullerManagerImpl& GetInstance(); - - void RegisterReceiver(int tagId, wp receiver, int64_t nextPullTimeNs, - int64_t intervalNs); - - void UnRegisterReceiver(int tagId, wp receiver); - - // Verify if we know how to pull for this matcher - bool PullerForMatcherExists(int tagId) const; - - void OnAlarmFired(const int64_t timeNs); - - bool Pull(const int tagId, const int64_t timeNs, vector>* data); - - int ForceClearPullerCache(); - - int ClearPullerCacheIfNecessary(int64_t timestampNs); - - void SetStatsCompanionService(sp statsCompanionService); - - const static std::map kAllPullAtomInfo; - - private: - StatsPullerManagerImpl(); - - sp mStatsCompanionService = nullptr; - - typedef struct { - int64_t nextPullTimeNs; - int64_t intervalNs; - wp receiver; - } ReceiverInfo; - - // mapping from simple matcher tagId to receivers - std::map> mReceivers; - - // locks for data receiver and StatsCompanionService changes - Mutex mLock; - - void updateAlarmLocked(); - - int64_t mNextPullTimeNs; - - FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvents); - FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvent_LateAlarm); - FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents); - FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents_LateAlarm); -}; - -} // namespace statsd -} // namespace os -} // namespace android diff --git a/cmds/statsd/src/external/puller_util.cpp b/cmds/statsd/src/external/puller_util.cpp index 57fe10e51bfcc..ea7fa972cb9cd 100644 --- a/cmds/statsd/src/external/puller_util.cpp +++ b/cmds/statsd/src/external/puller_util.cpp @@ -17,7 +17,7 @@ #define DEBUG false // STOPSHIP if true #include "Log.h" -#include "StatsPullerManagerImpl.h" +#include "StatsPullerManager.h" #include "puller_util.h" #include "statslog.h" @@ -107,8 +107,8 @@ bool tryMerge(vector>& data, int child_pos, const vector>& data, const sp& uidMap, int tagId) { - if (StatsPullerManagerImpl::kAllPullAtomInfo.find(tagId) == - StatsPullerManagerImpl::kAllPullAtomInfo.end()) { + if (StatsPullerManager::kAllPullAtomInfo.find(tagId) == + StatsPullerManager::kAllPullAtomInfo.end()) { VLOG("Unknown pull atom id %d", tagId); return; } @@ -121,9 +121,9 @@ void mergeIsolatedUidsToHostUid(vector>& data, const spsecond; // uidField is the field number in proto, } const vector& additiveFields = - StatsPullerManagerImpl::kAllPullAtomInfo.find(tagId)->second.additiveFields; + StatsPullerManager::kAllPullAtomInfo.find(tagId)->second.additiveFields; const vector& nonAdditiveFields = - StatsPullerManagerImpl::kAllPullAtomInfo.find(tagId)->second.nonAdditiveFields; + StatsPullerManager::kAllPullAtomInfo.find(tagId)->second.nonAdditiveFields; // map of host uid to their position in the original vector map> hostPosition; diff --git a/cmds/statsd/src/metrics/GaugeMetricProducer.cpp b/cmds/statsd/src/metrics/GaugeMetricProducer.cpp index aabd3616e2fe4..d75bb1093fb9a 100644 --- a/cmds/statsd/src/metrics/GaugeMetricProducer.cpp +++ b/cmds/statsd/src/metrics/GaugeMetricProducer.cpp @@ -72,9 +72,9 @@ GaugeMetricProducer::GaugeMetricProducer(const ConfigKey& key, const GaugeMetric const int conditionIndex, const sp& wizard, const int pullTagId, const int64_t timeBaseNs, const int64_t startTimeNs, - shared_ptr statsPullerManager) + const sp& pullerManager) : MetricProducer(metric.id(), key, timeBaseNs, conditionIndex, wizard), - mStatsPullerManager(statsPullerManager), + mPullerManager(pullerManager), mPullTagId(pullTagId), mMinBucketSizeNs(metric.min_bucket_size_nanos()), mDimensionSoftLimit(StatsdStats::kAtomDimensionKeySizeLimitMap.find(pullTagId) != @@ -127,8 +127,8 @@ GaugeMetricProducer::GaugeMetricProducer(const ConfigKey& key, const GaugeMetric flushIfNeededLocked(startTimeNs); // Kicks off the puller immediately. if (mPullTagId != -1 && mSamplingType == GaugeMetric::RANDOM_ONE_SAMPLE) { - mStatsPullerManager->RegisterReceiver( - mPullTagId, this, getCurrentBucketEndTimeNs(), mBucketSizeNs); + mPullerManager->RegisterReceiver(mPullTagId, this, getCurrentBucketEndTimeNs(), + mBucketSizeNs); } VLOG("Gauge metric %lld created. bucket size %lld start_time: %lld sliced %d", @@ -136,19 +136,10 @@ GaugeMetricProducer::GaugeMetricProducer(const ConfigKey& key, const GaugeMetric mConditionSliced); } -// for testing -GaugeMetricProducer::GaugeMetricProducer(const ConfigKey& key, const GaugeMetric& metric, - const int conditionIndex, - const sp& wizard, const int pullTagId, - const int64_t timeBaseNs, const int64_t startTimeNs) - : GaugeMetricProducer(key, metric, conditionIndex, wizard, pullTagId, timeBaseNs, startTimeNs, - make_shared()) { -} - GaugeMetricProducer::~GaugeMetricProducer() { VLOG("~GaugeMetricProducer() called"); if (mPullTagId != -1 && mSamplingType == GaugeMetric::RANDOM_ONE_SAMPLE) { - mStatsPullerManager->UnRegisterReceiver(mPullTagId, this); + mPullerManager->UnRegisterReceiver(mPullTagId, this); } } @@ -336,7 +327,7 @@ void GaugeMetricProducer::pullLocked(const int64_t timestampNs) { } vector> allData; - if (!mStatsPullerManager->Pull(mPullTagId, timestampNs, &allData)) { + if (!mPullerManager->Pull(mPullTagId, timestampNs, &allData)) { ALOGE("Gauge Stats puller failed for tag: %d", mPullTagId); return; } diff --git a/cmds/statsd/src/metrics/GaugeMetricProducer.h b/cmds/statsd/src/metrics/GaugeMetricProducer.h index c74f7927dfac7..62ec27eb8afd6 100644 --- a/cmds/statsd/src/metrics/GaugeMetricProducer.h +++ b/cmds/statsd/src/metrics/GaugeMetricProducer.h @@ -58,7 +58,8 @@ class GaugeMetricProducer : public virtual MetricProducer, public virtual PullDa public: GaugeMetricProducer(const ConfigKey& key, const GaugeMetric& gaugeMetric, const int conditionIndex, const sp& wizard, - const int pullTagId, const int64_t timeBaseNs, const int64_t startTimeNs); + const int pullTagId, const int64_t timeBaseNs, const int64_t startTimeNs, + const sp& pullerManager); virtual ~GaugeMetricProducer(); @@ -94,13 +95,6 @@ private: android::util::ProtoOutputStream* protoOutput) override; void clearPastBucketsLocked(const int64_t dumpTimeNs) override; - // for testing - GaugeMetricProducer(const ConfigKey& key, const GaugeMetric& gaugeMetric, - const int conditionIndex, const sp& wizard, - const int pullTagId, - const int64_t timeBaseNs, const int64_t startTimeNs, - std::shared_ptr statsPullerManager); - // Internal interface to handle condition change. void onConditionChangedLocked(const bool conditionMet, const int64_t eventTime) override; @@ -123,7 +117,7 @@ private: int mTagId; - std::shared_ptr mStatsPullerManager; + sp mPullerManager; // tagId for pulled data. -1 if this is not pulled const int mPullTagId; diff --git a/cmds/statsd/src/metrics/MetricsManager.cpp b/cmds/statsd/src/metrics/MetricsManager.cpp index 4fac0e1a141be..213f9abebe5f7 100644 --- a/cmds/statsd/src/metrics/MetricsManager.cpp +++ b/cmds/statsd/src/metrics/MetricsManager.cpp @@ -56,10 +56,12 @@ const int FIELD_ID_ANNOTATIONS_INT32 = 2; MetricsManager::MetricsManager(const ConfigKey& key, const StatsdConfig& config, const int64_t timeBaseNs, const int64_t currentTimeNs, - const sp &uidMap, + const sp& uidMap, + const sp& pullerManager, const sp& anomalyAlarmMonitor, const sp& periodicAlarmMonitor) - : mConfigKey(key), mUidMap(uidMap), + : mConfigKey(key), + mUidMap(uidMap), mTtlNs(config.has_ttl_in_seconds() ? config.ttl_in_seconds() * NS_PER_SEC : -1), mTtlEndNs(-1), mLastReportTimeNs(currentTimeNs), @@ -67,12 +69,11 @@ MetricsManager::MetricsManager(const ConfigKey& key, const StatsdConfig& config, // Init the ttl end timestamp. refreshTtl(timeBaseNs); - mConfigValid = - initStatsdConfig(key, config, *uidMap, anomalyAlarmMonitor, periodicAlarmMonitor, - timeBaseNs, currentTimeNs, mTagIds, mAllAtomMatchers, - mAllConditionTrackers, mAllMetricProducers, mAllAnomalyTrackers, - mAllPeriodicAlarmTrackers, mConditionToMetricMap, mTrackerToMetricMap, - mTrackerToConditionMap, mNoReportMetricIds); + mConfigValid = initStatsdConfig( + key, config, *uidMap, pullerManager, anomalyAlarmMonitor, periodicAlarmMonitor, + timeBaseNs, currentTimeNs, mTagIds, mAllAtomMatchers, mAllConditionTrackers, + mAllMetricProducers, mAllAnomalyTrackers, mAllPeriodicAlarmTrackers, + mConditionToMetricMap, mTrackerToMetricMap, mTrackerToConditionMap, mNoReportMetricIds); mHashStringsInReport = config.hash_strings_in_metric_report(); diff --git a/cmds/statsd/src/metrics/MetricsManager.h b/cmds/statsd/src/metrics/MetricsManager.h index 6f4db48def86b..dfbb69f1ab7c1 100644 --- a/cmds/statsd/src/metrics/MetricsManager.h +++ b/cmds/statsd/src/metrics/MetricsManager.h @@ -16,6 +16,7 @@ #pragma once +#include "external/StatsPullerManager.h" #include "anomaly/AlarmMonitor.h" #include "anomaly/AlarmTracker.h" #include "anomaly/AnomalyTracker.h" @@ -36,9 +37,10 @@ namespace statsd { // A MetricsManager is responsible for managing metrics from one single config source. class MetricsManager : public PackageInfoListener { public: - MetricsManager(const ConfigKey& configKey, const StatsdConfig& config, - const int64_t timeBaseNs, const int64_t currentTimeNs, - const sp& uidMap, const sp& anomalyAlarmMonitor, + MetricsManager(const ConfigKey& configKey, const StatsdConfig& config, const int64_t timeBaseNs, + const int64_t currentTimeNs, const sp& uidMap, + const sp& pullerManager, + const sp& anomalyAlarmMonitor, const sp& periodicAlarmMonitor); virtual ~MetricsManager(); diff --git a/cmds/statsd/src/metrics/ValueMetricProducer.cpp b/cmds/statsd/src/metrics/ValueMetricProducer.cpp index 41e55cb27f5eb..ef8b6cc83a26c 100644 --- a/cmds/statsd/src/metrics/ValueMetricProducer.cpp +++ b/cmds/statsd/src/metrics/ValueMetricProducer.cpp @@ -74,10 +74,10 @@ ValueMetricProducer::ValueMetricProducer(const ConfigKey& key, const ValueMetric const int conditionIndex, const sp& wizard, const int pullTagId, const int64_t timeBaseNs, const int64_t startTimestampNs, - shared_ptr statsPullerManager) + const sp& pullerManager) : MetricProducer(metric.id(), key, timeBaseNs, conditionIndex, wizard), + mPullerManager(pullerManager), mValueField(metric.value_field()), - mStatsPullerManager(statsPullerManager), mPullTagId(pullTagId), mMinBucketSizeNs(metric.min_bucket_size_nanos()), mDimensionSoftLimit(StatsdStats::kAtomDimensionKeySizeLimitMap.find(pullTagId) != @@ -127,27 +127,18 @@ ValueMetricProducer::ValueMetricProducer(const ConfigKey& key, const ValueMetric // Kicks off the puller immediately. flushIfNeededLocked(startTimestampNs); if (mPullTagId != -1) { - mStatsPullerManager->RegisterReceiver( - mPullTagId, this, mCurrentBucketStartTimeNs + mBucketSizeNs, mBucketSizeNs); + mPullerManager->RegisterReceiver(mPullTagId, this, + mCurrentBucketStartTimeNs + mBucketSizeNs, mBucketSizeNs); } VLOG("value metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(), (long long)mBucketSizeNs, (long long)mTimeBaseNs); } -// for testing -ValueMetricProducer::ValueMetricProducer(const ConfigKey& key, const ValueMetric& metric, - const int conditionIndex, - const sp& wizard, const int pullTagId, - const int64_t timeBaseNs, const int64_t startTimeNs) - : ValueMetricProducer(key, metric, conditionIndex, wizard, pullTagId, timeBaseNs, startTimeNs, - make_shared()) { -} - ValueMetricProducer::~ValueMetricProducer() { VLOG("~ValueMetricProducer() called"); if (mPullTagId != -1) { - mStatsPullerManager->UnRegisterReceiver(mPullTagId, this); + mPullerManager->UnRegisterReceiver(mPullTagId, this); } } @@ -283,7 +274,7 @@ void ValueMetricProducer::onConditionChangedLocked(const bool condition, if (mPullTagId != -1) { vector> allData; - if (mStatsPullerManager->Pull(mPullTagId, eventTimeNs, &allData)) { + if (mPullerManager->Pull(mPullTagId, eventTimeNs, &allData)) { if (allData.size() == 0) { return; } diff --git a/cmds/statsd/src/metrics/ValueMetricProducer.h b/cmds/statsd/src/metrics/ValueMetricProducer.h index cb6b051cd4849..75f311374d21e 100644 --- a/cmds/statsd/src/metrics/ValueMetricProducer.h +++ b/cmds/statsd/src/metrics/ValueMetricProducer.h @@ -40,7 +40,8 @@ class ValueMetricProducer : public virtual MetricProducer, public virtual PullDa public: ValueMetricProducer(const ConfigKey& key, const ValueMetric& valueMetric, const int conditionIndex, const sp& wizard, - const int pullTagId, const int64_t timeBaseNs, const int64_t startTimeNs); + const int pullTagId, const int64_t timeBaseNs, const int64_t startTimeNs, + const sp& pullerManager); virtual ~ValueMetricProducer(); @@ -53,7 +54,7 @@ public: if (mPullTagId != -1 && (mCondition == true || mConditionTrackerIndex < 0) ) { vector> allData; - mStatsPullerManager->Pull(mPullTagId, eventTimeNs, &allData); + mPullerManager->Pull(mPullTagId, eventTimeNs, &allData); if (allData.size() == 0) { // This shouldn't happen since this valuemetric is not useful now. } @@ -112,16 +113,10 @@ private: void dropDataLocked(const int64_t dropTimeNs) override; + sp mPullerManager; + const FieldMatcher mValueField; - std::shared_ptr mStatsPullerManager; - - // for testing - ValueMetricProducer(const ConfigKey& key, const ValueMetric& valueMetric, - const int conditionIndex, const sp& wizard, - const int pullTagId, const int64_t timeBaseNs, const int64_t startTimeNs, - std::shared_ptr statsPullerManager); - // tagId for pulled data. -1 if this is not pulled const int mPullTagId; diff --git a/cmds/statsd/src/metrics/metrics_manager_util.cpp b/cmds/statsd/src/metrics/metrics_manager_util.cpp index 811a00e47ae5f..deb9893f6eca1 100644 --- a/cmds/statsd/src/metrics/metrics_manager_util.cpp +++ b/cmds/statsd/src/metrics/metrics_manager_util.cpp @@ -262,9 +262,10 @@ bool initConditions(const ConfigKey& key, const StatsdConfig& config, return true; } -bool initMetrics(const ConfigKey& key, const StatsdConfig& config, - const int64_t timeBaseTimeNs, const int64_t currentTimeNs, - UidMap& uidMap, const unordered_map& logTrackerMap, +bool initMetrics(const ConfigKey& key, const StatsdConfig& config, const int64_t timeBaseTimeNs, + const int64_t currentTimeNs, UidMap& uidMap, + const sp& pullerManager, + const unordered_map& logTrackerMap, const unordered_map& conditionTrackerMap, const vector>& allAtomMatchers, vector>& allConditionTrackers, @@ -465,9 +466,9 @@ bool initMetrics(const ConfigKey& key, const StatsdConfig& config, } } - sp valueProducer = new ValueMetricProducer(key, metric, conditionIndex, - wizard, pullTagId, - timeBaseTimeNs, currentTimeNs); + sp valueProducer = + new ValueMetricProducer(key, metric, conditionIndex, wizard, pullTagId, + timeBaseTimeNs, currentTimeNs, pullerManager); allMetricProducers.push_back(valueProducer); } @@ -525,8 +526,9 @@ bool initMetrics(const ConfigKey& key, const StatsdConfig& config, } } - sp gaugeProducer = new GaugeMetricProducer( - key, metric, conditionIndex, wizard, pullTagId, timeBaseTimeNs, currentTimeNs); + sp gaugeProducer = + new GaugeMetricProducer(key, metric, conditionIndex, wizard, pullTagId, + timeBaseTimeNs, currentTimeNs, pullerManager); allMetricProducers.push_back(gaugeProducer); } for (int i = 0; i < config.no_report_metric_size(); ++i) { @@ -645,10 +647,10 @@ bool initAlarms(const StatsdConfig& config, const ConfigKey& key, } bool initStatsdConfig(const ConfigKey& key, const StatsdConfig& config, UidMap& uidMap, + const sp& pullerManager, const sp& anomalyAlarmMonitor, - const sp& periodicAlarmMonitor, - const int64_t timeBaseNs, const int64_t currentTimeNs, - set& allTagIds, + const sp& periodicAlarmMonitor, const int64_t timeBaseNs, + const int64_t currentTimeNs, set& allTagIds, vector>& allAtomMatchers, vector>& allConditionTrackers, vector>& allMetricProducers, @@ -674,9 +676,8 @@ bool initStatsdConfig(const ConfigKey& key, const StatsdConfig& config, UidMap& return false; } - if (!initMetrics(key, config, timeBaseNs, currentTimeNs, uidMap, - logTrackerMap, conditionTrackerMap, - allAtomMatchers, allConditionTrackers, allMetricProducers, + if (!initMetrics(key, config, timeBaseNs, currentTimeNs, uidMap, pullerManager, logTrackerMap, + conditionTrackerMap, allAtomMatchers, allConditionTrackers, allMetricProducers, conditionToMetricMap, trackerToMetricMap, metricProducerMap, noReportMetricIds)) { ALOGE("initMetricProducers failed"); diff --git a/cmds/statsd/src/metrics/metrics_manager_util.h b/cmds/statsd/src/metrics/metrics_manager_util.h index d749bf43c9bef..c6601493135fe 100644 --- a/cmds/statsd/src/metrics/metrics_manager_util.h +++ b/cmds/statsd/src/metrics/metrics_manager_util.h @@ -23,7 +23,7 @@ #include "../anomaly/AlarmTracker.h" #include "../condition/ConditionTracker.h" -#include "../external/StatsPullerManagerImpl.h" +#include "../external/StatsPullerManager.h" #include "../matchers/LogMatchingTracker.h" #include "../metrics/MetricProducer.h" @@ -81,9 +81,8 @@ bool initConditions(const ConfigKey& key, const StatsdConfig& config, // the list of MetricProducer index // [trackerToMetricMap]: contains the mapping from log tracker to MetricProducer index. bool initMetrics( - const ConfigKey& key, const StatsdConfig& config, - const int64_t timeBaseTimeNs, const int64_t currentTimeNs, - UidMap& uidMap, + const ConfigKey& key, const StatsdConfig& config, const int64_t timeBaseTimeNs, + const int64_t currentTimeNs, UidMap& uidMap, const sp& pullerManager, const std::unordered_map& logTrackerMap, const std::unordered_map& conditionTrackerMap, const std::unordered_map>& eventConditionLinks, @@ -97,10 +96,10 @@ bool initMetrics( // Initialize MetricsManager from StatsdConfig. // Parameters are the members of MetricsManager. See MetricsManager for declaration. bool initStatsdConfig(const ConfigKey& key, const StatsdConfig& config, UidMap& uidMap, + const sp& pullerManager, const sp& anomalyAlarmMonitor, - const sp& periodicAlarmMonitor, - const int64_t timeBaseNs, const int64_t currentTimeNs, - std::set& allTagIds, + const sp& periodicAlarmMonitor, const int64_t timeBaseNs, + const int64_t currentTimeNs, std::set& allTagIds, std::vector>& allAtomMatchers, std::vector>& allConditionTrackers, std::vector>& allMetricProducers, diff --git a/cmds/statsd/tests/MetricsManager_test.cpp b/cmds/statsd/tests/MetricsManager_test.cpp index 07378dbcce1a3..4de9986c3158b 100644 --- a/cmds/statsd/tests/MetricsManager_test.cpp +++ b/cmds/statsd/tests/MetricsManager_test.cpp @@ -271,6 +271,7 @@ StatsdConfig buildCirclePredicates() { TEST(MetricsManagerTest, TestGoodConfig) { UidMap uidMap; + sp pullerManager = new StatsPullerManager(); sp anomalyAlarmMonitor; sp periodicAlarmMonitor; StatsdConfig config = buildGoodConfig(); @@ -285,13 +286,11 @@ TEST(MetricsManagerTest, TestGoodConfig) { unordered_map> trackerToConditionMap; std::set noReportMetricIds; - EXPECT_TRUE(initStatsdConfig(kConfigKey, config, uidMap, - anomalyAlarmMonitor, periodicAlarmMonitor, - timeBaseSec, timeBaseSec, allTagIds, allAtomMatchers, - allConditionTrackers, allMetricProducers, allAnomalyTrackers, - allAlarmTrackers, - conditionToMetricMap, trackerToMetricMap, trackerToConditionMap, - noReportMetricIds)); + EXPECT_TRUE(initStatsdConfig(kConfigKey, config, uidMap, pullerManager, anomalyAlarmMonitor, + periodicAlarmMonitor, timeBaseSec, timeBaseSec, allTagIds, + allAtomMatchers, allConditionTrackers, allMetricProducers, + allAnomalyTrackers, allAlarmTrackers, conditionToMetricMap, + trackerToMetricMap, trackerToConditionMap, noReportMetricIds)); EXPECT_EQ(1u, allMetricProducers.size()); EXPECT_EQ(1u, allAnomalyTrackers.size()); EXPECT_EQ(1u, noReportMetricIds.size()); @@ -299,6 +298,7 @@ TEST(MetricsManagerTest, TestGoodConfig) { TEST(MetricsManagerTest, TestDimensionMetricsWithMultiTags) { UidMap uidMap; + sp pullerManager = new StatsPullerManager(); sp anomalyAlarmMonitor; sp periodicAlarmMonitor; StatsdConfig config = buildDimensionMetricsWithMultiTags(); @@ -313,17 +313,16 @@ TEST(MetricsManagerTest, TestDimensionMetricsWithMultiTags) { unordered_map> trackerToConditionMap; std::set noReportMetricIds; - EXPECT_FALSE(initStatsdConfig(kConfigKey, config, uidMap, - anomalyAlarmMonitor, periodicAlarmMonitor, - timeBaseSec, timeBaseSec, allTagIds, allAtomMatchers, - allConditionTrackers, allMetricProducers, allAnomalyTrackers, - allAlarmTrackers, - conditionToMetricMap, trackerToMetricMap, trackerToConditionMap, - noReportMetricIds)); + EXPECT_FALSE(initStatsdConfig(kConfigKey, config, uidMap, pullerManager, anomalyAlarmMonitor, + periodicAlarmMonitor, timeBaseSec, timeBaseSec, allTagIds, + allAtomMatchers, allConditionTrackers, allMetricProducers, + allAnomalyTrackers, allAlarmTrackers, conditionToMetricMap, + trackerToMetricMap, trackerToConditionMap, noReportMetricIds)); } TEST(MetricsManagerTest, TestCircleLogMatcherDependency) { UidMap uidMap; + sp pullerManager = new StatsPullerManager(); sp anomalyAlarmMonitor; sp periodicAlarmMonitor; StatsdConfig config = buildCircleMatchers(); @@ -338,17 +337,16 @@ TEST(MetricsManagerTest, TestCircleLogMatcherDependency) { unordered_map> trackerToConditionMap; std::set noReportMetricIds; - EXPECT_FALSE(initStatsdConfig(kConfigKey, config, uidMap, - anomalyAlarmMonitor, periodicAlarmMonitor, - timeBaseSec, timeBaseSec, allTagIds, allAtomMatchers, - allConditionTrackers, allMetricProducers, allAnomalyTrackers, - allAlarmTrackers, - conditionToMetricMap, trackerToMetricMap, trackerToConditionMap, - noReportMetricIds)); + EXPECT_FALSE(initStatsdConfig(kConfigKey, config, uidMap, pullerManager, anomalyAlarmMonitor, + periodicAlarmMonitor, timeBaseSec, timeBaseSec, allTagIds, + allAtomMatchers, allConditionTrackers, allMetricProducers, + allAnomalyTrackers, allAlarmTrackers, conditionToMetricMap, + trackerToMetricMap, trackerToConditionMap, noReportMetricIds)); } TEST(MetricsManagerTest, TestMissingMatchers) { UidMap uidMap; + sp pullerManager = new StatsPullerManager(); sp anomalyAlarmMonitor; sp periodicAlarmMonitor; StatsdConfig config = buildMissingMatchers(); @@ -362,17 +360,16 @@ TEST(MetricsManagerTest, TestMissingMatchers) { unordered_map> trackerToMetricMap; unordered_map> trackerToConditionMap; std::set noReportMetricIds; - EXPECT_FALSE(initStatsdConfig(kConfigKey, config, uidMap, - anomalyAlarmMonitor, periodicAlarmMonitor, - timeBaseSec, timeBaseSec, allTagIds, allAtomMatchers, - allConditionTrackers, allMetricProducers, allAnomalyTrackers, - allAlarmTrackers, - conditionToMetricMap, trackerToMetricMap, trackerToConditionMap, - noReportMetricIds)); + EXPECT_FALSE(initStatsdConfig(kConfigKey, config, uidMap, pullerManager, anomalyAlarmMonitor, + periodicAlarmMonitor, timeBaseSec, timeBaseSec, allTagIds, + allAtomMatchers, allConditionTrackers, allMetricProducers, + allAnomalyTrackers, allAlarmTrackers, conditionToMetricMap, + trackerToMetricMap, trackerToConditionMap, noReportMetricIds)); } TEST(MetricsManagerTest, TestMissingPredicate) { UidMap uidMap; + sp pullerManager = new StatsPullerManager(); sp anomalyAlarmMonitor; sp periodicAlarmMonitor; StatsdConfig config = buildMissingPredicate(); @@ -386,17 +383,16 @@ TEST(MetricsManagerTest, TestMissingPredicate) { unordered_map> trackerToMetricMap; unordered_map> trackerToConditionMap; std::set noReportMetricIds; - EXPECT_FALSE(initStatsdConfig(kConfigKey, config, uidMap, - anomalyAlarmMonitor, periodicAlarmMonitor, - timeBaseSec, timeBaseSec, allTagIds, allAtomMatchers, - allConditionTrackers, allMetricProducers, allAnomalyTrackers, - allAlarmTrackers, - conditionToMetricMap, trackerToMetricMap, trackerToConditionMap, - noReportMetricIds)); + EXPECT_FALSE(initStatsdConfig(kConfigKey, config, uidMap, pullerManager, anomalyAlarmMonitor, + periodicAlarmMonitor, timeBaseSec, timeBaseSec, allTagIds, + allAtomMatchers, allConditionTrackers, allMetricProducers, + allAnomalyTrackers, allAlarmTrackers, conditionToMetricMap, + trackerToMetricMap, trackerToConditionMap, noReportMetricIds)); } TEST(MetricsManagerTest, TestCirclePredicateDependency) { UidMap uidMap; + sp pullerManager = new StatsPullerManager(); sp anomalyAlarmMonitor; sp periodicAlarmMonitor; StatsdConfig config = buildCirclePredicates(); @@ -411,17 +407,16 @@ TEST(MetricsManagerTest, TestCirclePredicateDependency) { unordered_map> trackerToConditionMap; std::set noReportMetricIds; - EXPECT_FALSE(initStatsdConfig(kConfigKey, config, uidMap, - anomalyAlarmMonitor, periodicAlarmMonitor, - timeBaseSec, timeBaseSec, allTagIds, allAtomMatchers, - allConditionTrackers, allMetricProducers, allAnomalyTrackers, - allAlarmTrackers, - conditionToMetricMap, trackerToMetricMap, trackerToConditionMap, - noReportMetricIds)); + EXPECT_FALSE(initStatsdConfig(kConfigKey, config, uidMap, pullerManager, anomalyAlarmMonitor, + periodicAlarmMonitor, timeBaseSec, timeBaseSec, allTagIds, + allAtomMatchers, allConditionTrackers, allMetricProducers, + allAnomalyTrackers, allAlarmTrackers, conditionToMetricMap, + trackerToMetricMap, trackerToConditionMap, noReportMetricIds)); } TEST(MetricsManagerTest, testAlertWithUnknownMetric) { UidMap uidMap; + sp pullerManager = new StatsPullerManager(); sp anomalyAlarmMonitor; sp periodicAlarmMonitor; StatsdConfig config = buildAlertWithUnknownMetric(); @@ -436,13 +431,11 @@ TEST(MetricsManagerTest, testAlertWithUnknownMetric) { unordered_map> trackerToConditionMap; std::set noReportMetricIds; - EXPECT_FALSE(initStatsdConfig(kConfigKey, config, uidMap, - anomalyAlarmMonitor, periodicAlarmMonitor, - timeBaseSec, timeBaseSec, allTagIds, allAtomMatchers, - allConditionTrackers, allMetricProducers, allAnomalyTrackers, - allAlarmTrackers, - conditionToMetricMap, trackerToMetricMap, trackerToConditionMap, - noReportMetricIds)); + EXPECT_FALSE(initStatsdConfig(kConfigKey, config, uidMap, pullerManager, anomalyAlarmMonitor, + periodicAlarmMonitor, timeBaseSec, timeBaseSec, allTagIds, + allAtomMatchers, allConditionTrackers, allMetricProducers, + allAnomalyTrackers, allAlarmTrackers, conditionToMetricMap, + trackerToMetricMap, trackerToConditionMap, noReportMetricIds)); } #else diff --git a/cmds/statsd/tests/StatsLogProcessor_test.cpp b/cmds/statsd/tests/StatsLogProcessor_test.cpp index 76f3d8181deec..ecc57f5c4b26f 100644 --- a/cmds/statsd/tests/StatsLogProcessor_test.cpp +++ b/cmds/statsd/tests/StatsLogProcessor_test.cpp @@ -44,13 +44,13 @@ using android::util::ProtoOutputStream; */ class MockMetricsManager : public MetricsManager { public: - MockMetricsManager() : MetricsManager( - ConfigKey(1, 12345), StatsdConfig(), 1000, 1000, - new UidMap(), - new AlarmMonitor(10, [](const sp&, int64_t){}, - [](const sp&){}), - new AlarmMonitor(10, [](const sp&, int64_t){}, - [](const sp&){})) { + MockMetricsManager() + : MetricsManager(ConfigKey(1, 12345), StatsdConfig(), 1000, 1000, new UidMap(), + new StatsPullerManager(), + new AlarmMonitor(10, [](const sp&, int64_t) {}, + [](const sp&) {}), + new AlarmMonitor(10, [](const sp&, int64_t) {}, + [](const sp&) {})) { } MOCK_METHOD0(byteSize, size_t()); @@ -60,11 +60,12 @@ public: TEST(StatsLogProcessorTest, TestRateLimitByteSize) { sp m = new UidMap(); + sp pullerManager = new StatsPullerManager(); sp anomalyAlarmMonitor; sp periodicAlarmMonitor; // Construct the processor with a dummy sendBroadcast function that does nothing. - StatsLogProcessor p(m, anomalyAlarmMonitor, periodicAlarmMonitor, 0, - [](const ConfigKey& key) {return true;}); + StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, periodicAlarmMonitor, 0, + [](const ConfigKey& key) { return true; }); MockMetricsManager mockMetricsManager; @@ -79,11 +80,15 @@ TEST(StatsLogProcessorTest, TestRateLimitByteSize) { TEST(StatsLogProcessorTest, TestRateLimitBroadcast) { sp m = new UidMap(); + sp pullerManager = new StatsPullerManager(); sp anomalyAlarmMonitor; sp subscriberAlarmMonitor; int broadcastCount = 0; - StatsLogProcessor p(m, anomalyAlarmMonitor, subscriberAlarmMonitor, 0, - [&broadcastCount](const ConfigKey& key) { broadcastCount++; return true;}); + StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0, + [&broadcastCount](const ConfigKey& key) { + broadcastCount++; + return true; + }); MockMetricsManager mockMetricsManager; @@ -105,11 +110,15 @@ TEST(StatsLogProcessorTest, TestRateLimitBroadcast) { TEST(StatsLogProcessorTest, TestDropWhenByteSizeTooLarge) { sp m = new UidMap(); + sp pullerManager = new StatsPullerManager(); sp anomalyAlarmMonitor; sp subscriberAlarmMonitor; int broadcastCount = 0; - StatsLogProcessor p(m, anomalyAlarmMonitor, subscriberAlarmMonitor, 0, - [&broadcastCount](const ConfigKey& key) { broadcastCount++; return true;}); + StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0, + [&broadcastCount](const ConfigKey& key) { + broadcastCount++; + return true; + }); MockMetricsManager mockMetricsManager; @@ -143,12 +152,16 @@ StatsdConfig MakeConfig(bool includeMetric) { TEST(StatsLogProcessorTest, TestUidMapHasSnapshot) { // Setup simple config key corresponding to empty config. sp m = new UidMap(); + sp pullerManager = new StatsPullerManager(); m->updateMap(1, {1, 2}, {1, 2}, {String16("p1"), String16("p2")}); sp anomalyAlarmMonitor; sp subscriberAlarmMonitor; int broadcastCount = 0; - StatsLogProcessor p(m, anomalyAlarmMonitor, subscriberAlarmMonitor, 0, - [&broadcastCount](const ConfigKey& key) { broadcastCount++; return true;}); + StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0, + [&broadcastCount](const ConfigKey& key) { + broadcastCount++; + return true; + }); ConfigKey key(3, 4); StatsdConfig config = MakeConfig(true); p.OnConfigUpdated(0, key, config); @@ -168,12 +181,16 @@ TEST(StatsLogProcessorTest, TestUidMapHasSnapshot) { TEST(StatsLogProcessorTest, TestEmptyConfigHasNoUidMap) { // Setup simple config key corresponding to empty config. sp m = new UidMap(); + sp pullerManager = new StatsPullerManager(); m->updateMap(1, {1, 2}, {1, 2}, {String16("p1"), String16("p2")}); sp anomalyAlarmMonitor; sp subscriberAlarmMonitor; int broadcastCount = 0; - StatsLogProcessor p(m, anomalyAlarmMonitor, subscriberAlarmMonitor, 0, - [&broadcastCount](const ConfigKey& key) { broadcastCount++; return true;}); + StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0, + [&broadcastCount](const ConfigKey& key) { + broadcastCount++; + return true; + }); ConfigKey key(3, 4); StatsdConfig config = MakeConfig(false); p.OnConfigUpdated(0, key, config); @@ -191,11 +208,15 @@ TEST(StatsLogProcessorTest, TestEmptyConfigHasNoUidMap) { TEST(StatsLogProcessorTest, TestReportIncludesSubConfig) { // Setup simple config key corresponding to empty config. sp m = new UidMap(); + sp pullerManager = new StatsPullerManager(); sp anomalyAlarmMonitor; sp subscriberAlarmMonitor; int broadcastCount = 0; - StatsLogProcessor p(m, anomalyAlarmMonitor, subscriberAlarmMonitor, 0, - [&broadcastCount](const ConfigKey& key) { broadcastCount++; return true;}); + StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0, + [&broadcastCount](const ConfigKey& key) { + broadcastCount++; + return true; + }); ConfigKey key(3, 4); StatsdConfig config; auto annotation = config.add_annotation(); @@ -220,11 +241,15 @@ TEST(StatsLogProcessorTest, TestReportIncludesSubConfig) { TEST(StatsLogProcessorTest, TestOutOfOrderLogs) { // Setup simple config key corresponding to empty config. sp m = new UidMap(); + sp pullerManager = new StatsPullerManager(); sp anomalyAlarmMonitor; sp subscriberAlarmMonitor; int broadcastCount = 0; - StatsLogProcessor p(m, anomalyAlarmMonitor, subscriberAlarmMonitor, 0, - [&broadcastCount](const ConfigKey& key) { broadcastCount++; return true;}); + StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0, + [&broadcastCount](const ConfigKey& key) { + broadcastCount++; + return true; + }); LogEvent event1(0, 1 /*logd timestamp*/, 1001 /*elapsedRealtime*/); event1.init(); diff --git a/cmds/statsd/tests/UidMap_test.cpp b/cmds/statsd/tests/UidMap_test.cpp index e23131d7b45d9..99082cc647f66 100644 --- a/cmds/statsd/tests/UidMap_test.cpp +++ b/cmds/statsd/tests/UidMap_test.cpp @@ -40,11 +40,12 @@ const string kApp2 = "app2.sharing.1"; TEST(UidMapTest, TestIsolatedUID) { sp m = new UidMap(); + sp pullerManager = new StatsPullerManager(); sp anomalyAlarmMonitor; sp subscriberAlarmMonitor; // Construct the processor with a dummy sendBroadcast function that does nothing. - StatsLogProcessor p(m, anomalyAlarmMonitor, subscriberAlarmMonitor, 0, - [](const ConfigKey& key) {return true;}); + StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0, + [](const ConfigKey& key) { return true; }); LogEvent addEvent(android::util::ISOLATED_UID_CHANGED, 1); addEvent.write(100); // parent UID addEvent.write(101); // isolated UID diff --git a/cmds/statsd/tests/e2e/GaugeMetric_e2e_pull_test.cpp b/cmds/statsd/tests/e2e/GaugeMetric_e2e_pull_test.cpp index eca5690de478b..d98395e78467b 100644 --- a/cmds/statsd/tests/e2e/GaugeMetric_e2e_pull_test.cpp +++ b/cmds/statsd/tests/e2e/GaugeMetric_e2e_pull_test.cpp @@ -66,7 +66,7 @@ TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvents) { baseTimeNs, configAddedTimeNs, config, cfgKey); EXPECT_EQ(processor->mMetricsManagers.size(), 1u); EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); - processor->mStatsPullerManager.ForceClearPullerCache(); + processor->mPullerManager->ForceClearPullerCache(); int startBucketNum = processor->mMetricsManagers.begin()->second-> mAllMetricProducers[0]->getCurrentBucketNum(); @@ -74,12 +74,11 @@ TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvents) { // When creating the config, the gauge metric producer should register the alarm at the // end of the current bucket. - EXPECT_EQ((size_t)1, StatsPullerManagerImpl::GetInstance().mReceivers.size()); + EXPECT_EQ((size_t)1, processor->mPullerManager->mReceivers.size()); EXPECT_EQ(bucketSizeNs, - StatsPullerManagerImpl::GetInstance().mReceivers.begin()-> - second.front().intervalNs); - int64_t& nextPullTimeNs = StatsPullerManagerImpl::GetInstance().mReceivers.begin()-> - second.front().nextPullTimeNs; + processor->mPullerManager->mReceivers.begin()->second.front().intervalNs); + int64_t& nextPullTimeNs = + processor->mPullerManager->mReceivers.begin()->second.front().nextPullTimeNs; EXPECT_EQ(baseTimeNs + startBucketNum * bucketSizeNs + bucketSizeNs, nextPullTimeNs); auto screenOffEvent = CreateScreenStateChangedEvent(android::view::DISPLAY_STATE_OFF, @@ -212,7 +211,7 @@ TEST(GaugeMetricE2eTest, TestAllConditionChangesSamplePulledEvents) { baseTimeNs, configAddedTimeNs, config, cfgKey); EXPECT_EQ(processor->mMetricsManagers.size(), 1u); EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); - processor->mStatsPullerManager.ForceClearPullerCache(); + processor->mPullerManager->ForceClearPullerCache(); int startBucketNum = processor->mMetricsManagers.begin()->second-> mAllMetricProducers[0]->getCurrentBucketNum(); @@ -313,7 +312,7 @@ TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvent_LateAlarm) { baseTimeNs, configAddedTimeNs, config, cfgKey); EXPECT_EQ(processor->mMetricsManagers.size(), 1u); EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); - processor->mStatsPullerManager.ForceClearPullerCache(); + processor->mPullerManager->ForceClearPullerCache(); int startBucketNum = processor->mMetricsManagers.begin()->second-> mAllMetricProducers[0]->getCurrentBucketNum(); @@ -321,12 +320,11 @@ TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvent_LateAlarm) { // When creating the config, the gauge metric producer should register the alarm at the // end of the current bucket. - EXPECT_EQ((size_t)1, StatsPullerManagerImpl::GetInstance().mReceivers.size()); + EXPECT_EQ((size_t)1, processor->mPullerManager->mReceivers.size()); EXPECT_EQ(bucketSizeNs, - StatsPullerManagerImpl::GetInstance().mReceivers.begin()-> - second.front().intervalNs); - int64_t& nextPullTimeNs = StatsPullerManagerImpl::GetInstance().mReceivers.begin()-> - second.front().nextPullTimeNs; + processor->mPullerManager->mReceivers.begin()->second.front().intervalNs); + int64_t& nextPullTimeNs = + processor->mPullerManager->mReceivers.begin()->second.front().nextPullTimeNs; EXPECT_EQ(baseTimeNs + startBucketNum * bucketSizeNs + bucketSizeNs, nextPullTimeNs); auto screenOffEvent = CreateScreenStateChangedEvent(android::view::DISPLAY_STATE_OFF, diff --git a/cmds/statsd/tests/e2e/ValueMetric_pull_e2e_test.cpp b/cmds/statsd/tests/e2e/ValueMetric_pull_e2e_test.cpp index dd28d3611b4f7..744828e990f1c 100644 --- a/cmds/statsd/tests/e2e/ValueMetric_pull_e2e_test.cpp +++ b/cmds/statsd/tests/e2e/ValueMetric_pull_e2e_test.cpp @@ -66,7 +66,7 @@ TEST(ValueMetricE2eTest, TestPulledEvents) { baseTimeNs, configAddedTimeNs, config, cfgKey); EXPECT_EQ(processor->mMetricsManagers.size(), 1u); EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); - processor->mStatsPullerManager.ForceClearPullerCache(); + processor->mPullerManager->ForceClearPullerCache(); int startBucketNum = processor->mMetricsManagers.begin()->second-> mAllMetricProducers[0]->getCurrentBucketNum(); @@ -74,12 +74,11 @@ TEST(ValueMetricE2eTest, TestPulledEvents) { // When creating the config, the gauge metric producer should register the alarm at the // end of the current bucket. - EXPECT_EQ((size_t)1, StatsPullerManagerImpl::GetInstance().mReceivers.size()); + EXPECT_EQ((size_t)1, processor->mPullerManager->mReceivers.size()); EXPECT_EQ(bucketSizeNs, - StatsPullerManagerImpl::GetInstance().mReceivers.begin()-> - second.front().intervalNs); - int64_t& expectedPullTimeNs = StatsPullerManagerImpl::GetInstance().mReceivers.begin()-> - second.front().nextPullTimeNs; + processor->mPullerManager->mReceivers.begin()->second.front().intervalNs); + int64_t& expectedPullTimeNs = + processor->mPullerManager->mReceivers.begin()->second.front().nextPullTimeNs; EXPECT_EQ(baseTimeNs + startBucketNum * bucketSizeNs + bucketSizeNs, expectedPullTimeNs); auto screenOffEvent = CreateScreenStateChangedEvent(android::view::DISPLAY_STATE_OFF, @@ -173,7 +172,7 @@ TEST(ValueMetricE2eTest, TestPulledEvents_LateAlarm) { baseTimeNs, configAddedTimeNs, config, cfgKey); EXPECT_EQ(processor->mMetricsManagers.size(), 1u); EXPECT_TRUE(processor->mMetricsManagers.begin()->second->isConfigValid()); - processor->mStatsPullerManager.ForceClearPullerCache(); + processor->mPullerManager->ForceClearPullerCache(); int startBucketNum = processor->mMetricsManagers.begin()->second-> mAllMetricProducers[0]->getCurrentBucketNum(); @@ -181,12 +180,11 @@ TEST(ValueMetricE2eTest, TestPulledEvents_LateAlarm) { // When creating the config, the gauge metric producer should register the alarm at the // end of the current bucket. - EXPECT_EQ((size_t)1, StatsPullerManagerImpl::GetInstance().mReceivers.size()); + EXPECT_EQ((size_t)1, processor->mPullerManager->mReceivers.size()); EXPECT_EQ(bucketSizeNs, - StatsPullerManagerImpl::GetInstance().mReceivers.begin()-> - second.front().intervalNs); - int64_t& expectedPullTimeNs = StatsPullerManagerImpl::GetInstance().mReceivers.begin()-> - second.front().nextPullTimeNs; + processor->mPullerManager->mReceivers.begin()->second.front().intervalNs); + int64_t& expectedPullTimeNs = + processor->mPullerManager->mReceivers.begin()->second.front().nextPullTimeNs; EXPECT_EQ(baseTimeNs + startBucketNum * bucketSizeNs + bucketSizeNs, expectedPullTimeNs); // Screen off/on/off events. diff --git a/cmds/statsd/tests/metrics/GaugeMetricProducer_test.cpp b/cmds/statsd/tests/metrics/GaugeMetricProducer_test.cpp index 698ce727e6884..c7e72f97b2d31 100644 --- a/cmds/statsd/tests/metrics/GaugeMetricProducer_test.cpp +++ b/cmds/statsd/tests/metrics/GaugeMetricProducer_test.cpp @@ -61,8 +61,7 @@ TEST(GaugeMetricProducerTest, TestNoCondition) { // TODO: pending refactor of StatsPullerManager // For now we still need this so that it doesn't do real pulling. - shared_ptr pullerManager = - make_shared>(); + sp pullerManager = new StrictMock(); EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _, _)).WillOnce(Return()); EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillOnce(Return()); @@ -89,8 +88,7 @@ TEST(GaugeMetricProducerTest, TestNoCondition) { EXPECT_EQ(0UL, gaugeProducer.mPastBuckets.size()); allData.clear(); - std::shared_ptr event2 = - std::make_shared(tagId, bucket3StartTimeNs + 10); + std::shared_ptr event2 = std::make_shared(tagId, bucket3StartTimeNs + 10); event2->write(24); event2->write("some value"); event2->write(25); @@ -140,8 +138,7 @@ TEST(GaugeMetricProducerTest, TestPushedEventsWithUpgrade) { alert.set_trigger_if_sum_gt(25); alert.set_num_buckets(100); sp wizard = new NaggyMock(); - shared_ptr pullerManager = - make_shared>(); + sp pullerManager = new StrictMock(); GaugeMetricProducer gaugeProducer(kConfigKey, metric, -1 /*-1 meaning no condition*/, wizard, -1 /* -1 means no pulling */, bucketStartTimeNs, bucketStartTimeNs, pullerManager); @@ -211,8 +208,7 @@ TEST(GaugeMetricProducerTest, TestPulledWithUpgrade) { sp wizard = new NaggyMock(); - shared_ptr pullerManager = - make_shared>(); + sp pullerManager = new StrictMock(); EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _, _)).WillOnce(Return()); EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillOnce(Return()); EXPECT_CALL(*pullerManager, Pull(tagId, _, _)) @@ -280,8 +276,7 @@ TEST(GaugeMetricProducerTest, TestWithCondition) { sp wizard = new NaggyMock(); - shared_ptr pullerManager = - make_shared>(); + sp pullerManager = new StrictMock(); EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _, _)).WillOnce(Return()); EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillOnce(Return()); EXPECT_CALL(*pullerManager, Pull(tagId, _, _)) @@ -296,8 +291,8 @@ TEST(GaugeMetricProducerTest, TestWithCondition) { return true; })); - GaugeMetricProducer gaugeProducer(kConfigKey, metric, 1, wizard, tagId, - bucketStartTimeNs, bucketStartTimeNs, pullerManager); + GaugeMetricProducer gaugeProducer(kConfigKey, metric, 1, wizard, tagId, bucketStartTimeNs, + bucketStartTimeNs, pullerManager); gaugeProducer.setBucketSize(60 * NS_PER_SEC); gaugeProducer.onConditionChanged(true, bucketStartTimeNs + 8); @@ -372,8 +367,7 @@ TEST(GaugeMetricProducerTest, TestWithSlicedCondition) { return ConditionState::kTrue; })); - shared_ptr pullerManager = - make_shared>(); + sp pullerManager = new StrictMock(); EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _, _)).WillOnce(Return()); EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillOnce(Return()); EXPECT_CALL(*pullerManager, Pull(tagId, _, _)) @@ -421,8 +415,7 @@ TEST(GaugeMetricProducerTest, TestAnomalyDetection) { sp alarmMonitor; sp wizard = new NaggyMock(); - shared_ptr pullerManager = - make_shared>(); + sp pullerManager = new StrictMock(); EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _, _)).WillOnce(Return()); EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillOnce(Return()); @@ -472,7 +465,7 @@ TEST(GaugeMetricProducerTest, TestAnomalyDetection) { .mFields->begin() ->mValue.int_value); EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(DEFAULT_METRIC_DIMENSION_KEY), - std::ceil(1.0 * event2->GetElapsedTimestampNs() / NS_PER_SEC) + refPeriodSec); + std::ceil(1.0 * event2->GetElapsedTimestampNs() / NS_PER_SEC) + refPeriodSec); std::shared_ptr event3 = std::make_shared(tagId, bucketStartTimeNs + 2 * bucketSizeNs + 10); @@ -487,7 +480,7 @@ TEST(GaugeMetricProducerTest, TestAnomalyDetection) { .mFields->begin() ->mValue.int_value); EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(DEFAULT_METRIC_DIMENSION_KEY), - std::ceil(1.0 * event2->GetElapsedTimestampNs() / NS_PER_SEC + refPeriodSec)); + std::ceil(1.0 * event2->GetElapsedTimestampNs() / NS_PER_SEC + refPeriodSec)); // The event4 does not have the gauge field. Thus the current bucket value is 0. std::shared_ptr event4 = diff --git a/cmds/statsd/tests/metrics/ValueMetricProducer_test.cpp b/cmds/statsd/tests/metrics/ValueMetricProducer_test.cpp index e3a8a553acc92..d93b46fcc28bd 100644 --- a/cmds/statsd/tests/metrics/ValueMetricProducer_test.cpp +++ b/cmds/statsd/tests/metrics/ValueMetricProducer_test.cpp @@ -62,8 +62,7 @@ TEST(ValueMetricProducerTest, TestNonDimensionalEvents) { sp wizard = new NaggyMock(); // TODO: pending refactor of StatsPullerManager // For now we still need this so that it doesn't do real pulling. - shared_ptr pullerManager = - make_shared>(); + sp pullerManager = new StrictMock(); EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _, _)).WillOnce(Return()); EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillOnce(Return()); @@ -140,8 +139,7 @@ TEST(ValueMetricProducerTest, TestPulledEventsTakeAbsoluteValueOnReset) { metric.set_use_absolute_value_on_reset(true); sp wizard = new NaggyMock(); - shared_ptr pullerManager = - make_shared>(); + sp pullerManager = new StrictMock(); EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _, _)).WillOnce(Return()); EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillOnce(Return()); @@ -214,8 +212,7 @@ TEST(ValueMetricProducerTest, TestPulledEventsTakeZeroOnReset) { metric.mutable_value_field()->add_child()->set_field(2); sp wizard = new NaggyMock(); - shared_ptr pullerManager = - make_shared>(); + sp pullerManager = new StrictMock(); EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _, _)).WillOnce(Return()); EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillOnce(Return()); @@ -287,8 +284,7 @@ TEST(ValueMetricProducerTest, TestEventsWithNonSlicedCondition) { metric.set_condition(StringToId("SCREEN_ON")); sp wizard = new NaggyMock(); - shared_ptr pullerManager = - make_shared>(); + sp pullerManager = new StrictMock(); EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _, _)).WillOnce(Return()); EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillRepeatedly(Return()); @@ -365,8 +361,7 @@ TEST(ValueMetricProducerTest, TestPushedEventsWithUpgrade) { metric.mutable_value_field()->add_child()->set_field(2); sp wizard = new NaggyMock(); - shared_ptr pullerManager = - make_shared>(); + sp pullerManager = new StrictMock(); ValueMetricProducer valueProducer(kConfigKey, metric, -1, wizard, -1, bucketStartTimeNs, bucketStartTimeNs, pullerManager); valueProducer.setBucketSize(60 * NS_PER_SEC); @@ -408,8 +403,7 @@ TEST(ValueMetricProducerTest, TestPulledValueWithUpgrade) { metric.mutable_value_field()->add_child()->set_field(2); sp wizard = new NaggyMock(); - shared_ptr pullerManager = - make_shared>(); + sp pullerManager = new StrictMock(); EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _, _)).WillOnce(Return()); EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillOnce(Return()); EXPECT_CALL(*pullerManager, Pull(tagId, _, _)) @@ -464,8 +458,7 @@ TEST(ValueMetricProducerTest, TestPulledValueWithUpgradeWhileConditionFalse) { metric.set_condition(StringToId("SCREEN_ON")); sp wizard = new NaggyMock(); - shared_ptr pullerManager = - make_shared>(); + sp pullerManager = new StrictMock(); EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _, _)).WillOnce(Return()); EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillOnce(Return()); EXPECT_CALL(*pullerManager, Pull(tagId, _, _)) @@ -514,8 +507,7 @@ TEST(ValueMetricProducerTest, TestPushedEventsWithoutCondition) { metric.mutable_value_field()->add_child()->set_field(2); sp wizard = new NaggyMock(); - shared_ptr pullerManager = - make_shared>(); + sp pullerManager = new StrictMock(); ValueMetricProducer valueProducer(kConfigKey, metric, -1, wizard, -1, bucketStartTimeNs, bucketStartTimeNs, pullerManager); @@ -556,8 +548,7 @@ TEST(ValueMetricProducerTest, TestPushedEventsWithCondition) { metric.mutable_value_field()->add_child()->set_field(2); sp wizard = new NaggyMock(); - shared_ptr pullerManager = - make_shared>(); + sp pullerManager = new StrictMock(); ValueMetricProducer valueProducer(kConfigKey, metric, 1, wizard, -1, bucketStartTimeNs, bucketStartTimeNs, pullerManager); @@ -631,8 +622,10 @@ TEST(ValueMetricProducerTest, TestAnomalyDetection) { metric.mutable_value_field()->add_child()->set_field(2); sp wizard = new NaggyMock(); + sp pullerManager = new StrictMock(); ValueMetricProducer valueProducer(kConfigKey, metric, -1 /*-1 meaning no condition*/, wizard, - -1 /*not pulled*/, bucketStartTimeNs, bucketStartTimeNs); + -1 /*not pulled*/, bucketStartTimeNs, bucketStartTimeNs, + pullerManager); valueProducer.setBucketSize(60 * NS_PER_SEC); sp anomalyTracker = valueProducer.addAnomalyTracker(alert, alarmMonitor); @@ -705,8 +698,7 @@ TEST(ValueMetricProducerTest, TestBucketBoundaryNoCondition) { metric.mutable_value_field()->add_child()->set_field(2); sp wizard = new NaggyMock(); - shared_ptr pullerManager = - make_shared>(); + sp pullerManager = new StrictMock(); EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _, _)).WillOnce(Return()); EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillOnce(Return()); @@ -790,8 +782,7 @@ TEST(ValueMetricProducerTest, TestBucketBoundaryWithCondition) { metric.set_condition(StringToId("SCREEN_ON")); sp wizard = new NaggyMock(); - shared_ptr pullerManager = - make_shared>(); + sp pullerManager = new StrictMock(); EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _, _)).WillOnce(Return()); EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillRepeatedly(Return()); @@ -873,8 +864,7 @@ TEST(ValueMetricProducerTest, TestBucketBoundaryWithCondition2) { metric.set_condition(StringToId("SCREEN_ON")); sp wizard = new NaggyMock(); - shared_ptr pullerManager = - make_shared>(); + sp pullerManager = new StrictMock(); EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _, _)).WillRepeatedly(Return()); EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillRepeatedly(Return()); @@ -976,8 +966,7 @@ TEST(ValueMetricProducerTest, TestBucketBoundaryWithCondition3) { metric.set_condition(StringToId("SCREEN_ON")); sp wizard = new NaggyMock(); - shared_ptr pullerManager = - make_shared>(); + sp pullerManager = new StrictMock(); EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _, _)).WillOnce(Return()); EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillRepeatedly(Return()); diff --git a/cmds/statsd/tests/statsd_test_util.cpp b/cmds/statsd/tests/statsd_test_util.cpp index e0c98cb9735b0..b8b1a1db2c127 100644 --- a/cmds/statsd/tests/statsd_test_util.cpp +++ b/cmds/statsd/tests/statsd_test_util.cpp @@ -452,14 +452,16 @@ std::unique_ptr CreateIsolatedUidChangedEvent( sp CreateStatsLogProcessor(const int64_t timeBaseNs, const int64_t currentTimeNs, const StatsdConfig& config, const ConfigKey& key) { sp uidMap = new UidMap(); + sp pullerManager = new StatsPullerManager(); sp anomalyAlarmMonitor = new AlarmMonitor(1, [](const sp&, int64_t){}, [](const sp&){}); sp periodicAlarmMonitor = new AlarmMonitor(1, [](const sp&, int64_t){}, [](const sp&){}); - sp processor = new StatsLogProcessor( - uidMap, anomalyAlarmMonitor, periodicAlarmMonitor, timeBaseNs, [](const ConfigKey&){return true;}); + sp processor = + new StatsLogProcessor(uidMap, pullerManager, anomalyAlarmMonitor, periodicAlarmMonitor, + timeBaseNs, [](const ConfigKey&) { return true; }); processor->OnConfigUpdated(currentTimeNs, key, config); return processor; }