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 #pragma once
18 
19 #include <android/util/ProtoOutputStream.h>
20 
21 #include <unordered_map>
22 
23 #include "../anomaly/DurationAnomalyTracker.h"
24 #include "../condition/ConditionTracker.h"
25 #include "../matchers/matcher_util.h"
26 #include "MetricProducer.h"
27 #include "duration_helper/DurationTracker.h"
28 #include "duration_helper/MaxDurationTracker.h"
29 #include "duration_helper/OringDurationTracker.h"
30 #include "src/statsd_config.pb.h"
31 #include "stats_util.h"
32 
33 using namespace std;
34 
35 namespace android {
36 namespace os {
37 namespace statsd {
38 
39 class DurationMetricProducer : public MetricProducer {
40 public:
41     DurationMetricProducer(
42             const ConfigKey& key, const DurationMetric& durationMetric, int conditionIndex,
43             const vector<ConditionState>& initialConditionCache, int whatIndex,
44             const int startIndex, int stopIndex, int stopAllIndex, const bool nesting,
45             const sp<ConditionWizard>& wizard, const uint64_t protoHash,
46             const FieldMatcher& internalDimensions, int64_t timeBaseNs, const int64_t startTimeNs,
47             const wp<ConfigMetadataProvider> configMetadataProvider,
48             const unordered_map<int, shared_ptr<Activation>>& eventActivationMap = {},
49             const unordered_map<int, vector<shared_ptr<Activation>>>& eventDeactivationMap = {},
50             const vector<int>& slicedStateAtoms = {},
51             const unordered_map<int, unordered_map<int, int64_t>>& stateGroupMap = {});
52 
53     virtual ~DurationMetricProducer();
54 
55     sp<AnomalyTracker> addAnomalyTracker(const Alert& alert,
56                                          const sp<AlarmMonitor>& anomalyAlarmMonitor,
57                                          const UpdateStatus& updateStatus,
58                                          const int64_t updateTimeNs) override;
59 
60     void addAnomalyTracker(sp<AnomalyTracker>& anomalyTracker, int64_t updateTimeNs) override;
61 
62     void onStateChanged(const int64_t eventTimeNs, const int32_t atomId,
63                         const HashableDimensionKey& primaryKey, const FieldValue& oldState,
64                         const FieldValue& newState) override;
65 
getMetricType()66     MetricType getMetricType() const override {
67         return METRIC_TYPE_DURATION;
68     }
69 
70 protected:
71     void onMatchedLogEventLocked(const size_t matcherIndex, const LogEvent& event) override;
72 
73     void onMatchedLogEventInternalLocked(
74             const size_t matcherIndex, const MetricDimensionKey& eventKey,
75             const ConditionKey& conditionKeys, bool condition, const LogEvent& event,
76             const std::map<int, HashableDimensionKey>& statePrimaryKeys) override;
77 
78 private:
79     // Initializes true dimensions of the 'what' predicate. Only to be called during initialization.
80     void initTrueDimensions(const int whatIndex, int64_t startTimeNs);
81 
82     void handleMatchedLogEventValuesLocked(const size_t matcherIndex,
83                                            const std::vector<FieldValue>& values,
84                                            const int64_t eventTimeNs);
85     void handleStartEvent(const MetricDimensionKey& eventKey, const ConditionKey& conditionKeys,
86                           bool condition, int64_t eventTimeNs,
87                           const vector<FieldValue>& eventValues);
88 
89     void onDumpReportLocked(const int64_t dumpTimeNs,
90                             const bool include_current_partial_bucket,
91                             const bool erase_data,
92                             const DumpLatency dumpLatency,
93                             std::set<string> *str_set,
94                             android::util::ProtoOutputStream* protoOutput) override;
95 
96     void clearPastBucketsLocked(const int64_t dumpTimeNs) override;
97 
98     // Internal interface to handle condition change.
99     void onConditionChangedLocked(const bool conditionMet, int64_t eventTime) override;
100 
101     // Internal interface to handle active state change.
102     void onActiveStateChangedLocked(const int64_t eventTimeNs, const bool isActive) override;
103 
104     // Internal interface to handle sliced condition change.
105     void onSlicedConditionMayChangeLocked(bool overallCondition, int64_t eventTime) override;
106 
107     void onSlicedConditionMayChangeInternalLocked(const int64_t eventTimeNs);
108 
109     void onSlicedConditionMayChangeLocked_opt1(const int64_t eventTime);
110 
111     // Internal function to calculate the current used bytes.
112     size_t byteSizeLocked() const override;
113 
114     void dumpStatesLocked(int out, bool verbose) const override;
115 
116     void dropDataLocked(const int64_t dropTimeNs) override;
117 
118     // Util function to flush the old packet.
119     void flushIfNeededLocked(int64_t eventTime);
120 
121     void flushCurrentBucketLocked(int64_t eventTimeNs, int64_t nextBucketStartTimeNs) override;
122 
123     optional<InvalidConfigReason> onConfigUpdatedLocked(
124             const StatsdConfig& config, int configIndex, int metricIndex,
125             const std::vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers,
126             const std::unordered_map<int64_t, int>& oldAtomMatchingTrackerMap,
127             const std::unordered_map<int64_t, int>& newAtomMatchingTrackerMap,
128             const sp<EventMatcherWizard>& matcherWizard,
129             const std::vector<sp<ConditionTracker>>& allConditionTrackers,
130             const std::unordered_map<int64_t, int>& conditionTrackerMap,
131             const sp<ConditionWizard>& wizard,
132             const std::unordered_map<int64_t, int>& metricToActivationMap,
133             std::unordered_map<int, std::vector<int>>& trackerToMetricMap,
134             std::unordered_map<int, std::vector<int>>& conditionToMetricMap,
135             std::unordered_map<int, std::vector<int>>& activationAtomTrackerToMetricMap,
136             std::unordered_map<int, std::vector<int>>& deactivationAtomTrackerToMetricMap,
137             std::vector<int>& metricsWithActivation) override;
138 
139     void addAnomalyTrackerLocked(sp<AnomalyTracker>& anomalyTracker,
140                                  const UpdateStatus& updateStatus, int64_t updateTimeNs);
141 
142     size_t computeBucketSizeLocked(const bool isFullBucket, const MetricDimensionKey& dimKey,
143                                    const bool isFirstBucket) const override;
144 
145     const DurationMetric_AggregationType mAggregationType;
146 
147     // Index of the SimpleAtomMatcher which defines the start.
148     int mStartIndex;
149 
150     // Index of the SimpleAtomMatcher which defines the stop.
151     int mStopIndex;
152 
153     // Index of the SimpleAtomMatcher which defines the stop all for all dimensions.
154     int mStopAllIndex;
155 
156     // nest counting -- for the same key, stops must match the number of starts to make real stop
157     const bool mNested;
158 
159     // The dimension from the atom predicate. e.g., uid, wakelock name.
160     vector<Matcher> mInternalDimensions;
161 
162     bool mContainANYPositionInInternalDimensions;
163 
164     // This boolean is true iff When mInternalDimensions == mDimensionsInWhat
165     bool mUseWhatDimensionAsInternalDimension;
166 
167     // Caches the current unsliced part condition.
168     ConditionState mUnSlicedPartCondition;
169 
170     // Save the past buckets and we can clear when the StatsLogReport is dumped.
171     std::unordered_map<MetricDimensionKey, std::vector<DurationBucket>> mPastBuckets;
172 
173     // The duration trackers in the current bucket.
174     std::unordered_map<HashableDimensionKey, std::unique_ptr<DurationTracker>>
175             mCurrentSlicedDurationTrackerMap;
176 
177     const size_t mDimensionHardLimit;
178 
179     // Helper function to create a duration tracker given the metric aggregation type.
180     std::unique_ptr<DurationTracker> createDurationTracker(
181             const MetricDimensionKey& eventKey) const;
182 
183     // Util function to check whether the specified dimension hits the guardrail.
184     bool hitGuardRailLocked(const MetricDimensionKey& newKey) const;
185 
186     static const size_t kBucketSize = sizeof(DurationBucket{});
187 
188     FRIEND_TEST(DurationMetricTrackerTest, TestNoCondition);
189     FRIEND_TEST(DurationMetricTrackerTest, TestNonSlicedCondition);
190     FRIEND_TEST(DurationMetricTrackerTest, TestNonSlicedConditionUnknownState);
191     FRIEND_TEST(DurationMetricTrackerTest, TestFirstBucket);
192 
193     FRIEND_TEST(DurationMetricProducerTest, TestSumDurationAppUpgradeSplitDisabled);
194     FRIEND_TEST(DurationMetricProducerTest, TestClearCurrentSlicedTrackerMapWhenStop);
195     FRIEND_TEST(DurationMetricProducerTest_PartialBucket, TestSumDuration);
196     FRIEND_TEST(DurationMetricProducerTest_PartialBucket,
197                 TestSumDurationWithSplitInFollowingBucket);
198     FRIEND_TEST(DurationMetricProducerTest_PartialBucket, TestMaxDuration);
199     FRIEND_TEST(DurationMetricProducerTest_PartialBucket, TestMaxDurationWithSplitInNextBucket);
200 
201     FRIEND_TEST(ConfigUpdateTest, TestUpdateDurationMetrics);
202     FRIEND_TEST(ConfigUpdateTest, TestUpdateAlerts);
203 
204     FRIEND_TEST(MetricsManagerUtilDimLimitTest, TestDimLimit);
205 
206     FRIEND_TEST(ConfigUpdateDimLimitTest, TestDimLimit);
207 };
208 
209 }  // namespace statsd
210 }  // namespace os
211 }  // namespace android
212