blob: 7bd274dc129bbf3bce3957be9b1f64767ccd7f8e [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:
Yao Chenf09569f2017-12-13 17:00:51 -080042 MetricProducer(const std::string& name, const ConfigKey& key, const int64_t startTimeNs,
43 const int conditionIndex, const sp<ConditionWizard>& wizard)
44 : mName(name),
45 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),
52 mConditionTrackerIndex(conditionIndex) {
53 // reuse the same map for non-sliced metrics too. this way, we avoid too many if-else.
54 mDimensionKeyMap[DEFAULT_DIMENSION_KEY] = std::vector<KeyValuePair>();
55 };
Yao Chen44cf27c2017-09-14 22:32:50 -070056 virtual ~MetricProducer(){};
57
Yao Chend10f7b12017-12-18 12:53:50 -080058 void notifyAppUpgrade(const string& apk, const int uid, const int64_t version) override{
59 // TODO: Implement me.
60 };
61
62 void notifyAppRemoved(const string& apk, const int uid) override{
63 // TODO: Implement me.
64 };
65
66 void onUidMapReceived() override{
67 // TODO: Implement me.
68 };
69
Yao Chencaf339d2017-10-06 16:01:10 -070070 // Consume the parsed stats log entry that already matched the "what" of the metric.
Chenjie Yua7259ab2017-12-10 08:31:05 -080071 void onMatchedLogEvent(const size_t matcherIndex, const LogEvent& event) {
Yangsterf2bee6f2017-11-29 12:01:05 -080072 std::lock_guard<std::mutex> lock(mMutex);
Chenjie Yua7259ab2017-12-10 08:31:05 -080073 onMatchedLogEventLocked(matcherIndex, event);
Yangsterf2bee6f2017-11-29 12:01:05 -080074 }
Yao Chen44cf27c2017-09-14 22:32:50 -070075
Yangsterf2bee6f2017-11-29 12:01:05 -080076 void onConditionChanged(const bool condition, const uint64_t eventTime) {
77 std::lock_guard<std::mutex> lock(mMutex);
78 onConditionChangedLocked(condition, eventTime);
79 }
Yao Chencaf339d2017-10-06 16:01:10 -070080
Yangsterf2bee6f2017-11-29 12:01:05 -080081 void onSlicedConditionMayChange(const uint64_t eventTime) {
82 std::lock_guard<std::mutex> lock(mMutex);
83 onSlicedConditionMayChangeLocked(eventTime);
84 }
85
86 bool isConditionSliced() const {
87 std::lock_guard<std::mutex> lock(mMutex);
88 return mConditionSliced;
89 };
Yao Chen729093d2017-10-16 10:33:26 -070090
Yao Chen288c6002017-12-12 13:43:18 -080091 // Output the metrics data to [protoOutput]. All metrics reports end with the same timestamp.
92 void onDumpReport(const uint64_t dumpTimeNs, android::util::ProtoOutputStream* protoOutput) {
Yangsterf2bee6f2017-11-29 12:01:05 -080093 std::lock_guard<std::mutex> lock(mMutex);
Yao Chen288c6002017-12-12 13:43:18 -080094 return onDumpReportLocked(dumpTimeNs, protoOutput);
Yangsterf2bee6f2017-11-29 12:01:05 -080095 }
Yao Chen729093d2017-10-16 10:33:26 -070096
David Chen1d7b0cd2017-11-15 14:20:04 -080097 // Returns the memory in bytes currently used to store this metric's data. Does not change
98 // state.
Yangsterf2bee6f2017-11-29 12:01:05 -080099 size_t byteSize() const {
100 std::lock_guard<std::mutex> lock(mMutex);
101 return byteSizeLocked();
102 }
yro69007c82017-10-26 20:42:57 -0700103
Bookatz450099d2017-11-30 17:09:30 -0800104 virtual sp<AnomalyTracker> createAnomalyTracker(const Alert &alert) {
Bookatz8f2f3d82017-12-07 13:53:21 -0800105 return new AnomalyTracker(alert, mConfigKey);
Bookatz450099d2017-11-30 17:09:30 -0800106 }
107
Yangster-mace2cd6d52017-11-09 20:38:30 -0800108 void addAnomalyTracker(sp<AnomalyTracker> tracker) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800109 std::lock_guard<std::mutex> lock(mMutex);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800110 mAnomalyTrackers.push_back(tracker);
111 }
112
113 int64_t getBuckeSizeInNs() const {
Yangsterf2bee6f2017-11-29 12:01:05 -0800114 std::lock_guard<std::mutex> lock(mMutex);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800115 return mBucketSizeNs;
116 }
117
Yao Chen729093d2017-10-16 10:33:26 -0700118protected:
Yangsterf2bee6f2017-11-29 12:01:05 -0800119 virtual void onConditionChangedLocked(const bool condition, const uint64_t eventTime) = 0;
120 virtual void onSlicedConditionMayChangeLocked(const uint64_t eventTime) = 0;
Yao Chen288c6002017-12-12 13:43:18 -0800121 virtual void onDumpReportLocked(const uint64_t dumpTimeNs,
122 android::util::ProtoOutputStream* protoOutput) = 0;
Yangsterf2bee6f2017-11-29 12:01:05 -0800123 virtual size_t byteSizeLocked() const = 0;
124
Yao Chenf09569f2017-12-13 17:00:51 -0800125 const std::string mName;
126
Yao Chenb3561512017-11-21 18:07:17 -0800127 const ConfigKey mConfigKey;
128
Yao Chen288c6002017-12-12 13:43:18 -0800129 // The start time for the current in memory metrics data.
130 uint64_t mStartTimeNs;
Yao Chen729093d2017-10-16 10:33:26 -0700131
132 uint64_t mCurrentBucketStartTimeNs;
133
Yang Lu3eba6212017-10-25 19:54:45 -0700134 uint64_t mCurrentBucketNum;
135
Yao Chen729093d2017-10-16 10:33:26 -0700136 int64_t mBucketSizeNs;
137
138 bool mCondition;
139
140 bool mConditionSliced;
141
142 sp<ConditionWizard> mWizard;
143
144 int mConditionTrackerIndex;
145
146 std::vector<KeyMatcher> mDimension; // The dimension defined in statsd_config
147
148 // Keep the map from the internal HashableDimensionKey to std::vector<KeyValuePair>
149 // that StatsLogReport wants.
150 std::unordered_map<HashableDimensionKey, std::vector<KeyValuePair>> mDimensionKeyMap;
151
Stefan Lafona5b51912017-12-05 21:43:52 -0800152 std::vector<MetricConditionLink> mConditionLinks;
Yao Chenb7041772017-10-20 16:59:25 -0700153
Yangster-mace2cd6d52017-11-09 20:38:30 -0800154 std::vector<sp<AnomalyTracker>> mAnomalyTrackers;
155
Yao Chenb7041772017-10-20 16:59:25 -0700156 /*
157 * Individual metrics can implement their own business logic here. All pre-processing is done.
158 *
159 * [matcherIndex]: the index of the matcher which matched this event. This is interesting to
160 * DurationMetric, because it has start/stop/stop_all 3 matchers.
161 * [eventKey]: the extracted dimension key for the final output. if the metric doesn't have
162 * dimensions, it will be DEFAULT_DIMENSION_KEY
163 * [conditionKey]: the keys of conditions which should be used to query the condition for this
Stefan Lafona5b51912017-12-05 21:43:52 -0800164 * target event (from MetricConditionLink). This is passed to individual metrics
Yao Chenb7041772017-10-20 16:59:25 -0700165 * because DurationMetric needs it to be cached.
166 * [condition]: whether condition is met. If condition is sliced, this is the result coming from
167 * query with ConditionWizard; If condition is not sliced, this is the
168 * nonSlicedCondition.
169 * [event]: the log event, just in case the metric needs its data, e.g., EventMetric.
170 */
Yangsterf2bee6f2017-11-29 12:01:05 -0800171 virtual void onMatchedLogEventInternalLocked(
Yao Chenb7041772017-10-20 16:59:25 -0700172 const size_t matcherIndex, const HashableDimensionKey& eventKey,
173 const std::map<std::string, HashableDimensionKey>& conditionKey, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800174 const LogEvent& event) = 0;
yro2b0f8862017-11-06 14:27:31 -0800175
Yangsterf2bee6f2017-11-29 12:01:05 -0800176 // Consume the parsed stats log entry that already matched the "what" of the metric.
Chenjie Yua7259ab2017-12-10 08:31:05 -0800177 void onMatchedLogEventLocked(const size_t matcherIndex, const LogEvent& event);
Yangsterf2bee6f2017-11-29 12:01:05 -0800178
Yangsterf2bee6f2017-11-29 12:01:05 -0800179 mutable std::mutex mMutex;
Yao Chen44cf27c2017-09-14 22:32:50 -0700180};
181
182} // namespace statsd
183} // namespace os
184} // namespace android
185#endif // METRIC_PRODUCER_H