blob: 7ac97a8a2e8bc6c0d9e241969113ade34ecc54cd [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
Yao Chen729093d2017-10-16 10:33:26 -070020#include "condition/ConditionWizard.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070021#include "matchers/matcher_util.h"
22#include "packages/PackageInfoListener.h"
23
Yao Chen44cf27c2017-09-14 22:32:50 -070024#include <log/logprint.h>
Yao Chencaf339d2017-10-06 16:01:10 -070025#include <utils/RefBase.h>
Yao Chen729093d2017-10-16 10:33:26 -070026#include "frameworks/base/cmds/statsd/src/stats_log.pb.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070027
28namespace android {
29namespace os {
30namespace statsd {
31
32// A MetricProducer is responsible for compute one single metrics, creating stats log report, and
David Chende701692017-10-05 13:16:02 -070033// writing the report to dropbox. MetricProducers should respond to package changes as required in
34// PackageInfoListener, but if none of the metrics are slicing by package name, then the update can
35// be a no-op.
David Chenf12b5c62017-10-13 15:54:03 -070036class MetricProducer : public virtual PackageInfoListener {
Yao Chen44cf27c2017-09-14 22:32:50 -070037public:
Yao Chen729093d2017-10-16 10:33:26 -070038 MetricProducer(const int64_t startTimeNs, const int conditionIndex,
39 const sp<ConditionWizard>& wizard)
40 : mStartTimeNs(startTimeNs),
41 mCurrentBucketStartTimeNs(startTimeNs),
42 mCondition(conditionIndex >= 0 ? false : true),
Yao Chen93fe3a32017-11-02 13:52:59 -070043 mConditionSliced(false),
Yao Chen729093d2017-10-16 10:33:26 -070044 mWizard(wizard),
45 mConditionTrackerIndex(conditionIndex) {
46 // reuse the same map for non-sliced metrics too. this way, we avoid too many if-else.
47 mDimensionKeyMap[DEFAULT_DIMENSION_KEY] = std::vector<KeyValuePair>();
48 };
Yao Chen44cf27c2017-09-14 22:32:50 -070049 virtual ~MetricProducer(){};
50
Yao Chencaf339d2017-10-06 16:01:10 -070051 // Consume the parsed stats log entry that already matched the "what" of the metric.
Chenjie Yub3dda412017-10-24 13:41:59 -070052 void onMatchedLogEvent(const size_t matcherIndex, const LogEvent& event, bool scheduledPull);
Yao Chen44cf27c2017-09-14 22:32:50 -070053
Yao Chen5154a372017-10-30 22:57:06 -070054 virtual void onConditionChanged(const bool condition, const uint64_t eventTime) = 0;
Yao Chencaf339d2017-10-06 16:01:10 -070055
Yao Chen5154a372017-10-30 22:57:06 -070056 virtual void onSlicedConditionMayChange(const uint64_t eventTime) = 0;
Yao Chen729093d2017-10-16 10:33:26 -070057
Yao Chen44cf27c2017-09-14 22:32:50 -070058 // This is called when the metric collecting is done, e.g., when there is a new configuration
59 // coming. MetricProducer should do the clean up, and dump existing data to dropbox.
60 virtual void finish() = 0;
61
yro2b0f8862017-11-06 14:27:31 -080062 // TODO: Pass a timestamp as a parameter in onDumpReport and update all its
63 // implementations.
Yao Chen729093d2017-10-16 10:33:26 -070064 virtual StatsLogReport onDumpReport() = 0;
65
66 virtual bool isConditionSliced() const {
67 return mConditionSliced;
68 };
69
yro69007c82017-10-26 20:42:57 -070070 virtual size_t byteSize() = 0;
71
Yao Chen729093d2017-10-16 10:33:26 -070072protected:
73 const uint64_t mStartTimeNs;
74
75 uint64_t mCurrentBucketStartTimeNs;
76
77 int64_t mBucketSizeNs;
78
79 bool mCondition;
80
81 bool mConditionSliced;
82
83 sp<ConditionWizard> mWizard;
84
85 int mConditionTrackerIndex;
86
87 std::vector<KeyMatcher> mDimension; // The dimension defined in statsd_config
88
89 // Keep the map from the internal HashableDimensionKey to std::vector<KeyValuePair>
90 // that StatsLogReport wants.
91 std::unordered_map<HashableDimensionKey, std::vector<KeyValuePair>> mDimensionKeyMap;
92
93 std::vector<EventConditionLink> mConditionLinks;
Yao Chenb7041772017-10-20 16:59:25 -070094
95 /*
96 * Individual metrics can implement their own business logic here. All pre-processing is done.
97 *
98 * [matcherIndex]: the index of the matcher which matched this event. This is interesting to
99 * DurationMetric, because it has start/stop/stop_all 3 matchers.
100 * [eventKey]: the extracted dimension key for the final output. if the metric doesn't have
101 * dimensions, it will be DEFAULT_DIMENSION_KEY
102 * [conditionKey]: the keys of conditions which should be used to query the condition for this
103 * target event (from EventConditionLink). This is passed to individual metrics
104 * because DurationMetric needs it to be cached.
105 * [condition]: whether condition is met. If condition is sliced, this is the result coming from
106 * query with ConditionWizard; If condition is not sliced, this is the
107 * nonSlicedCondition.
108 * [event]: the log event, just in case the metric needs its data, e.g., EventMetric.
109 */
110 virtual void onMatchedLogEventInternal(
111 const size_t matcherIndex, const HashableDimensionKey& eventKey,
112 const std::map<std::string, HashableDimensionKey>& conditionKey, bool condition,
Chenjie Yub3dda412017-10-24 13:41:59 -0700113 const LogEvent& event, bool scheduledPull) = 0;
yro2b0f8862017-11-06 14:27:31 -0800114
115 std::unique_ptr<android::util::ProtoOutputStream> mProto;
116
117 long long mProtoToken;
118
119 virtual void startNewProtoOutputStream(long long timestamp) = 0;
120
121 std::unique_ptr<uint8_t[]> serializeProto();
Yao Chen44cf27c2017-09-14 22:32:50 -0700122};
123
124} // namespace statsd
125} // namespace os
126} // namespace android
127#endif // METRIC_PRODUCER_H