blob: 7b1944c595e5a53b095756fa09533573e4039eda [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
Yangster-maca7fb12d2018-01-03 17:17:20 -080020#include "dimension.h"
Chenjie Yub3dda412017-10-24 13:41:59 -070021#include "ValueMetricProducer.h"
Yao Chenb3561512017-11-21 18:07:17 -080022#include "guardrail/StatsdStats.h"
Yangster-mac20877162017-12-22 17:19:39 -080023#include "stats_log_util.h"
Chenjie Yub3dda412017-10-24 13:41:59 -070024
25#include <cutils/log.h>
26#include <limits.h>
27#include <stdlib.h>
28
yrob0378b02017-11-09 20:36:25 -080029using android::util::FIELD_COUNT_REPEATED;
yro2b0f8862017-11-06 14:27:31 -080030using android::util::FIELD_TYPE_BOOL;
31using android::util::FIELD_TYPE_FLOAT;
32using android::util::FIELD_TYPE_INT32;
33using android::util::FIELD_TYPE_INT64;
34using android::util::FIELD_TYPE_MESSAGE;
Yangster-macd1815dc2017-11-13 21:43:15 -080035using android::util::FIELD_TYPE_STRING;
yro2b0f8862017-11-06 14:27:31 -080036using android::util::ProtoOutputStream;
Chenjie Yub3dda412017-10-24 13:41:59 -070037using std::list;
Chenjie Yu6736c892017-11-09 10:50:09 -080038using std::make_pair;
Chenjie Yub3dda412017-10-24 13:41:59 -070039using std::make_shared;
Yao Chen93fe3a32017-11-02 13:52:59 -070040using std::map;
Chenjie Yub3dda412017-10-24 13:41:59 -070041using std::shared_ptr;
42using std::unique_ptr;
Yao Chen93fe3a32017-11-02 13:52:59 -070043using std::unordered_map;
Chenjie Yub3dda412017-10-24 13:41:59 -070044
45namespace android {
46namespace os {
47namespace statsd {
48
yro2b0f8862017-11-06 14:27:31 -080049// for StatsLogReport
Yangster-mac94e197c2018-01-02 16:03:03 -080050const int FIELD_ID_ID = 1;
yro2b0f8862017-11-06 14:27:31 -080051const int FIELD_ID_VALUE_METRICS = 7;
52// for ValueMetricDataWrapper
53const int FIELD_ID_DATA = 1;
54// for ValueMetricData
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;
yro2b0f8862017-11-06 14:27:31 -080058// for ValueBucketInfo
59const int FIELD_ID_START_BUCKET_NANOS = 1;
60const int FIELD_ID_END_BUCKET_NANOS = 2;
61const int FIELD_ID_VALUE = 3;
62
Chenjie Yub3dda412017-10-24 13:41:59 -070063// ValueMetric has a minimum bucket size of 10min so that we don't pull too frequently
Yao Chenb3561512017-11-21 18:07:17 -080064ValueMetricProducer::ValueMetricProducer(const ConfigKey& key, const ValueMetric& metric,
65 const int conditionIndex,
Yao Chen93fe3a32017-11-02 13:52:59 -070066 const sp<ConditionWizard>& wizard, const int pullTagId,
Chenjie Yu6736c892017-11-09 10:50:09 -080067 const uint64_t startTimeNs,
68 shared_ptr<StatsPullerManager> statsPullerManager)
Yangster-mac94e197c2018-01-02 16:03:03 -080069 : MetricProducer(metric.id(), key, startTimeNs, conditionIndex, wizard),
Yao Chenf09569f2017-12-13 17:00:51 -080070 mValueField(metric.value_field()),
Chenjie Yu6736c892017-11-09 10:50:09 -080071 mStatsPullerManager(statsPullerManager),
72 mPullTagId(pullTagId) {
Yao Chen93fe3a32017-11-02 13:52:59 -070073 // TODO: valuemetric for pushed events may need unlimited bucket length
Yangster-macb8144812018-01-04 10:56:23 -080074 int64_t bucketSizeMills = 0;
75 if (metric.has_bucket()) {
76 bucketSizeMills = TimeUnitToBucketSizeInMillis(metric.bucket());
Chenjie Yu6736c892017-11-09 10:50:09 -080077 } else {
Yangster-macb8144812018-01-04 10:56:23 -080078 bucketSizeMills = TimeUnitToBucketSizeInMillis(ONE_HOUR);
Chenjie Yu6736c892017-11-09 10:50:09 -080079 }
Chenjie Yub3dda412017-10-24 13:41:59 -070080
Yangster-macb8144812018-01-04 10:56:23 -080081 mBucketSizeNs = bucketSizeMills * 1000000;
Yangster-mac93694462018-01-22 20:49:31 -080082 mDimensionsInWhat = metric.dimensions_in_what();
83 mDimensionsInCondition = metric.dimensions_in_condition();
Chenjie Yub3dda412017-10-24 13:41:59 -070084
Yao Chen93fe3a32017-11-02 13:52:59 -070085 if (metric.links().size() > 0) {
86 mConditionLinks.insert(mConditionLinks.begin(), metric.links().begin(),
87 metric.links().end());
Yao Chen93fe3a32017-11-02 13:52:59 -070088 }
Yangster-mac93694462018-01-22 20:49:31 -080089 mConditionSliced = (metric.links().size() > 0)||
90 (mDimensionsInCondition.has_field() && mDimensionsInCondition.child_size() > 0);
Chenjie Yub3dda412017-10-24 13:41:59 -070091
Yao Chen93fe3a32017-11-02 13:52:59 -070092 if (!metric.has_condition() && mPullTagId != -1) {
Chenjie Yu6736c892017-11-09 10:50:09 -080093 VLOG("Setting up periodic pulling for %d", mPullTagId);
Yangster-macb8144812018-01-04 10:56:23 -080094 mStatsPullerManager->RegisterReceiver(mPullTagId, this, bucketSizeMills);
Yao Chen93fe3a32017-11-02 13:52:59 -070095 }
Yangster-mac94e197c2018-01-02 16:03:03 -080096 VLOG("value metric %lld created. bucket size %lld start_time: %lld",
97 (long long)metric.id(), (long long)mBucketSizeNs, (long long)mStartTimeNs);
Chenjie Yub3dda412017-10-24 13:41:59 -070098}
99
Chenjie Yu6736c892017-11-09 10:50:09 -0800100// for testing
Yao Chenb3561512017-11-21 18:07:17 -0800101ValueMetricProducer::ValueMetricProducer(const ConfigKey& key, const ValueMetric& metric,
102 const int conditionIndex,
Chenjie Yu6736c892017-11-09 10:50:09 -0800103 const sp<ConditionWizard>& wizard, const int pullTagId,
104 const uint64_t startTimeNs)
Yao Chenb3561512017-11-21 18:07:17 -0800105 : ValueMetricProducer(key, metric, conditionIndex, wizard, pullTagId, startTimeNs,
Chenjie Yu6736c892017-11-09 10:50:09 -0800106 make_shared<StatsPullerManager>()) {
107}
108
Chenjie Yub3dda412017-10-24 13:41:59 -0700109ValueMetricProducer::~ValueMetricProducer() {
Yao Chen93fe3a32017-11-02 13:52:59 -0700110 VLOG("~ValueMetricProducer() called");
Chenjie Yu6736c892017-11-09 10:50:09 -0800111 if (mPullTagId != -1) {
112 mStatsPullerManager->UnRegisterReceiver(mPullTagId, this);
113 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700114}
115
Yangsterf2bee6f2017-11-29 12:01:05 -0800116void ValueMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800117 VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700118}
119
Yangster-mac20877162017-12-22 17:19:39 -0800120void ValueMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs, StatsLogReport* report) {
121 flushIfNeededLocked(dumpTimeNs);
Yangster-mac94e197c2018-01-02 16:03:03 -0800122 report->set_metric_id(mMetricId);
Yangster-mac20877162017-12-22 17:19:39 -0800123 auto value_metrics = report->mutable_value_metrics();
124 for (const auto& pair : mPastBuckets) {
125 ValueMetricData* metricData = value_metrics->add_data();
Yangster-mac93694462018-01-22 20:49:31 -0800126 *metricData->mutable_dimensions_in_what() =
127 pair.first.getDimensionKeyInWhat().getDimensionsValue();
128 *metricData->mutable_dimensions_in_condition() =
129 pair.first.getDimensionKeyInCondition().getDimensionsValue();
Yangster-mac20877162017-12-22 17:19:39 -0800130 for (const auto& bucket : pair.second) {
131 ValueBucketInfo* bucketInfo = metricData->add_bucket_info();
132 bucketInfo->set_start_bucket_nanos(bucket.mBucketStartNs);
133 bucketInfo->set_end_bucket_nanos(bucket.mBucketEndNs);
134 bucketInfo->set_value(bucket.mValue);
135 }
136 }
137}
138
Yao Chen288c6002017-12-12 13:43:18 -0800139void ValueMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs,
140 ProtoOutputStream* protoOutput) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800141 VLOG("metric %lld dump report now...", (long long)mMetricId);
Yao Chen288c6002017-12-12 13:43:18 -0800142 flushIfNeededLocked(dumpTimeNs);
Yangster-mac635b4b32018-01-23 20:17:35 -0800143 if (mPastBuckets.empty()) {
144 return;
145 }
Yangster-mac94e197c2018-01-02 16:03:03 -0800146 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
Yao Chen288c6002017-12-12 13:43:18 -0800147 long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_VALUE_METRICS);
Yao Chen6a8c7992017-11-29 20:02:07 +0000148
Yao Chen93fe3a32017-11-02 13:52:59 -0700149 for (const auto& pair : mPastBuckets) {
Yangster-mac93694462018-01-22 20:49:31 -0800150 const MetricDimensionKey& dimensionKey = pair.first;
151 VLOG(" dimension key %s", dimensionKey.c_str());
yrob0378b02017-11-09 20:36:25 -0800152 long long wrapperToken =
Yao Chen288c6002017-12-12 13:43:18 -0800153 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
Chenjie Yub3dda412017-10-24 13:41:59 -0700154
Yangster-mac20877162017-12-22 17:19:39 -0800155 // First fill dimension.
156 long long dimensionToken = protoOutput->start(
Yangster-mac468ff042018-01-17 12:26:34 -0800157 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT);
Yangster-mac93694462018-01-22 20:49:31 -0800158 writeDimensionsValueProtoToStream(
159 dimensionKey.getDimensionKeyInWhat().getDimensionsValue(), protoOutput);
Yangster-mac20877162017-12-22 17:19:39 -0800160 protoOutput->end(dimensionToken);
Yangster-mac93694462018-01-22 20:49:31 -0800161 if (dimensionKey.hasDimensionKeyInCondition()) {
162 long long dimensionInConditionToken = protoOutput->start(
163 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION);
164 writeDimensionsValueProtoToStream(
165 dimensionKey.getDimensionKeyInCondition().getDimensionsValue(), protoOutput);
166 protoOutput->end(dimensionInConditionToken);
167 }
yro2b0f8862017-11-06 14:27:31 -0800168
169 // Then fill bucket_info (ValueBucketInfo).
170 for (const auto& bucket : pair.second) {
Yao Chen288c6002017-12-12 13:43:18 -0800171 long long bucketInfoToken = protoOutput->start(
172 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
173 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS,
174 (long long)bucket.mBucketStartNs);
175 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS,
176 (long long)bucket.mBucketEndNs);
177 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE, (long long)bucket.mValue);
178 protoOutput->end(bucketInfoToken);
yro2b0f8862017-11-06 14:27:31 -0800179 VLOG("\t bucket [%lld - %lld] count: %lld", (long long)bucket.mBucketStartNs,
180 (long long)bucket.mBucketEndNs, (long long)bucket.mValue);
181 }
Yao Chen288c6002017-12-12 13:43:18 -0800182 protoOutput->end(wrapperToken);
Chenjie Yub3dda412017-10-24 13:41:59 -0700183 }
Yao Chen288c6002017-12-12 13:43:18 -0800184 protoOutput->end(protoToken);
yro2b0f8862017-11-06 14:27:31 -0800185
Yangster-mac94e197c2018-01-02 16:03:03 -0800186 VLOG("metric %lld dump report now...", (long long)mMetricId);
Yao Chen6a8c7992017-11-29 20:02:07 +0000187 mPastBuckets.clear();
yro2b0f8862017-11-06 14:27:31 -0800188 // TODO: Clear mDimensionKeyMap once the report is dumped.
Chenjie Yub3dda412017-10-24 13:41:59 -0700189}
190
Yangsterf2bee6f2017-11-29 12:01:05 -0800191void ValueMetricProducer::onConditionChangedLocked(const bool condition, const uint64_t eventTime) {
Yao Chen6a8c7992017-11-29 20:02:07 +0000192 mCondition = condition;
Chenjie Yub3dda412017-10-24 13:41:59 -0700193
Yao Chen2794da22017-12-13 16:01:55 -0800194 if (eventTime < mCurrentBucketStartTimeNs) {
Chenjie Yua7259ab2017-12-10 08:31:05 -0800195 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTime,
196 (long long)mCurrentBucketStartTimeNs);
Yao Chen2794da22017-12-13 16:01:55 -0800197 return;
198 }
199
Chenjie Yua7259ab2017-12-10 08:31:05 -0800200 flushIfNeededLocked(eventTime);
201
Yao Chen6a8c7992017-11-29 20:02:07 +0000202 if (mPullTagId != -1) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700203 if (mCondition == true) {
Yao Chenf09569f2017-12-13 17:00:51 -0800204 mStatsPullerManager->RegisterReceiver(mPullTagId, this, mBucketSizeNs / 1000 / 1000);
Chenjie Yu6736c892017-11-09 10:50:09 -0800205 } else if (mCondition == false) {
206 mStatsPullerManager->UnRegisterReceiver(mPullTagId, this);
Chenjie Yub3dda412017-10-24 13:41:59 -0700207 }
Yangster8de69392017-11-27 13:48:29 -0800208
Yao Chen6a8c7992017-11-29 20:02:07 +0000209 vector<shared_ptr<LogEvent>> allData;
210 if (mStatsPullerManager->Pull(mPullTagId, &allData)) {
211 if (allData.size() == 0) {
212 return;
213 }
214 for (const auto& data : allData) {
Chenjie Yua7259ab2017-12-10 08:31:05 -0800215 onMatchedLogEventLocked(0, *data);
Yao Chen6a8c7992017-11-29 20:02:07 +0000216 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000217 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700218 return;
Chenjie Yub3dda412017-10-24 13:41:59 -0700219 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700220}
221
222void ValueMetricProducer::onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800223 std::lock_guard<std::mutex> lock(mMutex);
224
Yao Chenf09569f2017-12-13 17:00:51 -0800225 if (mCondition == true || mConditionTrackerIndex < 0) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700226 if (allData.size() == 0) {
227 return;
228 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800229 // For scheduled pulled data, the effective event time is snap to the nearest
230 // bucket boundary to make bucket finalize.
231 uint64_t realEventTime = allData.at(0)->GetTimestampNs();
Yangster-maca7fb12d2018-01-03 17:17:20 -0800232 uint64_t eventTime = mStartTimeNs +
233 ((realEventTime - mStartTimeNs)/mBucketSizeNs) * mBucketSizeNs;
Chenjie Yua7259ab2017-12-10 08:31:05 -0800234
235 mCondition = false;
Chenjie Yub3dda412017-10-24 13:41:59 -0700236 for (const auto& data : allData) {
Chenjie Yua7259ab2017-12-10 08:31:05 -0800237 data->setTimestampNs(eventTime-1);
238 onMatchedLogEventLocked(0, *data);
Chenjie Yub3dda412017-10-24 13:41:59 -0700239 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800240
241 mCondition = true;
242 for (const auto& data : allData) {
243 data->setTimestampNs(eventTime);
244 onMatchedLogEventLocked(0, *data);
245 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700246 }
247}
248
Yangster-mac93694462018-01-22 20:49:31 -0800249bool ValueMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) {
Yao Chenb3561512017-11-21 18:07:17 -0800250 // ===========GuardRail==============
251 // 1. Report the tuple count if the tuple count > soft limit
252 if (mCurrentSlicedBucket.find(newKey) != mCurrentSlicedBucket.end()) {
253 return false;
254 }
255 if (mCurrentSlicedBucket.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) {
256 size_t newTupleCount = mCurrentSlicedBucket.size() + 1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800257 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount);
Yao Chenb3561512017-11-21 18:07:17 -0800258 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
259 if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800260 ALOGE("ValueMetric %lld dropping data for dimension key %s",
261 (long long)mMetricId, newKey.c_str());
Yao Chenb3561512017-11-21 18:07:17 -0800262 return true;
263 }
264 }
265
266 return false;
267}
268
Yangsterf2bee6f2017-11-29 12:01:05 -0800269void ValueMetricProducer::onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -0800270 const size_t matcherIndex, const MetricDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -0800271 const ConditionKey& conditionKey, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800272 const LogEvent& event) {
Yangster8de69392017-11-27 13:48:29 -0800273 uint64_t eventTimeNs = event.GetTimestampNs();
Yao Chen6a8c7992017-11-29 20:02:07 +0000274 if (eventTimeNs < mCurrentBucketStartTimeNs) {
275 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs,
276 (long long)mCurrentBucketStartTimeNs);
277 return;
278 }
279
Chenjie Yua7259ab2017-12-10 08:31:05 -0800280 flushIfNeededLocked(eventTimeNs);
281
Yangsterf2bee6f2017-11-29 12:01:05 -0800282 if (hitGuardRailLocked(eventKey)) {
Yangster8de69392017-11-27 13:48:29 -0800283 return;
284 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000285 Interval& interval = mCurrentSlicedBucket[eventKey];
286
Yangster-maca7fb12d2018-01-03 17:17:20 -0800287 std::shared_ptr<FieldValueMap> valueFieldMap = getValueFields(event);
288 if (valueFieldMap->empty() || valueFieldMap->size() > 1) {
289 return;
290 }
291 const long value = getLongFromDimenValue(valueFieldMap->begin()->second);
Yao Chen6a8c7992017-11-29 20:02:07 +0000292
Chenjie Yua7259ab2017-12-10 08:31:05 -0800293 if (mPullTagId != -1) { // for pulled events
294 if (mCondition == true) {
295 interval.start = value;
296 interval.startUpdated = true;
Yao Chen6a8c7992017-11-29 20:02:07 +0000297 } else {
Chenjie Yu0ed268b2018-01-10 09:37:10 -0800298 // Generally we expect value to be monotonically increasing.
299 // If not, there was a reset event. We take the absolute value as
300 // diff in this case.
Chenjie Yua7259ab2017-12-10 08:31:05 -0800301 if (interval.startUpdated) {
Chenjie Yu0ed268b2018-01-10 09:37:10 -0800302 if (value > interval.start) {
303 interval.sum += (value - interval.start);
304 } else {
305 interval.sum += value;
306 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800307 interval.startUpdated = false;
Yao Chen6a8c7992017-11-29 20:02:07 +0000308 } else {
Chenjie Yua7259ab2017-12-10 08:31:05 -0800309 VLOG("No start for matching end %ld", value);
310 interval.tainted += 1;
Yao Chen6a8c7992017-11-29 20:02:07 +0000311 }
312 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800313 } else { // for pushed events
314 interval.sum += value;
Yangster8de69392017-11-27 13:48:29 -0800315 }
Bookatzde1b55622017-12-14 18:38:27 -0800316
David Chen27785a82018-01-19 17:06:45 -0800317 long wholeBucketVal = interval.sum;
318 auto prev = mCurrentFullBucket.find(eventKey);
319 if (prev != mCurrentFullBucket.end()) {
320 wholeBucketVal += prev->second;
321 }
Bookatzde1b55622017-12-14 18:38:27 -0800322 for (auto& tracker : mAnomalyTrackers) {
David Chen27785a82018-01-19 17:06:45 -0800323 tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, eventKey, wholeBucketVal);
Bookatzde1b55622017-12-14 18:38:27 -0800324 }
Yangster8de69392017-11-27 13:48:29 -0800325}
326
Yangster-maca7fb12d2018-01-03 17:17:20 -0800327std::shared_ptr<FieldValueMap> ValueMetricProducer::getValueFields(const LogEvent& event) {
328 std::shared_ptr<FieldValueMap> valueFields =
329 std::make_shared<FieldValueMap>(event.getFieldValueMap());
330 filterFields(mValueField, valueFields.get());
331 return valueFields;
Chenjie Yub3dda412017-10-24 13:41:59 -0700332}
333
Yangsterf2bee6f2017-11-29 12:01:05 -0800334void ValueMetricProducer::flushIfNeededLocked(const uint64_t& eventTimeNs) {
David Chen27785a82018-01-19 17:06:45 -0800335 uint64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs();
336
337 if (currentBucketEndTimeNs > eventTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700338 VLOG("eventTime is %lld, less than next bucket start time %lld", (long long)eventTimeNs,
David Chen27785a82018-01-19 17:06:45 -0800339 (long long)(currentBucketEndTimeNs));
Chenjie Yub3dda412017-10-24 13:41:59 -0700340 return;
341 }
David Chen27785a82018-01-19 17:06:45 -0800342
343 flushCurrentBucketLocked(eventTimeNs);
344
345 int64_t numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs;
346 mCurrentBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs;
347 mCurrentBucketNum += numBucketsForward;
348
349 if (numBucketsForward > 1) {
350 VLOG("Skipping forward %lld buckets", (long long)numBucketsForward);
351 }
352 VLOG("metric %lld: new bucket start time: %lld", (long long)mMetricId,
353 (long long)mCurrentBucketStartTimeNs);
354}
355
356void ValueMetricProducer::flushCurrentBucketLocked(const uint64_t& eventTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700357 VLOG("finalizing bucket for %ld, dumping %d slices", (long)mCurrentBucketStartTimeNs,
358 (int)mCurrentSlicedBucket.size());
David Chen27785a82018-01-19 17:06:45 -0800359 uint64_t fullBucketEndTimeNs = getCurrentBucketEndTimeNs();
360
yro2b0f8862017-11-06 14:27:31 -0800361 ValueBucket info;
362 info.mBucketStartNs = mCurrentBucketStartTimeNs;
David Chen27785a82018-01-19 17:06:45 -0800363 if (eventTimeNs < fullBucketEndTimeNs) {
364 info.mBucketEndNs = eventTimeNs;
365 } else {
366 info.mBucketEndNs = fullBucketEndTimeNs;
367 }
Yangster-mace2cd6d52017-11-09 20:38:30 -0800368 info.mBucketNum = mCurrentBucketNum;
Chenjie Yub3dda412017-10-24 13:41:59 -0700369
Chenjie Yu6736c892017-11-09 10:50:09 -0800370 int tainted = 0;
Chenjie Yub3dda412017-10-24 13:41:59 -0700371 for (const auto& slice : mCurrentSlicedBucket) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800372 tainted += slice.second.tainted;
Chenjie Yua7259ab2017-12-10 08:31:05 -0800373 info.mValue = slice.second.sum;
Yao Chen93fe3a32017-11-02 13:52:59 -0700374 // it will auto create new vector of ValuebucketInfo if the key is not found.
375 auto& bucketList = mPastBuckets[slice.first];
376 bucketList.push_back(info);
Chenjie Yub3dda412017-10-24 13:41:59 -0700377 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800378 VLOG("%d tainted pairs in the bucket", tainted);
Chenjie Yub3dda412017-10-24 13:41:59 -0700379
David Chen27785a82018-01-19 17:06:45 -0800380 if (eventTimeNs > fullBucketEndTimeNs) { // If full bucket, send to anomaly tracker.
381 // Accumulate partial buckets with current value and then send to anomaly tracker.
382 if (mCurrentFullBucket.size() > 0) {
383 for (const auto& slice : mCurrentSlicedBucket) {
384 mCurrentFullBucket[slice.first] += slice.second.sum;
385 }
386 for (const auto& slice : mCurrentFullBucket) {
387 for (auto& tracker : mAnomalyTrackers) {
388 if (tracker != nullptr) {
389 tracker->addPastBucket(slice.first, slice.second, mCurrentBucketNum);
390 }
391 }
392 }
393 mCurrentFullBucket.clear();
394 } else {
395 // Skip aggregating the partial buckets since there's no previous partial bucket.
396 for (const auto& slice : mCurrentSlicedBucket) {
397 for (auto& tracker : mAnomalyTrackers) {
398 if (tracker != nullptr) {
399 tracker->addPastBucket(slice.first, slice.second.sum, mCurrentBucketNum);
400 }
401 }
402 }
403 }
404 } else {
405 // Accumulate partial bucket.
406 for (const auto& slice : mCurrentSlicedBucket) {
407 mCurrentFullBucket[slice.first] += slice.second.sum;
408 }
409 }
410
Chenjie Yub3dda412017-10-24 13:41:59 -0700411 // Reset counters
Chenjie Yua7259ab2017-12-10 08:31:05 -0800412 mCurrentSlicedBucket.clear();
Chenjie Yub3dda412017-10-24 13:41:59 -0700413}
414
Yangsterf2bee6f2017-11-29 12:01:05 -0800415size_t ValueMetricProducer::byteSizeLocked() const {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800416 size_t totalSize = 0;
417 for (const auto& pair : mPastBuckets) {
418 totalSize += pair.second.size() * kBucketSize;
419 }
420 return totalSize;
yro2b0f8862017-11-06 14:27:31 -0800421}
422
Chenjie Yub3dda412017-10-24 13:41:59 -0700423} // namespace statsd
424} // namespace os
Yao Chen93fe3a32017-11-02 13:52:59 -0700425} // namespace android