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" |
| 27 | #include "MetricProducer.h" |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 28 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 29 | #include "../stats_util.h" |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | namespace os { |
| 33 | namespace statsd { |
| 34 | |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame^] | 35 | struct GaugeAtom { |
| 36 | std::shared_ptr<FieldValueMap> mFields; |
| 37 | int64_t mTimestamps; |
| 38 | }; |
| 39 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 40 | struct GaugeBucket { |
| 41 | int64_t mBucketStartNs; |
| 42 | int64_t mBucketEndNs; |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame^] | 43 | std::vector<GaugeAtom> mGaugeAtoms; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 44 | uint64_t mBucketNum; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 45 | }; |
| 46 | |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame^] | 47 | typedef std::unordered_map<HashableDimensionKey, std::vector<GaugeAtom>> |
| 48 | DimToGaugeAtomsMap; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 49 | |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 50 | // This gauge metric producer first register the puller to automatically pull the gauge at the |
| 51 | // beginning of each bucket. If the condition is met, insert it to the bucket info. Otherwise |
| 52 | // proactively pull the gauge when the condition is changed to be true. Therefore, the gauge metric |
| 53 | // producer always reports the guage at the earliest time of the bucket when the condition is met. |
| 54 | class GaugeMetricProducer : public virtual MetricProducer, public virtual PullDataReceiver { |
| 55 | public: |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame^] | 56 | GaugeMetricProducer(const ConfigKey& key, const GaugeMetric& gaugeMetric, |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 57 | const int conditionIndex, const sp<ConditionWizard>& wizard, |
Yangster-mac | a070b6a | 2018-01-04 13:28:38 -0800 | [diff] [blame] | 58 | const int pullTagId, const int64_t startTimeNs); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 59 | |
| 60 | virtual ~GaugeMetricProducer(); |
| 61 | |
| 62 | // Handles when the pulled data arrives. |
| 63 | void onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& data) override; |
| 64 | |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 65 | protected: |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 66 | void onMatchedLogEventInternalLocked( |
| 67 | const size_t matcherIndex, const HashableDimensionKey& eventKey, |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 68 | const ConditionKey& conditionKey, bool condition, |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 69 | const LogEvent& event) override; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 70 | |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 71 | private: |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 72 | void onDumpReportLocked(const uint64_t dumpTimeNs, |
| 73 | android::util::ProtoOutputStream* protoOutput) override; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 74 | void onDumpReportLocked(const uint64_t dumpTimeNs, StatsLogReport* report) override; |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 75 | |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 76 | // for testing |
| 77 | GaugeMetricProducer(const ConfigKey& key, const GaugeMetric& gaugeMetric, |
| 78 | const int conditionIndex, const sp<ConditionWizard>& wizard, |
Yangster-mac | a070b6a | 2018-01-04 13:28:38 -0800 | [diff] [blame] | 79 | const int pullTagId, const uint64_t startTimeNs, |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 80 | std::shared_ptr<StatsPullerManager> statsPullerManager); |
| 81 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 82 | // Internal interface to handle condition change. |
| 83 | void onConditionChangedLocked(const bool conditionMet, const uint64_t eventTime) override; |
| 84 | |
| 85 | // Internal interface to handle sliced condition change. |
| 86 | void onSlicedConditionMayChangeLocked(const uint64_t eventTime) override; |
| 87 | |
| 88 | // Internal function to calculate the current used bytes. |
| 89 | size_t byteSizeLocked() const override; |
| 90 | |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 91 | void dumpStatesLocked(FILE* out, bool verbose) const override{}; |
| 92 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 93 | // Util function to flush the old packet. |
| 94 | void flushIfNeededLocked(const uint64_t& eventTime); |
| 95 | |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 96 | std::shared_ptr<StatsPullerManager> mStatsPullerManager; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 97 | // tagId for pulled data. -1 if this is not pulled |
| 98 | const int mPullTagId; |
| 99 | |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 100 | // Save the past buckets and we can clear when the StatsLogReport is dumped. |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 101 | // TODO: Add a lock to mPastBuckets. |
| 102 | std::unordered_map<HashableDimensionKey, std::vector<GaugeBucket>> mPastBuckets; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 103 | |
| 104 | // The current bucket. |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame^] | 105 | std::shared_ptr<DimToGaugeAtomsMap> mCurrentSlicedBucket; |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 106 | |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 107 | // The current bucket for anomaly detection. |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 108 | std::shared_ptr<DimToValMap> mCurrentSlicedBucketForAnomaly; |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 109 | |
| 110 | // Translate Atom based bucket to single numeric value bucket for anomaly |
| 111 | void updateCurrentSlicedBucketForAnomaly(); |
| 112 | |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 113 | // Whitelist of fields to report. Empty means all are reported. |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 114 | FieldFilter mFieldFilter; |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 115 | |
Yangster-mac | 34ea110 | 2018-01-29 12:40:55 -0800 | [diff] [blame^] | 116 | GaugeMetric::SamplingType mSamplingType; |
| 117 | |
Chenjie Yu | d9dfda7 | 2017-12-11 17:41:20 -0800 | [diff] [blame] | 118 | // apply a whitelist on the original input |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 119 | std::shared_ptr<FieldValueMap> getGaugeFields(const LogEvent& event); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 120 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 121 | // Util function to check whether the specified dimension hits the guardrail. |
| 122 | bool hitGuardRailLocked(const HashableDimensionKey& newKey); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 123 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 124 | static const size_t kBucketSize = sizeof(GaugeBucket{}); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 125 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 126 | FRIEND_TEST(GaugeMetricProducerTest, TestWithCondition); |
| 127 | FRIEND_TEST(GaugeMetricProducerTest, TestNoCondition); |
| 128 | FRIEND_TEST(GaugeMetricProducerTest, TestAnomalyDetection); |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | } // namespace statsd |
| 132 | } // namespace os |
| 133 | } // namespace android |