Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -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 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 19 | #include <gtest/gtest_prod.h> |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 20 | #include <utils/threads.h> |
| 21 | #include <list> |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 22 | #include "../anomaly/AnomalyTracker.h" |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 23 | #include "../condition/ConditionTracker.h" |
| 24 | #include "../external/PullDataReceiver.h" |
| 25 | #include "../external/StatsPullerManager.h" |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 26 | #include "MetricProducer.h" |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 27 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
| 28 | |
| 29 | namespace android { |
| 30 | namespace os { |
| 31 | namespace statsd { |
| 32 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 33 | struct ValueBucket { |
| 34 | int64_t mBucketStartNs; |
| 35 | int64_t mBucketEndNs; |
| 36 | int64_t mValue; |
| 37 | }; |
| 38 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 39 | class ValueMetricProducer : public virtual MetricProducer, public virtual PullDataReceiver { |
| 40 | public: |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 41 | ValueMetricProducer(const ConfigKey& key, const ValueMetric& valueMetric, |
| 42 | const int conditionIndex, const sp<ConditionWizard>& wizard, |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 43 | const int pullTagId, const int64_t timeBaseNs, const int64_t startTimeNs); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 44 | |
| 45 | virtual ~ValueMetricProducer(); |
| 46 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 47 | void onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& data) override; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 48 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 49 | // ValueMetric needs special logic if it's a pulled atom. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 50 | void notifyAppUpgrade(const int64_t& eventTimeNs, const string& apk, const int uid, |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 51 | const int64_t version) override { |
| 52 | std::lock_guard<std::mutex> lock(mMutex); |
| 53 | |
| 54 | if (mPullTagId != -1) { |
| 55 | vector<shared_ptr<LogEvent>> allData; |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 56 | mStatsPullerManager->Pull(mPullTagId, eventTimeNs, &allData); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 57 | if (allData.size() == 0) { |
| 58 | // This shouldn't happen since this valuemetric is not useful now. |
| 59 | } |
| 60 | |
| 61 | // Pretend the pulled data occurs right before the app upgrade event. |
| 62 | mCondition = false; |
| 63 | for (const auto& data : allData) { |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 64 | data->setElapsedTimestampNs(eventTimeNs - 1); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 65 | onMatchedLogEventLocked(0, *data); |
| 66 | } |
| 67 | |
| 68 | flushCurrentBucketLocked(eventTimeNs); |
| 69 | mCurrentBucketStartTimeNs = eventTimeNs; |
| 70 | |
| 71 | mCondition = true; |
| 72 | for (const auto& data : allData) { |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 73 | data->setElapsedTimestampNs(eventTimeNs); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 74 | onMatchedLogEventLocked(0, *data); |
| 75 | } |
| 76 | } else { // For pushed value metric, we simply flush and reset the current bucket start. |
| 77 | flushCurrentBucketLocked(eventTimeNs); |
| 78 | mCurrentBucketStartTimeNs = eventTimeNs; |
| 79 | } |
| 80 | }; |
| 81 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 82 | protected: |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 83 | void onMatchedLogEventInternalLocked( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 84 | const size_t matcherIndex, const MetricDimensionKey& eventKey, |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 85 | const ConditionKey& conditionKey, bool condition, |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 86 | const LogEvent& event) override; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 87 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 88 | private: |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 89 | void onDumpReportLocked(const int64_t dumpTimeNs, |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 90 | const bool include_current_partial_bucket, |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 91 | android::util::ProtoOutputStream* protoOutput) override; |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 92 | |
| 93 | // Internal interface to handle condition change. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 94 | void onConditionChangedLocked(const bool conditionMet, const int64_t eventTime) override; |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 95 | |
| 96 | // Internal interface to handle sliced condition change. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 97 | void onSlicedConditionMayChangeLocked(bool overallCondition, const int64_t eventTime) override; |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 98 | |
| 99 | // Internal function to calculate the current used bytes. |
| 100 | size_t byteSizeLocked() const override; |
| 101 | |
Yangster-mac | a78d008 | 2018-03-12 12:02:56 -0700 | [diff] [blame] | 102 | void dumpStatesLocked(FILE* out, bool verbose) const override; |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 103 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 104 | // Util function to flush the old packet. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 105 | void flushIfNeededLocked(const int64_t& eventTime) override; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 106 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 107 | void flushCurrentBucketLocked(const int64_t& eventTimeNs) override; |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 108 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 109 | void dropDataLocked(const int64_t dropTimeNs) override; |
Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 110 | |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 111 | const FieldMatcher mValueField; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 112 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 113 | std::shared_ptr<StatsPullerManager> mStatsPullerManager; |
| 114 | |
| 115 | // for testing |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 116 | ValueMetricProducer(const ConfigKey& key, const ValueMetric& valueMetric, |
| 117 | const int conditionIndex, const sp<ConditionWizard>& wizard, |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 118 | const int pullTagId, const int64_t timeBaseNs, const int64_t startTimeNs, |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 119 | std::shared_ptr<StatsPullerManager> statsPullerManager); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 120 | |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 121 | // tagId for pulled data. -1 if this is not pulled |
| 122 | const int mPullTagId; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 123 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 124 | int mField; |
| 125 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 126 | // internal state of a bucket. |
| 127 | typedef struct { |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 128 | // Pulled data always come in pair of <start, end>. This holds the value |
| 129 | // for start. The diff (end - start) is added to sum. |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 130 | int64_t start; |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 131 | // Whether the start data point is updated |
| 132 | bool startUpdated; |
| 133 | // If end data point comes before the start, record this pair as tainted |
| 134 | // and the value is not added to the running sum. |
| 135 | int tainted; |
| 136 | // Running sum of known pairs in this bucket |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 137 | int64_t sum; |
Chenjie Yu | ae63b0a | 2018-04-10 14:59:31 -0700 | [diff] [blame] | 138 | // If this dimension has any non-tainted value. If not, don't report the |
| 139 | // dimension. |
| 140 | bool hasValue; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 141 | } Interval; |
| 142 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 143 | std::unordered_map<MetricDimensionKey, Interval> mCurrentSlicedBucket; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 144 | |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 145 | std::unordered_map<MetricDimensionKey, int64_t> mCurrentFullBucket; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 146 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 147 | // Save the past buckets and we can clear when the StatsLogReport is dumped. |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 148 | // TODO: Add a lock to mPastBuckets. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 149 | std::unordered_map<MetricDimensionKey, std::vector<ValueBucket>> mPastBuckets; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 150 | |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame^] | 151 | // Pairs of (elapsed start, elapsed end) denoting buckets that were skipped. |
| 152 | std::list<std::pair<int64_t, int64_t>> mSkippedBuckets; |
| 153 | |
| 154 | const int64_t mMinBucketSizeNs; |
| 155 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 156 | // Util function to check whether the specified dimension hits the guardrail. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 157 | bool hitGuardRailLocked(const MetricDimensionKey& newKey); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 158 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 159 | static const size_t kBucketSize = sizeof(ValueBucket{}); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 160 | |
Chenjie Yu | c587505 | 2018-03-09 10:13:11 -0800 | [diff] [blame] | 161 | const size_t mDimensionSoftLimit; |
| 162 | |
| 163 | const size_t mDimensionHardLimit; |
| 164 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 165 | FRIEND_TEST(ValueMetricProducerTest, TestNonDimensionalEvents); |
| 166 | FRIEND_TEST(ValueMetricProducerTest, TestEventsWithNonSlicedCondition); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 167 | FRIEND_TEST(ValueMetricProducerTest, TestPushedEventsWithUpgrade); |
| 168 | FRIEND_TEST(ValueMetricProducerTest, TestPulledValueWithUpgrade); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 169 | FRIEND_TEST(ValueMetricProducerTest, TestPushedEventsWithoutCondition); |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 170 | FRIEND_TEST(ValueMetricProducerTest, TestAnomalyDetection); |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 171 | FRIEND_TEST(ValueMetricProducerTest, TestBucketBoundaryNoCondition); |
| 172 | FRIEND_TEST(ValueMetricProducerTest, TestBucketBoundaryWithCondition); |
| 173 | FRIEND_TEST(ValueMetricProducerTest, TestBucketBoundaryWithCondition2); |
| 174 | FRIEND_TEST(ValueMetricProducerTest, TestBucketBoundaryWithCondition3); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | } // namespace statsd |
| 178 | } // namespace os |
| 179 | } // namespace android |