blob: afaab648c09fdc74bf4dfde0765ac0a311acc2cf [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),
43 mWizard(wizard),
44 mConditionTrackerIndex(conditionIndex) {
45 // reuse the same map for non-sliced metrics too. this way, we avoid too many if-else.
46 mDimensionKeyMap[DEFAULT_DIMENSION_KEY] = std::vector<KeyValuePair>();
47 };
Yao Chen44cf27c2017-09-14 22:32:50 -070048 virtual ~MetricProducer(){};
49
Yao Chencaf339d2017-10-06 16:01:10 -070050 // Consume the parsed stats log entry that already matched the "what" of the metric.
Yao Chen729093d2017-10-16 10:33:26 -070051 virtual void onMatchedLogEvent(const size_t matcherIndex, const LogEvent& event) = 0;
Yao Chen44cf27c2017-09-14 22:32:50 -070052
Yao Chencaf339d2017-10-06 16:01:10 -070053 virtual void onConditionChanged(const bool condition) = 0;
54
Yao Chen729093d2017-10-16 10:33:26 -070055 virtual void onSlicedConditionMayChange() = 0;
56
Yao Chen44cf27c2017-09-14 22:32:50 -070057 // This is called when the metric collecting is done, e.g., when there is a new configuration
58 // coming. MetricProducer should do the clean up, and dump existing data to dropbox.
59 virtual void finish() = 0;
60
Yao Chen729093d2017-10-16 10:33:26 -070061 virtual StatsLogReport onDumpReport() = 0;
62
63 virtual bool isConditionSliced() const {
64 return mConditionSliced;
65 };
66
67protected:
68 const uint64_t mStartTimeNs;
69
70 uint64_t mCurrentBucketStartTimeNs;
71
72 int64_t mBucketSizeNs;
73
74 bool mCondition;
75
76 bool mConditionSliced;
77
78 sp<ConditionWizard> mWizard;
79
80 int mConditionTrackerIndex;
81
82 std::vector<KeyMatcher> mDimension; // The dimension defined in statsd_config
83
84 // Keep the map from the internal HashableDimensionKey to std::vector<KeyValuePair>
85 // that StatsLogReport wants.
86 std::unordered_map<HashableDimensionKey, std::vector<KeyValuePair>> mDimensionKeyMap;
87
88 std::vector<EventConditionLink> mConditionLinks;
Yao Chen44cf27c2017-09-14 22:32:50 -070089};
90
91} // namespace statsd
92} // namespace os
93} // namespace android
94#endif // METRIC_PRODUCER_H