blob: 254d7d5a38f5db38157caba9dc4a8a82ecb6a273 [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;
Howard Ro9440e092018-12-16 19:15:21 -080052const int FIELD_ID_IS_ACTIVE = 14;
Yang Lub4722912018-11-15 11:02:03 -080053
yro24809bd2017-10-31 23:06:53 -070054// for CountMetricDataWrapper
55const int FIELD_ID_DATA = 1;
56// for CountMetricData
Yangster-mac468ff042018-01-17 12:26:34 -080057const int FIELD_ID_DIMENSION_IN_WHAT = 1;
58const int FIELD_ID_DIMENSION_IN_CONDITION = 2;
59const int FIELD_ID_BUCKET_INFO = 3;
Yangster-mac9def8e32018-04-17 13:55:51 -070060const int FIELD_ID_DIMENSION_LEAF_IN_WHAT = 4;
61const int FIELD_ID_DIMENSION_LEAF_IN_CONDITION = 5;
yro24809bd2017-10-31 23:06:53 -070062// for CountBucketInfo
yro24809bd2017-10-31 23:06:53 -070063const int FIELD_ID_COUNT = 3;
Yangster-mac9def8e32018-04-17 13:55:51 -070064const int FIELD_ID_BUCKET_NUM = 4;
65const int FIELD_ID_START_BUCKET_ELAPSED_MILLIS = 5;
66const int FIELD_ID_END_BUCKET_ELAPSED_MILLIS = 6;
yro24809bd2017-10-31 23:06:53 -070067
Yao Chenb3561512017-11-21 18:07:17 -080068CountMetricProducer::CountMetricProducer(const ConfigKey& key, const CountMetric& metric,
69 const int conditionIndex,
Yao Chen93fe3a32017-11-02 13:52:59 -070070 const sp<ConditionWizard>& wizard,
Chenjie Yue1361ed2018-07-23 17:33:09 -070071 const int64_t timeBaseNs, const int64_t startTimeNs)
72 : MetricProducer(metric.id(), key, timeBaseNs, conditionIndex, wizard) {
Yangster-macb8144812018-01-04 10:56:23 -080073 if (metric.has_bucket()) {
yro59cc24d2018-02-13 20:17:32 -080074 mBucketSizeNs =
75 TimeUnitToBucketSizeInMillisGuardrailed(key.GetUid(), metric.bucket()) * 1000000;
Yao Chen44cf27c2017-09-14 22:32:50 -070076 } else {
Yao Chen729093d2017-10-16 10:33:26 -070077 mBucketSizeNs = LLONG_MAX;
Yao Chen44cf27c2017-09-14 22:32:50 -070078 }
79
Yao Chen8a8d16c2018-02-08 14:50:40 -080080 if (metric.has_dimensions_in_what()) {
81 translateFieldMatcher(metric.dimensions_in_what(), &mDimensionsInWhat);
Yangster13fb7e42018-03-07 17:30:49 -080082 mContainANYPositionInDimensionsInWhat = HasPositionANY(metric.dimensions_in_what());
Yao Chen8a8d16c2018-02-08 14:50:40 -080083 }
84
Yangster-mac9def8e32018-04-17 13:55:51 -070085 mSliceByPositionALL = HasPositionALL(metric.dimensions_in_what()) ||
86 HasPositionALL(metric.dimensions_in_condition());
87
Yao Chen8a8d16c2018-02-08 14:50:40 -080088 if (metric.has_dimensions_in_condition()) {
89 translateFieldMatcher(metric.dimensions_in_condition(), &mDimensionsInCondition);
90 }
Yao Chen729093d2017-10-16 10:33:26 -070091
92 if (metric.links().size() > 0) {
Yao Chen8a8d16c2018-02-08 14:50:40 -080093 for (const auto& link : metric.links()) {
94 Metric2Condition mc;
95 mc.conditionId = link.condition();
96 translateFieldMatcher(link.fields_in_what(), &mc.metricFields);
97 translateFieldMatcher(link.fields_in_condition(), &mc.conditionFields);
98 mMetric2ConditionLinks.push_back(mc);
99 }
100 mConditionSliced = true;
Yao Chen729093d2017-10-16 10:33:26 -0700101 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800102
103 mConditionSliced = (metric.links().size() > 0) || (mDimensionsInCondition.size() > 0);
Yao Chen729093d2017-10-16 10:33:26 -0700104
Chenjie Yue1361ed2018-07-23 17:33:09 -0700105 flushIfNeededLocked(startTimeNs);
106 // Adjust start for partial bucket
107 mCurrentBucketStartTimeNs = startTimeNs;
108
Yangster-mac94e197c2018-01-02 16:03:03 -0800109 VLOG("metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(),
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700110 (long long)mBucketSizeNs, (long long)mTimeBaseNs);
Yao Chen44cf27c2017-09-14 22:32:50 -0700111}
112
Yao Chen44cf27c2017-09-14 22:32:50 -0700113CountMetricProducer::~CountMetricProducer() {
114 VLOG("~CountMetricProducer() called");
115}
116
Yangster-maca78d0082018-03-12 12:02:56 -0700117void CountMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const {
118 if (mCurrentSlicedCounter == nullptr ||
119 mCurrentSlicedCounter->size() == 0) {
120 return;
121 }
122
123 fprintf(out, "CountMetric %lld dimension size %lu\n", (long long)mMetricId,
124 (unsigned long)mCurrentSlicedCounter->size());
125 if (verbose) {
126 for (const auto& it : *mCurrentSlicedCounter) {
127 fprintf(out, "\t(what)%s\t(condition)%s %lld\n",
128 it.first.getDimensionKeyInWhat().toString().c_str(),
129 it.first.getDimensionKeyInCondition().toString().c_str(),
130 (unsigned long long)it.second);
131 }
132 }
133}
134
Yao Chen427d3722018-03-22 15:21:52 -0700135void CountMetricProducer::onSlicedConditionMayChangeLocked(bool overallCondition,
Yangster-macb142cc82018-03-30 15:22:08 -0700136 const int64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800137 VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId);
Yao Chen729093d2017-10-16 10:33:26 -0700138}
139
Yangster-maca802d732018-04-24 07:50:38 -0700140
141void CountMetricProducer::clearPastBucketsLocked(const int64_t dumpTimeNs) {
Yangster-maca802d732018-04-24 07:50:38 -0700142 mPastBuckets.clear();
143}
144
Yangster-macb142cc82018-03-30 15:22:08 -0700145void CountMetricProducer::onDumpReportLocked(const int64_t dumpTimeNs,
Yangster-mace68f3a52018-04-04 00:01:43 -0700146 const bool include_current_partial_bucket,
Bookatzff71cad2018-09-20 17:17:49 -0700147 const bool erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000148 const DumpLatency dumpLatency,
Yangster-mac9def8e32018-04-17 13:55:51 -0700149 std::set<string> *str_set,
Yao Chen288c6002017-12-12 13:43:18 -0800150 ProtoOutputStream* protoOutput) {
Yangster-mace68f3a52018-04-04 00:01:43 -0700151 if (include_current_partial_bucket) {
152 flushLocked(dumpTimeNs);
153 } else {
154 flushIfNeededLocked(dumpTimeNs);
155 }
Yang Lub4722912018-11-15 11:02:03 -0800156 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
Howard Ro07e23ff2018-12-17 17:28:07 -0800157 protoOutput->write(FIELD_TYPE_BOOL | FIELD_ID_IS_ACTIVE, isActiveLocked());
Yang Lub4722912018-11-15 11:02:03 -0800158
159
Yangster-mac635b4b32018-01-23 20:17:35 -0800160 if (mPastBuckets.empty()) {
161 return;
162 }
Yangster-mac9def8e32018-04-17 13:55:51 -0700163 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_TIME_BASE, (long long)mTimeBaseNs);
164 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_SIZE, (long long)mBucketSizeNs);
165
166 // Fills the dimension path if not slicing by ALL.
167 if (!mSliceByPositionALL) {
168 if (!mDimensionsInWhat.empty()) {
169 uint64_t dimenPathToken = protoOutput->start(
170 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_PATH_IN_WHAT);
171 writeDimensionPathToProto(mDimensionsInWhat, protoOutput);
172 protoOutput->end(dimenPathToken);
173 }
174 if (!mDimensionsInCondition.empty()) {
175 uint64_t dimenPathToken = protoOutput->start(
176 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_PATH_IN_CONDITION);
177 writeDimensionPathToProto(mDimensionsInCondition, protoOutput);
178 protoOutput->end(dimenPathToken);
179 }
180
181 }
182
Yi Jin5ee07872018-03-05 18:18:27 -0800183 uint64_t protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_COUNT_METRICS);
Yao Chen288c6002017-12-12 13:43:18 -0800184
Yao Chen93fe3a32017-11-02 13:52:59 -0700185 for (const auto& counter : mPastBuckets) {
Yangster-mac93694462018-01-22 20:49:31 -0800186 const MetricDimensionKey& dimensionKey = counter.first;
Yangster13fb7e42018-03-07 17:30:49 -0800187 VLOG(" dimension key %s", dimensionKey.toString().c_str());
Yao Chend5aa01b32017-12-19 16:46:36 -0800188
Yi Jin5ee07872018-03-05 18:18:27 -0800189 uint64_t wrapperToken =
Yao Chen288c6002017-12-12 13:43:18 -0800190 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
Yao Chen729093d2017-10-16 10:33:26 -0700191
Yangster-mac20877162017-12-22 17:19:39 -0800192 // First fill dimension.
Yangster-mac9def8e32018-04-17 13:55:51 -0700193 if (mSliceByPositionALL) {
194 uint64_t dimensionToken = protoOutput->start(
195 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT);
196 writeDimensionToProto(dimensionKey.getDimensionKeyInWhat(), str_set, protoOutput);
197 protoOutput->end(dimensionToken);
Yangster-mac93694462018-01-22 20:49:31 -0800198
Yangster-mac9def8e32018-04-17 13:55:51 -0700199 if (dimensionKey.hasDimensionKeyInCondition()) {
200 uint64_t dimensionInConditionToken = protoOutput->start(
201 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION);
202 writeDimensionToProto(dimensionKey.getDimensionKeyInCondition(),
203 str_set, protoOutput);
204 protoOutput->end(dimensionInConditionToken);
205 }
206 } else {
207 writeDimensionLeafNodesToProto(dimensionKey.getDimensionKeyInWhat(),
208 FIELD_ID_DIMENSION_LEAF_IN_WHAT, str_set, protoOutput);
209 if (dimensionKey.hasDimensionKeyInCondition()) {
210 writeDimensionLeafNodesToProto(dimensionKey.getDimensionKeyInCondition(),
211 FIELD_ID_DIMENSION_LEAF_IN_CONDITION,
212 str_set, protoOutput);
213 }
Yangster-mac93694462018-01-22 20:49:31 -0800214 }
yro24809bd2017-10-31 23:06:53 -0700215 // Then fill bucket_info (CountBucketInfo).
Yao Chen93fe3a32017-11-02 13:52:59 -0700216 for (const auto& bucket : counter.second) {
Yi Jin5ee07872018-03-05 18:18:27 -0800217 uint64_t bucketInfoToken = protoOutput->start(
Yao Chen288c6002017-12-12 13:43:18 -0800218 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
Yangster-mac9def8e32018-04-17 13:55:51 -0700219 // Partial bucket.
220 if (bucket.mBucketEndNs - bucket.mBucketStartNs != mBucketSizeNs) {
221 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_ELAPSED_MILLIS,
222 (long long)NanoToMillis(bucket.mBucketStartNs));
223 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_ELAPSED_MILLIS,
224 (long long)NanoToMillis(bucket.mBucketEndNs));
225 } else {
226 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_NUM,
227 (long long)(getBucketNumFromEndTimeNs(bucket.mBucketEndNs)));
228 }
Yao Chen288c6002017-12-12 13:43:18 -0800229 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_COUNT, (long long)bucket.mCount);
230 protoOutput->end(bucketInfoToken);
Yao Chen93fe3a32017-11-02 13:52:59 -0700231 VLOG("\t bucket [%lld - %lld] count: %lld", (long long)bucket.mBucketStartNs,
232 (long long)bucket.mBucketEndNs, (long long)bucket.mCount);
yro24809bd2017-10-31 23:06:53 -0700233 }
Yao Chen288c6002017-12-12 13:43:18 -0800234 protoOutput->end(wrapperToken);
Yao Chen729093d2017-10-16 10:33:26 -0700235 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000236
Yao Chen288c6002017-12-12 13:43:18 -0800237 protoOutput->end(protoToken);
yro24809bd2017-10-31 23:06:53 -0700238
Bookatzff71cad2018-09-20 17:17:49 -0700239 if (erase_data) {
240 mPastBuckets.clear();
241 }
Yao Chen06dba5d2018-01-26 13:38:16 -0800242}
Yao Chen6a8c7992017-11-29 20:02:07 +0000243
Yangster-macb142cc82018-03-30 15:22:08 -0700244void CountMetricProducer::dropDataLocked(const int64_t dropTimeNs) {
Yao Chen06dba5d2018-01-26 13:38:16 -0800245 flushIfNeededLocked(dropTimeNs);
Olivier Gaillard320952b2019-02-06 13:57:24 +0000246 StatsdStats::getInstance().noteBucketDropped(mMetricId);
Yao Chen06dba5d2018-01-26 13:38:16 -0800247 mPastBuckets.clear();
Yao Chen44cf27c2017-09-14 22:32:50 -0700248}
249
Yangsterf2bee6f2017-11-29 12:01:05 -0800250void CountMetricProducer::onConditionChangedLocked(const bool conditionMet,
Yangster-macb142cc82018-03-30 15:22:08 -0700251 const int64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800252 VLOG("Metric %lld onConditionChanged", (long long)mMetricId);
Olivier Gaillarde63d9e02019-02-12 14:43:59 +0000253 mCondition = conditionMet ? ConditionState::kTrue : ConditionState::kFalse;
Yao Chencaf339d2017-10-06 16:01:10 -0700254}
255
Yangster-mac93694462018-01-22 20:49:31 -0800256bool CountMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) {
Yao Chenb3561512017-11-21 18:07:17 -0800257 if (mCurrentSlicedCounter->find(newKey) != mCurrentSlicedCounter->end()) {
258 return false;
259 }
260 // ===========GuardRail==============
261 // 1. Report the tuple count if the tuple count > soft limit
262 if (mCurrentSlicedCounter->size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
263 size_t newTupleCount = mCurrentSlicedCounter->size() + 1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800264 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount);
Yao Chenb3561512017-11-21 18:07:17 -0800265 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
266 if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800267 ALOGE("CountMetric %lld dropping data for dimension key %s",
Yangster13fb7e42018-03-07 17:30:49 -0800268 (long long)mMetricId, newKey.toString().c_str());
Yao Chenb3561512017-11-21 18:07:17 -0800269 return true;
270 }
271 }
272
273 return false;
274}
Yangsterf2bee6f2017-11-29 12:01:05 -0800275
276void CountMetricProducer::onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -0800277 const size_t matcherIndex, const MetricDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -0800278 const ConditionKey& conditionKey, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800279 const LogEvent& event) {
Yangster-macb142cc82018-03-30 15:22:08 -0700280 int64_t eventTimeNs = event.GetElapsedTimestampNs();
Yangsterf2bee6f2017-11-29 12:01:05 -0800281 flushIfNeededLocked(eventTimeNs);
Yao Chen729093d2017-10-16 10:33:26 -0700282
Yao Chen6a8c7992017-11-29 20:02:07 +0000283 if (condition == false) {
Yao Chenb7041772017-10-20 16:59:25 -0700284 return;
Yao Chen44cf27c2017-09-14 22:32:50 -0700285 }
Yao Chen729093d2017-10-16 10:33:26 -0700286
Yao Chen6a8c7992017-11-29 20:02:07 +0000287 auto it = mCurrentSlicedCounter->find(eventKey);
Yao Chen6a8c7992017-11-29 20:02:07 +0000288 if (it == mCurrentSlicedCounter->end()) {
289 // ===========GuardRail==============
Yangsterf2bee6f2017-11-29 12:01:05 -0800290 if (hitGuardRailLocked(eventKey)) {
Yao Chenb3561512017-11-21 18:07:17 -0800291 return;
292 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000293 // create a counter for the new key
294 (*mCurrentSlicedCounter)[eventKey] = 1;
295 } else {
296 // increment the existing value
297 auto& count = it->second;
298 count++;
Yao Chen729093d2017-10-16 10:33:26 -0700299 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000300 for (auto& tracker : mAnomalyTrackers) {
David Chen27785a82018-01-19 17:06:45 -0800301 int64_t countWholeBucket = mCurrentSlicedCounter->find(eventKey)->second;
302 auto prev = mCurrentFullCounters->find(eventKey);
303 if (prev != mCurrentFullCounters->end()) {
304 countWholeBucket += prev->second;
305 }
Yao Chen4ce07292019-02-13 13:06:36 -0800306 tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, mMetricId, eventKey,
David Chen27785a82018-01-19 17:06:45 -0800307 countWholeBucket);
Yao Chen6a8c7992017-11-29 20:02:07 +0000308 }
309
Yangster13fb7e42018-03-07 17:30:49 -0800310 VLOG("metric %lld %s->%lld", (long long)mMetricId, eventKey.toString().c_str(),
Yao Chen6a8c7992017-11-29 20:02:07 +0000311 (long long)(*mCurrentSlicedCounter)[eventKey]);
Yao Chen44cf27c2017-09-14 22:32:50 -0700312}
313
Yao Chen729093d2017-10-16 10:33:26 -0700314// When a new matched event comes in, we check if event falls into the current
315// bucket. If not, flush the old counter to past buckets and initialize the new bucket.
Yangster-macb142cc82018-03-30 15:22:08 -0700316void CountMetricProducer::flushIfNeededLocked(const int64_t& eventTimeNs) {
317 int64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs();
David Chen27785a82018-01-19 17:06:45 -0800318 if (eventTimeNs < currentBucketEndTimeNs) {
Yao Chen44cf27c2017-09-14 22:32:50 -0700319 return;
320 }
321
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000322 flushCurrentBucketLocked(eventTimeNs, eventTimeNs);
David Chen27785a82018-01-19 17:06:45 -0800323 // Setup the bucket start time and number.
Yangster-macb142cc82018-03-30 15:22:08 -0700324 int64_t numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs;
David Chen27785a82018-01-19 17:06:45 -0800325 mCurrentBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs;
326 mCurrentBucketNum += numBucketsForward;
327 VLOG("metric %lld: new bucket start time: %lld", (long long)mMetricId,
328 (long long)mCurrentBucketStartTimeNs);
329}
330
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000331void CountMetricProducer::flushCurrentBucketLocked(const int64_t& eventTimeNs,
332 const int64_t& nextBucketStartTimeNs) {
Yangster-macb142cc82018-03-30 15:22:08 -0700333 int64_t fullBucketEndTimeNs = getCurrentBucketEndTimeNs();
Yao Chen93fe3a32017-11-02 13:52:59 -0700334 CountBucket info;
335 info.mBucketStartNs = mCurrentBucketStartTimeNs;
David Chen27785a82018-01-19 17:06:45 -0800336 if (eventTimeNs < fullBucketEndTimeNs) {
337 info.mBucketEndNs = eventTimeNs;
338 } else {
339 info.mBucketEndNs = fullBucketEndTimeNs;
340 }
Yang Lu3eba6212017-10-25 19:54:45 -0700341 for (const auto& counter : *mCurrentSlicedCounter) {
Yao Chen93fe3a32017-11-02 13:52:59 -0700342 info.mCount = counter.second;
343 auto& bucketList = mPastBuckets[counter.first];
344 bucketList.push_back(info);
Yangster13fb7e42018-03-07 17:30:49 -0800345 VLOG("metric %lld, dump key value: %s -> %lld", (long long)mMetricId,
346 counter.first.toString().c_str(),
David Chen27785a82018-01-19 17:06:45 -0800347 (long long)counter.second);
Yao Chen729093d2017-10-16 10:33:26 -0700348 }
349
David Chen27785a82018-01-19 17:06:45 -0800350 // If we have finished a full bucket, then send this to anomaly tracker.
351 if (eventTimeNs > fullBucketEndTimeNs) {
352 // Accumulate partial buckets with current value and then send to anomaly tracker.
353 if (mCurrentFullCounters->size() > 0) {
354 for (const auto& keyValuePair : *mCurrentSlicedCounter) {
355 (*mCurrentFullCounters)[keyValuePair.first] += keyValuePair.second;
356 }
357 for (auto& tracker : mAnomalyTrackers) {
358 tracker->addPastBucket(mCurrentFullCounters, mCurrentBucketNum);
359 }
360 mCurrentFullCounters = std::make_shared<DimToValMap>();
361 } else {
362 // Skip aggregating the partial buckets since there's no previous partial bucket.
363 for (auto& tracker : mAnomalyTrackers) {
364 tracker->addPastBucket(mCurrentSlicedCounter, mCurrentBucketNum);
365 }
366 }
367 } else {
368 // Accumulate partial bucket.
369 for (const auto& keyValuePair : *mCurrentSlicedCounter) {
370 (*mCurrentFullCounters)[keyValuePair.first] += keyValuePair.second;
371 }
Yang Lu3eba6212017-10-25 19:54:45 -0700372 }
Bookatzd3606c72017-10-19 10:13:49 -0700373
Olivier Gaillardf248c0d2019-02-21 15:56:58 +0000374 StatsdStats::getInstance().noteBucketCount(mMetricId);
David Chen27785a82018-01-19 17:06:45 -0800375 // Only resets the counters, but doesn't setup the times nor numbers.
376 // (Do not clear since the old one is still referenced in mAnomalyTrackers).
Yang Lu3eba6212017-10-25 19:54:45 -0700377 mCurrentSlicedCounter = std::make_shared<DimToValMap>();
Yao Chen44cf27c2017-09-14 22:32:50 -0700378}
379
yro24809bd2017-10-31 23:06:53 -0700380// Rough estimate of CountMetricProducer buffer stored. This number will be
381// greater than actual data size as it contains each dimension of
382// CountMetricData is duplicated.
Yangsterf2bee6f2017-11-29 12:01:05 -0800383size_t CountMetricProducer::byteSizeLocked() const {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800384 size_t totalSize = 0;
385 for (const auto& pair : mPastBuckets) {
386 totalSize += pair.second.size() * kBucketSize;
387 }
388 return totalSize;
yro69007c82017-10-26 20:42:57 -0700389}
390
Yao Chen44cf27c2017-09-14 22:32:50 -0700391} // namespace statsd
392} // namespace os
yro69007c82017-10-26 20:42:57 -0700393} // namespace android