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