1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef COUNT_METRIC_PRODUCER_H 18 #define COUNT_METRIC_PRODUCER_H 19 20 #include <android/util/ProtoOutputStream.h> 21 #include <gtest/gtest_prod.h> 22 23 #include <unordered_map> 24 25 #include "MetricProducer.h" 26 #include "anomaly/AnomalyTracker.h" 27 #include "condition/ConditionTracker.h" 28 #include "src/statsd_config.pb.h" 29 #include "matchers/matcher_util.h" 30 #include "stats_util.h" 31 32 namespace android { 33 namespace os { 34 namespace statsd { 35 36 struct CountBucket { 37 int64_t mBucketStartNs; 38 int64_t mBucketEndNs; 39 int64_t mCount; 40 }; 41 42 class CountMetricProducer : public MetricProducer { 43 public: 44 CountMetricProducer( 45 const ConfigKey& key, const CountMetric& countMetric, const int conditionIndex, 46 const vector<ConditionState>& initialConditionCache, const sp<ConditionWizard>& wizard, 47 const uint64_t protoHash, const int64_t timeBaseNs, const int64_t startTimeNs, 48 const std::unordered_map<int, std::shared_ptr<Activation>>& eventActivationMap = {}, 49 const std::unordered_map<int, std::vector<std::shared_ptr<Activation>>>& 50 eventDeactivationMap = {}, 51 const vector<int>& slicedStateAtoms = {}, 52 const unordered_map<int, unordered_map<int, int64_t>>& stateGroupMap = {}); 53 54 virtual ~CountMetricProducer(); 55 56 void onStateChanged(const int64_t eventTimeNs, const int32_t atomId, 57 const HashableDimensionKey& primaryKey, const FieldValue& oldState, 58 const FieldValue& newState) override; 59 getMetricType()60 MetricType getMetricType() const override { 61 return METRIC_TYPE_COUNT; 62 } 63 64 protected: 65 void onMatchedLogEventInternalLocked( 66 const size_t matcherIndex, const MetricDimensionKey& eventKey, 67 const ConditionKey& conditionKey, bool condition, const LogEvent& event, 68 const std::map<int, HashableDimensionKey>& statePrimaryKeys) override; 69 70 private: 71 72 void onDumpReportLocked(const int64_t dumpTimeNs, 73 const bool include_current_partial_bucket, 74 const bool erase_data, 75 const DumpLatency dumpLatency, 76 std::set<string> *str_set, 77 android::util::ProtoOutputStream* protoOutput) override; 78 79 void clearPastBucketsLocked(const int64_t dumpTimeNs) override; 80 81 // Internal interface to handle condition change. 82 void onConditionChangedLocked(const bool conditionMet, const int64_t eventTime) override; 83 84 // Internal interface to handle sliced condition change. 85 void onSlicedConditionMayChangeLocked(bool overallCondition, const int64_t eventTime) override; 86 87 // Internal function to calculate the current used bytes. 88 size_t byteSizeLocked() const override; 89 90 void dumpStatesLocked(FILE* out, bool verbose) const override; 91 92 void dropDataLocked(const int64_t dropTimeNs) override; 93 94 // Util function to flush the old packet. 95 void flushIfNeededLocked(const int64_t& newEventTime) override; 96 97 void flushCurrentBucketLocked(const int64_t& eventTimeNs, 98 const int64_t& nextBucketStartTimeNs) override; 99 100 bool onConfigUpdatedLocked( 101 const StatsdConfig& config, const int configIndex, const int metricIndex, 102 const std::vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers, 103 const std::unordered_map<int64_t, int>& oldAtomMatchingTrackerMap, 104 const std::unordered_map<int64_t, int>& newAtomMatchingTrackerMap, 105 const sp<EventMatcherWizard>& matcherWizard, 106 const std::vector<sp<ConditionTracker>>& allConditionTrackers, 107 const std::unordered_map<int64_t, int>& conditionTrackerMap, 108 const sp<ConditionWizard>& wizard, 109 const std::unordered_map<int64_t, int>& metricToActivationMap, 110 std::unordered_map<int, std::vector<int>>& trackerToMetricMap, 111 std::unordered_map<int, std::vector<int>>& conditionToMetricMap, 112 std::unordered_map<int, std::vector<int>>& activationAtomTrackerToMetricMap, 113 std::unordered_map<int, std::vector<int>>& deactivationAtomTrackerToMetricMap, 114 std::vector<int>& metricsWithActivation) override; 115 116 std::unordered_map<MetricDimensionKey, std::vector<CountBucket>> mPastBuckets; 117 118 // The current bucket (may be a partial bucket). 119 std::shared_ptr<DimToValMap> mCurrentSlicedCounter = std::make_shared<DimToValMap>(); 120 121 // The sum of previous partial buckets in the current full bucket (excluding the current 122 // partial bucket). This is only updated while flushing the current bucket. 123 std::shared_ptr<DimToValMap> mCurrentFullCounters = std::make_shared<DimToValMap>(); 124 125 static const size_t kBucketSize = sizeof(CountBucket{}); 126 127 bool hitGuardRailLocked(const MetricDimensionKey& newKey); 128 129 bool countPassesThreshold(const int64_t& count); 130 131 FRIEND_TEST(CountMetricProducerTest, TestNonDimensionalEvents); 132 FRIEND_TEST(CountMetricProducerTest, TestEventsWithNonSlicedCondition); 133 FRIEND_TEST(CountMetricProducerTest, TestEventsWithSlicedCondition); 134 FRIEND_TEST(CountMetricProducerTest, TestAnomalyDetectionUnSliced); 135 FRIEND_TEST(CountMetricProducerTest, TestFirstBucket); 136 FRIEND_TEST(CountMetricProducerTest, TestOneWeekTimeUnit); 137 138 FRIEND_TEST(CountMetricProducerTest_PartialBucket, TestSplitInCurrentBucket); 139 FRIEND_TEST(CountMetricProducerTest_PartialBucket, TestSplitInNextBucket); 140 }; 141 142 } // namespace statsd 143 } // namespace os 144 } // namespace android 145 #endif // COUNT_METRIC_PRODUCER_H 146