• 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 #ifndef COUNT_METRIC_PRODUCER_H
18 #define COUNT_METRIC_PRODUCER_H
19 
20 #include <unordered_map>
21 
22 #include <android/util/ProtoOutputStream.h>
23 #include <gtest/gtest_prod.h>
24 #include "../anomaly/AnomalyTracker.h"
25 #include "../condition/ConditionTracker.h"
26 #include "../matchers/matcher_util.h"
27 #include "MetricProducer.h"
28 #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
29 #include "stats_util.h"
30 
31 namespace android {
32 namespace os {
33 namespace statsd {
34 
35 struct CountBucket {
36     int64_t mBucketStartNs;
37     int64_t mBucketEndNs;
38     int64_t mCount;
39 };
40 
41 class CountMetricProducer : public MetricProducer {
42 public:
43     CountMetricProducer(const ConfigKey& key, const CountMetric& countMetric,
44                         const int conditionIndex, const sp<ConditionWizard>& wizard,
45                         const int64_t timeBaseNs, const int64_t startTimeNs);
46 
47     virtual ~CountMetricProducer();
48 
49 protected:
50     void onMatchedLogEventInternalLocked(
51             const size_t matcherIndex, const MetricDimensionKey& eventKey,
52             const ConditionKey& conditionKey, bool condition,
53             const LogEvent& event) override;
54 
55 private:
56 
57     void onDumpReportLocked(const int64_t dumpTimeNs,
58                             const bool include_current_partial_bucket,
59                             const bool erase_data,
60                             const DumpLatency dumpLatency,
61                             std::set<string> *str_set,
62                             android::util::ProtoOutputStream* protoOutput) override;
63 
64     void clearPastBucketsLocked(const int64_t dumpTimeNs) override;
65 
66     // Internal interface to handle condition change.
67     void onConditionChangedLocked(const bool conditionMet, const int64_t eventTime) override;
68 
69     // Internal interface to handle sliced condition change.
70     void onSlicedConditionMayChangeLocked(bool overallCondition, const int64_t eventTime) override;
71 
72     // Internal function to calculate the current used bytes.
73     size_t byteSizeLocked() const override;
74 
75     void dumpStatesLocked(FILE* out, bool verbose) const override;
76 
77     void dropDataLocked(const int64_t dropTimeNs) override;
78 
79     // Util function to flush the old packet.
80     void flushIfNeededLocked(const int64_t& newEventTime) override;
81 
82     void flushCurrentBucketLocked(const int64_t& eventTimeNs,
83                                   const int64_t& nextBucketStartTimeNs) override;
84 
85     std::unordered_map<MetricDimensionKey, std::vector<CountBucket>> mPastBuckets;
86 
87     // The current bucket (may be a partial bucket).
88     std::shared_ptr<DimToValMap> mCurrentSlicedCounter = std::make_shared<DimToValMap>();
89 
90     // The sum of previous partial buckets in the current full bucket (excluding the current
91     // partial bucket). This is only updated while flushing the current bucket.
92     std::shared_ptr<DimToValMap> mCurrentFullCounters = std::make_shared<DimToValMap>();
93 
94     static const size_t kBucketSize = sizeof(CountBucket{});
95 
96     bool hitGuardRailLocked(const MetricDimensionKey& newKey);
97 
98     FRIEND_TEST(CountMetricProducerTest, TestNonDimensionalEvents);
99     FRIEND_TEST(CountMetricProducerTest, TestEventsWithNonSlicedCondition);
100     FRIEND_TEST(CountMetricProducerTest, TestEventsWithSlicedCondition);
101     FRIEND_TEST(CountMetricProducerTest, TestAnomalyDetectionUnSliced);
102     FRIEND_TEST(CountMetricProducerTest, TestEventWithAppUpgrade);
103     FRIEND_TEST(CountMetricProducerTest, TestEventWithAppUpgradeInNextBucket);
104     FRIEND_TEST(CountMetricProducerTest, TestFirstBucket);
105 };
106 
107 }  // namespace statsd
108 }  // namespace os
109 }  // namespace android
110 #endif  // COUNT_METRIC_PRODUCER_H
111