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 | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 30 | #include <unordered_map> |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | namespace os { |
| 34 | namespace statsd { |
| 35 | |
| 36 | // 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] | 37 | // writing the report to dropbox. MetricProducers should respond to package changes as required in |
| 38 | // PackageInfoListener, but if none of the metrics are slicing by package name, then the update can |
| 39 | // be a no-op. |
David Chen | f12b5c6 | 2017-10-13 15:54:03 -0700 | [diff] [blame] | 40 | class MetricProducer : public virtual PackageInfoListener { |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 41 | public: |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame^] | 42 | MetricProducer(const int64_t& metricId, const ConfigKey& key, const int64_t startTimeNs, |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 43 | const int conditionIndex, const sp<ConditionWizard>& wizard) |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame^] | 44 | : mMetricId(metricId), |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 45 | mConfigKey(key), |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 46 | mStartTimeNs(startTimeNs), |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 47 | mCurrentBucketStartTimeNs(startTimeNs), |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 48 | mCurrentBucketNum(0), |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 49 | mCondition(conditionIndex >= 0 ? false : true), |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 50 | mConditionSliced(false), |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 51 | mWizard(wizard), |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 52 | mConditionTrackerIndex(conditionIndex){}; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 53 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 54 | virtual ~MetricProducer(){}; |
| 55 | |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 56 | void notifyAppUpgrade(const string& apk, const int uid, const int64_t version) override{ |
| 57 | // TODO: Implement me. |
| 58 | }; |
| 59 | |
| 60 | void notifyAppRemoved(const string& apk, const int uid) override{ |
| 61 | // TODO: Implement me. |
| 62 | }; |
| 63 | |
| 64 | void onUidMapReceived() override{ |
| 65 | // TODO: Implement me. |
| 66 | }; |
| 67 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 68 | // 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] | 69 | void onMatchedLogEvent(const size_t matcherIndex, const LogEvent& event) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 70 | std::lock_guard<std::mutex> lock(mMutex); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 71 | onMatchedLogEventLocked(matcherIndex, event); |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 72 | } |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 73 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 74 | void onConditionChanged(const bool condition, const uint64_t eventTime) { |
| 75 | std::lock_guard<std::mutex> lock(mMutex); |
| 76 | onConditionChangedLocked(condition, eventTime); |
| 77 | } |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 78 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 79 | void onSlicedConditionMayChange(const uint64_t eventTime) { |
| 80 | std::lock_guard<std::mutex> lock(mMutex); |
| 81 | onSlicedConditionMayChangeLocked(eventTime); |
| 82 | } |
| 83 | |
| 84 | bool isConditionSliced() const { |
| 85 | std::lock_guard<std::mutex> lock(mMutex); |
| 86 | return mConditionSliced; |
| 87 | }; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 88 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 89 | // Output the metrics data to [protoOutput]. All metrics reports end with the same timestamp. |
| 90 | void onDumpReport(const uint64_t dumpTimeNs, android::util::ProtoOutputStream* protoOutput) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 91 | std::lock_guard<std::mutex> lock(mMutex); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 92 | return onDumpReportLocked(dumpTimeNs, protoOutput); |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 93 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 94 | void onDumpReport(const uint64_t dumpTimeNs, StatsLogReport* report) { |
| 95 | std::lock_guard<std::mutex> lock(mMutex); |
| 96 | return onDumpReportLocked(dumpTimeNs, report); |
| 97 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 98 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 99 | // Returns the memory in bytes currently used to store this metric's data. Does not change |
| 100 | // state. |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 101 | size_t byteSize() const { |
| 102 | std::lock_guard<std::mutex> lock(mMutex); |
| 103 | return byteSizeLocked(); |
| 104 | } |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 105 | |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 106 | virtual sp<AnomalyTracker> addAnomalyTracker(const Alert &alert) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 107 | std::lock_guard<std::mutex> lock(mMutex); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 108 | sp<AnomalyTracker> anomalyTracker = new AnomalyTracker(alert, mConfigKey); |
| 109 | if (anomalyTracker != nullptr) { |
| 110 | mAnomalyTrackers.push_back(anomalyTracker); |
| 111 | } |
| 112 | return anomalyTracker; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | int64_t getBuckeSizeInNs() const { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 116 | std::lock_guard<std::mutex> lock(mMutex); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 117 | return mBucketSizeNs; |
| 118 | } |
| 119 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame^] | 120 | inline const int64_t& getMetricId() { |
| 121 | return mMetricId; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 124 | protected: |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 125 | virtual void onConditionChangedLocked(const bool condition, const uint64_t eventTime) = 0; |
| 126 | virtual void onSlicedConditionMayChangeLocked(const uint64_t eventTime) = 0; |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 127 | virtual void onDumpReportLocked(const uint64_t dumpTimeNs, |
| 128 | android::util::ProtoOutputStream* protoOutput) = 0; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 129 | virtual void onDumpReportLocked(const uint64_t dumpTimeNs, StatsLogReport* report) = 0; |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 130 | virtual size_t byteSizeLocked() const = 0; |
| 131 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame^] | 132 | const int64_t mMetricId; |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 133 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 134 | const ConfigKey mConfigKey; |
| 135 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 136 | // The start time for the current in memory metrics data. |
| 137 | uint64_t mStartTimeNs; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 138 | |
| 139 | uint64_t mCurrentBucketStartTimeNs; |
| 140 | |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 141 | uint64_t mCurrentBucketNum; |
| 142 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 143 | int64_t mBucketSizeNs; |
| 144 | |
| 145 | bool mCondition; |
| 146 | |
| 147 | bool mConditionSliced; |
| 148 | |
| 149 | sp<ConditionWizard> mWizard; |
| 150 | |
| 151 | int mConditionTrackerIndex; |
| 152 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 153 | FieldMatcher mDimensions; // The dimension defined in statsd_config |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 154 | |
Stefan Lafon | a5b5191 | 2017-12-05 21:43:52 -0800 | [diff] [blame] | 155 | std::vector<MetricConditionLink> mConditionLinks; |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 156 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 157 | std::vector<sp<AnomalyTracker>> mAnomalyTrackers; |
| 158 | |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 159 | /* |
| 160 | * Individual metrics can implement their own business logic here. All pre-processing is done. |
| 161 | * |
| 162 | * [matcherIndex]: the index of the matcher which matched this event. This is interesting to |
| 163 | * DurationMetric, because it has start/stop/stop_all 3 matchers. |
| 164 | * [eventKey]: the extracted dimension key for the final output. if the metric doesn't have |
| 165 | * dimensions, it will be DEFAULT_DIMENSION_KEY |
| 166 | * [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] | 167 | * target event (from MetricConditionLink). This is passed to individual metrics |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 168 | * because DurationMetric needs it to be cached. |
| 169 | * [condition]: whether condition is met. If condition is sliced, this is the result coming from |
| 170 | * query with ConditionWizard; If condition is not sliced, this is the |
| 171 | * nonSlicedCondition. |
| 172 | * [event]: the log event, just in case the metric needs its data, e.g., EventMetric. |
| 173 | */ |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 174 | virtual void onMatchedLogEventInternalLocked( |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 175 | const size_t matcherIndex, const HashableDimensionKey& eventKey, |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 176 | const ConditionKey& conditionKey, bool condition, |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 177 | const LogEvent& event) = 0; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 178 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 179 | // 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] | 180 | void onMatchedLogEventLocked(const size_t matcherIndex, const LogEvent& event); |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 181 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 182 | mutable std::mutex mMutex; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | } // namespace statsd |
| 186 | } // namespace os |
| 187 | } // namespace android |
| 188 | #endif // METRIC_PRODUCER_H |