blob: 6aba13ca78597084d556f735f03058c643e20c5b [file] [log] [blame]
Yao Chen44cf27c2017-09-14 22:32:50 -07001/*
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
Muhammad Qureshi844694b2019-04-05 10:10:40 -070020#include <frameworks/base/cmds/statsd/src/active_config_list.pb.h>
tsaichristined21aacf2019-10-07 14:47:38 -070021#include <utils/RefBase.h>
22
23#include <unordered_map>
24
Yao Chen8a8d16c2018-02-08 14:50:40 -080025#include "HashableDimensionKey.h"
Yangster-mace2cd6d52017-11-09 20:38:30 -080026#include "anomaly/AnomalyTracker.h"
Yao Chen729093d2017-10-16 10:33:26 -070027#include "condition/ConditionWizard.h"
Yao Chenb3561512017-11-21 18:07:17 -080028#include "config/ConfigKey.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070029#include "matchers/matcher_util.h"
30#include "packages/PackageInfoListener.h"
tsaichristined21aacf2019-10-07 14:47:38 -070031#include "state/StateListener.h"
tsaichristine69000e62019-10-18 17:34:52 -070032#include "state/StateManager.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070033
34namespace android {
35namespace os {
36namespace statsd {
37
Tej Singhf53d4452019-05-09 18:17:59 -070038// Keep this in sync with DumpReportReason enum in stats_log.proto
39enum DumpReportReason {
40 DEVICE_SHUTDOWN = 1,
41 CONFIG_UPDATED = 2,
42 CONFIG_REMOVED = 3,
43 GET_DATA_CALLED = 4,
44 ADB_DUMP = 5,
45 CONFIG_RESET = 6,
46 STATSCOMPANION_DIED = 7,
47 TERMINATION_SIGNAL_RECEIVED = 8
48};
49
Yangster-mac849dfdc22018-10-12 15:41:45 -070050// If the metric has no activation requirement, it will be active once the metric producer is
51// created.
52// If the metric needs to be activated by atoms, the metric producer will start
Chenjie Yua9a310e2019-02-06 13:40:10 -080053// with kNotActive state, turn to kActive or kActiveOnBoot when the activation event arrives, become
54// kNotActive when it reaches the duration limit (timebomb). If the activation event arrives again
55// before or after it expires, the event producer will be re-activated and ttl will be reset.
Yangster-mac849dfdc22018-10-12 15:41:45 -070056enum ActivationState {
57 kNotActive = 0,
58 kActive = 1,
Chenjie Yua9a310e2019-02-06 13:40:10 -080059 kActiveOnBoot = 2,
Yangster-mac849dfdc22018-10-12 15:41:45 -070060};
61
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +000062enum DumpLatency {
63 // In some cases, we only have a short time range to do the dump, e.g. statsd is being killed.
64 // We might be able to return all the data in this mode. For instance, pull metrics might need
65 // to be pulled when the current bucket is requested.
66 FAST = 1,
67 // In other cases, it is fine for a dump to take more than a few milliseconds, e.g. config
68 // updates.
69 NO_TIME_CONSTRAINTS = 2
70};
71
tsaichristineb87ca152019-12-09 15:19:41 -080072// Keep this in sync with BucketDropReason enum in stats_log.proto
73enum BucketDropReason {
74 // For ValueMetric, a bucket is dropped during a dump report request iff
75 // current bucket should be included, a pull is needed (pulled metric and
76 // condition is true), and we are under fast time constraints.
77 DUMP_REPORT_REQUESTED = 1,
78 EVENT_IN_WRONG_BUCKET = 2,
79 CONDITION_UNKNOWN = 3,
80 PULL_FAILED = 4,
81 PULL_DELAYED = 5,
82 DIMENSION_GUARDRAIL_REACHED = 6,
83 MULTIPLE_BUCKETS_SKIPPED = 7,
84 // Not an invalid bucket case, but the bucket is dropped.
85 BUCKET_TOO_SMALL = 8
86};
87
Ruchir Rastogi21a287b2019-10-02 12:04:33 -070088struct Activation {
89 Activation(const ActivationType& activationType, const int64_t ttlNs)
90 : ttl_ns(ttlNs),
91 start_ns(0),
92 state(ActivationState::kNotActive),
93 activationType(activationType) {}
94
95 const int64_t ttl_ns;
96 int64_t start_ns;
97 ActivationState state;
98 const ActivationType activationType;
99};
100
tsaichristineb87ca152019-12-09 15:19:41 -0800101struct DropEvent {
102 // Reason for dropping the bucket and/or marking the bucket invalid.
103 BucketDropReason reason;
104 // The timestamp of the drop event.
105 int64_t dropTimeNs;
106};
107
108struct SkippedBucket {
109 // Start time of the dropped bucket.
110 int64_t bucketStartTimeNs;
111 // End time of the dropped bucket.
112 int64_t bucketEndTimeNs;
113 // List of events that invalidated this bucket.
114 std::vector<DropEvent> dropEvents;
115
116 void reset() {
117 bucketStartTimeNs = 0;
118 bucketEndTimeNs = 0;
119 dropEvents.clear();
120 }
121};
122
Yao Chen44cf27c2017-09-14 22:32:50 -0700123// A MetricProducer is responsible for compute one single metrics, creating stats log report, and
David Chende701692017-10-05 13:16:02 -0700124// writing the report to dropbox. MetricProducers should respond to package changes as required in
125// PackageInfoListener, but if none of the metrics are slicing by package name, then the update can
126// be a no-op.
Tej Singh9ec159a2019-11-14 11:59:48 -0800127class MetricProducer : public virtual android::RefBase, public virtual StateListener {
Yao Chen44cf27c2017-09-14 22:32:50 -0700128public:
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700129 MetricProducer(const int64_t& metricId, const ConfigKey& key, const int64_t timeBaseNs,
Ruchir Rastogi21a287b2019-10-02 12:04:33 -0700130 const int conditionIndex, const sp<ConditionWizard>& wizard,
131 const std::unordered_map<int, std::shared_ptr<Activation>>& eventActivationMap,
132 const std::unordered_map<int, std::vector<std::shared_ptr<Activation>>>&
tsaichristined21aacf2019-10-07 14:47:38 -0700133 eventDeactivationMap,
134 const vector<int>& slicedStateAtoms,
135 const unordered_map<int, unordered_map<int, int64_t>>& stateGroupMap);
Yangster-mac20877162017-12-22 17:19:39 -0800136
Yao Chen44cf27c2017-09-14 22:32:50 -0700137 virtual ~MetricProducer(){};
138
Olivier Gaillarde35b2822019-02-27 17:09:40 +0000139 ConditionState initialCondition(const int conditionIndex) const {
140 return conditionIndex >= 0 ? ConditionState::kUnknown : ConditionState::kTrue;
141 }
142
David Chen27785a82018-01-19 17:06:45 -0800143 /**
144 * Forces this metric to split into a partial bucket right now. If we're past a full bucket, we
145 * first call the standard flushing code to flush up to the latest full bucket. Then we call
146 * the flush again when the end timestamp is forced to be now, and then after flushing, update
147 * the start timestamp to be now.
148 */
Tej Singh9ec159a2019-11-14 11:59:48 -0800149 virtual void notifyAppUpgrade(const int64_t& eventTimeNs, const string& apk, const int uid,
150 const int64_t version) {
David Chen27785a82018-01-19 17:06:45 -0800151 std::lock_guard<std::mutex> lock(mMutex);
152
153 if (eventTimeNs > getCurrentBucketEndTimeNs()) {
154 // Flush full buckets on the normal path up to the latest bucket boundary.
155 flushIfNeededLocked(eventTimeNs);
156 }
157 // Now flush a partial bucket.
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000158 flushCurrentBucketLocked(eventTimeNs, eventTimeNs);
David Chen27785a82018-01-19 17:06:45 -0800159 // Don't update the current bucket number so that the anomaly tracker knows this bucket
160 // is a partial bucket and can merge it with the previous bucket.
161 };
162
Tej Singh9ec159a2019-11-14 11:59:48 -0800163 void notifyAppRemoved(const int64_t& eventTimeNs, const string& apk, const int uid) {
David Chenbd125272018-04-04 19:02:50 -0700164 // Force buckets to split on removal also.
165 notifyAppUpgrade(eventTimeNs, apk, uid, 0);
Yao Chend10f7b12017-12-18 12:53:50 -0800166 };
167
Yao Chencaf339d2017-10-06 16:01:10 -0700168 // Consume the parsed stats log entry that already matched the "what" of the metric.
Chenjie Yua7259ab2017-12-10 08:31:05 -0800169 void onMatchedLogEvent(const size_t matcherIndex, const LogEvent& event) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800170 std::lock_guard<std::mutex> lock(mMutex);
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700171 onMatchedLogEventLocked(matcherIndex, event);
Yangsterf2bee6f2017-11-29 12:01:05 -0800172 }
Yao Chen44cf27c2017-09-14 22:32:50 -0700173
Yangster-macb142cc82018-03-30 15:22:08 -0700174 void onConditionChanged(const bool condition, const int64_t eventTime) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800175 std::lock_guard<std::mutex> lock(mMutex);
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700176 onConditionChangedLocked(condition, eventTime);
Yangsterf2bee6f2017-11-29 12:01:05 -0800177 }
Yao Chencaf339d2017-10-06 16:01:10 -0700178
Yangster-macb142cc82018-03-30 15:22:08 -0700179 void onSlicedConditionMayChange(bool overallCondition, const int64_t eventTime) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800180 std::lock_guard<std::mutex> lock(mMutex);
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700181 onSlicedConditionMayChangeLocked(overallCondition, eventTime);
Yangsterf2bee6f2017-11-29 12:01:05 -0800182 }
183
184 bool isConditionSliced() const {
185 std::lock_guard<std::mutex> lock(mMutex);
186 return mConditionSliced;
187 };
Yao Chen729093d2017-10-16 10:33:26 -0700188
tsaichristine8d73dc92019-12-06 02:11:02 -0800189 void onStateChanged(const int64_t eventTimeNs, const int32_t atomId,
tsaichristine1449fa42020-01-02 12:12:05 -0800190 const HashableDimensionKey& primaryKey, const int32_t oldState,
191 const int32_t newState){};
tsaichristined21aacf2019-10-07 14:47:38 -0700192
Yao Chen288c6002017-12-12 13:43:18 -0800193 // Output the metrics data to [protoOutput]. All metrics reports end with the same timestamp.
David Chen27785a82018-01-19 17:06:45 -0800194 // This method clears all the past buckets.
Yangster-mace68f3a52018-04-04 00:01:43 -0700195 void onDumpReport(const int64_t dumpTimeNs,
196 const bool include_current_partial_bucket,
Bookatzff71cad2018-09-20 17:17:49 -0700197 const bool erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000198 const DumpLatency dumpLatency,
Yangster-mac9def8e32018-04-17 13:55:51 -0700199 std::set<string> *str_set,
Yangster-mace68f3a52018-04-04 00:01:43 -0700200 android::util::ProtoOutputStream* protoOutput) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800201 std::lock_guard<std::mutex> lock(mMutex);
Bookatzff71cad2018-09-20 17:17:49 -0700202 return onDumpReportLocked(dumpTimeNs, include_current_partial_bucket, erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000203 dumpLatency, str_set, protoOutput);
Yangsterf2bee6f2017-11-29 12:01:05 -0800204 }
Yao Chen729093d2017-10-16 10:33:26 -0700205
Yangster-maca802d732018-04-24 07:50:38 -0700206 void clearPastBuckets(const int64_t dumpTimeNs) {
207 std::lock_guard<std::mutex> lock(mMutex);
208 return clearPastBucketsLocked(dumpTimeNs);
209 }
210
Tej Singh3be093b2020-03-04 20:08:38 -0800211 void prepareFirstBucket() {
212 std::lock_guard<std::mutex> lock(mMutex);
213 prepareFirstBucketLocked();
214 }
215
David Chen1d7b0cd2017-11-15 14:20:04 -0800216 // Returns the memory in bytes currently used to store this metric's data. Does not change
217 // state.
Yangsterf2bee6f2017-11-29 12:01:05 -0800218 size_t byteSize() const {
219 std::lock_guard<std::mutex> lock(mMutex);
220 return byteSizeLocked();
221 }
yro69007c82017-10-26 20:42:57 -0700222
tsaichristinea05cfe02019-09-18 16:28:48 -0700223 void dumpStates(FILE* out, bool verbose) const {
Yangsterf2bee6f2017-11-29 12:01:05 -0800224 std::lock_guard<std::mutex> lock(mMutex);
tsaichristinea05cfe02019-09-18 16:28:48 -0700225 dumpStatesLocked(out, verbose);
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800226 }
227
Yao Chen06dba5d2018-01-26 13:38:16 -0800228 // Let MetricProducer drop in-memory data to save memory.
229 // We still need to keep future data valid and anomaly tracking work, which means we will
230 // have to flush old data, informing anomaly trackers then safely drop old data.
231 // We still keep current bucket data for future metrics' validity.
Yangster-macb142cc82018-03-30 15:22:08 -0700232 void dropData(const int64_t dropTimeNs) {
Yao Chen06dba5d2018-01-26 13:38:16 -0800233 std::lock_guard<std::mutex> lock(mMutex);
234 dropDataLocked(dropTimeNs);
235 }
236
tsaichristinea05cfe02019-09-18 16:28:48 -0700237 void loadActiveMetric(const ActiveMetric& activeMetric, int64_t currentTimeNs) {
238 std::lock_guard<std::mutex> lock(mMutex);
239 loadActiveMetricLocked(activeMetric, currentTimeNs);
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700240 }
241
Yangster-mac849dfdc22018-10-12 15:41:45 -0700242 void activate(int activationTrackerIndex, int64_t elapsedTimestampNs) {
243 std::lock_guard<std::mutex> lock(mMutex);
244 activateLocked(activationTrackerIndex, elapsedTimestampNs);
245 }
246
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700247 void cancelEventActivation(int deactivationTrackerIndex) {
248 std::lock_guard<std::mutex> lock(mMutex);
249 cancelEventActivationLocked(deactivationTrackerIndex);
250 }
251
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800252 bool isActive() const {
253 std::lock_guard<std::mutex> lock(mMutex);
254 return isActiveLocked();
255 }
256
tsaichristinea05cfe02019-09-18 16:28:48 -0700257 void flushIfExpire(int64_t elapsedTimestampNs);
258
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700259 void writeActiveMetricToProtoOutputStream(
Tej Singhf53d4452019-05-09 18:17:59 -0700260 int64_t currentTimeNs, const DumpReportReason reason, ProtoOutputStream* proto);
Yangsterf2bee6f2017-11-29 12:01:05 -0800261
tsaichristinea05cfe02019-09-18 16:28:48 -0700262 // Start: getters/setters
263 inline const int64_t& getMetricId() const {
264 return mMetricId;
Yang Lub4722912018-11-15 11:02:03 -0800265 }
266
tsaichristinea05cfe02019-09-18 16:28:48 -0700267 // For test only.
268 inline int64_t getCurrentBucketNum() const {
269 return mCurrentBucketNum;
270 }
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800271
tsaichristinea05cfe02019-09-18 16:28:48 -0700272 int64_t getBucketSizeInNs() const {
273 std::lock_guard<std::mutex> lock(mMutex);
274 return mBucketSizeNs;
275 }
276
tsaichristined21aacf2019-10-07 14:47:38 -0700277 inline const std::vector<int> getSlicedStateAtoms() {
278 std::lock_guard<std::mutex> lock(mMutex);
279 return mSlicedStateAtoms;
280 }
281
tsaichristinea05cfe02019-09-18 16:28:48 -0700282 /* If alert is valid, adds an AnomalyTracker and returns it. If invalid, returns nullptr. */
283 virtual sp<AnomalyTracker> addAnomalyTracker(const Alert &alert,
284 const sp<AlarmMonitor>& anomalyAlarmMonitor) {
285 std::lock_guard<std::mutex> lock(mMutex);
286 sp<AnomalyTracker> anomalyTracker = new AnomalyTracker(alert, mConfigKey);
287 if (anomalyTracker != nullptr) {
288 mAnomalyTrackers.push_back(anomalyTracker);
289 }
290 return anomalyTracker;
291 }
292 // End: getters/setters
293protected:
David Chen27785a82018-01-19 17:06:45 -0800294 /**
Yangster-mace68f3a52018-04-04 00:01:43 -0700295 * Flushes the current bucket if the eventTime is after the current bucket's end time. This will
296 also flush the current partial bucket in memory.
David Chen27785a82018-01-19 17:06:45 -0800297 */
Yangster-macb142cc82018-03-30 15:22:08 -0700298 virtual void flushIfNeededLocked(const int64_t& eventTime){};
David Chen27785a82018-01-19 17:06:45 -0800299
300 /**
301 * For metrics that aggregate (ie, every metric producer except for EventMetricProducer),
302 * we need to be able to flush the current buckets on demand (ie, end the current bucket and
303 * start new bucket). If this function is called when eventTimeNs is greater than the current
304 * bucket's end timestamp, than we flush up to the end of the latest full bucket; otherwise,
305 * we assume that we want to flush a partial bucket. The bucket start timestamp and bucket
306 * number are not changed by this function. This method should only be called by
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700307 * flushIfNeededLocked or flushLocked or the app upgrade handler; the caller MUST update the
308 * bucket timestamp and bucket number as needed.
David Chen27785a82018-01-19 17:06:45 -0800309 */
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000310 virtual void flushCurrentBucketLocked(const int64_t& eventTimeNs,
311 const int64_t& nextBucketStartTimeNs) {};
David Chen27785a82018-01-19 17:06:45 -0800312
tsaichristinea05cfe02019-09-18 16:28:48 -0700313 /**
314 * Flushes all the data including the current partial bucket.
315 */
316 virtual void flushLocked(const int64_t& eventTimeNs) {
317 flushIfNeededLocked(eventTimeNs);
318 flushCurrentBucketLocked(eventTimeNs, eventTimeNs);
319 };
Yangster-mace2cd6d52017-11-09 20:38:30 -0800320
Yao Chenb7041772017-10-20 16:59:25 -0700321 /*
322 * Individual metrics can implement their own business logic here. All pre-processing is done.
323 *
324 * [matcherIndex]: the index of the matcher which matched this event. This is interesting to
325 * DurationMetric, because it has start/stop/stop_all 3 matchers.
326 * [eventKey]: the extracted dimension key for the final output. if the metric doesn't have
327 * dimensions, it will be DEFAULT_DIMENSION_KEY
328 * [conditionKey]: the keys of conditions which should be used to query the condition for this
Stefan Lafona5b51912017-12-05 21:43:52 -0800329 * target event (from MetricConditionLink). This is passed to individual metrics
Yao Chenb7041772017-10-20 16:59:25 -0700330 * because DurationMetric needs it to be cached.
331 * [condition]: whether condition is met. If condition is sliced, this is the result coming from
332 * query with ConditionWizard; If condition is not sliced, this is the
333 * nonSlicedCondition.
334 * [event]: the log event, just in case the metric needs its data, e.g., EventMetric.
335 */
Yangsterf2bee6f2017-11-29 12:01:05 -0800336 virtual void onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -0800337 const size_t matcherIndex, const MetricDimensionKey& eventKey,
tsaichristinec876b492019-12-10 13:47:05 -0800338 const ConditionKey& conditionKey, bool condition, const LogEvent& event,
339 const map<int, HashableDimensionKey>& statePrimaryKeys) = 0;
yro2b0f8862017-11-06 14:27:31 -0800340
Yangsterf2bee6f2017-11-29 12:01:05 -0800341 // Consume the parsed stats log entry that already matched the "what" of the metric.
Yangster-mac53928882018-02-25 23:02:56 -0800342 virtual void onMatchedLogEventLocked(const size_t matcherIndex, const LogEvent& event);
tsaichristinea05cfe02019-09-18 16:28:48 -0700343 virtual void onConditionChangedLocked(const bool condition, const int64_t eventTime) = 0;
344 virtual void onSlicedConditionMayChangeLocked(bool overallCondition,
345 const int64_t eventTime) = 0;
346 virtual void onDumpReportLocked(const int64_t dumpTimeNs,
347 const bool include_current_partial_bucket,
348 const bool erase_data,
349 const DumpLatency dumpLatency,
350 std::set<string> *str_set,
351 android::util::ProtoOutputStream* protoOutput) = 0;
352 virtual void clearPastBucketsLocked(const int64_t dumpTimeNs) = 0;
Tej Singh3be093b2020-03-04 20:08:38 -0800353 virtual void prepareFirstBucketLocked(){};
tsaichristinea05cfe02019-09-18 16:28:48 -0700354 virtual size_t byteSizeLocked() const = 0;
355 virtual void dumpStatesLocked(FILE* out, bool verbose) const = 0;
356 virtual void dropDataLocked(const int64_t dropTimeNs) = 0;
tsaichristinea05cfe02019-09-18 16:28:48 -0700357 void loadActiveMetricLocked(const ActiveMetric& activeMetric, int64_t currentTimeNs);
358 void activateLocked(int activationTrackerIndex, int64_t elapsedTimestampNs);
359 void cancelEventActivationLocked(int deactivationTrackerIndex);
360
361 bool evaluateActiveStateLocked(int64_t elapsedTimestampNs);
362
363 virtual void onActiveStateChangedLocked(const int64_t& eventTimeNs) {
364 if (!mIsActive) {
365 flushLocked(eventTimeNs);
366 }
367 }
368
369 inline bool isActiveLocked() const {
370 return mIsActive;
371 }
372
373 // Convenience to compute the current bucket's end time, which is always aligned with the
374 // start time of the metric.
375 int64_t getCurrentBucketEndTimeNs() const {
376 return mTimeBaseNs + (mCurrentBucketNum + 1) * mBucketSizeNs;
377 }
378
379 int64_t getBucketNumFromEndTimeNs(const int64_t endNs) {
380 return (endNs - mTimeBaseNs) / mBucketSizeNs - 1;
381 }
382
tsaichristine1449fa42020-01-02 12:12:05 -0800383 // Query StateManager for original state value using the queryKey.
384 // The field and value are output.
385 void queryStateValue(const int32_t atomId, const HashableDimensionKey& queryKey,
386 FieldValue* value);
387
388 // If a state map exists for the given atom, replace the original state
389 // value with the group id mapped to the value.
390 // If no state map exists, keep the original state value.
391 void mapStateValue(const int32_t atomId, FieldValue* value);
tsaichristine69000e62019-10-18 17:34:52 -0700392
tsaichristineb87ca152019-12-09 15:19:41 -0800393 DropEvent buildDropEvent(const int64_t dropTimeNs, const BucketDropReason reason);
394
395 // Returns true if the number of drop events in the current bucket has
396 // exceeded the maximum number allowed, which is currently capped at 10.
397 bool maxDropEventsReached();
398
tsaichristinea05cfe02019-09-18 16:28:48 -0700399 const int64_t mMetricId;
400
401 const ConfigKey mConfigKey;
402
403 // The time when this metric producer was first created. The end time for the current bucket
404 // can be computed from this based on mCurrentBucketNum.
405 int64_t mTimeBaseNs;
406
407 // Start time may not be aligned with the start of statsd if there is an app upgrade in the
408 // middle of a bucket.
409 int64_t mCurrentBucketStartTimeNs;
410
411 // Used by anomaly detector to track which bucket we are in. This is not sent with the produced
412 // report.
413 int64_t mCurrentBucketNum;
414
415 int64_t mBucketSizeNs;
416
417 ConditionState mCondition;
418
419 int mConditionTrackerIndex;
420
421 bool mConditionSliced;
422
423 sp<ConditionWizard> mWizard;
424
425 bool mContainANYPositionInDimensionsInWhat;
426
427 bool mSliceByPositionALL;
428
429 vector<Matcher> mDimensionsInWhat; // The dimensions_in_what defined in statsd_config
430
431 // True iff the metric to condition links cover all dimension fields in the condition tracker.
432 // This field is always false for combinational condition trackers.
433 bool mHasLinksToAllConditionDimensionsInTracker;
434
435 std::vector<Metric2Condition> mMetric2ConditionLinks;
436
437 std::vector<sp<AnomalyTracker>> mAnomalyTrackers;
Yangsterf2bee6f2017-11-29 12:01:05 -0800438
Yangsterf2bee6f2017-11-29 12:01:05 -0800439 mutable std::mutex mMutex;
Yangster-mac849dfdc22018-10-12 15:41:45 -0700440
Yangster-mac849dfdc22018-10-12 15:41:45 -0700441 // When the metric producer has multiple activations, these activations are ORed to determine
442 // whether the metric producer is ready to generate metrics.
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700443 std::unordered_map<int, std::shared_ptr<Activation>> mEventActivationMap;
444
Tej Singhee4495e2019-06-03 18:37:35 -0700445 // Maps index of atom matcher for deactivation to a list of Activation structs.
446 std::unordered_map<int, std::vector<std::shared_ptr<Activation>>> mEventDeactivationMap;
Yangster-mac849dfdc22018-10-12 15:41:45 -0700447
448 bool mIsActive;
449
tsaichristined21aacf2019-10-07 14:47:38 -0700450 // The slice_by_state atom ids defined in statsd_config.
tsaichristine69000e62019-10-18 17:34:52 -0700451 std::vector<int32_t> mSlicedStateAtoms;
tsaichristined21aacf2019-10-07 14:47:38 -0700452
453 // Maps atom ids and state values to group_ids (<atom_id, <value, group_id>>).
Muhammad Qureshibfc4bdb2020-04-08 06:26:49 -0700454 const std::unordered_map<int32_t, std::unordered_map<int, int64_t>> mStateGroupMap;
tsaichristined21aacf2019-10-07 14:47:38 -0700455
tsaichristine69000e62019-10-18 17:34:52 -0700456 // MetricStateLinks defined in statsd_config that link fields in the state
457 // atom to fields in the "what" atom.
458 std::vector<Metric2State> mMetric2StateLinks;
459
tsaichristineb87ca152019-12-09 15:19:41 -0800460 SkippedBucket mCurrentSkippedBucket;
461 // Buckets that were invalidated and had their data dropped.
462 std::vector<SkippedBucket> mSkippedBuckets;
463
tsaichristine69000e62019-10-18 17:34:52 -0700464 FRIEND_TEST(CountMetricE2eTest, TestSlicedState);
465 FRIEND_TEST(CountMetricE2eTest, TestSlicedStateWithMap);
466 FRIEND_TEST(CountMetricE2eTest, TestMultipleSlicedStates);
467 FRIEND_TEST(CountMetricE2eTest, TestSlicedStateWithPrimaryFields);
tsaichristined21aacf2019-10-07 14:47:38 -0700468
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700469 FRIEND_TEST(DurationMetricE2eTest, TestOneBucket);
470 FRIEND_TEST(DurationMetricE2eTest, TestTwoBuckets);
471 FRIEND_TEST(DurationMetricE2eTest, TestWithActivation);
472 FRIEND_TEST(DurationMetricE2eTest, TestWithCondition);
473 FRIEND_TEST(DurationMetricE2eTest, TestWithSlicedCondition);
474 FRIEND_TEST(DurationMetricE2eTest, TestWithActivationAndSlicedCondition);
tsaichristine1449fa42020-01-02 12:12:05 -0800475 FRIEND_TEST(DurationMetricE2eTest, TestWithSlicedState);
476 FRIEND_TEST(DurationMetricE2eTest, TestWithConditionAndSlicedState);
477 FRIEND_TEST(DurationMetricE2eTest, TestWithSlicedStateMapped);
478 FRIEND_TEST(DurationMetricE2eTest, TestSlicedStatePrimaryFieldsNotSubsetDimInWhat);
479 FRIEND_TEST(DurationMetricE2eTest, TestWithSlicedStatePrimaryFieldsSubset);
Muhammad Qureshi18e46922019-05-24 16:38:49 -0700480
Yangster-mac849dfdc22018-10-12 15:41:45 -0700481 FRIEND_TEST(MetricActivationE2eTest, TestCountMetric);
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700482 FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithOneDeactivation);
483 FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithTwoDeactivations);
Tej Singhee4495e2019-06-03 18:37:35 -0700484 FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithSameDeactivation);
Muhammad Qureshi3a5ebf52019-03-28 12:38:21 -0700485 FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithTwoMetricsTwoDeactivations);
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800486
487 FRIEND_TEST(StatsLogProcessorTest, TestActiveConfigMetricDiskWriteRead);
Chenjie Yua9a310e2019-02-06 13:40:10 -0800488 FRIEND_TEST(StatsLogProcessorTest, TestActivationOnBoot);
Muhammad Qureshi844694b2019-04-05 10:10:40 -0700489 FRIEND_TEST(StatsLogProcessorTest, TestActivationOnBootMultipleActivations);
Muhammad Qureshi15f8da92019-04-05 10:10:40 -0700490 FRIEND_TEST(StatsLogProcessorTest,
491 TestActivationOnBootMultipleActivationsDifferentActivationTypes);
Tej Singhf53d4452019-05-09 18:17:59 -0700492 FRIEND_TEST(StatsLogProcessorTest, TestActivationsPersistAcrossSystemServerRestart);
tsaichristinec876b492019-12-10 13:47:05 -0800493
494 FRIEND_TEST(ValueMetricE2eTest, TestInitWithSlicedState);
495 FRIEND_TEST(ValueMetricE2eTest, TestInitWithSlicedState_WithDimensions);
496 FRIEND_TEST(ValueMetricE2eTest, TestInitWithSlicedState_WithIncorrectDimensions);
Yao Chen44cf27c2017-09-14 22:32:50 -0700497};
498
499} // namespace statsd
500} // namespace os
501} // namespace android
502#endif // METRIC_PRODUCER_H