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 #define DEBUG false  // STOPSHIP if true
18 #include "Log.h"
19 
20 #include "CountMetricProducer.h"
21 
22 #include <inttypes.h>
23 #include <limits.h>
24 #include <stdlib.h>
25 
26 #include "guardrail/StatsdStats.h"
27 #include "stats_log_util.h"
28 #include "stats_util.h"
29 
30 using android::util::FIELD_COUNT_REPEATED;
31 using android::util::FIELD_TYPE_BOOL;
32 using android::util::FIELD_TYPE_FLOAT;
33 using android::util::FIELD_TYPE_INT32;
34 using android::util::FIELD_TYPE_INT64;
35 using android::util::FIELD_TYPE_MESSAGE;
36 using android::util::FIELD_TYPE_STRING;
37 using android::util::ProtoOutputStream;
38 using std::map;
39 using std::string;
40 using std::unordered_map;
41 using std::vector;
42 using std::shared_ptr;
43 
44 namespace android {
45 namespace os {
46 namespace statsd {
47 
48 // for StatsLogReport
49 const int FIELD_ID_ID = 1;
50 const int FIELD_ID_COUNT_METRICS = 5;
51 const int FIELD_ID_TIME_BASE = 9;
52 const int FIELD_ID_BUCKET_SIZE = 10;
53 const int FIELD_ID_DIMENSION_PATH_IN_WHAT = 11;
54 const int FIELD_ID_IS_ACTIVE = 14;
55 
56 // for CountMetricDataWrapper
57 const int FIELD_ID_DATA = 1;
58 // for CountMetricData
59 const int FIELD_ID_DIMENSION_IN_WHAT = 1;
60 const int FIELD_ID_SLICE_BY_STATE = 6;
61 const int FIELD_ID_BUCKET_INFO = 3;
62 const int FIELD_ID_DIMENSION_LEAF_IN_WHAT = 4;
63 // for CountBucketInfo
64 const int FIELD_ID_COUNT = 3;
65 const int FIELD_ID_BUCKET_NUM = 4;
66 const int FIELD_ID_START_BUCKET_ELAPSED_MILLIS = 5;
67 const int FIELD_ID_END_BUCKET_ELAPSED_MILLIS = 6;
68 
CountMetricProducer(const ConfigKey & key,const CountMetric & metric,const int conditionIndex,const vector<ConditionState> & initialConditionCache,const sp<ConditionWizard> & wizard,const int64_t timeBaseNs,const int64_t startTimeNs,const unordered_map<int,shared_ptr<Activation>> & eventActivationMap,const unordered_map<int,vector<shared_ptr<Activation>>> & eventDeactivationMap,const vector<int> & slicedStateAtoms,const unordered_map<int,unordered_map<int,int64_t>> & stateGroupMap)69 CountMetricProducer::CountMetricProducer(
70         const ConfigKey& key, const CountMetric& metric, const int conditionIndex,
71         const vector<ConditionState>& initialConditionCache, const sp<ConditionWizard>& wizard,
72         const int64_t timeBaseNs, const int64_t startTimeNs,
73         const unordered_map<int, shared_ptr<Activation>>& eventActivationMap,
74         const unordered_map<int, vector<shared_ptr<Activation>>>& eventDeactivationMap,
75         const vector<int>& slicedStateAtoms,
76         const unordered_map<int, unordered_map<int, int64_t>>& stateGroupMap)
77     : MetricProducer(metric.id(), key, timeBaseNs, conditionIndex, initialConditionCache, wizard,
78                      eventActivationMap, eventDeactivationMap, slicedStateAtoms, stateGroupMap) {
79     if (metric.has_bucket()) {
80         mBucketSizeNs =
81                 TimeUnitToBucketSizeInMillisGuardrailed(key.GetUid(), metric.bucket()) * 1000000;
82     } else {
83         mBucketSizeNs = LLONG_MAX;
84     }
85 
86     if (metric.has_dimensions_in_what()) {
87         translateFieldMatcher(metric.dimensions_in_what(), &mDimensionsInWhat);
88         mContainANYPositionInDimensionsInWhat = HasPositionANY(metric.dimensions_in_what());
89     }
90 
91     mSliceByPositionALL = HasPositionALL(metric.dimensions_in_what());
92 
93     if (metric.links().size() > 0) {
94         for (const auto& link : metric.links()) {
95             Metric2Condition mc;
96             mc.conditionId = link.condition();
97             translateFieldMatcher(link.fields_in_what(), &mc.metricFields);
98             translateFieldMatcher(link.fields_in_condition(), &mc.conditionFields);
99             mMetric2ConditionLinks.push_back(mc);
100         }
101         mConditionSliced = true;
102     }
103 
104     for (const auto& stateLink : metric.state_link()) {
105         Metric2State ms;
106         ms.stateAtomId = stateLink.state_atom_id();
107         translateFieldMatcher(stateLink.fields_in_what(), &ms.metricFields);
108         translateFieldMatcher(stateLink.fields_in_state(), &ms.stateFields);
109         mMetric2StateLinks.push_back(ms);
110     }
111 
112     flushIfNeededLocked(startTimeNs);
113     // Adjust start for partial bucket
114     mCurrentBucketStartTimeNs = startTimeNs;
115 
116     VLOG("metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(),
117          (long long)mBucketSizeNs, (long long)mTimeBaseNs);
118 }
119 
~CountMetricProducer()120 CountMetricProducer::~CountMetricProducer() {
121     VLOG("~CountMetricProducer() called");
122 }
123 
onStateChanged(const int64_t eventTimeNs,const int32_t atomId,const HashableDimensionKey & primaryKey,const FieldValue & oldState,const FieldValue & newState)124 void CountMetricProducer::onStateChanged(const int64_t eventTimeNs, const int32_t atomId,
125                                          const HashableDimensionKey& primaryKey,
126                                          const FieldValue& oldState, const FieldValue& newState) {
127     VLOG("CountMetric %lld onStateChanged time %lld, State%d, key %s, %d -> %d",
128          (long long)mMetricId, (long long)eventTimeNs, atomId, primaryKey.toString().c_str(),
129          oldState.mValue.int_value, newState.mValue.int_value);
130 }
131 
dumpStatesLocked(FILE * out,bool verbose) const132 void CountMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const {
133     if (mCurrentSlicedCounter == nullptr ||
134         mCurrentSlicedCounter->size() == 0) {
135         return;
136     }
137 
138     fprintf(out, "CountMetric %lld dimension size %lu\n", (long long)mMetricId,
139             (unsigned long)mCurrentSlicedCounter->size());
140     if (verbose) {
141         for (const auto& it : *mCurrentSlicedCounter) {
142             fprintf(out, "\t(what)%s\t(state)%s  %lld\n",
143                     it.first.getDimensionKeyInWhat().toString().c_str(),
144                     it.first.getStateValuesKey().toString().c_str(), (unsigned long long)it.second);
145         }
146     }
147 }
148 
onSlicedConditionMayChangeLocked(bool overallCondition,const int64_t eventTime)149 void CountMetricProducer::onSlicedConditionMayChangeLocked(bool overallCondition,
150                                                            const int64_t eventTime) {
151     VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId);
152 }
153 
154 
clearPastBucketsLocked(const int64_t dumpTimeNs)155 void CountMetricProducer::clearPastBucketsLocked(const int64_t dumpTimeNs) {
156     mPastBuckets.clear();
157 }
158 
onDumpReportLocked(const int64_t dumpTimeNs,const bool include_current_partial_bucket,const bool erase_data,const DumpLatency dumpLatency,std::set<string> * str_set,ProtoOutputStream * protoOutput)159 void CountMetricProducer::onDumpReportLocked(const int64_t dumpTimeNs,
160                                              const bool include_current_partial_bucket,
161                                              const bool erase_data,
162                                              const DumpLatency dumpLatency,
163                                              std::set<string> *str_set,
164                                              ProtoOutputStream* protoOutput) {
165     if (include_current_partial_bucket) {
166         flushLocked(dumpTimeNs);
167     } else {
168         flushIfNeededLocked(dumpTimeNs);
169     }
170     protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
171     protoOutput->write(FIELD_TYPE_BOOL | FIELD_ID_IS_ACTIVE, isActiveLocked());
172 
173 
174     if (mPastBuckets.empty()) {
175         return;
176     }
177     protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_TIME_BASE, (long long)mTimeBaseNs);
178     protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_SIZE, (long long)mBucketSizeNs);
179 
180     // Fills the dimension path if not slicing by ALL.
181     if (!mSliceByPositionALL) {
182         if (!mDimensionsInWhat.empty()) {
183             uint64_t dimenPathToken = protoOutput->start(
184                     FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_PATH_IN_WHAT);
185             writeDimensionPathToProto(mDimensionsInWhat, protoOutput);
186             protoOutput->end(dimenPathToken);
187         }
188     }
189 
190     uint64_t protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_COUNT_METRICS);
191 
192     for (const auto& counter : mPastBuckets) {
193         const MetricDimensionKey& dimensionKey = counter.first;
194         VLOG("  dimension key %s", dimensionKey.toString().c_str());
195 
196         uint64_t wrapperToken =
197                 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
198 
199         // First fill dimension.
200         if (mSliceByPositionALL) {
201             uint64_t dimensionToken = protoOutput->start(
202                     FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT);
203             writeDimensionToProto(dimensionKey.getDimensionKeyInWhat(), str_set, protoOutput);
204             protoOutput->end(dimensionToken);
205         } else {
206             writeDimensionLeafNodesToProto(dimensionKey.getDimensionKeyInWhat(),
207                                            FIELD_ID_DIMENSION_LEAF_IN_WHAT, str_set, protoOutput);
208         }
209         // Then fill slice_by_state.
210         for (auto state : dimensionKey.getStateValuesKey().getValues()) {
211             uint64_t stateToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
212                                                      FIELD_ID_SLICE_BY_STATE);
213             writeStateToProto(state, protoOutput);
214             protoOutput->end(stateToken);
215         }
216         // Then fill bucket_info (CountBucketInfo).
217         for (const auto& bucket : counter.second) {
218             uint64_t bucketInfoToken = protoOutput->start(
219                     FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
220             // Partial bucket.
221             if (bucket.mBucketEndNs - bucket.mBucketStartNs != mBucketSizeNs) {
222                 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_ELAPSED_MILLIS,
223                                    (long long)NanoToMillis(bucket.mBucketStartNs));
224                 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_ELAPSED_MILLIS,
225                                    (long long)NanoToMillis(bucket.mBucketEndNs));
226             } else {
227                 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_NUM,
228                                    (long long)(getBucketNumFromEndTimeNs(bucket.mBucketEndNs)));
229             }
230             protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_COUNT, (long long)bucket.mCount);
231             protoOutput->end(bucketInfoToken);
232             VLOG("\t bucket [%lld - %lld] count: %lld", (long long)bucket.mBucketStartNs,
233                  (long long)bucket.mBucketEndNs, (long long)bucket.mCount);
234         }
235         protoOutput->end(wrapperToken);
236     }
237 
238     protoOutput->end(protoToken);
239 
240     if (erase_data) {
241         mPastBuckets.clear();
242     }
243 }
244 
dropDataLocked(const int64_t dropTimeNs)245 void CountMetricProducer::dropDataLocked(const int64_t dropTimeNs) {
246     flushIfNeededLocked(dropTimeNs);
247     StatsdStats::getInstance().noteBucketDropped(mMetricId);
248     mPastBuckets.clear();
249 }
250 
onConditionChangedLocked(const bool conditionMet,const int64_t eventTime)251 void CountMetricProducer::onConditionChangedLocked(const bool conditionMet,
252                                                    const int64_t eventTime) {
253     VLOG("Metric %lld onConditionChanged", (long long)mMetricId);
254     mCondition = conditionMet ? ConditionState::kTrue : ConditionState::kFalse;
255 }
256 
hitGuardRailLocked(const MetricDimensionKey & newKey)257 bool CountMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) {
258     if (mCurrentSlicedCounter->find(newKey) != mCurrentSlicedCounter->end()) {
259         return false;
260     }
261     // ===========GuardRail==============
262     // 1. Report the tuple count if the tuple count > soft limit
263     if (mCurrentSlicedCounter->size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
264         size_t newTupleCount = mCurrentSlicedCounter->size() + 1;
265         StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount);
266         // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
267         if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
268             ALOGE("CountMetric %lld dropping data for dimension key %s",
269                 (long long)mMetricId, newKey.toString().c_str());
270             StatsdStats::getInstance().noteHardDimensionLimitReached(mMetricId);
271             return true;
272         }
273     }
274 
275     return false;
276 }
277 
onMatchedLogEventInternalLocked(const size_t matcherIndex,const MetricDimensionKey & eventKey,const ConditionKey & conditionKey,bool condition,const LogEvent & event,const map<int,HashableDimensionKey> & statePrimaryKeys)278 void CountMetricProducer::onMatchedLogEventInternalLocked(
279         const size_t matcherIndex, const MetricDimensionKey& eventKey,
280         const ConditionKey& conditionKey, bool condition, const LogEvent& event,
281         const map<int, HashableDimensionKey>& statePrimaryKeys) {
282     int64_t eventTimeNs = event.GetElapsedTimestampNs();
283     flushIfNeededLocked(eventTimeNs);
284 
285     if (!condition) {
286         return;
287     }
288 
289     auto it = mCurrentSlicedCounter->find(eventKey);
290     if (it == mCurrentSlicedCounter->end()) {
291         // ===========GuardRail==============
292         if (hitGuardRailLocked(eventKey)) {
293             return;
294         }
295         // create a counter for the new key
296         (*mCurrentSlicedCounter)[eventKey] = 1;
297     } else {
298         // increment the existing value
299         auto& count = it->second;
300         count++;
301     }
302     for (auto& tracker : mAnomalyTrackers) {
303         int64_t countWholeBucket = mCurrentSlicedCounter->find(eventKey)->second;
304         auto prev = mCurrentFullCounters->find(eventKey);
305         if (prev != mCurrentFullCounters->end()) {
306             countWholeBucket += prev->second;
307         }
308         tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, mMetricId, eventKey,
309                                          countWholeBucket);
310     }
311 
312     VLOG("metric %lld %s->%lld", (long long)mMetricId, eventKey.toString().c_str(),
313          (long long)(*mCurrentSlicedCounter)[eventKey]);
314 }
315 
316 // When a new matched event comes in, we check if event falls into the current
317 // bucket. If not, flush the old counter to past buckets and initialize the new bucket.
flushIfNeededLocked(const int64_t & eventTimeNs)318 void CountMetricProducer::flushIfNeededLocked(const int64_t& eventTimeNs) {
319     int64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs();
320     if (eventTimeNs < currentBucketEndTimeNs) {
321         return;
322     }
323 
324     // Setup the bucket start time and number.
325     int64_t numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs;
326     int64_t nextBucketNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs;
327     flushCurrentBucketLocked(eventTimeNs, nextBucketNs);
328 
329     mCurrentBucketNum += numBucketsForward;
330     VLOG("metric %lld: new bucket start time: %lld", (long long)mMetricId,
331          (long long)mCurrentBucketStartTimeNs);
332 }
333 
flushCurrentBucketLocked(const int64_t & eventTimeNs,const int64_t & nextBucketStartTimeNs)334 void CountMetricProducer::flushCurrentBucketLocked(const int64_t& eventTimeNs,
335                                                    const int64_t& nextBucketStartTimeNs) {
336     int64_t fullBucketEndTimeNs = getCurrentBucketEndTimeNs();
337     CountBucket info;
338     info.mBucketStartNs = mCurrentBucketStartTimeNs;
339     if (eventTimeNs < fullBucketEndTimeNs) {
340         info.mBucketEndNs = eventTimeNs;
341     } else {
342         info.mBucketEndNs = fullBucketEndTimeNs;
343     }
344     for (const auto& counter : *mCurrentSlicedCounter) {
345         info.mCount = counter.second;
346         auto& bucketList = mPastBuckets[counter.first];
347         bucketList.push_back(info);
348         VLOG("metric %lld, dump key value: %s -> %lld", (long long)mMetricId,
349              counter.first.toString().c_str(),
350              (long long)counter.second);
351     }
352 
353     // If we have finished a full bucket, then send this to anomaly tracker.
354     if (eventTimeNs > fullBucketEndTimeNs) {
355         // Accumulate partial buckets with current value and then send to anomaly tracker.
356         if (mCurrentFullCounters->size() > 0) {
357             for (const auto& keyValuePair : *mCurrentSlicedCounter) {
358                 (*mCurrentFullCounters)[keyValuePair.first] += keyValuePair.second;
359             }
360             for (auto& tracker : mAnomalyTrackers) {
361                 tracker->addPastBucket(mCurrentFullCounters, mCurrentBucketNum);
362             }
363             mCurrentFullCounters = std::make_shared<DimToValMap>();
364         } else {
365             // Skip aggregating the partial buckets since there's no previous partial bucket.
366             for (auto& tracker : mAnomalyTrackers) {
367                 tracker->addPastBucket(mCurrentSlicedCounter, mCurrentBucketNum);
368             }
369         }
370     } else {
371         // Accumulate partial bucket.
372         for (const auto& keyValuePair : *mCurrentSlicedCounter) {
373             (*mCurrentFullCounters)[keyValuePair.first] += keyValuePair.second;
374         }
375     }
376 
377     StatsdStats::getInstance().noteBucketCount(mMetricId);
378     // Only resets the counters, but doesn't setup the times nor numbers.
379     // (Do not clear since the old one is still referenced in mAnomalyTrackers).
380     mCurrentSlicedCounter = std::make_shared<DimToValMap>();
381     mCurrentBucketStartTimeNs = nextBucketStartTimeNs;
382 }
383 
384 // Rough estimate of CountMetricProducer buffer stored. This number will be
385 // greater than actual data size as it contains each dimension of
386 // CountMetricData is  duplicated.
byteSizeLocked() const387 size_t CountMetricProducer::byteSizeLocked() const {
388     size_t totalSize = 0;
389     for (const auto& pair : mPastBuckets) {
390         totalSize += pair.second.size() * kBucketSize;
391     }
392     return totalSize;
393 }
394 
395 }  // namespace statsd
396 }  // namespace os
397 }  // namespace android
398