blob: 99cb5d4389c705ffb7b78cf83ca080ec84f7f244 [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 Gaillarde63d9e02019-02-12 14:43:59 +000072 mCondition(conditionIndex >= 0 ? ConditionState::kUnknown : ConditionState::kTrue),
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
David Chen27785a82018-01-19 17:06:45 -080085 /**
86 * Forces this metric to split into a partial bucket right now. If we're past a full bucket, we
87 * first call the standard flushing code to flush up to the latest full bucket. Then we call
88 * the flush again when the end timestamp is forced to be now, and then after flushing, update
89 * the start timestamp to be now.
90 */
Yangster-macb142cc82018-03-30 15:22:08 -070091 void notifyAppUpgrade(const int64_t& eventTimeNs, const string& apk, const int uid,
David Chen27785a82018-01-19 17:06:45 -080092 const int64_t version) override {
93 std::lock_guard<std::mutex> lock(mMutex);
94
95 if (eventTimeNs > getCurrentBucketEndTimeNs()) {
96 // Flush full buckets on the normal path up to the latest bucket boundary.
97 flushIfNeededLocked(eventTimeNs);
98 }
99 // Now flush a partial bucket.
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000100 flushCurrentBucketLocked(eventTimeNs, eventTimeNs);
David Chen27785a82018-01-19 17:06:45 -0800101 // Don't update the current bucket number so that the anomaly tracker knows this bucket
102 // is a partial bucket and can merge it with the previous bucket.
103 };
104
Yangster-macb142cc82018-03-30 15:22:08 -0700105 void notifyAppRemoved(const int64_t& eventTimeNs, const string& apk, const int uid) override{
David Chenbd125272018-04-04 19:02:50 -0700106 // Force buckets to split on removal also.
107 notifyAppUpgrade(eventTimeNs, apk, uid, 0);
Yao Chend10f7b12017-12-18 12:53:50 -0800108 };
109
Yangster-macb142cc82018-03-30 15:22:08 -0700110 void onUidMapReceived(const int64_t& eventTimeNs) override{
David Chenbd125272018-04-04 19:02:50 -0700111 // Purposefully don't flush partial buckets on a new snapshot.
112 // This occurs if a new user is added/removed or statsd crashes.
Yao Chend10f7b12017-12-18 12:53:50 -0800113 };
114
Yao Chencaf339d2017-10-06 16:01:10 -0700115 // Consume the parsed stats log entry that already matched the "what" of the metric.
Chenjie Yua7259ab2017-12-10 08:31:05 -0800116 void onMatchedLogEvent(const size_t matcherIndex, const LogEvent& event) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800117 std::lock_guard<std::mutex> lock(mMutex);
Yangster-mac849dfdc22018-10-12 15:41:45 -0700118 if (mIsActive) {
119 onMatchedLogEventLocked(matcherIndex, event);
120 }
Yangsterf2bee6f2017-11-29 12:01:05 -0800121 }
Yao Chen44cf27c2017-09-14 22:32:50 -0700122
Yangster-macb142cc82018-03-30 15:22:08 -0700123 void onConditionChanged(const bool condition, const int64_t eventTime) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800124 std::lock_guard<std::mutex> lock(mMutex);
Yangster-mac849dfdc22018-10-12 15:41:45 -0700125 if (mIsActive) {
126 onConditionChangedLocked(condition, eventTime);
127 }
Yangsterf2bee6f2017-11-29 12:01:05 -0800128 }
Yao Chencaf339d2017-10-06 16:01:10 -0700129
Yangster-macb142cc82018-03-30 15:22:08 -0700130 void onSlicedConditionMayChange(bool overallCondition, const int64_t eventTime) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800131 std::lock_guard<std::mutex> lock(mMutex);
Yangster-mac849dfdc22018-10-12 15:41:45 -0700132 if (mIsActive) {
133 onSlicedConditionMayChangeLocked(overallCondition, eventTime);
134 }
Yangsterf2bee6f2017-11-29 12:01:05 -0800135 }
136
137 bool isConditionSliced() const {
138 std::lock_guard<std::mutex> lock(mMutex);
139 return mConditionSliced;
140 };
Yao Chen729093d2017-10-16 10:33:26 -0700141
Yao Chen288c6002017-12-12 13:43:18 -0800142 // Output the metrics data to [protoOutput]. All metrics reports end with the same timestamp.
David Chen27785a82018-01-19 17:06:45 -0800143 // This method clears all the past buckets.
Yangster-mace68f3a52018-04-04 00:01:43 -0700144 void onDumpReport(const int64_t dumpTimeNs,
145 const bool include_current_partial_bucket,
Bookatzff71cad2018-09-20 17:17:49 -0700146 const bool erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000147 const DumpLatency dumpLatency,
Yangster-mac9def8e32018-04-17 13:55:51 -0700148 std::set<string> *str_set,
Yangster-mace68f3a52018-04-04 00:01:43 -0700149 android::util::ProtoOutputStream* protoOutput) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800150 std::lock_guard<std::mutex> lock(mMutex);
Bookatzff71cad2018-09-20 17:17:49 -0700151 return onDumpReportLocked(dumpTimeNs, include_current_partial_bucket, erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000152 dumpLatency, str_set, protoOutput);
Yangsterf2bee6f2017-11-29 12:01:05 -0800153 }
Yao Chen729093d2017-10-16 10:33:26 -0700154
Yangster-maca802d732018-04-24 07:50:38 -0700155 void clearPastBuckets(const int64_t dumpTimeNs) {
156 std::lock_guard<std::mutex> lock(mMutex);
157 return clearPastBucketsLocked(dumpTimeNs);
158 }
159
Yao Chen884c8c12018-01-26 10:36:25 -0800160 void dumpStates(FILE* out, bool verbose) const {
161 std::lock_guard<std::mutex> lock(mMutex);
162 dumpStatesLocked(out, verbose);
163 }
164
David Chen1d7b0cd2017-11-15 14:20:04 -0800165 // Returns the memory in bytes currently used to store this metric's data. Does not change
166 // state.
Yangsterf2bee6f2017-11-29 12:01:05 -0800167 size_t byteSize() const {
168 std::lock_guard<std::mutex> lock(mMutex);
169 return byteSizeLocked();
170 }
yro69007c82017-10-26 20:42:57 -0700171
Bookatz1476ef22018-02-13 12:26:01 -0800172 /* If alert is valid, adds an AnomalyTracker and returns it. If invalid, returns nullptr. */
Yangster-mac932ecec2018-02-01 10:23:52 -0800173 virtual sp<AnomalyTracker> addAnomalyTracker(const Alert &alert,
174 const sp<AlarmMonitor>& anomalyAlarmMonitor) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800175 std::lock_guard<std::mutex> lock(mMutex);
Bookatz857aaa52017-12-19 15:29:06 -0800176 sp<AnomalyTracker> anomalyTracker = new AnomalyTracker(alert, mConfigKey);
177 if (anomalyTracker != nullptr) {
178 mAnomalyTrackers.push_back(anomalyTracker);
179 }
180 return anomalyTracker;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800181 }
182
183 int64_t getBuckeSizeInNs() const {
Yangsterf2bee6f2017-11-29 12:01:05 -0800184 std::lock_guard<std::mutex> lock(mMutex);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800185 return mBucketSizeNs;
186 }
187
yro59cc24d2018-02-13 20:17:32 -0800188 // Only needed for unit-testing to override guardrail.
189 void setBucketSize(int64_t bucketSize) {
190 mBucketSizeNs = bucketSize;
191 }
192
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800193 inline const int64_t& getMetricId() const {
Yangster-mac94e197c2018-01-02 16:03:03 -0800194 return mMetricId;
Yangster-mac20877162017-12-22 17:19:39 -0800195 }
196
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800197 int64_t getRemainingTtlNs(int64_t currentTimeNs) const {
198 std::lock_guard<std::mutex> lock(mMutex);
199 return getRemainingTtlNsLocked(currentTimeNs);
200 }
201
202 // Set metric to active for ttlNs.
203 void setActive(int64_t currentTimeNs, int64_t remainingTtlNs) {
204 std::lock_guard<std::mutex> lock(mMutex);
205 setActiveLocked(currentTimeNs, remainingTtlNs);
206 }
207
Yao Chen06dba5d2018-01-26 13:38:16 -0800208 // Let MetricProducer drop in-memory data to save memory.
209 // We still need to keep future data valid and anomaly tracking work, which means we will
210 // have to flush old data, informing anomaly trackers then safely drop old data.
211 // We still keep current bucket data for future metrics' validity.
Yangster-macb142cc82018-03-30 15:22:08 -0700212 void dropData(const int64_t dropTimeNs) {
Yao Chen06dba5d2018-01-26 13:38:16 -0800213 std::lock_guard<std::mutex> lock(mMutex);
214 dropDataLocked(dropTimeNs);
215 }
216
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700217 // For test only.
218 inline int64_t getCurrentBucketNum() const {
219 return mCurrentBucketNum;
220 }
221
Yangster-mac849dfdc22018-10-12 15:41:45 -0700222 void activate(int activationTrackerIndex, int64_t elapsedTimestampNs) {
223 std::lock_guard<std::mutex> lock(mMutex);
224 activateLocked(activationTrackerIndex, elapsedTimestampNs);
225 }
226
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800227 bool isActive() const {
228 std::lock_guard<std::mutex> lock(mMutex);
229 return isActiveLocked();
230 }
231
Chenjie Yua9a310e2019-02-06 13:40:10 -0800232 void prepActiveForBootIfNecessary(int64_t currentTimeNs) {
233 std::lock_guard<std::mutex> lock(mMutex);
234 prepActiveForBootIfNecessaryLocked(currentTimeNs);
235 }
236
Yangster-mac849dfdc22018-10-12 15:41:45 -0700237 void addActivation(int activationTrackerIndex, int64_t ttl_seconds);
238
Chenjie Yua9a310e2019-02-06 13:40:10 -0800239 inline void setActivationType(const MetricActivation::ActivationType& activationType) {
240 mActivationType = activationType;
241 }
242
Yangster-mac849dfdc22018-10-12 15:41:45 -0700243 void flushIfExpire(int64_t elapsedTimestampNs);
244
Yao Chen729093d2017-10-16 10:33:26 -0700245protected:
Yangster-macb142cc82018-03-30 15:22:08 -0700246 virtual void onConditionChangedLocked(const bool condition, const int64_t eventTime) = 0;
Yao Chen427d3722018-03-22 15:21:52 -0700247 virtual void onSlicedConditionMayChangeLocked(bool overallCondition,
Yangster-macb142cc82018-03-30 15:22:08 -0700248 const int64_t eventTime) = 0;
249 virtual void onDumpReportLocked(const int64_t dumpTimeNs,
Yangster-mace68f3a52018-04-04 00:01:43 -0700250 const bool include_current_partial_bucket,
Bookatzff71cad2018-09-20 17:17:49 -0700251 const bool erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000252 const DumpLatency dumpLatency,
Yangster-mac9def8e32018-04-17 13:55:51 -0700253 std::set<string> *str_set,
Yao Chen288c6002017-12-12 13:43:18 -0800254 android::util::ProtoOutputStream* protoOutput) = 0;
Yangster-maca802d732018-04-24 07:50:38 -0700255 virtual void clearPastBucketsLocked(const int64_t dumpTimeNs) = 0;
Yangsterf2bee6f2017-11-29 12:01:05 -0800256 virtual size_t byteSizeLocked() const = 0;
Yao Chen884c8c12018-01-26 10:36:25 -0800257 virtual void dumpStatesLocked(FILE* out, bool verbose) const = 0;
Yangsterf2bee6f2017-11-29 12:01:05 -0800258
Yangster-mac849dfdc22018-10-12 15:41:45 -0700259 bool evaluateActiveStateLocked(int64_t elapsedTimestampNs);
260
261 void activateLocked(int activationTrackerIndex, int64_t elapsedTimestampNs);
262
Yang Lub4722912018-11-15 11:02:03 -0800263 inline bool isActiveLocked() const {
264 return mIsActive;
265 }
266
Chenjie Yua9a310e2019-02-06 13:40:10 -0800267 void prepActiveForBootIfNecessaryLocked(int64_t currentTimeNs);
268
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800269 int64_t getRemainingTtlNsLocked(int64_t currentTimeNs) const;
270
271 void setActiveLocked(int64_t currentTimeNs, int64_t remainingTtlNs);
272
David Chen27785a82018-01-19 17:06:45 -0800273 /**
Yangster-mace68f3a52018-04-04 00:01:43 -0700274 * Flushes the current bucket if the eventTime is after the current bucket's end time. This will
275 also flush the current partial bucket in memory.
David Chen27785a82018-01-19 17:06:45 -0800276 */
Yangster-macb142cc82018-03-30 15:22:08 -0700277 virtual void flushIfNeededLocked(const int64_t& eventTime){};
David Chen27785a82018-01-19 17:06:45 -0800278
279 /**
Yangster-mace68f3a52018-04-04 00:01:43 -0700280 * Flushes all the data including the current partial bucket.
281 */
Yangster-mac849dfdc22018-10-12 15:41:45 -0700282 virtual void flushLocked(const int64_t& eventTimeNs) {
283 flushIfNeededLocked(eventTimeNs);
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000284 flushCurrentBucketLocked(eventTimeNs, eventTimeNs);
Yangster-mace68f3a52018-04-04 00:01:43 -0700285 };
286
287 /**
David Chen27785a82018-01-19 17:06:45 -0800288 * For metrics that aggregate (ie, every metric producer except for EventMetricProducer),
289 * we need to be able to flush the current buckets on demand (ie, end the current bucket and
290 * start new bucket). If this function is called when eventTimeNs is greater than the current
291 * bucket's end timestamp, than we flush up to the end of the latest full bucket; otherwise,
292 * we assume that we want to flush a partial bucket. The bucket start timestamp and bucket
293 * number are not changed by this function. This method should only be called by
294 * flushIfNeededLocked or the app upgrade handler; the caller MUST update the bucket timestamp
295 * and bucket number as needed.
296 */
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000297 virtual void flushCurrentBucketLocked(const int64_t& eventTimeNs,
298 const int64_t& nextBucketStartTimeNs) {};
David Chen27785a82018-01-19 17:06:45 -0800299
300 // Convenience to compute the current bucket's end time, which is always aligned with the
301 // start time of the metric.
Yangster-macb142cc82018-03-30 15:22:08 -0700302 int64_t getCurrentBucketEndTimeNs() const {
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700303 return mTimeBaseNs + (mCurrentBucketNum + 1) * mBucketSizeNs;
David Chen27785a82018-01-19 17:06:45 -0800304 }
305
Yangster-mac9def8e32018-04-17 13:55:51 -0700306 int64_t getBucketNumFromEndTimeNs(const int64_t endNs) {
307 return (endNs - mTimeBaseNs) / mBucketSizeNs - 1;
308 }
309
Yangster-macb142cc82018-03-30 15:22:08 -0700310 virtual void dropDataLocked(const int64_t dropTimeNs) = 0;
Yao Chen06dba5d2018-01-26 13:38:16 -0800311
Yangster-mac94e197c2018-01-02 16:03:03 -0800312 const int64_t mMetricId;
Yao Chenf09569f2017-12-13 17:00:51 -0800313
Yao Chenb3561512017-11-21 18:07:17 -0800314 const ConfigKey mConfigKey;
315
David Chen27785a82018-01-19 17:06:45 -0800316 // The time when this metric producer was first created. The end time for the current bucket
317 // can be computed from this based on mCurrentBucketNum.
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700318 int64_t mTimeBaseNs;
Yao Chen729093d2017-10-16 10:33:26 -0700319
David Chen27785a82018-01-19 17:06:45 -0800320 // Start time may not be aligned with the start of statsd if there is an app upgrade in the
321 // middle of a bucket.
Yangster-macb142cc82018-03-30 15:22:08 -0700322 int64_t mCurrentBucketStartTimeNs;
Yao Chen729093d2017-10-16 10:33:26 -0700323
David Chen27785a82018-01-19 17:06:45 -0800324 // Used by anomaly detector to track which bucket we are in. This is not sent with the produced
325 // report.
Yangster-macb142cc82018-03-30 15:22:08 -0700326 int64_t mCurrentBucketNum;
Yang Lu3eba6212017-10-25 19:54:45 -0700327
Yao Chen729093d2017-10-16 10:33:26 -0700328 int64_t mBucketSizeNs;
329
Olivier Gaillarde63d9e02019-02-12 14:43:59 +0000330 ConditionState mCondition;
Yao Chen729093d2017-10-16 10:33:26 -0700331
332 bool mConditionSliced;
333
334 sp<ConditionWizard> mWizard;
335
336 int mConditionTrackerIndex;
337
Yao Chen8a8d16c2018-02-08 14:50:40 -0800338 vector<Matcher> mDimensionsInWhat; // The dimensions_in_what defined in statsd_config
339 vector<Matcher> mDimensionsInCondition; // The dimensions_in_condition defined in statsd_config
Yao Chen729093d2017-10-16 10:33:26 -0700340
Yangster13fb7e42018-03-07 17:30:49 -0800341 bool mContainANYPositionInDimensionsInWhat;
Yangster-mac9def8e32018-04-17 13:55:51 -0700342 bool mSliceByPositionALL;
Yangster13fb7e42018-03-07 17:30:49 -0800343
344 // True iff the condition dimensions equal to the sliced dimensions in the simple condition
345 // tracker. This field is always false for combinational condition trackers.
346 bool mSameConditionDimensionsInTracker;
347
348 // True iff the metric to condition links cover all dimension fields in the condition tracker.
349 // This field is always false for combinational condition trackers.
350 bool mHasLinksToAllConditionDimensionsInTracker;
351
Yao Chen8a8d16c2018-02-08 14:50:40 -0800352 std::vector<Metric2Condition> mMetric2ConditionLinks;
Yao Chenb7041772017-10-20 16:59:25 -0700353
Yangster-mace2cd6d52017-11-09 20:38:30 -0800354 std::vector<sp<AnomalyTracker>> mAnomalyTrackers;
355
Yao Chenb7041772017-10-20 16:59:25 -0700356 /*
357 * Individual metrics can implement their own business logic here. All pre-processing is done.
358 *
359 * [matcherIndex]: the index of the matcher which matched this event. This is interesting to
360 * DurationMetric, because it has start/stop/stop_all 3 matchers.
361 * [eventKey]: the extracted dimension key for the final output. if the metric doesn't have
362 * dimensions, it will be DEFAULT_DIMENSION_KEY
363 * [conditionKey]: the keys of conditions which should be used to query the condition for this
Stefan Lafona5b51912017-12-05 21:43:52 -0800364 * target event (from MetricConditionLink). This is passed to individual metrics
Yao Chenb7041772017-10-20 16:59:25 -0700365 * because DurationMetric needs it to be cached.
366 * [condition]: whether condition is met. If condition is sliced, this is the result coming from
367 * query with ConditionWizard; If condition is not sliced, this is the
368 * nonSlicedCondition.
369 * [event]: the log event, just in case the metric needs its data, e.g., EventMetric.
370 */
Yangsterf2bee6f2017-11-29 12:01:05 -0800371 virtual void onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -0800372 const size_t matcherIndex, const MetricDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -0800373 const ConditionKey& conditionKey, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800374 const LogEvent& event) = 0;
yro2b0f8862017-11-06 14:27:31 -0800375
Yangsterf2bee6f2017-11-29 12:01:05 -0800376 // Consume the parsed stats log entry that already matched the "what" of the metric.
Yangster-mac53928882018-02-25 23:02:56 -0800377 virtual void onMatchedLogEventLocked(const size_t matcherIndex, const LogEvent& event);
Yangsterf2bee6f2017-11-29 12:01:05 -0800378
Yangsterf2bee6f2017-11-29 12:01:05 -0800379 mutable std::mutex mMutex;
Yangster-mac849dfdc22018-10-12 15:41:45 -0700380
381 struct Activation {
382 Activation() : ttl_ns(0), activation_ns(0), state(ActivationState::kNotActive) {}
383
384 int64_t ttl_ns;
385 int64_t activation_ns;
386 ActivationState state;
387 };
388 // When the metric producer has multiple activations, these activations are ORed to determine
389 // whether the metric producer is ready to generate metrics.
390 std::unordered_map<int, Activation> mEventActivationMap;
391
392 bool mIsActive;
393
Chenjie Yua9a310e2019-02-06 13:40:10 -0800394 MetricActivation::ActivationType mActivationType;
395
Yangster-mac849dfdc22018-10-12 15:41:45 -0700396 FRIEND_TEST(MetricActivationE2eTest, TestCountMetric);
Chenjie Yuc7939cb2019-02-04 17:25:45 -0800397
398 FRIEND_TEST(StatsLogProcessorTest, TestActiveConfigMetricDiskWriteRead);
Chenjie Yua9a310e2019-02-06 13:40:10 -0800399 FRIEND_TEST(StatsLogProcessorTest, TestActivationOnBoot);
Yao Chen44cf27c2017-09-14 22:32:50 -0700400};
401
402} // namespace statsd
403} // namespace os
404} // namespace android
405#endif // METRIC_PRODUCER_H