Yao Chen | 44cf27c | 2017-09-14 22:32:50 -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 |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 18 | #include "Log.h" |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 19 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 20 | #include "CountMetricProducer.h" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 21 | #include "guardrail/StatsdStats.h" |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 22 | #include "stats_util.h" |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 23 | #include "stats_log_util.h" |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 24 | #include "dimension.h" |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 25 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 26 | #include <limits.h> |
| 27 | #include <stdlib.h> |
| 28 | |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 29 | using android::util::FIELD_COUNT_REPEATED; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 30 | using android::util::FIELD_TYPE_BOOL; |
| 31 | using android::util::FIELD_TYPE_FLOAT; |
| 32 | using android::util::FIELD_TYPE_INT32; |
| 33 | using android::util::FIELD_TYPE_INT64; |
| 34 | using android::util::FIELD_TYPE_MESSAGE; |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 35 | using android::util::FIELD_TYPE_STRING; |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 36 | using android::util::ProtoOutputStream; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 37 | using std::map; |
| 38 | using std::string; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 39 | using std::unordered_map; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 40 | using std::vector; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 41 | |
| 42 | namespace android { |
| 43 | namespace os { |
| 44 | namespace statsd { |
| 45 | |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 46 | // for StatsLogReport |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 47 | const int FIELD_ID_ID = 1; |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 48 | const int FIELD_ID_START_REPORT_NANOS = 2; |
| 49 | const int FIELD_ID_END_REPORT_NANOS = 3; |
| 50 | const int FIELD_ID_COUNT_METRICS = 5; |
| 51 | // for CountMetricDataWrapper |
| 52 | const int FIELD_ID_DATA = 1; |
| 53 | // for CountMetricData |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 54 | const int FIELD_ID_DIMENSION_IN_WHAT = 1; |
| 55 | const int FIELD_ID_DIMENSION_IN_CONDITION = 2; |
| 56 | const int FIELD_ID_BUCKET_INFO = 3; |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 57 | // for CountBucketInfo |
| 58 | const int FIELD_ID_START_BUCKET_NANOS = 1; |
| 59 | const int FIELD_ID_END_BUCKET_NANOS = 2; |
| 60 | const int FIELD_ID_COUNT = 3; |
| 61 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 62 | CountMetricProducer::CountMetricProducer(const ConfigKey& key, const CountMetric& metric, |
| 63 | const int conditionIndex, |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 64 | const sp<ConditionWizard>& wizard, |
| 65 | const uint64_t startTimeNs) |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 66 | : MetricProducer(metric.id(), key, startTimeNs, conditionIndex, wizard) { |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 67 | // TODO: evaluate initial conditions. and set mConditionMet. |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 68 | if (metric.has_bucket()) { |
| 69 | mBucketSizeNs = TimeUnitToBucketSizeInMillis(metric.bucket()) * 1000000; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 70 | } else { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 71 | mBucketSizeNs = LLONG_MAX; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 74 | // TODO: use UidMap if uid->pkg_name is required |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 75 | mDimensionsInWhat = metric.dimensions_in_what(); |
| 76 | mDimensionsInCondition = metric.dimensions_in_condition(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 77 | |
| 78 | if (metric.links().size() > 0) { |
| 79 | mConditionLinks.insert(mConditionLinks.begin(), metric.links().begin(), |
| 80 | metric.links().end()); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 81 | } |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 82 | mConditionSliced = (metric.links().size() > 0)|| |
| 83 | (mDimensionsInCondition.has_field() && mDimensionsInCondition.child_size() > 0); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 84 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 85 | VLOG("metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(), |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 86 | (long long)mBucketSizeNs, (long long)mStartTimeNs); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 89 | CountMetricProducer::~CountMetricProducer() { |
| 90 | VLOG("~CountMetricProducer() called"); |
| 91 | } |
| 92 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 93 | void CountMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 94 | VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 97 | void CountMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs, StatsLogReport* report) { |
| 98 | flushIfNeededLocked(dumpTimeNs); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 99 | report->set_metric_id(mMetricId); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 100 | report->set_start_report_nanos(mStartTimeNs); |
| 101 | |
| 102 | auto count_metrics = report->mutable_count_metrics(); |
| 103 | for (const auto& counter : mPastBuckets) { |
| 104 | CountMetricData* metricData = count_metrics->add_data(); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 105 | *metricData->mutable_dimensions_in_what() = |
| 106 | counter.first.getDimensionKeyInWhat().getDimensionsValue(); |
| 107 | *metricData->mutable_dimensions_in_condition() = |
| 108 | counter.first.getDimensionKeyInCondition().getDimensionsValue(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 109 | for (const auto& bucket : counter.second) { |
| 110 | CountBucketInfo* bucketInfo = metricData->add_bucket_info(); |
| 111 | bucketInfo->set_start_bucket_nanos(bucket.mBucketStartNs); |
| 112 | bucketInfo->set_end_bucket_nanos(bucket.mBucketEndNs); |
| 113 | bucketInfo->set_count(bucket.mCount); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 118 | void CountMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs, |
| 119 | ProtoOutputStream* protoOutput) { |
| 120 | flushIfNeededLocked(dumpTimeNs); |
Yangster-mac | 635b4b3 | 2018-01-23 20:17:35 -0800 | [diff] [blame] | 121 | if (mPastBuckets.empty()) { |
| 122 | return; |
| 123 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 124 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 125 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 126 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, (long long)mStartTimeNs); |
| 127 | long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_COUNT_METRICS); |
| 128 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 129 | VLOG("metric %lld dump report now...",(long long)mMetricId); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 130 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 131 | for (const auto& counter : mPastBuckets) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 132 | const MetricDimensionKey& dimensionKey = counter.first; |
| 133 | VLOG(" dimension key %s", dimensionKey.c_str()); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 134 | |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 135 | long long wrapperToken = |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 136 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 137 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 138 | // First fill dimension. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 139 | long long dimensionInWhatToken = protoOutput->start( |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 140 | FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 141 | writeDimensionsValueProtoToStream( |
| 142 | dimensionKey.getDimensionKeyInWhat().getDimensionsValue(), protoOutput); |
| 143 | protoOutput->end(dimensionInWhatToken); |
| 144 | |
| 145 | if (dimensionKey.hasDimensionKeyInCondition()) { |
| 146 | long long dimensionInConditionToken = protoOutput->start( |
| 147 | FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION); |
| 148 | writeDimensionsValueProtoToStream( |
| 149 | dimensionKey.getDimensionKeyInCondition().getDimensionsValue(), protoOutput); |
| 150 | protoOutput->end(dimensionInConditionToken); |
| 151 | } |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 152 | |
| 153 | // Then fill bucket_info (CountBucketInfo). |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 154 | for (const auto& bucket : counter.second) { |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 155 | long long bucketInfoToken = protoOutput->start( |
| 156 | FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO); |
| 157 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS, |
| 158 | (long long)bucket.mBucketStartNs); |
| 159 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS, |
| 160 | (long long)bucket.mBucketEndNs); |
| 161 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_COUNT, (long long)bucket.mCount); |
| 162 | protoOutput->end(bucketInfoToken); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 163 | VLOG("\t bucket [%lld - %lld] count: %lld", (long long)bucket.mBucketStartNs, |
| 164 | (long long)bucket.mBucketEndNs, (long long)bucket.mCount); |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 165 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 166 | protoOutput->end(wrapperToken); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 167 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 168 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 169 | protoOutput->end(protoToken); |
| 170 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_REPORT_NANOS, (long long)dumpTimeNs); |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 171 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 172 | mPastBuckets.clear(); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 173 | |
| 174 | // TODO: Clear mDimensionKeyMap once the report is dumped. |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 177 | void CountMetricProducer::onConditionChangedLocked(const bool conditionMet, |
| 178 | const uint64_t eventTime) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 179 | VLOG("Metric %lld onConditionChanged", (long long)mMetricId); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 180 | mCondition = conditionMet; |
| 181 | } |
| 182 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 183 | bool CountMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 184 | if (mCurrentSlicedCounter->find(newKey) != mCurrentSlicedCounter->end()) { |
| 185 | return false; |
| 186 | } |
| 187 | // ===========GuardRail============== |
| 188 | // 1. Report the tuple count if the tuple count > soft limit |
| 189 | if (mCurrentSlicedCounter->size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) { |
| 190 | size_t newTupleCount = mCurrentSlicedCounter->size() + 1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 191 | StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 192 | // 2. Don't add more tuples, we are above the allowed threshold. Drop the data. |
| 193 | if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 194 | ALOGE("CountMetric %lld dropping data for dimension key %s", |
| 195 | (long long)mMetricId, newKey.c_str()); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 196 | return true; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return false; |
| 201 | } |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 202 | |
| 203 | void CountMetricProducer::onMatchedLogEventInternalLocked( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 204 | const size_t matcherIndex, const MetricDimensionKey& eventKey, |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 205 | const ConditionKey& conditionKey, bool condition, |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 206 | const LogEvent& event) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 207 | uint64_t eventTimeNs = event.GetTimestampNs(); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 208 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 209 | flushIfNeededLocked(eventTimeNs); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 210 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 211 | if (condition == false) { |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 212 | return; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 213 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 214 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 215 | auto it = mCurrentSlicedCounter->find(eventKey); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 216 | if (it == mCurrentSlicedCounter->end()) { |
| 217 | // ===========GuardRail============== |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 218 | if (hitGuardRailLocked(eventKey)) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 219 | return; |
| 220 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 221 | // create a counter for the new key |
| 222 | (*mCurrentSlicedCounter)[eventKey] = 1; |
| 223 | } else { |
| 224 | // increment the existing value |
| 225 | auto& count = it->second; |
| 226 | count++; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 227 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 228 | for (auto& tracker : mAnomalyTrackers) { |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 229 | int64_t countWholeBucket = mCurrentSlicedCounter->find(eventKey)->second; |
| 230 | auto prev = mCurrentFullCounters->find(eventKey); |
| 231 | if (prev != mCurrentFullCounters->end()) { |
| 232 | countWholeBucket += prev->second; |
| 233 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 234 | tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, eventKey, |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 235 | countWholeBucket); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 238 | VLOG("metric %lld %s->%lld", (long long)mMetricId, eventKey.c_str(), |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 239 | (long long)(*mCurrentSlicedCounter)[eventKey]); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 242 | // When a new matched event comes in, we check if event falls into the current |
| 243 | // bucket. If not, flush the old counter to past buckets and initialize the new bucket. |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 244 | void CountMetricProducer::flushIfNeededLocked(const uint64_t& eventTimeNs) { |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 245 | uint64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs(); |
| 246 | if (eventTimeNs < currentBucketEndTimeNs) { |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 247 | return; |
| 248 | } |
| 249 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 250 | flushCurrentBucketLocked(eventTimeNs); |
| 251 | // Setup the bucket start time and number. |
| 252 | uint64_t numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs; |
| 253 | mCurrentBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs; |
| 254 | mCurrentBucketNum += numBucketsForward; |
| 255 | VLOG("metric %lld: new bucket start time: %lld", (long long)mMetricId, |
| 256 | (long long)mCurrentBucketStartTimeNs); |
| 257 | } |
| 258 | |
| 259 | void CountMetricProducer::flushCurrentBucketLocked(const uint64_t& eventTimeNs) { |
| 260 | uint64_t fullBucketEndTimeNs = getCurrentBucketEndTimeNs(); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 261 | CountBucket info; |
| 262 | info.mBucketStartNs = mCurrentBucketStartTimeNs; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 263 | if (eventTimeNs < fullBucketEndTimeNs) { |
| 264 | info.mBucketEndNs = eventTimeNs; |
| 265 | } else { |
| 266 | info.mBucketEndNs = fullBucketEndTimeNs; |
| 267 | } |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 268 | info.mBucketNum = mCurrentBucketNum; |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 269 | for (const auto& counter : *mCurrentSlicedCounter) { |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 270 | info.mCount = counter.second; |
| 271 | auto& bucketList = mPastBuckets[counter.first]; |
| 272 | bucketList.push_back(info); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 273 | VLOG("metric %lld, dump key value: %s -> %lld", (long long)mMetricId, counter.first.c_str(), |
| 274 | (long long)counter.second); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 275 | } |
| 276 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 277 | // If we have finished a full bucket, then send this to anomaly tracker. |
| 278 | if (eventTimeNs > fullBucketEndTimeNs) { |
| 279 | // Accumulate partial buckets with current value and then send to anomaly tracker. |
| 280 | if (mCurrentFullCounters->size() > 0) { |
| 281 | for (const auto& keyValuePair : *mCurrentSlicedCounter) { |
| 282 | (*mCurrentFullCounters)[keyValuePair.first] += keyValuePair.second; |
| 283 | } |
| 284 | for (auto& tracker : mAnomalyTrackers) { |
| 285 | tracker->addPastBucket(mCurrentFullCounters, mCurrentBucketNum); |
| 286 | } |
| 287 | mCurrentFullCounters = std::make_shared<DimToValMap>(); |
| 288 | } else { |
| 289 | // Skip aggregating the partial buckets since there's no previous partial bucket. |
| 290 | for (auto& tracker : mAnomalyTrackers) { |
| 291 | tracker->addPastBucket(mCurrentSlicedCounter, mCurrentBucketNum); |
| 292 | } |
| 293 | } |
| 294 | } else { |
| 295 | // Accumulate partial bucket. |
| 296 | for (const auto& keyValuePair : *mCurrentSlicedCounter) { |
| 297 | (*mCurrentFullCounters)[keyValuePair.first] += keyValuePair.second; |
| 298 | } |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 299 | } |
Bookatz | d3606c7 | 2017-10-19 10:13:49 -0700 | [diff] [blame] | 300 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 301 | // Only resets the counters, but doesn't setup the times nor numbers. |
| 302 | // (Do not clear since the old one is still referenced in mAnomalyTrackers). |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 303 | mCurrentSlicedCounter = std::make_shared<DimToValMap>(); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 304 | } |
| 305 | |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 306 | // Rough estimate of CountMetricProducer buffer stored. This number will be |
| 307 | // greater than actual data size as it contains each dimension of |
| 308 | // CountMetricData is duplicated. |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 309 | size_t CountMetricProducer::byteSizeLocked() const { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 310 | size_t totalSize = 0; |
| 311 | for (const auto& pair : mPastBuckets) { |
| 312 | totalSize += pair.second.size() * kBucketSize; |
| 313 | } |
| 314 | return totalSize; |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 317 | } // namespace statsd |
| 318 | } // namespace os |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 319 | } // namespace android |