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/ConditionTimer.h"
28 #include "condition/ConditionTracker.h"
29 #include "matchers/matcher_util.h"
30 #include "src/statsd_config.pb.h"
31 #include "stats_util.h"
32 
33 namespace android {
34 namespace os {
35 namespace statsd {
36 
37 struct CountBucket {
38     int64_t mBucketStartNs;
39     int64_t mBucketEndNs;
40     int64_t mCount;
41     int64_t mConditionTrueNs;
42 };
43 
44 class CountMetricProducer : public MetricProducer {
45 public:
46     CountMetricProducer(
47             const ConfigKey& key, const CountMetric& countMetric, int conditionIndex,
48             const vector<ConditionState>& initialConditionCache, const sp<ConditionWizard>& wizard,
49             const uint64_t protoHash, int64_t timeBaseNs, int64_t startTimeNs,
50             const wp<ConfigMetadataProvider> configMetadataProvider,
51             const std::unordered_map<int, std::shared_ptr<Activation>>& eventActivationMap = {},
52             const std::unordered_map<int, std::vector<std::shared_ptr<Activation>>>&
53                     eventDeactivationMap = {},
54             const vector<int>& slicedStateAtoms = {},
55             const unordered_map<int, unordered_map<int, int64_t>>& stateGroupMap = {});
56 
57     virtual ~CountMetricProducer();
58 
59     void onStateChanged(const int64_t eventTimeNs, const int32_t atomId,
60                         const HashableDimensionKey& primaryKey, const FieldValue& oldState,
61                         const FieldValue& newState) override;
62 
getMetricType()63     MetricType getMetricType() const override {
64         return METRIC_TYPE_COUNT;
65     }
66 
67 protected:
68     void onMatchedLogEventInternalLocked(
69             const size_t matcherIndex, const MetricDimensionKey& eventKey,
70             const ConditionKey& conditionKey, bool condition, const LogEvent& event,
71             const std::map<int, HashableDimensionKey>& statePrimaryKeys) override;
72 
73 private:
74 
75     void onDumpReportLocked(const int64_t dumpTimeNs,
76                             const bool include_current_partial_bucket,
77                             const bool erase_data,
78                             const DumpLatency dumpLatency,
79                             std::set<string> *str_set,
80                             android::util::ProtoOutputStream* protoOutput) override;
81 
82     void clearPastBucketsLocked(const int64_t dumpTimeNs) override;
83 
84     // Internal interface to handle condition change.
85     void onConditionChangedLocked(const bool conditionMet, int64_t eventTime) override;
86 
87     // Internal interface to handle sliced condition change.
88     void onSlicedConditionMayChangeLocked(bool overallCondition, int64_t eventTime) override;
89 
90     // Internal function to calculate the current used bytes.
91     size_t byteSizeLocked() const override;
92 
93     void dumpStatesLocked(int out, bool verbose) const override;
94 
95     void dropDataLocked(const int64_t dropTimeNs) override;
96 
97     // Util function to flush the old packet.
98     void flushIfNeededLocked(int64_t newEventTime) override;
99 
100     void flushCurrentBucketLocked(int64_t eventTimeNs, int64_t nextBucketStartTimeNs) override;
101 
102     void onActiveStateChangedLocked(const int64_t eventTimeNs, const bool isActive) override;
103 
104     size_t computeBucketSizeLocked(const bool isFullBucket, const MetricDimensionKey& dimKey,
105                                    const bool isFirstBucket) const override;
106 
107     optional<InvalidConfigReason> onConfigUpdatedLocked(
108             const StatsdConfig& config, int configIndex, int metricIndex,
109             const std::vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers,
110             const std::unordered_map<int64_t, int>& oldAtomMatchingTrackerMap,
111             const std::unordered_map<int64_t, int>& newAtomMatchingTrackerMap,
112             const sp<EventMatcherWizard>& matcherWizard,
113             const std::vector<sp<ConditionTracker>>& allConditionTrackers,
114             const std::unordered_map<int64_t, int>& conditionTrackerMap,
115             const sp<ConditionWizard>& wizard,
116             const std::unordered_map<int64_t, int>& metricToActivationMap,
117             std::unordered_map<int, std::vector<int>>& trackerToMetricMap,
118             std::unordered_map<int, std::vector<int>>& conditionToMetricMap,
119             std::unordered_map<int, std::vector<int>>& activationAtomTrackerToMetricMap,
120             std::unordered_map<int, std::vector<int>>& deactivationAtomTrackerToMetricMap,
121             std::vector<int>& metricsWithActivation) override;
122 
123     std::unordered_map<MetricDimensionKey, std::vector<CountBucket>> mPastBuckets;
124 
125     // The current bucket (may be a partial bucket).
126     std::shared_ptr<DimToValMap> mCurrentSlicedCounter = std::make_shared<DimToValMap>();
127 
128     // The sum of previous partial buckets in the current full bucket (excluding the current
129     // partial bucket). This is only updated while flushing the current bucket.
130     std::shared_ptr<DimToValMap> mCurrentFullCounters = std::make_shared<DimToValMap>();
131 
132     static const size_t kBucketSize = sizeof(CountBucket{});
133 
134     bool hitGuardRailLocked(const MetricDimensionKey& newKey);
135 
136     bool countPassesThreshold(int64_t count);
137 
138     // Tracks if the dimension guardrail has been hit in the current report.
139     bool mDimensionGuardrailHit;
140 
141     const size_t mDimensionHardLimit;
142 
143     FRIEND_TEST(CountMetricProducerTest, TestNonDimensionalEvents);
144     FRIEND_TEST(CountMetricProducerTest, TestEventsWithNonSlicedCondition);
145     FRIEND_TEST(CountMetricProducerTest, TestEventsWithSlicedCondition);
146     FRIEND_TEST(CountMetricProducerTest, TestAnomalyDetectionUnSliced);
147     FRIEND_TEST(CountMetricProducerTest, TestFirstBucket);
148     FRIEND_TEST(CountMetricProducerTest, TestOneWeekTimeUnit);
149     FRIEND_TEST(CountMetricProducerTest, TestSplitOnAppUpgradeDisabled);
150 
151     FRIEND_TEST(CountMetricProducerTest_PartialBucket, TestSplitInCurrentBucket);
152     FRIEND_TEST(CountMetricProducerTest_PartialBucket, TestSplitInNextBucket);
153 
154     FRIEND_TEST(MetricsManagerUtilDimLimitTest, TestDimLimit);
155 
156     FRIEND_TEST(ConfigUpdateDimLimitTest, TestDimLimit);
157 };
158 
159 }  // namespace statsd
160 }  // namespace os
161 }  // namespace android
162 #endif  // COUNT_METRIC_PRODUCER_H
163