blob: a894782db9f73b9cbb72a58a4553f6dc013e405a [file] [log] [blame]
Yao Chen44cf27c2017-09-14 22:32:50 -07001/*
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
Yao Chen3c0b95c2017-12-16 14:34:20 -080017#define DEBUG false // STOPSHIP if true
Joe Onorato9fc9edf2017-10-15 20:08:52 -070018#include "Log.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070019
Yao Chen729093d2017-10-16 10:33:26 -070020#include "CountMetricProducer.h"
Yao Chenb3561512017-11-21 18:07:17 -080021#include "guardrail/StatsdStats.h"
Yao Chen729093d2017-10-16 10:33:26 -070022#include "stats_util.h"
Yangster-mac20877162017-12-22 17:19:39 -080023#include "stats_log_util.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070024
Yao Chen44cf27c2017-09-14 22:32:50 -070025#include <limits.h>
26#include <stdlib.h>
27
yrob0378b02017-11-09 20:36:25 -080028using android::util::FIELD_COUNT_REPEATED;
yro2b0f8862017-11-06 14:27:31 -080029using android::util::FIELD_TYPE_BOOL;
30using android::util::FIELD_TYPE_FLOAT;
31using android::util::FIELD_TYPE_INT32;
32using android::util::FIELD_TYPE_INT64;
33using android::util::FIELD_TYPE_MESSAGE;
Yangster-macd1815dc2017-11-13 21:43:15 -080034using android::util::FIELD_TYPE_STRING;
yro24809bd2017-10-31 23:06:53 -070035using android::util::ProtoOutputStream;
Yao Chen729093d2017-10-16 10:33:26 -070036using std::map;
37using std::string;
Yao Chen44cf27c2017-09-14 22:32:50 -070038using std::unordered_map;
Yao Chen729093d2017-10-16 10:33:26 -070039using std::vector;
Yao Chen44cf27c2017-09-14 22:32:50 -070040
41namespace android {
42namespace os {
43namespace statsd {
44
yro24809bd2017-10-31 23:06:53 -070045// for StatsLogReport
Yangster-mac94e197c2018-01-02 16:03:03 -080046const int FIELD_ID_ID = 1;
yro24809bd2017-10-31 23:06:53 -070047const int FIELD_ID_COUNT_METRICS = 5;
Yangster-mac9def8e32018-04-17 13:55:51 -070048const int FIELD_ID_TIME_BASE = 9;
49const int FIELD_ID_BUCKET_SIZE = 10;
50const int FIELD_ID_DIMENSION_PATH_IN_WHAT = 11;
51const int FIELD_ID_DIMENSION_PATH_IN_CONDITION = 12;
yro24809bd2017-10-31 23:06:53 -070052// for CountMetricDataWrapper
53const int FIELD_ID_DATA = 1;
54// for CountMetricData
Yangster-mac468ff042018-01-17 12:26:34 -080055const int FIELD_ID_DIMENSION_IN_WHAT = 1;
56const int FIELD_ID_DIMENSION_IN_CONDITION = 2;
57const int FIELD_ID_BUCKET_INFO = 3;
Yangster-mac9def8e32018-04-17 13:55:51 -070058const int FIELD_ID_DIMENSION_LEAF_IN_WHAT = 4;
59const int FIELD_ID_DIMENSION_LEAF_IN_CONDITION = 5;
yro24809bd2017-10-31 23:06:53 -070060// for CountBucketInfo
yro24809bd2017-10-31 23:06:53 -070061const int FIELD_ID_COUNT = 3;
Yangster-mac9def8e32018-04-17 13:55:51 -070062const int FIELD_ID_BUCKET_NUM = 4;
63const int FIELD_ID_START_BUCKET_ELAPSED_MILLIS = 5;
64const int FIELD_ID_END_BUCKET_ELAPSED_MILLIS = 6;
yro24809bd2017-10-31 23:06:53 -070065
Yao Chenb3561512017-11-21 18:07:17 -080066CountMetricProducer::CountMetricProducer(const ConfigKey& key, const CountMetric& metric,
67 const int conditionIndex,
Yao Chen93fe3a32017-11-02 13:52:59 -070068 const sp<ConditionWizard>& wizard,
Yangster-macb142cc82018-03-30 15:22:08 -070069 const int64_t startTimeNs)
Yangster-mac94e197c2018-01-02 16:03:03 -080070 : MetricProducer(metric.id(), key, startTimeNs, conditionIndex, wizard) {
Yangster-macb8144812018-01-04 10:56:23 -080071 if (metric.has_bucket()) {
yro59cc24d2018-02-13 20:17:32 -080072 mBucketSizeNs =
73 TimeUnitToBucketSizeInMillisGuardrailed(key.GetUid(), metric.bucket()) * 1000000;
Yao Chen44cf27c2017-09-14 22:32:50 -070074 } else {
Yao Chen729093d2017-10-16 10:33:26 -070075 mBucketSizeNs = LLONG_MAX;
Yao Chen44cf27c2017-09-14 22:32:50 -070076 }
77
Yao Chen8a8d16c2018-02-08 14:50:40 -080078 if (metric.has_dimensions_in_what()) {
79 translateFieldMatcher(metric.dimensions_in_what(), &mDimensionsInWhat);
Yangster13fb7e42018-03-07 17:30:49 -080080 mContainANYPositionInDimensionsInWhat = HasPositionANY(metric.dimensions_in_what());
Yao Chen8a8d16c2018-02-08 14:50:40 -080081 }
82
Yangster-mac9def8e32018-04-17 13:55:51 -070083 mSliceByPositionALL = HasPositionALL(metric.dimensions_in_what()) ||
84 HasPositionALL(metric.dimensions_in_condition());
85
Yao Chen8a8d16c2018-02-08 14:50:40 -080086 if (metric.has_dimensions_in_condition()) {
87 translateFieldMatcher(metric.dimensions_in_condition(), &mDimensionsInCondition);
88 }
Yao Chen729093d2017-10-16 10:33:26 -070089
90 if (metric.links().size() > 0) {
Yao Chen8a8d16c2018-02-08 14:50:40 -080091 for (const auto& link : metric.links()) {
92 Metric2Condition mc;
93 mc.conditionId = link.condition();
94 translateFieldMatcher(link.fields_in_what(), &mc.metricFields);
95 translateFieldMatcher(link.fields_in_condition(), &mc.conditionFields);
96 mMetric2ConditionLinks.push_back(mc);
97 }
98 mConditionSliced = true;
Yao Chen729093d2017-10-16 10:33:26 -070099 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800100
101 mConditionSliced = (metric.links().size() > 0) || (mDimensionsInCondition.size() > 0);
Yao Chen729093d2017-10-16 10:33:26 -0700102
Yangster-mac94e197c2018-01-02 16:03:03 -0800103 VLOG("metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(),
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700104 (long long)mBucketSizeNs, (long long)mTimeBaseNs);
Yao Chen44cf27c2017-09-14 22:32:50 -0700105}
106
Yao Chen44cf27c2017-09-14 22:32:50 -0700107CountMetricProducer::~CountMetricProducer() {
108 VLOG("~CountMetricProducer() called");
109}
110
Yangster-maca78d0082018-03-12 12:02:56 -0700111void CountMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const {
112 if (mCurrentSlicedCounter == nullptr ||
113 mCurrentSlicedCounter->size() == 0) {
114 return;
115 }
116
117 fprintf(out, "CountMetric %lld dimension size %lu\n", (long long)mMetricId,
118 (unsigned long)mCurrentSlicedCounter->size());
119 if (verbose) {
120 for (const auto& it : *mCurrentSlicedCounter) {
121 fprintf(out, "\t(what)%s\t(condition)%s %lld\n",
122 it.first.getDimensionKeyInWhat().toString().c_str(),
123 it.first.getDimensionKeyInCondition().toString().c_str(),
124 (unsigned long long)it.second);
125 }
126 }
127}
128
Yao Chen427d3722018-03-22 15:21:52 -0700129void CountMetricProducer::onSlicedConditionMayChangeLocked(bool overallCondition,
Yangster-macb142cc82018-03-30 15:22:08 -0700130 const int64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800131 VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId);
Yao Chen729093d2017-10-16 10:33:26 -0700132}
133
Yangster-maca802d732018-04-24 07:50:38 -0700134
135void CountMetricProducer::clearPastBucketsLocked(const int64_t dumpTimeNs) {
136 flushIfNeededLocked(dumpTimeNs);
137 mPastBuckets.clear();
138}
139
Yangster-macb142cc82018-03-30 15:22:08 -0700140void CountMetricProducer::onDumpReportLocked(const int64_t dumpTimeNs,
Yangster-mace68f3a52018-04-04 00:01:43 -0700141 const bool include_current_partial_bucket,
Yangster-mac9def8e32018-04-17 13:55:51 -0700142 std::set<string> *str_set,
Yao Chen288c6002017-12-12 13:43:18 -0800143 ProtoOutputStream* protoOutput) {
Yangster-mace68f3a52018-04-04 00:01:43 -0700144 if (include_current_partial_bucket) {
145 flushLocked(dumpTimeNs);
146 } else {
147 flushIfNeededLocked(dumpTimeNs);
148 }
Yangster-mac635b4b32018-01-23 20:17:35 -0800149 if (mPastBuckets.empty()) {
150 return;
151 }
Yangster-mac94e197c2018-01-02 16:03:03 -0800152 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
Yangster-mac9def8e32018-04-17 13:55:51 -0700153 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_TIME_BASE, (long long)mTimeBaseNs);
154 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_SIZE, (long long)mBucketSizeNs);
155
156 // Fills the dimension path if not slicing by ALL.
157 if (!mSliceByPositionALL) {
158 if (!mDimensionsInWhat.empty()) {
159 uint64_t dimenPathToken = protoOutput->start(
160 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_PATH_IN_WHAT);
161 writeDimensionPathToProto(mDimensionsInWhat, protoOutput);
162 protoOutput->end(dimenPathToken);
163 }
164 if (!mDimensionsInCondition.empty()) {
165 uint64_t dimenPathToken = protoOutput->start(
166 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_PATH_IN_CONDITION);
167 writeDimensionPathToProto(mDimensionsInCondition, protoOutput);
168 protoOutput->end(dimenPathToken);
169 }
170
171 }
172
Yi Jin5ee07872018-03-05 18:18:27 -0800173 uint64_t protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_COUNT_METRICS);
Yao Chen288c6002017-12-12 13:43:18 -0800174
Yao Chen93fe3a32017-11-02 13:52:59 -0700175 for (const auto& counter : mPastBuckets) {
Yangster-mac93694462018-01-22 20:49:31 -0800176 const MetricDimensionKey& dimensionKey = counter.first;
Yangster13fb7e42018-03-07 17:30:49 -0800177 VLOG(" dimension key %s", dimensionKey.toString().c_str());
Yao Chend5aa01b32017-12-19 16:46:36 -0800178
Yi Jin5ee07872018-03-05 18:18:27 -0800179 uint64_t wrapperToken =
Yao Chen288c6002017-12-12 13:43:18 -0800180 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
Yao Chen729093d2017-10-16 10:33:26 -0700181
Yangster-mac20877162017-12-22 17:19:39 -0800182 // First fill dimension.
Yangster-mac9def8e32018-04-17 13:55:51 -0700183 if (mSliceByPositionALL) {
184 uint64_t dimensionToken = protoOutput->start(
185 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT);
186 writeDimensionToProto(dimensionKey.getDimensionKeyInWhat(), str_set, protoOutput);
187 protoOutput->end(dimensionToken);
Yangster-mac93694462018-01-22 20:49:31 -0800188
Yangster-mac9def8e32018-04-17 13:55:51 -0700189 if (dimensionKey.hasDimensionKeyInCondition()) {
190 uint64_t dimensionInConditionToken = protoOutput->start(
191 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION);
192 writeDimensionToProto(dimensionKey.getDimensionKeyInCondition(),
193 str_set, protoOutput);
194 protoOutput->end(dimensionInConditionToken);
195 }
196 } else {
197 writeDimensionLeafNodesToProto(dimensionKey.getDimensionKeyInWhat(),
198 FIELD_ID_DIMENSION_LEAF_IN_WHAT, str_set, protoOutput);
199 if (dimensionKey.hasDimensionKeyInCondition()) {
200 writeDimensionLeafNodesToProto(dimensionKey.getDimensionKeyInCondition(),
201 FIELD_ID_DIMENSION_LEAF_IN_CONDITION,
202 str_set, protoOutput);
203 }
Yangster-mac93694462018-01-22 20:49:31 -0800204 }
yro24809bd2017-10-31 23:06:53 -0700205 // Then fill bucket_info (CountBucketInfo).
Yao Chen93fe3a32017-11-02 13:52:59 -0700206 for (const auto& bucket : counter.second) {
Yi Jin5ee07872018-03-05 18:18:27 -0800207 uint64_t bucketInfoToken = protoOutput->start(
Yao Chen288c6002017-12-12 13:43:18 -0800208 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
Yangster-mac9def8e32018-04-17 13:55:51 -0700209 // Partial bucket.
210 if (bucket.mBucketEndNs - bucket.mBucketStartNs != mBucketSizeNs) {
211 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_ELAPSED_MILLIS,
212 (long long)NanoToMillis(bucket.mBucketStartNs));
213 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_ELAPSED_MILLIS,
214 (long long)NanoToMillis(bucket.mBucketEndNs));
215 } else {
216 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_NUM,
217 (long long)(getBucketNumFromEndTimeNs(bucket.mBucketEndNs)));
218 }
Yao Chen288c6002017-12-12 13:43:18 -0800219 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_COUNT, (long long)bucket.mCount);
220 protoOutput->end(bucketInfoToken);
Yao Chen93fe3a32017-11-02 13:52:59 -0700221 VLOG("\t bucket [%lld - %lld] count: %lld", (long long)bucket.mBucketStartNs,
222 (long long)bucket.mBucketEndNs, (long long)bucket.mCount);
yro24809bd2017-10-31 23:06:53 -0700223 }
Yao Chen288c6002017-12-12 13:43:18 -0800224 protoOutput->end(wrapperToken);
Yao Chen729093d2017-10-16 10:33:26 -0700225 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000226
Yao Chen288c6002017-12-12 13:43:18 -0800227 protoOutput->end(protoToken);
yro24809bd2017-10-31 23:06:53 -0700228
Yao Chen6a8c7992017-11-29 20:02:07 +0000229 mPastBuckets.clear();
Yao Chen06dba5d2018-01-26 13:38:16 -0800230}
Yao Chen6a8c7992017-11-29 20:02:07 +0000231
Yangster-macb142cc82018-03-30 15:22:08 -0700232void CountMetricProducer::dropDataLocked(const int64_t dropTimeNs) {
Yao Chen06dba5d2018-01-26 13:38:16 -0800233 flushIfNeededLocked(dropTimeNs);
234 mPastBuckets.clear();
Yao Chen44cf27c2017-09-14 22:32:50 -0700235}
236
Yangsterf2bee6f2017-11-29 12:01:05 -0800237void CountMetricProducer::onConditionChangedLocked(const bool conditionMet,
Yangster-macb142cc82018-03-30 15:22:08 -0700238 const int64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800239 VLOG("Metric %lld onConditionChanged", (long long)mMetricId);
Yao Chencaf339d2017-10-06 16:01:10 -0700240 mCondition = conditionMet;
241}
242
Yangster-mac93694462018-01-22 20:49:31 -0800243bool CountMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) {
Yao Chenb3561512017-11-21 18:07:17 -0800244 if (mCurrentSlicedCounter->find(newKey) != mCurrentSlicedCounter->end()) {
245 return false;
246 }
247 // ===========GuardRail==============
248 // 1. Report the tuple count if the tuple count > soft limit
249 if (mCurrentSlicedCounter->size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
250 size_t newTupleCount = mCurrentSlicedCounter->size() + 1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800251 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount);
Yao Chenb3561512017-11-21 18:07:17 -0800252 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
253 if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800254 ALOGE("CountMetric %lld dropping data for dimension key %s",
Yangster13fb7e42018-03-07 17:30:49 -0800255 (long long)mMetricId, newKey.toString().c_str());
Yao Chenb3561512017-11-21 18:07:17 -0800256 return true;
257 }
258 }
259
260 return false;
261}
Yangsterf2bee6f2017-11-29 12:01:05 -0800262
263void CountMetricProducer::onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -0800264 const size_t matcherIndex, const MetricDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -0800265 const ConditionKey& conditionKey, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800266 const LogEvent& event) {
Yangster-macb142cc82018-03-30 15:22:08 -0700267 int64_t eventTimeNs = event.GetElapsedTimestampNs();
Yangsterf2bee6f2017-11-29 12:01:05 -0800268 flushIfNeededLocked(eventTimeNs);
Yao Chen729093d2017-10-16 10:33:26 -0700269
Yao Chen6a8c7992017-11-29 20:02:07 +0000270 if (condition == false) {
Yao Chenb7041772017-10-20 16:59:25 -0700271 return;
Yao Chen44cf27c2017-09-14 22:32:50 -0700272 }
Yao Chen729093d2017-10-16 10:33:26 -0700273
Yao Chen6a8c7992017-11-29 20:02:07 +0000274 auto it = mCurrentSlicedCounter->find(eventKey);
Yao Chen6a8c7992017-11-29 20:02:07 +0000275 if (it == mCurrentSlicedCounter->end()) {
276 // ===========GuardRail==============
Yangsterf2bee6f2017-11-29 12:01:05 -0800277 if (hitGuardRailLocked(eventKey)) {
Yao Chenb3561512017-11-21 18:07:17 -0800278 return;
279 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000280 // create a counter for the new key
281 (*mCurrentSlicedCounter)[eventKey] = 1;
282 } else {
283 // increment the existing value
284 auto& count = it->second;
285 count++;
Yao Chen729093d2017-10-16 10:33:26 -0700286 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000287 for (auto& tracker : mAnomalyTrackers) {
David Chen27785a82018-01-19 17:06:45 -0800288 int64_t countWholeBucket = mCurrentSlicedCounter->find(eventKey)->second;
289 auto prev = mCurrentFullCounters->find(eventKey);
290 if (prev != mCurrentFullCounters->end()) {
291 countWholeBucket += prev->second;
292 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000293 tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, eventKey,
David Chen27785a82018-01-19 17:06:45 -0800294 countWholeBucket);
Yao Chen6a8c7992017-11-29 20:02:07 +0000295 }
296
Yangster13fb7e42018-03-07 17:30:49 -0800297 VLOG("metric %lld %s->%lld", (long long)mMetricId, eventKey.toString().c_str(),
Yao Chen6a8c7992017-11-29 20:02:07 +0000298 (long long)(*mCurrentSlicedCounter)[eventKey]);
Yao Chen44cf27c2017-09-14 22:32:50 -0700299}
300
Yao Chen729093d2017-10-16 10:33:26 -0700301// When a new matched event comes in, we check if event falls into the current
302// bucket. If not, flush the old counter to past buckets and initialize the new bucket.
Yangster-macb142cc82018-03-30 15:22:08 -0700303void CountMetricProducer::flushIfNeededLocked(const int64_t& eventTimeNs) {
304 int64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs();
David Chen27785a82018-01-19 17:06:45 -0800305 if (eventTimeNs < currentBucketEndTimeNs) {
Yao Chen44cf27c2017-09-14 22:32:50 -0700306 return;
307 }
308
David Chen27785a82018-01-19 17:06:45 -0800309 flushCurrentBucketLocked(eventTimeNs);
310 // Setup the bucket start time and number.
Yangster-macb142cc82018-03-30 15:22:08 -0700311 int64_t numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs;
David Chen27785a82018-01-19 17:06:45 -0800312 mCurrentBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs;
313 mCurrentBucketNum += numBucketsForward;
314 VLOG("metric %lld: new bucket start time: %lld", (long long)mMetricId,
315 (long long)mCurrentBucketStartTimeNs);
316}
317
Yangster-macb142cc82018-03-30 15:22:08 -0700318void CountMetricProducer::flushCurrentBucketLocked(const int64_t& eventTimeNs) {
319 int64_t fullBucketEndTimeNs = getCurrentBucketEndTimeNs();
Yao Chen93fe3a32017-11-02 13:52:59 -0700320 CountBucket info;
321 info.mBucketStartNs = mCurrentBucketStartTimeNs;
David Chen27785a82018-01-19 17:06:45 -0800322 if (eventTimeNs < fullBucketEndTimeNs) {
323 info.mBucketEndNs = eventTimeNs;
324 } else {
325 info.mBucketEndNs = fullBucketEndTimeNs;
326 }
Yang Lu3eba6212017-10-25 19:54:45 -0700327 for (const auto& counter : *mCurrentSlicedCounter) {
Yao Chen93fe3a32017-11-02 13:52:59 -0700328 info.mCount = counter.second;
329 auto& bucketList = mPastBuckets[counter.first];
330 bucketList.push_back(info);
Yangster13fb7e42018-03-07 17:30:49 -0800331 VLOG("metric %lld, dump key value: %s -> %lld", (long long)mMetricId,
332 counter.first.toString().c_str(),
David Chen27785a82018-01-19 17:06:45 -0800333 (long long)counter.second);
Yao Chen729093d2017-10-16 10:33:26 -0700334 }
335
David Chen27785a82018-01-19 17:06:45 -0800336 // If we have finished a full bucket, then send this to anomaly tracker.
337 if (eventTimeNs > fullBucketEndTimeNs) {
338 // Accumulate partial buckets with current value and then send to anomaly tracker.
339 if (mCurrentFullCounters->size() > 0) {
340 for (const auto& keyValuePair : *mCurrentSlicedCounter) {
341 (*mCurrentFullCounters)[keyValuePair.first] += keyValuePair.second;
342 }
343 for (auto& tracker : mAnomalyTrackers) {
344 tracker->addPastBucket(mCurrentFullCounters, mCurrentBucketNum);
345 }
346 mCurrentFullCounters = std::make_shared<DimToValMap>();
347 } else {
348 // Skip aggregating the partial buckets since there's no previous partial bucket.
349 for (auto& tracker : mAnomalyTrackers) {
350 tracker->addPastBucket(mCurrentSlicedCounter, mCurrentBucketNum);
351 }
352 }
353 } else {
354 // Accumulate partial bucket.
355 for (const auto& keyValuePair : *mCurrentSlicedCounter) {
356 (*mCurrentFullCounters)[keyValuePair.first] += keyValuePair.second;
357 }
Yang Lu3eba6212017-10-25 19:54:45 -0700358 }
Bookatzd3606c72017-10-19 10:13:49 -0700359
David Chen27785a82018-01-19 17:06:45 -0800360 // Only resets the counters, but doesn't setup the times nor numbers.
361 // (Do not clear since the old one is still referenced in mAnomalyTrackers).
Yang Lu3eba6212017-10-25 19:54:45 -0700362 mCurrentSlicedCounter = std::make_shared<DimToValMap>();
Yao Chen44cf27c2017-09-14 22:32:50 -0700363}
364
yro24809bd2017-10-31 23:06:53 -0700365// Rough estimate of CountMetricProducer buffer stored. This number will be
366// greater than actual data size as it contains each dimension of
367// CountMetricData is duplicated.
Yangsterf2bee6f2017-11-29 12:01:05 -0800368size_t CountMetricProducer::byteSizeLocked() const {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800369 size_t totalSize = 0;
370 for (const auto& pair : mPastBuckets) {
371 totalSize += pair.second.size() * kBucketSize;
372 }
373 return totalSize;
yro69007c82017-10-26 20:42:57 -0700374}
375
Yao Chen44cf27c2017-09-14 22:32:50 -0700376} // namespace statsd
377} // namespace os
yro69007c82017-10-26 20:42:57 -0700378} // namespace android