blob: e7e84ab1dcc80a91cdf86a78ed3ef53bbb308f17 [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 Chen44cf27c2017-09-14 22:32:50 -070030
31namespace android {
32namespace os {
33namespace statsd {
34
35// A MetricProducer is responsible for compute one single metrics, creating stats log report, and
David Chende701692017-10-05 13:16:02 -070036// writing the report to dropbox. MetricProducers should respond to package changes as required in
37// PackageInfoListener, but if none of the metrics are slicing by package name, then the update can
38// be a no-op.
David Chenf12b5c62017-10-13 15:54:03 -070039class MetricProducer : public virtual PackageInfoListener {
Yao Chen44cf27c2017-09-14 22:32:50 -070040public:
Yao Chenb3561512017-11-21 18:07:17 -080041 MetricProducer(const ConfigKey& key, const int64_t startTimeNs, const int conditionIndex,
Yao Chen729093d2017-10-16 10:33:26 -070042 const sp<ConditionWizard>& wizard)
Yao Chenb3561512017-11-21 18:07:17 -080043 : mConfigKey(key),
44 mStartTimeNs(startTimeNs),
Yao Chen729093d2017-10-16 10:33:26 -070045 mCurrentBucketStartTimeNs(startTimeNs),
Yang Lu3eba6212017-10-25 19:54:45 -070046 mCurrentBucketNum(0),
Yao Chen729093d2017-10-16 10:33:26 -070047 mCondition(conditionIndex >= 0 ? false : true),
Yao Chen93fe3a32017-11-02 13:52:59 -070048 mConditionSliced(false),
Yao Chen729093d2017-10-16 10:33:26 -070049 mWizard(wizard),
50 mConditionTrackerIndex(conditionIndex) {
51 // reuse the same map for non-sliced metrics too. this way, we avoid too many if-else.
52 mDimensionKeyMap[DEFAULT_DIMENSION_KEY] = std::vector<KeyValuePair>();
53 };
Yao Chen44cf27c2017-09-14 22:32:50 -070054 virtual ~MetricProducer(){};
55
Yao Chencaf339d2017-10-06 16:01:10 -070056 // Consume the parsed stats log entry that already matched the "what" of the metric.
Yangsterf2bee6f2017-11-29 12:01:05 -080057 void onMatchedLogEvent(const size_t matcherIndex, const LogEvent& event, bool scheduledPull) {
58 std::lock_guard<std::mutex> lock(mMutex);
59 onMatchedLogEventLocked(matcherIndex, event, scheduledPull);
60 }
Yao Chen44cf27c2017-09-14 22:32:50 -070061
Yangsterf2bee6f2017-11-29 12:01:05 -080062 void onConditionChanged(const bool condition, const uint64_t eventTime) {
63 std::lock_guard<std::mutex> lock(mMutex);
64 onConditionChangedLocked(condition, eventTime);
65 }
Yao Chencaf339d2017-10-06 16:01:10 -070066
Yangsterf2bee6f2017-11-29 12:01:05 -080067 void onSlicedConditionMayChange(const uint64_t eventTime) {
68 std::lock_guard<std::mutex> lock(mMutex);
69 onSlicedConditionMayChangeLocked(eventTime);
70 }
71
72 bool isConditionSliced() const {
73 std::lock_guard<std::mutex> lock(mMutex);
74 return mConditionSliced;
75 };
Yao Chen729093d2017-10-16 10:33:26 -070076
Yao Chen44cf27c2017-09-14 22:32:50 -070077 // This is called when the metric collecting is done, e.g., when there is a new configuration
78 // coming. MetricProducer should do the clean up, and dump existing data to dropbox.
79 virtual void finish() = 0;
80
yro2b0f8862017-11-06 14:27:31 -080081 // TODO: Pass a timestamp as a parameter in onDumpReport and update all its
82 // implementations.
David Chen1d7b0cd2017-11-15 14:20:04 -080083 // onDumpReport returns the proto-serialized output and clears the previously stored contents.
Yangsterf2bee6f2017-11-29 12:01:05 -080084 std::unique_ptr<std::vector<uint8_t>> onDumpReport() {
85 std::lock_guard<std::mutex> lock(mMutex);
86 return onDumpReportLocked();
87 }
Yao Chen729093d2017-10-16 10:33:26 -070088
David Chen1d7b0cd2017-11-15 14:20:04 -080089 // Returns the memory in bytes currently used to store this metric's data. Does not change
90 // state.
Yangsterf2bee6f2017-11-29 12:01:05 -080091 size_t byteSize() const {
92 std::lock_guard<std::mutex> lock(mMutex);
93 return byteSizeLocked();
94 }
yro69007c82017-10-26 20:42:57 -070095
Bookatz450099d2017-11-30 17:09:30 -080096 virtual sp<AnomalyTracker> createAnomalyTracker(const Alert &alert) {
97 return new AnomalyTracker(alert);
98 }
99
Yangster-mace2cd6d52017-11-09 20:38:30 -0800100 void addAnomalyTracker(sp<AnomalyTracker> tracker) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800101 std::lock_guard<std::mutex> lock(mMutex);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800102 mAnomalyTrackers.push_back(tracker);
103 }
104
105 int64_t getBuckeSizeInNs() const {
Yangsterf2bee6f2017-11-29 12:01:05 -0800106 std::lock_guard<std::mutex> lock(mMutex);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800107 return mBucketSizeNs;
108 }
109
Yao Chen729093d2017-10-16 10:33:26 -0700110protected:
Yangsterf2bee6f2017-11-29 12:01:05 -0800111 virtual void onConditionChangedLocked(const bool condition, const uint64_t eventTime) = 0;
112 virtual void onSlicedConditionMayChangeLocked(const uint64_t eventTime) = 0;
113 virtual std::unique_ptr<std::vector<uint8_t>> onDumpReportLocked() = 0;
114 virtual size_t byteSizeLocked() const = 0;
115
Yao Chenb3561512017-11-21 18:07:17 -0800116 const ConfigKey mConfigKey;
117
Yao Chen729093d2017-10-16 10:33:26 -0700118 const uint64_t mStartTimeNs;
119
120 uint64_t mCurrentBucketStartTimeNs;
121
Yang Lu3eba6212017-10-25 19:54:45 -0700122 uint64_t mCurrentBucketNum;
123
Yao Chen729093d2017-10-16 10:33:26 -0700124 int64_t mBucketSizeNs;
125
126 bool mCondition;
127
128 bool mConditionSliced;
129
130 sp<ConditionWizard> mWizard;
131
132 int mConditionTrackerIndex;
133
134 std::vector<KeyMatcher> mDimension; // The dimension defined in statsd_config
135
136 // Keep the map from the internal HashableDimensionKey to std::vector<KeyValuePair>
137 // that StatsLogReport wants.
138 std::unordered_map<HashableDimensionKey, std::vector<KeyValuePair>> mDimensionKeyMap;
139
Stefan Lafona5b51912017-12-05 21:43:52 -0800140 std::vector<MetricConditionLink> mConditionLinks;
Yao Chenb7041772017-10-20 16:59:25 -0700141
Yangster-mace2cd6d52017-11-09 20:38:30 -0800142 std::vector<sp<AnomalyTracker>> mAnomalyTrackers;
143
Yao Chenb7041772017-10-20 16:59:25 -0700144 /*
145 * Individual metrics can implement their own business logic here. All pre-processing is done.
146 *
147 * [matcherIndex]: the index of the matcher which matched this event. This is interesting to
148 * DurationMetric, because it has start/stop/stop_all 3 matchers.
149 * [eventKey]: the extracted dimension key for the final output. if the metric doesn't have
150 * dimensions, it will be DEFAULT_DIMENSION_KEY
151 * [conditionKey]: the keys of conditions which should be used to query the condition for this
Stefan Lafona5b51912017-12-05 21:43:52 -0800152 * target event (from MetricConditionLink). This is passed to individual metrics
Yao Chenb7041772017-10-20 16:59:25 -0700153 * because DurationMetric needs it to be cached.
154 * [condition]: whether condition is met. If condition is sliced, this is the result coming from
155 * query with ConditionWizard; If condition is not sliced, this is the
156 * nonSlicedCondition.
157 * [event]: the log event, just in case the metric needs its data, e.g., EventMetric.
158 */
Yangsterf2bee6f2017-11-29 12:01:05 -0800159 virtual void onMatchedLogEventInternalLocked(
Yao Chenb7041772017-10-20 16:59:25 -0700160 const size_t matcherIndex, const HashableDimensionKey& eventKey,
161 const std::map<std::string, HashableDimensionKey>& conditionKey, bool condition,
Chenjie Yub3dda412017-10-24 13:41:59 -0700162 const LogEvent& event, bool scheduledPull) = 0;
yro2b0f8862017-11-06 14:27:31 -0800163
Yangsterf2bee6f2017-11-29 12:01:05 -0800164 // Consume the parsed stats log entry that already matched the "what" of the metric.
165 void onMatchedLogEventLocked(const size_t matcherIndex, const LogEvent& event,
166 bool scheduledPull);
167
yro2b0f8862017-11-06 14:27:31 -0800168 std::unique_ptr<android::util::ProtoOutputStream> mProto;
169
170 long long mProtoToken;
171
Yangsterf2bee6f2017-11-29 12:01:05 -0800172 // Read/Write mutex to make the producer thread-safe.
173 // TODO(yanglu): replace with std::shared_mutex when available in libc++.
174 mutable std::mutex mMutex;
yro2b0f8862017-11-06 14:27:31 -0800175
Yangsterf2bee6f2017-11-29 12:01:05 -0800176 std::unique_ptr<std::vector<uint8_t>> serializeProtoLocked();
Yao Chen44cf27c2017-09-14 22:32:50 -0700177};
178
179} // namespace statsd
180} // namespace os
181} // namespace android
182#endif // METRIC_PRODUCER_H