blob: 6f33073c633c945a2c9944922e4a0f51b143cb77 [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
Yangster-mace2cd6d52017-11-09 20:38:30 -080022#include "anomaly/AnomalyTracker.h"
Yao Chen729093d2017-10-16 10:33:26 -070023#include "condition/ConditionWizard.h"
Yao Chenb3561512017-11-21 18:07:17 -080024#include "config/ConfigKey.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070025#include "matchers/matcher_util.h"
26#include "packages/PackageInfoListener.h"
27
Yao Chen44cf27c2017-09-14 22:32:50 -070028#include <log/logprint.h>
Yao Chencaf339d2017-10-06 16:01:10 -070029#include <utils/RefBase.h>
Yao Chend10f7b12017-12-18 12:53:50 -080030#include <unordered_map>
Yao Chen44cf27c2017-09-14 22:32:50 -070031
32namespace android {
33namespace os {
34namespace statsd {
35
36// A MetricProducer is responsible for compute one single metrics, creating stats log report, and
David Chende701692017-10-05 13:16:02 -070037// writing the report to dropbox. MetricProducers should respond to package changes as required in
38// PackageInfoListener, but if none of the metrics are slicing by package name, then the update can
39// be a no-op.
David Chenf12b5c62017-10-13 15:54:03 -070040class MetricProducer : public virtual PackageInfoListener {
Yao Chen44cf27c2017-09-14 22:32:50 -070041public:
Yangster-mac94e197c2018-01-02 16:03:03 -080042 MetricProducer(const int64_t& metricId, const ConfigKey& key, const int64_t startTimeNs,
Yao Chenf09569f2017-12-13 17:00:51 -080043 const int conditionIndex, const sp<ConditionWizard>& wizard)
Yangster-mac94e197c2018-01-02 16:03:03 -080044 : mMetricId(metricId),
Yao Chenf09569f2017-12-13 17:00:51 -080045 mConfigKey(key),
Yao Chenb3561512017-11-21 18:07:17 -080046 mStartTimeNs(startTimeNs),
Yao Chen729093d2017-10-16 10:33:26 -070047 mCurrentBucketStartTimeNs(startTimeNs),
Yang Lu3eba6212017-10-25 19:54:45 -070048 mCurrentBucketNum(0),
Yao Chen729093d2017-10-16 10:33:26 -070049 mCondition(conditionIndex >= 0 ? false : true),
Yao Chen93fe3a32017-11-02 13:52:59 -070050 mConditionSliced(false),
Yao Chen729093d2017-10-16 10:33:26 -070051 mWizard(wizard),
Yao Chend5aa01b32017-12-19 16:46:36 -080052 mConditionTrackerIndex(conditionIndex){};
Yangster-mac20877162017-12-22 17:19:39 -080053
Yao Chen44cf27c2017-09-14 22:32:50 -070054 virtual ~MetricProducer(){};
55
Yao Chend10f7b12017-12-18 12:53:50 -080056 void notifyAppUpgrade(const string& apk, const int uid, const int64_t version) override{
57 // TODO: Implement me.
58 };
59
60 void notifyAppRemoved(const string& apk, const int uid) override{
61 // TODO: Implement me.
62 };
63
64 void onUidMapReceived() override{
65 // TODO: Implement me.
66 };
67
Yao Chencaf339d2017-10-06 16:01:10 -070068 // Consume the parsed stats log entry that already matched the "what" of the metric.
Chenjie Yua7259ab2017-12-10 08:31:05 -080069 void onMatchedLogEvent(const size_t matcherIndex, const LogEvent& event) {
Yangsterf2bee6f2017-11-29 12:01:05 -080070 std::lock_guard<std::mutex> lock(mMutex);
Chenjie Yua7259ab2017-12-10 08:31:05 -080071 onMatchedLogEventLocked(matcherIndex, event);
Yangsterf2bee6f2017-11-29 12:01:05 -080072 }
Yao Chen44cf27c2017-09-14 22:32:50 -070073
Yangsterf2bee6f2017-11-29 12:01:05 -080074 void onConditionChanged(const bool condition, const uint64_t eventTime) {
75 std::lock_guard<std::mutex> lock(mMutex);
76 onConditionChangedLocked(condition, eventTime);
77 }
Yao Chencaf339d2017-10-06 16:01:10 -070078
Yangsterf2bee6f2017-11-29 12:01:05 -080079 void onSlicedConditionMayChange(const uint64_t eventTime) {
80 std::lock_guard<std::mutex> lock(mMutex);
81 onSlicedConditionMayChangeLocked(eventTime);
82 }
83
84 bool isConditionSliced() const {
85 std::lock_guard<std::mutex> lock(mMutex);
86 return mConditionSliced;
87 };
Yao Chen729093d2017-10-16 10:33:26 -070088
Yao Chen288c6002017-12-12 13:43:18 -080089 // Output the metrics data to [protoOutput]. All metrics reports end with the same timestamp.
90 void onDumpReport(const uint64_t dumpTimeNs, android::util::ProtoOutputStream* protoOutput) {
Yangsterf2bee6f2017-11-29 12:01:05 -080091 std::lock_guard<std::mutex> lock(mMutex);
Yao Chen288c6002017-12-12 13:43:18 -080092 return onDumpReportLocked(dumpTimeNs, protoOutput);
Yangsterf2bee6f2017-11-29 12:01:05 -080093 }
Yangster-mac20877162017-12-22 17:19:39 -080094 void onDumpReport(const uint64_t dumpTimeNs, StatsLogReport* report) {
95 std::lock_guard<std::mutex> lock(mMutex);
96 return onDumpReportLocked(dumpTimeNs, report);
97 }
Yao Chen729093d2017-10-16 10:33:26 -070098
Yao Chen884c8c12018-01-26 10:36:25 -080099 void dumpStates(FILE* out, bool verbose) const {
100 std::lock_guard<std::mutex> lock(mMutex);
101 dumpStatesLocked(out, verbose);
102 }
103
David Chen1d7b0cd2017-11-15 14:20:04 -0800104 // Returns the memory in bytes currently used to store this metric's data. Does not change
105 // state.
Yangsterf2bee6f2017-11-29 12:01:05 -0800106 size_t byteSize() const {
107 std::lock_guard<std::mutex> lock(mMutex);
108 return byteSizeLocked();
109 }
yro69007c82017-10-26 20:42:57 -0700110
Bookatz857aaa52017-12-19 15:29:06 -0800111 virtual sp<AnomalyTracker> addAnomalyTracker(const Alert &alert) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800112 std::lock_guard<std::mutex> lock(mMutex);
Bookatz857aaa52017-12-19 15:29:06 -0800113 sp<AnomalyTracker> anomalyTracker = new AnomalyTracker(alert, mConfigKey);
114 if (anomalyTracker != nullptr) {
115 mAnomalyTrackers.push_back(anomalyTracker);
116 }
117 return anomalyTracker;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800118 }
119
120 int64_t getBuckeSizeInNs() const {
Yangsterf2bee6f2017-11-29 12:01:05 -0800121 std::lock_guard<std::mutex> lock(mMutex);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800122 return mBucketSizeNs;
123 }
124
Yangster-mac94e197c2018-01-02 16:03:03 -0800125 inline const int64_t& getMetricId() {
126 return mMetricId;
Yangster-mac20877162017-12-22 17:19:39 -0800127 }
128
Yao Chen729093d2017-10-16 10:33:26 -0700129protected:
Yangsterf2bee6f2017-11-29 12:01:05 -0800130 virtual void onConditionChangedLocked(const bool condition, const uint64_t eventTime) = 0;
131 virtual void onSlicedConditionMayChangeLocked(const uint64_t eventTime) = 0;
Yao Chen288c6002017-12-12 13:43:18 -0800132 virtual void onDumpReportLocked(const uint64_t dumpTimeNs,
133 android::util::ProtoOutputStream* protoOutput) = 0;
Yangster-mac20877162017-12-22 17:19:39 -0800134 virtual void onDumpReportLocked(const uint64_t dumpTimeNs, StatsLogReport* report) = 0;
Yangsterf2bee6f2017-11-29 12:01:05 -0800135 virtual size_t byteSizeLocked() const = 0;
Yao Chen884c8c12018-01-26 10:36:25 -0800136 virtual void dumpStatesLocked(FILE* out, bool verbose) const = 0;
Yangsterf2bee6f2017-11-29 12:01:05 -0800137
Yangster-mac94e197c2018-01-02 16:03:03 -0800138 const int64_t mMetricId;
Yao Chenf09569f2017-12-13 17:00:51 -0800139
Yao Chenb3561512017-11-21 18:07:17 -0800140 const ConfigKey mConfigKey;
141
Yao Chen288c6002017-12-12 13:43:18 -0800142 // The start time for the current in memory metrics data.
143 uint64_t mStartTimeNs;
Yao Chen729093d2017-10-16 10:33:26 -0700144
145 uint64_t mCurrentBucketStartTimeNs;
146
Yang Lu3eba6212017-10-25 19:54:45 -0700147 uint64_t mCurrentBucketNum;
148
Yao Chen729093d2017-10-16 10:33:26 -0700149 int64_t mBucketSizeNs;
150
151 bool mCondition;
152
153 bool mConditionSliced;
154
155 sp<ConditionWizard> mWizard;
156
157 int mConditionTrackerIndex;
158
Yangster-mac20877162017-12-22 17:19:39 -0800159 FieldMatcher mDimensions; // The dimension defined in statsd_config
Yao Chen729093d2017-10-16 10:33:26 -0700160
Stefan Lafona5b51912017-12-05 21:43:52 -0800161 std::vector<MetricConditionLink> mConditionLinks;
Yao Chenb7041772017-10-20 16:59:25 -0700162
Yangster-mace2cd6d52017-11-09 20:38:30 -0800163 std::vector<sp<AnomalyTracker>> mAnomalyTrackers;
164
Yao Chenb7041772017-10-20 16:59:25 -0700165 /*
166 * Individual metrics can implement their own business logic here. All pre-processing is done.
167 *
168 * [matcherIndex]: the index of the matcher which matched this event. This is interesting to
169 * DurationMetric, because it has start/stop/stop_all 3 matchers.
170 * [eventKey]: the extracted dimension key for the final output. if the metric doesn't have
171 * dimensions, it will be DEFAULT_DIMENSION_KEY
172 * [conditionKey]: the keys of conditions which should be used to query the condition for this
Stefan Lafona5b51912017-12-05 21:43:52 -0800173 * target event (from MetricConditionLink). This is passed to individual metrics
Yao Chenb7041772017-10-20 16:59:25 -0700174 * because DurationMetric needs it to be cached.
175 * [condition]: whether condition is met. If condition is sliced, this is the result coming from
176 * query with ConditionWizard; If condition is not sliced, this is the
177 * nonSlicedCondition.
178 * [event]: the log event, just in case the metric needs its data, e.g., EventMetric.
179 */
Yangsterf2bee6f2017-11-29 12:01:05 -0800180 virtual void onMatchedLogEventInternalLocked(
Yao Chenb7041772017-10-20 16:59:25 -0700181 const size_t matcherIndex, const HashableDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -0800182 const ConditionKey& conditionKey, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800183 const LogEvent& event) = 0;
yro2b0f8862017-11-06 14:27:31 -0800184
Yangsterf2bee6f2017-11-29 12:01:05 -0800185 // Consume the parsed stats log entry that already matched the "what" of the metric.
Chenjie Yua7259ab2017-12-10 08:31:05 -0800186 void onMatchedLogEventLocked(const size_t matcherIndex, const LogEvent& event);
Yangsterf2bee6f2017-11-29 12:01:05 -0800187
Yangsterf2bee6f2017-11-29 12:01:05 -0800188 mutable std::mutex mMutex;
Yao Chen44cf27c2017-09-14 22:32:50 -0700189};
190
191} // namespace statsd
192} // namespace os
193} // namespace android
194#endif // METRIC_PRODUCER_H