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