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 | |
| 37 | // 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] | 38 | // writing the report to dropbox. MetricProducers should respond to package changes as required in |
| 39 | // PackageInfoListener, but if none of the metrics are slicing by package name, then the update can |
| 40 | // be a no-op. |
David Chen | f12b5c6 | 2017-10-13 15:54:03 -0700 | [diff] [blame] | 41 | class MetricProducer : public virtual PackageInfoListener { |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 42 | public: |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 43 | MetricProducer(const int64_t& metricId, const ConfigKey& key, const int64_t startTimeNs, |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 44 | const int conditionIndex, const sp<ConditionWizard>& wizard) |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 45 | : mMetricId(metricId), |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 46 | mConfigKey(key), |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 47 | mStartTimeNs(startTimeNs), |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 48 | mCurrentBucketStartTimeNs(startTimeNs), |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 49 | mCurrentBucketNum(0), |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 50 | mCondition(conditionIndex >= 0 ? false : true), |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 51 | mConditionSliced(false), |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 52 | mWizard(wizard), |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 53 | mConditionTrackerIndex(conditionIndex){}; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 54 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 55 | virtual ~MetricProducer(){}; |
| 56 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 57 | /** |
| 58 | * Forces this metric to split into a partial bucket right now. If we're past a full bucket, we |
| 59 | * first call the standard flushing code to flush up to the latest full bucket. Then we call |
| 60 | * the flush again when the end timestamp is forced to be now, and then after flushing, update |
| 61 | * the start timestamp to be now. |
| 62 | */ |
| 63 | void notifyAppUpgrade(const uint64_t& eventTimeNs, const string& apk, const int uid, |
| 64 | const int64_t version) override { |
| 65 | std::lock_guard<std::mutex> lock(mMutex); |
| 66 | |
| 67 | if (eventTimeNs > getCurrentBucketEndTimeNs()) { |
| 68 | // Flush full buckets on the normal path up to the latest bucket boundary. |
| 69 | flushIfNeededLocked(eventTimeNs); |
| 70 | } |
| 71 | // Now flush a partial bucket. |
| 72 | flushCurrentBucketLocked(eventTimeNs); |
| 73 | mCurrentBucketStartTimeNs = eventTimeNs; |
| 74 | // Don't update the current bucket number so that the anomaly tracker knows this bucket |
| 75 | // is a partial bucket and can merge it with the previous bucket. |
| 76 | }; |
| 77 | |
| 78 | void notifyAppRemoved(const uint64_t& eventTimeNs, const string& apk, const int uid) override{ |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 79 | // TODO: Implement me. |
| 80 | }; |
| 81 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 82 | void onUidMapReceived(const uint64_t& eventTimeNs) override{ |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 83 | // TODO: Implement me. |
| 84 | }; |
| 85 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 86 | // 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] | 87 | void onMatchedLogEvent(const size_t matcherIndex, const LogEvent& event) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 88 | std::lock_guard<std::mutex> lock(mMutex); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 89 | onMatchedLogEventLocked(matcherIndex, event); |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 90 | } |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 91 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 92 | void onConditionChanged(const bool condition, const uint64_t eventTime) { |
| 93 | std::lock_guard<std::mutex> lock(mMutex); |
| 94 | onConditionChangedLocked(condition, eventTime); |
| 95 | } |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 96 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 97 | void onSlicedConditionMayChange(const uint64_t eventTime) { |
| 98 | std::lock_guard<std::mutex> lock(mMutex); |
| 99 | onSlicedConditionMayChangeLocked(eventTime); |
| 100 | } |
| 101 | |
| 102 | bool isConditionSliced() const { |
| 103 | std::lock_guard<std::mutex> lock(mMutex); |
| 104 | return mConditionSliced; |
| 105 | }; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 106 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 107 | // 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] | 108 | // This method clears all the past buckets. |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 109 | void onDumpReport(const uint64_t dumpTimeNs, android::util::ProtoOutputStream* protoOutput) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 110 | std::lock_guard<std::mutex> lock(mMutex); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 111 | return onDumpReportLocked(dumpTimeNs, protoOutput); |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 112 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 113 | |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 114 | void dumpStates(FILE* out, bool verbose) const { |
| 115 | std::lock_guard<std::mutex> lock(mMutex); |
| 116 | dumpStatesLocked(out, verbose); |
| 117 | } |
| 118 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 119 | // Returns the memory in bytes currently used to store this metric's data. Does not change |
| 120 | // state. |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 121 | size_t byteSize() const { |
| 122 | std::lock_guard<std::mutex> lock(mMutex); |
| 123 | return byteSizeLocked(); |
| 124 | } |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 125 | |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 126 | virtual sp<AnomalyTracker> addAnomalyTracker(const Alert &alert) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 127 | std::lock_guard<std::mutex> lock(mMutex); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 128 | sp<AnomalyTracker> anomalyTracker = new AnomalyTracker(alert, mConfigKey); |
| 129 | if (anomalyTracker != nullptr) { |
| 130 | mAnomalyTrackers.push_back(anomalyTracker); |
| 131 | } |
| 132 | return anomalyTracker; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | int64_t getBuckeSizeInNs() const { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 136 | std::lock_guard<std::mutex> lock(mMutex); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 137 | return mBucketSizeNs; |
| 138 | } |
| 139 | |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame^] | 140 | // Only needed for unit-testing to override guardrail. |
| 141 | void setBucketSize(int64_t bucketSize) { |
| 142 | mBucketSizeNs = bucketSize; |
| 143 | } |
| 144 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 145 | inline const int64_t& getMetricId() { |
| 146 | return mMetricId; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 149 | protected: |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 150 | virtual void onConditionChangedLocked(const bool condition, const uint64_t eventTime) = 0; |
| 151 | virtual void onSlicedConditionMayChangeLocked(const uint64_t eventTime) = 0; |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 152 | virtual void onDumpReportLocked(const uint64_t dumpTimeNs, |
| 153 | android::util::ProtoOutputStream* protoOutput) = 0; |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 154 | virtual size_t byteSizeLocked() const = 0; |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 155 | virtual void dumpStatesLocked(FILE* out, bool verbose) const = 0; |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 156 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 157 | /** |
| 158 | * Flushes the current bucket if the eventTime is after the current bucket's end time. |
| 159 | */ |
| 160 | virtual void flushIfNeededLocked(const uint64_t& eventTime){}; |
| 161 | |
| 162 | /** |
| 163 | * For metrics that aggregate (ie, every metric producer except for EventMetricProducer), |
| 164 | * we need to be able to flush the current buckets on demand (ie, end the current bucket and |
| 165 | * start new bucket). If this function is called when eventTimeNs is greater than the current |
| 166 | * bucket's end timestamp, than we flush up to the end of the latest full bucket; otherwise, |
| 167 | * we assume that we want to flush a partial bucket. The bucket start timestamp and bucket |
| 168 | * number are not changed by this function. This method should only be called by |
| 169 | * flushIfNeededLocked or the app upgrade handler; the caller MUST update the bucket timestamp |
| 170 | * and bucket number as needed. |
| 171 | */ |
| 172 | virtual void flushCurrentBucketLocked(const uint64_t& eventTimeNs){}; |
| 173 | |
| 174 | // Convenience to compute the current bucket's end time, which is always aligned with the |
| 175 | // start time of the metric. |
| 176 | uint64_t getCurrentBucketEndTimeNs() { |
| 177 | return mStartTimeNs + (mCurrentBucketNum + 1) * mBucketSizeNs; |
| 178 | } |
| 179 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 180 | const int64_t mMetricId; |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 181 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 182 | const ConfigKey mConfigKey; |
| 183 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 184 | // The time when this metric producer was first created. The end time for the current bucket |
| 185 | // can be computed from this based on mCurrentBucketNum. |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 186 | uint64_t mStartTimeNs; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 187 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 188 | // Start time may not be aligned with the start of statsd if there is an app upgrade in the |
| 189 | // middle of a bucket. |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 190 | uint64_t mCurrentBucketStartTimeNs; |
| 191 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 192 | // Used by anomaly detector to track which bucket we are in. This is not sent with the produced |
| 193 | // report. |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 194 | uint64_t mCurrentBucketNum; |
| 195 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 196 | int64_t mBucketSizeNs; |
| 197 | |
| 198 | bool mCondition; |
| 199 | |
| 200 | bool mConditionSliced; |
| 201 | |
| 202 | sp<ConditionWizard> mWizard; |
| 203 | |
| 204 | int mConditionTrackerIndex; |
| 205 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 206 | vector<Matcher> mDimensionsInWhat; // The dimensions_in_what defined in statsd_config |
| 207 | vector<Matcher> mDimensionsInCondition; // The dimensions_in_condition defined in statsd_config |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 208 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 209 | std::vector<Metric2Condition> mMetric2ConditionLinks; |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 210 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 211 | std::vector<sp<AnomalyTracker>> mAnomalyTrackers; |
| 212 | |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 213 | /* |
| 214 | * Individual metrics can implement their own business logic here. All pre-processing is done. |
| 215 | * |
| 216 | * [matcherIndex]: the index of the matcher which matched this event. This is interesting to |
| 217 | * DurationMetric, because it has start/stop/stop_all 3 matchers. |
| 218 | * [eventKey]: the extracted dimension key for the final output. if the metric doesn't have |
| 219 | * dimensions, it will be DEFAULT_DIMENSION_KEY |
| 220 | * [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] | 221 | * target event (from MetricConditionLink). This is passed to individual metrics |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 222 | * because DurationMetric needs it to be cached. |
| 223 | * [condition]: whether condition is met. If condition is sliced, this is the result coming from |
| 224 | * query with ConditionWizard; If condition is not sliced, this is the |
| 225 | * nonSlicedCondition. |
| 226 | * [event]: the log event, just in case the metric needs its data, e.g., EventMetric. |
| 227 | */ |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 228 | virtual void onMatchedLogEventInternalLocked( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 229 | const size_t matcherIndex, const MetricDimensionKey& eventKey, |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 230 | const ConditionKey& conditionKey, bool condition, |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 231 | const LogEvent& event) = 0; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 232 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 233 | // 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] | 234 | void onMatchedLogEventLocked(const size_t matcherIndex, const LogEvent& event); |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 235 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 236 | mutable std::mutex mMutex; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 237 | }; |
| 238 | |
| 239 | } // namespace statsd |
| 240 | } // namespace os |
| 241 | } // namespace android |
| 242 | #endif // METRIC_PRODUCER_H |