blob: 3779c4487d233778fab3619a9db27493625d11ef [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
David Chen1d7b0cd2017-11-15 14:20:04 -080099 // Returns the memory in bytes currently used to store this metric's data. Does not change
100 // state.
Yangsterf2bee6f2017-11-29 12:01:05 -0800101 size_t byteSize() const {
102 std::lock_guard<std::mutex> lock(mMutex);
103 return byteSizeLocked();
104 }
yro69007c82017-10-26 20:42:57 -0700105
Bookatz857aaa52017-12-19 15:29:06 -0800106 virtual sp<AnomalyTracker> addAnomalyTracker(const Alert &alert) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800107 std::lock_guard<std::mutex> lock(mMutex);
Bookatz857aaa52017-12-19 15:29:06 -0800108 sp<AnomalyTracker> anomalyTracker = new AnomalyTracker(alert, mConfigKey);
109 if (anomalyTracker != nullptr) {
110 mAnomalyTrackers.push_back(anomalyTracker);
111 }
112 return anomalyTracker;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800113 }
114
115 int64_t getBuckeSizeInNs() const {
Yangsterf2bee6f2017-11-29 12:01:05 -0800116 std::lock_guard<std::mutex> lock(mMutex);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800117 return mBucketSizeNs;
118 }
119
Yangster-mac94e197c2018-01-02 16:03:03 -0800120 inline const int64_t& getMetricId() {
121 return mMetricId;
Yangster-mac20877162017-12-22 17:19:39 -0800122 }
123
Yao Chen729093d2017-10-16 10:33:26 -0700124protected:
Yangsterf2bee6f2017-11-29 12:01:05 -0800125 virtual void onConditionChangedLocked(const bool condition, const uint64_t eventTime) = 0;
126 virtual void onSlicedConditionMayChangeLocked(const uint64_t eventTime) = 0;
Yao Chen288c6002017-12-12 13:43:18 -0800127 virtual void onDumpReportLocked(const uint64_t dumpTimeNs,
128 android::util::ProtoOutputStream* protoOutput) = 0;
Yangster-mac20877162017-12-22 17:19:39 -0800129 virtual void onDumpReportLocked(const uint64_t dumpTimeNs, StatsLogReport* report) = 0;
Yangsterf2bee6f2017-11-29 12:01:05 -0800130 virtual size_t byteSizeLocked() const = 0;
131
Yangster-mac94e197c2018-01-02 16:03:03 -0800132 const int64_t mMetricId;
Yao Chenf09569f2017-12-13 17:00:51 -0800133
Yao Chenb3561512017-11-21 18:07:17 -0800134 const ConfigKey mConfigKey;
135
Yao Chen288c6002017-12-12 13:43:18 -0800136 // The start time for the current in memory metrics data.
137 uint64_t mStartTimeNs;
Yao Chen729093d2017-10-16 10:33:26 -0700138
139 uint64_t mCurrentBucketStartTimeNs;
140
Yang Lu3eba6212017-10-25 19:54:45 -0700141 uint64_t mCurrentBucketNum;
142
Yao Chen729093d2017-10-16 10:33:26 -0700143 int64_t mBucketSizeNs;
144
145 bool mCondition;
146
147 bool mConditionSliced;
148
149 sp<ConditionWizard> mWizard;
150
151 int mConditionTrackerIndex;
152
Yangster-mac20877162017-12-22 17:19:39 -0800153 FieldMatcher mDimensions; // The dimension defined in statsd_config
Yao Chen729093d2017-10-16 10:33:26 -0700154
Stefan Lafona5b51912017-12-05 21:43:52 -0800155 std::vector<MetricConditionLink> mConditionLinks;
Yao Chenb7041772017-10-20 16:59:25 -0700156
Yangster-mace2cd6d52017-11-09 20:38:30 -0800157 std::vector<sp<AnomalyTracker>> mAnomalyTrackers;
158
Yao Chenb7041772017-10-20 16:59:25 -0700159 /*
160 * Individual metrics can implement their own business logic here. All pre-processing is done.
161 *
162 * [matcherIndex]: the index of the matcher which matched this event. This is interesting to
163 * DurationMetric, because it has start/stop/stop_all 3 matchers.
164 * [eventKey]: the extracted dimension key for the final output. if the metric doesn't have
165 * dimensions, it will be DEFAULT_DIMENSION_KEY
166 * [conditionKey]: the keys of conditions which should be used to query the condition for this
Stefan Lafona5b51912017-12-05 21:43:52 -0800167 * target event (from MetricConditionLink). This is passed to individual metrics
Yao Chenb7041772017-10-20 16:59:25 -0700168 * because DurationMetric needs it to be cached.
169 * [condition]: whether condition is met. If condition is sliced, this is the result coming from
170 * query with ConditionWizard; If condition is not sliced, this is the
171 * nonSlicedCondition.
172 * [event]: the log event, just in case the metric needs its data, e.g., EventMetric.
173 */
Yangsterf2bee6f2017-11-29 12:01:05 -0800174 virtual void onMatchedLogEventInternalLocked(
Yao Chenb7041772017-10-20 16:59:25 -0700175 const size_t matcherIndex, const HashableDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -0800176 const ConditionKey& conditionKey, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800177 const LogEvent& event) = 0;
yro2b0f8862017-11-06 14:27:31 -0800178
Yangsterf2bee6f2017-11-29 12:01:05 -0800179 // Consume the parsed stats log entry that already matched the "what" of the metric.
Chenjie Yua7259ab2017-12-10 08:31:05 -0800180 void onMatchedLogEventLocked(const size_t matcherIndex, const LogEvent& event);
Yangsterf2bee6f2017-11-29 12:01:05 -0800181
Yangsterf2bee6f2017-11-29 12:01:05 -0800182 mutable std::mutex mMutex;
Yao Chen44cf27c2017-09-14 22:32:50 -0700183};
184
185} // namespace statsd
186} // namespace os
187} // namespace android
188#endif // METRIC_PRODUCER_H