blob: 4ee94718de927f9e8d3fe0f01548bfeffdf9a613 [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
37// A MetricProducer is responsible for compute one single metrics, creating stats log report, and
David Chende701692017-10-05 13:16:02 -070038// writing the report to dropbox. MetricProducers should respond to package changes as required in
39// PackageInfoListener, but if none of the metrics are slicing by package name, then the update can
40// be a no-op.
David Chenf12b5c62017-10-13 15:54:03 -070041class MetricProducer : public virtual PackageInfoListener {
Yao Chen44cf27c2017-09-14 22:32:50 -070042public:
Yangster-mac94e197c2018-01-02 16:03:03 -080043 MetricProducer(const int64_t& metricId, const ConfigKey& key, const int64_t startTimeNs,
Yao Chenf09569f2017-12-13 17:00:51 -080044 const int conditionIndex, const sp<ConditionWizard>& wizard)
Yangster-mac94e197c2018-01-02 16:03:03 -080045 : mMetricId(metricId),
Yao Chenf09569f2017-12-13 17:00:51 -080046 mConfigKey(key),
Yao Chenb3561512017-11-21 18:07:17 -080047 mStartTimeNs(startTimeNs),
Yao Chen729093d2017-10-16 10:33:26 -070048 mCurrentBucketStartTimeNs(startTimeNs),
Yang Lu3eba6212017-10-25 19:54:45 -070049 mCurrentBucketNum(0),
Yao Chen729093d2017-10-16 10:33:26 -070050 mCondition(conditionIndex >= 0 ? false : true),
Yao Chen93fe3a32017-11-02 13:52:59 -070051 mConditionSliced(false),
Yao Chen729093d2017-10-16 10:33:26 -070052 mWizard(wizard),
Yao Chend5aa01b32017-12-19 16:46:36 -080053 mConditionTrackerIndex(conditionIndex){};
Yangster-mac20877162017-12-22 17:19:39 -080054
Yao Chen44cf27c2017-09-14 22:32:50 -070055 virtual ~MetricProducer(){};
56
David Chen27785a82018-01-19 17:06:45 -080057 /**
58 * Forces this metric to split into a partial bucket right now. If we're past a full bucket, we
59 * first call the standard flushing code to flush up to the latest full bucket. Then we call
60 * the flush again when the end timestamp is forced to be now, and then after flushing, update
61 * the start timestamp to be now.
62 */
63 void notifyAppUpgrade(const uint64_t& eventTimeNs, const string& apk, const int uid,
64 const int64_t version) override {
65 std::lock_guard<std::mutex> lock(mMutex);
66
67 if (eventTimeNs > getCurrentBucketEndTimeNs()) {
68 // Flush full buckets on the normal path up to the latest bucket boundary.
69 flushIfNeededLocked(eventTimeNs);
70 }
71 // Now flush a partial bucket.
72 flushCurrentBucketLocked(eventTimeNs);
73 mCurrentBucketStartTimeNs = eventTimeNs;
74 // Don't update the current bucket number so that the anomaly tracker knows this bucket
75 // is a partial bucket and can merge it with the previous bucket.
76 };
77
78 void notifyAppRemoved(const uint64_t& eventTimeNs, const string& apk, const int uid) override{
Yao Chend10f7b12017-12-18 12:53:50 -080079 // TODO: Implement me.
80 };
81
David Chen27785a82018-01-19 17:06:45 -080082 void onUidMapReceived(const uint64_t& eventTimeNs) override{
Yao Chend10f7b12017-12-18 12:53:50 -080083 // TODO: Implement me.
84 };
85
Yao Chencaf339d2017-10-06 16:01:10 -070086 // Consume the parsed stats log entry that already matched the "what" of the metric.
Chenjie Yua7259ab2017-12-10 08:31:05 -080087 void onMatchedLogEvent(const size_t matcherIndex, const LogEvent& event) {
Yangsterf2bee6f2017-11-29 12:01:05 -080088 std::lock_guard<std::mutex> lock(mMutex);
Chenjie Yua7259ab2017-12-10 08:31:05 -080089 onMatchedLogEventLocked(matcherIndex, event);
Yangsterf2bee6f2017-11-29 12:01:05 -080090 }
Yao Chen44cf27c2017-09-14 22:32:50 -070091
Yangsterf2bee6f2017-11-29 12:01:05 -080092 void onConditionChanged(const bool condition, const uint64_t eventTime) {
93 std::lock_guard<std::mutex> lock(mMutex);
94 onConditionChangedLocked(condition, eventTime);
95 }
Yao Chencaf339d2017-10-06 16:01:10 -070096
Yangsterf2bee6f2017-11-29 12:01:05 -080097 void onSlicedConditionMayChange(const uint64_t eventTime) {
98 std::lock_guard<std::mutex> lock(mMutex);
99 onSlicedConditionMayChangeLocked(eventTime);
100 }
101
102 bool isConditionSliced() const {
103 std::lock_guard<std::mutex> lock(mMutex);
104 return mConditionSliced;
105 };
Yao Chen729093d2017-10-16 10:33:26 -0700106
Yao Chen288c6002017-12-12 13:43:18 -0800107 // Output the metrics data to [protoOutput]. All metrics reports end with the same timestamp.
David Chen27785a82018-01-19 17:06:45 -0800108 // This method clears all the past buckets.
Yao Chen288c6002017-12-12 13:43:18 -0800109 void onDumpReport(const uint64_t dumpTimeNs, android::util::ProtoOutputStream* protoOutput) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800110 std::lock_guard<std::mutex> lock(mMutex);
Yao Chen288c6002017-12-12 13:43:18 -0800111 return onDumpReportLocked(dumpTimeNs, protoOutput);
Yangsterf2bee6f2017-11-29 12:01:05 -0800112 }
Yao Chen729093d2017-10-16 10:33:26 -0700113
Yao Chen884c8c12018-01-26 10:36:25 -0800114 void dumpStates(FILE* out, bool verbose) const {
115 std::lock_guard<std::mutex> lock(mMutex);
116 dumpStatesLocked(out, verbose);
117 }
118
David Chen1d7b0cd2017-11-15 14:20:04 -0800119 // Returns the memory in bytes currently used to store this metric's data. Does not change
120 // state.
Yangsterf2bee6f2017-11-29 12:01:05 -0800121 size_t byteSize() const {
122 std::lock_guard<std::mutex> lock(mMutex);
123 return byteSizeLocked();
124 }
yro69007c82017-10-26 20:42:57 -0700125
Bookatz1476ef22018-02-13 12:26:01 -0800126 /* If alert is valid, adds an AnomalyTracker and returns it. If invalid, returns nullptr. */
Bookatz857aaa52017-12-19 15:29:06 -0800127 virtual sp<AnomalyTracker> addAnomalyTracker(const Alert &alert) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800128 std::lock_guard<std::mutex> lock(mMutex);
Bookatz857aaa52017-12-19 15:29:06 -0800129 sp<AnomalyTracker> anomalyTracker = new AnomalyTracker(alert, mConfigKey);
130 if (anomalyTracker != nullptr) {
131 mAnomalyTrackers.push_back(anomalyTracker);
132 }
133 return anomalyTracker;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800134 }
135
136 int64_t getBuckeSizeInNs() const {
Yangsterf2bee6f2017-11-29 12:01:05 -0800137 std::lock_guard<std::mutex> lock(mMutex);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800138 return mBucketSizeNs;
139 }
140
Yangster-mac94e197c2018-01-02 16:03:03 -0800141 inline const int64_t& getMetricId() {
142 return mMetricId;
Yangster-mac20877162017-12-22 17:19:39 -0800143 }
144
Yao Chen729093d2017-10-16 10:33:26 -0700145protected:
Yangsterf2bee6f2017-11-29 12:01:05 -0800146 virtual void onConditionChangedLocked(const bool condition, const uint64_t eventTime) = 0;
147 virtual void onSlicedConditionMayChangeLocked(const uint64_t eventTime) = 0;
Yao Chen288c6002017-12-12 13:43:18 -0800148 virtual void onDumpReportLocked(const uint64_t dumpTimeNs,
149 android::util::ProtoOutputStream* protoOutput) = 0;
Yangsterf2bee6f2017-11-29 12:01:05 -0800150 virtual size_t byteSizeLocked() const = 0;
Yao Chen884c8c12018-01-26 10:36:25 -0800151 virtual void dumpStatesLocked(FILE* out, bool verbose) const = 0;
Yangsterf2bee6f2017-11-29 12:01:05 -0800152
David Chen27785a82018-01-19 17:06:45 -0800153 /**
154 * Flushes the current bucket if the eventTime is after the current bucket's end time.
155 */
156 virtual void flushIfNeededLocked(const uint64_t& eventTime){};
157
158 /**
159 * For metrics that aggregate (ie, every metric producer except for EventMetricProducer),
160 * we need to be able to flush the current buckets on demand (ie, end the current bucket and
161 * start new bucket). If this function is called when eventTimeNs is greater than the current
162 * bucket's end timestamp, than we flush up to the end of the latest full bucket; otherwise,
163 * we assume that we want to flush a partial bucket. The bucket start timestamp and bucket
164 * number are not changed by this function. This method should only be called by
165 * flushIfNeededLocked or the app upgrade handler; the caller MUST update the bucket timestamp
166 * and bucket number as needed.
167 */
168 virtual void flushCurrentBucketLocked(const uint64_t& eventTimeNs){};
169
170 // Convenience to compute the current bucket's end time, which is always aligned with the
171 // start time of the metric.
172 uint64_t getCurrentBucketEndTimeNs() {
173 return mStartTimeNs + (mCurrentBucketNum + 1) * mBucketSizeNs;
174 }
175
Yangster-mac94e197c2018-01-02 16:03:03 -0800176 const int64_t mMetricId;
Yao Chenf09569f2017-12-13 17:00:51 -0800177
Yao Chenb3561512017-11-21 18:07:17 -0800178 const ConfigKey mConfigKey;
179
David Chen27785a82018-01-19 17:06:45 -0800180 // The time when this metric producer was first created. The end time for the current bucket
181 // can be computed from this based on mCurrentBucketNum.
Yao Chen288c6002017-12-12 13:43:18 -0800182 uint64_t mStartTimeNs;
Yao Chen729093d2017-10-16 10:33:26 -0700183
David Chen27785a82018-01-19 17:06:45 -0800184 // Start time may not be aligned with the start of statsd if there is an app upgrade in the
185 // middle of a bucket.
Yao Chen729093d2017-10-16 10:33:26 -0700186 uint64_t mCurrentBucketStartTimeNs;
187
David Chen27785a82018-01-19 17:06:45 -0800188 // Used by anomaly detector to track which bucket we are in. This is not sent with the produced
189 // report.
Yang Lu3eba6212017-10-25 19:54:45 -0700190 uint64_t mCurrentBucketNum;
191
Yao Chen729093d2017-10-16 10:33:26 -0700192 int64_t mBucketSizeNs;
193
194 bool mCondition;
195
196 bool mConditionSliced;
197
198 sp<ConditionWizard> mWizard;
199
200 int mConditionTrackerIndex;
201
Yao Chen8a8d16c2018-02-08 14:50:40 -0800202 vector<Matcher> mDimensionsInWhat; // The dimensions_in_what defined in statsd_config
203 vector<Matcher> mDimensionsInCondition; // The dimensions_in_condition defined in statsd_config
Yao Chen729093d2017-10-16 10:33:26 -0700204
Yao Chen8a8d16c2018-02-08 14:50:40 -0800205 std::vector<Metric2Condition> mMetric2ConditionLinks;
Yao Chenb7041772017-10-20 16:59:25 -0700206
Yangster-mace2cd6d52017-11-09 20:38:30 -0800207 std::vector<sp<AnomalyTracker>> mAnomalyTrackers;
208
Yao Chenb7041772017-10-20 16:59:25 -0700209 /*
210 * Individual metrics can implement their own business logic here. All pre-processing is done.
211 *
212 * [matcherIndex]: the index of the matcher which matched this event. This is interesting to
213 * DurationMetric, because it has start/stop/stop_all 3 matchers.
214 * [eventKey]: the extracted dimension key for the final output. if the metric doesn't have
215 * dimensions, it will be DEFAULT_DIMENSION_KEY
216 * [conditionKey]: the keys of conditions which should be used to query the condition for this
Stefan Lafona5b51912017-12-05 21:43:52 -0800217 * target event (from MetricConditionLink). This is passed to individual metrics
Yao Chenb7041772017-10-20 16:59:25 -0700218 * because DurationMetric needs it to be cached.
219 * [condition]: whether condition is met. If condition is sliced, this is the result coming from
220 * query with ConditionWizard; If condition is not sliced, this is the
221 * nonSlicedCondition.
222 * [event]: the log event, just in case the metric needs its data, e.g., EventMetric.
223 */
Yangsterf2bee6f2017-11-29 12:01:05 -0800224 virtual void onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -0800225 const size_t matcherIndex, const MetricDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -0800226 const ConditionKey& conditionKey, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800227 const LogEvent& event) = 0;
yro2b0f8862017-11-06 14:27:31 -0800228
Yangsterf2bee6f2017-11-29 12:01:05 -0800229 // Consume the parsed stats log entry that already matched the "what" of the metric.
Chenjie Yua7259ab2017-12-10 08:31:05 -0800230 void onMatchedLogEventLocked(const size_t matcherIndex, const LogEvent& event);
Yangsterf2bee6f2017-11-29 12:01:05 -0800231
Yangsterf2bee6f2017-11-29 12:01:05 -0800232 mutable std::mutex mMutex;
Yao Chen44cf27c2017-09-14 22:32:50 -0700233};
234
235} // namespace statsd
236} // namespace os
237} // namespace android
238#endif // METRIC_PRODUCER_H