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 <frameworks/base/cmds/statsd/src/active_config_list.pb.h>
21 #include <utils/RefBase.h>
22 
23 #include <unordered_map>
24 
25 #include "HashableDimensionKey.h"
26 #include "anomaly/AnomalyTracker.h"
27 #include "condition/ConditionWizard.h"
28 #include "config/ConfigKey.h"
29 #include "matchers/matcher_util.h"
30 #include "packages/PackageInfoListener.h"
31 
32 namespace android {
33 namespace os {
34 namespace statsd {
35 
36 // Keep this in sync with DumpReportReason enum in stats_log.proto
37 enum DumpReportReason {
38     DEVICE_SHUTDOWN = 1,
39     CONFIG_UPDATED = 2,
40     CONFIG_REMOVED = 3,
41     GET_DATA_CALLED = 4,
42     ADB_DUMP = 5,
43     CONFIG_RESET = 6,
44     STATSCOMPANION_DIED = 7,
45     TERMINATION_SIGNAL_RECEIVED = 8
46 };
47 
48 // If the metric has no activation requirement, it will be active once the metric producer is
49 // created.
50 // If the metric needs to be activated by atoms, the metric producer will start
51 // with kNotActive state, turn to kActive or kActiveOnBoot when the activation event arrives, become
52 // kNotActive when it reaches the duration limit (timebomb). If the activation event arrives again
53 // before or after it expires, the event producer will be re-activated and ttl will be reset.
54 enum ActivationState {
55     kNotActive = 0,
56     kActive = 1,
57     kActiveOnBoot = 2,
58 };
59 
60 enum DumpLatency {
61     // In some cases, we only have a short time range to do the dump, e.g. statsd is being killed.
62     // We might be able to return all the data in this mode. For instance, pull metrics might need
63     // to be pulled when the current bucket is requested.
64     FAST = 1,
65     // In other cases, it is fine for a dump to take more than a few milliseconds, e.g. config
66     // updates.
67     NO_TIME_CONSTRAINTS = 2
68 };
69 
70 // A MetricProducer is responsible for compute one single metrics, creating stats log report, and
71 // writing the report to dropbox. MetricProducers should respond to package changes as required in
72 // PackageInfoListener, but if none of the metrics are slicing by package name, then the update can
73 // be a no-op.
74 class MetricProducer : public virtual android::RefBase {
75 public:
MetricProducer(const int64_t & metricId,const ConfigKey & key,const int64_t timeBaseNs,const int conditionIndex,const sp<ConditionWizard> & wizard)76     MetricProducer(const int64_t& metricId, const ConfigKey& key, const int64_t timeBaseNs,
77                    const int conditionIndex, const sp<ConditionWizard>& wizard)
78         : mMetricId(metricId),
79           mConfigKey(key),
80           mTimeBaseNs(timeBaseNs),
81           mCurrentBucketStartTimeNs(timeBaseNs),
82           mCurrentBucketNum(0),
83           mCondition(initialCondition(conditionIndex)),
84           mConditionSliced(false),
85           mWizard(wizard),
86           mConditionTrackerIndex(conditionIndex),
87           mContainANYPositionInDimensionsInWhat(false),
88           mSliceByPositionALL(false),
89           mSameConditionDimensionsInTracker(false),
90           mHasLinksToAllConditionDimensionsInTracker(false),
91           mIsActive(true) {
92     }
93 
~MetricProducer()94     virtual ~MetricProducer(){};
95 
initialCondition(const int conditionIndex)96     ConditionState initialCondition(const int conditionIndex) const {
97         return conditionIndex >= 0 ? ConditionState::kUnknown : ConditionState::kTrue;
98     }
99 
100     /**
101      * Forces this metric to split into a partial bucket right now. If we're past a full bucket, we
102      * first call the standard flushing code to flush up to the latest full bucket. Then we call
103      * the flush again when the end timestamp is forced to be now, and then after flushing, update
104      * the start timestamp to be now.
105      */
notifyAppUpgrade(const int64_t & eventTimeNs,const string & apk,const int uid,const int64_t version)106     virtual void notifyAppUpgrade(const int64_t& eventTimeNs, const string& apk, const int uid,
107                           const int64_t version) {
108         std::lock_guard<std::mutex> lock(mMutex);
109 
110         if (eventTimeNs > getCurrentBucketEndTimeNs()) {
111             // Flush full buckets on the normal path up to the latest bucket boundary.
112             flushIfNeededLocked(eventTimeNs);
113         }
114         // Now flush a partial bucket.
115         flushCurrentBucketLocked(eventTimeNs, eventTimeNs);
116         // Don't update the current bucket number so that the anomaly tracker knows this bucket
117         // is a partial bucket and can merge it with the previous bucket.
118     };
119 
notifyAppRemoved(const int64_t & eventTimeNs,const string & apk,const int uid)120     void notifyAppRemoved(const int64_t& eventTimeNs, const string& apk, const int uid) {
121         // Force buckets to split on removal also.
122         notifyAppUpgrade(eventTimeNs, apk, uid, 0);
123     };
124 
125     // Consume the parsed stats log entry that already matched the "what" of the metric.
onMatchedLogEvent(const size_t matcherIndex,const LogEvent & event)126     void onMatchedLogEvent(const size_t matcherIndex, const LogEvent& event) {
127         std::lock_guard<std::mutex> lock(mMutex);
128         onMatchedLogEventLocked(matcherIndex, event);
129     }
130 
onConditionChanged(const bool condition,const int64_t eventTime)131     void onConditionChanged(const bool condition, const int64_t eventTime) {
132         std::lock_guard<std::mutex> lock(mMutex);
133         onConditionChangedLocked(condition, eventTime);
134     }
135 
onSlicedConditionMayChange(bool overallCondition,const int64_t eventTime)136     void onSlicedConditionMayChange(bool overallCondition, const int64_t eventTime) {
137         std::lock_guard<std::mutex> lock(mMutex);
138         onSlicedConditionMayChangeLocked(overallCondition, eventTime);
139     }
140 
isConditionSliced()141     bool isConditionSliced() const {
142         std::lock_guard<std::mutex> lock(mMutex);
143         return mConditionSliced;
144     };
145 
146     // Output the metrics data to [protoOutput]. All metrics reports end with the same timestamp.
147     // This method clears all the past buckets.
onDumpReport(const int64_t dumpTimeNs,const bool include_current_partial_bucket,const bool erase_data,const DumpLatency dumpLatency,std::set<string> * str_set,android::util::ProtoOutputStream * protoOutput)148     void onDumpReport(const int64_t dumpTimeNs,
149                       const bool include_current_partial_bucket,
150                       const bool erase_data,
151                       const DumpLatency dumpLatency,
152                       std::set<string> *str_set,
153                       android::util::ProtoOutputStream* protoOutput) {
154         std::lock_guard<std::mutex> lock(mMutex);
155         return onDumpReportLocked(dumpTimeNs, include_current_partial_bucket, erase_data,
156                 dumpLatency, str_set, protoOutput);
157     }
158 
clearPastBuckets(const int64_t dumpTimeNs)159     void clearPastBuckets(const int64_t dumpTimeNs) {
160         std::lock_guard<std::mutex> lock(mMutex);
161         return clearPastBucketsLocked(dumpTimeNs);
162     }
163 
dumpStates(FILE * out,bool verbose)164     void dumpStates(FILE* out, bool verbose) const {
165         std::lock_guard<std::mutex> lock(mMutex);
166         dumpStatesLocked(out, verbose);
167     }
168 
169     // Returns the memory in bytes currently used to store this metric's data. Does not change
170     // state.
byteSize()171     size_t byteSize() const {
172         std::lock_guard<std::mutex> lock(mMutex);
173         return byteSizeLocked();
174     }
175 
176     /* If alert is valid, adds an AnomalyTracker and returns it. If invalid, returns nullptr. */
addAnomalyTracker(const Alert & alert,const sp<AlarmMonitor> & anomalyAlarmMonitor)177     virtual sp<AnomalyTracker> addAnomalyTracker(const Alert &alert,
178                                                  const sp<AlarmMonitor>& anomalyAlarmMonitor) {
179         std::lock_guard<std::mutex> lock(mMutex);
180         sp<AnomalyTracker> anomalyTracker = new AnomalyTracker(alert, mConfigKey);
181         if (anomalyTracker != nullptr) {
182             mAnomalyTrackers.push_back(anomalyTracker);
183         }
184         return anomalyTracker;
185     }
186 
getBuckeSizeInNs()187     int64_t getBuckeSizeInNs() const {
188         std::lock_guard<std::mutex> lock(mMutex);
189         return mBucketSizeNs;
190     }
191 
192     // Only needed for unit-testing to override guardrail.
setBucketSize(int64_t bucketSize)193     void setBucketSize(int64_t bucketSize) {
194         mBucketSizeNs = bucketSize;
195     }
196 
getMetricId()197     inline const int64_t& getMetricId() const {
198         return mMetricId;
199     }
200 
loadActiveMetric(const ActiveMetric & activeMetric,int64_t currentTimeNs)201     void loadActiveMetric(const ActiveMetric& activeMetric, int64_t currentTimeNs) {
202         std::lock_guard<std::mutex> lock(mMutex);
203         loadActiveMetricLocked(activeMetric, currentTimeNs);
204     }
205 
206     // Let MetricProducer drop in-memory data to save memory.
207     // We still need to keep future data valid and anomaly tracking work, which means we will
208     // have to flush old data, informing anomaly trackers then safely drop old data.
209     // We still keep current bucket data for future metrics' validity.
dropData(const int64_t dropTimeNs)210     void dropData(const int64_t dropTimeNs) {
211         std::lock_guard<std::mutex> lock(mMutex);
212         dropDataLocked(dropTimeNs);
213     }
214 
215     // For test only.
getCurrentBucketNum()216     inline int64_t getCurrentBucketNum() const {
217         return mCurrentBucketNum;
218     }
219 
activate(int activationTrackerIndex,int64_t elapsedTimestampNs)220     void activate(int activationTrackerIndex, int64_t elapsedTimestampNs) {
221         std::lock_guard<std::mutex> lock(mMutex);
222         activateLocked(activationTrackerIndex, elapsedTimestampNs);
223     }
224 
cancelEventActivation(int deactivationTrackerIndex)225     void cancelEventActivation(int deactivationTrackerIndex) {
226         std::lock_guard<std::mutex> lock(mMutex);
227         cancelEventActivationLocked(deactivationTrackerIndex);
228     }
229 
isActive()230     bool isActive() const {
231         std::lock_guard<std::mutex> lock(mMutex);
232         return isActiveLocked();
233     }
234 
235     void addActivation(int activationTrackerIndex, const ActivationType& activationType,
236             int64_t ttl_seconds, int deactivationTrackerIndex = -1);
237 
prepareFirstBucket()238     void prepareFirstBucket() {
239         std::lock_guard<std::mutex> lock(mMutex);
240         prepareFirstBucketLocked();
241     }
242 
243     void flushIfExpire(int64_t elapsedTimestampNs);
244 
245     void writeActiveMetricToProtoOutputStream(
246             int64_t currentTimeNs, const DumpReportReason reason, ProtoOutputStream* proto);
247 protected:
248     virtual void onConditionChangedLocked(const bool condition, const int64_t eventTime) = 0;
249     virtual void onSlicedConditionMayChangeLocked(bool overallCondition,
250                                                   const int64_t eventTime) = 0;
251     virtual void onDumpReportLocked(const int64_t dumpTimeNs,
252                                     const bool include_current_partial_bucket,
253                                     const bool erase_data,
254                                     const DumpLatency dumpLatency,
255                                     std::set<string> *str_set,
256                                     android::util::ProtoOutputStream* protoOutput) = 0;
257     virtual void clearPastBucketsLocked(const int64_t dumpTimeNs) = 0;
258     virtual size_t byteSizeLocked() const = 0;
259     virtual void dumpStatesLocked(FILE* out, bool verbose) const = 0;
260 
261     bool evaluateActiveStateLocked(int64_t elapsedTimestampNs);
262 
263     void activateLocked(int activationTrackerIndex, int64_t elapsedTimestampNs);
264     void cancelEventActivationLocked(int deactivationTrackerIndex);
265 
isActiveLocked()266     inline bool isActiveLocked() const {
267         return mIsActive;
268     }
269 
270     void loadActiveMetricLocked(const ActiveMetric& activeMetric, int64_t currentTimeNs);
271 
prepareFirstBucketLocked()272     virtual void prepareFirstBucketLocked() {};
273     /**
274      * Flushes the current bucket if the eventTime is after the current bucket's end time. This will
275        also flush the current partial bucket in memory.
276      */
flushIfNeededLocked(const int64_t & eventTime)277     virtual void flushIfNeededLocked(const int64_t& eventTime){};
278 
279     /**
280      * Flushes all the data including the current partial bucket.
281      */
flushLocked(const int64_t & eventTimeNs)282     virtual void flushLocked(const int64_t& eventTimeNs) {
283         flushIfNeededLocked(eventTimeNs);
284         flushCurrentBucketLocked(eventTimeNs, eventTimeNs);
285     };
286 
287     /**
288      * For metrics that aggregate (ie, every metric producer except for EventMetricProducer),
289      * we need to be able to flush the current buckets on demand (ie, end the current bucket and
290      * start new bucket). If this function is called when eventTimeNs is greater than the current
291      * bucket's end timestamp, than we flush up to the end of the latest full bucket; otherwise,
292      * we assume that we want to flush a partial bucket. The bucket start timestamp and bucket
293      * number are not changed by this function. This method should only be called by
294      * flushIfNeededLocked or flushLocked or the app upgrade handler; the caller MUST update the
295      * bucket timestamp and bucket number as needed.
296      */
flushCurrentBucketLocked(const int64_t & eventTimeNs,const int64_t & nextBucketStartTimeNs)297     virtual void flushCurrentBucketLocked(const int64_t& eventTimeNs,
298                                           const int64_t& nextBucketStartTimeNs) {};
299 
onActiveStateChangedLocked(const int64_t & eventTimeNs)300     virtual void onActiveStateChangedLocked(const int64_t& eventTimeNs) {
301         if (!mIsActive) {
302             flushLocked(eventTimeNs);
303         }
304     }
305 
306     // Convenience to compute the current bucket's end time, which is always aligned with the
307     // start time of the metric.
getCurrentBucketEndTimeNs()308     int64_t getCurrentBucketEndTimeNs() const {
309         return mTimeBaseNs + (mCurrentBucketNum + 1) * mBucketSizeNs;
310     }
311 
getBucketNumFromEndTimeNs(const int64_t endNs)312     int64_t getBucketNumFromEndTimeNs(const int64_t endNs) {
313         return (endNs - mTimeBaseNs) / mBucketSizeNs - 1;
314     }
315 
316     virtual void dropDataLocked(const int64_t dropTimeNs) = 0;
317 
318     const int64_t mMetricId;
319 
320     const ConfigKey mConfigKey;
321 
322     // The time when this metric producer was first created. The end time for the current bucket
323     // can be computed from this based on mCurrentBucketNum.
324     int64_t mTimeBaseNs;
325 
326     // Start time may not be aligned with the start of statsd if there is an app upgrade in the
327     // middle of a bucket.
328     int64_t mCurrentBucketStartTimeNs;
329 
330     // Used by anomaly detector to track which bucket we are in. This is not sent with the produced
331     // report.
332     int64_t mCurrentBucketNum;
333 
334     int64_t mBucketSizeNs;
335 
336     ConditionState mCondition;
337 
338     bool mConditionSliced;
339 
340     sp<ConditionWizard> mWizard;
341 
342     int mConditionTrackerIndex;
343 
344     vector<Matcher> mDimensionsInWhat;       // The dimensions_in_what defined in statsd_config
345     vector<Matcher> mDimensionsInCondition;  // The dimensions_in_condition defined in statsd_config
346 
347     bool mContainANYPositionInDimensionsInWhat;
348     bool mSliceByPositionALL;
349 
350     // True iff the condition dimensions equal to the sliced dimensions in the simple condition
351     // tracker. This field is always false for combinational condition trackers.
352     bool mSameConditionDimensionsInTracker;
353 
354     // True iff the metric to condition links cover all dimension fields in the condition tracker.
355     // This field is always false for combinational condition trackers.
356     bool mHasLinksToAllConditionDimensionsInTracker;
357 
358     std::vector<Metric2Condition> mMetric2ConditionLinks;
359 
360     std::vector<sp<AnomalyTracker>> mAnomalyTrackers;
361 
362     /*
363      * Individual metrics can implement their own business logic here. All pre-processing is done.
364      *
365      * [matcherIndex]: the index of the matcher which matched this event. This is interesting to
366      *                 DurationMetric, because it has start/stop/stop_all 3 matchers.
367      * [eventKey]: the extracted dimension key for the final output. if the metric doesn't have
368      *             dimensions, it will be DEFAULT_DIMENSION_KEY
369      * [conditionKey]: the keys of conditions which should be used to query the condition for this
370      *                 target event (from MetricConditionLink). This is passed to individual metrics
371      *                 because DurationMetric needs it to be cached.
372      * [condition]: whether condition is met. If condition is sliced, this is the result coming from
373      *              query with ConditionWizard; If condition is not sliced, this is the
374      *              nonSlicedCondition.
375      * [event]: the log event, just in case the metric needs its data, e.g., EventMetric.
376      */
377     virtual void onMatchedLogEventInternalLocked(
378             const size_t matcherIndex, const MetricDimensionKey& eventKey,
379             const ConditionKey& conditionKey, bool condition,
380             const LogEvent& event) = 0;
381 
382     // Consume the parsed stats log entry that already matched the "what" of the metric.
383     virtual void onMatchedLogEventLocked(const size_t matcherIndex, const LogEvent& event);
384 
385     mutable std::mutex mMutex;
386 
387     struct Activation {
ActivationActivation388         Activation(const ActivationType& activationType, const int64_t ttlNs)
389             : ttl_ns(ttlNs),
390               start_ns(0),
391               state(ActivationState::kNotActive),
392               activationType(activationType) {}
393 
394         const int64_t ttl_ns;
395         int64_t start_ns;
396         ActivationState state;
397         const ActivationType activationType;
398     };
399     // When the metric producer has multiple activations, these activations are ORed to determine
400     // whether the metric producer is ready to generate metrics.
401     std::unordered_map<int, std::shared_ptr<Activation>> mEventActivationMap;
402 
403     // Maps index of atom matcher for deactivation to a list of Activation structs.
404     std::unordered_map<int, std::vector<std::shared_ptr<Activation>>> mEventDeactivationMap;
405 
406     bool mIsActive;
407 
408     FRIEND_TEST(DurationMetricE2eTest, TestOneBucket);
409     FRIEND_TEST(DurationMetricE2eTest, TestTwoBuckets);
410     FRIEND_TEST(DurationMetricE2eTest, TestWithActivation);
411     FRIEND_TEST(DurationMetricE2eTest, TestWithCondition);
412     FRIEND_TEST(DurationMetricE2eTest, TestWithSlicedCondition);
413     FRIEND_TEST(DurationMetricE2eTest, TestWithActivationAndSlicedCondition);
414 
415     FRIEND_TEST(MetricActivationE2eTest, TestCountMetric);
416     FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithOneDeactivation);
417     FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithTwoDeactivations);
418     FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithSameDeactivation);
419     FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithTwoMetricsTwoDeactivations);
420 
421     FRIEND_TEST(StatsLogProcessorTest, TestActiveConfigMetricDiskWriteRead);
422     FRIEND_TEST(StatsLogProcessorTest, TestActivationOnBoot);
423     FRIEND_TEST(StatsLogProcessorTest, TestActivationOnBootMultipleActivations);
424     FRIEND_TEST(StatsLogProcessorTest,
425             TestActivationOnBootMultipleActivationsDifferentActivationTypes);
426     FRIEND_TEST(StatsLogProcessorTest, TestActivationsPersistAcrossSystemServerRestart);
427 };
428 
429 }  // namespace statsd
430 }  // namespace os
431 }  // namespace android
432 #endif  // METRIC_PRODUCER_H
433