blob: ef2ef2965fc16212da7fc953e1a2342c61ef3156 [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 Chen288c6002017-12-12 13:43:18 -080077 // Output the metrics data to [protoOutput]. All metrics reports end with the same timestamp.
78 void onDumpReport(const uint64_t dumpTimeNs, android::util::ProtoOutputStream* protoOutput) {
Yangsterf2bee6f2017-11-29 12:01:05 -080079 std::lock_guard<std::mutex> lock(mMutex);
Yao Chen288c6002017-12-12 13:43:18 -080080 return onDumpReportLocked(dumpTimeNs, protoOutput);
Yangsterf2bee6f2017-11-29 12:01:05 -080081 }
Yao Chen729093d2017-10-16 10:33:26 -070082
David Chen1d7b0cd2017-11-15 14:20:04 -080083 // Returns the memory in bytes currently used to store this metric's data. Does not change
84 // state.
Yangsterf2bee6f2017-11-29 12:01:05 -080085 size_t byteSize() const {
86 std::lock_guard<std::mutex> lock(mMutex);
87 return byteSizeLocked();
88 }
yro69007c82017-10-26 20:42:57 -070089
Bookatz450099d2017-11-30 17:09:30 -080090 virtual sp<AnomalyTracker> createAnomalyTracker(const Alert &alert) {
Bookatz8f2f3d82017-12-07 13:53:21 -080091 return new AnomalyTracker(alert, mConfigKey);
Bookatz450099d2017-11-30 17:09:30 -080092 }
93
Yangster-mace2cd6d52017-11-09 20:38:30 -080094 void addAnomalyTracker(sp<AnomalyTracker> tracker) {
Yangsterf2bee6f2017-11-29 12:01:05 -080095 std::lock_guard<std::mutex> lock(mMutex);
Yangster-mace2cd6d52017-11-09 20:38:30 -080096 mAnomalyTrackers.push_back(tracker);
97 }
98
99 int64_t getBuckeSizeInNs() const {
Yangsterf2bee6f2017-11-29 12:01:05 -0800100 std::lock_guard<std::mutex> lock(mMutex);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800101 return mBucketSizeNs;
102 }
103
Yao Chen729093d2017-10-16 10:33:26 -0700104protected:
Yangsterf2bee6f2017-11-29 12:01:05 -0800105 virtual void onConditionChangedLocked(const bool condition, const uint64_t eventTime) = 0;
106 virtual void onSlicedConditionMayChangeLocked(const uint64_t eventTime) = 0;
Yao Chen288c6002017-12-12 13:43:18 -0800107 virtual void onDumpReportLocked(const uint64_t dumpTimeNs,
108 android::util::ProtoOutputStream* protoOutput) = 0;
Yangsterf2bee6f2017-11-29 12:01:05 -0800109 virtual size_t byteSizeLocked() const = 0;
110
Yao Chenb3561512017-11-21 18:07:17 -0800111 const ConfigKey mConfigKey;
112
Yao Chen288c6002017-12-12 13:43:18 -0800113 // The start time for the current in memory metrics data.
114 uint64_t mStartTimeNs;
Yao Chen729093d2017-10-16 10:33:26 -0700115
116 uint64_t mCurrentBucketStartTimeNs;
117
Yang Lu3eba6212017-10-25 19:54:45 -0700118 uint64_t mCurrentBucketNum;
119
Yao Chen729093d2017-10-16 10:33:26 -0700120 int64_t mBucketSizeNs;
121
122 bool mCondition;
123
124 bool mConditionSliced;
125
126 sp<ConditionWizard> mWizard;
127
128 int mConditionTrackerIndex;
129
130 std::vector<KeyMatcher> mDimension; // The dimension defined in statsd_config
131
132 // Keep the map from the internal HashableDimensionKey to std::vector<KeyValuePair>
133 // that StatsLogReport wants.
134 std::unordered_map<HashableDimensionKey, std::vector<KeyValuePair>> mDimensionKeyMap;
135
Stefan Lafona5b51912017-12-05 21:43:52 -0800136 std::vector<MetricConditionLink> mConditionLinks;
Yao Chenb7041772017-10-20 16:59:25 -0700137
Yangster-mace2cd6d52017-11-09 20:38:30 -0800138 std::vector<sp<AnomalyTracker>> mAnomalyTrackers;
139
Yao Chenb7041772017-10-20 16:59:25 -0700140 /*
141 * Individual metrics can implement their own business logic here. All pre-processing is done.
142 *
143 * [matcherIndex]: the index of the matcher which matched this event. This is interesting to
144 * DurationMetric, because it has start/stop/stop_all 3 matchers.
145 * [eventKey]: the extracted dimension key for the final output. if the metric doesn't have
146 * dimensions, it will be DEFAULT_DIMENSION_KEY
147 * [conditionKey]: the keys of conditions which should be used to query the condition for this
Stefan Lafona5b51912017-12-05 21:43:52 -0800148 * target event (from MetricConditionLink). This is passed to individual metrics
Yao Chenb7041772017-10-20 16:59:25 -0700149 * because DurationMetric needs it to be cached.
150 * [condition]: whether condition is met. If condition is sliced, this is the result coming from
151 * query with ConditionWizard; If condition is not sliced, this is the
152 * nonSlicedCondition.
153 * [event]: the log event, just in case the metric needs its data, e.g., EventMetric.
154 */
Yangsterf2bee6f2017-11-29 12:01:05 -0800155 virtual void onMatchedLogEventInternalLocked(
Yao Chenb7041772017-10-20 16:59:25 -0700156 const size_t matcherIndex, const HashableDimensionKey& eventKey,
157 const std::map<std::string, HashableDimensionKey>& conditionKey, bool condition,
Chenjie Yub3dda412017-10-24 13:41:59 -0700158 const LogEvent& event, bool scheduledPull) = 0;
yro2b0f8862017-11-06 14:27:31 -0800159
Yangsterf2bee6f2017-11-29 12:01:05 -0800160 // Consume the parsed stats log entry that already matched the "what" of the metric.
161 void onMatchedLogEventLocked(const size_t matcherIndex, const LogEvent& event,
162 bool scheduledPull);
163
Yangsterf2bee6f2017-11-29 12:01:05 -0800164 mutable std::mutex mMutex;
Yao Chen44cf27c2017-09-14 22:32:50 -0700165};
166
167} // namespace statsd
168} // namespace os
169} // namespace android
170#endif // METRIC_PRODUCER_H