• 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 #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, const int conditionIndex,
43             const vector<ConditionState>& initialConditionCache, const int whatIndex,
44             const int startIndex, const int stopIndex, const int stopAllIndex, const bool nesting,
45             const sp<ConditionWizard>& wizard, const uint64_t protoHash,
46             const FieldMatcher& internalDimensions, const int64_t timeBaseNs,
47             const int64_t startTimeNs,
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, const 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, const 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, const 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, const int64_t eventTime) override;
100 
101     // Internal interface to handle active state change.
102     void onActiveStateChangedLocked(const int64_t& eventTimeNs) override;
103 
104     // Internal interface to handle sliced condition change.
105     void onSlicedConditionMayChangeLocked(bool overallCondition, const int64_t eventTime) override;
106 
107     void onSlicedConditionMayChangeInternalLocked(bool overallCondition,
108                                                   const int64_t eventTimeNs);
109 
110     void onSlicedConditionMayChangeLocked_opt1(bool overallCondition, const int64_t eventTime);
111     void onSlicedConditionMayChangeLocked_opt2(bool overallCondition, const int64_t eventTime);
112 
113     // Internal function to calculate the current used bytes.
114     size_t byteSizeLocked() const override;
115 
116     void dumpStatesLocked(FILE* out, bool verbose) const override;
117 
118     void dropDataLocked(const int64_t dropTimeNs) override;
119 
120     // Util function to flush the old packet.
121     void flushIfNeededLocked(const int64_t& eventTime);
122 
123     void flushCurrentBucketLocked(const int64_t& eventTimeNs,
124                                   const int64_t& nextBucketStartTimeNs) override;
125 
126     bool onConfigUpdatedLocked(
127             const StatsdConfig& config, const int configIndex, const int metricIndex,
128             const std::vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers,
129             const std::unordered_map<int64_t, int>& oldAtomMatchingTrackerMap,
130             const std::unordered_map<int64_t, int>& newAtomMatchingTrackerMap,
131             const sp<EventMatcherWizard>& matcherWizard,
132             const std::vector<sp<ConditionTracker>>& allConditionTrackers,
133             const std::unordered_map<int64_t, int>& conditionTrackerMap,
134             const sp<ConditionWizard>& wizard,
135             const std::unordered_map<int64_t, int>& metricToActivationMap,
136             std::unordered_map<int, std::vector<int>>& trackerToMetricMap,
137             std::unordered_map<int, std::vector<int>>& conditionToMetricMap,
138             std::unordered_map<int, std::vector<int>>& activationAtomTrackerToMetricMap,
139             std::unordered_map<int, std::vector<int>>& deactivationAtomTrackerToMetricMap,
140             std::vector<int>& metricsWithActivation) override;
141 
142     void addAnomalyTrackerLocked(sp<AnomalyTracker>& anomalyTracker,
143                                  const UpdateStatus& updateStatus, const int64_t updateTimeNs);
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     // Helper function to create a duration tracker given the metric aggregation type.
178     std::unique_ptr<DurationTracker> createDurationTracker(
179             const MetricDimensionKey& eventKey) const;
180 
181     // Util function to check whether the specified dimension hits the guardrail.
182     bool hitGuardRailLocked(const MetricDimensionKey& newKey);
183 
184     static const size_t kBucketSize = sizeof(DurationBucket{});
185 
186     FRIEND_TEST(DurationMetricTrackerTest, TestNoCondition);
187     FRIEND_TEST(DurationMetricTrackerTest, TestNonSlicedCondition);
188     FRIEND_TEST(DurationMetricTrackerTest, TestNonSlicedConditionUnknownState);
189     FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicates);
190     FRIEND_TEST(DurationMetricTrackerTest, TestFirstBucket);
191 
192     FRIEND_TEST(DurationMetricProducerTest_PartialBucket, TestSumDuration);
193     FRIEND_TEST(DurationMetricProducerTest_PartialBucket,
194                 TestSumDurationWithSplitInFollowingBucket);
195     FRIEND_TEST(DurationMetricProducerTest_PartialBucket, TestMaxDuration);
196     FRIEND_TEST(DurationMetricProducerTest_PartialBucket, TestMaxDurationWithSplitInNextBucket);
197 
198     FRIEND_TEST(ConfigUpdateTest, TestUpdateDurationMetrics);
199     FRIEND_TEST(ConfigUpdateTest, TestUpdateAlerts);
200 };
201 
202 }  // namespace statsd
203 }  // namespace os
204 }  // namespace android
205