• 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 
20 #include <unordered_map>
21 
22 #include <android/util/ProtoOutputStream.h>
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 "frameworks/base/cmds/statsd/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(const ConfigKey& key, const DurationMetric& durationMetric,
42                            const int conditionIndex, const size_t startIndex,
43                            const size_t stopIndex, const size_t stopAllIndex, const bool nesting,
44                            const sp<ConditionWizard>& wizard,
45                            const FieldMatcher& internalDimensions, const int64_t startTimeNs);
46 
47     virtual ~DurationMetricProducer();
48 
49     sp<AnomalyTracker> addAnomalyTracker(const Alert &alert,
50                                          const sp<AlarmMonitor>& anomalyAlarmMonitor) override;
51 
52 protected:
53     void onMatchedLogEventLocked(const size_t matcherIndex, const LogEvent& event) override;
54 
55     void onMatchedLogEventInternalLocked(
56             const size_t matcherIndex, const MetricDimensionKey& eventKey,
57             const ConditionKey& conditionKeys, bool condition,
58             const LogEvent& event) override;
59 
60 private:
61     void handleStartEvent(const MetricDimensionKey& eventKey, const ConditionKey& conditionKeys,
62                           bool condition, const LogEvent& event);
63 
64     void onDumpReportLocked(const int64_t dumpTimeNs,
65                             const bool include_current_partial_bucket,
66                             std::set<string> *str_set,
67                             android::util::ProtoOutputStream* protoOutput) override;
68 
69     void clearPastBucketsLocked(const int64_t dumpTimeNs) override;
70 
71     // Internal interface to handle condition change.
72     void onConditionChangedLocked(const bool conditionMet, const int64_t eventTime) override;
73 
74     // Internal interface to handle sliced condition change.
75     void onSlicedConditionMayChangeLocked(bool overallCondition, const int64_t eventTime) override;
76 
77     void onSlicedConditionMayChangeLocked_opt1(bool overallCondition, const int64_t eventTime);
78     void onSlicedConditionMayChangeLocked_opt2(bool overallCondition, const int64_t eventTime);
79 
80     // Internal function to calculate the current used bytes.
81     size_t byteSizeLocked() const override;
82 
83     void dumpStatesLocked(FILE* out, bool verbose) const override;
84 
85     void dropDataLocked(const int64_t dropTimeNs) override;
86 
87     // Util function to flush the old packet.
88     void flushIfNeededLocked(const int64_t& eventTime);
89 
90     void flushCurrentBucketLocked(const int64_t& eventTimeNs) override;
91 
92     const DurationMetric_AggregationType mAggregationType;
93 
94     // Index of the SimpleAtomMatcher which defines the start.
95     const size_t mStartIndex;
96 
97     // Index of the SimpleAtomMatcher which defines the stop.
98     const size_t mStopIndex;
99 
100     // Index of the SimpleAtomMatcher which defines the stop all for all dimensions.
101     const size_t mStopAllIndex;
102 
103     // nest counting -- for the same key, stops must match the number of starts to make real stop
104     const bool mNested;
105 
106     // The dimension from the atom predicate. e.g., uid, wakelock name.
107     vector<Matcher> mInternalDimensions;
108 
109     bool mContainANYPositionInInternalDimensions;
110 
111     // This boolean is true iff When mInternalDimensions == mDimensionsInWhat
112     bool mUseWhatDimensionAsInternalDimension;
113 
114     // Caches the current unsliced part condition.
115     ConditionState mUnSlicedPartCondition;
116 
117     // Save the past buckets and we can clear when the StatsLogReport is dumped.
118     // TODO: Add a lock to mPastBuckets.
119     std::unordered_map<MetricDimensionKey, std::vector<DurationBucket>> mPastBuckets;
120 
121     // The duration trackers in the current bucket.
122     std::unordered_map<HashableDimensionKey,
123         std::unordered_map<HashableDimensionKey, std::unique_ptr<DurationTracker>>>
124             mCurrentSlicedDurationTrackerMap;
125 
126     // Helper function to create a duration tracker given the metric aggregation type.
127     std::unique_ptr<DurationTracker> createDurationTracker(
128         const MetricDimensionKey& eventKey) const;
129 
130     // This hides the base class's std::vector<sp<AnomalyTracker>> mAnomalyTrackers
131     std::vector<sp<DurationAnomalyTracker>> mAnomalyTrackers;
132 
133     // Util function to check whether the specified dimension hits the guardrail.
134     bool hitGuardRailLocked(const MetricDimensionKey& newKey);
135 
136     static const size_t kBucketSize = sizeof(DurationBucket{});
137 
138     FRIEND_TEST(DurationMetricTrackerTest, TestNoCondition);
139     FRIEND_TEST(DurationMetricTrackerTest, TestNonSlicedCondition);
140     FRIEND_TEST(DurationMetricTrackerTest, TestSumDurationWithUpgrade);
141     FRIEND_TEST(DurationMetricTrackerTest, TestSumDurationWithUpgradeInFollowingBucket);
142     FRIEND_TEST(DurationMetricTrackerTest, TestMaxDurationWithUpgrade);
143     FRIEND_TEST(DurationMetricTrackerTest, TestMaxDurationWithUpgradeInNextBucket);
144     FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicates);
145 };
146 
147 }  // namespace statsd
148 }  // namespace os
149 }  // namespace android
150