• 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 METRIC_PRODUCER_H
18 #define METRIC_PRODUCER_H
19 
20 #include <src/active_config_list.pb.h>
21 #include <utils/RefBase.h>
22 
23 #include <unordered_map>
24 
25 #include "HashableDimensionKey.h"
26 #include "anomaly/AnomalyTracker.h"
27 #include "condition/ConditionTimer.h"
28 #include "condition/ConditionWizard.h"
29 #include "config/ConfigKey.h"
30 #include "guardrail/StatsdStats.h"
31 #include "matchers/EventMatcherWizard.h"
32 #include "matchers/matcher_util.h"
33 #include "packages/PackageInfoListener.h"
34 #include "src/statsd_metadata.pb.h"  // MetricMetadata
35 #include "state/StateListener.h"
36 #include "state/StateManager.h"
37 #include "utils/DbUtils.h"
38 #include "utils/ShardOffsetProvider.h"
39 
40 namespace android {
41 namespace os {
42 namespace statsd {
43 
44 // Keep this in sync with DumpReportReason enum in stats_log.proto
45 enum DumpReportReason {
46     DEVICE_SHUTDOWN = 1,
47     CONFIG_UPDATED = 2,
48     CONFIG_REMOVED = 3,
49     GET_DATA_CALLED = 4,
50     ADB_DUMP = 5,
51     CONFIG_RESET = 6,
52     STATSCOMPANION_DIED = 7,
53     TERMINATION_SIGNAL_RECEIVED = 8
54 };
55 
56 // If the metric has no activation requirement, it will be active once the metric producer is
57 // created.
58 // If the metric needs to be activated by atoms, the metric producer will start
59 // with kNotActive state, turn to kActive or kActiveOnBoot when the activation event arrives, become
60 // kNotActive when it reaches the duration limit (timebomb). If the activation event arrives again
61 // before or after it expires, the event producer will be re-activated and ttl will be reset.
62 enum ActivationState {
63     kNotActive = 0,
64     kActive = 1,
65     kActiveOnBoot = 2,
66 };
67 
68 enum DumpLatency {
69     // In some cases, we only have a short time range to do the dump, e.g. statsd is being killed.
70     // We might be able to return all the data in this mode. For instance, pull metrics might need
71     // to be pulled when the current bucket is requested.
72     FAST = 1,
73     // In other cases, it is fine for a dump to take more than a few milliseconds, e.g. config
74     // updates.
75     NO_TIME_CONSTRAINTS = 2
76 };
77 
78 // Keep this in sync with BucketDropReason enum in stats_log.proto
79 enum BucketDropReason {
80     // For ValueMetric, a bucket is dropped during a dump report request iff
81     // current bucket should be included, a pull is needed (pulled metric and
82     // condition is true), and we are under fast time constraints.
83     DUMP_REPORT_REQUESTED = 1,
84     EVENT_IN_WRONG_BUCKET = 2,
85     CONDITION_UNKNOWN = 3,
86     PULL_FAILED = 4,
87     PULL_DELAYED = 5,
88     DIMENSION_GUARDRAIL_REACHED = 6,
89     MULTIPLE_BUCKETS_SKIPPED = 7,
90     // Not an invalid bucket case, but the bucket is dropped.
91     BUCKET_TOO_SMALL = 8,
92     // Not an invalid bucket case, but the bucket is skipped.
93     NO_DATA = 9
94 };
95 
96 enum MetricType {
97     METRIC_TYPE_EVENT = 1,
98     METRIC_TYPE_COUNT = 2,
99     METRIC_TYPE_DURATION = 3,
100     METRIC_TYPE_GAUGE = 4,
101     METRIC_TYPE_VALUE = 5,
102     METRIC_TYPE_KLL = 6,
103 };
104 
105 struct Activation {
ActivationActivation106     Activation(const ActivationType& activationType, const int64_t ttlNs)
107         : ttl_ns(ttlNs),
108           start_ns(0),
109           state(ActivationState::kNotActive),
110           activationType(activationType) {}
111 
112     const int64_t ttl_ns;
113     int64_t start_ns;
114     ActivationState state;
115     const ActivationType activationType;
116 };
117 
118 struct DropEvent {
119     // Reason for dropping the bucket and/or marking the bucket invalid.
120     BucketDropReason reason;
121     // The timestamp of the drop event.
122     int64_t dropTimeNs;
123 };
124 
125 struct SkippedBucket {
126     // Start time of the dropped bucket.
127     int64_t bucketStartTimeNs;
128     // End time of the dropped bucket.
129     int64_t bucketEndTimeNs;
130     // List of events that invalidated this bucket.
131     std::vector<DropEvent> dropEvents;
132 
resetSkippedBucket133     void reset() {
134         bucketStartTimeNs = 0;
135         bucketEndTimeNs = 0;
136         dropEvents.clear();
137     }
138 };
139 
140 struct SamplingInfo {
141     // Matchers for sampled fields. Currently only one sampled dimension is supported.
142     std::vector<Matcher> sampledWhatFields;
143 
144     int shardCount = 0;
145 };
146 
147 template <class T>
getAppUpgradeBucketSplit(const T & metric)148 optional<bool> getAppUpgradeBucketSplit(const T& metric) {
149     return metric.has_split_bucket_for_app_upgrade()
150                    ? std::make_optional<bool>(metric.split_bucket_for_app_upgrade())
151                    : std::nullopt;
152 }
153 
154 // A MetricProducer is responsible for compute one single metric, creating stats log report, and
155 // writing the report to dropbox. MetricProducers should respond to package changes as required in
156 // PackageInfoListener, but if none of the metrics are slicing by package name, then the update can
157 // be a no-op.
158 class MetricProducer : public virtual RefBase, public virtual StateListener {
159 public:
160     MetricProducer(const int64_t& metricId, const ConfigKey& key, const int64_t timeBaseNs,
161                    const int conditionIndex, const vector<ConditionState>& initialConditionCache,
162                    const sp<ConditionWizard>& wizard, const uint64_t protoHash,
163                    const std::unordered_map<int, std::shared_ptr<Activation>>& eventActivationMap,
164                    const std::unordered_map<int, std::vector<std::shared_ptr<Activation>>>&
165                            eventDeactivationMap,
166                    const vector<int>& slicedStateAtoms,
167                    const unordered_map<int, unordered_map<int, int64_t>>& stateGroupMap,
168                    const optional<bool> splitBucketForAppUpgrade);
169 
~MetricProducer()170     virtual ~MetricProducer(){};
171 
initialCondition(const int conditionIndex,const vector<ConditionState> & initialConditionCache)172     ConditionState initialCondition(const int conditionIndex,
173                                     const vector<ConditionState>& initialConditionCache) const {
174         return conditionIndex >= 0 ? initialConditionCache[conditionIndex] : ConditionState::kTrue;
175     }
176 
177     // Update appropriate state on config updates. Primarily, all indices need to be updated.
178     // This metric and all of its dependencies are guaranteed to be preserved across the update.
179     // This function also updates several maps used by metricsManager.
180     // This function clears all anomaly trackers. All anomaly trackers need to be added again.
onConfigUpdated(const StatsdConfig & config,const int configIndex,const int metricIndex,const std::vector<sp<AtomMatchingTracker>> & allAtomMatchingTrackers,const std::unordered_map<int64_t,int> & oldAtomMatchingTrackerMap,const std::unordered_map<int64_t,int> & newAtomMatchingTrackerMap,const sp<EventMatcherWizard> & matcherWizard,const std::vector<sp<ConditionTracker>> & allConditionTrackers,const std::unordered_map<int64_t,int> & conditionTrackerMap,const sp<ConditionWizard> & wizard,const std::unordered_map<int64_t,int> & metricToActivationMap,std::unordered_map<int,std::vector<int>> & trackerToMetricMap,std::unordered_map<int,std::vector<int>> & conditionToMetricMap,std::unordered_map<int,std::vector<int>> & activationAtomTrackerToMetricMap,std::unordered_map<int,std::vector<int>> & deactivationAtomTrackerToMetricMap,std::vector<int> & metricsWithActivation)181     optional<InvalidConfigReason> onConfigUpdated(
182             const StatsdConfig& config, const int configIndex, const int metricIndex,
183             const std::vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers,
184             const std::unordered_map<int64_t, int>& oldAtomMatchingTrackerMap,
185             const std::unordered_map<int64_t, int>& newAtomMatchingTrackerMap,
186             const sp<EventMatcherWizard>& matcherWizard,
187             const std::vector<sp<ConditionTracker>>& allConditionTrackers,
188             const std::unordered_map<int64_t, int>& conditionTrackerMap,
189             const sp<ConditionWizard>& wizard,
190             const std::unordered_map<int64_t, int>& metricToActivationMap,
191             std::unordered_map<int, std::vector<int>>& trackerToMetricMap,
192             std::unordered_map<int, std::vector<int>>& conditionToMetricMap,
193             std::unordered_map<int, std::vector<int>>& activationAtomTrackerToMetricMap,
194             std::unordered_map<int, std::vector<int>>& deactivationAtomTrackerToMetricMap,
195             std::vector<int>& metricsWithActivation) {
196         std::lock_guard<std::mutex> lock(mMutex);
197         return onConfigUpdatedLocked(config, configIndex, metricIndex, allAtomMatchingTrackers,
198                                      oldAtomMatchingTrackerMap, newAtomMatchingTrackerMap,
199                                      matcherWizard, allConditionTrackers, conditionTrackerMap,
200                                      wizard, metricToActivationMap, trackerToMetricMap,
201                                      conditionToMetricMap, activationAtomTrackerToMetricMap,
202                                      deactivationAtomTrackerToMetricMap, metricsWithActivation);
203     };
204 
205     /**
206      * Force a partial bucket split on app upgrade
207      */
notifyAppUpgrade(const int64_t & eventTimeNs)208     void notifyAppUpgrade(const int64_t& eventTimeNs) {
209         std::lock_guard<std::mutex> lock(mMutex);
210         const bool splitBucket =
211                 mSplitBucketForAppUpgrade ? mSplitBucketForAppUpgrade.value() : false;
212         if (!splitBucket) {
213             return;
214         }
215         notifyAppUpgradeInternalLocked(eventTimeNs);
216     };
217 
notifyAppRemoved(const int64_t & eventTimeNs)218     void notifyAppRemoved(const int64_t& eventTimeNs) {
219         // Force buckets to split on removal also.
220         notifyAppUpgrade(eventTimeNs);
221     };
222 
223     /**
224      * Force a partial bucket split on boot complete.
225      */
onStatsdInitCompleted(const int64_t & eventTimeNs)226     virtual void onStatsdInitCompleted(const int64_t& eventTimeNs) {
227         std::lock_guard<std::mutex> lock(mMutex);
228         flushLocked(eventTimeNs);
229     }
230     // Consume the parsed stats log entry that already matched the "what" of the metric.
onMatchedLogEvent(const size_t matcherIndex,const LogEvent & event)231     void onMatchedLogEvent(const size_t matcherIndex, const LogEvent& event) {
232         std::lock_guard<std::mutex> lock(mMutex);
233         onMatchedLogEventLocked(matcherIndex, event);
234     }
235 
onConditionChanged(const bool condition,const int64_t eventTime)236     void onConditionChanged(const bool condition, const int64_t eventTime) {
237         std::lock_guard<std::mutex> lock(mMutex);
238         onConditionChangedLocked(condition, eventTime);
239     }
240 
onSlicedConditionMayChange(bool overallCondition,const int64_t eventTime)241     void onSlicedConditionMayChange(bool overallCondition, const int64_t eventTime) {
242         std::lock_guard<std::mutex> lock(mMutex);
243         onSlicedConditionMayChangeLocked(overallCondition, eventTime);
244     }
245 
isConditionSliced()246     bool isConditionSliced() const {
247         std::lock_guard<std::mutex> lock(mMutex);
248         return mConditionSliced;
249     };
250 
onStateChanged(const int64_t eventTimeNs,const int32_t atomId,const HashableDimensionKey & primaryKey,const FieldValue & oldState,const FieldValue & newState)251     void onStateChanged(const int64_t eventTimeNs, const int32_t atomId,
252                         const HashableDimensionKey& primaryKey, const FieldValue& oldState,
253                         const FieldValue& newState){};
254 
255     // Output the metrics data to [protoOutput]. All metrics reports end with the same timestamp.
256     // This method clears all the past buckets.
onDumpReport(const int64_t dumpTimeNs,const bool include_current_partial_bucket,const bool erase_data,const DumpLatency dumpLatency,std::set<string> * str_set,android::util::ProtoOutputStream * protoOutput)257     void onDumpReport(const int64_t dumpTimeNs,
258                       const bool include_current_partial_bucket,
259                       const bool erase_data,
260                       const DumpLatency dumpLatency,
261                       std::set<string> *str_set,
262                       android::util::ProtoOutputStream* protoOutput) {
263         std::lock_guard<std::mutex> lock(mMutex);
264         return onDumpReportLocked(dumpTimeNs, include_current_partial_bucket, erase_data,
265                 dumpLatency, str_set, protoOutput);
266     }
267 
268     virtual optional<InvalidConfigReason> onConfigUpdatedLocked(
269             const StatsdConfig& config, const int configIndex, const int metricIndex,
270             const std::vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers,
271             const std::unordered_map<int64_t, int>& oldAtomMatchingTrackerMap,
272             const std::unordered_map<int64_t, int>& newAtomMatchingTrackerMap,
273             const sp<EventMatcherWizard>& matcherWizard,
274             const std::vector<sp<ConditionTracker>>& allConditionTrackers,
275             const std::unordered_map<int64_t, int>& conditionTrackerMap,
276             const sp<ConditionWizard>& wizard,
277             const std::unordered_map<int64_t, int>& metricToActivationMap,
278             std::unordered_map<int, std::vector<int>>& trackerToMetricMap,
279             std::unordered_map<int, std::vector<int>>& conditionToMetricMap,
280             std::unordered_map<int, std::vector<int>>& activationAtomTrackerToMetricMap,
281             std::unordered_map<int, std::vector<int>>& deactivationAtomTrackerToMetricMap,
282             std::vector<int>& metricsWithActivation);
283 
clearPastBuckets(const int64_t dumpTimeNs)284     void clearPastBuckets(const int64_t dumpTimeNs) {
285         std::lock_guard<std::mutex> lock(mMutex);
286         return clearPastBucketsLocked(dumpTimeNs);
287     }
288 
prepareFirstBucket()289     void prepareFirstBucket() {
290         std::lock_guard<std::mutex> lock(mMutex);
291         prepareFirstBucketLocked();
292     }
293 
294     // Returns the memory in bytes currently used to store this metric's data. Does not change
295     // state.
byteSize()296     size_t byteSize() const {
297         std::lock_guard<std::mutex> lock(mMutex);
298         return byteSizeLocked();
299     }
300 
dumpStates(FILE * out,bool verbose)301     void dumpStates(FILE* out, bool verbose) const {
302         std::lock_guard<std::mutex> lock(mMutex);
303         dumpStatesLocked(out, verbose);
304     }
305 
306     // Let MetricProducer drop in-memory data to save memory.
307     // We still need to keep future data valid and anomaly tracking work, which means we will
308     // have to flush old data, informing anomaly trackers then safely drop old data.
309     // We still keep current bucket data for future metrics' validity.
dropData(const int64_t dropTimeNs)310     void dropData(const int64_t dropTimeNs) {
311         std::lock_guard<std::mutex> lock(mMutex);
312         dropDataLocked(dropTimeNs);
313     }
314 
loadActiveMetric(const ActiveMetric & activeMetric,int64_t currentTimeNs)315     void loadActiveMetric(const ActiveMetric& activeMetric, int64_t currentTimeNs) {
316         std::lock_guard<std::mutex> lock(mMutex);
317         loadActiveMetricLocked(activeMetric, currentTimeNs);
318     }
319 
activate(int activationTrackerIndex,int64_t elapsedTimestampNs)320     void activate(int activationTrackerIndex, int64_t elapsedTimestampNs) {
321         std::lock_guard<std::mutex> lock(mMutex);
322         activateLocked(activationTrackerIndex, elapsedTimestampNs);
323     }
324 
cancelEventActivation(int deactivationTrackerIndex)325     void cancelEventActivation(int deactivationTrackerIndex) {
326         std::lock_guard<std::mutex> lock(mMutex);
327         cancelEventActivationLocked(deactivationTrackerIndex);
328     }
329 
isActive()330     bool isActive() const {
331         std::lock_guard<std::mutex> lock(mMutex);
332         return isActiveLocked();
333     }
334 
335     void flushIfExpire(int64_t elapsedTimestampNs);
336 
337     void writeActiveMetricToProtoOutputStream(
338             int64_t currentTimeNs, const DumpReportReason reason, ProtoOutputStream* proto);
339 
enforceRestrictedDataTtl(sqlite3 * db,const int64_t wallClockNs)340     virtual void enforceRestrictedDataTtl(sqlite3* db, const int64_t wallClockNs){};
341 
writeMetricMetadataToProto(metadata::MetricMetadata * metricMetadata)342     virtual bool writeMetricMetadataToProto(metadata::MetricMetadata* metricMetadata) {
343         return false;
344     }
345 
loadMetricMetadataFromProto(const metadata::MetricMetadata & metricMetadata)346     virtual void loadMetricMetadataFromProto(const metadata::MetricMetadata& metricMetadata){};
347 
348     /* Called when the metric is to about to be removed from config. */
onMetricRemove()349     virtual void onMetricRemove() {
350     }
351 
flushRestrictedData()352     virtual void flushRestrictedData() {
353     }
354 
355     // Start: getters/setters
getMetricId()356     inline int64_t getMetricId() const {
357         return mMetricId;
358     }
359 
getProtoHash()360     inline uint64_t getProtoHash() const {
361         return mProtoHash;
362     }
363 
364     virtual MetricType getMetricType() const = 0;
365 
366     // For test only.
getCurrentBucketNum()367     inline int64_t getCurrentBucketNum() const {
368         return mCurrentBucketNum;
369     }
370 
getBucketSizeInNs()371     int64_t getBucketSizeInNs() const {
372         std::lock_guard<std::mutex> lock(mMutex);
373         return mBucketSizeNs;
374     }
375 
getSlicedStateAtoms()376     inline const std::vector<int> getSlicedStateAtoms() {
377         std::lock_guard<std::mutex> lock(mMutex);
378         return mSlicedStateAtoms;
379     }
380 
isValid()381     inline bool isValid() const {
382         return mValid;
383     }
384 
385     /* Adds an AnomalyTracker and returns it. */
addAnomalyTracker(const Alert & alert,const sp<AlarmMonitor> & anomalyAlarmMonitor,const UpdateStatus & updateStatus,const int64_t updateTimeNs)386     virtual sp<AnomalyTracker> addAnomalyTracker(const Alert& alert,
387                                                  const sp<AlarmMonitor>& anomalyAlarmMonitor,
388                                                  const UpdateStatus& updateStatus,
389                                                  const int64_t updateTimeNs) {
390         std::lock_guard<std::mutex> lock(mMutex);
391         sp<AnomalyTracker> anomalyTracker = new AnomalyTracker(alert, mConfigKey);
392         mAnomalyTrackers.push_back(anomalyTracker);
393         return anomalyTracker;
394     }
395 
396     /* Adds an AnomalyTracker that has already been created */
addAnomalyTracker(sp<AnomalyTracker> & anomalyTracker,const int64_t updateTimeNs)397     virtual void addAnomalyTracker(sp<AnomalyTracker>& anomalyTracker, const int64_t updateTimeNs) {
398         std::lock_guard<std::mutex> lock(mMutex);
399         mAnomalyTrackers.push_back(anomalyTracker);
400     }
401 
setSamplingInfo(SamplingInfo samplingInfo)402     void setSamplingInfo(SamplingInfo samplingInfo) {
403         std::lock_guard<std::mutex> lock(mMutex);
404         mSampledWhatFields.swap(samplingInfo.sampledWhatFields);
405         mShardCount = samplingInfo.shardCount;
406     }
407     // End: getters/setters
408 protected:
409     /**
410      * Flushes the current bucket if the eventTime is after the current bucket's end time.
411      */
flushIfNeededLocked(const int64_t & eventTime)412     virtual void flushIfNeededLocked(const int64_t& eventTime){};
413 
414     /**
415      * For metrics that aggregate (ie, every metric producer except for EventMetricProducer),
416      * we need to be able to flush the current buckets on demand (ie, end the current bucket and
417      * start new bucket). If this function is called when eventTimeNs is greater than the current
418      * bucket's end timestamp, than we flush up to the end of the latest full bucket; otherwise,
419      * we assume that we want to flush a partial bucket. The bucket start timestamp and bucket
420      * number are not changed by this function. This method should only be called by
421      * flushIfNeededLocked or flushLocked or the app upgrade handler; the caller MUST update the
422      * bucket timestamp and bucket number as needed.
423      */
flushCurrentBucketLocked(const int64_t & eventTimeNs,const int64_t & nextBucketStartTimeNs)424     virtual void flushCurrentBucketLocked(const int64_t& eventTimeNs,
425                                           const int64_t& nextBucketStartTimeNs) {};
426 
427     /**
428      * Flushes all the data including the current partial bucket.
429      */
flushLocked(const int64_t & eventTimeNs)430     void flushLocked(const int64_t& eventTimeNs) {
431         flushIfNeededLocked(eventTimeNs);
432         flushCurrentBucketLocked(eventTimeNs, eventTimeNs);
433     };
434 
notifyAppUpgradeInternalLocked(const int64_t eventTimeNs)435     virtual void notifyAppUpgradeInternalLocked(const int64_t eventTimeNs) {
436         flushLocked(eventTimeNs);
437     }
438 
439     /*
440      * Individual metrics can implement their own business logic here. All pre-processing is done.
441      *
442      * [matcherIndex]: the index of the matcher which matched this event. This is interesting to
443      *                 DurationMetric, because it has start/stop/stop_all 3 matchers.
444      * [eventKey]: the extracted dimension key for the final output. if the metric doesn't have
445      *             dimensions, it will be DEFAULT_DIMENSION_KEY
446      * [conditionKey]: the keys of conditions which should be used to query the condition for this
447      *                 target event (from MetricConditionLink). This is passed to individual metrics
448      *                 because DurationMetric needs it to be cached.
449      * [condition]: whether condition is met. If condition is sliced, this is the result coming from
450      *              query with ConditionWizard; If condition is not sliced, this is the
451      *              nonSlicedCondition.
452      * [event]: the log event, just in case the metric needs its data, e.g., EventMetric.
453      */
454     virtual void onMatchedLogEventInternalLocked(
455             const size_t matcherIndex, const MetricDimensionKey& eventKey,
456             const ConditionKey& conditionKey, bool condition, const LogEvent& event,
457             const map<int, HashableDimensionKey>& statePrimaryKeys) = 0;
458 
459     // Consume the parsed stats log entry that already matched the "what" of the metric.
460     virtual void onMatchedLogEventLocked(const size_t matcherIndex, const LogEvent& event);
461     virtual void onConditionChangedLocked(const bool condition, const int64_t eventTime) = 0;
462     virtual void onSlicedConditionMayChangeLocked(bool overallCondition,
463                                                   const int64_t eventTime) = 0;
464     virtual void onDumpReportLocked(const int64_t dumpTimeNs,
465                                     const bool include_current_partial_bucket,
466                                     const bool erase_data,
467                                     const DumpLatency dumpLatency,
468                                     std::set<string> *str_set,
469                                     android::util::ProtoOutputStream* protoOutput) = 0;
470     virtual void clearPastBucketsLocked(const int64_t dumpTimeNs) = 0;
prepareFirstBucketLocked()471     virtual void prepareFirstBucketLocked(){};
472     virtual size_t byteSizeLocked() const = 0;
473     virtual void dumpStatesLocked(FILE* out, bool verbose) const = 0;
474     virtual void dropDataLocked(const int64_t dropTimeNs) = 0;
475     void loadActiveMetricLocked(const ActiveMetric& activeMetric, int64_t currentTimeNs);
476     void activateLocked(int activationTrackerIndex, int64_t elapsedTimestampNs);
477     void cancelEventActivationLocked(int deactivationTrackerIndex);
478 
479     bool evaluateActiveStateLocked(int64_t elapsedTimestampNs);
480 
onActiveStateChangedLocked(const int64_t eventTimeNs,const bool isActive)481     virtual void onActiveStateChangedLocked(const int64_t eventTimeNs, const bool isActive) {
482         if (!isActive) {
483             flushLocked(eventTimeNs);
484         }
485     }
486 
isActiveLocked()487     inline bool isActiveLocked() const {
488         return mIsActive;
489     }
490 
491     // Convenience to compute the current bucket's end time, which is always aligned with the
492     // start time of the metric.
getCurrentBucketEndTimeNs()493     int64_t getCurrentBucketEndTimeNs() const {
494         return mTimeBaseNs + (mCurrentBucketNum + 1) * mBucketSizeNs;
495     }
496 
getBucketNumFromEndTimeNs(const int64_t endNs)497     int64_t getBucketNumFromEndTimeNs(const int64_t endNs) {
498         return (endNs - mTimeBaseNs) / mBucketSizeNs - 1;
499     }
500 
501     // Query StateManager for original state value using the queryKey.
502     // The field and value are output.
503     void queryStateValue(const int32_t atomId, const HashableDimensionKey& queryKey,
504                          FieldValue* value);
505 
506     // If a state map exists for the given atom, replace the original state
507     // value with the group id mapped to the value.
508     // If no state map exists, keep the original state value.
509     void mapStateValue(const int32_t atomId, FieldValue* value);
510 
511     // Returns a HashableDimensionKey with unknown state value for each state
512     // atom.
513     HashableDimensionKey getUnknownStateKey();
514 
515     DropEvent buildDropEvent(const int64_t dropTimeNs, const BucketDropReason reason) const;
516 
517     // Returns true if the number of drop events in the current bucket has
518     // exceeded the maximum number allowed, which is currently capped at 10.
519     bool maxDropEventsReached() const;
520 
521     bool passesSampleCheckLocked(const vector<FieldValue>& values) const;
522 
523     const int64_t mMetricId;
524 
525     // Hash of the Metric's proto bytes from StatsdConfig, including any activations.
526     // Used to determine if the definition of this metric has changed across a config update.
527     const uint64_t mProtoHash;
528 
529     const ConfigKey mConfigKey;
530 
531     bool mValid;
532 
533     // The time when this metric producer was first created. The end time for the current bucket
534     // can be computed from this based on mCurrentBucketNum.
535     int64_t mTimeBaseNs;
536 
537     // Start time may not be aligned with the start of statsd if there is an app upgrade in the
538     // middle of a bucket.
539     int64_t mCurrentBucketStartTimeNs;
540 
541     // Used by anomaly detector to track which bucket we are in. This is not sent with the produced
542     // report.
543     int64_t mCurrentBucketNum;
544 
545     int64_t mBucketSizeNs;
546 
547     ConditionState mCondition;
548 
549     ConditionTimer mConditionTimer;
550 
551     int mConditionTrackerIndex;
552 
553     // TODO(b/185770739): use !mMetric2ConditionLinks.empty()
554     bool mConditionSliced;
555 
556     sp<ConditionWizard> mWizard;
557 
558     bool mContainANYPositionInDimensionsInWhat;
559 
560     // Metrics slicing by primitive repeated field and/or position ALL need to use nested
561     // dimensions.
562     bool mShouldUseNestedDimensions;
563 
564     vector<Matcher> mDimensionsInWhat;  // The dimensions_in_what defined in statsd_config
565 
566     // True iff the metric to condition links cover all dimension fields in the condition tracker.
567     // This field is always false for combinational condition trackers.
568     bool mHasLinksToAllConditionDimensionsInTracker;
569 
570     std::vector<Metric2Condition> mMetric2ConditionLinks;
571 
572     std::vector<sp<AnomalyTracker>> mAnomalyTrackers;
573 
574     mutable std::mutex mMutex;
575 
576     // When the metric producer has multiple activations, these activations are ORed to determine
577     // whether the metric producer is ready to generate metrics.
578     std::unordered_map<int, std::shared_ptr<Activation>> mEventActivationMap;
579 
580     // Maps index of atom matcher for deactivation to a list of Activation structs.
581     std::unordered_map<int, std::vector<std::shared_ptr<Activation>>> mEventDeactivationMap;
582 
583     bool mIsActive;
584 
585     // The slice_by_state atom ids defined in statsd_config.
586     const std::vector<int32_t> mSlicedStateAtoms;
587 
588     // Maps atom ids and state values to group_ids (<atom_id, <value, group_id>>).
589     const std::unordered_map<int32_t, std::unordered_map<int, int64_t>> mStateGroupMap;
590 
591     // MetricStateLinks defined in statsd_config that link fields in the state
592     // atom to fields in the "what" atom.
593     std::vector<Metric2State> mMetric2StateLinks;
594 
595     optional<UploadThreshold> mUploadThreshold;
596 
597     const optional<bool> mSplitBucketForAppUpgrade;
598 
599     SkippedBucket mCurrentSkippedBucket;
600     // Buckets that were invalidated and had their data dropped.
601     std::vector<SkippedBucket> mSkippedBuckets;
602 
603     // If hard dimension guardrail is hit, do not spam logcat
604     bool mHasHitGuardrail;
605 
606     // Matchers for sampled fields. Currently only one sampled dimension is supported.
607     std::vector<Matcher> mSampledWhatFields;
608 
609     int mShardCount;
610 
611     FRIEND_TEST(CountMetricE2eTest, TestSlicedState);
612     FRIEND_TEST(CountMetricE2eTest, TestSlicedStateWithMap);
613     FRIEND_TEST(CountMetricE2eTest, TestMultipleSlicedStates);
614     FRIEND_TEST(CountMetricE2eTest, TestSlicedStateWithPrimaryFields);
615     FRIEND_TEST(CountMetricE2eTest, TestInitialConditionChanges);
616 
617     FRIEND_TEST(DurationMetricE2eTest, TestOneBucket);
618     FRIEND_TEST(DurationMetricE2eTest, TestTwoBuckets);
619     FRIEND_TEST(DurationMetricE2eTest, TestWithActivation);
620     FRIEND_TEST(DurationMetricE2eTest, TestWithCondition);
621     FRIEND_TEST(DurationMetricE2eTest, TestWithSlicedCondition);
622     FRIEND_TEST(DurationMetricE2eTest, TestWithActivationAndSlicedCondition);
623     FRIEND_TEST(DurationMetricE2eTest, TestWithSlicedState);
624     FRIEND_TEST(DurationMetricE2eTest, TestWithConditionAndSlicedState);
625     FRIEND_TEST(DurationMetricE2eTest, TestWithSlicedStateMapped);
626     FRIEND_TEST(DurationMetricE2eTest, TestSlicedStatePrimaryFieldsNotSubsetDimInWhat);
627     FRIEND_TEST(DurationMetricE2eTest, TestWithSlicedStatePrimaryFieldsSubset);
628     FRIEND_TEST(DurationMetricE2eTest, TestUploadThreshold);
629 
630     FRIEND_TEST(MetricActivationE2eTest, TestCountMetric);
631     FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithOneDeactivation);
632     FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithTwoDeactivations);
633     FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithSameDeactivation);
634     FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithTwoMetricsTwoDeactivations);
635 
636     FRIEND_TEST(StatsLogProcessorTest, TestActiveConfigMetricDiskWriteRead);
637     FRIEND_TEST(StatsLogProcessorTest, TestActivationOnBoot);
638     FRIEND_TEST(StatsLogProcessorTest, TestActivationOnBootMultipleActivations);
639     FRIEND_TEST(StatsLogProcessorTest,
640             TestActivationOnBootMultipleActivationsDifferentActivationTypes);
641     FRIEND_TEST(StatsLogProcessorTest, TestActivationsPersistAcrossSystemServerRestart);
642 
643     FRIEND_TEST(ValueMetricE2eTest, TestInitWithSlicedState);
644     FRIEND_TEST(ValueMetricE2eTest, TestInitWithSlicedState_WithDimensions);
645     FRIEND_TEST(ValueMetricE2eTest, TestInitWithSlicedState_WithIncorrectDimensions);
646     FRIEND_TEST(ValueMetricE2eTest, TestInitialConditionChanges);
647 
648     FRIEND_TEST(MetricsManagerUtilTest, TestInitialConditions);
649     FRIEND_TEST(MetricsManagerUtilTest, TestSampledMetrics);
650 
651     FRIEND_TEST(ConfigUpdateTest, TestUpdateMetricActivations);
652     FRIEND_TEST(ConfigUpdateTest, TestUpdateCountMetrics);
653     FRIEND_TEST(ConfigUpdateTest, TestUpdateEventMetrics);
654     FRIEND_TEST(ConfigUpdateTest, TestUpdateGaugeMetrics);
655     FRIEND_TEST(ConfigUpdateTest, TestUpdateDurationMetrics);
656     FRIEND_TEST(ConfigUpdateTest, TestUpdateMetricsMultipleTypes);
657     FRIEND_TEST(ConfigUpdateTest, TestUpdateAlerts);
658 };
659 
660 }  // namespace statsd
661 }  // namespace os
662 }  // namespace android
663 #endif  // METRIC_PRODUCER_H
664