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" |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 24 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 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 | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 35 | using android::util::ProtoOutputStream; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 36 | using std::map; |
| 37 | using std::string; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 38 | using std::unordered_map; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 39 | using std::vector; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 40 | |
| 41 | namespace android { |
| 42 | namespace os { |
| 43 | namespace statsd { |
| 44 | |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 45 | // for StatsLogReport |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 46 | const int FIELD_ID_ID = 1; |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 47 | const int FIELD_ID_COUNT_METRICS = 5; |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 48 | const int FIELD_ID_TIME_BASE = 9; |
| 49 | const int FIELD_ID_BUCKET_SIZE = 10; |
| 50 | const int FIELD_ID_DIMENSION_PATH_IN_WHAT = 11; |
| 51 | const int FIELD_ID_DIMENSION_PATH_IN_CONDITION = 12; |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 52 | // for CountMetricDataWrapper |
| 53 | const int FIELD_ID_DATA = 1; |
| 54 | // for CountMetricData |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 55 | const int FIELD_ID_DIMENSION_IN_WHAT = 1; |
| 56 | const int FIELD_ID_DIMENSION_IN_CONDITION = 2; |
| 57 | const int FIELD_ID_BUCKET_INFO = 3; |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 58 | const int FIELD_ID_DIMENSION_LEAF_IN_WHAT = 4; |
| 59 | const int FIELD_ID_DIMENSION_LEAF_IN_CONDITION = 5; |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 60 | // for CountBucketInfo |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 61 | const int FIELD_ID_COUNT = 3; |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 62 | const int FIELD_ID_BUCKET_NUM = 4; |
| 63 | const int FIELD_ID_START_BUCKET_ELAPSED_MILLIS = 5; |
| 64 | const int FIELD_ID_END_BUCKET_ELAPSED_MILLIS = 6; |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 65 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 66 | CountMetricProducer::CountMetricProducer(const ConfigKey& key, const CountMetric& metric, |
| 67 | const int conditionIndex, |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 68 | const sp<ConditionWizard>& wizard, |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 69 | const int64_t startTimeNs) |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 70 | : MetricProducer(metric.id(), key, startTimeNs, conditionIndex, wizard) { |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 71 | if (metric.has_bucket()) { |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 72 | mBucketSizeNs = |
| 73 | TimeUnitToBucketSizeInMillisGuardrailed(key.GetUid(), metric.bucket()) * 1000000; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 74 | } else { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 75 | mBucketSizeNs = LLONG_MAX; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 78 | if (metric.has_dimensions_in_what()) { |
| 79 | translateFieldMatcher(metric.dimensions_in_what(), &mDimensionsInWhat); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 80 | mContainANYPositionInDimensionsInWhat = HasPositionANY(metric.dimensions_in_what()); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 83 | mSliceByPositionALL = HasPositionALL(metric.dimensions_in_what()) || |
| 84 | HasPositionALL(metric.dimensions_in_condition()); |
| 85 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 86 | if (metric.has_dimensions_in_condition()) { |
| 87 | translateFieldMatcher(metric.dimensions_in_condition(), &mDimensionsInCondition); |
| 88 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 89 | |
| 90 | if (metric.links().size() > 0) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 91 | for (const auto& link : metric.links()) { |
| 92 | Metric2Condition mc; |
| 93 | mc.conditionId = link.condition(); |
| 94 | translateFieldMatcher(link.fields_in_what(), &mc.metricFields); |
| 95 | translateFieldMatcher(link.fields_in_condition(), &mc.conditionFields); |
| 96 | mMetric2ConditionLinks.push_back(mc); |
| 97 | } |
| 98 | mConditionSliced = true; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 99 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 100 | |
| 101 | mConditionSliced = (metric.links().size() > 0) || (mDimensionsInCondition.size() > 0); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 102 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 103 | VLOG("metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(), |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 104 | (long long)mBucketSizeNs, (long long)mTimeBaseNs); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 107 | CountMetricProducer::~CountMetricProducer() { |
| 108 | VLOG("~CountMetricProducer() called"); |
| 109 | } |
| 110 | |
Yangster-mac | a78d008 | 2018-03-12 12:02:56 -0700 | [diff] [blame] | 111 | void CountMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const { |
| 112 | if (mCurrentSlicedCounter == nullptr || |
| 113 | mCurrentSlicedCounter->size() == 0) { |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | fprintf(out, "CountMetric %lld dimension size %lu\n", (long long)mMetricId, |
| 118 | (unsigned long)mCurrentSlicedCounter->size()); |
| 119 | if (verbose) { |
| 120 | for (const auto& it : *mCurrentSlicedCounter) { |
| 121 | fprintf(out, "\t(what)%s\t(condition)%s %lld\n", |
| 122 | it.first.getDimensionKeyInWhat().toString().c_str(), |
| 123 | it.first.getDimensionKeyInCondition().toString().c_str(), |
| 124 | (unsigned long long)it.second); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 129 | void CountMetricProducer::onSlicedConditionMayChangeLocked(bool overallCondition, |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 130 | const int64_t eventTime) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 131 | VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Yangster-mac | a802d73 | 2018-04-24 07:50:38 -0700 | [diff] [blame] | 134 | |
| 135 | void CountMetricProducer::clearPastBucketsLocked(const int64_t dumpTimeNs) { |
| 136 | flushIfNeededLocked(dumpTimeNs); |
| 137 | mPastBuckets.clear(); |
| 138 | } |
| 139 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 140 | void CountMetricProducer::onDumpReportLocked(const int64_t dumpTimeNs, |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 141 | const bool include_current_partial_bucket, |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 142 | std::set<string> *str_set, |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 143 | ProtoOutputStream* protoOutput) { |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 144 | if (include_current_partial_bucket) { |
| 145 | flushLocked(dumpTimeNs); |
| 146 | } else { |
| 147 | flushIfNeededLocked(dumpTimeNs); |
| 148 | } |
Yangster-mac | 635b4b3 | 2018-01-23 20:17:35 -0800 | [diff] [blame] | 149 | if (mPastBuckets.empty()) { |
| 150 | return; |
| 151 | } |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 152 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 153 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_TIME_BASE, (long long)mTimeBaseNs); |
| 154 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_SIZE, (long long)mBucketSizeNs); |
| 155 | |
| 156 | // Fills the dimension path if not slicing by ALL. |
| 157 | if (!mSliceByPositionALL) { |
| 158 | if (!mDimensionsInWhat.empty()) { |
| 159 | uint64_t dimenPathToken = protoOutput->start( |
| 160 | FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_PATH_IN_WHAT); |
| 161 | writeDimensionPathToProto(mDimensionsInWhat, protoOutput); |
| 162 | protoOutput->end(dimenPathToken); |
| 163 | } |
| 164 | if (!mDimensionsInCondition.empty()) { |
| 165 | uint64_t dimenPathToken = protoOutput->start( |
| 166 | FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_PATH_IN_CONDITION); |
| 167 | writeDimensionPathToProto(mDimensionsInCondition, protoOutput); |
| 168 | protoOutput->end(dimenPathToken); |
| 169 | } |
| 170 | |
| 171 | } |
| 172 | |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 173 | uint64_t protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_COUNT_METRICS); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 174 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 175 | for (const auto& counter : mPastBuckets) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 176 | const MetricDimensionKey& dimensionKey = counter.first; |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 177 | VLOG(" dimension key %s", dimensionKey.toString().c_str()); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 178 | |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 179 | uint64_t wrapperToken = |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 180 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 181 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 182 | // First fill dimension. |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 183 | if (mSliceByPositionALL) { |
| 184 | uint64_t dimensionToken = protoOutput->start( |
| 185 | FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT); |
| 186 | writeDimensionToProto(dimensionKey.getDimensionKeyInWhat(), str_set, protoOutput); |
| 187 | protoOutput->end(dimensionToken); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 188 | |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 189 | if (dimensionKey.hasDimensionKeyInCondition()) { |
| 190 | uint64_t dimensionInConditionToken = protoOutput->start( |
| 191 | FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION); |
| 192 | writeDimensionToProto(dimensionKey.getDimensionKeyInCondition(), |
| 193 | str_set, protoOutput); |
| 194 | protoOutput->end(dimensionInConditionToken); |
| 195 | } |
| 196 | } else { |
| 197 | writeDimensionLeafNodesToProto(dimensionKey.getDimensionKeyInWhat(), |
| 198 | FIELD_ID_DIMENSION_LEAF_IN_WHAT, str_set, protoOutput); |
| 199 | if (dimensionKey.hasDimensionKeyInCondition()) { |
| 200 | writeDimensionLeafNodesToProto(dimensionKey.getDimensionKeyInCondition(), |
| 201 | FIELD_ID_DIMENSION_LEAF_IN_CONDITION, |
| 202 | str_set, protoOutput); |
| 203 | } |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 204 | } |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 205 | // Then fill bucket_info (CountBucketInfo). |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 206 | for (const auto& bucket : counter.second) { |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 207 | uint64_t bucketInfoToken = protoOutput->start( |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 208 | FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 209 | // Partial bucket. |
| 210 | if (bucket.mBucketEndNs - bucket.mBucketStartNs != mBucketSizeNs) { |
| 211 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_ELAPSED_MILLIS, |
| 212 | (long long)NanoToMillis(bucket.mBucketStartNs)); |
| 213 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_ELAPSED_MILLIS, |
| 214 | (long long)NanoToMillis(bucket.mBucketEndNs)); |
| 215 | } else { |
| 216 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_NUM, |
| 217 | (long long)(getBucketNumFromEndTimeNs(bucket.mBucketEndNs))); |
| 218 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 219 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_COUNT, (long long)bucket.mCount); |
| 220 | protoOutput->end(bucketInfoToken); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 221 | VLOG("\t bucket [%lld - %lld] count: %lld", (long long)bucket.mBucketStartNs, |
| 222 | (long long)bucket.mBucketEndNs, (long long)bucket.mCount); |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 223 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 224 | protoOutput->end(wrapperToken); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 225 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 226 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 227 | protoOutput->end(protoToken); |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 228 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 229 | mPastBuckets.clear(); |
Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 230 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 231 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 232 | void CountMetricProducer::dropDataLocked(const int64_t dropTimeNs) { |
Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 233 | flushIfNeededLocked(dropTimeNs); |
| 234 | mPastBuckets.clear(); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 237 | void CountMetricProducer::onConditionChangedLocked(const bool conditionMet, |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 238 | const int64_t eventTime) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 239 | VLOG("Metric %lld onConditionChanged", (long long)mMetricId); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 240 | mCondition = conditionMet; |
| 241 | } |
| 242 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 243 | bool CountMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 244 | if (mCurrentSlicedCounter->find(newKey) != mCurrentSlicedCounter->end()) { |
| 245 | return false; |
| 246 | } |
| 247 | // ===========GuardRail============== |
| 248 | // 1. Report the tuple count if the tuple count > soft limit |
| 249 | if (mCurrentSlicedCounter->size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) { |
| 250 | size_t newTupleCount = mCurrentSlicedCounter->size() + 1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 251 | StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 252 | // 2. Don't add more tuples, we are above the allowed threshold. Drop the data. |
| 253 | if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 254 | ALOGE("CountMetric %lld dropping data for dimension key %s", |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 255 | (long long)mMetricId, newKey.toString().c_str()); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 256 | return true; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | return false; |
| 261 | } |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 262 | |
| 263 | void CountMetricProducer::onMatchedLogEventInternalLocked( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 264 | const size_t matcherIndex, const MetricDimensionKey& eventKey, |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 265 | const ConditionKey& conditionKey, bool condition, |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 266 | const LogEvent& event) { |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 267 | int64_t eventTimeNs = event.GetElapsedTimestampNs(); |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 268 | flushIfNeededLocked(eventTimeNs); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 269 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 270 | if (condition == false) { |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 271 | return; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 272 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 273 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 274 | auto it = mCurrentSlicedCounter->find(eventKey); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 275 | if (it == mCurrentSlicedCounter->end()) { |
| 276 | // ===========GuardRail============== |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 277 | if (hitGuardRailLocked(eventKey)) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 278 | return; |
| 279 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 280 | // create a counter for the new key |
| 281 | (*mCurrentSlicedCounter)[eventKey] = 1; |
| 282 | } else { |
| 283 | // increment the existing value |
| 284 | auto& count = it->second; |
| 285 | count++; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 286 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 287 | for (auto& tracker : mAnomalyTrackers) { |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 288 | int64_t countWholeBucket = mCurrentSlicedCounter->find(eventKey)->second; |
| 289 | auto prev = mCurrentFullCounters->find(eventKey); |
| 290 | if (prev != mCurrentFullCounters->end()) { |
| 291 | countWholeBucket += prev->second; |
| 292 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 293 | tracker->detectAndDeclareAnomaly(eventTimeNs, mCurrentBucketNum, eventKey, |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 294 | countWholeBucket); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 297 | VLOG("metric %lld %s->%lld", (long long)mMetricId, eventKey.toString().c_str(), |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 298 | (long long)(*mCurrentSlicedCounter)[eventKey]); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 301 | // When a new matched event comes in, we check if event falls into the current |
| 302 | // bucket. If not, flush the old counter to past buckets and initialize the new bucket. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 303 | void CountMetricProducer::flushIfNeededLocked(const int64_t& eventTimeNs) { |
| 304 | int64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs(); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 305 | if (eventTimeNs < currentBucketEndTimeNs) { |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 306 | return; |
| 307 | } |
| 308 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 309 | flushCurrentBucketLocked(eventTimeNs); |
| 310 | // Setup the bucket start time and number. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 311 | int64_t numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 312 | mCurrentBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs; |
| 313 | mCurrentBucketNum += numBucketsForward; |
| 314 | VLOG("metric %lld: new bucket start time: %lld", (long long)mMetricId, |
| 315 | (long long)mCurrentBucketStartTimeNs); |
| 316 | } |
| 317 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 318 | void CountMetricProducer::flushCurrentBucketLocked(const int64_t& eventTimeNs) { |
| 319 | int64_t fullBucketEndTimeNs = getCurrentBucketEndTimeNs(); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 320 | CountBucket info; |
| 321 | info.mBucketStartNs = mCurrentBucketStartTimeNs; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 322 | if (eventTimeNs < fullBucketEndTimeNs) { |
| 323 | info.mBucketEndNs = eventTimeNs; |
| 324 | } else { |
| 325 | info.mBucketEndNs = fullBucketEndTimeNs; |
| 326 | } |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 327 | for (const auto& counter : *mCurrentSlicedCounter) { |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 328 | info.mCount = counter.second; |
| 329 | auto& bucketList = mPastBuckets[counter.first]; |
| 330 | bucketList.push_back(info); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 331 | VLOG("metric %lld, dump key value: %s -> %lld", (long long)mMetricId, |
| 332 | counter.first.toString().c_str(), |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 333 | (long long)counter.second); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 334 | } |
| 335 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 336 | // If we have finished a full bucket, then send this to anomaly tracker. |
| 337 | if (eventTimeNs > fullBucketEndTimeNs) { |
| 338 | // Accumulate partial buckets with current value and then send to anomaly tracker. |
| 339 | if (mCurrentFullCounters->size() > 0) { |
| 340 | for (const auto& keyValuePair : *mCurrentSlicedCounter) { |
| 341 | (*mCurrentFullCounters)[keyValuePair.first] += keyValuePair.second; |
| 342 | } |
| 343 | for (auto& tracker : mAnomalyTrackers) { |
| 344 | tracker->addPastBucket(mCurrentFullCounters, mCurrentBucketNum); |
| 345 | } |
| 346 | mCurrentFullCounters = std::make_shared<DimToValMap>(); |
| 347 | } else { |
| 348 | // Skip aggregating the partial buckets since there's no previous partial bucket. |
| 349 | for (auto& tracker : mAnomalyTrackers) { |
| 350 | tracker->addPastBucket(mCurrentSlicedCounter, mCurrentBucketNum); |
| 351 | } |
| 352 | } |
| 353 | } else { |
| 354 | // Accumulate partial bucket. |
| 355 | for (const auto& keyValuePair : *mCurrentSlicedCounter) { |
| 356 | (*mCurrentFullCounters)[keyValuePair.first] += keyValuePair.second; |
| 357 | } |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 358 | } |
Bookatz | d3606c7 | 2017-10-19 10:13:49 -0700 | [diff] [blame] | 359 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 360 | // Only resets the counters, but doesn't setup the times nor numbers. |
| 361 | // (Do not clear since the old one is still referenced in mAnomalyTrackers). |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 362 | mCurrentSlicedCounter = std::make_shared<DimToValMap>(); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 363 | } |
| 364 | |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 365 | // Rough estimate of CountMetricProducer buffer stored. This number will be |
| 366 | // greater than actual data size as it contains each dimension of |
| 367 | // CountMetricData is duplicated. |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 368 | size_t CountMetricProducer::byteSizeLocked() const { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 369 | size_t totalSize = 0; |
| 370 | for (const auto& pair : mPastBuckets) { |
| 371 | totalSize += pair.second.size() * kBucketSize; |
| 372 | } |
| 373 | return totalSize; |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 376 | } // namespace statsd |
| 377 | } // namespace os |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 378 | } // namespace android |