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, const bool isActive) override; 103 104 // Internal interface to handle sliced condition change. 105 void onSlicedConditionMayChangeLocked(bool overallCondition, const 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(FILE* 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(const int64_t& eventTime); 120 121 void flushCurrentBucketLocked(const int64_t& eventTimeNs, 122 const int64_t& nextBucketStartTimeNs) override; 123 124 optional<InvalidConfigReason> onConfigUpdatedLocked( 125 const StatsdConfig& config, const int configIndex, const int metricIndex, 126 const std::vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers, 127 const std::unordered_map<int64_t, int>& oldAtomMatchingTrackerMap, 128 const std::unordered_map<int64_t, int>& newAtomMatchingTrackerMap, 129 const sp<EventMatcherWizard>& matcherWizard, 130 const std::vector<sp<ConditionTracker>>& allConditionTrackers, 131 const std::unordered_map<int64_t, int>& conditionTrackerMap, 132 const sp<ConditionWizard>& wizard, 133 const std::unordered_map<int64_t, int>& metricToActivationMap, 134 std::unordered_map<int, std::vector<int>>& trackerToMetricMap, 135 std::unordered_map<int, std::vector<int>>& conditionToMetricMap, 136 std::unordered_map<int, std::vector<int>>& activationAtomTrackerToMetricMap, 137 std::unordered_map<int, std::vector<int>>& deactivationAtomTrackerToMetricMap, 138 std::vector<int>& metricsWithActivation) override; 139 140 void addAnomalyTrackerLocked(sp<AnomalyTracker>& anomalyTracker, 141 const UpdateStatus& updateStatus, const int64_t updateTimeNs); 142 143 const DurationMetric_AggregationType mAggregationType; 144 145 // Index of the SimpleAtomMatcher which defines the start. 146 int mStartIndex; 147 148 // Index of the SimpleAtomMatcher which defines the stop. 149 int mStopIndex; 150 151 // Index of the SimpleAtomMatcher which defines the stop all for all dimensions. 152 int mStopAllIndex; 153 154 // nest counting -- for the same key, stops must match the number of starts to make real stop 155 const bool mNested; 156 157 // The dimension from the atom predicate. e.g., uid, wakelock name. 158 vector<Matcher> mInternalDimensions; 159 160 bool mContainANYPositionInInternalDimensions; 161 162 // This boolean is true iff When mInternalDimensions == mDimensionsInWhat 163 bool mUseWhatDimensionAsInternalDimension; 164 165 // Caches the current unsliced part condition. 166 ConditionState mUnSlicedPartCondition; 167 168 // Save the past buckets and we can clear when the StatsLogReport is dumped. 169 std::unordered_map<MetricDimensionKey, std::vector<DurationBucket>> mPastBuckets; 170 171 // The duration trackers in the current bucket. 172 std::unordered_map<HashableDimensionKey, std::unique_ptr<DurationTracker>> 173 mCurrentSlicedDurationTrackerMap; 174 175 // Helper function to create a duration tracker given the metric aggregation type. 176 std::unique_ptr<DurationTracker> createDurationTracker( 177 const MetricDimensionKey& eventKey) const; 178 179 // Util function to check whether the specified dimension hits the guardrail. 180 bool hitGuardRailLocked(const MetricDimensionKey& newKey); 181 182 static const size_t kBucketSize = sizeof(DurationBucket{}); 183 184 FRIEND_TEST(DurationMetricTrackerTest, TestNoCondition); 185 FRIEND_TEST(DurationMetricTrackerTest, TestNonSlicedCondition); 186 FRIEND_TEST(DurationMetricTrackerTest, TestNonSlicedConditionUnknownState); 187 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicates); 188 FRIEND_TEST(DurationMetricTrackerTest, TestFirstBucket); 189 190 FRIEND_TEST(DurationMetricProducerTest, TestSumDurationAppUpgradeSplitDisabled); 191 FRIEND_TEST(DurationMetricProducerTest_PartialBucket, TestSumDuration); 192 FRIEND_TEST(DurationMetricProducerTest_PartialBucket, 193 TestSumDurationWithSplitInFollowingBucket); 194 FRIEND_TEST(DurationMetricProducerTest_PartialBucket, TestMaxDuration); 195 FRIEND_TEST(DurationMetricProducerTest_PartialBucket, TestMaxDurationWithSplitInNextBucket); 196 197 FRIEND_TEST(ConfigUpdateTest, TestUpdateDurationMetrics); 198 FRIEND_TEST(ConfigUpdateTest, TestUpdateAlerts); 199 }; 200 201 } // namespace statsd 202 } // namespace os 203 } // namespace android 204