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