Yao Chen | 729093d | 2017-10-16 10:33:26 -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 |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 18 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 19 | #include "Log.h" |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 20 | #include "DurationMetricProducer.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 | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 24 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -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 | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 35 | using android::util::ProtoOutputStream; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 36 | using std::string; |
| 37 | using std::unordered_map; |
| 38 | using std::vector; |
Ruchir Rastogi | 21a287b | 2019-10-02 12:04:33 -0700 | [diff] [blame] | 39 | using std::shared_ptr; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 40 | |
| 41 | namespace android { |
| 42 | namespace os { |
| 43 | namespace statsd { |
| 44 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [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 | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 47 | const int FIELD_ID_DURATION_METRICS = 6; |
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; |
Howard Ro | 9440e09 | 2018-12-16 19:15:21 -0800 | [diff] [blame] | 51 | const int FIELD_ID_IS_ACTIVE = 14; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 52 | // for DurationMetricDataWrapper |
| 53 | const int FIELD_ID_DATA = 1; |
| 54 | // for DurationMetricData |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 55 | const int FIELD_ID_DIMENSION_IN_WHAT = 1; |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 56 | const int FIELD_ID_BUCKET_INFO = 3; |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 57 | const int FIELD_ID_DIMENSION_LEAF_IN_WHAT = 4; |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 58 | const int FIELD_ID_SLICE_BY_STATE = 6; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 59 | // for DurationBucketInfo |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 60 | const int FIELD_ID_DURATION = 3; |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 61 | const int FIELD_ID_BUCKET_NUM = 4; |
| 62 | const int FIELD_ID_START_BUCKET_ELAPSED_MILLIS = 5; |
| 63 | const int FIELD_ID_END_BUCKET_ELAPSED_MILLIS = 6; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 64 | |
Ruchir Rastogi | 21a287b | 2019-10-02 12:04:33 -0700 | [diff] [blame] | 65 | DurationMetricProducer::DurationMetricProducer( |
| 66 | const ConfigKey& key, const DurationMetric& metric, const int conditionIndex, |
| 67 | const size_t startIndex, const size_t stopIndex, const size_t stopAllIndex, |
| 68 | const bool nesting, const sp<ConditionWizard>& wizard, |
| 69 | const FieldMatcher& internalDimensions, const int64_t timeBaseNs, const int64_t startTimeNs, |
| 70 | const unordered_map<int, shared_ptr<Activation>>& eventActivationMap, |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 71 | const unordered_map<int, vector<shared_ptr<Activation>>>& eventDeactivationMap, |
| 72 | const vector<int>& slicedStateAtoms, |
| 73 | const unordered_map<int, unordered_map<int, int64_t>>& stateGroupMap) |
Ruchir Rastogi | 21a287b | 2019-10-02 12:04:33 -0700 | [diff] [blame] | 74 | : MetricProducer(metric.id(), key, timeBaseNs, conditionIndex, wizard, eventActivationMap, |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 75 | eventDeactivationMap, slicedStateAtoms, stateGroupMap), |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 76 | mAggregationType(metric.aggregation_type()), |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 77 | mStartIndex(startIndex), |
| 78 | mStopIndex(stopIndex), |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 79 | mStopAllIndex(stopAllIndex), |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 80 | mNested(nesting), |
| 81 | mContainANYPositionInInternalDimensions(false) { |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 82 | if (metric.has_bucket()) { |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 83 | mBucketSizeNs = |
| 84 | TimeUnitToBucketSizeInMillisGuardrailed(key.GetUid(), metric.bucket()) * 1000000; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 85 | } else { |
| 86 | mBucketSizeNs = LLONG_MAX; |
| 87 | } |
| 88 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 89 | if (metric.has_dimensions_in_what()) { |
| 90 | translateFieldMatcher(metric.dimensions_in_what(), &mDimensionsInWhat); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 91 | mContainANYPositionInDimensionsInWhat = HasPositionANY(metric.dimensions_in_what()); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | if (internalDimensions.has_field()) { |
| 95 | translateFieldMatcher(internalDimensions, &mInternalDimensions); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 96 | mContainANYPositionInInternalDimensions = HasPositionANY(internalDimensions); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 97 | } |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 98 | if (mContainANYPositionInInternalDimensions) { |
| 99 | ALOGE("Position ANY in internal dimension not supported."); |
| 100 | } |
| 101 | if (mContainANYPositionInDimensionsInWhat) { |
| 102 | ALOGE("Position ANY in dimension_in_what not supported."); |
| 103 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 104 | |
tsaichristine | 7685337 | 2019-08-06 17:17:03 -0700 | [diff] [blame] | 105 | mSliceByPositionALL = HasPositionALL(metric.dimensions_in_what()); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 106 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 107 | if (metric.links().size() > 0) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 108 | for (const auto& link : metric.links()) { |
| 109 | Metric2Condition mc; |
| 110 | mc.conditionId = link.condition(); |
| 111 | translateFieldMatcher(link.fields_in_what(), &mc.metricFields); |
| 112 | translateFieldMatcher(link.fields_in_condition(), &mc.conditionFields); |
| 113 | mMetric2ConditionLinks.push_back(mc); |
| 114 | } |
tsaichristine | 7685337 | 2019-08-06 17:17:03 -0700 | [diff] [blame] | 115 | mConditionSliced = true; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 116 | } |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 117 | mUnSlicedPartCondition = ConditionState::kUnknown; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 118 | |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 119 | for (const auto& stateLink : metric.state_link()) { |
| 120 | Metric2State ms; |
| 121 | ms.stateAtomId = stateLink.state_atom_id(); |
| 122 | translateFieldMatcher(stateLink.fields_in_what(), &ms.metricFields); |
| 123 | translateFieldMatcher(stateLink.fields_in_state(), &ms.stateFields); |
| 124 | mMetric2StateLinks.push_back(ms); |
| 125 | } |
| 126 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 127 | mUseWhatDimensionAsInternalDimension = equalDimensions(mDimensionsInWhat, mInternalDimensions); |
tsaichristine | 7685337 | 2019-08-06 17:17:03 -0700 | [diff] [blame] | 128 | if (mWizard != nullptr && mConditionTrackerIndex >= 0 && |
| 129 | mMetric2ConditionLinks.size() == 1) { |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 130 | mHasLinksToAllConditionDimensionsInTracker = mWizard->equalOutputDimensions( |
| 131 | mConditionTrackerIndex, mMetric2ConditionLinks.begin()->conditionFields); |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 132 | } |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 133 | flushIfNeededLocked(startTimeNs); |
| 134 | // Adjust start for partial bucket |
| 135 | mCurrentBucketStartTimeNs = startTimeNs; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 136 | 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] | 137 | (long long)mBucketSizeNs, (long long)mTimeBaseNs); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | DurationMetricProducer::~DurationMetricProducer() { |
| 141 | VLOG("~DurationMetric() called"); |
| 142 | } |
| 143 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 144 | sp<AnomalyTracker> DurationMetricProducer::addAnomalyTracker( |
| 145 | const Alert &alert, const sp<AlarmMonitor>& anomalyAlarmMonitor) { |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 146 | std::lock_guard<std::mutex> lock(mMutex); |
Bookatz | 423f753 | 2018-03-08 15:45:14 -0800 | [diff] [blame] | 147 | if (mAggregationType == DurationMetric_AggregationType_SUM) { |
| 148 | if (alert.trigger_if_sum_gt() > alert.num_buckets() * mBucketSizeNs) { |
| 149 | ALOGW("invalid alert for SUM: threshold (%f) > possible recordable value (%d x %lld)", |
| 150 | alert.trigger_if_sum_gt(), alert.num_buckets(), (long long)mBucketSizeNs); |
| 151 | return nullptr; |
| 152 | } |
| 153 | } |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 154 | sp<DurationAnomalyTracker> anomalyTracker = |
| 155 | new DurationAnomalyTracker(alert, mConfigKey, anomalyAlarmMonitor); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 156 | if (anomalyTracker != nullptr) { |
| 157 | mAnomalyTrackers.push_back(anomalyTracker); |
| 158 | } |
| 159 | return anomalyTracker; |
Bookatz | 450099d | 2017-11-30 17:09:30 -0800 | [diff] [blame] | 160 | } |
| 161 | |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 162 | void DurationMetricProducer::onStateChanged(const int64_t eventTimeNs, const int32_t atomId, |
| 163 | const HashableDimensionKey& primaryKey, |
| 164 | const int32_t oldState, const int32_t newState) { |
| 165 | // Create a FieldValue object to hold the new state. |
| 166 | FieldValue value; |
| 167 | value.mValue.setInt(newState); |
| 168 | // Check if this metric has a StateMap. If so, map the new state value to |
| 169 | // the correct state group id. |
| 170 | mapStateValue(atomId, &value); |
| 171 | |
| 172 | flushIfNeededLocked(eventTimeNs); |
| 173 | |
| 174 | // Each duration tracker is mapped to a different whatKey (a set of values from the |
| 175 | // dimensionsInWhat fields). We notify all trackers iff the primaryKey field values from the |
| 176 | // state change event are a subset of the tracker's whatKey field values. |
| 177 | // |
| 178 | // Ex. For a duration metric dimensioned on uid and tag: |
| 179 | // DurationTracker1 whatKey = uid: 1001, tag: 1 |
| 180 | // DurationTracker2 whatKey = uid: 1002, tag 1 |
| 181 | // |
| 182 | // If the state change primaryKey = uid: 1001, we only notify DurationTracker1 of a state |
| 183 | // change. |
| 184 | for (auto& whatIt : mCurrentSlicedDurationTrackerMap) { |
| 185 | if (!containsLinkedStateValues(whatIt.first, primaryKey, mMetric2StateLinks, atomId)) { |
| 186 | continue; |
| 187 | } |
| 188 | whatIt.second->onStateChanged(eventTimeNs, atomId, value); |
| 189 | } |
| 190 | } |
| 191 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 192 | unique_ptr<DurationTracker> DurationMetricProducer::createDurationTracker( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 193 | const MetricDimensionKey& eventKey) const { |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 194 | switch (mAggregationType) { |
Stefan Lafon | cfed20b | 2017-11-18 09:26:53 -0800 | [diff] [blame] | 195 | case DurationMetric_AggregationType_SUM: |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 196 | return make_unique<OringDurationTracker>( |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 197 | mConfigKey, mMetricId, eventKey, mWizard, mConditionTrackerIndex, mNested, |
| 198 | mCurrentBucketStartTimeNs, mCurrentBucketNum, mTimeBaseNs, mBucketSizeNs, |
| 199 | mConditionSliced, mHasLinksToAllConditionDimensionsInTracker, mAnomalyTrackers); |
Stefan Lafon | cfed20b | 2017-11-18 09:26:53 -0800 | [diff] [blame] | 200 | case DurationMetric_AggregationType_MAX_SPARSE: |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 201 | return make_unique<MaxDurationTracker>( |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 202 | mConfigKey, mMetricId, eventKey, mWizard, mConditionTrackerIndex, mNested, |
| 203 | mCurrentBucketStartTimeNs, mCurrentBucketNum, mTimeBaseNs, mBucketSizeNs, |
| 204 | mConditionSliced, mHasLinksToAllConditionDimensionsInTracker, mAnomalyTrackers); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
| 208 | // SlicedConditionChange optimization case 1: |
| 209 | // 1. If combination condition, logical operation is AND, only one sliced child predicate. |
tsaichristine | 7685337 | 2019-08-06 17:17:03 -0700 | [diff] [blame] | 210 | // 2. The links covers all dimension fields in the sliced child condition predicate. |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 211 | void DurationMetricProducer::onSlicedConditionMayChangeLocked_opt1(bool condition, |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 212 | const int64_t eventTime) { |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 213 | if (mMetric2ConditionLinks.size() != 1 || |
tsaichristine | 7685337 | 2019-08-06 17:17:03 -0700 | [diff] [blame] | 214 | !mHasLinksToAllConditionDimensionsInTracker) { |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 215 | return; |
| 216 | } |
| 217 | |
| 218 | bool currentUnSlicedPartCondition = true; |
| 219 | if (!mWizard->IsSimpleCondition(mConditionTrackerIndex)) { |
| 220 | ConditionState unslicedPartState = |
| 221 | mWizard->getUnSlicedPartConditionState(mConditionTrackerIndex); |
| 222 | // When the unsliced part is still false, return directly. |
| 223 | if (mUnSlicedPartCondition == ConditionState::kFalse && |
| 224 | unslicedPartState == ConditionState::kFalse) { |
| 225 | return; |
| 226 | } |
| 227 | mUnSlicedPartCondition = unslicedPartState; |
| 228 | currentUnSlicedPartCondition = mUnSlicedPartCondition > 0; |
| 229 | } |
| 230 | |
| 231 | auto dimensionsChangedToTrue = mWizard->getChangedToTrueDimensions(mConditionTrackerIndex); |
| 232 | auto dimensionsChangedToFalse = mWizard->getChangedToFalseDimensions(mConditionTrackerIndex); |
| 233 | |
| 234 | // The condition change is from the unsliced predicates. |
| 235 | // We need to find out the true dimensions from the sliced predicate and flip their condition |
| 236 | // state based on the new unsliced condition state. |
| 237 | if (dimensionsChangedToTrue == nullptr || dimensionsChangedToFalse == nullptr || |
| 238 | (dimensionsChangedToTrue->empty() && dimensionsChangedToFalse->empty())) { |
| 239 | std::set<HashableDimensionKey> trueConditionDimensions; |
| 240 | mWizard->getTrueSlicedDimensions(mConditionTrackerIndex, &trueConditionDimensions); |
| 241 | for (auto& whatIt : mCurrentSlicedDurationTrackerMap) { |
| 242 | HashableDimensionKey linkedConditionDimensionKey; |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 243 | getDimensionForCondition(whatIt.first.getValues(), mMetric2ConditionLinks[0], |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 244 | &linkedConditionDimensionKey); |
| 245 | if (trueConditionDimensions.find(linkedConditionDimensionKey) != |
| 246 | trueConditionDimensions.end()) { |
tsaichristine | 81899f1 | 2020-01-08 12:42:12 -0800 | [diff] [blame] | 247 | whatIt.second->onConditionChanged(currentUnSlicedPartCondition, eventTime); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | } else { |
| 251 | // Handle the condition change from the sliced predicate. |
| 252 | if (currentUnSlicedPartCondition) { |
| 253 | for (auto& whatIt : mCurrentSlicedDurationTrackerMap) { |
| 254 | HashableDimensionKey linkedConditionDimensionKey; |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 255 | getDimensionForCondition(whatIt.first.getValues(), mMetric2ConditionLinks[0], |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 256 | &linkedConditionDimensionKey); |
| 257 | if (dimensionsChangedToTrue->find(linkedConditionDimensionKey) != |
| 258 | dimensionsChangedToTrue->end()) { |
tsaichristine | 81899f1 | 2020-01-08 12:42:12 -0800 | [diff] [blame] | 259 | whatIt.second->onConditionChanged(true, eventTime); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 260 | } |
| 261 | if (dimensionsChangedToFalse->find(linkedConditionDimensionKey) != |
| 262 | dimensionsChangedToFalse->end()) { |
tsaichristine | 81899f1 | 2020-01-08 12:42:12 -0800 | [diff] [blame] | 263 | whatIt.second->onConditionChanged(false, eventTime); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
Muhammad Qureshi | 18e4692 | 2019-05-24 16:38:49 -0700 | [diff] [blame] | 270 | void DurationMetricProducer::onSlicedConditionMayChangeInternalLocked(bool overallCondition, |
| 271 | const int64_t eventTimeNs) { |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 272 | bool changeDimTrackable = mWizard->IsChangedDimensionTrackable(mConditionTrackerIndex); |
tsaichristine | 7685337 | 2019-08-06 17:17:03 -0700 | [diff] [blame] | 273 | if (changeDimTrackable && mHasLinksToAllConditionDimensionsInTracker) { |
Muhammad Qureshi | 18e4692 | 2019-05-24 16:38:49 -0700 | [diff] [blame] | 274 | onSlicedConditionMayChangeLocked_opt1(overallCondition, eventTimeNs); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 275 | return; |
| 276 | } |
| 277 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 278 | // Now for each of the on-going event, check if the condition has changed for them. |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 279 | for (auto& whatIt : mCurrentSlicedDurationTrackerMap) { |
tsaichristine | 81899f1 | 2020-01-08 12:42:12 -0800 | [diff] [blame] | 280 | whatIt.second->onSlicedConditionMayChange(overallCondition, eventTimeNs); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
Muhammad Qureshi | 18e4692 | 2019-05-24 16:38:49 -0700 | [diff] [blame] | 284 | void DurationMetricProducer::onSlicedConditionMayChangeLocked(bool overallCondition, |
| 285 | const int64_t eventTime) { |
| 286 | VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId); |
| 287 | |
| 288 | if (!mIsActive) { |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | flushIfNeededLocked(eventTime); |
| 293 | |
| 294 | if (!mConditionSliced) { |
| 295 | return; |
| 296 | } |
| 297 | |
| 298 | onSlicedConditionMayChangeInternalLocked(overallCondition, eventTime); |
| 299 | } |
| 300 | |
| 301 | void DurationMetricProducer::onActiveStateChangedLocked(const int64_t& eventTimeNs) { |
| 302 | MetricProducer::onActiveStateChangedLocked(eventTimeNs); |
| 303 | |
| 304 | if (!mConditionSliced) { |
| 305 | if (ConditionState::kTrue != mCondition) { |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | if (mIsActive) { |
| 310 | flushIfNeededLocked(eventTimeNs); |
| 311 | } |
| 312 | |
| 313 | for (auto& whatIt : mCurrentSlicedDurationTrackerMap) { |
tsaichristine | 81899f1 | 2020-01-08 12:42:12 -0800 | [diff] [blame] | 314 | whatIt.second->onConditionChanged(mIsActive, eventTimeNs); |
Muhammad Qureshi | 18e4692 | 2019-05-24 16:38:49 -0700 | [diff] [blame] | 315 | } |
| 316 | } else if (mIsActive) { |
| 317 | flushIfNeededLocked(eventTimeNs); |
| 318 | onSlicedConditionMayChangeInternalLocked(mIsActive, eventTimeNs); |
| 319 | } else { // mConditionSliced == true && !mIsActive |
| 320 | for (auto& whatIt : mCurrentSlicedDurationTrackerMap) { |
tsaichristine | 81899f1 | 2020-01-08 12:42:12 -0800 | [diff] [blame] | 321 | whatIt.second->onConditionChanged(mIsActive, eventTimeNs); |
Muhammad Qureshi | 18e4692 | 2019-05-24 16:38:49 -0700 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 326 | void DurationMetricProducer::onConditionChangedLocked(const bool conditionMet, |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 327 | const int64_t eventTime) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 328 | VLOG("Metric %lld onConditionChanged", (long long)mMetricId); |
Olivier Gaillard | e63d9e0 | 2019-02-12 14:43:59 +0000 | [diff] [blame] | 329 | mCondition = conditionMet ? ConditionState::kTrue : ConditionState::kFalse; |
Muhammad Qureshi | 18e4692 | 2019-05-24 16:38:49 -0700 | [diff] [blame] | 330 | |
| 331 | if (!mIsActive) { |
| 332 | return; |
| 333 | } |
| 334 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 335 | flushIfNeededLocked(eventTime); |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 336 | for (auto& whatIt : mCurrentSlicedDurationTrackerMap) { |
tsaichristine | 81899f1 | 2020-01-08 12:42:12 -0800 | [diff] [blame] | 337 | whatIt.second->onConditionChanged(conditionMet, eventTime); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 341 | void DurationMetricProducer::dropDataLocked(const int64_t dropTimeNs) { |
Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 342 | flushIfNeededLocked(dropTimeNs); |
Olivier Gaillard | 320952b | 2019-02-06 13:57:24 +0000 | [diff] [blame] | 343 | StatsdStats::getInstance().noteBucketDropped(mMetricId); |
Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 344 | mPastBuckets.clear(); |
| 345 | } |
| 346 | |
Yangster-mac | a802d73 | 2018-04-24 07:50:38 -0700 | [diff] [blame] | 347 | void DurationMetricProducer::clearPastBucketsLocked(const int64_t dumpTimeNs) { |
| 348 | flushIfNeededLocked(dumpTimeNs); |
| 349 | mPastBuckets.clear(); |
| 350 | } |
| 351 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 352 | void DurationMetricProducer::onDumpReportLocked(const int64_t dumpTimeNs, |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 353 | const bool include_current_partial_bucket, |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 354 | const bool erase_data, |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 355 | const DumpLatency dumpLatency, |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 356 | std::set<string> *str_set, |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 357 | ProtoOutputStream* protoOutput) { |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 358 | if (include_current_partial_bucket) { |
| 359 | flushLocked(dumpTimeNs); |
| 360 | } else { |
| 361 | flushIfNeededLocked(dumpTimeNs); |
| 362 | } |
Yang Lu | b472291 | 2018-11-15 11:02:03 -0800 | [diff] [blame] | 363 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId); |
Howard Ro | 07e23ff | 2018-12-17 17:28:07 -0800 | [diff] [blame] | 364 | protoOutput->write(FIELD_TYPE_BOOL | FIELD_ID_IS_ACTIVE, isActiveLocked()); |
Yang Lu | b472291 | 2018-11-15 11:02:03 -0800 | [diff] [blame] | 365 | |
Yangster-mac | 635b4b3 | 2018-01-23 20:17:35 -0800 | [diff] [blame] | 366 | if (mPastBuckets.empty()) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 367 | VLOG(" Duration metric, empty return"); |
Yangster-mac | 635b4b3 | 2018-01-23 20:17:35 -0800 | [diff] [blame] | 368 | return; |
| 369 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 370 | |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 371 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_TIME_BASE, (long long)mTimeBaseNs); |
| 372 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_SIZE, (long long)mBucketSizeNs); |
| 373 | |
| 374 | if (!mSliceByPositionALL) { |
| 375 | if (!mDimensionsInWhat.empty()) { |
| 376 | uint64_t dimenPathToken = protoOutput->start( |
| 377 | FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_PATH_IN_WHAT); |
| 378 | writeDimensionPathToProto(mDimensionsInWhat, protoOutput); |
| 379 | protoOutput->end(dimenPathToken); |
| 380 | } |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 383 | uint64_t protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DURATION_METRICS); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 384 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 385 | VLOG("Duration metric %lld dump report now...", (long long)mMetricId); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 386 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 387 | for (const auto& pair : mPastBuckets) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 388 | const MetricDimensionKey& dimensionKey = pair.first; |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 389 | VLOG(" dimension key %s", dimensionKey.toString().c_str()); |
Yao Chen | 1ff4f43 | 2017-11-16 17:01:40 -0800 | [diff] [blame] | 390 | |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 391 | uint64_t wrapperToken = |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 392 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 393 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 394 | // First fill dimension. |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 395 | if (mSliceByPositionALL) { |
| 396 | uint64_t dimensionToken = protoOutput->start( |
| 397 | FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT); |
| 398 | writeDimensionToProto(dimensionKey.getDimensionKeyInWhat(), str_set, protoOutput); |
| 399 | protoOutput->end(dimensionToken); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 400 | } else { |
| 401 | writeDimensionLeafNodesToProto(dimensionKey.getDimensionKeyInWhat(), |
| 402 | FIELD_ID_DIMENSION_LEAF_IN_WHAT, str_set, protoOutput); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 403 | } |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 404 | // Then fill slice_by_state. |
| 405 | for (auto state : dimensionKey.getStateValuesKey().getValues()) { |
| 406 | uint64_t stateToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | |
| 407 | FIELD_ID_SLICE_BY_STATE); |
| 408 | writeStateToProto(state, protoOutput); |
| 409 | protoOutput->end(stateToken); |
| 410 | } |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 411 | // Then fill bucket_info (DurationBucketInfo). |
| 412 | for (const auto& bucket : pair.second) { |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 413 | uint64_t bucketInfoToken = protoOutput->start( |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 414 | FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO); |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 415 | if (bucket.mBucketEndNs - bucket.mBucketStartNs != mBucketSizeNs) { |
| 416 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_ELAPSED_MILLIS, |
| 417 | (long long)NanoToMillis(bucket.mBucketStartNs)); |
| 418 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_ELAPSED_MILLIS, |
| 419 | (long long)NanoToMillis(bucket.mBucketEndNs)); |
| 420 | } else { |
| 421 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_BUCKET_NUM, |
| 422 | (long long)(getBucketNumFromEndTimeNs(bucket.mBucketEndNs))); |
| 423 | } |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 424 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_DURATION, (long long)bucket.mDuration); |
| 425 | protoOutput->end(bucketInfoToken); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 426 | VLOG("\t bucket [%lld - %lld] duration: %lld", (long long)bucket.mBucketStartNs, |
| 427 | (long long)bucket.mBucketEndNs, (long long)bucket.mDuration); |
| 428 | } |
| 429 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 430 | protoOutput->end(wrapperToken); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 431 | } |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 432 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 433 | protoOutput->end(protoToken); |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 434 | if (erase_data) { |
| 435 | mPastBuckets.clear(); |
| 436 | } |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 437 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 438 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 439 | void DurationMetricProducer::flushIfNeededLocked(const int64_t& eventTimeNs) { |
| 440 | int64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs(); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 441 | |
| 442 | if (currentBucketEndTimeNs > eventTimeNs) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 443 | return; |
| 444 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 445 | VLOG("flushing..........."); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 446 | int numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs; |
Muhammad Qureshi | 902529a | 2019-03-14 16:03:21 -0700 | [diff] [blame] | 447 | int64_t nextBucketNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs; |
| 448 | flushCurrentBucketLocked(eventTimeNs, nextBucketNs); |
| 449 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 450 | mCurrentBucketNum += numBucketsForward; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 453 | void DurationMetricProducer::flushCurrentBucketLocked(const int64_t& eventTimeNs, |
| 454 | const int64_t& nextBucketStartTimeNs) { |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 455 | for (auto whatIt = mCurrentSlicedDurationTrackerMap.begin(); |
| 456 | whatIt != mCurrentSlicedDurationTrackerMap.end();) { |
tsaichristine | 81899f1 | 2020-01-08 12:42:12 -0800 | [diff] [blame] | 457 | if (whatIt->second->flushCurrentBucket(eventTimeNs, &mPastBuckets)) { |
| 458 | VLOG("erase bucket for key %s", whatIt->first.toString().c_str()); |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 459 | whatIt = mCurrentSlicedDurationTrackerMap.erase(whatIt); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 460 | } else { |
tsaichristine | 81899f1 | 2020-01-08 12:42:12 -0800 | [diff] [blame] | 461 | ++whatIt; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 462 | } |
| 463 | } |
Olivier Gaillard | f248c0d | 2019-02-21 15:56:58 +0000 | [diff] [blame] | 464 | StatsdStats::getInstance().noteBucketCount(mMetricId); |
Muhammad Qureshi | 902529a | 2019-03-14 16:03:21 -0700 | [diff] [blame] | 465 | mCurrentBucketStartTimeNs = nextBucketStartTimeNs; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 466 | } |
| 467 | |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 468 | void DurationMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 469 | if (mCurrentSlicedDurationTrackerMap.size() == 0) { |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 470 | return; |
| 471 | } |
| 472 | |
| 473 | fprintf(out, "DurationMetric %lld dimension size %lu\n", (long long)mMetricId, |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 474 | (unsigned long)mCurrentSlicedDurationTrackerMap.size()); |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 475 | if (verbose) { |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 476 | for (const auto& whatIt : mCurrentSlicedDurationTrackerMap) { |
tsaichristine | 81899f1 | 2020-01-08 12:42:12 -0800 | [diff] [blame] | 477 | fprintf(out, "\t(what)%s\n", whatIt.first.toString().c_str()); |
| 478 | whatIt.second->dumpStates(out, verbose); |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 483 | bool DurationMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) { |
Yangster-mac | 306ccc2 | 2018-03-24 15:03:40 -0700 | [diff] [blame] | 484 | auto whatIt = mCurrentSlicedDurationTrackerMap.find(newKey.getDimensionKeyInWhat()); |
tsaichristine | 81899f1 | 2020-01-08 12:42:12 -0800 | [diff] [blame] | 485 | if (whatIt == mCurrentSlicedDurationTrackerMap.end()) { |
Yangster-mac | 306ccc2 | 2018-03-24 15:03:40 -0700 | [diff] [blame] | 486 | // 1. Report the tuple count if the tuple count > soft limit |
| 487 | if (mCurrentSlicedDurationTrackerMap.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) { |
| 488 | size_t newTupleCount = mCurrentSlicedDurationTrackerMap.size() + 1; |
| 489 | StatsdStats::getInstance().noteMetricDimensionSize( |
| 490 | mConfigKey, mMetricId, newTupleCount); |
| 491 | // 2. Don't add more tuples, we are above the allowed threshold. Drop the data. |
| 492 | if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) { |
| 493 | ALOGE("DurationMetric %lld dropping data for what dimension key %s", |
| 494 | (long long)mMetricId, newKey.getDimensionKeyInWhat().toString().c_str()); |
Muhammad Qureshi | 87348a6 | 2019-02-14 16:17:52 -0800 | [diff] [blame] | 495 | StatsdStats::getInstance().noteHardDimensionLimitReached(mMetricId); |
Yangster-mac | 306ccc2 | 2018-03-24 15:03:40 -0700 | [diff] [blame] | 496 | return true; |
| 497 | } |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | return false; |
| 501 | } |
| 502 | |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 503 | void DurationMetricProducer::handleStartEvent(const MetricDimensionKey& eventKey, |
| 504 | const ConditionKey& conditionKeys, |
| 505 | bool condition, const LogEvent& event) { |
| 506 | const auto& whatKey = eventKey.getDimensionKeyInWhat(); |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 507 | auto whatIt = mCurrentSlicedDurationTrackerMap.find(whatKey); |
| 508 | if (whatIt == mCurrentSlicedDurationTrackerMap.end()) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 509 | if (hitGuardRailLocked(eventKey)) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 510 | return; |
| 511 | } |
tsaichristine | 81899f1 | 2020-01-08 12:42:12 -0800 | [diff] [blame] | 512 | mCurrentSlicedDurationTrackerMap[whatKey] = createDurationTracker(eventKey); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 513 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 514 | |
tsaichristine | 81899f1 | 2020-01-08 12:42:12 -0800 | [diff] [blame] | 515 | auto it = mCurrentSlicedDurationTrackerMap.find(whatKey); |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 516 | if (mUseWhatDimensionAsInternalDimension) { |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 517 | it->second->noteStart(whatKey, condition, event.GetElapsedTimestampNs(), conditionKeys); |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 518 | return; |
| 519 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 520 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 521 | if (mInternalDimensions.empty()) { |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 522 | it->second->noteStart(DEFAULT_DIMENSION_KEY, condition, event.GetElapsedTimestampNs(), |
| 523 | conditionKeys); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 524 | } else { |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 525 | HashableDimensionKey dimensionKey = DEFAULT_DIMENSION_KEY; |
| 526 | filterValues(mInternalDimensions, event.getValues(), &dimensionKey); |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 527 | it->second->noteStart(dimensionKey, condition, event.GetElapsedTimestampNs(), |
| 528 | conditionKeys); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 529 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 530 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 533 | void DurationMetricProducer::onMatchedLogEventInternalLocked( |
| 534 | const size_t matcherIndex, const MetricDimensionKey& eventKey, |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 535 | const ConditionKey& conditionKeys, bool condition, const LogEvent& event, |
| 536 | const map<int, HashableDimensionKey>& statePrimaryKeys) { |
Yangster-mac | 5392888 | 2018-02-25 23:02:56 -0800 | [diff] [blame] | 537 | ALOGW("Not used in duration tracker."); |
| 538 | } |
| 539 | |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 540 | void DurationMetricProducer::onMatchedLogEventLocked(const size_t matcherIndex, |
| 541 | const LogEvent& event) { |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 542 | int64_t eventTimeNs = event.GetElapsedTimestampNs(); |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 543 | if (eventTimeNs < mTimeBaseNs) { |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 544 | return; |
| 545 | } |
| 546 | |
Muhammad Qureshi | 18e4692 | 2019-05-24 16:38:49 -0700 | [diff] [blame] | 547 | if (mIsActive) { |
| 548 | flushIfNeededLocked(event.GetElapsedTimestampNs()); |
| 549 | } |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 550 | |
| 551 | // Handles Stopall events. |
| 552 | if (matcherIndex == mStopAllIndex) { |
| 553 | for (auto& whatIt : mCurrentSlicedDurationTrackerMap) { |
tsaichristine | 81899f1 | 2020-01-08 12:42:12 -0800 | [diff] [blame] | 554 | whatIt.second->noteStopAll(event.GetElapsedTimestampNs()); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 555 | } |
| 556 | return; |
| 557 | } |
| 558 | |
tsaichristine | 81899f1 | 2020-01-08 12:42:12 -0800 | [diff] [blame] | 559 | HashableDimensionKey dimensionInWhat = DEFAULT_DIMENSION_KEY; |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 560 | if (!mDimensionsInWhat.empty()) { |
| 561 | filterValues(mDimensionsInWhat, event.getValues(), &dimensionInWhat); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 562 | } |
| 563 | |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 564 | // Stores atom id to primary key pairs for each state atom that the metric is |
| 565 | // sliced by. |
| 566 | std::map<int, HashableDimensionKey> statePrimaryKeys; |
| 567 | |
| 568 | // For states with primary fields, use MetricStateLinks to get the primary |
| 569 | // field values from the log event. These values will form a primary key |
| 570 | // that will be used to query StateTracker for the correct state value. |
| 571 | for (const auto& stateLink : mMetric2StateLinks) { |
| 572 | getDimensionForState(event.getValues(), stateLink, |
| 573 | &statePrimaryKeys[stateLink.stateAtomId]); |
| 574 | } |
| 575 | |
| 576 | // For each sliced state, query StateTracker for the state value using |
| 577 | // either the primary key from the previous step or the DEFAULT_DIMENSION_KEY. |
| 578 | // |
| 579 | // Expected functionality: for any case where the MetricStateLinks are |
| 580 | // initialized incorrectly (ex. # of state links != # of primary fields, no |
| 581 | // links are provided for a state with primary fields, links are provided |
| 582 | // in the wrong order, etc.), StateTracker will simply return kStateUnknown |
| 583 | // when queried using an incorrect key. |
| 584 | HashableDimensionKey stateValuesKey = DEFAULT_DIMENSION_KEY; |
| 585 | for (auto atomId : mSlicedStateAtoms) { |
| 586 | FieldValue value; |
| 587 | if (statePrimaryKeys.find(atomId) != statePrimaryKeys.end()) { |
| 588 | // found a primary key for this state, query using the key |
| 589 | queryStateValue(atomId, statePrimaryKeys[atomId], &value); |
| 590 | } else { |
| 591 | // if no MetricStateLinks exist for this state atom, |
| 592 | // query using the default dimension key (empty HashableDimensionKey) |
| 593 | queryStateValue(atomId, DEFAULT_DIMENSION_KEY, &value); |
| 594 | } |
| 595 | mapStateValue(atomId, &value); |
| 596 | stateValuesKey.addValue(value); |
| 597 | } |
| 598 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 599 | // Handles Stop events. |
| 600 | if (matcherIndex == mStopIndex) { |
| 601 | if (mUseWhatDimensionAsInternalDimension) { |
| 602 | auto whatIt = mCurrentSlicedDurationTrackerMap.find(dimensionInWhat); |
| 603 | if (whatIt != mCurrentSlicedDurationTrackerMap.end()) { |
tsaichristine | 81899f1 | 2020-01-08 12:42:12 -0800 | [diff] [blame] | 604 | whatIt->second->noteStop(dimensionInWhat, event.GetElapsedTimestampNs(), false); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 605 | } |
| 606 | return; |
| 607 | } |
| 608 | |
| 609 | HashableDimensionKey internalDimensionKey = DEFAULT_DIMENSION_KEY; |
| 610 | if (!mInternalDimensions.empty()) { |
| 611 | filterValues(mInternalDimensions, event.getValues(), &internalDimensionKey); |
| 612 | } |
| 613 | |
| 614 | auto whatIt = mCurrentSlicedDurationTrackerMap.find(dimensionInWhat); |
| 615 | if (whatIt != mCurrentSlicedDurationTrackerMap.end()) { |
tsaichristine | 81899f1 | 2020-01-08 12:42:12 -0800 | [diff] [blame] | 616 | whatIt->second->noteStop(internalDimensionKey, event.GetElapsedTimestampNs(), false); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 617 | } |
| 618 | return; |
| 619 | } |
| 620 | |
| 621 | bool condition; |
| 622 | ConditionKey conditionKey; |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 623 | if (mConditionSliced) { |
| 624 | for (const auto& link : mMetric2ConditionLinks) { |
| 625 | getDimensionForCondition(event.getValues(), link, &conditionKey[link.conditionId]); |
| 626 | } |
| 627 | |
| 628 | auto conditionState = |
tsaichristine | 7685337 | 2019-08-06 17:17:03 -0700 | [diff] [blame] | 629 | mWizard->query(mConditionTrackerIndex, conditionKey, |
| 630 | !mHasLinksToAllConditionDimensionsInTracker); |
Olivier Gaillard | e63d9e0 | 2019-02-12 14:43:59 +0000 | [diff] [blame] | 631 | condition = conditionState == ConditionState::kTrue; |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 632 | } else { |
Olivier Gaillard | e63d9e0 | 2019-02-12 14:43:59 +0000 | [diff] [blame] | 633 | // TODO: The unknown condition state is not handled here, we should fix it. |
| 634 | condition = mCondition == ConditionState::kTrue; |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 635 | } |
| 636 | |
Muhammad Qureshi | 18e4692 | 2019-05-24 16:38:49 -0700 | [diff] [blame] | 637 | condition = condition && mIsActive; |
| 638 | |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 639 | handleStartEvent(MetricDimensionKey(dimensionInWhat, stateValuesKey), conditionKey, condition, |
| 640 | event); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 641 | } |
| 642 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 643 | size_t DurationMetricProducer::byteSizeLocked() const { |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 644 | size_t totalSize = 0; |
| 645 | for (const auto& pair : mPastBuckets) { |
| 646 | totalSize += pair.second.size() * kBucketSize; |
| 647 | } |
| 648 | return totalSize; |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 651 | } // namespace statsd |
| 652 | } // namespace os |
| 653 | } // namespace android |