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 EVENT_METRIC_PRODUCER_H
18 #define EVENT_METRIC_PRODUCER_H
19 
20 #include <unordered_map>
21 
22 #include <android/util/ProtoOutputStream.h>
23 
24 #include "../condition/ConditionTracker.h"
25 #include "../matchers/matcher_util.h"
26 #include "HashableDimensionKey.h"
27 #include "MetricProducer.h"
28 #include "src/statsd_config.pb.h"
29 #include "stats_util.h"
30 
31 namespace android {
32 namespace os {
33 namespace statsd {
34 
35 class EventMetricProducer : public MetricProducer {
36 public:
37     EventMetricProducer(
38             const ConfigKey& key, const EventMetric& eventMetric, int conditionIndex,
39             const vector<ConditionState>& initialConditionCache, const sp<ConditionWizard>& wizard,
40             const uint64_t protoHash, int64_t startTimeNs,
41             const wp<ConfigMetadataProvider> configMetadataProvider,
42             const std::unordered_map<int, std::shared_ptr<Activation>>& eventActivationMap = {},
43             const std::unordered_map<int, std::vector<std::shared_ptr<Activation>>>&
44                     eventDeactivationMap = {},
45             const vector<int>& slicedStateAtoms = {},
46             const unordered_map<int, unordered_map<int, int64_t>>& stateGroupMap = {});
47 
48     virtual ~EventMetricProducer();
49 
getMetricType()50     MetricType getMetricType() const override {
51         return METRIC_TYPE_EVENT;
52     }
53 
54 private:
55     void onMatchedLogEventInternalLocked(
56             const size_t matcherIndex, const MetricDimensionKey& eventKey,
57             const ConditionKey& conditionKey, bool condition, const LogEvent& event,
58             const std::map<int, HashableDimensionKey>& statePrimaryKeys) override;
59 
60     void onDumpReportLocked(const int64_t dumpTimeNs, const bool include_current_partial_bucket,
61                             const bool erase_data, const DumpLatency dumpLatency,
62                             std::set<string>* str_set, std::set<int32_t>& usedUids,
63                             android::util::ProtoOutputStream* protoOutput) override;
64     void clearPastBucketsLocked(const int64_t dumpTimeNs) override;
65 
66     // Internal interface to handle condition change.
67     void onConditionChangedLocked(const bool conditionMet, int64_t eventTime) override;
68 
69     // Internal interface to handle sliced condition change.
70     void onSlicedConditionMayChangeLocked(bool overallCondition, int64_t eventTime) override;
71 
72     optional<InvalidConfigReason> onConfigUpdatedLocked(
73             const StatsdConfig& config, int configIndex, int metricIndex,
74             const std::vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers,
75             const std::unordered_map<int64_t, int>& oldAtomMatchingTrackerMap,
76             const std::unordered_map<int64_t, int>& newAtomMatchingTrackerMap,
77             const sp<EventMatcherWizard>& matcherWizard,
78             const std::vector<sp<ConditionTracker>>& allConditionTrackers,
79             const std::unordered_map<int64_t, int>& conditionTrackerMap,
80             const sp<ConditionWizard>& wizard,
81             const std::unordered_map<int64_t, int>& metricToActivationMap,
82             std::unordered_map<int, std::vector<int>>& trackerToMetricMap,
83             std::unordered_map<int, std::vector<int>>& conditionToMetricMap,
84             std::unordered_map<int, std::vector<int>>& activationAtomTrackerToMetricMap,
85             std::unordered_map<int, std::vector<int>>& deactivationAtomTrackerToMetricMap,
86             std::vector<int>& metricsWithActivation) override;
87 
88     void dropDataLocked(const int64_t dropTimeNs) override;
89 
90     // Internal function to calculate the current used bytes.
91     size_t byteSizeLocked() const override;
92 
dumpStatesLocked(int out,bool verbose)93     void dumpStatesLocked(int out, bool verbose) const override{};
94 
95     DataCorruptionSeverity determineCorruptionSeverity(int32_t atomId, DataCorruptedReason reason,
96                                                        LostAtomType atomType) const override;
97 
98     // Maps the field/value pairs of an atom to a list of timestamps used to deduplicate atoms.
99     std::unordered_map<AtomDimensionKey, std::vector<int64_t>> mAggregatedAtoms;
100 
101     const int mSamplingPercentage;
102 };
103 
104 }  // namespace statsd
105 }  // namespace os
106 }  // namespace android
107 #endif  // EVENT_METRIC_PRODUCER_H
108