blob: ef8b6cc83a26c873c5555e455b771d2e1c0a9951 [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;
Yangster-mac9def8e32018-04-17 13:55:51 -070051const int FIELD_ID_TIME_BASE = 9;
52const int FIELD_ID_BUCKET_SIZE = 10;
53const int FIELD_ID_DIMENSION_PATH_IN_WHAT = 11;
54const int FIELD_ID_DIMENSION_PATH_IN_CONDITION = 12;
yro2b0f8862017-11-06 14:27:31 -080055// for ValueMetricDataWrapper
56const int FIELD_ID_DATA = 1;
David Chen81245fd2018-04-12 14:33:37 -070057const int FIELD_ID_SKIPPED = 2;
Yangster-mac9def8e32018-04-17 13:55:51 -070058const int FIELD_ID_SKIPPED_START_MILLIS = 3;
59const int FIELD_ID_SKIPPED_END_MILLIS = 4;
yro2b0f8862017-11-06 14:27:31 -080060// for ValueMetricData
Yangster-mac468ff042018-01-17 12:26:34 -080061const int FIELD_ID_DIMENSION_IN_WHAT = 1;
62const int FIELD_ID_DIMENSION_IN_CONDITION = 2;
63const int FIELD_ID_BUCKET_INFO = 3;
Yangster-mac9def8e32018-04-17 13:55:51 -070064const int FIELD_ID_DIMENSION_LEAF_IN_WHAT = 4;
65const int FIELD_ID_DIMENSION_LEAF_IN_CONDITION = 5;
yro2b0f8862017-11-06 14:27:31 -080066// for ValueBucketInfo
yro2b0f8862017-11-06 14:27:31 -080067const int FIELD_ID_VALUE = 3;
Yangster-mac9def8e32018-04-17 13:55:51 -070068const int FIELD_ID_BUCKET_NUM = 4;
69const int FIELD_ID_START_BUCKET_ELAPSED_MILLIS = 5;
70const int FIELD_ID_END_BUCKET_ELAPSED_MILLIS = 6;
yro2b0f8862017-11-06 14:27:31 -080071
Chenjie Yub3dda412017-10-24 13:41:59 -070072// ValueMetric has a minimum bucket size of 10min so that we don't pull too frequently
Yao Chenb3561512017-11-21 18:07:17 -080073ValueMetricProducer::ValueMetricProducer(const ConfigKey& key, const ValueMetric& metric,
74 const int conditionIndex,
Yao Chen93fe3a32017-11-02 13:52:59 -070075 const sp<ConditionWizard>& wizard, const int pullTagId,
Yangster-mac15f6bbc2018-04-08 11:52:26 -070076 const int64_t timeBaseNs, const int64_t startTimestampNs,
Chenjie Yue2219202018-06-08 10:07:51 -070077 const sp<StatsPullerManager>& pullerManager)
Yangster-mac15f6bbc2018-04-08 11:52:26 -070078 : MetricProducer(metric.id(), key, timeBaseNs, conditionIndex, wizard),
Chenjie Yue2219202018-06-08 10:07:51 -070079 mPullerManager(pullerManager),
Yao Chenf09569f2017-12-13 17:00:51 -080080 mValueField(metric.value_field()),
Chenjie Yuc5875052018-03-09 10:13:11 -080081 mPullTagId(pullTagId),
David Chen81245fd2018-04-12 14:33:37 -070082 mMinBucketSizeNs(metric.min_bucket_size_nanos()),
Chenjie Yuc5875052018-03-09 10:13:11 -080083 mDimensionSoftLimit(StatsdStats::kAtomDimensionKeySizeLimitMap.find(pullTagId) !=
84 StatsdStats::kAtomDimensionKeySizeLimitMap.end()
85 ? StatsdStats::kAtomDimensionKeySizeLimitMap.at(pullTagId).first
86 : StatsdStats::kDimensionKeySizeSoftLimit),
87 mDimensionHardLimit(StatsdStats::kAtomDimensionKeySizeLimitMap.find(pullTagId) !=
88 StatsdStats::kAtomDimensionKeySizeLimitMap.end()
89 ? StatsdStats::kAtomDimensionKeySizeLimitMap.at(pullTagId).second
Chenjie Yu47234642018-05-14 10:14:16 -070090 : StatsdStats::kDimensionKeySizeHardLimit),
91 mUseAbsoluteValueOnReset(metric.use_absolute_value_on_reset()) {
Yao Chen93fe3a32017-11-02 13:52:59 -070092 // TODO: valuemetric for pushed events may need unlimited bucket length
Yangster-macb8144812018-01-04 10:56:23 -080093 int64_t bucketSizeMills = 0;
94 if (metric.has_bucket()) {
yro59cc24d2018-02-13 20:17:32 -080095 bucketSizeMills = TimeUnitToBucketSizeInMillisGuardrailed(key.GetUid(), metric.bucket());
Chenjie Yu6736c892017-11-09 10:50:09 -080096 } else {
Yangster-macb8144812018-01-04 10:56:23 -080097 bucketSizeMills = TimeUnitToBucketSizeInMillis(ONE_HOUR);
Chenjie Yu6736c892017-11-09 10:50:09 -080098 }
Chenjie Yub3dda412017-10-24 13:41:59 -070099
Yangster-macb8144812018-01-04 10:56:23 -0800100 mBucketSizeNs = bucketSizeMills * 1000000;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800101 if (metric.has_dimensions_in_what()) {
102 translateFieldMatcher(metric.dimensions_in_what(), &mDimensionsInWhat);
Yangster13fb7e42018-03-07 17:30:49 -0800103 mContainANYPositionInDimensionsInWhat = HasPositionANY(metric.dimensions_in_what());
Yao Chen8a8d16c2018-02-08 14:50:40 -0800104 }
105
106 if (metric.has_dimensions_in_condition()) {
107 translateFieldMatcher(metric.dimensions_in_condition(), &mDimensionsInCondition);
108 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700109
Yao Chen93fe3a32017-11-02 13:52:59 -0700110 if (metric.links().size() > 0) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800111 for (const auto& link : metric.links()) {
112 Metric2Condition mc;
113 mc.conditionId = link.condition();
114 translateFieldMatcher(link.fields_in_what(), &mc.metricFields);
115 translateFieldMatcher(link.fields_in_condition(), &mc.conditionFields);
116 mMetric2ConditionLinks.push_back(mc);
117 }
Yao Chen93fe3a32017-11-02 13:52:59 -0700118 }
Yao Chen8a8d16c2018-02-08 14:50:40 -0800119
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700120 if (mValueField.child_size() > 0) {
Yao Chen8a8d16c2018-02-08 14:50:40 -0800121 mField = mValueField.child(0).field();
122 }
123 mConditionSliced = (metric.links().size() > 0) || (mDimensionsInCondition.size() > 0);
Yangster-mac9def8e32018-04-17 13:55:51 -0700124 mSliceByPositionALL = HasPositionALL(metric.dimensions_in_what()) ||
125 HasPositionALL(metric.dimensions_in_condition());
Chenjie Yub3dda412017-10-24 13:41:59 -0700126
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700127 // Kicks off the puller immediately.
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700128 flushIfNeededLocked(startTimestampNs);
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700129 if (mPullTagId != -1) {
Chenjie Yue2219202018-06-08 10:07:51 -0700130 mPullerManager->RegisterReceiver(mPullTagId, this,
131 mCurrentBucketStartTimeNs + mBucketSizeNs, mBucketSizeNs);
Yao Chen93fe3a32017-11-02 13:52:59 -0700132 }
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700133
Yangster-mac94e197c2018-01-02 16:03:03 -0800134 VLOG("value metric %lld created. bucket size %lld start_time: %lld",
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700135 (long long)metric.id(), (long long)mBucketSizeNs, (long long)mTimeBaseNs);
Chenjie Yub3dda412017-10-24 13:41:59 -0700136}
137
138ValueMetricProducer::~ValueMetricProducer() {
Yao Chen93fe3a32017-11-02 13:52:59 -0700139 VLOG("~ValueMetricProducer() called");
Chenjie Yu6736c892017-11-09 10:50:09 -0800140 if (mPullTagId != -1) {
Chenjie Yue2219202018-06-08 10:07:51 -0700141 mPullerManager->UnRegisterReceiver(mPullTagId, this);
Chenjie Yu6736c892017-11-09 10:50:09 -0800142 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700143}
144
Yao Chen427d3722018-03-22 15:21:52 -0700145void ValueMetricProducer::onSlicedConditionMayChangeLocked(bool overallCondition,
Yangster-macb142cc82018-03-30 15:22:08 -0700146 const int64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800147 VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700148}
149
Yangster-macb142cc82018-03-30 15:22:08 -0700150void ValueMetricProducer::dropDataLocked(const int64_t dropTimeNs) {
Yao Chen06dba5d2018-01-26 13:38:16 -0800151 flushIfNeededLocked(dropTimeNs);
152 mPastBuckets.clear();
153}
154
Yangster-maca802d732018-04-24 07:50:38 -0700155void ValueMetricProducer::clearPastBucketsLocked(const int64_t dumpTimeNs) {
156 flushIfNeededLocked(dumpTimeNs);
157 mPastBuckets.clear();
158 mSkippedBuckets.clear();
159}
160
Yangster-macb142cc82018-03-30 15:22:08 -0700161void ValueMetricProducer::onDumpReportLocked(const int64_t dumpTimeNs,
Yangster-mace68f3a52018-04-04 00:01:43 -0700162 const bool include_current_partial_bucket,
Yangster-mac9def8e32018-04-17 13:55:51 -0700163 std::set<string> *str_set,
Yao Chen288c6002017-12-12 13:43:18 -0800164 ProtoOutputStream* protoOutput) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800165 VLOG("metric %lld dump report now...", (long long)mMetricId);
Yangster-mace68f3a52018-04-04 00:01:43 -0700166 if (include_current_partial_bucket) {
167 flushLocked(dumpTimeNs);
168 } else {
169 flushIfNeededLocked(dumpTimeNs);
170 }
David Chen81245fd2018-04-12 14:33:37 -0700171 if (mPastBuckets.empty() && mSkippedBuckets.empty()) {
Yangster-mac635b4b32018-01-23 20:17:35 -0800172 return;
173 }
Yangster-mac94e197c2018-01-02 16:03:03 -0800174 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
Yangster-mac9def8e32018-04-17 13:55:51 -0700175 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_TIME_BASE, (long long)mTimeBaseNs);
176 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_SIZE, (long long)mBucketSizeNs);
177 // Fills the dimension path if not slicing by ALL.
178 if (!mSliceByPositionALL) {
179 if (!mDimensionsInWhat.empty()) {
180 uint64_t dimenPathToken = protoOutput->start(
181 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_PATH_IN_WHAT);
182 writeDimensionPathToProto(mDimensionsInWhat, protoOutput);
183 protoOutput->end(dimenPathToken);
184 }
185 if (!mDimensionsInCondition.empty()) {
186 uint64_t dimenPathToken = protoOutput->start(
187 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_PATH_IN_CONDITION);
188 writeDimensionPathToProto(mDimensionsInCondition, protoOutput);
189 protoOutput->end(dimenPathToken);
190 }
191 }
192
Yi Jin5ee07872018-03-05 18:18:27 -0800193 uint64_t protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_VALUE_METRICS);
Yao Chen6a8c7992017-11-29 20:02:07 +0000194
David Chen81245fd2018-04-12 14:33:37 -0700195 for (const auto& pair : mSkippedBuckets) {
196 uint64_t wrapperToken =
197 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_SKIPPED);
Yangster-mac9def8e32018-04-17 13:55:51 -0700198 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_SKIPPED_START_MILLIS,
199 (long long)(NanoToMillis(pair.first)));
200 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_SKIPPED_END_MILLIS,
201 (long long)(NanoToMillis(pair.second)));
David Chen81245fd2018-04-12 14:33:37 -0700202 protoOutput->end(wrapperToken);
203 }
204 mSkippedBuckets.clear();
205
Yao Chen93fe3a32017-11-02 13:52:59 -0700206 for (const auto& pair : mPastBuckets) {
Yangster-mac93694462018-01-22 20:49:31 -0800207 const MetricDimensionKey& dimensionKey = pair.first;
Yangster13fb7e42018-03-07 17:30:49 -0800208 VLOG(" dimension key %s", dimensionKey.toString().c_str());
Yi Jin5ee07872018-03-05 18:18:27 -0800209 uint64_t wrapperToken =
Yao Chen288c6002017-12-12 13:43:18 -0800210 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
Chenjie Yub3dda412017-10-24 13:41:59 -0700211
Yangster-mac20877162017-12-22 17:19:39 -0800212 // First fill dimension.
Yangster-mac9def8e32018-04-17 13:55:51 -0700213 if (mSliceByPositionALL) {
214 uint64_t dimensionToken = protoOutput->start(
215 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT);
216 writeDimensionToProto(dimensionKey.getDimensionKeyInWhat(), str_set, protoOutput);
217 protoOutput->end(dimensionToken);
218 if (dimensionKey.hasDimensionKeyInCondition()) {
219 uint64_t dimensionInConditionToken = protoOutput->start(
220 FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION);
221 writeDimensionToProto(dimensionKey.getDimensionKeyInCondition(),
222 str_set, protoOutput);
223 protoOutput->end(dimensionInConditionToken);
224 }
225 } else {
226 writeDimensionLeafNodesToProto(dimensionKey.getDimensionKeyInWhat(),
227 FIELD_ID_DIMENSION_LEAF_IN_WHAT, str_set, protoOutput);
228 if (dimensionKey.hasDimensionKeyInCondition()) {
229 writeDimensionLeafNodesToProto(dimensionKey.getDimensionKeyInCondition(),
230 FIELD_ID_DIMENSION_LEAF_IN_CONDITION,
231 str_set, protoOutput);
232 }
Yangster-mac93694462018-01-22 20:49:31 -0800233 }
yro2b0f8862017-11-06 14:27:31 -0800234
235 // Then fill bucket_info (ValueBucketInfo).
236 for (const auto& bucket : pair.second) {
Yi Jin5ee07872018-03-05 18:18:27 -0800237 uint64_t bucketInfoToken = protoOutput->start(
Yao Chen288c6002017-12-12 13:43:18 -0800238 FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
Yangster-mac9def8e32018-04-17 13:55:51 -0700239
240 if (bucket.mBucketEndNs - bucket.mBucketStartNs != mBucketSizeNs) {
241 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_ELAPSED_MILLIS,
242 (long long)NanoToMillis(bucket.mBucketStartNs));
243 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_ELAPSED_MILLIS,
244 (long long)NanoToMillis(bucket.mBucketEndNs));
245 } else {
246 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_NUM,
247 (long long)(getBucketNumFromEndTimeNs(bucket.mBucketEndNs)));
248 }
249
Yao Chen288c6002017-12-12 13:43:18 -0800250 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE, (long long)bucket.mValue);
251 protoOutput->end(bucketInfoToken);
yro2b0f8862017-11-06 14:27:31 -0800252 VLOG("\t bucket [%lld - %lld] count: %lld", (long long)bucket.mBucketStartNs,
253 (long long)bucket.mBucketEndNs, (long long)bucket.mValue);
254 }
Yao Chen288c6002017-12-12 13:43:18 -0800255 protoOutput->end(wrapperToken);
Chenjie Yub3dda412017-10-24 13:41:59 -0700256 }
Yao Chen288c6002017-12-12 13:43:18 -0800257 protoOutput->end(protoToken);
yro2b0f8862017-11-06 14:27:31 -0800258
Yangster-mac94e197c2018-01-02 16:03:03 -0800259 VLOG("metric %lld dump report now...", (long long)mMetricId);
Yao Chen6a8c7992017-11-29 20:02:07 +0000260 mPastBuckets.clear();
Chenjie Yub3dda412017-10-24 13:41:59 -0700261}
262
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700263void ValueMetricProducer::onConditionChangedLocked(const bool condition,
Yangster-macb142cc82018-03-30 15:22:08 -0700264 const int64_t eventTimeNs) {
Yao Chen6a8c7992017-11-29 20:02:07 +0000265 mCondition = condition;
Chenjie Yub3dda412017-10-24 13:41:59 -0700266
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700267 if (eventTimeNs < mCurrentBucketStartTimeNs) {
268 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800269 (long long)mCurrentBucketStartTimeNs);
Yao Chen2794da22017-12-13 16:01:55 -0800270 return;
271 }
272
Chenjie Yu1a0a9412018-03-28 10:07:22 -0700273 flushIfNeededLocked(eventTimeNs);
Chenjie Yua7259ab2017-12-10 08:31:05 -0800274
Yao Chen6a8c7992017-11-29 20:02:07 +0000275 if (mPullTagId != -1) {
Yao Chen6a8c7992017-11-29 20:02:07 +0000276 vector<shared_ptr<LogEvent>> allData;
Chenjie Yue2219202018-06-08 10:07:51 -0700277 if (mPullerManager->Pull(mPullTagId, eventTimeNs, &allData)) {
Yao Chen6a8c7992017-11-29 20:02:07 +0000278 if (allData.size() == 0) {
279 return;
280 }
281 for (const auto& data : allData) {
Chenjie Yua7259ab2017-12-10 08:31:05 -0800282 onMatchedLogEventLocked(0, *data);
Yao Chen6a8c7992017-11-29 20:02:07 +0000283 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000284 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700285 return;
Chenjie Yub3dda412017-10-24 13:41:59 -0700286 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700287}
288
289void ValueMetricProducer::onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData) {
Yangsterf2bee6f2017-11-29 12:01:05 -0800290 std::lock_guard<std::mutex> lock(mMutex);
291
Yao Chenf09569f2017-12-13 17:00:51 -0800292 if (mCondition == true || mConditionTrackerIndex < 0) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700293 if (allData.size() == 0) {
294 return;
295 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800296 // For scheduled pulled data, the effective event time is snap to the nearest
297 // bucket boundary to make bucket finalize.
Yangster-macb142cc82018-03-30 15:22:08 -0700298 int64_t realEventTime = allData.at(0)->GetElapsedTimestampNs();
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700299 int64_t eventTime = mTimeBaseNs +
300 ((realEventTime - mTimeBaseNs) / mBucketSizeNs) * mBucketSizeNs;
Chenjie Yua7259ab2017-12-10 08:31:05 -0800301
302 mCondition = false;
Chenjie Yub3dda412017-10-24 13:41:59 -0700303 for (const auto& data : allData) {
Yangster-mac330af582018-02-08 15:24:38 -0800304 data->setElapsedTimestampNs(eventTime - 1);
Chenjie Yua7259ab2017-12-10 08:31:05 -0800305 onMatchedLogEventLocked(0, *data);
Chenjie Yub3dda412017-10-24 13:41:59 -0700306 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800307
308 mCondition = true;
309 for (const auto& data : allData) {
Yangster-mac330af582018-02-08 15:24:38 -0800310 data->setElapsedTimestampNs(eventTime);
Chenjie Yua7259ab2017-12-10 08:31:05 -0800311 onMatchedLogEventLocked(0, *data);
312 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700313 }
314}
315
Yangster-maca78d0082018-03-12 12:02:56 -0700316void ValueMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const {
317 if (mCurrentSlicedBucket.size() == 0) {
318 return;
319 }
320
321 fprintf(out, "ValueMetric %lld dimension size %lu\n", (long long)mMetricId,
322 (unsigned long)mCurrentSlicedBucket.size());
323 if (verbose) {
324 for (const auto& it : mCurrentSlicedBucket) {
325 fprintf(out, "\t(what)%s\t(condition)%s (value)%lld\n",
326 it.first.getDimensionKeyInWhat().toString().c_str(),
327 it.first.getDimensionKeyInCondition().toString().c_str(),
328 (unsigned long long)it.second.sum);
329 }
330 }
331}
332
Yangster-mac93694462018-01-22 20:49:31 -0800333bool ValueMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) {
Yao Chenb3561512017-11-21 18:07:17 -0800334 // ===========GuardRail==============
335 // 1. Report the tuple count if the tuple count > soft limit
336 if (mCurrentSlicedBucket.find(newKey) != mCurrentSlicedBucket.end()) {
337 return false;
338 }
Chenjie Yuc5875052018-03-09 10:13:11 -0800339 if (mCurrentSlicedBucket.size() > mDimensionSoftLimit - 1) {
Yao Chenb3561512017-11-21 18:07:17 -0800340 size_t newTupleCount = mCurrentSlicedBucket.size() + 1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800341 StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount);
Yao Chenb3561512017-11-21 18:07:17 -0800342 // 2. Don't add more tuples, we are above the allowed threshold. Drop the data.
Chenjie Yuc5875052018-03-09 10:13:11 -0800343 if (newTupleCount > mDimensionHardLimit) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800344 ALOGE("ValueMetric %lld dropping data for dimension key %s",
Yangster13fb7e42018-03-07 17:30:49 -0800345 (long long)mMetricId, newKey.toString().c_str());
Yao Chenb3561512017-11-21 18:07:17 -0800346 return true;
347 }
348 }
349
350 return false;
351}
352
Yangsterf2bee6f2017-11-29 12:01:05 -0800353void ValueMetricProducer::onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -0800354 const size_t matcherIndex, const MetricDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -0800355 const ConditionKey& conditionKey, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800356 const LogEvent& event) {
Yangster-macb142cc82018-03-30 15:22:08 -0700357 int64_t eventTimeNs = event.GetElapsedTimestampNs();
Yao Chen6a8c7992017-11-29 20:02:07 +0000358 if (eventTimeNs < mCurrentBucketStartTimeNs) {
359 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs,
360 (long long)mCurrentBucketStartTimeNs);
361 return;
362 }
363
Chenjie Yua7259ab2017-12-10 08:31:05 -0800364 flushIfNeededLocked(eventTimeNs);
365
Yangsterf2bee6f2017-11-29 12:01:05 -0800366 if (hitGuardRailLocked(eventKey)) {
Yangster8de69392017-11-27 13:48:29 -0800367 return;
368 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000369 Interval& interval = mCurrentSlicedBucket[eventKey];
370
Yao Chen8a8d16c2018-02-08 14:50:40 -0800371 int error = 0;
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700372 const int64_t value = event.GetLong(mField, &error);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800373 if (error < 0) {
Yangster-maca7fb12d2018-01-03 17:17:20 -0800374 return;
375 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000376
Chenjie Yua7259ab2017-12-10 08:31:05 -0800377 if (mPullTagId != -1) { // for pulled events
378 if (mCondition == true) {
Chenjie Yu6d370f42018-03-25 14:57:30 -0700379 if (!interval.startUpdated) {
380 interval.start = value;
381 interval.startUpdated = true;
382 } else {
383 // skip it if there is already value recorded for the start
384 VLOG("Already recorded value for this dimension %s", eventKey.toString().c_str());
385 }
Yao Chen6a8c7992017-11-29 20:02:07 +0000386 } else {
Chenjie Yu0ed268b2018-01-10 09:37:10 -0800387 // Generally we expect value to be monotonically increasing.
Chenjie Yu47234642018-05-14 10:14:16 -0700388 // If not, take absolute value or drop it, based on config.
Chenjie Yua7259ab2017-12-10 08:31:05 -0800389 if (interval.startUpdated) {
Yangster613a7e22018-05-08 15:12:30 -0700390 if (value >= interval.start) {
Chenjie Yu0ed268b2018-01-10 09:37:10 -0800391 interval.sum += (value - interval.start);
Chenjie Yu47234642018-05-14 10:14:16 -0700392 interval.hasValue = true;
Chenjie Yu0ed268b2018-01-10 09:37:10 -0800393 } else {
Chenjie Yu47234642018-05-14 10:14:16 -0700394 if (mUseAbsoluteValueOnReset) {
395 interval.sum += value;
396 interval.hasValue = true;
397 } else {
398 VLOG("Dropping data for atom %d, prev: %lld, now: %lld", mPullTagId,
399 (long long)interval.start, (long long)value);
400 }
Chenjie Yu0ed268b2018-01-10 09:37:10 -0800401 }
Chenjie Yua7259ab2017-12-10 08:31:05 -0800402 interval.startUpdated = false;
Yao Chen6a8c7992017-11-29 20:02:07 +0000403 } else {
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700404 VLOG("No start for matching end %lld", (long long)value);
Chenjie Yua7259ab2017-12-10 08:31:05 -0800405 interval.tainted += 1;
Yao Chen6a8c7992017-11-29 20:02:07 +0000406 }
407 }
Chenjie Yu021e2532018-05-16 12:23:07 -0700408 } else { // for pushed events, only accumulate when condition is true
409 if (mCondition == true || mConditionTrackerIndex < 0) {
410 interval.sum += value;
411 interval.hasValue = true;
412 }
Yangster8de69392017-11-27 13:48:29 -0800413 }
Bookatzde1b55622017-12-14 18:38:27 -0800414
David Chen27785a82018-01-19 17:06:45 -0800415 long wholeBucketVal = interval.sum;
416 auto prev = mCurrentFullBucket.find(eventKey);
417 if (prev != mCurrentFullBucket.end()) {
418 wholeBucketVal += prev->second;
419 }
Bookatzde1b55622017-12-14 18:38:27 -0800420 for (auto& tracker : mAnomalyTrackers) {
David Chen27785a82018-01-19 17:06:45 -0800421 tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, eventKey, wholeBucketVal);
Bookatzde1b55622017-12-14 18:38:27 -0800422 }
Yangster8de69392017-11-27 13:48:29 -0800423}
424
Yangster-macb142cc82018-03-30 15:22:08 -0700425void ValueMetricProducer::flushIfNeededLocked(const int64_t& eventTimeNs) {
426 int64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs();
David Chen27785a82018-01-19 17:06:45 -0800427
Yangster-mac15f6bbc2018-04-08 11:52:26 -0700428 if (eventTimeNs < currentBucketEndTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700429 VLOG("eventTime is %lld, less than next bucket start time %lld", (long long)eventTimeNs,
David Chen27785a82018-01-19 17:06:45 -0800430 (long long)(currentBucketEndTimeNs));
Chenjie Yub3dda412017-10-24 13:41:59 -0700431 return;
432 }
David Chen27785a82018-01-19 17:06:45 -0800433
434 flushCurrentBucketLocked(eventTimeNs);
435
436 int64_t numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs;
437 mCurrentBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs;
438 mCurrentBucketNum += numBucketsForward;
439
440 if (numBucketsForward > 1) {
441 VLOG("Skipping forward %lld buckets", (long long)numBucketsForward);
442 }
443 VLOG("metric %lld: new bucket start time: %lld", (long long)mMetricId,
444 (long long)mCurrentBucketStartTimeNs);
445}
446
Yangster-macb142cc82018-03-30 15:22:08 -0700447void ValueMetricProducer::flushCurrentBucketLocked(const int64_t& eventTimeNs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700448 VLOG("finalizing bucket for %ld, dumping %d slices", (long)mCurrentBucketStartTimeNs,
449 (int)mCurrentSlicedBucket.size());
Yangster-macb142cc82018-03-30 15:22:08 -0700450 int64_t fullBucketEndTimeNs = getCurrentBucketEndTimeNs();
David Chen27785a82018-01-19 17:06:45 -0800451
yro2b0f8862017-11-06 14:27:31 -0800452 ValueBucket info;
453 info.mBucketStartNs = mCurrentBucketStartTimeNs;
David Chen27785a82018-01-19 17:06:45 -0800454 if (eventTimeNs < fullBucketEndTimeNs) {
455 info.mBucketEndNs = eventTimeNs;
456 } else {
457 info.mBucketEndNs = fullBucketEndTimeNs;
458 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700459
David Chen81245fd2018-04-12 14:33:37 -0700460 if (info.mBucketEndNs - mCurrentBucketStartTimeNs >= mMinBucketSizeNs) {
461 // The current bucket is large enough to keep.
462 int tainted = 0;
463 for (const auto& slice : mCurrentSlicedBucket) {
464 tainted += slice.second.tainted;
465 tainted += slice.second.startUpdated;
466 if (slice.second.hasValue) {
467 info.mValue = slice.second.sum;
468 // it will auto create new vector of ValuebucketInfo if the key is not found.
469 auto& bucketList = mPastBuckets[slice.first];
470 bucketList.push_back(info);
471 }
Chenjie Yuae63b0a2018-04-10 14:59:31 -0700472 }
David Chen81245fd2018-04-12 14:33:37 -0700473 VLOG("%d tainted pairs in the bucket", tainted);
474 } else {
475 mSkippedBuckets.emplace_back(info.mBucketStartNs, info.mBucketEndNs);
Chenjie Yub3dda412017-10-24 13:41:59 -0700476 }
477
David Chen27785a82018-01-19 17:06:45 -0800478 if (eventTimeNs > fullBucketEndTimeNs) { // If full bucket, send to anomaly tracker.
479 // Accumulate partial buckets with current value and then send to anomaly tracker.
480 if (mCurrentFullBucket.size() > 0) {
481 for (const auto& slice : mCurrentSlicedBucket) {
482 mCurrentFullBucket[slice.first] += slice.second.sum;
483 }
484 for (const auto& slice : mCurrentFullBucket) {
485 for (auto& tracker : mAnomalyTrackers) {
486 if (tracker != nullptr) {
487 tracker->addPastBucket(slice.first, slice.second, mCurrentBucketNum);
488 }
489 }
490 }
491 mCurrentFullBucket.clear();
492 } else {
493 // Skip aggregating the partial buckets since there's no previous partial bucket.
494 for (const auto& slice : mCurrentSlicedBucket) {
495 for (auto& tracker : mAnomalyTrackers) {
496 if (tracker != nullptr) {
497 tracker->addPastBucket(slice.first, slice.second.sum, mCurrentBucketNum);
498 }
499 }
500 }
501 }
502 } else {
503 // Accumulate partial bucket.
504 for (const auto& slice : mCurrentSlicedBucket) {
505 mCurrentFullBucket[slice.first] += slice.second.sum;
506 }
507 }
508
Chenjie Yub3dda412017-10-24 13:41:59 -0700509 // Reset counters
Chenjie Yua7259ab2017-12-10 08:31:05 -0800510 mCurrentSlicedBucket.clear();
Chenjie Yub3dda412017-10-24 13:41:59 -0700511}
512
Yangsterf2bee6f2017-11-29 12:01:05 -0800513size_t ValueMetricProducer::byteSizeLocked() const {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800514 size_t totalSize = 0;
515 for (const auto& pair : mPastBuckets) {
516 totalSize += pair.second.size() * kBucketSize;
517 }
518 return totalSize;
yro2b0f8862017-11-06 14:27:31 -0800519}
520
Chenjie Yub3dda412017-10-24 13:41:59 -0700521} // namespace statsd
522} // namespace os
Yao Chen93fe3a32017-11-02 13:52:59 -0700523} // namespace android