blob: 844c72801ea61a2891fd8b25251c2eb8e2083956 [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"
Chenjie Yuc5875052018-03-09 10:13:11 -080021#include "../guardrail/StatsdStats.h"
22#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,
Yangster-mac15f6bbc2018-04-08 11:52:26 -070066 const int64_t timeBaseNs, const int64_t startTimestampNs,
Chenjie Yu6736c892017-11-09 10:50:09 -080067 shared_ptr<StatsPullerManager> statsPullerManager)
Yangster-mac15f6bbc2018-04-08 11:52:26 -070068 : MetricProducer(metric.id(), key, timeBaseNs, conditionIndex, wizard),
Yao Chenf09569f2017-12-13 17:00:51 -080069 mValueField(metric.value_field()),
Chenjie Yu6736c892017-11-09 10:50:09 -080070 mStatsPullerManager(statsPullerManager),
Chenjie Yuc5875052018-03-09 10:13:11 -080071 mPullTagId(pullTagId),
72 mDimensionSoftLimit(StatsdStats::kAtomDimensionKeySizeLimitMap.find(pullTagId) !=
73 StatsdStats::kAtomDimensionKeySizeLimitMap.end()
74 ? StatsdStats::kAtomDimensionKeySizeLimitMap.at(pullTagId).first
75 : StatsdStats::kDimensionKeySizeSoftLimit),
76 mDimensionHardLimit(StatsdStats::kAtomDimensionKeySizeLimitMap.find(pullTagId) !=
77 StatsdStats::kAtomDimensionKeySizeLimitMap.end()
78 ? StatsdStats::kAtomDimensionKeySizeLimitMap.at(pullTagId).second
79 : StatsdStats::kDimensionKeySizeHardLimit) {
Yao Chen93fe3a32017-11-02 13:52:59 -070080 // TODO: valuemetric for pushed events may need unlimited bucket length
Yangster-macb8144812018-01-04 10:56:23 -080081 int64_t bucketSizeMills = 0;
82 if (metric.has_bucket()) {
yro59cc24d2018-02-13 20:17:32 -080083 bucketSizeMills = TimeUnitToBucketSizeInMillisGuardrailed(key.GetUid(), metric.bucket());
Chenjie Yu6736c892017-11-09 10:50:09 -080084 } else {
Yangster-macb8144812018-01-04 10:56:23 -080085 bucketSizeMills = TimeUnitToBucketSizeInMillis(ONE_HOUR);
Chenjie Yu6736c892017-11-09 10:50:09 -080086 }
Chenjie Yub3dda412017-10-24 13:41:59 -070087
Yangster-macb8144812018-01-04 10:56:23 -080088 mBucketSizeNs = bucketSizeMills * 1000000;
Yao Chen8a8d16c2018-02-08 14:50:40 -080089 if (metric.has_dimensions_in_what()) {
90 translateFieldMatcher(metric.dimensions_in_what(), &mDimensionsInWhat);
Yangster13fb7e42018-03-07 17:30:49 -080091 mContainANYPositionInDimensionsInWhat = HasPositionANY(metric.dimensions_in_what());
Yao Chen8a8d16c2018-02-08 14:50:40 -080092 }
93
94 if (metric.has_dimensions_in_condition()) {
95 translateFieldMatcher(metric.dimensions_in_condition(), &mDimensionsInCondition);
96 }
Chenjie Yub3dda412017-10-24 13:41:59 -070097
Yao Chen93fe3a32017-11-02 13:52:59 -070098 if (metric.links().size() > 0) {
Yao Chen8a8d16c2018-02-08 14:50:40 -080099 for (const auto& link : metric.links()) {
100 Metric2Condition mc;
101 mc.conditionId = link.condition();
102 translateFieldMatcher(link.fields_in_what(), &mc.metricFields);
103 translateFieldMatcher(link.fields_in_condition(), &mc.conditionFields);
104 mMetric2ConditionLinks.push_back(mc);
105 }
Yao Chen93fe3a32017-11-02 13:52:59 -0700106 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800107
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700108 if (mValueField.child_size() > 0) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800109 mField = mValueField.child(0).field();
110 }
111 mConditionSliced = (metric.links().size() > 0) || (mDimensionsInCondition.size() > 0);
Chenjie Yub3dda412017-10-24 13:41:59 -0700112
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700113 // Kicks off the puller immediately.
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700114 flushIfNeededLocked(startTimestampNs);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700115 if (mPullTagId != -1) {
116 mStatsPullerManager->RegisterReceiver(
117 mPullTagId, this, mCurrentBucketStartTimeNs + mBucketSizeNs, mBucketSizeNs);
Yao Chen93fe3a32017-11-02 13:52:59 -0700118 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700119
Yangster-mac94e197c2018-01-02 16:03:03 -0800120 VLOG("value metric %lld created. bucket size %lld start_time: %lld",
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700121 (long long)metric.id(), (long long)mBucketSizeNs, (long long)mTimeBaseNs);
Chenjie Yub3dda412017-10-24 13:41:59 -0700122}
123
Chenjie Yu6736c892017-11-09 10:50:09 -0800124// for testing
Yao Chenb3561512017-11-21 18:07:17 -0800125ValueMetricProducer::ValueMetricProducer(const ConfigKey& key, const ValueMetric& metric,
126 const int conditionIndex,
Chenjie Yu6736c892017-11-09 10:50:09 -0800127 const sp<ConditionWizard>& wizard, const int pullTagId,
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700128 const int64_t timeBaseNs, const int64_t startTimeNs)
129 : ValueMetricProducer(key, metric, conditionIndex, wizard, pullTagId, timeBaseNs, startTimeNs,
Chenjie Yu6736c892017-11-09 10:50:09 -0800130 make_shared<StatsPullerManager>()) {
131}
132
Chenjie Yub3dda412017-10-24 13:41:59 -0700133ValueMetricProducer::~ValueMetricProducer() {
Yao Chen93fe3a32017-11-02 13:52:59 -0700134 VLOG("~ValueMetricProducer() called");
Chenjie Yu6736c892017-11-09 10:50:09 -0800135 if (mPullTagId != -1) {
136 mStatsPullerManager->UnRegisterReceiver(mPullTagId, this);
137 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700138}
139
Yao Chen427d3722018-03-22 15:21:52 -0700140void ValueMetricProducer::onSlicedConditionMayChangeLocked(bool overallCondition,
Yangster-macb142cc82018-03-30 15:22:08 -0700141 const int64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800142 VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700143}
144
Yangster-macb142cc82018-03-30 15:22:08 -0700145void ValueMetricProducer::dropDataLocked(const int64_t dropTimeNs) {
Yao Chen06dba5d2018-01-26 13:38:16 -0800146 flushIfNeededLocked(dropTimeNs);
147 mPastBuckets.clear();
148}
149
Yangster-macb142cc82018-03-30 15:22:08 -0700150void ValueMetricProducer::onDumpReportLocked(const int64_t dumpTimeNs,
Yangster-mace68f3a52018-04-04 00:01:43 -0700151 const bool include_current_partial_bucket,
Yao Chen288c6002017-12-12 13:43:18 -0800152 ProtoOutputStream* protoOutput) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800153 VLOG("metric %lld dump report now...", (long long)mMetricId);
Yangster-mace68f3a52018-04-04 00:01:43 -0700154 if (include_current_partial_bucket) {
155 flushLocked(dumpTimeNs);
156 } else {
157 flushIfNeededLocked(dumpTimeNs);
158 }
Yangster-mac635b4b32018-01-23 20:17:35 -0800159 if (mPastBuckets.empty()) {
160 return;
161 }
Yangster-mac94e197c2018-01-02 16:03:03 -0800162 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
Yi Jin5ee07872018-03-05 18:18:27 -0800163 uint64_t protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_VALUE_METRICS);
Yao Chen6a8c7992017-11-29 20:02:07 +0000164
Yao Chen93fe3a32017-11-02 13:52:59 -0700165 for (const auto& pair : mPastBuckets) {
Yangster-mac93694462018-01-22 20:49:31 -0800166 const MetricDimensionKey& dimensionKey = pair.first;
Yangster13fb7e42018-03-07 17:30:49 -0800167 VLOG(" dimension key %s", dimensionKey.toString().c_str());
Yi Jin5ee07872018-03-05 18:18:27 -0800168 uint64_t wrapperToken =
Yao Chen288c6002017-12-12 13:43:18 -0800169 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
Chenjie Yub3dda412017-10-24 13:41:59 -0700170
Yangster-mac20877162017-12-22 17:19:39 -0800171 // First fill dimension.
Yi Jin5ee07872018-03-05 18:18:27 -0800172 uint64_t dimensionToken = protoOutput->start(
Yangster-mac468ff042018-01-17 12:26:34 -0800173 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800174 writeDimensionToProto(dimensionKey.getDimensionKeyInWhat(), protoOutput);
Yangster-mac20877162017-12-22 17:19:39 -0800175 protoOutput->end(dimensionToken);
Yangster-mac93694462018-01-22 20:49:31 -0800176 if (dimensionKey.hasDimensionKeyInCondition()) {
Yi Jin5ee07872018-03-05 18:18:27 -0800177 uint64_t dimensionInConditionToken = protoOutput->start(
Yangster-mac93694462018-01-22 20:49:31 -0800178 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800179 writeDimensionToProto(dimensionKey.getDimensionKeyInCondition(), protoOutput);
Yangster-mac93694462018-01-22 20:49:31 -0800180 protoOutput->end(dimensionInConditionToken);
181 }
yro2b0f8862017-11-06 14:27:31 -0800182
183 // Then fill bucket_info (ValueBucketInfo).
184 for (const auto& bucket : pair.second) {
Yi Jin5ee07872018-03-05 18:18:27 -0800185 uint64_t bucketInfoToken = protoOutput->start(
Yao Chen288c6002017-12-12 13:43:18 -0800186 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
187 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS,
188 (long long)bucket.mBucketStartNs);
189 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS,
190 (long long)bucket.mBucketEndNs);
191 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE, (long long)bucket.mValue);
192 protoOutput->end(bucketInfoToken);
yro2b0f8862017-11-06 14:27:31 -0800193 VLOG("\t bucket [%lld - %lld] count: %lld", (long long)bucket.mBucketStartNs,
194 (long long)bucket.mBucketEndNs, (long long)bucket.mValue);
195 }
Yao Chen288c6002017-12-12 13:43:18 -0800196 protoOutput->end(wrapperToken);
Chenjie Yub3dda412017-10-24 13:41:59 -0700197 }
Yao Chen288c6002017-12-12 13:43:18 -0800198 protoOutput->end(protoToken);
yro2b0f8862017-11-06 14:27:31 -0800199
Yangster-mac94e197c2018-01-02 16:03:03 -0800200 VLOG("metric %lld dump report now...", (long long)mMetricId);
Yao Chen6a8c7992017-11-29 20:02:07 +0000201 mPastBuckets.clear();
Chenjie Yub3dda412017-10-24 13:41:59 -0700202}
203
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700204void ValueMetricProducer::onConditionChangedLocked(const bool condition,
Yangster-macb142cc82018-03-30 15:22:08 -0700205 const int64_t eventTimeNs) {
Yao Chen6a8c7992017-11-29 20:02:07 +0000206 mCondition = condition;
Chenjie Yub3dda412017-10-24 13:41:59 -0700207
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700208 if (eventTimeNs < mCurrentBucketStartTimeNs) {
209 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800210 (long long)mCurrentBucketStartTimeNs);
Yao Chen2794da22017-12-13 16:01:55 -0800211 return;
212 }
213
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700214 flushIfNeededLocked(eventTimeNs);
Chenjie Yua7259ab2017-12-10 08:31:05 -0800215
Yao Chen6a8c7992017-11-29 20:02:07 +0000216 if (mPullTagId != -1) {
Yao Chen6a8c7992017-11-29 20:02:07 +0000217 vector<shared_ptr<LogEvent>> allData;
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700218 if (mStatsPullerManager->Pull(mPullTagId, eventTimeNs, &allData)) {
Yao Chen6a8c7992017-11-29 20:02:07 +0000219 if (allData.size() == 0) {
220 return;
221 }
222 for (const auto& data : allData) {
Chenjie Yua7259ab2017-12-10 08:31:05 -0800223 onMatchedLogEventLocked(0, *data);
Yao Chen6a8c7992017-11-29 20:02:07 +0000224 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000225 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700226 return;
Chenjie Yub3dda412017-10-24 13:41:59 -0700227 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700228}
229
230void ValueMetricProducer::onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800231 std::lock_guard<std::mutex> lock(mMutex);
232
Yao Chenf09569f2017-12-13 17:00:51 -0800233 if (mCondition == true || mConditionTrackerIndex < 0) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700234 if (allData.size() == 0) {
235 return;
236 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800237 // For scheduled pulled data, the effective event time is snap to the nearest
238 // bucket boundary to make bucket finalize.
Yangster-macb142cc82018-03-30 15:22:08 -0700239 int64_t realEventTime = allData.at(0)->GetElapsedTimestampNs();
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700240 int64_t eventTime = mTimeBaseNs +
241 ((realEventTime - mTimeBaseNs) / mBucketSizeNs) * mBucketSizeNs;
Chenjie Yua7259ab2017-12-10 08:31:05 -0800242
243 mCondition = false;
Chenjie Yub3dda412017-10-24 13:41:59 -0700244 for (const auto& data : allData) {
Yangster-mac330af582018-02-08 15:24:38 -0800245 data->setElapsedTimestampNs(eventTime - 1);
Chenjie Yua7259ab2017-12-10 08:31:05 -0800246 onMatchedLogEventLocked(0, *data);
Chenjie Yub3dda412017-10-24 13:41:59 -0700247 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800248
249 mCondition = true;
250 for (const auto& data : allData) {
Yangster-mac330af582018-02-08 15:24:38 -0800251 data->setElapsedTimestampNs(eventTime);
Chenjie Yua7259ab2017-12-10 08:31:05 -0800252 onMatchedLogEventLocked(0, *data);
253 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700254 }
255}
256
Yangster-maca78d0082018-03-12 12:02:56 -0700257void ValueMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const {
258 if (mCurrentSlicedBucket.size() == 0) {
259 return;
260 }
261
262 fprintf(out, "ValueMetric %lld dimension size %lu\n", (long long)mMetricId,
263 (unsigned long)mCurrentSlicedBucket.size());
264 if (verbose) {
265 for (const auto& it : mCurrentSlicedBucket) {
266 fprintf(out, "\t(what)%s\t(condition)%s (value)%lld\n",
267 it.first.getDimensionKeyInWhat().toString().c_str(),
268 it.first.getDimensionKeyInCondition().toString().c_str(),
269 (unsigned long long)it.second.sum);
270 }
271 }
272}
273
Yangster-mac93694462018-01-22 20:49:31 -0800274bool ValueMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) {
Yao Chenb3561512017-11-21 18:07:17 -0800275 // ===========GuardRail==============
276 // 1. Report the tuple count if the tuple count > soft limit
277 if (mCurrentSlicedBucket.find(newKey) != mCurrentSlicedBucket.end()) {
278 return false;
279 }
Chenjie Yuc5875052018-03-09 10:13:11 -0800280 if (mCurrentSlicedBucket.size() > mDimensionSoftLimit - 1) {
Yao Chenb3561512017-11-21 18:07:17 -0800281 size_t newTupleCount = mCurrentSlicedBucket.size() + 1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800282 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount);
Yao Chenb3561512017-11-21 18:07:17 -0800283 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
Chenjie Yuc5875052018-03-09 10:13:11 -0800284 if (newTupleCount > mDimensionHardLimit) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800285 ALOGE("ValueMetric %lld dropping data for dimension key %s",
Yangster13fb7e42018-03-07 17:30:49 -0800286 (long long)mMetricId, newKey.toString().c_str());
Yao Chenb3561512017-11-21 18:07:17 -0800287 return true;
288 }
289 }
290
291 return false;
292}
293
Yangsterf2bee6f2017-11-29 12:01:05 -0800294void ValueMetricProducer::onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -0800295 const size_t matcherIndex, const MetricDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -0800296 const ConditionKey& conditionKey, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800297 const LogEvent& event) {
Yangster-macb142cc82018-03-30 15:22:08 -0700298 int64_t eventTimeNs = event.GetElapsedTimestampNs();
Yao Chen6a8c7992017-11-29 20:02:07 +0000299 if (eventTimeNs < mCurrentBucketStartTimeNs) {
300 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs,
301 (long long)mCurrentBucketStartTimeNs);
302 return;
303 }
304
Chenjie Yua7259ab2017-12-10 08:31:05 -0800305 flushIfNeededLocked(eventTimeNs);
306
Yangsterf2bee6f2017-11-29 12:01:05 -0800307 if (hitGuardRailLocked(eventKey)) {
Yangster8de69392017-11-27 13:48:29 -0800308 return;
309 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000310 Interval& interval = mCurrentSlicedBucket[eventKey];
311
Yao Chen8a8d16c2018-02-08 14:50:40 -0800312 int error = 0;
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700313 const int64_t value = event.GetLong(mField, &error);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800314 if (error < 0) {
Yangster-maca7fb12d2018-01-03 17:17:20 -0800315 return;
316 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000317
Chenjie Yua7259ab2017-12-10 08:31:05 -0800318 if (mPullTagId != -1) { // for pulled events
319 if (mCondition == true) {
Chenjie Yu6d370f42018-03-25 14:57:30 -0700320 if (!interval.startUpdated) {
321 interval.start = value;
322 interval.startUpdated = true;
323 } else {
324 // skip it if there is already value recorded for the start
325 VLOG("Already recorded value for this dimension %s", eventKey.toString().c_str());
326 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000327 } else {
Chenjie Yu0ed268b2018-01-10 09:37:10 -0800328 // Generally we expect value to be monotonically increasing.
329 // If not, there was a reset event. We take the absolute value as
330 // diff in this case.
Chenjie Yua7259ab2017-12-10 08:31:05 -0800331 if (interval.startUpdated) {
Chenjie Yu0ed268b2018-01-10 09:37:10 -0800332 if (value > interval.start) {
333 interval.sum += (value - interval.start);
334 } else {
335 interval.sum += value;
336 }
Chenjie Yuae63b0a2018-04-10 14:59:31 -0700337 interval.hasValue = true;
Chenjie Yua7259ab2017-12-10 08:31:05 -0800338 interval.startUpdated = false;
Yao Chen6a8c7992017-11-29 20:02:07 +0000339 } else {
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700340 VLOG("No start for matching end %lld", (long long)value);
Chenjie Yua7259ab2017-12-10 08:31:05 -0800341 interval.tainted += 1;
Yao Chen6a8c7992017-11-29 20:02:07 +0000342 }
343 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800344 } else { // for pushed events
345 interval.sum += value;
Chenjie Yuae63b0a2018-04-10 14:59:31 -0700346 interval.hasValue = true;
Yangster8de69392017-11-27 13:48:29 -0800347 }
Bookatzde1b55622017-12-14 18:38:27 -0800348
David Chen27785a82018-01-19 17:06:45 -0800349 long wholeBucketVal = interval.sum;
350 auto prev = mCurrentFullBucket.find(eventKey);
351 if (prev != mCurrentFullBucket.end()) {
352 wholeBucketVal += prev->second;
353 }
Bookatzde1b55622017-12-14 18:38:27 -0800354 for (auto& tracker : mAnomalyTrackers) {
David Chen27785a82018-01-19 17:06:45 -0800355 tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, eventKey, wholeBucketVal);
Bookatzde1b55622017-12-14 18:38:27 -0800356 }
Yangster8de69392017-11-27 13:48:29 -0800357}
358
Yangster-macb142cc82018-03-30 15:22:08 -0700359void ValueMetricProducer::flushIfNeededLocked(const int64_t& eventTimeNs) {
360 int64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs();
David Chen27785a82018-01-19 17:06:45 -0800361
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700362 if (eventTimeNs < currentBucketEndTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700363 VLOG("eventTime is %lld, less than next bucket start time %lld", (long long)eventTimeNs,
David Chen27785a82018-01-19 17:06:45 -0800364 (long long)(currentBucketEndTimeNs));
Chenjie Yub3dda412017-10-24 13:41:59 -0700365 return;
366 }
David Chen27785a82018-01-19 17:06:45 -0800367
368 flushCurrentBucketLocked(eventTimeNs);
369
370 int64_t numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs;
371 mCurrentBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs;
372 mCurrentBucketNum += numBucketsForward;
373
374 if (numBucketsForward > 1) {
375 VLOG("Skipping forward %lld buckets", (long long)numBucketsForward);
376 }
377 VLOG("metric %lld: new bucket start time: %lld", (long long)mMetricId,
378 (long long)mCurrentBucketStartTimeNs);
379}
380
Yangster-macb142cc82018-03-30 15:22:08 -0700381void ValueMetricProducer::flushCurrentBucketLocked(const int64_t& eventTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700382 VLOG("finalizing bucket for %ld, dumping %d slices", (long)mCurrentBucketStartTimeNs,
383 (int)mCurrentSlicedBucket.size());
Yangster-macb142cc82018-03-30 15:22:08 -0700384 int64_t fullBucketEndTimeNs = getCurrentBucketEndTimeNs();
David Chen27785a82018-01-19 17:06:45 -0800385
yro2b0f8862017-11-06 14:27:31 -0800386 ValueBucket info;
387 info.mBucketStartNs = mCurrentBucketStartTimeNs;
David Chen27785a82018-01-19 17:06:45 -0800388 if (eventTimeNs < fullBucketEndTimeNs) {
389 info.mBucketEndNs = eventTimeNs;
390 } else {
391 info.mBucketEndNs = fullBucketEndTimeNs;
392 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700393
Chenjie Yu6736c892017-11-09 10:50:09 -0800394 int tainted = 0;
Chenjie Yub3dda412017-10-24 13:41:59 -0700395 for (const auto& slice : mCurrentSlicedBucket) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800396 tainted += slice.second.tainted;
Chenjie Yu6d370f42018-03-25 14:57:30 -0700397 tainted += slice.second.startUpdated;
Chenjie Yuae63b0a2018-04-10 14:59:31 -0700398 if (slice.second.hasValue) {
399 info.mValue = slice.second.sum;
400 // it will auto create new vector of ValuebucketInfo if the key is not found.
401 auto& bucketList = mPastBuckets[slice.first];
402 bucketList.push_back(info);
403 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700404 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800405 VLOG("%d tainted pairs in the bucket", tainted);
Chenjie Yub3dda412017-10-24 13:41:59 -0700406
David Chen27785a82018-01-19 17:06:45 -0800407 if (eventTimeNs > fullBucketEndTimeNs) { // If full bucket, send to anomaly tracker.
408 // Accumulate partial buckets with current value and then send to anomaly tracker.
409 if (mCurrentFullBucket.size() > 0) {
410 for (const auto& slice : mCurrentSlicedBucket) {
411 mCurrentFullBucket[slice.first] += slice.second.sum;
412 }
413 for (const auto& slice : mCurrentFullBucket) {
414 for (auto& tracker : mAnomalyTrackers) {
415 if (tracker != nullptr) {
416 tracker->addPastBucket(slice.first, slice.second, mCurrentBucketNum);
417 }
418 }
419 }
420 mCurrentFullBucket.clear();
421 } else {
422 // Skip aggregating the partial buckets since there's no previous partial bucket.
423 for (const auto& slice : mCurrentSlicedBucket) {
424 for (auto& tracker : mAnomalyTrackers) {
425 if (tracker != nullptr) {
426 tracker->addPastBucket(slice.first, slice.second.sum, mCurrentBucketNum);
427 }
428 }
429 }
430 }
431 } else {
432 // Accumulate partial bucket.
433 for (const auto& slice : mCurrentSlicedBucket) {
434 mCurrentFullBucket[slice.first] += slice.second.sum;
435 }
436 }
437
Chenjie Yub3dda412017-10-24 13:41:59 -0700438 // Reset counters
Chenjie Yua7259ab2017-12-10 08:31:05 -0800439 mCurrentSlicedBucket.clear();
Chenjie Yub3dda412017-10-24 13:41:59 -0700440}
441
Yangsterf2bee6f2017-11-29 12:01:05 -0800442size_t ValueMetricProducer::byteSizeLocked() const {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800443 size_t totalSize = 0;
444 for (const auto& pair : mPastBuckets) {
445 totalSize += pair.second.size() * kBucketSize;
446 }
447 return totalSize;
yro2b0f8862017-11-06 14:27:31 -0800448}
449
Chenjie Yub3dda412017-10-24 13:41:59 -0700450} // namespace statsd
451} // namespace os
Yao Chen93fe3a32017-11-02 13:52:59 -0700452} // namespace android