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 | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 20 | #include <shared_mutex> |
| 21 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 22 | #include "anomaly/AnomalyTracker.h" |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 23 | #include "condition/ConditionWizard.h" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 24 | #include "config/ConfigKey.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 25 | #include "matchers/matcher_util.h" |
| 26 | #include "packages/PackageInfoListener.h" |
| 27 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 28 | #include <log/logprint.h> |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 29 | #include <utils/RefBase.h> |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | namespace os { |
| 33 | namespace statsd { |
| 34 | |
| 35 | // 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] | 36 | // 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 Chen | f12b5c6 | 2017-10-13 15:54:03 -0700 | [diff] [blame] | 39 | class MetricProducer : public virtual PackageInfoListener { |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 40 | public: |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 41 | MetricProducer(const ConfigKey& key, const int64_t startTimeNs, const int conditionIndex, |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 42 | const sp<ConditionWizard>& wizard) |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 43 | : mConfigKey(key), |
| 44 | mStartTimeNs(startTimeNs), |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 45 | mCurrentBucketStartTimeNs(startTimeNs), |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 46 | mCurrentBucketNum(0), |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 47 | mCondition(conditionIndex >= 0 ? false : true), |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 48 | mConditionSliced(false), |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 49 | 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 Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 54 | virtual ~MetricProducer(){}; |
| 55 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 56 | // Consume the parsed stats log entry that already matched the "what" of the metric. |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 57 | 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 Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 61 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 62 | void onConditionChanged(const bool condition, const uint64_t eventTime) { |
| 63 | std::lock_guard<std::mutex> lock(mMutex); |
| 64 | onConditionChangedLocked(condition, eventTime); |
| 65 | } |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 66 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 67 | 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 Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 76 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 77 | // This is called when the metric collecting is done, e.g., when there is a new configuration |
| 78 | // coming. MetricProducer should do the clean up, and dump existing data to dropbox. |
| 79 | virtual void finish() = 0; |
| 80 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 81 | // TODO: Pass a timestamp as a parameter in onDumpReport and update all its |
| 82 | // implementations. |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 83 | // onDumpReport returns the proto-serialized output and clears the previously stored contents. |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 84 | std::unique_ptr<std::vector<uint8_t>> onDumpReport() { |
| 85 | std::lock_guard<std::mutex> lock(mMutex); |
| 86 | return onDumpReportLocked(); |
| 87 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 88 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 89 | // Returns the memory in bytes currently used to store this metric's data. Does not change |
| 90 | // state. |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 91 | size_t byteSize() const { |
| 92 | std::lock_guard<std::mutex> lock(mMutex); |
| 93 | return byteSizeLocked(); |
| 94 | } |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 95 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 96 | void addAnomalyTracker(sp<AnomalyTracker> tracker) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 97 | std::lock_guard<std::mutex> lock(mMutex); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 98 | mAnomalyTrackers.push_back(tracker); |
| 99 | } |
| 100 | |
| 101 | int64_t getBuckeSizeInNs() const { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 102 | std::lock_guard<std::mutex> lock(mMutex); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 103 | return mBucketSizeNs; |
| 104 | } |
| 105 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 106 | protected: |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 107 | virtual void onConditionChangedLocked(const bool condition, const uint64_t eventTime) = 0; |
| 108 | virtual void onSlicedConditionMayChangeLocked(const uint64_t eventTime) = 0; |
| 109 | virtual std::unique_ptr<std::vector<uint8_t>> onDumpReportLocked() = 0; |
| 110 | virtual size_t byteSizeLocked() const = 0; |
| 111 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 112 | const ConfigKey mConfigKey; |
| 113 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 114 | const uint64_t mStartTimeNs; |
| 115 | |
| 116 | uint64_t mCurrentBucketStartTimeNs; |
| 117 | |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 118 | uint64_t mCurrentBucketNum; |
| 119 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 120 | 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 | |
| 136 | std::vector<EventConditionLink> mConditionLinks; |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 137 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 138 | std::vector<sp<AnomalyTracker>> mAnomalyTrackers; |
| 139 | |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 140 | /* |
| 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 |
| 148 | * target event (from EventConditionLink). This is passed to individual metrics |
| 149 | * 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 | */ |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 155 | virtual void onMatchedLogEventInternalLocked( |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 156 | const size_t matcherIndex, const HashableDimensionKey& eventKey, |
| 157 | const std::map<std::string, HashableDimensionKey>& conditionKey, bool condition, |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 158 | const LogEvent& event, bool scheduledPull) = 0; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 159 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 160 | // 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 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 164 | std::unique_ptr<android::util::ProtoOutputStream> mProto; |
| 165 | |
| 166 | long long mProtoToken; |
| 167 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 168 | // Read/Write mutex to make the producer thread-safe. |
| 169 | // TODO(yanglu): replace with std::shared_mutex when available in libc++. |
| 170 | mutable std::mutex mMutex; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 171 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame^] | 172 | std::unique_ptr<std::vector<uint8_t>> serializeProtoLocked(); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | } // namespace statsd |
| 176 | } // namespace os |
| 177 | } // namespace android |
| 178 | #endif // METRIC_PRODUCER_H |