/* * 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 "anomaly/AlarmTracker.h" #include "condition/ConditionTracker.h" #include "config/ConfigMetadataProvider.h" #include "external/StatsPullerManager.h" #include "matchers/AtomMatchingTracker.h" #include "metrics/MetricProducer.h" namespace android { namespace os { namespace statsd { // Helper functions for creating, validating, and updating config components from StatsdConfig. // Should only be called from metrics_manager_util and config_update_utils. // Create a AtomMatchingTracker. // input: // [logMatcher]: the input AtomMatcher from the StatsdConfig // [invalidConfigReason]: logging ids if config is invalid // output: // new AtomMatchingTracker, or null if the tracker is unable to be created sp createAtomMatchingTracker( const AtomMatcher& logMatcher, const sp& uidMap, optional& invalidConfigReason); // Create a ConditionTracker. // input: // [predicate]: the input Predicate from the StatsdConfig // [index]: the index of the condition tracker // [atomMatchingTrackerMap]: map of atom matcher id to its index in allAtomMatchingTrackers // [invalidConfigReason]: logging ids if config is invalid // output: // new ConditionTracker, or null if the tracker is unable to be created sp createConditionTracker( const ConfigKey& key, const Predicate& predicate, int index, const unordered_map& atomMatchingTrackerMap, optional& invalidConfigReason); // Get the hash of a metric, combining the activation if the metric has one. optional getMetricProtoHash( const StatsdConfig& config, const google::protobuf::MessageLite& metric, int64_t id, const std::unordered_map& metricToActivationMap, uint64_t& metricHash); // 1. Validates matcher existence // 2. Enforces matchers with dimensions and those used for trigger_event are about one atom // 3. Gets matcher index and updates tracker to metric map optional handleMetricWithAtomMatchingTrackers( const int64_t matcherId, int64_t metricId, int metricIndex, const bool enforceOneAtom, const std::vector>& allAtomMatchingTrackers, const std::unordered_map& atomMatchingTrackerMap, std::unordered_map>& trackerToMetricMap, int& logTrackerIndex); // 1. Validates condition existence, including those in links // 2. Gets condition index and updates condition to metric map optional handleMetricWithConditions( const int64_t condition, int64_t metricId, int metricIndex, const std::unordered_map& conditionTrackerMap, const ::google::protobuf::RepeatedPtrField& links, const std::vector>& allConditionTrackers, int& conditionIndex, std::unordered_map>& conditionToMetricMap); // Validates a metricActivation and populates state. // Fills the new event activation/deactivation maps, preserving the existing activations. // Returns nullopt if successful and InvalidConfigReason if not. optional handleMetricActivationOnConfigUpdate( const StatsdConfig& config, int64_t metricId, int metricIndex, const std::unordered_map& metricToActivationMap, const std::unordered_map& oldAtomMatchingTrackerMap, const std::unordered_map& newAtomMatchingTrackerMap, const std::unordered_map>& oldEventActivationMap, std::unordered_map>& activationAtomTrackerToMetricMap, std::unordered_map>& deactivationAtomTrackerToMetricMap, std::vector& metricsWithActivation, std::unordered_map>& newEventActivationMap, std::unordered_map>>& newEventDeactivationMap); // Creates a CountMetricProducer and updates the vectors/maps used by MetricsManager with // the appropriate indices. Returns an sp to the producer, or nullopt if there was an error. optional> createCountMetricProducerAndUpdateMetadata( const ConfigKey& key, const StatsdConfig& config, int64_t timeBaseNs, const int64_t currentTimeNs, const CountMetric& metric, int metricIndex, const std::vector>& allAtomMatchingTrackers, const std::unordered_map& atomMatchingTrackerMap, std::vector>& allConditionTrackers, const std::unordered_map& conditionTrackerMap, const std::vector& initialConditionCache, const sp& wizard, const std::unordered_map& stateAtomIdMap, const std::unordered_map>& allStateGroupMaps, const std::unordered_map& metricToActivationMap, std::unordered_map>& trackerToMetricMap, std::unordered_map>& conditionToMetricMap, std::unordered_map>& activationAtomTrackerToMetricMap, std::unordered_map>& deactivationAtomTrackerToMetricMap, std::vector& metricsWithActivation, optional& invalidConfigReason, const wp configMetadataProvider); // Creates a DurationMetricProducer and updates the vectors/maps used by MetricsManager with // the appropriate indices. Returns an sp to the producer, or nullopt if there was an error. optional> createDurationMetricProducerAndUpdateMetadata( const ConfigKey& key, const StatsdConfig& config, int64_t timeBaseNs, const int64_t currentTimeNs, const DurationMetric& metric, int metricIndex, const std::vector>& allAtomMatchingTrackers, const std::unordered_map& atomMatchingTrackerMap, std::vector>& allConditionTrackers, const std::unordered_map& conditionTrackerMap, const std::vector& initialConditionCache, const sp& wizard, const std::unordered_map& stateAtomIdMap, const std::unordered_map>& allStateGroupMaps, const std::unordered_map& metricToActivationMap, std::unordered_map>& trackerToMetricMap, std::unordered_map>& conditionToMetricMap, std::unordered_map>& activationAtomTrackerToMetricMap, std::unordered_map>& deactivationAtomTrackerToMetricMap, std::vector& metricsWithActivation, optional& invalidConfigReason, const wp configMetadataProvider); // Creates an EventMetricProducer and updates the vectors/maps used by MetricsManager with // the appropriate indices. Returns an sp to the producer, or nullopt if there was an error. optional> createEventMetricProducerAndUpdateMetadata( const ConfigKey& key, const StatsdConfig& config, int64_t timeBaseNs, const EventMetric& metric, int metricIndex, const std::vector>& allAtomMatchingTrackers, const std::unordered_map& atomMatchingTrackerMap, std::vector>& allConditionTrackers, const std::unordered_map& conditionTrackerMap, const std::vector& initialConditionCache, const sp& wizard, const std::unordered_map& metricToActivationMap, std::unordered_map>& trackerToMetricMap, std::unordered_map>& conditionToMetricMap, std::unordered_map>& activationAtomTrackerToMetricMap, std::unordered_map>& deactivationAtomTrackerToMetricMap, std::vector& metricsWithActivation, optional& invalidConfigReason, const wp configMetadataProvider); // Creates a NumericValueMetricProducer and updates the vectors/maps used by MetricsManager with // the appropriate indices. Returns an sp to the producer, or nullopt if there was an error. optional> createNumericValueMetricProducerAndUpdateMetadata( const ConfigKey& key, const StatsdConfig& config, int64_t timeBaseNs, const int64_t currentTimeNs, const sp& pullerManager, const ValueMetric& metric, int metricIndex, const std::vector>& allAtomMatchingTrackers, const std::unordered_map& atomMatchingTrackerMap, std::vector>& allConditionTrackers, const std::unordered_map& conditionTrackerMap, const std::vector& initialConditionCache, const sp& wizard, const sp& matcherWizard, const std::unordered_map& stateAtomIdMap, const std::unordered_map>& allStateGroupMaps, const std::unordered_map& metricToActivationMap, std::unordered_map>& trackerToMetricMap, std::unordered_map>& conditionToMetricMap, std::unordered_map>& activationAtomTrackerToMetricMap, std::unordered_map>& deactivationAtomTrackerToMetricMap, std::vector& metricsWithActivation, optional& invalidConfigReason, const wp configMetadataProvider); // Creates a GaugeMetricProducer and updates the vectors/maps used by MetricsManager with // the appropriate indices. Returns an sp to the producer, or nullopt if there was an error. optional> createGaugeMetricProducerAndUpdateMetadata( const ConfigKey& key, const StatsdConfig& config, int64_t timeBaseNs, const int64_t currentTimeNs, const sp& pullerManager, const GaugeMetric& metric, int metricIndex, const std::vector>& allAtomMatchingTrackers, const std::unordered_map& atomMatchingTrackerMap, std::vector>& allConditionTrackers, const std::unordered_map& conditionTrackerMap, const std::vector& initialConditionCache, const sp& wizard, const sp& matcherWizard, const std::unordered_map& metricToActivationMap, std::unordered_map>& trackerToMetricMap, std::unordered_map>& conditionToMetricMap, std::unordered_map>& activationAtomTrackerToMetricMap, std::unordered_map>& deactivationAtomTrackerToMetricMap, std::vector& metricsWithActivation, optional& invalidConfigReason, const wp configMetadataProvider); // Creates a KllMetricProducer and updates the vectors/maps used by MetricsManager with // the appropriate indices. Returns an sp to the producer, or nullopt if there was an error. optional> createKllMetricProducerAndUpdateMetadata( const ConfigKey& key, const StatsdConfig& config, int64_t timeBaseNs, const int64_t currentTimeNs, const sp& pullerManager, const KllMetric& metric, int metricIndex, const vector>& allAtomMatchingTrackers, const unordered_map& atomMatchingTrackerMap, vector>& allConditionTrackers, const unordered_map& conditionTrackerMap, const vector& initialConditionCache, const sp& wizard, const sp& matcherWizard, const unordered_map& stateAtomIdMap, const unordered_map>& allStateGroupMaps, const unordered_map& metricToActivationMap, unordered_map>& trackerToMetricMap, unordered_map>& conditionToMetricMap, unordered_map>& activationAtomTrackerToMetricMap, unordered_map>& deactivationAtomTrackerToMetricMap, vector& metricsWithActivation, optional& invalidConfigReason, const wp configMetadataProvider); // Creates an AnomalyTracker and adds it to the appropriate metric. // Returns an sp to the AnomalyTracker, or nullopt if there was an error. optional> createAnomalyTracker( const Alert& alert, const sp& anomalyAlarmMonitor, const UpdateStatus& updateStatus, int64_t currentTimeNs, const std::unordered_map& metricProducerMap, std::vector>& allMetricProducers, optional& invalidConfigReason); // Templated function for adding subscriptions to alarms or alerts. Returns nullopt if successful // and InvalidConfigReason if not. template optional initSubscribersForSubscriptionType( const StatsdConfig& config, const Subscription_RuleType ruleType, const std::unordered_map& ruleMap, std::vector& allRules) { for (int i = 0; i < config.subscription_size(); ++i) { const Subscription& subscription = config.subscription(i); if (subscription.rule_type() != ruleType) { continue; } if (subscription.subscriber_information_case() == Subscription::SubscriberInformationCase::SUBSCRIBER_INFORMATION_NOT_SET) { ALOGW("subscription \"%lld\" has no subscriber info.\"", (long long)subscription.id()); return createInvalidConfigReasonWithSubscription( INVALID_CONFIG_REASON_SUBSCRIPTION_SUBSCRIBER_INFO_MISSING, subscription.id()); } const auto& itr = ruleMap.find(subscription.rule_id()); if (itr == ruleMap.end()) { ALOGW("subscription \"%lld\" has unknown rule id: \"%lld\"", (long long)subscription.id(), (long long)subscription.rule_id()); switch (subscription.rule_type()) { case Subscription::ALARM: return createInvalidConfigReasonWithSubscriptionAndAlarm( INVALID_CONFIG_REASON_SUBSCRIPTION_RULE_NOT_FOUND, subscription.id(), subscription.rule_id()); case Subscription::ALERT: return createInvalidConfigReasonWithSubscriptionAndAlert( INVALID_CONFIG_REASON_SUBSCRIPTION_RULE_NOT_FOUND, subscription.id(), subscription.rule_id()); case Subscription::RULE_TYPE_UNSPECIFIED: return createInvalidConfigReasonWithSubscription( INVALID_CONFIG_REASON_SUBSCRIPTION_RULE_NOT_FOUND, subscription.id()); } } const int ruleIndex = itr->second; allRules[ruleIndex]->addSubscription(subscription); } return nullopt; } // Helper functions for MetricsManager to initialize from StatsdConfig. // *Note*: only initStatsdConfig() should be called from outside. // All other functions are intermediate // steps, created to make unit tests easier. And most of the parameters in these // functions are temporary objects in the initialization phase. // Initialize the AtomMatchingTrackers. // input: // [key]: the config key that this config belongs to // [config]: the input StatsdConfig // output: // [atomMatchingTrackerMap]: this map should contain matcher name to index mapping // [allAtomMatchingTrackers]: should store the sp to all the AtomMatchingTracker // [allTagIdsToMatchersMap]: maps of tag ids to atom matchers // Returns nullopt if successful and InvalidConfigReason if not. optional initAtomMatchingTrackers( const StatsdConfig& config, const sp& uidMap, std::unordered_map& atomMatchingTrackerMap, std::vector>& allAtomMatchingTrackers, std::unordered_map>& allTagIdsToMatchersMap); // Initialize ConditionTrackers // input: // [key]: the config key that this config belongs to // [config]: the input config // [atomMatchingTrackerMap]: AtomMatchingTracker name to index mapping from previous step. // output: // [conditionTrackerMap]: this map should contain condition name to index mapping // [allConditionTrackers]: stores the sp to all the ConditionTrackers // [trackerToConditionMap]: contain the mapping from index of // log tracker to condition trackers that use the log tracker // [initialConditionCache]: stores the initial conditions for each ConditionTracker // Returns nullopt if successful and InvalidConfigReason if not. optional initConditions( const ConfigKey& key, const StatsdConfig& config, const std::unordered_map& atomMatchingTrackerMap, std::unordered_map& conditionTrackerMap, std::vector>& allConditionTrackers, std::unordered_map>& trackerToConditionMap, std::vector& initialConditionCache); // Initialize State maps using State protos in the config. These maps will // eventually be passed to MetricProducers to initialize their state info. // input: // [config]: the input config // output: // [stateAtomIdMap]: this map should contain the mapping from state ids to atom ids // [allStateGroupMaps]: this map should contain the mapping from states ids and state // values to state group ids for all states // [stateProtoHashes]: contains a map of state id to the hash of the State proto from the config // Returns nullopt if successful and InvalidConfigReason if not. optional initStates( const StatsdConfig& config, unordered_map& stateAtomIdMap, unordered_map>& allStateGroupMaps, std::map& stateProtoHashes); // Initialize MetricProducers. // input: // [key]: the config key that this config belongs to // [config]: the input config // [timeBaseSec]: start time base for all metrics // [atomMatchingTrackerMap]: AtomMatchingTracker name to index mapping from previous step. // [conditionTrackerMap]: condition name to index mapping // [stateAtomIdMap]: contains the mapping from state ids to atom ids // [allStateGroupMaps]: contains the mapping from atom ids and state values to // state group ids for all states // output: // [allMetricProducers]: contains the list of sp to the MetricProducers created. // [conditionToMetricMap]: contains the mapping from condition tracker index to // the list of MetricProducer index // [trackerToMetricMap]: contains the mapping from log tracker to MetricProducer index. // Returns nullopt if successful and InvalidConfigReason if not. optional initMetrics( const ConfigKey& key, const StatsdConfig& config, int64_t timeBaseTimeNs, const int64_t currentTimeNs, const sp& pullerManager, const std::unordered_map& atomMatchingTrackerMap, const std::unordered_map& conditionTrackerMap, const vector>& allAtomMatchingTrackers, const unordered_map& stateAtomIdMap, const unordered_map>& allStateGroupMaps, vector>& allConditionTrackers, const std::vector& initialConditionCache, std::vector>& allMetricProducers, std::unordered_map>& conditionToMetricMap, std::unordered_map>& trackerToMetricMap, std::set& noReportMetricIds, std::unordered_map>& activationAtomTrackerToMetricMap, std::unordered_map>& deactivationAtomTrackerToMetricMap, std::vector& metricsWithActivation, const wp configMetadataProvider); // Initialize alarms // Is called both on initialize new configs and config updates since alarms do not have any state. optional initAlarms(const StatsdConfig& config, const ConfigKey& key, const sp& periodicAlarmMonitor, const int64_t timeBaseNs, int64_t currentTimeNs, std::vector>& allAlarmTrackers); // Initialize MetricsManager from StatsdConfig. // Parameters are the members of MetricsManager. See MetricsManager for declaration. optional initStatsdConfig( const ConfigKey& key, const StatsdConfig& config, const sp& uidMap, const sp& pullerManager, const sp& anomalyAlarmMonitor, const sp& periodicAlarmMonitor, int64_t timeBaseNs, const int64_t currentTimeNs, const wp configMetadataProvider, std::unordered_map>& allTagIdsToMatchersMap, std::vector>& allAtomMatchingTrackers, std::unordered_map& atomMatchingTrackerMap, std::vector>& allConditionTrackers, std::unordered_map& conditionTrackerMap, std::vector>& allMetricProducers, std::unordered_map& metricProducerMap, vector>& allAnomalyTrackers, vector>& allPeriodicAlarmTrackers, std::unordered_map>& conditionToMetricMap, std::unordered_map>& trackerToMetricMap, std::unordered_map>& trackerToConditionMap, std::unordered_map>& activationAtomTrackerToMetricMap, std::unordered_map>& deactivationAtomTrackerToMetricMap, std::unordered_map& alertTrackerMap, std::vector& metricsWithActivation, std::map& stateProtoHashes, std::set& noReportMetricIds); } // namespace statsd } // namespace os } // namespace android