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