Home | History | Annotate | Download | only in metrics
      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 timeBaseNs, 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                             const bool erase_data,
     67                             const DumpLatency dumpLatency,
     68                             std::set<string> *str_set,
     69                             android::util::ProtoOutputStream* protoOutput) override;
     70 
     71     void clearPastBucketsLocked(const int64_t dumpTimeNs) override;
     72 
     73     // Internal interface to handle condition change.
     74     void onConditionChangedLocked(const bool conditionMet, const int64_t eventTime) override;
     75 
     76     // Internal interface to handle active state change.
     77     void onActiveStateChangedLocked(const int64_t& eventTimeNs) override;
     78 
     79     // Internal interface to handle sliced condition change.
     80     void onSlicedConditionMayChangeLocked(bool overallCondition, const int64_t eventTime) override;
     81 
     82     void onSlicedConditionMayChangeInternalLocked(bool overallCondition,
     83                                                   const int64_t eventTimeNs);
     84 
     85     void onSlicedConditionMayChangeLocked_opt1(bool overallCondition, const int64_t eventTime);
     86     void onSlicedConditionMayChangeLocked_opt2(bool overallCondition, const int64_t eventTime);
     87 
     88     // Internal function to calculate the current used bytes.
     89     size_t byteSizeLocked() const override;
     90 
     91     void dumpStatesLocked(FILE* out, bool verbose) const override;
     92 
     93     void dropDataLocked(const int64_t dropTimeNs) override;
     94 
     95     // Util function to flush the old packet.
     96     void flushIfNeededLocked(const int64_t& eventTime);
     97 
     98     void flushCurrentBucketLocked(const int64_t& eventTimeNs,
     99                                   const int64_t& nextBucketStartTimeNs) override;
    100 
    101     const DurationMetric_AggregationType mAggregationType;
    102 
    103     // Index of the SimpleAtomMatcher which defines the start.
    104     const size_t mStartIndex;
    105 
    106     // Index of the SimpleAtomMatcher which defines the stop.
    107     const size_t mStopIndex;
    108 
    109     // Index of the SimpleAtomMatcher which defines the stop all for all dimensions.
    110     const size_t mStopAllIndex;
    111 
    112     // nest counting -- for the same key, stops must match the number of starts to make real stop
    113     const bool mNested;
    114 
    115     // The dimension from the atom predicate. e.g., uid, wakelock name.
    116     vector<Matcher> mInternalDimensions;
    117 
    118     bool mContainANYPositionInInternalDimensions;
    119 
    120     // This boolean is true iff When mInternalDimensions == mDimensionsInWhat
    121     bool mUseWhatDimensionAsInternalDimension;
    122 
    123     // Caches the current unsliced part condition.
    124     ConditionState mUnSlicedPartCondition;
    125 
    126     // Save the past buckets and we can clear when the StatsLogReport is dumped.
    127     std::unordered_map<MetricDimensionKey, std::vector<DurationBucket>> mPastBuckets;
    128 
    129     // The duration trackers in the current bucket.
    130     std::unordered_map<HashableDimensionKey,
    131         std::unordered_map<HashableDimensionKey, std::unique_ptr<DurationTracker>>>
    132             mCurrentSlicedDurationTrackerMap;
    133 
    134     // Helper function to create a duration tracker given the metric aggregation type.
    135     std::unique_ptr<DurationTracker> createDurationTracker(
    136         const MetricDimensionKey& eventKey) const;
    137 
    138     // This hides the base class's std::vector<sp<AnomalyTracker>> mAnomalyTrackers
    139     std::vector<sp<DurationAnomalyTracker>> mAnomalyTrackers;
    140 
    141     // Util function to check whether the specified dimension hits the guardrail.
    142     bool hitGuardRailLocked(const MetricDimensionKey& newKey);
    143 
    144     static const size_t kBucketSize = sizeof(DurationBucket{});
    145 
    146     FRIEND_TEST(DurationMetricTrackerTest, TestNoCondition);
    147     FRIEND_TEST(DurationMetricTrackerTest, TestNonSlicedCondition);
    148     FRIEND_TEST(DurationMetricTrackerTest, TestNonSlicedConditionUnknownState);
    149     FRIEND_TEST(DurationMetricTrackerTest, TestSumDurationWithUpgrade);
    150     FRIEND_TEST(DurationMetricTrackerTest, TestSumDurationWithUpgradeInFollowingBucket);
    151     FRIEND_TEST(DurationMetricTrackerTest, TestMaxDurationWithUpgrade);
    152     FRIEND_TEST(DurationMetricTrackerTest, TestMaxDurationWithUpgradeInNextBucket);
    153     FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicates);
    154     FRIEND_TEST(DurationMetricTrackerTest, TestFirstBucket);
    155 };
    156 
    157 }  // namespace statsd
    158 }  // namespace os
    159 }  // namespace android
    160