blob: e88daf793dc313d11290c8186634c3ccfe4041f0 [file] [log] [blame]
Chenjie Yub3dda412017-10-24 13:41:59 -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
Chenjie Yub3dda412017-10-24 13:41:59 -070018#include "Log.h"
19
20#include "ValueMetricProducer.h"
Yao Chenb3561512017-11-21 18:07:17 -080021#include "guardrail/StatsdStats.h"
Yangster-mac20877162017-12-22 17:19:39 -080022#include "stats_log_util.h"
Chenjie Yub3dda412017-10-24 13:41:59 -070023
24#include <cutils/log.h>
25#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;
yro2b0f8862017-11-06 14:27:31 -080035using android::util::ProtoOutputStream;
Chenjie Yub3dda412017-10-24 13:41:59 -070036using std::list;
Chenjie Yu6736c892017-11-09 10:50:09 -080037using std::make_pair;
Chenjie Yub3dda412017-10-24 13:41:59 -070038using std::make_shared;
Yao Chen93fe3a32017-11-02 13:52:59 -070039using std::map;
Chenjie Yub3dda412017-10-24 13:41:59 -070040using std::shared_ptr;
41using std::unique_ptr;
Yao Chen93fe3a32017-11-02 13:52:59 -070042using std::unordered_map;
Chenjie Yub3dda412017-10-24 13:41:59 -070043
44namespace android {
45namespace os {
46namespace statsd {
47
yro2b0f8862017-11-06 14:27:31 -080048// for StatsLogReport
Yangster-mac94e197c2018-01-02 16:03:03 -080049const int FIELD_ID_ID = 1;
yro2b0f8862017-11-06 14:27:31 -080050const int FIELD_ID_VALUE_METRICS = 7;
51// for ValueMetricDataWrapper
52const int FIELD_ID_DATA = 1;
53// for ValueMetricData
Yangster-mac468ff042018-01-17 12:26:34 -080054const int FIELD_ID_DIMENSION_IN_WHAT = 1;
55const int FIELD_ID_DIMENSION_IN_CONDITION = 2;
56const int FIELD_ID_BUCKET_INFO = 3;
yro2b0f8862017-11-06 14:27:31 -080057// for ValueBucketInfo
58const int FIELD_ID_START_BUCKET_NANOS = 1;
59const int FIELD_ID_END_BUCKET_NANOS = 2;
60const int FIELD_ID_VALUE = 3;
61
Chenjie Yub3dda412017-10-24 13:41:59 -070062// ValueMetric has a minimum bucket size of 10min so that we don't pull too frequently
Yao Chenb3561512017-11-21 18:07:17 -080063ValueMetricProducer::ValueMetricProducer(const ConfigKey& key, const ValueMetric& metric,
64 const int conditionIndex,
Yao Chen93fe3a32017-11-02 13:52:59 -070065 const sp<ConditionWizard>& wizard, const int pullTagId,
Chenjie Yu6736c892017-11-09 10:50:09 -080066 const uint64_t startTimeNs,
67 shared_ptr<StatsPullerManager> statsPullerManager)
Yangster-mac94e197c2018-01-02 16:03:03 -080068 : MetricProducer(metric.id(), key, startTimeNs, conditionIndex, wizard),
Yao Chenf09569f2017-12-13 17:00:51 -080069 mValueField(metric.value_field()),
Chenjie Yu6736c892017-11-09 10:50:09 -080070 mStatsPullerManager(statsPullerManager),
71 mPullTagId(pullTagId) {
Yao Chen93fe3a32017-11-02 13:52:59 -070072 // TODO: valuemetric for pushed events may need unlimited bucket length
Yangster-macb8144812018-01-04 10:56:23 -080073 int64_t bucketSizeMills = 0;
74 if (metric.has_bucket()) {
yro59cc24d2018-02-13 20:17:32 -080075 bucketSizeMills = TimeUnitToBucketSizeInMillisGuardrailed(key.GetUid(), metric.bucket());
Chenjie Yu6736c892017-11-09 10:50:09 -080076 } else {
Yangster-macb8144812018-01-04 10:56:23 -080077 bucketSizeMills = TimeUnitToBucketSizeInMillis(ONE_HOUR);
Chenjie Yu6736c892017-11-09 10:50:09 -080078 }
Chenjie Yub3dda412017-10-24 13:41:59 -070079
Yangster-macb8144812018-01-04 10:56:23 -080080 mBucketSizeNs = bucketSizeMills * 1000000;
Yao Chen8a8d16c2018-02-08 14:50:40 -080081 if (metric.has_dimensions_in_what()) {
82 translateFieldMatcher(metric.dimensions_in_what(), &mDimensionsInWhat);
83 }
84
85 if (metric.has_dimensions_in_condition()) {
86 translateFieldMatcher(metric.dimensions_in_condition(), &mDimensionsInCondition);
87 }
Chenjie Yub3dda412017-10-24 13:41:59 -070088
Yao Chen93fe3a32017-11-02 13:52:59 -070089 if (metric.links().size() > 0) {
Yao Chen8a8d16c2018-02-08 14:50:40 -080090 for (const auto& link : metric.links()) {
91 Metric2Condition mc;
92 mc.conditionId = link.condition();
93 translateFieldMatcher(link.fields_in_what(), &mc.metricFields);
94 translateFieldMatcher(link.fields_in_condition(), &mc.conditionFields);
95 mMetric2ConditionLinks.push_back(mc);
96 }
Yao Chen93fe3a32017-11-02 13:52:59 -070097 }
Yao Chen8a8d16c2018-02-08 14:50:40 -080098
99 if (mValueField.child_size()) {
100 mField = mValueField.child(0).field();
101 }
102 mConditionSliced = (metric.links().size() > 0) || (mDimensionsInCondition.size() > 0);
Chenjie Yub3dda412017-10-24 13:41:59 -0700103
Yao Chen93fe3a32017-11-02 13:52:59 -0700104 if (!metric.has_condition() && mPullTagId != -1) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800105 VLOG("Setting up periodic pulling for %d", mPullTagId);
Yangster-macb8144812018-01-04 10:56:23 -0800106 mStatsPullerManager->RegisterReceiver(mPullTagId, this, bucketSizeMills);
Yao Chen93fe3a32017-11-02 13:52:59 -0700107 }
Yangster-mac94e197c2018-01-02 16:03:03 -0800108 VLOG("value metric %lld created. bucket size %lld start_time: %lld",
109 (long long)metric.id(), (long long)mBucketSizeNs, (long long)mStartTimeNs);
Chenjie Yub3dda412017-10-24 13:41:59 -0700110}
111
Chenjie Yu6736c892017-11-09 10:50:09 -0800112// for testing
Yao Chenb3561512017-11-21 18:07:17 -0800113ValueMetricProducer::ValueMetricProducer(const ConfigKey& key, const ValueMetric& metric,
114 const int conditionIndex,
Chenjie Yu6736c892017-11-09 10:50:09 -0800115 const sp<ConditionWizard>& wizard, const int pullTagId,
116 const uint64_t startTimeNs)
Yao Chenb3561512017-11-21 18:07:17 -0800117 : ValueMetricProducer(key, metric, conditionIndex, wizard, pullTagId, startTimeNs,
Chenjie Yu6736c892017-11-09 10:50:09 -0800118 make_shared<StatsPullerManager>()) {
119}
120
Chenjie Yub3dda412017-10-24 13:41:59 -0700121ValueMetricProducer::~ValueMetricProducer() {
Yao Chen93fe3a32017-11-02 13:52:59 -0700122 VLOG("~ValueMetricProducer() called");
Chenjie Yu6736c892017-11-09 10:50:09 -0800123 if (mPullTagId != -1) {
124 mStatsPullerManager->UnRegisterReceiver(mPullTagId, this);
125 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700126}
127
Yangsterf2bee6f2017-11-29 12:01:05 -0800128void ValueMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800129 VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700130}
131
Yao Chen06dba5d2018-01-26 13:38:16 -0800132void ValueMetricProducer::dropDataLocked(const uint64_t dropTimeNs) {
133 flushIfNeededLocked(dropTimeNs);
134 mPastBuckets.clear();
135}
136
Yao Chen288c6002017-12-12 13:43:18 -0800137void ValueMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs,
138 ProtoOutputStream* protoOutput) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800139 VLOG("metric %lld dump report now...", (long long)mMetricId);
Yao Chen288c6002017-12-12 13:43:18 -0800140 flushIfNeededLocked(dumpTimeNs);
Yangster-mac635b4b32018-01-23 20:17:35 -0800141 if (mPastBuckets.empty()) {
142 return;
143 }
Yangster-mac94e197c2018-01-02 16:03:03 -0800144 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
Yi Jin5ee07872018-03-05 18:18:27 -0800145 uint64_t protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_VALUE_METRICS);
Yao Chen6a8c7992017-11-29 20:02:07 +0000146
Yao Chen93fe3a32017-11-02 13:52:59 -0700147 for (const auto& pair : mPastBuckets) {
Yangster-mac93694462018-01-22 20:49:31 -0800148 const MetricDimensionKey& dimensionKey = pair.first;
149 VLOG(" dimension key %s", dimensionKey.c_str());
Yi Jin5ee07872018-03-05 18:18:27 -0800150 uint64_t wrapperToken =
Yao Chen288c6002017-12-12 13:43:18 -0800151 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
Chenjie Yub3dda412017-10-24 13:41:59 -0700152
Yangster-mac20877162017-12-22 17:19:39 -0800153 // First fill dimension.
Yi Jin5ee07872018-03-05 18:18:27 -0800154 uint64_t dimensionToken = protoOutput->start(
Yangster-mac468ff042018-01-17 12:26:34 -0800155 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800156 writeDimensionToProto(dimensionKey.getDimensionKeyInWhat(), protoOutput);
Yangster-mac20877162017-12-22 17:19:39 -0800157 protoOutput->end(dimensionToken);
Yangster-mac93694462018-01-22 20:49:31 -0800158 if (dimensionKey.hasDimensionKeyInCondition()) {
Yi Jin5ee07872018-03-05 18:18:27 -0800159 uint64_t dimensionInConditionToken = protoOutput->start(
Yangster-mac93694462018-01-22 20:49:31 -0800160 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800161 writeDimensionToProto(dimensionKey.getDimensionKeyInCondition(), protoOutput);
Yangster-mac93694462018-01-22 20:49:31 -0800162 protoOutput->end(dimensionInConditionToken);
163 }
yro2b0f8862017-11-06 14:27:31 -0800164
165 // Then fill bucket_info (ValueBucketInfo).
166 for (const auto& bucket : pair.second) {
Yi Jin5ee07872018-03-05 18:18:27 -0800167 uint64_t bucketInfoToken = protoOutput->start(
Yao Chen288c6002017-12-12 13:43:18 -0800168 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
169 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS,
170 (long long)bucket.mBucketStartNs);
171 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS,
172 (long long)bucket.mBucketEndNs);
173 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE, (long long)bucket.mValue);
174 protoOutput->end(bucketInfoToken);
yro2b0f8862017-11-06 14:27:31 -0800175 VLOG("\t bucket [%lld - %lld] count: %lld", (long long)bucket.mBucketStartNs,
176 (long long)bucket.mBucketEndNs, (long long)bucket.mValue);
177 }
Yao Chen288c6002017-12-12 13:43:18 -0800178 protoOutput->end(wrapperToken);
Chenjie Yub3dda412017-10-24 13:41:59 -0700179 }
Yao Chen288c6002017-12-12 13:43:18 -0800180 protoOutput->end(protoToken);
yro2b0f8862017-11-06 14:27:31 -0800181
Yangster-mac94e197c2018-01-02 16:03:03 -0800182 VLOG("metric %lld dump report now...", (long long)mMetricId);
Yao Chen6a8c7992017-11-29 20:02:07 +0000183 mPastBuckets.clear();
yro2b0f8862017-11-06 14:27:31 -0800184 // TODO: Clear mDimensionKeyMap once the report is dumped.
Chenjie Yub3dda412017-10-24 13:41:59 -0700185}
186
Yangsterf2bee6f2017-11-29 12:01:05 -0800187void ValueMetricProducer::onConditionChangedLocked(const bool condition, const uint64_t eventTime) {
Yao Chen6a8c7992017-11-29 20:02:07 +0000188 mCondition = condition;
Chenjie Yub3dda412017-10-24 13:41:59 -0700189
Yao Chen2794da22017-12-13 16:01:55 -0800190 if (eventTime < mCurrentBucketStartTimeNs) {
Chenjie Yua7259ab2017-12-10 08:31:05 -0800191 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTime,
192 (long long)mCurrentBucketStartTimeNs);
Yao Chen2794da22017-12-13 16:01:55 -0800193 return;
194 }
195
Chenjie Yua7259ab2017-12-10 08:31:05 -0800196 flushIfNeededLocked(eventTime);
197
Yao Chen6a8c7992017-11-29 20:02:07 +0000198 if (mPullTagId != -1) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700199 if (mCondition == true) {
Yao Chenf09569f2017-12-13 17:00:51 -0800200 mStatsPullerManager->RegisterReceiver(mPullTagId, this, mBucketSizeNs / 1000 / 1000);
Chenjie Yu6736c892017-11-09 10:50:09 -0800201 } else if (mCondition == false) {
202 mStatsPullerManager->UnRegisterReceiver(mPullTagId, this);
Chenjie Yub3dda412017-10-24 13:41:59 -0700203 }
Yangster8de69392017-11-27 13:48:29 -0800204
Yao Chen6a8c7992017-11-29 20:02:07 +0000205 vector<shared_ptr<LogEvent>> allData;
206 if (mStatsPullerManager->Pull(mPullTagId, &allData)) {
207 if (allData.size() == 0) {
208 return;
209 }
210 for (const auto& data : allData) {
Chenjie Yua7259ab2017-12-10 08:31:05 -0800211 onMatchedLogEventLocked(0, *data);
Yao Chen6a8c7992017-11-29 20:02:07 +0000212 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000213 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700214 return;
Chenjie Yub3dda412017-10-24 13:41:59 -0700215 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700216}
217
218void ValueMetricProducer::onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800219 std::lock_guard<std::mutex> lock(mMutex);
220
Yao Chenf09569f2017-12-13 17:00:51 -0800221 if (mCondition == true || mConditionTrackerIndex < 0) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700222 if (allData.size() == 0) {
223 return;
224 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800225 // For scheduled pulled data, the effective event time is snap to the nearest
226 // bucket boundary to make bucket finalize.
Yangster-mac330af582018-02-08 15:24:38 -0800227 uint64_t realEventTime = allData.at(0)->GetElapsedTimestampNs();
Yangster-maca7fb12d2018-01-03 17:17:20 -0800228 uint64_t eventTime = mStartTimeNs +
Yangster-mac330af582018-02-08 15:24:38 -0800229 ((realEventTime - mStartTimeNs) / mBucketSizeNs) * mBucketSizeNs;
Chenjie Yua7259ab2017-12-10 08:31:05 -0800230
231 mCondition = false;
Chenjie Yub3dda412017-10-24 13:41:59 -0700232 for (const auto& data : allData) {
Yangster-mac330af582018-02-08 15:24:38 -0800233 data->setElapsedTimestampNs(eventTime - 1);
Chenjie Yua7259ab2017-12-10 08:31:05 -0800234 onMatchedLogEventLocked(0, *data);
Chenjie Yub3dda412017-10-24 13:41:59 -0700235 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800236
237 mCondition = true;
238 for (const auto& data : allData) {
Yangster-mac330af582018-02-08 15:24:38 -0800239 data->setElapsedTimestampNs(eventTime);
Chenjie Yua7259ab2017-12-10 08:31:05 -0800240 onMatchedLogEventLocked(0, *data);
241 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700242 }
243}
244
Yangster-mac93694462018-01-22 20:49:31 -0800245bool ValueMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) {
Yao Chenb3561512017-11-21 18:07:17 -0800246 // ===========GuardRail==============
247 // 1. Report the tuple count if the tuple count > soft limit
248 if (mCurrentSlicedBucket.find(newKey) != mCurrentSlicedBucket.end()) {
249 return false;
250 }
251 if (mCurrentSlicedBucket.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
252 size_t newTupleCount = mCurrentSlicedBucket.size() + 1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800253 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount);
Yao Chenb3561512017-11-21 18:07:17 -0800254 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
255 if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800256 ALOGE("ValueMetric %lld dropping data for dimension key %s",
257 (long long)mMetricId, newKey.c_str());
Yao Chenb3561512017-11-21 18:07:17 -0800258 return true;
259 }
260 }
261
262 return false;
263}
264
Yangsterf2bee6f2017-11-29 12:01:05 -0800265void ValueMetricProducer::onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -0800266 const size_t matcherIndex, const MetricDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -0800267 const ConditionKey& conditionKey, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800268 const LogEvent& event) {
Yangster-mac330af582018-02-08 15:24:38 -0800269 uint64_t eventTimeNs = event.GetElapsedTimestampNs();
Yao Chen6a8c7992017-11-29 20:02:07 +0000270 if (eventTimeNs < mCurrentBucketStartTimeNs) {
271 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs,
272 (long long)mCurrentBucketStartTimeNs);
273 return;
274 }
275
Chenjie Yua7259ab2017-12-10 08:31:05 -0800276 flushIfNeededLocked(eventTimeNs);
277
Yangsterf2bee6f2017-11-29 12:01:05 -0800278 if (hitGuardRailLocked(eventKey)) {
Yangster8de69392017-11-27 13:48:29 -0800279 return;
280 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000281 Interval& interval = mCurrentSlicedBucket[eventKey];
282
Yao Chen8a8d16c2018-02-08 14:50:40 -0800283 int error = 0;
284 const long value = event.GetLong(mField, &error);
285 if (error < 0) {
Yangster-maca7fb12d2018-01-03 17:17:20 -0800286 return;
287 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000288
Chenjie Yua7259ab2017-12-10 08:31:05 -0800289 if (mPullTagId != -1) { // for pulled events
290 if (mCondition == true) {
291 interval.start = value;
292 interval.startUpdated = true;
Yao Chen6a8c7992017-11-29 20:02:07 +0000293 } else {
Chenjie Yu0ed268b2018-01-10 09:37:10 -0800294 // Generally we expect value to be monotonically increasing.
295 // If not, there was a reset event. We take the absolute value as
296 // diff in this case.
Chenjie Yua7259ab2017-12-10 08:31:05 -0800297 if (interval.startUpdated) {
Chenjie Yu0ed268b2018-01-10 09:37:10 -0800298 if (value > interval.start) {
299 interval.sum += (value - interval.start);
300 } else {
301 interval.sum += value;
302 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800303 interval.startUpdated = false;
Yao Chen6a8c7992017-11-29 20:02:07 +0000304 } else {
Chenjie Yua7259ab2017-12-10 08:31:05 -0800305 VLOG("No start for matching end %ld", value);
306 interval.tainted += 1;
Yao Chen6a8c7992017-11-29 20:02:07 +0000307 }
308 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800309 } else { // for pushed events
310 interval.sum += value;
Yangster8de69392017-11-27 13:48:29 -0800311 }
Bookatzde1b55622017-12-14 18:38:27 -0800312
David Chen27785a82018-01-19 17:06:45 -0800313 long wholeBucketVal = interval.sum;
314 auto prev = mCurrentFullBucket.find(eventKey);
315 if (prev != mCurrentFullBucket.end()) {
316 wholeBucketVal += prev->second;
317 }
Bookatzde1b55622017-12-14 18:38:27 -0800318 for (auto& tracker : mAnomalyTrackers) {
David Chen27785a82018-01-19 17:06:45 -0800319 tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, eventKey, wholeBucketVal);
Bookatzde1b55622017-12-14 18:38:27 -0800320 }
Yangster8de69392017-11-27 13:48:29 -0800321}
322
Yangsterf2bee6f2017-11-29 12:01:05 -0800323void ValueMetricProducer::flushIfNeededLocked(const uint64_t& eventTimeNs) {
David Chen27785a82018-01-19 17:06:45 -0800324 uint64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs();
325
326 if (currentBucketEndTimeNs > eventTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700327 VLOG("eventTime is %lld, less than next bucket start time %lld", (long long)eventTimeNs,
David Chen27785a82018-01-19 17:06:45 -0800328 (long long)(currentBucketEndTimeNs));
Chenjie Yub3dda412017-10-24 13:41:59 -0700329 return;
330 }
David Chen27785a82018-01-19 17:06:45 -0800331
332 flushCurrentBucketLocked(eventTimeNs);
333
334 int64_t numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs;
335 mCurrentBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs;
336 mCurrentBucketNum += numBucketsForward;
337
338 if (numBucketsForward > 1) {
339 VLOG("Skipping forward %lld buckets", (long long)numBucketsForward);
340 }
341 VLOG("metric %lld: new bucket start time: %lld", (long long)mMetricId,
342 (long long)mCurrentBucketStartTimeNs);
343}
344
345void ValueMetricProducer::flushCurrentBucketLocked(const uint64_t& eventTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700346 VLOG("finalizing bucket for %ld, dumping %d slices", (long)mCurrentBucketStartTimeNs,
347 (int)mCurrentSlicedBucket.size());
David Chen27785a82018-01-19 17:06:45 -0800348 uint64_t fullBucketEndTimeNs = getCurrentBucketEndTimeNs();
349
yro2b0f8862017-11-06 14:27:31 -0800350 ValueBucket info;
351 info.mBucketStartNs = mCurrentBucketStartTimeNs;
David Chen27785a82018-01-19 17:06:45 -0800352 if (eventTimeNs < fullBucketEndTimeNs) {
353 info.mBucketEndNs = eventTimeNs;
354 } else {
355 info.mBucketEndNs = fullBucketEndTimeNs;
356 }
Yangster-mace2cd6d52017-11-09 20:38:30 -0800357 info.mBucketNum = mCurrentBucketNum;
Chenjie Yub3dda412017-10-24 13:41:59 -0700358
Chenjie Yu6736c892017-11-09 10:50:09 -0800359 int tainted = 0;
Chenjie Yub3dda412017-10-24 13:41:59 -0700360 for (const auto& slice : mCurrentSlicedBucket) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800361 tainted += slice.second.tainted;
Chenjie Yua7259ab2017-12-10 08:31:05 -0800362 info.mValue = slice.second.sum;
Yao Chen93fe3a32017-11-02 13:52:59 -0700363 // it will auto create new vector of ValuebucketInfo if the key is not found.
364 auto& bucketList = mPastBuckets[slice.first];
365 bucketList.push_back(info);
Chenjie Yub3dda412017-10-24 13:41:59 -0700366 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800367 VLOG("%d tainted pairs in the bucket", tainted);
Chenjie Yub3dda412017-10-24 13:41:59 -0700368
David Chen27785a82018-01-19 17:06:45 -0800369 if (eventTimeNs > fullBucketEndTimeNs) { // If full bucket, send to anomaly tracker.
370 // Accumulate partial buckets with current value and then send to anomaly tracker.
371 if (mCurrentFullBucket.size() > 0) {
372 for (const auto& slice : mCurrentSlicedBucket) {
373 mCurrentFullBucket[slice.first] += slice.second.sum;
374 }
375 for (const auto& slice : mCurrentFullBucket) {
376 for (auto& tracker : mAnomalyTrackers) {
377 if (tracker != nullptr) {
378 tracker->addPastBucket(slice.first, slice.second, mCurrentBucketNum);
379 }
380 }
381 }
382 mCurrentFullBucket.clear();
383 } else {
384 // Skip aggregating the partial buckets since there's no previous partial bucket.
385 for (const auto& slice : mCurrentSlicedBucket) {
386 for (auto& tracker : mAnomalyTrackers) {
387 if (tracker != nullptr) {
388 tracker->addPastBucket(slice.first, slice.second.sum, mCurrentBucketNum);
389 }
390 }
391 }
392 }
393 } else {
394 // Accumulate partial bucket.
395 for (const auto& slice : mCurrentSlicedBucket) {
396 mCurrentFullBucket[slice.first] += slice.second.sum;
397 }
398 }
399
Chenjie Yub3dda412017-10-24 13:41:59 -0700400 // Reset counters
Chenjie Yua7259ab2017-12-10 08:31:05 -0800401 mCurrentSlicedBucket.clear();
Chenjie Yub3dda412017-10-24 13:41:59 -0700402}
403
Yangsterf2bee6f2017-11-29 12:01:05 -0800404size_t ValueMetricProducer::byteSizeLocked() const {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800405 size_t totalSize = 0;
406 for (const auto& pair : mPastBuckets) {
407 totalSize += pair.second.size() * kBucketSize;
408 }
409 return totalSize;
yro2b0f8862017-11-06 14:27:31 -0800410}
411
Chenjie Yub3dda412017-10-24 13:41:59 -0700412} // namespace statsd
413} // namespace os
Yao Chen93fe3a32017-11-02 13:52:59 -0700414} // namespace android