• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, const int conditionIndex,
48             const vector<ConditionState>& initialConditionCache, const sp<ConditionWizard>& wizard,
49             const uint64_t protoHash, const int64_t timeBaseNs, const int64_t startTimeNs,
50             const std::unordered_map<int, std::shared_ptr<Activation>>& eventActivationMap = {},
51             const std::unordered_map<int, std::vector<std::shared_ptr<Activation>>>&
52                     eventDeactivationMap = {},
53             const vector<int>& slicedStateAtoms = {},
54             const unordered_map<int, unordered_map<int, int64_t>>& stateGroupMap = {});
55 
56     virtual ~CountMetricProducer();
57 
58     void onStateChanged(const int64_t eventTimeNs, const int32_t atomId,
59                         const HashableDimensionKey& primaryKey, const FieldValue& oldState,
60                         const FieldValue& newState) override;
61 
getMetricType()62     MetricType getMetricType() const override {
63         return METRIC_TYPE_COUNT;
64     }
65 
66 protected:
67     void onMatchedLogEventInternalLocked(
68             const size_t matcherIndex, const MetricDimensionKey& eventKey,
69             const ConditionKey& conditionKey, bool condition, const LogEvent& event,
70             const std::map<int, HashableDimensionKey>& statePrimaryKeys) override;
71 
72 private:
73 
74     void onDumpReportLocked(const int64_t dumpTimeNs,
75                             const bool include_current_partial_bucket,
76                             const bool erase_data,
77                             const DumpLatency dumpLatency,
78                             std::set<string> *str_set,
79                             android::util::ProtoOutputStream* protoOutput) override;
80 
81     void clearPastBucketsLocked(const int64_t dumpTimeNs) override;
82 
83     // Internal interface to handle condition change.
84     void onConditionChangedLocked(const bool conditionMet, const int64_t eventTime) override;
85 
86     // Internal interface to handle sliced condition change.
87     void onSlicedConditionMayChangeLocked(bool overallCondition, const int64_t eventTime) override;
88 
89     // Internal function to calculate the current used bytes.
90     size_t byteSizeLocked() const override;
91 
92     void dumpStatesLocked(FILE* out, bool verbose) const override;
93 
94     void dropDataLocked(const int64_t dropTimeNs) override;
95 
96     // Util function to flush the old packet.
97     void flushIfNeededLocked(const int64_t& newEventTime) override;
98 
99     void flushCurrentBucketLocked(const int64_t& eventTimeNs,
100                                   const int64_t& nextBucketStartTimeNs) override;
101 
102     void onActiveStateChangedLocked(const int64_t eventTimeNs, const bool isActive) override;
103 
104     optional<InvalidConfigReason> onConfigUpdatedLocked(
105             const StatsdConfig& config, const int configIndex, const int metricIndex,
106             const std::vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers,
107             const std::unordered_map<int64_t, int>& oldAtomMatchingTrackerMap,
108             const std::unordered_map<int64_t, int>& newAtomMatchingTrackerMap,
109             const sp<EventMatcherWizard>& matcherWizard,
110             const std::vector<sp<ConditionTracker>>& allConditionTrackers,
111             const std::unordered_map<int64_t, int>& conditionTrackerMap,
112             const sp<ConditionWizard>& wizard,
113             const std::unordered_map<int64_t, int>& metricToActivationMap,
114             std::unordered_map<int, std::vector<int>>& trackerToMetricMap,
115             std::unordered_map<int, std::vector<int>>& conditionToMetricMap,
116             std::unordered_map<int, std::vector<int>>& activationAtomTrackerToMetricMap,
117             std::unordered_map<int, std::vector<int>>& deactivationAtomTrackerToMetricMap,
118             std::vector<int>& metricsWithActivation) override;
119 
120     std::unordered_map<MetricDimensionKey, std::vector<CountBucket>> mPastBuckets;
121 
122     // The current bucket (may be a partial bucket).
123     std::shared_ptr<DimToValMap> mCurrentSlicedCounter = std::make_shared<DimToValMap>();
124 
125     // The sum of previous partial buckets in the current full bucket (excluding the current
126     // partial bucket). This is only updated while flushing the current bucket.
127     std::shared_ptr<DimToValMap> mCurrentFullCounters = std::make_shared<DimToValMap>();
128 
129     static const size_t kBucketSize = sizeof(CountBucket{});
130 
131     bool hitGuardRailLocked(const MetricDimensionKey& newKey);
132 
133     bool countPassesThreshold(const int64_t& count);
134 
135     FRIEND_TEST(CountMetricProducerTest, TestNonDimensionalEvents);
136     FRIEND_TEST(CountMetricProducerTest, TestEventsWithNonSlicedCondition);
137     FRIEND_TEST(CountMetricProducerTest, TestEventsWithSlicedCondition);
138     FRIEND_TEST(CountMetricProducerTest, TestAnomalyDetectionUnSliced);
139     FRIEND_TEST(CountMetricProducerTest, TestFirstBucket);
140     FRIEND_TEST(CountMetricProducerTest, TestOneWeekTimeUnit);
141     FRIEND_TEST(CountMetricProducerTest, TestSplitOnAppUpgradeDisabled);
142 
143     FRIEND_TEST(CountMetricProducerTest_PartialBucket, TestSplitInCurrentBucket);
144     FRIEND_TEST(CountMetricProducerTest_PartialBucket, TestSplitInNextBucket);
145 };
146 
147 }  // namespace statsd
148 }  // namespace os
149 }  // namespace android
150 #endif  // COUNT_METRIC_PRODUCER_H
151