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 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 22 | #include "HashableDimensionKey.h" |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 23 | #include "anomaly/AnomalyTracker.h" |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 24 | #include "condition/ConditionWizard.h" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 25 | #include "config/ConfigKey.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 26 | #include "matchers/matcher_util.h" |
| 27 | #include "packages/PackageInfoListener.h" |
| 28 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 29 | #include <log/logprint.h> |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 30 | #include <utils/RefBase.h> |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 31 | #include <unordered_map> |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | namespace os { |
| 35 | namespace statsd { |
| 36 | |
Yangster-mac | 849dfdc2 | 2018-10-12 15:41:45 -0700 | [diff] [blame] | 37 | // If the metric has no activation requirement, it will be active once the metric producer is |
| 38 | // created. |
| 39 | // If the metric needs to be activated by atoms, the metric producer will start |
| 40 | // with kNotActive state, turn to kActive when the activation event arrives, become kNotActive |
| 41 | // when it reaches the duration limit (timebomb). If the activation event arrives again before |
| 42 | // or after it expires, the event producer will be re-activated and ttl will be reset. |
| 43 | enum ActivationState { |
| 44 | kNotActive = 0, |
| 45 | kActive = 1, |
| 46 | }; |
| 47 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 48 | // 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] | 49 | // writing the report to dropbox. MetricProducers should respond to package changes as required in |
| 50 | // PackageInfoListener, but if none of the metrics are slicing by package name, then the update can |
| 51 | // be a no-op. |
David Chen | f12b5c6 | 2017-10-13 15:54:03 -0700 | [diff] [blame] | 52 | class MetricProducer : public virtual PackageInfoListener { |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 53 | public: |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 54 | MetricProducer(const int64_t& metricId, const ConfigKey& key, const int64_t timeBaseNs, |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 55 | const int conditionIndex, const sp<ConditionWizard>& wizard) |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 56 | : mMetricId(metricId), |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 57 | mConfigKey(key), |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 58 | mTimeBaseNs(timeBaseNs), |
| 59 | mCurrentBucketStartTimeNs(timeBaseNs), |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 60 | mCurrentBucketNum(0), |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 61 | mCondition(conditionIndex >= 0 ? false : true), |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 62 | mConditionSliced(false), |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 63 | mWizard(wizard), |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 64 | mConditionTrackerIndex(conditionIndex), |
| 65 | mContainANYPositionInDimensionsInWhat(false), |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 66 | mSliceByPositionALL(false), |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 67 | mSameConditionDimensionsInTracker(false), |
Yangster-mac | 849dfdc2 | 2018-10-12 15:41:45 -0700 | [diff] [blame] | 68 | mHasLinksToAllConditionDimensionsInTracker(false), |
| 69 | mIsActive(true) { |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 70 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 71 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 72 | virtual ~MetricProducer(){}; |
| 73 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 74 | /** |
| 75 | * Forces this metric to split into a partial bucket right now. If we're past a full bucket, we |
| 76 | * first call the standard flushing code to flush up to the latest full bucket. Then we call |
| 77 | * the flush again when the end timestamp is forced to be now, and then after flushing, update |
| 78 | * the start timestamp to be now. |
| 79 | */ |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 80 | void notifyAppUpgrade(const int64_t& eventTimeNs, const string& apk, const int uid, |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 81 | const int64_t version) override { |
| 82 | std::lock_guard<std::mutex> lock(mMutex); |
| 83 | |
| 84 | if (eventTimeNs > getCurrentBucketEndTimeNs()) { |
| 85 | // Flush full buckets on the normal path up to the latest bucket boundary. |
| 86 | flushIfNeededLocked(eventTimeNs); |
| 87 | } |
| 88 | // Now flush a partial bucket. |
| 89 | flushCurrentBucketLocked(eventTimeNs); |
| 90 | mCurrentBucketStartTimeNs = eventTimeNs; |
| 91 | // Don't update the current bucket number so that the anomaly tracker knows this bucket |
| 92 | // is a partial bucket and can merge it with the previous bucket. |
| 93 | }; |
| 94 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 95 | void notifyAppRemoved(const int64_t& eventTimeNs, const string& apk, const int uid) override{ |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 96 | // Force buckets to split on removal also. |
| 97 | notifyAppUpgrade(eventTimeNs, apk, uid, 0); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 98 | }; |
| 99 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 100 | void onUidMapReceived(const int64_t& eventTimeNs) override{ |
David Chen | bd12527 | 2018-04-04 19:02:50 -0700 | [diff] [blame] | 101 | // Purposefully don't flush partial buckets on a new snapshot. |
| 102 | // This occurs if a new user is added/removed or statsd crashes. |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 103 | }; |
| 104 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 105 | // Consume the parsed stats log entry that already matched the "what" of the metric. |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 106 | void onMatchedLogEvent(const size_t matcherIndex, const LogEvent& event) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 107 | std::lock_guard<std::mutex> lock(mMutex); |
Yangster-mac | 849dfdc2 | 2018-10-12 15:41:45 -0700 | [diff] [blame] | 108 | if (mIsActive) { |
| 109 | onMatchedLogEventLocked(matcherIndex, event); |
| 110 | } |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 111 | } |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 112 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 113 | void onConditionChanged(const bool condition, const int64_t eventTime) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 114 | std::lock_guard<std::mutex> lock(mMutex); |
Yangster-mac | 849dfdc2 | 2018-10-12 15:41:45 -0700 | [diff] [blame] | 115 | if (mIsActive) { |
| 116 | onConditionChangedLocked(condition, eventTime); |
| 117 | } |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 118 | } |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 119 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 120 | void onSlicedConditionMayChange(bool overallCondition, const int64_t eventTime) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 121 | std::lock_guard<std::mutex> lock(mMutex); |
Yangster-mac | 849dfdc2 | 2018-10-12 15:41:45 -0700 | [diff] [blame] | 122 | if (mIsActive) { |
| 123 | onSlicedConditionMayChangeLocked(overallCondition, eventTime); |
| 124 | } |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | bool isConditionSliced() const { |
| 128 | std::lock_guard<std::mutex> lock(mMutex); |
| 129 | return mConditionSliced; |
| 130 | }; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 131 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 132 | // Output the metrics data to [protoOutput]. All metrics reports end with the same timestamp. |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 133 | // This method clears all the past buckets. |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 134 | void onDumpReport(const int64_t dumpTimeNs, |
| 135 | const bool include_current_partial_bucket, |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 136 | const bool erase_data, |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 137 | std::set<string> *str_set, |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 138 | android::util::ProtoOutputStream* protoOutput) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 139 | std::lock_guard<std::mutex> lock(mMutex); |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 140 | return onDumpReportLocked(dumpTimeNs, include_current_partial_bucket, erase_data, |
| 141 | str_set, protoOutput); |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 142 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 143 | |
Yangster-mac | a802d73 | 2018-04-24 07:50:38 -0700 | [diff] [blame] | 144 | void clearPastBuckets(const int64_t dumpTimeNs) { |
| 145 | std::lock_guard<std::mutex> lock(mMutex); |
| 146 | return clearPastBucketsLocked(dumpTimeNs); |
| 147 | } |
| 148 | |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 149 | void dumpStates(FILE* out, bool verbose) const { |
| 150 | std::lock_guard<std::mutex> lock(mMutex); |
| 151 | dumpStatesLocked(out, verbose); |
| 152 | } |
| 153 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 154 | // Returns the memory in bytes currently used to store this metric's data. Does not change |
| 155 | // state. |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 156 | size_t byteSize() const { |
| 157 | std::lock_guard<std::mutex> lock(mMutex); |
| 158 | return byteSizeLocked(); |
| 159 | } |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 160 | |
Bookatz | 1476ef2 | 2018-02-13 12:26:01 -0800 | [diff] [blame] | 161 | /* If alert is valid, adds an AnomalyTracker and returns it. If invalid, returns nullptr. */ |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 162 | virtual sp<AnomalyTracker> addAnomalyTracker(const Alert &alert, |
| 163 | const sp<AlarmMonitor>& anomalyAlarmMonitor) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 164 | std::lock_guard<std::mutex> lock(mMutex); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 165 | sp<AnomalyTracker> anomalyTracker = new AnomalyTracker(alert, mConfigKey); |
| 166 | if (anomalyTracker != nullptr) { |
| 167 | mAnomalyTrackers.push_back(anomalyTracker); |
| 168 | } |
| 169 | return anomalyTracker; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | int64_t getBuckeSizeInNs() const { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 173 | std::lock_guard<std::mutex> lock(mMutex); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 174 | return mBucketSizeNs; |
| 175 | } |
| 176 | |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 177 | // Only needed for unit-testing to override guardrail. |
| 178 | void setBucketSize(int64_t bucketSize) { |
| 179 | mBucketSizeNs = bucketSize; |
| 180 | } |
| 181 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 182 | inline const int64_t& getMetricId() { |
| 183 | return mMetricId; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 186 | // Let MetricProducer drop in-memory data to save memory. |
| 187 | // We still need to keep future data valid and anomaly tracking work, which means we will |
| 188 | // have to flush old data, informing anomaly trackers then safely drop old data. |
| 189 | // We still keep current bucket data for future metrics' validity. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 190 | void dropData(const int64_t dropTimeNs) { |
Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 191 | std::lock_guard<std::mutex> lock(mMutex); |
| 192 | dropDataLocked(dropTimeNs); |
| 193 | } |
| 194 | |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 195 | // For test only. |
| 196 | inline int64_t getCurrentBucketNum() const { |
| 197 | return mCurrentBucketNum; |
| 198 | } |
| 199 | |
Yangster-mac | 849dfdc2 | 2018-10-12 15:41:45 -0700 | [diff] [blame] | 200 | void activate(int activationTrackerIndex, int64_t elapsedTimestampNs) { |
| 201 | std::lock_guard<std::mutex> lock(mMutex); |
| 202 | activateLocked(activationTrackerIndex, elapsedTimestampNs); |
| 203 | } |
| 204 | |
| 205 | void addActivation(int activationTrackerIndex, int64_t ttl_seconds); |
| 206 | |
| 207 | void flushIfExpire(int64_t elapsedTimestampNs); |
| 208 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 209 | protected: |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 210 | virtual void onConditionChangedLocked(const bool condition, const int64_t eventTime) = 0; |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 211 | virtual void onSlicedConditionMayChangeLocked(bool overallCondition, |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 212 | const int64_t eventTime) = 0; |
| 213 | virtual void onDumpReportLocked(const int64_t dumpTimeNs, |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 214 | const bool include_current_partial_bucket, |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 215 | const bool erase_data, |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 216 | std::set<string> *str_set, |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 217 | android::util::ProtoOutputStream* protoOutput) = 0; |
Yangster-mac | a802d73 | 2018-04-24 07:50:38 -0700 | [diff] [blame] | 218 | virtual void clearPastBucketsLocked(const int64_t dumpTimeNs) = 0; |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 219 | virtual size_t byteSizeLocked() const = 0; |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 220 | virtual void dumpStatesLocked(FILE* out, bool verbose) const = 0; |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 221 | |
Yangster-mac | 849dfdc2 | 2018-10-12 15:41:45 -0700 | [diff] [blame] | 222 | bool evaluateActiveStateLocked(int64_t elapsedTimestampNs); |
| 223 | |
| 224 | void activateLocked(int activationTrackerIndex, int64_t elapsedTimestampNs); |
| 225 | |
Yang Lu | b472291 | 2018-11-15 11:02:03 -0800 | [diff] [blame^] | 226 | inline bool isActiveLocked() const { |
| 227 | return mIsActive; |
| 228 | } |
| 229 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 230 | /** |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 231 | * Flushes the current bucket if the eventTime is after the current bucket's end time. This will |
| 232 | also flush the current partial bucket in memory. |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 233 | */ |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 234 | virtual void flushIfNeededLocked(const int64_t& eventTime){}; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 235 | |
| 236 | /** |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 237 | * Flushes all the data including the current partial bucket. |
| 238 | */ |
Yangster-mac | 849dfdc2 | 2018-10-12 15:41:45 -0700 | [diff] [blame] | 239 | virtual void flushLocked(const int64_t& eventTimeNs) { |
| 240 | flushIfNeededLocked(eventTimeNs); |
| 241 | flushCurrentBucketLocked(eventTimeNs); |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 242 | }; |
| 243 | |
| 244 | /** |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 245 | * For metrics that aggregate (ie, every metric producer except for EventMetricProducer), |
| 246 | * we need to be able to flush the current buckets on demand (ie, end the current bucket and |
| 247 | * start new bucket). If this function is called when eventTimeNs is greater than the current |
| 248 | * bucket's end timestamp, than we flush up to the end of the latest full bucket; otherwise, |
| 249 | * we assume that we want to flush a partial bucket. The bucket start timestamp and bucket |
| 250 | * number are not changed by this function. This method should only be called by |
| 251 | * flushIfNeededLocked or the app upgrade handler; the caller MUST update the bucket timestamp |
| 252 | * and bucket number as needed. |
| 253 | */ |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 254 | virtual void flushCurrentBucketLocked(const int64_t& eventTimeNs){}; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 255 | |
| 256 | // Convenience to compute the current bucket's end time, which is always aligned with the |
| 257 | // start time of the metric. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 258 | int64_t getCurrentBucketEndTimeNs() const { |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 259 | return mTimeBaseNs + (mCurrentBucketNum + 1) * mBucketSizeNs; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 262 | int64_t getBucketNumFromEndTimeNs(const int64_t endNs) { |
| 263 | return (endNs - mTimeBaseNs) / mBucketSizeNs - 1; |
| 264 | } |
| 265 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 266 | virtual void dropDataLocked(const int64_t dropTimeNs) = 0; |
Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 267 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 268 | const int64_t mMetricId; |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 269 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 270 | const ConfigKey mConfigKey; |
| 271 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 272 | // The time when this metric producer was first created. The end time for the current bucket |
| 273 | // can be computed from this based on mCurrentBucketNum. |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 274 | int64_t mTimeBaseNs; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 275 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 276 | // Start time may not be aligned with the start of statsd if there is an app upgrade in the |
| 277 | // middle of a bucket. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 278 | int64_t mCurrentBucketStartTimeNs; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 279 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 280 | // Used by anomaly detector to track which bucket we are in. This is not sent with the produced |
| 281 | // report. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 282 | int64_t mCurrentBucketNum; |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 283 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 284 | int64_t mBucketSizeNs; |
| 285 | |
| 286 | bool mCondition; |
| 287 | |
| 288 | bool mConditionSliced; |
| 289 | |
| 290 | sp<ConditionWizard> mWizard; |
| 291 | |
| 292 | int mConditionTrackerIndex; |
| 293 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 294 | vector<Matcher> mDimensionsInWhat; // The dimensions_in_what defined in statsd_config |
| 295 | vector<Matcher> mDimensionsInCondition; // The dimensions_in_condition defined in statsd_config |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 296 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 297 | bool mContainANYPositionInDimensionsInWhat; |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 298 | bool mSliceByPositionALL; |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 299 | |
| 300 | // True iff the condition dimensions equal to the sliced dimensions in the simple condition |
| 301 | // tracker. This field is always false for combinational condition trackers. |
| 302 | bool mSameConditionDimensionsInTracker; |
| 303 | |
| 304 | // True iff the metric to condition links cover all dimension fields in the condition tracker. |
| 305 | // This field is always false for combinational condition trackers. |
| 306 | bool mHasLinksToAllConditionDimensionsInTracker; |
| 307 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 308 | std::vector<Metric2Condition> mMetric2ConditionLinks; |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 309 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 310 | std::vector<sp<AnomalyTracker>> mAnomalyTrackers; |
| 311 | |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 312 | /* |
| 313 | * Individual metrics can implement their own business logic here. All pre-processing is done. |
| 314 | * |
| 315 | * [matcherIndex]: the index of the matcher which matched this event. This is interesting to |
| 316 | * DurationMetric, because it has start/stop/stop_all 3 matchers. |
| 317 | * [eventKey]: the extracted dimension key for the final output. if the metric doesn't have |
| 318 | * dimensions, it will be DEFAULT_DIMENSION_KEY |
| 319 | * [conditionKey]: the keys of conditions which should be used to query the condition for this |
Stefan Lafon | a5b5191 | 2017-12-05 21:43:52 -0800 | [diff] [blame] | 320 | * target event (from MetricConditionLink). This is passed to individual metrics |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 321 | * because DurationMetric needs it to be cached. |
| 322 | * [condition]: whether condition is met. If condition is sliced, this is the result coming from |
| 323 | * query with ConditionWizard; If condition is not sliced, this is the |
| 324 | * nonSlicedCondition. |
| 325 | * [event]: the log event, just in case the metric needs its data, e.g., EventMetric. |
| 326 | */ |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 327 | virtual void onMatchedLogEventInternalLocked( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 328 | const size_t matcherIndex, const MetricDimensionKey& eventKey, |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 329 | const ConditionKey& conditionKey, bool condition, |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 330 | const LogEvent& event) = 0; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 331 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 332 | // Consume the parsed stats log entry that already matched the "what" of the metric. |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 333 | virtual void onMatchedLogEventLocked(const size_t matcherIndex, const LogEvent& event); |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 334 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 335 | mutable std::mutex mMutex; |
Yangster-mac | 849dfdc2 | 2018-10-12 15:41:45 -0700 | [diff] [blame] | 336 | |
| 337 | struct Activation { |
| 338 | Activation() : ttl_ns(0), activation_ns(0), state(ActivationState::kNotActive) {} |
| 339 | |
| 340 | int64_t ttl_ns; |
| 341 | int64_t activation_ns; |
| 342 | ActivationState state; |
| 343 | }; |
| 344 | // When the metric producer has multiple activations, these activations are ORed to determine |
| 345 | // whether the metric producer is ready to generate metrics. |
| 346 | std::unordered_map<int, Activation> mEventActivationMap; |
| 347 | |
| 348 | bool mIsActive; |
| 349 | |
| 350 | FRIEND_TEST(MetricActivationE2eTest, TestCountMetric); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 351 | }; |
| 352 | |
| 353 | } // namespace statsd |
| 354 | } // namespace os |
| 355 | } // namespace android |
| 356 | #endif // METRIC_PRODUCER_H |