Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 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 | |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 20 | #include <android/util/ProtoOutputStream.h> |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 21 | #include <gtest/gtest_prod.h> |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 22 | |
| 23 | #include <unordered_map> |
| 24 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 25 | #include "MetricProducer.h" |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 26 | #include "anomaly/AnomalyTracker.h" |
| 27 | #include "condition/ConditionTracker.h" |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 28 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 29 | #include "matchers/matcher_util.h" |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 30 | #include "stats_util.h" |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | namespace os { |
| 34 | namespace statsd { |
| 35 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 36 | struct CountBucket { |
| 37 | int64_t mBucketStartNs; |
| 38 | int64_t mBucketEndNs; |
| 39 | int64_t mCount; |
| 40 | }; |
| 41 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 42 | class CountMetricProducer : public MetricProducer { |
| 43 | public: |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 44 | CountMetricProducer( |
| 45 | const ConfigKey& key, const CountMetric& countMetric, const int conditionIndex, |
tsaichristine | 6e2e92d | 2020-05-18 14:39:45 -0700 | [diff] [blame^] | 46 | const vector<ConditionState>& initialConditionCache, const sp<ConditionWizard>& wizard, |
| 47 | const int64_t timeBaseNs, const int64_t startTimeNs, |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 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 = {}); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 53 | |
| 54 | virtual ~CountMetricProducer(); |
| 55 | |
tsaichristine | 8d73dc9 | 2019-12-06 02:11:02 -0800 | [diff] [blame] | 56 | void onStateChanged(const int64_t eventTimeNs, const int32_t atomId, |
tsaichristine | 9f95105 | 2020-05-13 14:32:37 -0700 | [diff] [blame] | 57 | const HashableDimensionKey& primaryKey, const FieldValue& oldState, |
| 58 | const FieldValue& newState) override; |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 59 | |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 60 | protected: |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 61 | void onMatchedLogEventInternalLocked( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 62 | const size_t matcherIndex, const MetricDimensionKey& eventKey, |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 63 | const ConditionKey& conditionKey, bool condition, const LogEvent& event, |
| 64 | const std::map<int, HashableDimensionKey>& statePrimaryKeys) override; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 65 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 66 | private: |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 67 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 68 | void onDumpReportLocked(const int64_t dumpTimeNs, |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 69 | const bool include_current_partial_bucket, |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 70 | const bool erase_data, |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 71 | const DumpLatency dumpLatency, |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 72 | std::set<string> *str_set, |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 73 | android::util::ProtoOutputStream* protoOutput) override; |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 74 | |
Yangster-mac | a802d73 | 2018-04-24 07:50:38 -0700 | [diff] [blame] | 75 | void clearPastBucketsLocked(const int64_t dumpTimeNs) override; |
| 76 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 77 | // Internal interface to handle condition change. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 78 | void onConditionChangedLocked(const bool conditionMet, const int64_t eventTime) override; |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 79 | |
| 80 | // Internal interface to handle sliced condition change. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 81 | void onSlicedConditionMayChangeLocked(bool overallCondition, const int64_t eventTime) override; |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 82 | |
| 83 | // Internal function to calculate the current used bytes. |
| 84 | size_t byteSizeLocked() const override; |
| 85 | |
Yangster-mac | a78d008 | 2018-03-12 12:02:56 -0700 | [diff] [blame] | 86 | void dumpStatesLocked(FILE* out, bool verbose) const override; |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 87 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 88 | void dropDataLocked(const int64_t dropTimeNs) override; |
Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 89 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 90 | // Util function to flush the old packet. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 91 | void flushIfNeededLocked(const int64_t& newEventTime) override; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 92 | |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 93 | void flushCurrentBucketLocked(const int64_t& eventTimeNs, |
| 94 | const int64_t& nextBucketStartTimeNs) override; |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 95 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 96 | std::unordered_map<MetricDimensionKey, std::vector<CountBucket>> mPastBuckets; |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 97 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 98 | // The current bucket (may be a partial bucket). |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 99 | std::shared_ptr<DimToValMap> mCurrentSlicedCounter = std::make_shared<DimToValMap>(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 100 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 101 | // The sum of previous partial buckets in the current full bucket (excluding the current |
| 102 | // partial bucket). This is only updated while flushing the current bucket. |
| 103 | std::shared_ptr<DimToValMap> mCurrentFullCounters = std::make_shared<DimToValMap>(); |
| 104 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 105 | static const size_t kBucketSize = sizeof(CountBucket{}); |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 106 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 107 | bool hitGuardRailLocked(const MetricDimensionKey& newKey); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 108 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 109 | FRIEND_TEST(CountMetricProducerTest, TestNonDimensionalEvents); |
| 110 | FRIEND_TEST(CountMetricProducerTest, TestEventsWithNonSlicedCondition); |
| 111 | FRIEND_TEST(CountMetricProducerTest, TestEventsWithSlicedCondition); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 112 | FRIEND_TEST(CountMetricProducerTest, TestAnomalyDetectionUnSliced); |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 113 | FRIEND_TEST(CountMetricProducerTest, TestFirstBucket); |
Ruchir Rastogi | 060fbfc | 2019-10-11 13:16:51 -0700 | [diff] [blame] | 114 | FRIEND_TEST(CountMetricProducerTest, TestOneWeekTimeUnit); |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 115 | |
| 116 | FRIEND_TEST(CountMetricProducerTest_PartialBucket, TestSplitInCurrentBucket); |
| 117 | FRIEND_TEST(CountMetricProducerTest_PartialBucket, TestSplitInNextBucket); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | } // namespace statsd |
| 121 | } // namespace os |
| 122 | } // namespace android |
| 123 | #endif // COUNT_METRIC_PRODUCER_H |