Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 1 | /* |
| 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 Chen | 3c0b95c | 2017-12-16 14:34:20 -0800 | [diff] [blame] | 17 | #define DEBUG false // STOPSHIP if true |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 18 | #include "Log.h" |
| 19 | |
| 20 | #include "ValueMetricProducer.h" |
Chenjie Yu | c587505 | 2018-03-09 10:13:11 -0800 | [diff] [blame] | 21 | #include "../guardrail/StatsdStats.h" |
| 22 | #include "../stats_log_util.h" |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 23 | |
| 24 | #include <cutils/log.h> |
| 25 | #include <limits.h> |
| 26 | #include <stdlib.h> |
| 27 | |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 28 | using android::util::FIELD_COUNT_REPEATED; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 29 | using android::util::FIELD_TYPE_BOOL; |
| 30 | using android::util::FIELD_TYPE_FLOAT; |
| 31 | using android::util::FIELD_TYPE_INT32; |
| 32 | using android::util::FIELD_TYPE_INT64; |
| 33 | using android::util::FIELD_TYPE_MESSAGE; |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 34 | using android::util::FIELD_TYPE_STRING; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 35 | using android::util::ProtoOutputStream; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 36 | using std::list; |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 37 | using std::make_pair; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 38 | using std::make_shared; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 39 | using std::map; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 40 | using std::shared_ptr; |
| 41 | using std::unique_ptr; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 42 | using std::unordered_map; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 43 | |
| 44 | namespace android { |
| 45 | namespace os { |
| 46 | namespace statsd { |
| 47 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 48 | // for StatsLogReport |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 49 | const int FIELD_ID_ID = 1; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 50 | const int FIELD_ID_VALUE_METRICS = 7; |
| 51 | // for ValueMetricDataWrapper |
| 52 | const int FIELD_ID_DATA = 1; |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame^] | 53 | const int FIELD_ID_SKIPPED = 2; |
| 54 | const int FIELD_ID_SKIPPED_START = 1; |
| 55 | const int FIELD_ID_SKIPPED_END = 2; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 56 | // for ValueMetricData |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 57 | const int FIELD_ID_DIMENSION_IN_WHAT = 1; |
| 58 | const int FIELD_ID_DIMENSION_IN_CONDITION = 2; |
| 59 | const int FIELD_ID_BUCKET_INFO = 3; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 60 | // for ValueBucketInfo |
| 61 | const int FIELD_ID_START_BUCKET_NANOS = 1; |
| 62 | const int FIELD_ID_END_BUCKET_NANOS = 2; |
| 63 | const int FIELD_ID_VALUE = 3; |
| 64 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 65 | // ValueMetric has a minimum bucket size of 10min so that we don't pull too frequently |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 66 | ValueMetricProducer::ValueMetricProducer(const ConfigKey& key, const ValueMetric& metric, |
| 67 | const int conditionIndex, |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 68 | const sp<ConditionWizard>& wizard, const int pullTagId, |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 69 | const int64_t timeBaseNs, const int64_t startTimestampNs, |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 70 | shared_ptr<StatsPullerManager> statsPullerManager) |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 71 | : MetricProducer(metric.id(), key, timeBaseNs, conditionIndex, wizard), |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 72 | mValueField(metric.value_field()), |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 73 | mStatsPullerManager(statsPullerManager), |
Chenjie Yu | c587505 | 2018-03-09 10:13:11 -0800 | [diff] [blame] | 74 | mPullTagId(pullTagId), |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame^] | 75 | mMinBucketSizeNs(metric.min_bucket_size_nanos()), |
Chenjie Yu | c587505 | 2018-03-09 10:13:11 -0800 | [diff] [blame] | 76 | mDimensionSoftLimit(StatsdStats::kAtomDimensionKeySizeLimitMap.find(pullTagId) != |
| 77 | StatsdStats::kAtomDimensionKeySizeLimitMap.end() |
| 78 | ? StatsdStats::kAtomDimensionKeySizeLimitMap.at(pullTagId).first |
| 79 | : StatsdStats::kDimensionKeySizeSoftLimit), |
| 80 | mDimensionHardLimit(StatsdStats::kAtomDimensionKeySizeLimitMap.find(pullTagId) != |
| 81 | StatsdStats::kAtomDimensionKeySizeLimitMap.end() |
| 82 | ? StatsdStats::kAtomDimensionKeySizeLimitMap.at(pullTagId).second |
| 83 | : StatsdStats::kDimensionKeySizeHardLimit) { |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 84 | // TODO: valuemetric for pushed events may need unlimited bucket length |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 85 | int64_t bucketSizeMills = 0; |
| 86 | if (metric.has_bucket()) { |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 87 | bucketSizeMills = TimeUnitToBucketSizeInMillisGuardrailed(key.GetUid(), metric.bucket()); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 88 | } else { |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 89 | bucketSizeMills = TimeUnitToBucketSizeInMillis(ONE_HOUR); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 90 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 91 | |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 92 | mBucketSizeNs = bucketSizeMills * 1000000; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 93 | if (metric.has_dimensions_in_what()) { |
| 94 | translateFieldMatcher(metric.dimensions_in_what(), &mDimensionsInWhat); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 95 | mContainANYPositionInDimensionsInWhat = HasPositionANY(metric.dimensions_in_what()); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | if (metric.has_dimensions_in_condition()) { |
| 99 | translateFieldMatcher(metric.dimensions_in_condition(), &mDimensionsInCondition); |
| 100 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 101 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 102 | if (metric.links().size() > 0) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 103 | for (const auto& link : metric.links()) { |
| 104 | Metric2Condition mc; |
| 105 | mc.conditionId = link.condition(); |
| 106 | translateFieldMatcher(link.fields_in_what(), &mc.metricFields); |
| 107 | translateFieldMatcher(link.fields_in_condition(), &mc.conditionFields); |
| 108 | mMetric2ConditionLinks.push_back(mc); |
| 109 | } |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 110 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 111 | |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 112 | if (mValueField.child_size() > 0) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 113 | mField = mValueField.child(0).field(); |
| 114 | } |
| 115 | mConditionSliced = (metric.links().size() > 0) || (mDimensionsInCondition.size() > 0); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 116 | |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 117 | // Kicks off the puller immediately. |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 118 | flushIfNeededLocked(startTimestampNs); |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 119 | if (mPullTagId != -1) { |
| 120 | mStatsPullerManager->RegisterReceiver( |
| 121 | mPullTagId, this, mCurrentBucketStartTimeNs + mBucketSizeNs, mBucketSizeNs); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 122 | } |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 123 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 124 | VLOG("value metric %lld created. bucket size %lld start_time: %lld", |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 125 | (long long)metric.id(), (long long)mBucketSizeNs, (long long)mTimeBaseNs); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 128 | // for testing |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 129 | ValueMetricProducer::ValueMetricProducer(const ConfigKey& key, const ValueMetric& metric, |
| 130 | const int conditionIndex, |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 131 | const sp<ConditionWizard>& wizard, const int pullTagId, |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 132 | const int64_t timeBaseNs, const int64_t startTimeNs) |
| 133 | : ValueMetricProducer(key, metric, conditionIndex, wizard, pullTagId, timeBaseNs, startTimeNs, |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 134 | make_shared<StatsPullerManager>()) { |
| 135 | } |
| 136 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 137 | ValueMetricProducer::~ValueMetricProducer() { |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 138 | VLOG("~ValueMetricProducer() called"); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 139 | if (mPullTagId != -1) { |
| 140 | mStatsPullerManager->UnRegisterReceiver(mPullTagId, this); |
| 141 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 144 | void ValueMetricProducer::onSlicedConditionMayChangeLocked(bool overallCondition, |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 145 | const int64_t eventTime) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 146 | VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 149 | void ValueMetricProducer::dropDataLocked(const int64_t dropTimeNs) { |
Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 150 | flushIfNeededLocked(dropTimeNs); |
| 151 | mPastBuckets.clear(); |
| 152 | } |
| 153 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 154 | void ValueMetricProducer::onDumpReportLocked(const int64_t dumpTimeNs, |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 155 | const bool include_current_partial_bucket, |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 156 | ProtoOutputStream* protoOutput) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 157 | VLOG("metric %lld dump report now...", (long long)mMetricId); |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 158 | if (include_current_partial_bucket) { |
| 159 | flushLocked(dumpTimeNs); |
| 160 | } else { |
| 161 | flushIfNeededLocked(dumpTimeNs); |
| 162 | } |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame^] | 163 | if (mPastBuckets.empty() && mSkippedBuckets.empty()) { |
Yangster-mac | 635b4b3 | 2018-01-23 20:17:35 -0800 | [diff] [blame] | 164 | return; |
| 165 | } |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 166 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId); |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 167 | uint64_t protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_VALUE_METRICS); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 168 | |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame^] | 169 | for (const auto& pair : mSkippedBuckets) { |
| 170 | uint64_t wrapperToken = |
| 171 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_SKIPPED); |
| 172 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_SKIPPED_START, (long long)pair.first); |
| 173 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_SKIPPED_END, (long long)pair.second); |
| 174 | protoOutput->end(wrapperToken); |
| 175 | } |
| 176 | mSkippedBuckets.clear(); |
| 177 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 178 | for (const auto& pair : mPastBuckets) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 179 | const MetricDimensionKey& dimensionKey = pair.first; |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 180 | VLOG(" dimension key %s", dimensionKey.toString().c_str()); |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 181 | uint64_t wrapperToken = |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 182 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 183 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 184 | // First fill dimension. |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 185 | uint64_t dimensionToken = protoOutput->start( |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 186 | FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 187 | writeDimensionToProto(dimensionKey.getDimensionKeyInWhat(), protoOutput); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 188 | protoOutput->end(dimensionToken); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 189 | if (dimensionKey.hasDimensionKeyInCondition()) { |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 190 | uint64_t dimensionInConditionToken = protoOutput->start( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 191 | FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 192 | writeDimensionToProto(dimensionKey.getDimensionKeyInCondition(), protoOutput); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 193 | protoOutput->end(dimensionInConditionToken); |
| 194 | } |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 195 | |
| 196 | // Then fill bucket_info (ValueBucketInfo). |
| 197 | for (const auto& bucket : pair.second) { |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 198 | uint64_t bucketInfoToken = protoOutput->start( |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 199 | FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO); |
| 200 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS, |
| 201 | (long long)bucket.mBucketStartNs); |
| 202 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS, |
| 203 | (long long)bucket.mBucketEndNs); |
| 204 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE, (long long)bucket.mValue); |
| 205 | protoOutput->end(bucketInfoToken); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 206 | VLOG("\t bucket [%lld - %lld] count: %lld", (long long)bucket.mBucketStartNs, |
| 207 | (long long)bucket.mBucketEndNs, (long long)bucket.mValue); |
| 208 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 209 | protoOutput->end(wrapperToken); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 210 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 211 | protoOutput->end(protoToken); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 212 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 213 | VLOG("metric %lld dump report now...", (long long)mMetricId); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 214 | mPastBuckets.clear(); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 217 | void ValueMetricProducer::onConditionChangedLocked(const bool condition, |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 218 | const int64_t eventTimeNs) { |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 219 | mCondition = condition; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 220 | |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 221 | if (eventTimeNs < mCurrentBucketStartTimeNs) { |
| 222 | VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs, |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 223 | (long long)mCurrentBucketStartTimeNs); |
Yao Chen | 2794da2 | 2017-12-13 16:01:55 -0800 | [diff] [blame] | 224 | return; |
| 225 | } |
| 226 | |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 227 | flushIfNeededLocked(eventTimeNs); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 228 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 229 | if (mPullTagId != -1) { |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 230 | vector<shared_ptr<LogEvent>> allData; |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 231 | if (mStatsPullerManager->Pull(mPullTagId, eventTimeNs, &allData)) { |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 232 | if (allData.size() == 0) { |
| 233 | return; |
| 234 | } |
| 235 | for (const auto& data : allData) { |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 236 | onMatchedLogEventLocked(0, *data); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 237 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 238 | } |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 239 | return; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 240 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | void ValueMetricProducer::onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 244 | std::lock_guard<std::mutex> lock(mMutex); |
| 245 | |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 246 | if (mCondition == true || mConditionTrackerIndex < 0) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 247 | if (allData.size() == 0) { |
| 248 | return; |
| 249 | } |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 250 | // For scheduled pulled data, the effective event time is snap to the nearest |
| 251 | // bucket boundary to make bucket finalize. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 252 | int64_t realEventTime = allData.at(0)->GetElapsedTimestampNs(); |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 253 | int64_t eventTime = mTimeBaseNs + |
| 254 | ((realEventTime - mTimeBaseNs) / mBucketSizeNs) * mBucketSizeNs; |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 255 | |
| 256 | mCondition = false; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 257 | for (const auto& data : allData) { |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 258 | data->setElapsedTimestampNs(eventTime - 1); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 259 | onMatchedLogEventLocked(0, *data); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 260 | } |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 261 | |
| 262 | mCondition = true; |
| 263 | for (const auto& data : allData) { |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 264 | data->setElapsedTimestampNs(eventTime); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 265 | onMatchedLogEventLocked(0, *data); |
| 266 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
Yangster-mac | a78d008 | 2018-03-12 12:02:56 -0700 | [diff] [blame] | 270 | void ValueMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const { |
| 271 | if (mCurrentSlicedBucket.size() == 0) { |
| 272 | return; |
| 273 | } |
| 274 | |
| 275 | fprintf(out, "ValueMetric %lld dimension size %lu\n", (long long)mMetricId, |
| 276 | (unsigned long)mCurrentSlicedBucket.size()); |
| 277 | if (verbose) { |
| 278 | for (const auto& it : mCurrentSlicedBucket) { |
| 279 | fprintf(out, "\t(what)%s\t(condition)%s (value)%lld\n", |
| 280 | it.first.getDimensionKeyInWhat().toString().c_str(), |
| 281 | it.first.getDimensionKeyInCondition().toString().c_str(), |
| 282 | (unsigned long long)it.second.sum); |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 287 | bool ValueMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 288 | // ===========GuardRail============== |
| 289 | // 1. Report the tuple count if the tuple count > soft limit |
| 290 | if (mCurrentSlicedBucket.find(newKey) != mCurrentSlicedBucket.end()) { |
| 291 | return false; |
| 292 | } |
Chenjie Yu | c587505 | 2018-03-09 10:13:11 -0800 | [diff] [blame] | 293 | if (mCurrentSlicedBucket.size() > mDimensionSoftLimit - 1) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 294 | size_t newTupleCount = mCurrentSlicedBucket.size() + 1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 295 | StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 296 | // 2. Don't add more tuples, we are above the allowed threshold. Drop the data. |
Chenjie Yu | c587505 | 2018-03-09 10:13:11 -0800 | [diff] [blame] | 297 | if (newTupleCount > mDimensionHardLimit) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 298 | ALOGE("ValueMetric %lld dropping data for dimension key %s", |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 299 | (long long)mMetricId, newKey.toString().c_str()); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 300 | return true; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | return false; |
| 305 | } |
| 306 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 307 | void ValueMetricProducer::onMatchedLogEventInternalLocked( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 308 | const size_t matcherIndex, const MetricDimensionKey& eventKey, |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 309 | const ConditionKey& conditionKey, bool condition, |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 310 | const LogEvent& event) { |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 311 | int64_t eventTimeNs = event.GetElapsedTimestampNs(); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 312 | if (eventTimeNs < mCurrentBucketStartTimeNs) { |
| 313 | VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs, |
| 314 | (long long)mCurrentBucketStartTimeNs); |
| 315 | return; |
| 316 | } |
| 317 | |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 318 | flushIfNeededLocked(eventTimeNs); |
| 319 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 320 | if (hitGuardRailLocked(eventKey)) { |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 321 | return; |
| 322 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 323 | Interval& interval = mCurrentSlicedBucket[eventKey]; |
| 324 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 325 | int error = 0; |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 326 | const int64_t value = event.GetLong(mField, &error); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 327 | if (error < 0) { |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 328 | return; |
| 329 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 330 | |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 331 | if (mPullTagId != -1) { // for pulled events |
| 332 | if (mCondition == true) { |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 333 | if (!interval.startUpdated) { |
| 334 | interval.start = value; |
| 335 | interval.startUpdated = true; |
| 336 | } else { |
| 337 | // skip it if there is already value recorded for the start |
| 338 | VLOG("Already recorded value for this dimension %s", eventKey.toString().c_str()); |
| 339 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 340 | } else { |
Chenjie Yu | 0ed268b | 2018-01-10 09:37:10 -0800 | [diff] [blame] | 341 | // Generally we expect value to be monotonically increasing. |
| 342 | // If not, there was a reset event. We take the absolute value as |
| 343 | // diff in this case. |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 344 | if (interval.startUpdated) { |
Chenjie Yu | 0ed268b | 2018-01-10 09:37:10 -0800 | [diff] [blame] | 345 | if (value > interval.start) { |
| 346 | interval.sum += (value - interval.start); |
| 347 | } else { |
| 348 | interval.sum += value; |
| 349 | } |
Chenjie Yu | ae63b0a | 2018-04-10 14:59:31 -0700 | [diff] [blame] | 350 | interval.hasValue = true; |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 351 | interval.startUpdated = false; |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 352 | } else { |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 353 | VLOG("No start for matching end %lld", (long long)value); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 354 | interval.tainted += 1; |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 355 | } |
| 356 | } |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 357 | } else { // for pushed events |
| 358 | interval.sum += value; |
Chenjie Yu | ae63b0a | 2018-04-10 14:59:31 -0700 | [diff] [blame] | 359 | interval.hasValue = true; |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 360 | } |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 361 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 362 | long wholeBucketVal = interval.sum; |
| 363 | auto prev = mCurrentFullBucket.find(eventKey); |
| 364 | if (prev != mCurrentFullBucket.end()) { |
| 365 | wholeBucketVal += prev->second; |
| 366 | } |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 367 | for (auto& tracker : mAnomalyTrackers) { |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 368 | tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, eventKey, wholeBucketVal); |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 369 | } |
Yangster | 8de6939 | 2017-11-27 13:48:29 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 372 | void ValueMetricProducer::flushIfNeededLocked(const int64_t& eventTimeNs) { |
| 373 | int64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs(); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 374 | |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 375 | if (eventTimeNs < currentBucketEndTimeNs) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 376 | VLOG("eventTime is %lld, less than next bucket start time %lld", (long long)eventTimeNs, |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 377 | (long long)(currentBucketEndTimeNs)); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 378 | return; |
| 379 | } |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 380 | |
| 381 | flushCurrentBucketLocked(eventTimeNs); |
| 382 | |
| 383 | int64_t numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs; |
| 384 | mCurrentBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs; |
| 385 | mCurrentBucketNum += numBucketsForward; |
| 386 | |
| 387 | if (numBucketsForward > 1) { |
| 388 | VLOG("Skipping forward %lld buckets", (long long)numBucketsForward); |
| 389 | } |
| 390 | VLOG("metric %lld: new bucket start time: %lld", (long long)mMetricId, |
| 391 | (long long)mCurrentBucketStartTimeNs); |
| 392 | } |
| 393 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 394 | void ValueMetricProducer::flushCurrentBucketLocked(const int64_t& eventTimeNs) { |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 395 | VLOG("finalizing bucket for %ld, dumping %d slices", (long)mCurrentBucketStartTimeNs, |
| 396 | (int)mCurrentSlicedBucket.size()); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 397 | int64_t fullBucketEndTimeNs = getCurrentBucketEndTimeNs(); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 398 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 399 | ValueBucket info; |
| 400 | info.mBucketStartNs = mCurrentBucketStartTimeNs; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 401 | if (eventTimeNs < fullBucketEndTimeNs) { |
| 402 | info.mBucketEndNs = eventTimeNs; |
| 403 | } else { |
| 404 | info.mBucketEndNs = fullBucketEndTimeNs; |
| 405 | } |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 406 | |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame^] | 407 | if (info.mBucketEndNs - mCurrentBucketStartTimeNs >= mMinBucketSizeNs) { |
| 408 | // The current bucket is large enough to keep. |
| 409 | int tainted = 0; |
| 410 | for (const auto& slice : mCurrentSlicedBucket) { |
| 411 | tainted += slice.second.tainted; |
| 412 | tainted += slice.second.startUpdated; |
| 413 | if (slice.second.hasValue) { |
| 414 | info.mValue = slice.second.sum; |
| 415 | // it will auto create new vector of ValuebucketInfo if the key is not found. |
| 416 | auto& bucketList = mPastBuckets[slice.first]; |
| 417 | bucketList.push_back(info); |
| 418 | } |
Chenjie Yu | ae63b0a | 2018-04-10 14:59:31 -0700 | [diff] [blame] | 419 | } |
David Chen | 81245fd | 2018-04-12 14:33:37 -0700 | [diff] [blame^] | 420 | VLOG("%d tainted pairs in the bucket", tainted); |
| 421 | } else { |
| 422 | mSkippedBuckets.emplace_back(info.mBucketStartNs, info.mBucketEndNs); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 423 | } |
| 424 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 425 | if (eventTimeNs > fullBucketEndTimeNs) { // If full bucket, send to anomaly tracker. |
| 426 | // Accumulate partial buckets with current value and then send to anomaly tracker. |
| 427 | if (mCurrentFullBucket.size() > 0) { |
| 428 | for (const auto& slice : mCurrentSlicedBucket) { |
| 429 | mCurrentFullBucket[slice.first] += slice.second.sum; |
| 430 | } |
| 431 | for (const auto& slice : mCurrentFullBucket) { |
| 432 | for (auto& tracker : mAnomalyTrackers) { |
| 433 | if (tracker != nullptr) { |
| 434 | tracker->addPastBucket(slice.first, slice.second, mCurrentBucketNum); |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | mCurrentFullBucket.clear(); |
| 439 | } else { |
| 440 | // Skip aggregating the partial buckets since there's no previous partial bucket. |
| 441 | for (const auto& slice : mCurrentSlicedBucket) { |
| 442 | for (auto& tracker : mAnomalyTrackers) { |
| 443 | if (tracker != nullptr) { |
| 444 | tracker->addPastBucket(slice.first, slice.second.sum, mCurrentBucketNum); |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | } else { |
| 450 | // Accumulate partial bucket. |
| 451 | for (const auto& slice : mCurrentSlicedBucket) { |
| 452 | mCurrentFullBucket[slice.first] += slice.second.sum; |
| 453 | } |
| 454 | } |
| 455 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 456 | // Reset counters |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 457 | mCurrentSlicedBucket.clear(); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 460 | size_t ValueMetricProducer::byteSizeLocked() const { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 461 | size_t totalSize = 0; |
| 462 | for (const auto& pair : mPastBuckets) { |
| 463 | totalSize += pair.second.size() * kBucketSize; |
| 464 | } |
| 465 | return totalSize; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 466 | } |
| 467 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 468 | } // namespace statsd |
| 469 | } // namespace os |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 470 | } // namespace android |