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