blob: 3b1498f076329d3d17cb3664816ff2819bd487a7 [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-mac93694462018-01-22 20:49:31 -080094
Yangster-mac20877162017-12-22 17:19:39 -080095 void onDumpReport(const uint64_t dumpTimeNs, StatsLogReport* report) {
96 std::lock_guard<std::mutex> lock(mMutex);
97 return onDumpReportLocked(dumpTimeNs, report);
98 }
Yao Chen729093d2017-10-16 10:33:26 -070099
Yao Chen884c8c12018-01-26 10:36:25 -0800100 void dumpStates(FILE* out, bool verbose) const {
101 std::lock_guard<std::mutex> lock(mMutex);
102 dumpStatesLocked(out, verbose);
103 }
104
David Chen1d7b0cd2017-11-15 14:20:04 -0800105 // Returns the memory in bytes currently used to store this metric's data. Does not change
106 // state.
Yangsterf2bee6f2017-11-29 12:01:05 -0800107 size_t byteSize() const {
108 std::lock_guard<std::mutex> lock(mMutex);
109 return byteSizeLocked();
110 }
yro69007c82017-10-26 20:42:57 -0700111
Bookatz857aaa52017-12-19 15:29:06 -0800112 virtual sp<AnomalyTracker> addAnomalyTracker(const Alert &alert) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800113 std::lock_guard<std::mutex> lock(mMutex);
Bookatz857aaa52017-12-19 15:29:06 -0800114 sp<AnomalyTracker> anomalyTracker = new AnomalyTracker(alert, mConfigKey);
115 if (anomalyTracker != nullptr) {
116 mAnomalyTrackers.push_back(anomalyTracker);
117 }
118 return anomalyTracker;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800119 }
120
121 int64_t getBuckeSizeInNs() const {
Yangsterf2bee6f2017-11-29 12:01:05 -0800122 std::lock_guard<std::mutex> lock(mMutex);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800123 return mBucketSizeNs;
124 }
125
Yangster-mac94e197c2018-01-02 16:03:03 -0800126 inline const int64_t& getMetricId() {
127 return mMetricId;
Yangster-mac20877162017-12-22 17:19:39 -0800128 }
129
Yao Chen729093d2017-10-16 10:33:26 -0700130protected:
Yangsterf2bee6f2017-11-29 12:01:05 -0800131 virtual void onConditionChangedLocked(const bool condition, const uint64_t eventTime) = 0;
132 virtual void onSlicedConditionMayChangeLocked(const uint64_t eventTime) = 0;
Yao Chen288c6002017-12-12 13:43:18 -0800133 virtual void onDumpReportLocked(const uint64_t dumpTimeNs,
134 android::util::ProtoOutputStream* protoOutput) = 0;
Yangster-mac20877162017-12-22 17:19:39 -0800135 virtual void onDumpReportLocked(const uint64_t dumpTimeNs, StatsLogReport* report) = 0;
Yangsterf2bee6f2017-11-29 12:01:05 -0800136 virtual size_t byteSizeLocked() const = 0;
Yao Chen884c8c12018-01-26 10:36:25 -0800137 virtual void dumpStatesLocked(FILE* out, bool verbose) const = 0;
Yangsterf2bee6f2017-11-29 12:01:05 -0800138
Yangster-mac94e197c2018-01-02 16:03:03 -0800139 const int64_t mMetricId;
Yao Chenf09569f2017-12-13 17:00:51 -0800140
Yao Chenb3561512017-11-21 18:07:17 -0800141 const ConfigKey mConfigKey;
142
Yao Chen288c6002017-12-12 13:43:18 -0800143 // The start time for the current in memory metrics data.
144 uint64_t mStartTimeNs;
Yao Chen729093d2017-10-16 10:33:26 -0700145
146 uint64_t mCurrentBucketStartTimeNs;
147
Yang Lu3eba6212017-10-25 19:54:45 -0700148 uint64_t mCurrentBucketNum;
149
Yao Chen729093d2017-10-16 10:33:26 -0700150 int64_t mBucketSizeNs;
151
152 bool mCondition;
153
154 bool mConditionSliced;
155
156 sp<ConditionWizard> mWizard;
157
158 int mConditionTrackerIndex;
159
Yangster-mac93694462018-01-22 20:49:31 -0800160 FieldMatcher mDimensionsInWhat; // The dimensions_in_what defined in statsd_config
161 FieldMatcher mDimensionsInCondition; // The dimensions_in_condition defined in statsd_config
Yao Chen729093d2017-10-16 10:33:26 -0700162
Stefan Lafona5b51912017-12-05 21:43:52 -0800163 std::vector<MetricConditionLink> mConditionLinks;
Yao Chenb7041772017-10-20 16:59:25 -0700164
Yangster-mace2cd6d52017-11-09 20:38:30 -0800165 std::vector<sp<AnomalyTracker>> mAnomalyTrackers;
166
Yao Chenb7041772017-10-20 16:59:25 -0700167 /*
168 * Individual metrics can implement their own business logic here. All pre-processing is done.
169 *
170 * [matcherIndex]: the index of the matcher which matched this event. This is interesting to
171 * DurationMetric, because it has start/stop/stop_all 3 matchers.
172 * [eventKey]: the extracted dimension key for the final output. if the metric doesn't have
173 * dimensions, it will be DEFAULT_DIMENSION_KEY
174 * [conditionKey]: the keys of conditions which should be used to query the condition for this
Stefan Lafona5b51912017-12-05 21:43:52 -0800175 * target event (from MetricConditionLink). This is passed to individual metrics
Yao Chenb7041772017-10-20 16:59:25 -0700176 * because DurationMetric needs it to be cached.
177 * [condition]: whether condition is met. If condition is sliced, this is the result coming from
178 * query with ConditionWizard; If condition is not sliced, this is the
179 * nonSlicedCondition.
180 * [event]: the log event, just in case the metric needs its data, e.g., EventMetric.
181 */
Yangsterf2bee6f2017-11-29 12:01:05 -0800182 virtual void onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -0800183 const size_t matcherIndex, const MetricDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -0800184 const ConditionKey& conditionKey, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800185 const LogEvent& event) = 0;
yro2b0f8862017-11-06 14:27:31 -0800186
Yangsterf2bee6f2017-11-29 12:01:05 -0800187 // Consume the parsed stats log entry that already matched the "what" of the metric.
Chenjie Yua7259ab2017-12-10 08:31:05 -0800188 void onMatchedLogEventLocked(const size_t matcherIndex, const LogEvent& event);
Yangsterf2bee6f2017-11-29 12:01:05 -0800189
Yangsterf2bee6f2017-11-29 12:01:05 -0800190 mutable std::mutex mMutex;
Yao Chen44cf27c2017-09-14 22:32:50 -0700191};
192
193} // namespace statsd
194} // namespace os
195} // namespace android
196#endif // METRIC_PRODUCER_H