Yangster | 1d4d686 | 2017-10-31 12:58:51 -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 | #pragma once |
| 18 | |
| 19 | #include <unordered_map> |
| 20 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 21 | #include <android/util/ProtoOutputStream.h> |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 22 | #include <gtest/gtest_prod.h> |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 23 | #include "../condition/ConditionTracker.h" |
| 24 | #include "../external/PullDataReceiver.h" |
| 25 | #include "../external/StatsPullerManager.h" |
| 26 | #include "../matchers/matcher_util.h" |
Yangster-mac | 32f07af | 2018-10-13 17:08:11 -0700 | [diff] [blame] | 27 | #include "../matchers/EventMatcherWizard.h" |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 28 | #include "MetricProducer.h" |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 29 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 30 | #include "../stats_util.h" |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | namespace os { |
| 34 | namespace statsd { |
| 35 | |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 36 | struct GaugeAtom { |
Bookatz | fe2dde8 | 2018-08-28 13:24:40 -0700 | [diff] [blame] | 37 | GaugeAtom(std::shared_ptr<vector<FieldValue>> fields, int64_t elapsedTimeNs) |
| 38 | : mFields(fields), mElapsedTimestamps(elapsedTimeNs) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 39 | } |
| 40 | std::shared_ptr<vector<FieldValue>> mFields; |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 41 | int64_t mElapsedTimestamps; |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 42 | }; |
| 43 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 44 | struct GaugeBucket { |
| 45 | int64_t mBucketStartNs; |
| 46 | int64_t mBucketEndNs; |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 47 | std::vector<GaugeAtom> mGaugeAtoms; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 48 | }; |
| 49 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 50 | typedef std::unordered_map<MetricDimensionKey, std::vector<GaugeAtom>> |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 51 | DimToGaugeAtomsMap; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 52 | |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 53 | // This gauge metric producer first register the puller to automatically pull the gauge at the |
| 54 | // beginning of each bucket. If the condition is met, insert it to the bucket info. Otherwise |
| 55 | // proactively pull the gauge when the condition is changed to be true. Therefore, the gauge metric |
| 56 | // producer always reports the guage at the earliest time of the bucket when the condition is met. |
| 57 | class GaugeMetricProducer : public virtual MetricProducer, public virtual PullDataReceiver { |
| 58 | public: |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 59 | GaugeMetricProducer(const ConfigKey& key, const GaugeMetric& gaugeMetric, |
Yangster-mac | 32f07af | 2018-10-13 17:08:11 -0700 | [diff] [blame] | 60 | const int conditionIndex, const sp<ConditionWizard>& conditionWizard, |
| 61 | const int whatMatcherIndex, |
| 62 | const sp<EventMatcherWizard>& matcherWizard, |
Chenjie Yu | 8858897 | 2018-08-03 09:49:22 -0700 | [diff] [blame] | 63 | const int pullTagId, const int triggerAtomId, const int atomId, |
| 64 | const int64_t timeBaseNs, const int64_t startTimeNs, |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 65 | const sp<StatsPullerManager>& pullerManager); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 66 | |
| 67 | virtual ~GaugeMetricProducer(); |
| 68 | |
| 69 | // Handles when the pulled data arrives. |
Olivier Gaillard | c5f11c4 | 2019-02-05 12:44:58 +0000 | [diff] [blame^] | 70 | void onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& data, |
| 71 | bool pullSuccess) override; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 72 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 73 | // GaugeMetric needs to immediately trigger another pull when we create the partial bucket. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 74 | void notifyAppUpgrade(const int64_t& eventTimeNs, const string& apk, const int uid, |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 75 | const int64_t version) override { |
| 76 | std::lock_guard<std::mutex> lock(mMutex); |
| 77 | |
Chenjie Yu | cd1b797 | 2019-01-16 20:38:15 -0800 | [diff] [blame] | 78 | if (!mSplitBucketForAppUpgrade) { |
| 79 | return; |
| 80 | } |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 81 | if (eventTimeNs > getCurrentBucketEndTimeNs()) { |
| 82 | // Flush full buckets on the normal path up to the latest bucket boundary. |
| 83 | flushIfNeededLocked(eventTimeNs); |
| 84 | } |
| 85 | flushCurrentBucketLocked(eventTimeNs); |
| 86 | mCurrentBucketStartTimeNs = eventTimeNs; |
Chenjie Yu | e077f57 | 2018-11-02 11:51:37 -0700 | [diff] [blame] | 87 | if (mIsPulled && mSamplingType == GaugeMetric::RANDOM_ONE_SAMPLE) { |
Yangster-mac | 32f07af | 2018-10-13 17:08:11 -0700 | [diff] [blame] | 88 | pullAndMatchEventsLocked(eventTimeNs); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 89 | } |
| 90 | }; |
| 91 | |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 92 | protected: |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 93 | void onMatchedLogEventInternalLocked( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 94 | const size_t matcherIndex, const MetricDimensionKey& eventKey, |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 95 | const ConditionKey& conditionKey, bool condition, |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 96 | const LogEvent& event) override; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 97 | |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 98 | private: |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 99 | void onDumpReportLocked(const int64_t dumpTimeNs, |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 100 | const bool include_current_partial_bucket, |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 101 | const bool erase_data, |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 102 | std::set<string> *str_set, |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 103 | android::util::ProtoOutputStream* protoOutput) override; |
Yangster-mac | a802d73 | 2018-04-24 07:50:38 -0700 | [diff] [blame] | 104 | void clearPastBucketsLocked(const int64_t dumpTimeNs) override; |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 105 | |
| 106 | // Internal interface to handle condition change. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 107 | void onConditionChangedLocked(const bool conditionMet, const int64_t eventTime) override; |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 108 | |
| 109 | // Internal interface to handle sliced condition change. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 110 | void onSlicedConditionMayChangeLocked(bool overallCondition, const int64_t eventTime) override; |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 111 | |
| 112 | // Internal function to calculate the current used bytes. |
| 113 | size_t byteSizeLocked() const override; |
| 114 | |
Yangster-mac | a78d008 | 2018-03-12 12:02:56 -0700 | [diff] [blame] | 115 | void dumpStatesLocked(FILE* out, bool verbose) const override; |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 116 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 117 | void dropDataLocked(const int64_t dropTimeNs) override; |
Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 118 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 119 | // Util function to flush the old packet. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 120 | void flushIfNeededLocked(const int64_t& eventTime) override; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 121 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 122 | void flushCurrentBucketLocked(const int64_t& eventTimeNs) override; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 123 | |
Yangster-mac | 32f07af | 2018-10-13 17:08:11 -0700 | [diff] [blame] | 124 | void pullAndMatchEventsLocked(const int64_t timestampNs); |
| 125 | |
| 126 | const int mWhatMatcherIndex; |
| 127 | |
| 128 | sp<EventMatcherWizard> mEventMatcherWizard; |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 129 | |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 130 | sp<StatsPullerManager> mPullerManager; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 131 | // tagId for pulled data. -1 if this is not pulled |
| 132 | const int mPullTagId; |
| 133 | |
Chenjie Yu | 8858897 | 2018-08-03 09:49:22 -0700 | [diff] [blame] | 134 | // tagId for atoms that trigger the pulling, if any |
| 135 | const int mTriggerAtomId; |
| 136 | |
| 137 | // tagId for output atom |
| 138 | const int mAtomId; |
| 139 | |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 140 | // if this is pulled metric |
| 141 | const bool mIsPulled; |
| 142 | |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 143 | // Save the past buckets and we can clear when the StatsLogReport is dumped. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 144 | std::unordered_map<MetricDimensionKey, std::vector<GaugeBucket>> mPastBuckets; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 145 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 146 | // The current partial bucket. |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 147 | std::shared_ptr<DimToGaugeAtomsMap> mCurrentSlicedBucket; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 148 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 149 | // The current full bucket for anomaly detection. This is updated to the latest value seen for |
| 150 | // this slice (ie, for partial buckets, we use the last partial bucket in this full bucket). |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 151 | std::shared_ptr<DimToValMap> mCurrentSlicedBucketForAnomaly; |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 152 | |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame] | 153 | // Pairs of (elapsed start, elapsed end) denoting buckets that were skipped. |
| 154 | std::list<std::pair<int64_t, int64_t>> mSkippedBuckets; |
| 155 | |
| 156 | const int64_t mMinBucketSizeNs; |
| 157 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 158 | // Translate Atom based bucket to single numeric value bucket for anomaly and updates the map |
| 159 | // for each slice with the latest value. |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 160 | void updateCurrentSlicedBucketForAnomaly(); |
| 161 | |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 162 | // Whitelist of fields to report. Empty means all are reported. |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 163 | std::vector<Matcher> mFieldMatchers; |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 164 | |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame] | 165 | GaugeMetric::SamplingType mSamplingType; |
| 166 | |
Chenjie Yu | 0bd73db | 2018-12-16 07:37:04 -0800 | [diff] [blame] | 167 | const int64_t mMaxPullDelayNs; |
| 168 | |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 169 | // apply a whitelist on the original input |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 170 | std::shared_ptr<vector<FieldValue>> getGaugeFields(const LogEvent& event); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 171 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 172 | // Util function to check whether the specified dimension hits the guardrail. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 173 | bool hitGuardRailLocked(const MetricDimensionKey& newKey); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 174 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 175 | static const size_t kBucketSize = sizeof(GaugeBucket{}); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 176 | |
Chenjie Yu | c587505 | 2018-03-09 10:13:11 -0800 | [diff] [blame] | 177 | const size_t mDimensionSoftLimit; |
| 178 | |
| 179 | const size_t mDimensionHardLimit; |
| 180 | |
Yangster-mac | 50b0c9a | 2018-05-10 17:13:12 -0700 | [diff] [blame] | 181 | const size_t mGaugeAtomsPerDimensionLimit; |
| 182 | |
Chenjie Yu | cd1b797 | 2019-01-16 20:38:15 -0800 | [diff] [blame] | 183 | const bool mSplitBucketForAppUpgrade; |
| 184 | |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 185 | FRIEND_TEST(GaugeMetricProducerTest, TestPulledEventsWithCondition); |
| 186 | FRIEND_TEST(GaugeMetricProducerTest, TestPulledEventsWithSlicedCondition); |
| 187 | FRIEND_TEST(GaugeMetricProducerTest, TestPulledEventsNoCondition); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 188 | FRIEND_TEST(GaugeMetricProducerTest, TestPushedEventsWithUpgrade); |
| 189 | FRIEND_TEST(GaugeMetricProducerTest, TestPulledWithUpgrade); |
Chenjie Yu | cd1b797 | 2019-01-16 20:38:15 -0800 | [diff] [blame] | 190 | FRIEND_TEST(GaugeMetricProducerTest, TestPulledWithAppUpgradeDisabled); |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 191 | FRIEND_TEST(GaugeMetricProducerTest, TestPulledEventsAnomalyDetection); |
| 192 | FRIEND_TEST(GaugeMetricProducerTest, TestFirstBucket); |
Chenjie Yu | 8858897 | 2018-08-03 09:49:22 -0700 | [diff] [blame] | 193 | FRIEND_TEST(GaugeMetricProducerTest, TestPullOnTrigger); |
Chenjie Yu | 4c31f67 | 2018-08-21 15:42:40 -0700 | [diff] [blame] | 194 | FRIEND_TEST(GaugeMetricProducerTest, TestRemoveDimensionInOutput); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | } // namespace statsd |
| 198 | } // namespace os |
| 199 | } // namespace android |