Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 1 | /* |
| 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-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame^] | 20 | #include "anomaly/AnomalyTracker.h" |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 21 | #include "condition/ConditionWizard.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 22 | #include "matchers/matcher_util.h" |
| 23 | #include "packages/PackageInfoListener.h" |
| 24 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 25 | #include <log/logprint.h> |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 26 | #include <utils/RefBase.h> |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | namespace os { |
| 30 | namespace statsd { |
| 31 | |
| 32 | // A MetricProducer is responsible for compute one single metrics, creating stats log report, and |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 33 | // 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 Chen | f12b5c6 | 2017-10-13 15:54:03 -0700 | [diff] [blame] | 36 | class MetricProducer : public virtual PackageInfoListener { |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 37 | public: |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 38 | MetricProducer(const int64_t startTimeNs, const int conditionIndex, |
| 39 | const sp<ConditionWizard>& wizard) |
| 40 | : mStartTimeNs(startTimeNs), |
| 41 | mCurrentBucketStartTimeNs(startTimeNs), |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 42 | mCurrentBucketNum(0), |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 43 | mCondition(conditionIndex >= 0 ? false : true), |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 44 | mConditionSliced(false), |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 45 | mWizard(wizard), |
| 46 | mConditionTrackerIndex(conditionIndex) { |
| 47 | // reuse the same map for non-sliced metrics too. this way, we avoid too many if-else. |
| 48 | mDimensionKeyMap[DEFAULT_DIMENSION_KEY] = std::vector<KeyValuePair>(); |
| 49 | }; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 50 | virtual ~MetricProducer(){}; |
| 51 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 52 | // Consume the parsed stats log entry that already matched the "what" of the metric. |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 53 | void onMatchedLogEvent(const size_t matcherIndex, const LogEvent& event, bool scheduledPull); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 54 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 55 | virtual void onConditionChanged(const bool condition, const uint64_t eventTime) = 0; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 56 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 57 | virtual void onSlicedConditionMayChange(const uint64_t eventTime) = 0; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 58 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 59 | // This is called when the metric collecting is done, e.g., when there is a new configuration |
| 60 | // coming. MetricProducer should do the clean up, and dump existing data to dropbox. |
| 61 | virtual void finish() = 0; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame^] | 62 | virtual void flushIfNeeded(const uint64_t newEventTime) = 0; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 63 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 64 | // TODO: Pass a timestamp as a parameter in onDumpReport and update all its |
| 65 | // implementations. |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 66 | // onDumpReport returns the proto-serialized output and clears the previously stored contents. |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 67 | virtual std::unique_ptr<std::vector<uint8_t>> onDumpReport() = 0; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 68 | |
| 69 | virtual bool isConditionSliced() const { |
| 70 | return mConditionSliced; |
| 71 | }; |
| 72 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 73 | // Returns the memory in bytes currently used to store this metric's data. Does not change |
| 74 | // state. |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 75 | virtual size_t byteSize() = 0; |
| 76 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame^] | 77 | void addAnomalyTracker(sp<AnomalyTracker> tracker) { |
| 78 | mAnomalyTrackers.push_back(tracker); |
| 79 | } |
| 80 | |
| 81 | int64_t getBuckeSizeInNs() const { |
| 82 | return mBucketSizeNs; |
| 83 | } |
| 84 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 85 | protected: |
| 86 | const uint64_t mStartTimeNs; |
| 87 | |
| 88 | uint64_t mCurrentBucketStartTimeNs; |
| 89 | |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 90 | uint64_t mCurrentBucketNum; |
| 91 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 92 | int64_t mBucketSizeNs; |
| 93 | |
| 94 | bool mCondition; |
| 95 | |
| 96 | bool mConditionSliced; |
| 97 | |
| 98 | sp<ConditionWizard> mWizard; |
| 99 | |
| 100 | int mConditionTrackerIndex; |
| 101 | |
| 102 | std::vector<KeyMatcher> mDimension; // The dimension defined in statsd_config |
| 103 | |
| 104 | // Keep the map from the internal HashableDimensionKey to std::vector<KeyValuePair> |
| 105 | // that StatsLogReport wants. |
| 106 | std::unordered_map<HashableDimensionKey, std::vector<KeyValuePair>> mDimensionKeyMap; |
| 107 | |
| 108 | std::vector<EventConditionLink> mConditionLinks; |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 109 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame^] | 110 | std::vector<sp<AnomalyTracker>> mAnomalyTrackers; |
| 111 | |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 112 | /* |
| 113 | * Individual metrics can implement their own business logic here. All pre-processing is done. |
| 114 | * |
| 115 | * [matcherIndex]: the index of the matcher which matched this event. This is interesting to |
| 116 | * DurationMetric, because it has start/stop/stop_all 3 matchers. |
| 117 | * [eventKey]: the extracted dimension key for the final output. if the metric doesn't have |
| 118 | * dimensions, it will be DEFAULT_DIMENSION_KEY |
| 119 | * [conditionKey]: the keys of conditions which should be used to query the condition for this |
| 120 | * target event (from EventConditionLink). This is passed to individual metrics |
| 121 | * because DurationMetric needs it to be cached. |
| 122 | * [condition]: whether condition is met. If condition is sliced, this is the result coming from |
| 123 | * query with ConditionWizard; If condition is not sliced, this is the |
| 124 | * nonSlicedCondition. |
| 125 | * [event]: the log event, just in case the metric needs its data, e.g., EventMetric. |
| 126 | */ |
| 127 | virtual void onMatchedLogEventInternal( |
| 128 | const size_t matcherIndex, const HashableDimensionKey& eventKey, |
| 129 | const std::map<std::string, HashableDimensionKey>& conditionKey, bool condition, |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 130 | const LogEvent& event, bool scheduledPull) = 0; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 131 | |
| 132 | std::unique_ptr<android::util::ProtoOutputStream> mProto; |
| 133 | |
| 134 | long long mProtoToken; |
| 135 | |
| 136 | virtual void startNewProtoOutputStream(long long timestamp) = 0; |
| 137 | |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 138 | std::unique_ptr<std::vector<uint8_t>> serializeProto(); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | } // namespace statsd |
| 142 | } // namespace os |
| 143 | } // namespace android |
| 144 | #endif // METRIC_PRODUCER_H |