• 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 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     void onStateChanged(const int64_t eventTimeNs, const int32_t atomId,
55                         const HashableDimensionKey& primaryKey, const FieldValue& oldState,
56                         const FieldValue& newState) override;
57 
58 private:
59     void onMatchedLogEventInternalLocked(
60             const size_t matcherIndex, const MetricDimensionKey& eventKey,
61             const ConditionKey& conditionKey, bool condition, const LogEvent& event,
62             const std::map<int, HashableDimensionKey>& statePrimaryKeys) override;
63 
64     void onDumpReportLocked(const int64_t dumpTimeNs, const bool include_current_partial_bucket,
65                             const bool erase_data, const DumpLatency dumpLatency,
66                             std::set<string>* str_set, std::set<int32_t>& usedUids,
67                             android::util::ProtoOutputStream* protoOutput) override;
68     void clearPastBucketsLocked(const int64_t dumpTimeNs) override;
69 
70     // Internal interface to handle condition change.
71     void onConditionChangedLocked(const bool conditionMet, int64_t eventTime) override;
72 
73     // Internal interface to handle sliced condition change.
74     void onSlicedConditionMayChangeLocked(bool overallCondition, int64_t eventTime) override;
75 
76     optional<InvalidConfigReason> onConfigUpdatedLocked(
77             const StatsdConfig& config, int configIndex, int metricIndex,
78             const std::vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers,
79             const std::unordered_map<int64_t, int>& oldAtomMatchingTrackerMap,
80             const std::unordered_map<int64_t, int>& newAtomMatchingTrackerMap,
81             const sp<EventMatcherWizard>& matcherWizard,
82             const std::vector<sp<ConditionTracker>>& allConditionTrackers,
83             const std::unordered_map<int64_t, int>& conditionTrackerMap,
84             const sp<ConditionWizard>& wizard,
85             const std::unordered_map<int64_t, int>& metricToActivationMap,
86             std::unordered_map<int, std::vector<int>>& trackerToMetricMap,
87             std::unordered_map<int, std::vector<int>>& conditionToMetricMap,
88             std::unordered_map<int, std::vector<int>>& activationAtomTrackerToMetricMap,
89             std::unordered_map<int, std::vector<int>>& deactivationAtomTrackerToMetricMap,
90             std::vector<int>& metricsWithActivation) override;
91 
92     void dropDataLocked(const int64_t dropTimeNs) override;
93 
94     // Internal function to calculate the current used bytes.
95     size_t byteSizeLocked() const override;
96 
dumpStatesLocked(int out,bool verbose)97     void dumpStatesLocked(int out, bool verbose) const override{};
98 
99     DataCorruptionSeverity determineCorruptionSeverity(int32_t atomId, DataCorruptedReason reason,
100                                                        LostAtomType atomType) const override;
101 
102     // Maps the field/value pairs of an atom to a list of timestamps used to deduplicate atoms.
103     // Used when event metric DOES NOT use slice_by_state. Empty otherwise.
104     std::unordered_map<AtomDimensionKey, std::vector<int64_t>> mAggregatedAtoms;
105 
106     // Maps the field/value pairs of an atom to the field/value pairs of a state to a list of
107     // timestamps used to deduplicate atoms and states.
108     // Used when event metric DOES use slice_by_state. Empty otherwise.
109     std::unordered_map<AtomDimensionKey,
110                        std::unordered_map<HashableDimensionKey, std::vector<int64_t>>>
111             mAggAtomsAndStates;
112 
113     const int mSamplingPercentage;
114 
115     // Allowlist/denylist of fields to report. Empty means all are reported.
116     // If mOmitFields == true, this is a denylist, otherwise it's an allowlist.
117     const std::vector<Matcher> mFieldMatchers;
118     const bool mOmitFields;
119 };
120 
121 }  // namespace statsd
122 }  // namespace os
123 }  // namespace android
124 #endif  // EVENT_METRIC_PRODUCER_H
125