blob: b22ff6f3348cce9db90876d26f7186d79fc24863 [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
Yangster-mace2cd6d52017-11-09 20:38:30 -080020#include "anomaly/AnomalyTracker.h"
Yao Chen729093d2017-10-16 10:33:26 -070021#include "condition/ConditionWizard.h"
Yao Chenb3561512017-11-21 18:07:17 -080022#include "config/ConfigKey.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070023#include "matchers/matcher_util.h"
24#include "packages/PackageInfoListener.h"
25
Yao Chen44cf27c2017-09-14 22:32:50 -070026#include <log/logprint.h>
Yao Chencaf339d2017-10-06 16:01:10 -070027#include <utils/RefBase.h>
Yao Chen44cf27c2017-09-14 22:32:50 -070028
29namespace android {
30namespace os {
31namespace statsd {
32
33// A MetricProducer is responsible for compute one single metrics, creating stats log report, and
David Chende701692017-10-05 13:16:02 -070034// writing the report to dropbox. MetricProducers should respond to package changes as required in
35// PackageInfoListener, but if none of the metrics are slicing by package name, then the update can
36// be a no-op.
David Chenf12b5c62017-10-13 15:54:03 -070037class MetricProducer : public virtual PackageInfoListener {
Yao Chen44cf27c2017-09-14 22:32:50 -070038public:
Yao Chenb3561512017-11-21 18:07:17 -080039 MetricProducer(const ConfigKey& key, const int64_t startTimeNs, const int conditionIndex,
Yao Chen729093d2017-10-16 10:33:26 -070040 const sp<ConditionWizard>& wizard)
Yao Chenb3561512017-11-21 18:07:17 -080041 : mConfigKey(key),
42 mStartTimeNs(startTimeNs),
Yao Chen729093d2017-10-16 10:33:26 -070043 mCurrentBucketStartTimeNs(startTimeNs),
Yang Lu3eba6212017-10-25 19:54:45 -070044 mCurrentBucketNum(0),
Yao Chen729093d2017-10-16 10:33:26 -070045 mCondition(conditionIndex >= 0 ? false : true),
Yao Chen93fe3a32017-11-02 13:52:59 -070046 mConditionSliced(false),
Yao Chen729093d2017-10-16 10:33:26 -070047 mWizard(wizard),
48 mConditionTrackerIndex(conditionIndex) {
49 // reuse the same map for non-sliced metrics too. this way, we avoid too many if-else.
50 mDimensionKeyMap[DEFAULT_DIMENSION_KEY] = std::vector<KeyValuePair>();
51 };
Yao Chen44cf27c2017-09-14 22:32:50 -070052 virtual ~MetricProducer(){};
53
Yao Chencaf339d2017-10-06 16:01:10 -070054 // Consume the parsed stats log entry that already matched the "what" of the metric.
Chenjie Yub3dda412017-10-24 13:41:59 -070055 void onMatchedLogEvent(const size_t matcherIndex, const LogEvent& event, bool scheduledPull);
Yao Chen44cf27c2017-09-14 22:32:50 -070056
Yao Chen5154a372017-10-30 22:57:06 -070057 virtual void onConditionChanged(const bool condition, const uint64_t eventTime) = 0;
Yao Chencaf339d2017-10-06 16:01:10 -070058
Yao Chen5154a372017-10-30 22:57:06 -070059 virtual void onSlicedConditionMayChange(const uint64_t eventTime) = 0;
Yao Chen729093d2017-10-16 10:33:26 -070060
Yao Chen44cf27c2017-09-14 22:32:50 -070061 // This is called when the metric collecting is done, e.g., when there is a new configuration
62 // coming. MetricProducer should do the clean up, and dump existing data to dropbox.
63 virtual void finish() = 0;
Yangster-mace2cd6d52017-11-09 20:38:30 -080064 virtual void flushIfNeeded(const uint64_t newEventTime) = 0;
Yao Chen44cf27c2017-09-14 22:32:50 -070065
yro2b0f8862017-11-06 14:27:31 -080066 // TODO: Pass a timestamp as a parameter in onDumpReport and update all its
67 // implementations.
David Chen1d7b0cd2017-11-15 14:20:04 -080068 // onDumpReport returns the proto-serialized output and clears the previously stored contents.
yro17adac92017-11-08 23:16:29 -080069 virtual std::unique_ptr<std::vector<uint8_t>> onDumpReport() = 0;
Yao Chen729093d2017-10-16 10:33:26 -070070
71 virtual bool isConditionSliced() const {
72 return mConditionSliced;
73 };
74
David Chen1d7b0cd2017-11-15 14:20:04 -080075 // Returns the memory in bytes currently used to store this metric's data. Does not change
76 // state.
Yangster7c334a12017-11-22 14:24:24 -080077 virtual size_t byteSize() const = 0;
yro69007c82017-10-26 20:42:57 -070078
Yangster-mace2cd6d52017-11-09 20:38:30 -080079 void addAnomalyTracker(sp<AnomalyTracker> tracker) {
80 mAnomalyTrackers.push_back(tracker);
81 }
82
83 int64_t getBuckeSizeInNs() const {
84 return mBucketSizeNs;
85 }
86
Yao Chen729093d2017-10-16 10:33:26 -070087protected:
Yao Chenb3561512017-11-21 18:07:17 -080088 const ConfigKey mConfigKey;
89
Yao Chen729093d2017-10-16 10:33:26 -070090 const uint64_t mStartTimeNs;
91
92 uint64_t mCurrentBucketStartTimeNs;
93
Yang Lu3eba6212017-10-25 19:54:45 -070094 uint64_t mCurrentBucketNum;
95
Yao Chen729093d2017-10-16 10:33:26 -070096 int64_t mBucketSizeNs;
97
98 bool mCondition;
99
100 bool mConditionSliced;
101
102 sp<ConditionWizard> mWizard;
103
104 int mConditionTrackerIndex;
105
106 std::vector<KeyMatcher> mDimension; // The dimension defined in statsd_config
107
108 // Keep the map from the internal HashableDimensionKey to std::vector<KeyValuePair>
109 // that StatsLogReport wants.
110 std::unordered_map<HashableDimensionKey, std::vector<KeyValuePair>> mDimensionKeyMap;
111
112 std::vector<EventConditionLink> mConditionLinks;
Yao Chenb7041772017-10-20 16:59:25 -0700113
Yangster-mace2cd6d52017-11-09 20:38:30 -0800114 std::vector<sp<AnomalyTracker>> mAnomalyTrackers;
115
Yao Chenb7041772017-10-20 16:59:25 -0700116 /*
117 * Individual metrics can implement their own business logic here. All pre-processing is done.
118 *
119 * [matcherIndex]: the index of the matcher which matched this event. This is interesting to
120 * DurationMetric, because it has start/stop/stop_all 3 matchers.
121 * [eventKey]: the extracted dimension key for the final output. if the metric doesn't have
122 * dimensions, it will be DEFAULT_DIMENSION_KEY
123 * [conditionKey]: the keys of conditions which should be used to query the condition for this
124 * target event (from EventConditionLink). This is passed to individual metrics
125 * because DurationMetric needs it to be cached.
126 * [condition]: whether condition is met. If condition is sliced, this is the result coming from
127 * query with ConditionWizard; If condition is not sliced, this is the
128 * nonSlicedCondition.
129 * [event]: the log event, just in case the metric needs its data, e.g., EventMetric.
130 */
131 virtual void onMatchedLogEventInternal(
132 const size_t matcherIndex, const HashableDimensionKey& eventKey,
133 const std::map<std::string, HashableDimensionKey>& conditionKey, bool condition,
Chenjie Yub3dda412017-10-24 13:41:59 -0700134 const LogEvent& event, bool scheduledPull) = 0;
yro2b0f8862017-11-06 14:27:31 -0800135
136 std::unique_ptr<android::util::ProtoOutputStream> mProto;
137
138 long long mProtoToken;
139
140 virtual void startNewProtoOutputStream(long long timestamp) = 0;
141
yro17adac92017-11-08 23:16:29 -0800142 std::unique_ptr<std::vector<uint8_t>> serializeProto();
Yao Chen44cf27c2017-09-14 22:32:50 -0700143};
144
145} // namespace statsd
146} // namespace os
147} // namespace android
148#endif // METRIC_PRODUCER_H