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 
20 #include <shared_mutex>
21 
22 #include "HashableDimensionKey.h"
23 #include "anomaly/AnomalyTracker.h"
24 #include "condition/ConditionWizard.h"
25 #include "config/ConfigKey.h"
26 #include "matchers/matcher_util.h"
27 #include "packages/PackageInfoListener.h"
28 
29 #include <log/logprint.h>
30 #include <utils/RefBase.h>
31 #include <unordered_map>
32 
33 namespace android {
34 namespace os {
35 namespace statsd {
36 
37 // A MetricProducer is responsible for compute one single metrics, creating stats log report, and
38 // writing the report to dropbox. MetricProducers should respond to package changes as required in
39 // PackageInfoListener, but if none of the metrics are slicing by package name, then the update can
40 // be a no-op.
41 class MetricProducer : public virtual PackageInfoListener {
42 public:
MetricProducer(const int64_t & metricId,const ConfigKey & key,const int64_t timeBaseNs,const int conditionIndex,const sp<ConditionWizard> & wizard)43     MetricProducer(const int64_t& metricId, const ConfigKey& key, const int64_t timeBaseNs,
44                    const int conditionIndex, const sp<ConditionWizard>& wizard)
45         : mMetricId(metricId),
46           mConfigKey(key),
47           mTimeBaseNs(timeBaseNs),
48           mCurrentBucketStartTimeNs(timeBaseNs),
49           mCurrentBucketNum(0),
50           mCondition(conditionIndex >= 0 ? false : true),
51           mConditionSliced(false),
52           mWizard(wizard),
53           mConditionTrackerIndex(conditionIndex),
54           mContainANYPositionInDimensionsInWhat(false),
55           mSliceByPositionALL(false),
56           mSameConditionDimensionsInTracker(false),
57           mHasLinksToAllConditionDimensionsInTracker(false) {
58     }
59 
~MetricProducer()60     virtual ~MetricProducer(){};
61 
62     /**
63      * Forces this metric to split into a partial bucket right now. If we're past a full bucket, we
64      * first call the standard flushing code to flush up to the latest full bucket. Then we call
65      * the flush again when the end timestamp is forced to be now, and then after flushing, update
66      * the start timestamp to be now.
67      */
notifyAppUpgrade(const int64_t & eventTimeNs,const string & apk,const int uid,const int64_t version)68     void notifyAppUpgrade(const int64_t& eventTimeNs, const string& apk, const int uid,
69                           const int64_t version) override {
70         std::lock_guard<std::mutex> lock(mMutex);
71 
72         if (eventTimeNs > getCurrentBucketEndTimeNs()) {
73             // Flush full buckets on the normal path up to the latest bucket boundary.
74             flushIfNeededLocked(eventTimeNs);
75         }
76         // Now flush a partial bucket.
77         flushCurrentBucketLocked(eventTimeNs);
78         mCurrentBucketStartTimeNs = eventTimeNs;
79         // Don't update the current bucket number so that the anomaly tracker knows this bucket
80         // is a partial bucket and can merge it with the previous bucket.
81     };
82 
notifyAppRemoved(const int64_t & eventTimeNs,const string & apk,const int uid)83     void notifyAppRemoved(const int64_t& eventTimeNs, const string& apk, const int uid) override{
84         // Force buckets to split on removal also.
85         notifyAppUpgrade(eventTimeNs, apk, uid, 0);
86     };
87 
onUidMapReceived(const int64_t & eventTimeNs)88     void onUidMapReceived(const int64_t& eventTimeNs) override{
89             // Purposefully don't flush partial buckets on a new snapshot.
90             // This occurs if a new user is added/removed or statsd crashes.
91     };
92 
93     // Consume the parsed stats log entry that already matched the "what" of the metric.
onMatchedLogEvent(const size_t matcherIndex,const LogEvent & event)94     void onMatchedLogEvent(const size_t matcherIndex, const LogEvent& event) {
95         std::lock_guard<std::mutex> lock(mMutex);
96         onMatchedLogEventLocked(matcherIndex, event);
97     }
98 
onConditionChanged(const bool condition,const int64_t eventTime)99     void onConditionChanged(const bool condition, const int64_t eventTime) {
100         std::lock_guard<std::mutex> lock(mMutex);
101         onConditionChangedLocked(condition, eventTime);
102     }
103 
onSlicedConditionMayChange(bool overallCondition,const int64_t eventTime)104     void onSlicedConditionMayChange(bool overallCondition, const int64_t eventTime) {
105         std::lock_guard<std::mutex> lock(mMutex);
106         onSlicedConditionMayChangeLocked(overallCondition, eventTime);
107     }
108 
isConditionSliced()109     bool isConditionSliced() const {
110         std::lock_guard<std::mutex> lock(mMutex);
111         return mConditionSliced;
112     };
113 
114     // Output the metrics data to [protoOutput]. All metrics reports end with the same timestamp.
115     // This method clears all the past buckets.
onDumpReport(const int64_t dumpTimeNs,const bool include_current_partial_bucket,std::set<string> * str_set,android::util::ProtoOutputStream * protoOutput)116     void onDumpReport(const int64_t dumpTimeNs,
117                       const bool include_current_partial_bucket,
118                       std::set<string> *str_set,
119                       android::util::ProtoOutputStream* protoOutput) {
120         std::lock_guard<std::mutex> lock(mMutex);
121         return onDumpReportLocked(dumpTimeNs, include_current_partial_bucket, str_set, protoOutput);
122     }
123 
clearPastBuckets(const int64_t dumpTimeNs)124     void clearPastBuckets(const int64_t dumpTimeNs) {
125         std::lock_guard<std::mutex> lock(mMutex);
126         return clearPastBucketsLocked(dumpTimeNs);
127     }
128 
dumpStates(FILE * out,bool verbose)129     void dumpStates(FILE* out, bool verbose) const {
130         std::lock_guard<std::mutex> lock(mMutex);
131         dumpStatesLocked(out, verbose);
132     }
133 
134     // Returns the memory in bytes currently used to store this metric's data. Does not change
135     // state.
byteSize()136     size_t byteSize() const {
137         std::lock_guard<std::mutex> lock(mMutex);
138         return byteSizeLocked();
139     }
140 
141     /* If alert is valid, adds an AnomalyTracker and returns it. If invalid, returns nullptr. */
addAnomalyTracker(const Alert & alert,const sp<AlarmMonitor> & anomalyAlarmMonitor)142     virtual sp<AnomalyTracker> addAnomalyTracker(const Alert &alert,
143                                                  const sp<AlarmMonitor>& anomalyAlarmMonitor) {
144         std::lock_guard<std::mutex> lock(mMutex);
145         sp<AnomalyTracker> anomalyTracker = new AnomalyTracker(alert, mConfigKey);
146         if (anomalyTracker != nullptr) {
147             mAnomalyTrackers.push_back(anomalyTracker);
148         }
149         return anomalyTracker;
150     }
151 
getBuckeSizeInNs()152     int64_t getBuckeSizeInNs() const {
153         std::lock_guard<std::mutex> lock(mMutex);
154         return mBucketSizeNs;
155     }
156 
157     // Only needed for unit-testing to override guardrail.
setBucketSize(int64_t bucketSize)158     void setBucketSize(int64_t bucketSize) {
159         mBucketSizeNs = bucketSize;
160     }
161 
getMetricId()162     inline const int64_t& getMetricId() {
163         return mMetricId;
164     }
165 
166     // Let MetricProducer drop in-memory data to save memory.
167     // We still need to keep future data valid and anomaly tracking work, which means we will
168     // have to flush old data, informing anomaly trackers then safely drop old data.
169     // We still keep current bucket data for future metrics' validity.
dropData(const int64_t dropTimeNs)170     void dropData(const int64_t dropTimeNs) {
171         std::lock_guard<std::mutex> lock(mMutex);
172         dropDataLocked(dropTimeNs);
173     }
174 
175     // For test only.
getCurrentBucketNum()176     inline int64_t getCurrentBucketNum() const {
177         return mCurrentBucketNum;
178     }
179 
180 protected:
181     virtual void onConditionChangedLocked(const bool condition, const int64_t eventTime) = 0;
182     virtual void onSlicedConditionMayChangeLocked(bool overallCondition,
183                                                   const int64_t eventTime) = 0;
184     virtual void onDumpReportLocked(const int64_t dumpTimeNs,
185                                     const bool include_current_partial_bucket,
186                                     std::set<string> *str_set,
187                                     android::util::ProtoOutputStream* protoOutput) = 0;
188     virtual void clearPastBucketsLocked(const int64_t dumpTimeNs) = 0;
189     virtual size_t byteSizeLocked() const = 0;
190     virtual void dumpStatesLocked(FILE* out, bool verbose) const = 0;
191 
192     /**
193      * Flushes the current bucket if the eventTime is after the current bucket's end time. This will
194        also flush the current partial bucket in memory.
195      */
flushIfNeededLocked(const int64_t & eventTime)196     virtual void flushIfNeededLocked(const int64_t& eventTime){};
197 
198     /**
199      * Flushes all the data including the current partial bucket.
200      */
flushLocked(const int64_t & eventTime)201     virtual void flushLocked(const int64_t& eventTime) {
202         flushIfNeededLocked(eventTime);
203         flushCurrentBucketLocked(eventTime);
204     };
205 
206     /**
207      * For metrics that aggregate (ie, every metric producer except for EventMetricProducer),
208      * we need to be able to flush the current buckets on demand (ie, end the current bucket and
209      * start new bucket). If this function is called when eventTimeNs is greater than the current
210      * bucket's end timestamp, than we flush up to the end of the latest full bucket; otherwise,
211      * we assume that we want to flush a partial bucket. The bucket start timestamp and bucket
212      * number are not changed by this function. This method should only be called by
213      * flushIfNeededLocked or the app upgrade handler; the caller MUST update the bucket timestamp
214      * and bucket number as needed.
215      */
flushCurrentBucketLocked(const int64_t & eventTimeNs)216     virtual void flushCurrentBucketLocked(const int64_t& eventTimeNs){};
217 
218     // Convenience to compute the current bucket's end time, which is always aligned with the
219     // start time of the metric.
getCurrentBucketEndTimeNs()220     int64_t getCurrentBucketEndTimeNs() const {
221         return mTimeBaseNs + (mCurrentBucketNum + 1) * mBucketSizeNs;
222     }
223 
getBucketNumFromEndTimeNs(const int64_t endNs)224     int64_t getBucketNumFromEndTimeNs(const int64_t endNs) {
225         return (endNs - mTimeBaseNs) / mBucketSizeNs - 1;
226     }
227 
228     virtual void dropDataLocked(const int64_t dropTimeNs) = 0;
229 
230     const int64_t mMetricId;
231 
232     const ConfigKey mConfigKey;
233 
234     // The time when this metric producer was first created. The end time for the current bucket
235     // can be computed from this based on mCurrentBucketNum.
236     int64_t mTimeBaseNs;
237 
238     // Start time may not be aligned with the start of statsd if there is an app upgrade in the
239     // middle of a bucket.
240     int64_t mCurrentBucketStartTimeNs;
241 
242     // Used by anomaly detector to track which bucket we are in. This is not sent with the produced
243     // report.
244     int64_t mCurrentBucketNum;
245 
246     int64_t mBucketSizeNs;
247 
248     bool mCondition;
249 
250     bool mConditionSliced;
251 
252     sp<ConditionWizard> mWizard;
253 
254     int mConditionTrackerIndex;
255 
256     vector<Matcher> mDimensionsInWhat;       // The dimensions_in_what defined in statsd_config
257     vector<Matcher> mDimensionsInCondition;  // The dimensions_in_condition defined in statsd_config
258 
259     bool mContainANYPositionInDimensionsInWhat;
260     bool mSliceByPositionALL;
261 
262     // True iff the condition dimensions equal to the sliced dimensions in the simple condition
263     // tracker. This field is always false for combinational condition trackers.
264     bool mSameConditionDimensionsInTracker;
265 
266     // True iff the metric to condition links cover all dimension fields in the condition tracker.
267     // This field is always false for combinational condition trackers.
268     bool mHasLinksToAllConditionDimensionsInTracker;
269 
270     std::vector<Metric2Condition> mMetric2ConditionLinks;
271 
272     std::vector<sp<AnomalyTracker>> mAnomalyTrackers;
273 
274     /*
275      * Individual metrics can implement their own business logic here. All pre-processing is done.
276      *
277      * [matcherIndex]: the index of the matcher which matched this event. This is interesting to
278      *                 DurationMetric, because it has start/stop/stop_all 3 matchers.
279      * [eventKey]: the extracted dimension key for the final output. if the metric doesn't have
280      *             dimensions, it will be DEFAULT_DIMENSION_KEY
281      * [conditionKey]: the keys of conditions which should be used to query the condition for this
282      *                 target event (from MetricConditionLink). This is passed to individual metrics
283      *                 because DurationMetric needs it to be cached.
284      * [condition]: whether condition is met. If condition is sliced, this is the result coming from
285      *              query with ConditionWizard; If condition is not sliced, this is the
286      *              nonSlicedCondition.
287      * [event]: the log event, just in case the metric needs its data, e.g., EventMetric.
288      */
289     virtual void onMatchedLogEventInternalLocked(
290             const size_t matcherIndex, const MetricDimensionKey& eventKey,
291             const ConditionKey& conditionKey, bool condition,
292             const LogEvent& event) = 0;
293 
294     // Consume the parsed stats log entry that already matched the "what" of the metric.
295     virtual void onMatchedLogEventLocked(const size_t matcherIndex, const LogEvent& event);
296 
297     mutable std::mutex mMutex;
298 };
299 
300 }  // namespace statsd
301 }  // namespace os
302 }  // namespace android
303 #endif  // METRIC_PRODUCER_H
304