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 
21 #include <android/util/ProtoOutputStream.h>
22 #include <gtest/gtest_prod.h>
23 #include "../condition/ConditionTracker.h"
24 #include "../external/PullDataReceiver.h"
25 #include "../external/StatsPullerManager.h"
26 #include "../matchers/matcher_util.h"
27 #include "../matchers/EventMatcherWizard.h"
28 #include "MetricProducer.h"
29 #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
30 #include "../stats_util.h"
31 
32 namespace android {
33 namespace os {
34 namespace statsd {
35 
36 struct GaugeAtom {
GaugeAtomGaugeAtom37     GaugeAtom(std::shared_ptr<vector<FieldValue>> fields, int64_t elapsedTimeNs)
38         : mFields(fields), mElapsedTimestampNs(elapsedTimeNs) {
39     }
40     std::shared_ptr<vector<FieldValue>> mFields;
41     int64_t mElapsedTimestampNs;
42 };
43 
44 struct GaugeBucket {
45     int64_t mBucketStartNs;
46     int64_t mBucketEndNs;
47     std::vector<GaugeAtom> mGaugeAtoms;
48 };
49 
50 typedef std::unordered_map<MetricDimensionKey, std::vector<GaugeAtom>>
51     DimToGaugeAtomsMap;
52 
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:
59     GaugeMetricProducer(
60             const ConfigKey& key, const GaugeMetric& gaugeMetric, const int conditionIndex,
61             const vector<ConditionState>& initialConditionCache,
62             const sp<ConditionWizard>& conditionWizard, const int whatMatcherIndex,
63             const sp<EventMatcherWizard>& matcherWizard, const int pullTagId,
64             const int triggerAtomId, const int atomId, const int64_t timeBaseNs,
65             const int64_t startTimeNs, const sp<StatsPullerManager>& pullerManager,
66             const std::unordered_map<int, std::shared_ptr<Activation>>& eventActivationMap = {},
67             const std::unordered_map<int, std::vector<std::shared_ptr<Activation>>>&
68                     eventDeactivationMap = {});
69 
70     virtual ~GaugeMetricProducer();
71 
72     // Handles when the pulled data arrives.
73     void onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& data,
74                       bool pullSuccess, int64_t originalPullTimeNs) override;
75 
76     // GaugeMetric needs to immediately trigger another pull when we create the partial bucket.
notifyAppUpgrade(const int64_t & eventTimeNs)77     void notifyAppUpgrade(const int64_t& eventTimeNs) override {
78         std::lock_guard<std::mutex> lock(mMutex);
79 
80         if (!mSplitBucketForAppUpgrade) {
81             return;
82         }
83         flushLocked(eventTimeNs);
84         if (mIsPulled && mSamplingType == GaugeMetric::RANDOM_ONE_SAMPLE) {
85             pullAndMatchEventsLocked(eventTimeNs);
86         }
87     };
88 
89     // GaugeMetric needs to immediately trigger another pull when we create the partial bucket.
onStatsdInitCompleted(const int64_t & eventTimeNs)90     void onStatsdInitCompleted(const int64_t& eventTimeNs) override {
91         std::lock_guard<std::mutex> lock(mMutex);
92 
93         flushLocked(eventTimeNs);
94         if (mIsPulled && mSamplingType == GaugeMetric::RANDOM_ONE_SAMPLE) {
95             pullAndMatchEventsLocked(eventTimeNs);
96         }
97     };
98 
99 protected:
100     void onMatchedLogEventInternalLocked(
101             const size_t matcherIndex, const MetricDimensionKey& eventKey,
102             const ConditionKey& conditionKey, bool condition, const LogEvent& event,
103             const std::map<int, HashableDimensionKey>& statePrimaryKeys) override;
104 
105 private:
106     void onDumpReportLocked(const int64_t dumpTimeNs,
107                             const bool include_current_partial_bucket,
108                             const bool erase_data,
109                             const DumpLatency dumpLatency,
110                             std::set<string> *str_set,
111                             android::util::ProtoOutputStream* protoOutput) override;
112     void clearPastBucketsLocked(const int64_t dumpTimeNs) override;
113 
114     // Internal interface to handle condition change.
115     void onConditionChangedLocked(const bool conditionMet, const int64_t eventTime) override;
116 
117     // Internal interface to handle active state change.
118     void onActiveStateChangedLocked(const int64_t& eventTimeNs) override;
119 
120     // Internal interface to handle sliced condition change.
121     void onSlicedConditionMayChangeLocked(bool overallCondition, const int64_t eventTime) override;
122 
123     // Internal function to calculate the current used bytes.
124     size_t byteSizeLocked() const override;
125 
126     void dumpStatesLocked(FILE* out, bool verbose) const override;
127 
128     void dropDataLocked(const int64_t dropTimeNs) override;
129 
130     // Util function to flush the old packet.
131     void flushIfNeededLocked(const int64_t& eventTime) override;
132 
133     void flushCurrentBucketLocked(const int64_t& eventTimeNs,
134                                   const int64_t& nextBucketStartTimeNs) override;
135 
136     void prepareFirstBucketLocked() override;
137 
138     void pullAndMatchEventsLocked(const int64_t timestampNs);
139 
140     const int mWhatMatcherIndex;
141 
142     sp<EventMatcherWizard> mEventMatcherWizard;
143 
144     sp<StatsPullerManager> mPullerManager;
145     // tagId for pulled data. -1 if this is not pulled
146     const int mPullTagId;
147 
148     // tagId for atoms that trigger the pulling, if any
149     const int mTriggerAtomId;
150 
151     // tagId for output atom
152     const int mAtomId;
153 
154     // if this is pulled metric
155     const bool mIsPulled;
156 
157     // Save the past buckets and we can clear when the StatsLogReport is dumped.
158     std::unordered_map<MetricDimensionKey, std::vector<GaugeBucket>> mPastBuckets;
159 
160     // The current partial bucket.
161     std::shared_ptr<DimToGaugeAtomsMap> mCurrentSlicedBucket;
162 
163     // The current full bucket for anomaly detection. This is updated to the latest value seen for
164     // this slice (ie, for partial buckets, we use the last partial bucket in this full bucket).
165     std::shared_ptr<DimToValMap> mCurrentSlicedBucketForAnomaly;
166 
167     const int64_t mMinBucketSizeNs;
168 
169     // Translate Atom based bucket to single numeric value bucket for anomaly and updates the map
170     // for each slice with the latest value.
171     void updateCurrentSlicedBucketForAnomaly();
172 
173     // Whitelist of fields to report. Empty means all are reported.
174     std::vector<Matcher> mFieldMatchers;
175 
176     GaugeMetric::SamplingType mSamplingType;
177 
178     const int64_t mMaxPullDelayNs;
179 
180     // apply a whitelist on the original input
181     std::shared_ptr<vector<FieldValue>> getGaugeFields(const LogEvent& event);
182 
183     // Util function to check whether the specified dimension hits the guardrail.
184     bool hitGuardRailLocked(const MetricDimensionKey& newKey);
185 
186     static const size_t kBucketSize = sizeof(GaugeBucket{});
187 
188     const size_t mDimensionSoftLimit;
189 
190     const size_t mDimensionHardLimit;
191 
192     const size_t mGaugeAtomsPerDimensionLimit;
193 
194     const bool mSplitBucketForAppUpgrade;
195 
196     FRIEND_TEST(GaugeMetricProducerTest, TestPulledEventsWithCondition);
197     FRIEND_TEST(GaugeMetricProducerTest, TestPulledEventsWithSlicedCondition);
198     FRIEND_TEST(GaugeMetricProducerTest, TestPulledEventsNoCondition);
199     FRIEND_TEST(GaugeMetricProducerTest, TestPulledWithAppUpgradeDisabled);
200     FRIEND_TEST(GaugeMetricProducerTest, TestPulledEventsAnomalyDetection);
201     FRIEND_TEST(GaugeMetricProducerTest, TestFirstBucket);
202     FRIEND_TEST(GaugeMetricProducerTest, TestPullOnTrigger);
203     FRIEND_TEST(GaugeMetricProducerTest, TestRemoveDimensionInOutput);
204 
205     FRIEND_TEST(GaugeMetricProducerTest_PartialBucket, TestPushedEvents);
206     FRIEND_TEST(GaugeMetricProducerTest_PartialBucket, TestPulled);
207 };
208 
209 }  // namespace statsd
210 }  // namespace os
211 }  // namespace android
212