blob: 046f9963b351acb5d40328d6f936f22c43ebc690 [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
Yangsterf2bee6f2017-11-29 12:01:05 -080020#include <shared_mutex>
21
Yao Chen8a8d16c2018-02-08 14:50:40 -080022#include "HashableDimensionKey.h"
Yangster-mace2cd6d52017-11-09 20:38:30 -080023#include "anomaly/AnomalyTracker.h"
Yao Chen729093d2017-10-16 10:33:26 -070024#include "condition/ConditionWizard.h"
Yao Chenb3561512017-11-21 18:07:17 -080025#include "config/ConfigKey.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070026#include "matchers/matcher_util.h"
27#include "packages/PackageInfoListener.h"
28
Yao Chen44cf27c2017-09-14 22:32:50 -070029#include <log/logprint.h>
Yao Chencaf339d2017-10-06 16:01:10 -070030#include <utils/RefBase.h>
Yao Chend10f7b12017-12-18 12:53:50 -080031#include <unordered_map>
Yao Chen44cf27c2017-09-14 22:32:50 -070032
33namespace android {
34namespace os {
35namespace statsd {
36
Yangster-mac849dfdc22018-10-12 15:41:45 -070037// If the metric has no activation requirement, it will be active once the metric producer is
38// created.
39// If the metric needs to be activated by atoms, the metric producer will start
Chenjie Yua9a310e2019-02-06 13:40:10 -080040// with kNotActive state, turn to kActive or kActiveOnBoot when the activation event arrives, become
41// kNotActive when it reaches the duration limit (timebomb). If the activation event arrives again
42// before or after it expires, the event producer will be re-activated and ttl will be reset.
Yangster-mac849dfdc22018-10-12 15:41:45 -070043enum ActivationState {
44 kNotActive = 0,
45 kActive = 1,
Chenjie Yua9a310e2019-02-06 13:40:10 -080046 kActiveOnBoot = 2,
Yangster-mac849dfdc22018-10-12 15:41:45 -070047};
48
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +000049enum DumpLatency {
50 // In some cases, we only have a short time range to do the dump, e.g. statsd is being killed.
51 // We might be able to return all the data in this mode. For instance, pull metrics might need
52 // to be pulled when the current bucket is requested.
53 FAST = 1,
54 // In other cases, it is fine for a dump to take more than a few milliseconds, e.g. config
55 // updates.
56 NO_TIME_CONSTRAINTS = 2
57};
58
Yao Chen44cf27c2017-09-14 22:32:50 -070059// A MetricProducer is responsible for compute one single metrics, creating stats log report, and
David Chende701692017-10-05 13:16:02 -070060// writing the report to dropbox. MetricProducers should respond to package changes as required in
61// PackageInfoListener, but if none of the metrics are slicing by package name, then the update can
62// be a no-op.
David Chenf12b5c62017-10-13 15:54:03 -070063class MetricProducer : public virtual PackageInfoListener {
Yao Chen44cf27c2017-09-14 22:32:50 -070064public:
Yangster-mac15f6bbc2018-04-08 11:52:26 -070065 MetricProducer(const int64_t& metricId, const ConfigKey& key, const int64_t timeBaseNs,
Yao Chenf09569f2017-12-13 17:00:51 -080066 const int conditionIndex, const sp<ConditionWizard>& wizard)
Yangster-mac94e197c2018-01-02 16:03:03 -080067 : mMetricId(metricId),
Yao Chenf09569f2017-12-13 17:00:51 -080068 mConfigKey(key),
Yangster-mac15f6bbc2018-04-08 11:52:26 -070069 mTimeBaseNs(timeBaseNs),
70 mCurrentBucketStartTimeNs(timeBaseNs),
Yang Lu3eba6212017-10-25 19:54:45 -070071 mCurrentBucketNum(0),
Olivier Gaillarde35b2822019-02-27 17:09:40 +000072 mCondition(initialCondition(conditionIndex)),
Yao Chen93fe3a32017-11-02 13:52:59 -070073 mConditionSliced(false),
Yao Chen729093d2017-10-16 10:33:26 -070074 mWizard(wizard),
Yangster13fb7e42018-03-07 17:30:49 -080075 mConditionTrackerIndex(conditionIndex),
76 mContainANYPositionInDimensionsInWhat(false),
Yangster-mac9def8e32018-04-17 13:55:51 -070077 mSliceByPositionALL(false),
Yangster13fb7e42018-03-07 17:30:49 -080078 mSameConditionDimensionsInTracker(false),
Yangster-mac849dfdc22018-10-12 15:41:45 -070079 mHasLinksToAllConditionDimensionsInTracker(false),
80 mIsActive(true) {
Yangster13fb7e42018-03-07 17:30:49 -080081 }
Yangster-mac20877162017-12-22 17:19:39 -080082
Yao Chen44cf27c2017-09-14 22:32:50 -070083 virtual ~MetricProducer(){};
84
Olivier Gaillarde35b2822019-02-27 17:09:40 +000085 ConditionState initialCondition(const int conditionIndex) const {
86 return conditionIndex >= 0 ? ConditionState::kUnknown : ConditionState::kTrue;
87 }
88
David Chen27785a82018-01-19 17:06:45 -080089 /**
90 * Forces this metric to split into a partial bucket right now. If we're past a full bucket, we
91 * first call the standard flushing code to flush up to the latest full bucket. Then we call
92 * the flush again when the end timestamp is forced to be now, and then after flushing, update
93 * the start timestamp to be now.
94 */
Yangster-macb142cc82018-03-30 15:22:08 -070095 void notifyAppUpgrade(const int64_t& eventTimeNs, const string& apk, const int uid,
David Chen27785a82018-01-19 17:06:45 -080096 const int64_t version) override {
97 std::lock_guard<std::mutex> lock(mMutex);
98
99 if (eventTimeNs > getCurrentBucketEndTimeNs()) {
100 // Flush full buckets on the normal path up to the latest bucket boundary.
101 flushIfNeededLocked(eventTimeNs);
102 }
103 // Now flush a partial bucket.
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000104 flushCurrentBucketLocked(eventTimeNs, eventTimeNs);
David Chen27785a82018-01-19 17:06:45 -0800105 // Don't update the current bucket number so that the anomaly tracker knows this bucket
106 // is a partial bucket and can merge it with the previous bucket.
107 };
108
Yangster-macb142cc82018-03-30 15:22:08 -0700109 void notifyAppRemoved(const int64_t& eventTimeNs, const string& apk, const int uid) override{
David Chenbd125272018-04-04 19:02:50 -0700110 // Force buckets to split on removal also.
111 notifyAppUpgrade(eventTimeNs, apk, uid, 0);
Yao Chend10f7b12017-12-18 12:53:50 -0800112 };
113
Yangster-macb142cc82018-03-30 15:22:08 -0700114 void onUidMapReceived(const int64_t& eventTimeNs) override{
David Chenbd125272018-04-04 19:02:50 -0700115 // Purposefully don't flush partial buckets on a new snapshot.
116 // This occurs if a new user is added/removed or statsd crashes.
Yao Chend10f7b12017-12-18 12:53:50 -0800117 };
118
Yao Chencaf339d2017-10-06 16:01:10 -0700119 // Consume the parsed stats log entry that already matched the "what" of the metric.
Chenjie Yua7259ab2017-12-10 08:31:05 -0800120 void onMatchedLogEvent(const size_t matcherIndex, const LogEvent& event) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800121 std::lock_guard<std::mutex> lock(mMutex);
Yangster-mac849dfdc22018-10-12 15:41:45 -0700122 if (mIsActive) {
123 onMatchedLogEventLocked(matcherIndex, event);
124 }
Yangsterf2bee6f2017-11-29 12:01:05 -0800125 }
Yao Chen44cf27c2017-09-14 22:32:50 -0700126
Yangster-macb142cc82018-03-30 15:22:08 -0700127 void onConditionChanged(const bool condition, const int64_t eventTime) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800128 std::lock_guard<std::mutex> lock(mMutex);
Yangster-mac849dfdc22018-10-12 15:41:45 -0700129 if (mIsActive) {
130 onConditionChangedLocked(condition, eventTime);
131 }
Yangsterf2bee6f2017-11-29 12:01:05 -0800132 }
Yao Chencaf339d2017-10-06 16:01:10 -0700133
Yangster-macb142cc82018-03-30 15:22:08 -0700134 void onSlicedConditionMayChange(bool overallCondition, const int64_t eventTime) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800135 std::lock_guard<std::mutex> lock(mMutex);
Yangster-mac849dfdc22018-10-12 15:41:45 -0700136 if (mIsActive) {
137 onSlicedConditionMayChangeLocked(overallCondition, eventTime);
138 }
Yangsterf2bee6f2017-11-29 12:01:05 -0800139 }
140
141 bool isConditionSliced() const {
142 std::lock_guard<std::mutex> lock(mMutex);
143 return mConditionSliced;
144 };
Yao Chen729093d2017-10-16 10:33:26 -0700145
Yao Chen288c6002017-12-12 13:43:18 -0800146 // Output the metrics data to [protoOutput]. All metrics reports end with the same timestamp.
David Chen27785a82018-01-19 17:06:45 -0800147 // This method clears all the past buckets.
Yangster-mace68f3a52018-04-04 00:01:43 -0700148 void onDumpReport(const int64_t dumpTimeNs,
149 const bool include_current_partial_bucket,
Bookatzff71cad2018-09-20 17:17:49 -0700150 const bool erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000151 const DumpLatency dumpLatency,
Yangster-mac9def8e32018-04-17 13:55:51 -0700152 std::set<string> *str_set,
Yangster-mace68f3a52018-04-04 00:01:43 -0700153 android::util::ProtoOutputStream* protoOutput) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800154 std::lock_guard<std::mutex> lock(mMutex);
Bookatzff71cad2018-09-20 17:17:49 -0700155 return onDumpReportLocked(dumpTimeNs, include_current_partial_bucket, erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000156 dumpLatency, str_set, protoOutput);
Yangsterf2bee6f2017-11-29 12:01:05 -0800157 }
Yao Chen729093d2017-10-16 10:33:26 -0700158
Yangster-maca802d732018-04-24 07:50:38 -0700159 void clearPastBuckets(const int64_t dumpTimeNs) {
160 std::lock_guard<std::mutex> lock(mMutex);
161 return clearPastBucketsLocked(dumpTimeNs);
162 }
163
Yao Chen884c8c12018-01-26 10:36:25 -0800164 void dumpStates(FILE* out, bool verbose) const {
165 std::lock_guard<std::mutex> lock(mMutex);
166 dumpStatesLocked(out, verbose);
167 }
168
David Chen1d7b0cd2017-11-15 14:20:04 -0800169 // Returns the memory in bytes currently used to store this metric's data. Does not change
170 // state.
Yangsterf2bee6f2017-11-29 12:01:05 -0800171 size_t byteSize() const {
172 std::lock_guard<std::mutex> lock(mMutex);
173 return byteSizeLocked();
174 }
yro69007c82017-10-26 20:42:57 -0700175
Bookatz1476ef22018-02-13 12:26:01 -0800176 /* If alert is valid, adds an AnomalyTracker and returns it. If invalid, returns nullptr. */
Yangster-mac932ecec2018-02-01 10:23:52 -0800177 virtual sp<AnomalyTracker> addAnomalyTracker(const Alert &alert,
178 const sp<AlarmMonitor>& anomalyAlarmMonitor) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800179 std::lock_guard<std::mutex> lock(mMutex);
Bookatz857aaa52017-12-19 15:29:06 -0800180 sp<AnomalyTracker> anomalyTracker = new AnomalyTracker(alert, mConfigKey);
181 if (anomalyTracker != nullptr) {
182 mAnomalyTrackers.push_back(anomalyTracker);
183 }
184 return anomalyTracker;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800185 }
186
187 int64_t getBuckeSizeInNs() const {
Yangsterf2bee6f2017-11-29 12:01:05 -0800188 std::lock_guard<std::mutex> lock(mMutex);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800189 return mBucketSizeNs;
190 }
191
yro59cc24d2018-02-13 20:17:32 -0800192 // Only needed for unit-testing to override guardrail.
193 void setBucketSize(int64_t bucketSize) {
194 mBucketSizeNs = bucketSize;
195 }
196
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800197 inline const int64_t& getMetricId() const {
Yangster-mac94e197c2018-01-02 16:03:03 -0800198 return mMetricId;
Yangster-mac20877162017-12-22 17:19:39 -0800199 }
200
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800201 int64_t getRemainingTtlNs(int64_t currentTimeNs) const {
202 std::lock_guard<std::mutex> lock(mMutex);
203 return getRemainingTtlNsLocked(currentTimeNs);
204 }
205
206 // Set metric to active for ttlNs.
207 void setActive(int64_t currentTimeNs, int64_t remainingTtlNs) {
208 std::lock_guard<std::mutex> lock(mMutex);
209 setActiveLocked(currentTimeNs, remainingTtlNs);
210 }
211
Yao Chen06dba5d2018-01-26 13:38:16 -0800212 // Let MetricProducer drop in-memory data to save memory.
213 // We still need to keep future data valid and anomaly tracking work, which means we will
214 // have to flush old data, informing anomaly trackers then safely drop old data.
215 // We still keep current bucket data for future metrics' validity.
Yangster-macb142cc82018-03-30 15:22:08 -0700216 void dropData(const int64_t dropTimeNs) {
Yao Chen06dba5d2018-01-26 13:38:16 -0800217 std::lock_guard<std::mutex> lock(mMutex);
218 dropDataLocked(dropTimeNs);
219 }
220
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700221 // For test only.
222 inline int64_t getCurrentBucketNum() const {
223 return mCurrentBucketNum;
224 }
225
Yangster-mac849dfdc22018-10-12 15:41:45 -0700226 void activate(int activationTrackerIndex, int64_t elapsedTimestampNs) {
227 std::lock_guard<std::mutex> lock(mMutex);
228 activateLocked(activationTrackerIndex, elapsedTimestampNs);
229 }
230
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800231 bool isActive() const {
232 std::lock_guard<std::mutex> lock(mMutex);
233 return isActiveLocked();
234 }
235
Chenjie Yua9a310e2019-02-06 13:40:10 -0800236 void prepActiveForBootIfNecessary(int64_t currentTimeNs) {
237 std::lock_guard<std::mutex> lock(mMutex);
238 prepActiveForBootIfNecessaryLocked(currentTimeNs);
239 }
240
Yangster-mac849dfdc22018-10-12 15:41:45 -0700241 void addActivation(int activationTrackerIndex, int64_t ttl_seconds);
242
Chenjie Yua9a310e2019-02-06 13:40:10 -0800243 inline void setActivationType(const MetricActivation::ActivationType& activationType) {
244 mActivationType = activationType;
245 }
246
Yangster-mac849dfdc22018-10-12 15:41:45 -0700247 void flushIfExpire(int64_t elapsedTimestampNs);
248
Yao Chen729093d2017-10-16 10:33:26 -0700249protected:
Yangster-macb142cc82018-03-30 15:22:08 -0700250 virtual void onConditionChangedLocked(const bool condition, const int64_t eventTime) = 0;
Yao Chen427d3722018-03-22 15:21:52 -0700251 virtual void onSlicedConditionMayChangeLocked(bool overallCondition,
Yangster-macb142cc82018-03-30 15:22:08 -0700252 const int64_t eventTime) = 0;
253 virtual void onDumpReportLocked(const int64_t dumpTimeNs,
Yangster-mace68f3a52018-04-04 00:01:43 -0700254 const bool include_current_partial_bucket,
Bookatzff71cad2018-09-20 17:17:49 -0700255 const bool erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000256 const DumpLatency dumpLatency,
Yangster-mac9def8e32018-04-17 13:55:51 -0700257 std::set<string> *str_set,
Yao Chen288c6002017-12-12 13:43:18 -0800258 android::util::ProtoOutputStream* protoOutput) = 0;
Yangster-maca802d732018-04-24 07:50:38 -0700259 virtual void clearPastBucketsLocked(const int64_t dumpTimeNs) = 0;
Yangsterf2bee6f2017-11-29 12:01:05 -0800260 virtual size_t byteSizeLocked() const = 0;
Yao Chen884c8c12018-01-26 10:36:25 -0800261 virtual void dumpStatesLocked(FILE* out, bool verbose) const = 0;
Yangsterf2bee6f2017-11-29 12:01:05 -0800262
Yangster-mac849dfdc22018-10-12 15:41:45 -0700263 bool evaluateActiveStateLocked(int64_t elapsedTimestampNs);
264
265 void activateLocked(int activationTrackerIndex, int64_t elapsedTimestampNs);
266
Yang Lub4722912018-11-15 11:02:03 -0800267 inline bool isActiveLocked() const {
268 return mIsActive;
269 }
270
Chenjie Yua9a310e2019-02-06 13:40:10 -0800271 void prepActiveForBootIfNecessaryLocked(int64_t currentTimeNs);
272
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800273 int64_t getRemainingTtlNsLocked(int64_t currentTimeNs) const;
274
275 void setActiveLocked(int64_t currentTimeNs, int64_t remainingTtlNs);
276
David Chen27785a82018-01-19 17:06:45 -0800277 /**
Yangster-mace68f3a52018-04-04 00:01:43 -0700278 * Flushes the current bucket if the eventTime is after the current bucket's end time. This will
279 also flush the current partial bucket in memory.
David Chen27785a82018-01-19 17:06:45 -0800280 */
Yangster-macb142cc82018-03-30 15:22:08 -0700281 virtual void flushIfNeededLocked(const int64_t& eventTime){};
David Chen27785a82018-01-19 17:06:45 -0800282
283 /**
Yangster-mace68f3a52018-04-04 00:01:43 -0700284 * Flushes all the data including the current partial bucket.
285 */
Yangster-mac849dfdc22018-10-12 15:41:45 -0700286 virtual void flushLocked(const int64_t& eventTimeNs) {
287 flushIfNeededLocked(eventTimeNs);
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000288 flushCurrentBucketLocked(eventTimeNs, eventTimeNs);
Yangster-mace68f3a52018-04-04 00:01:43 -0700289 };
290
291 /**
David Chen27785a82018-01-19 17:06:45 -0800292 * For metrics that aggregate (ie, every metric producer except for EventMetricProducer),
293 * we need to be able to flush the current buckets on demand (ie, end the current bucket and
294 * start new bucket). If this function is called when eventTimeNs is greater than the current
295 * bucket's end timestamp, than we flush up to the end of the latest full bucket; otherwise,
296 * we assume that we want to flush a partial bucket. The bucket start timestamp and bucket
297 * number are not changed by this function. This method should only be called by
298 * flushIfNeededLocked or the app upgrade handler; the caller MUST update the bucket timestamp
299 * and bucket number as needed.
300 */
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000301 virtual void flushCurrentBucketLocked(const int64_t& eventTimeNs,
302 const int64_t& nextBucketStartTimeNs) {};
David Chen27785a82018-01-19 17:06:45 -0800303
304 // Convenience to compute the current bucket's end time, which is always aligned with the
305 // start time of the metric.
Yangster-macb142cc82018-03-30 15:22:08 -0700306 int64_t getCurrentBucketEndTimeNs() const {
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700307 return mTimeBaseNs + (mCurrentBucketNum + 1) * mBucketSizeNs;
David Chen27785a82018-01-19 17:06:45 -0800308 }
309
Yangster-mac9def8e32018-04-17 13:55:51 -0700310 int64_t getBucketNumFromEndTimeNs(const int64_t endNs) {
311 return (endNs - mTimeBaseNs) / mBucketSizeNs - 1;
312 }
313
Yangster-macb142cc82018-03-30 15:22:08 -0700314 virtual void dropDataLocked(const int64_t dropTimeNs) = 0;
Yao Chen06dba5d2018-01-26 13:38:16 -0800315
Yangster-mac94e197c2018-01-02 16:03:03 -0800316 const int64_t mMetricId;
Yao Chenf09569f2017-12-13 17:00:51 -0800317
Yao Chenb3561512017-11-21 18:07:17 -0800318 const ConfigKey mConfigKey;
319
David Chen27785a82018-01-19 17:06:45 -0800320 // The time when this metric producer was first created. The end time for the current bucket
321 // can be computed from this based on mCurrentBucketNum.
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700322 int64_t mTimeBaseNs;
Yao Chen729093d2017-10-16 10:33:26 -0700323
David Chen27785a82018-01-19 17:06:45 -0800324 // Start time may not be aligned with the start of statsd if there is an app upgrade in the
325 // middle of a bucket.
Yangster-macb142cc82018-03-30 15:22:08 -0700326 int64_t mCurrentBucketStartTimeNs;
Yao Chen729093d2017-10-16 10:33:26 -0700327
David Chen27785a82018-01-19 17:06:45 -0800328 // Used by anomaly detector to track which bucket we are in. This is not sent with the produced
329 // report.
Yangster-macb142cc82018-03-30 15:22:08 -0700330 int64_t mCurrentBucketNum;
Yang Lu3eba6212017-10-25 19:54:45 -0700331
Yao Chen729093d2017-10-16 10:33:26 -0700332 int64_t mBucketSizeNs;
333
Olivier Gaillarde63d9e02019-02-12 14:43:59 +0000334 ConditionState mCondition;
Yao Chen729093d2017-10-16 10:33:26 -0700335
336 bool mConditionSliced;
337
338 sp<ConditionWizard> mWizard;
339
340 int mConditionTrackerIndex;
341
Yao Chen8a8d16c2018-02-08 14:50:40 -0800342 vector<Matcher> mDimensionsInWhat; // The dimensions_in_what defined in statsd_config
343 vector<Matcher> mDimensionsInCondition; // The dimensions_in_condition defined in statsd_config
Yao Chen729093d2017-10-16 10:33:26 -0700344
Yangster13fb7e42018-03-07 17:30:49 -0800345 bool mContainANYPositionInDimensionsInWhat;
Yangster-mac9def8e32018-04-17 13:55:51 -0700346 bool mSliceByPositionALL;
Yangster13fb7e42018-03-07 17:30:49 -0800347
348 // True iff the condition dimensions equal to the sliced dimensions in the simple condition
349 // tracker. This field is always false for combinational condition trackers.
350 bool mSameConditionDimensionsInTracker;
351
352 // True iff the metric to condition links cover all dimension fields in the condition tracker.
353 // This field is always false for combinational condition trackers.
354 bool mHasLinksToAllConditionDimensionsInTracker;
355
Yao Chen8a8d16c2018-02-08 14:50:40 -0800356 std::vector<Metric2Condition> mMetric2ConditionLinks;
Yao Chenb7041772017-10-20 16:59:25 -0700357
Yangster-mace2cd6d52017-11-09 20:38:30 -0800358 std::vector<sp<AnomalyTracker>> mAnomalyTrackers;
359
Yao Chenb7041772017-10-20 16:59:25 -0700360 /*
361 * Individual metrics can implement their own business logic here. All pre-processing is done.
362 *
363 * [matcherIndex]: the index of the matcher which matched this event. This is interesting to
364 * DurationMetric, because it has start/stop/stop_all 3 matchers.
365 * [eventKey]: the extracted dimension key for the final output. if the metric doesn't have
366 * dimensions, it will be DEFAULT_DIMENSION_KEY
367 * [conditionKey]: the keys of conditions which should be used to query the condition for this
Stefan Lafona5b51912017-12-05 21:43:52 -0800368 * target event (from MetricConditionLink). This is passed to individual metrics
Yao Chenb7041772017-10-20 16:59:25 -0700369 * because DurationMetric needs it to be cached.
370 * [condition]: whether condition is met. If condition is sliced, this is the result coming from
371 * query with ConditionWizard; If condition is not sliced, this is the
372 * nonSlicedCondition.
373 * [event]: the log event, just in case the metric needs its data, e.g., EventMetric.
374 */
Yangsterf2bee6f2017-11-29 12:01:05 -0800375 virtual void onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -0800376 const size_t matcherIndex, const MetricDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -0800377 const ConditionKey& conditionKey, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800378 const LogEvent& event) = 0;
yro2b0f8862017-11-06 14:27:31 -0800379
Yangsterf2bee6f2017-11-29 12:01:05 -0800380 // Consume the parsed stats log entry that already matched the "what" of the metric.
Yangster-mac53928882018-02-25 23:02:56 -0800381 virtual void onMatchedLogEventLocked(const size_t matcherIndex, const LogEvent& event);
Yangsterf2bee6f2017-11-29 12:01:05 -0800382
Yangsterf2bee6f2017-11-29 12:01:05 -0800383 mutable std::mutex mMutex;
Yangster-mac849dfdc22018-10-12 15:41:45 -0700384
385 struct Activation {
386 Activation() : ttl_ns(0), activation_ns(0), state(ActivationState::kNotActive) {}
387
388 int64_t ttl_ns;
389 int64_t activation_ns;
390 ActivationState state;
391 };
392 // When the metric producer has multiple activations, these activations are ORed to determine
393 // whether the metric producer is ready to generate metrics.
394 std::unordered_map<int, Activation> mEventActivationMap;
395
396 bool mIsActive;
397
Chenjie Yua9a310e2019-02-06 13:40:10 -0800398 MetricActivation::ActivationType mActivationType;
399
Yangster-mac849dfdc22018-10-12 15:41:45 -0700400 FRIEND_TEST(MetricActivationE2eTest, TestCountMetric);
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800401
402 FRIEND_TEST(StatsLogProcessorTest, TestActiveConfigMetricDiskWriteRead);
Chenjie Yua9a310e2019-02-06 13:40:10 -0800403 FRIEND_TEST(StatsLogProcessorTest, TestActivationOnBoot);
Yao Chen44cf27c2017-09-14 22:32:50 -0700404};
405
406} // namespace statsd
407} // namespace os
408} // namespace android
409#endif // METRIC_PRODUCER_H