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 <unordered_map> 21 22 #include <android/util/ProtoOutputStream.h> 23 #include <gtest/gtest_prod.h> 24 #include "../anomaly/AnomalyTracker.h" 25 #include "../condition/ConditionTracker.h" 26 #include "../matchers/matcher_util.h" 27 #include "MetricProducer.h" 28 #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" 29 #include "stats_util.h" 30 31 namespace android { 32 namespace os { 33 namespace statsd { 34 35 struct CountBucket { 36 int64_t mBucketStartNs; 37 int64_t mBucketEndNs; 38 int64_t mCount; 39 }; 40 41 class CountMetricProducer : public MetricProducer { 42 public: 43 // TODO: Pass in the start time from MetricsManager, it should be consistent for all metrics. 44 CountMetricProducer(const ConfigKey& key, const CountMetric& countMetric, 45 const int conditionIndex, const sp<ConditionWizard>& wizard, 46 const int64_t startTimeNs); 47 48 virtual ~CountMetricProducer(); 49 50 protected: 51 void onMatchedLogEventInternalLocked( 52 const size_t matcherIndex, const MetricDimensionKey& eventKey, 53 const ConditionKey& conditionKey, bool condition, 54 const LogEvent& event) override; 55 56 private: 57 58 void onDumpReportLocked(const int64_t dumpTimeNs, 59 const bool include_current_partial_bucket, 60 std::set<string> *str_set, 61 android::util::ProtoOutputStream* protoOutput) override; 62 63 void clearPastBucketsLocked(const int64_t dumpTimeNs) override; 64 65 // Internal interface to handle condition change. 66 void onConditionChangedLocked(const bool conditionMet, const int64_t eventTime) override; 67 68 // Internal interface to handle sliced condition change. 69 void onSlicedConditionMayChangeLocked(bool overallCondition, const int64_t eventTime) override; 70 71 // Internal function to calculate the current used bytes. 72 size_t byteSizeLocked() const override; 73 74 void dumpStatesLocked(FILE* out, bool verbose) const override; 75 76 void dropDataLocked(const int64_t dropTimeNs) override; 77 78 // Util function to flush the old packet. 79 void flushIfNeededLocked(const int64_t& newEventTime) override; 80 81 void flushCurrentBucketLocked(const int64_t& eventTimeNs) override; 82 83 // TODO: Add a lock to mPastBuckets. 84 std::unordered_map<MetricDimensionKey, std::vector<CountBucket>> mPastBuckets; 85 86 // The current bucket (may be a partial bucket). 87 std::shared_ptr<DimToValMap> mCurrentSlicedCounter = std::make_shared<DimToValMap>(); 88 89 // The sum of previous partial buckets in the current full bucket (excluding the current 90 // partial bucket). This is only updated while flushing the current bucket. 91 std::shared_ptr<DimToValMap> mCurrentFullCounters = std::make_shared<DimToValMap>(); 92 93 static const size_t kBucketSize = sizeof(CountBucket{}); 94 95 bool hitGuardRailLocked(const MetricDimensionKey& newKey); 96 97 FRIEND_TEST(CountMetricProducerTest, TestNonDimensionalEvents); 98 FRIEND_TEST(CountMetricProducerTest, TestEventsWithNonSlicedCondition); 99 FRIEND_TEST(CountMetricProducerTest, TestEventsWithSlicedCondition); 100 FRIEND_TEST(CountMetricProducerTest, TestAnomalyDetectionUnSliced); 101 FRIEND_TEST(CountMetricProducerTest, TestEventWithAppUpgrade); 102 FRIEND_TEST(CountMetricProducerTest, TestEventWithAppUpgradeInNextBucket); 103 }; 104 105 } // namespace statsd 106 } // namespace os 107 } // namespace android 108 #endif // COUNT_METRIC_PRODUCER_H 109