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; |
| 39 | |
| 40 | namespace android { |
| 41 | namespace os { |
| 42 | namespace statsd { |
| 43 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 44 | // for StatsLogReport |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 45 | const int FIELD_ID_ID = 1; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 46 | const int FIELD_ID_DURATION_METRICS = 6; |
| 47 | // for DurationMetricDataWrapper |
| 48 | const int FIELD_ID_DATA = 1; |
| 49 | // for DurationMetricData |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 50 | const int FIELD_ID_DIMENSION_IN_WHAT = 1; |
| 51 | const int FIELD_ID_DIMENSION_IN_CONDITION = 2; |
| 52 | const int FIELD_ID_BUCKET_INFO = 3; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 53 | // for DurationBucketInfo |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 54 | const int FIELD_ID_START_BUCKET_ELAPSED_NANOS = 1; |
| 55 | const int FIELD_ID_END_BUCKET_ELAPSED_NANOS = 2; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 56 | const int FIELD_ID_DURATION = 3; |
| 57 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 58 | DurationMetricProducer::DurationMetricProducer(const ConfigKey& key, const DurationMetric& metric, |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 59 | const int conditionIndex, const size_t startIndex, |
| 60 | const size_t stopIndex, const size_t stopAllIndex, |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 61 | const bool nesting, |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 62 | const sp<ConditionWizard>& wizard, |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 63 | const FieldMatcher& internalDimensions, |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 64 | const uint64_t startTimeNs) |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 65 | : MetricProducer(metric.id(), key, startTimeNs, conditionIndex, wizard), |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 66 | mAggregationType(metric.aggregation_type()), |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 67 | mStartIndex(startIndex), |
| 68 | mStopIndex(stopIndex), |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 69 | mStopAllIndex(stopAllIndex), |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 70 | mNested(nesting) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 71 | // TODO: The following boiler plate code appears in all MetricProducers, but we can't abstract |
| 72 | // them in the base class, because the proto generated CountMetric, and DurationMetric are |
| 73 | // not related. Maybe we should add a template in the future?? |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 74 | if (metric.has_bucket()) { |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 75 | mBucketSizeNs = |
| 76 | TimeUnitToBucketSizeInMillisGuardrailed(key.GetUid(), metric.bucket()) * 1000000; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 77 | } else { |
| 78 | mBucketSizeNs = LLONG_MAX; |
| 79 | } |
| 80 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 81 | if (metric.has_dimensions_in_what()) { |
| 82 | translateFieldMatcher(metric.dimensions_in_what(), &mDimensionsInWhat); |
| 83 | } |
| 84 | |
| 85 | if (internalDimensions.has_field()) { |
| 86 | translateFieldMatcher(internalDimensions, &mInternalDimensions); |
| 87 | } |
| 88 | |
| 89 | if (metric.has_dimensions_in_condition()) { |
| 90 | translateFieldMatcher(metric.dimensions_in_condition(), &mDimensionsInCondition); |
| 91 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 92 | |
| 93 | if (metric.links().size() > 0) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 94 | for (const auto& link : metric.links()) { |
| 95 | Metric2Condition mc; |
| 96 | mc.conditionId = link.condition(); |
| 97 | translateFieldMatcher(link.fields_in_what(), &mc.metricFields); |
| 98 | translateFieldMatcher(link.fields_in_condition(), &mc.conditionFields); |
| 99 | mMetric2ConditionLinks.push_back(mc); |
| 100 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 101 | } |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 102 | mConditionSliced = (metric.links().size() > 0) || (mDimensionsInCondition.size() > 0); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 103 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 104 | VLOG("metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(), |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 105 | (long long)mBucketSizeNs, (long long)mStartTimeNs); |
| 106 | } |
| 107 | |
| 108 | DurationMetricProducer::~DurationMetricProducer() { |
| 109 | VLOG("~DurationMetric() called"); |
| 110 | } |
| 111 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame^] | 112 | sp<AnomalyTracker> DurationMetricProducer::addAnomalyTracker( |
| 113 | const Alert &alert, const sp<AlarmMonitor>& anomalyAlarmMonitor) { |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 114 | std::lock_guard<std::mutex> lock(mMutex); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame^] | 115 | sp<DurationAnomalyTracker> anomalyTracker = |
| 116 | new DurationAnomalyTracker(alert, mConfigKey, anomalyAlarmMonitor); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 117 | if (anomalyTracker != nullptr) { |
| 118 | mAnomalyTrackers.push_back(anomalyTracker); |
| 119 | } |
| 120 | return anomalyTracker; |
Bookatz | 450099d | 2017-11-30 17:09:30 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 123 | unique_ptr<DurationTracker> DurationMetricProducer::createDurationTracker( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 124 | const MetricDimensionKey& eventKey) const { |
Yao Chen | f09569f | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 125 | switch (mAggregationType) { |
Stefan Lafon | cfed20b | 2017-11-18 09:26:53 -0800 | [diff] [blame] | 126 | case DurationMetric_AggregationType_SUM: |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 127 | return make_unique<OringDurationTracker>( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 128 | mConfigKey, mMetricId, eventKey, mWizard, mConditionTrackerIndex, |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 129 | mDimensionsInCondition, mNested, mCurrentBucketStartTimeNs, mCurrentBucketNum, |
| 130 | mStartTimeNs, mBucketSizeNs, mConditionSliced, mAnomalyTrackers); |
Stefan Lafon | cfed20b | 2017-11-18 09:26:53 -0800 | [diff] [blame] | 131 | case DurationMetric_AggregationType_MAX_SPARSE: |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 132 | return make_unique<MaxDurationTracker>( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 133 | mConfigKey, mMetricId, eventKey, mWizard, mConditionTrackerIndex, |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 134 | mDimensionsInCondition, mNested, mCurrentBucketStartTimeNs, mCurrentBucketNum, |
| 135 | mStartTimeNs, mBucketSizeNs, mConditionSliced, mAnomalyTrackers); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 139 | void DurationMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 140 | VLOG("Metric %lld onSlicedConditionMayChange", (long long)mMetricId); |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 141 | flushIfNeededLocked(eventTime); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 142 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 143 | // Now for each of the on-going event, check if the condition has changed for them. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 144 | for (auto& pair : mCurrentSlicedDurationTrackerMap) { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 145 | pair.second->onSlicedConditionMayChange(eventTime); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 146 | } |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 147 | |
| 148 | |
| 149 | std::unordered_set<HashableDimensionKey> conditionDimensionsKeySet; |
Yao Chen | 4c959cb | 2018-02-13 13:27:48 -0800 | [diff] [blame] | 150 | mWizard->getMetConditionDimension(mConditionTrackerIndex, mDimensionsInCondition, |
| 151 | &conditionDimensionsKeySet); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 152 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 153 | for (auto& pair : mCurrentSlicedDurationTrackerMap) { |
| 154 | conditionDimensionsKeySet.erase(pair.first.getDimensionKeyInCondition()); |
| 155 | } |
| 156 | std::unordered_set<MetricDimensionKey> newKeys; |
| 157 | for (const auto& conditionDimensionsKey : conditionDimensionsKeySet) { |
| 158 | for (auto& pair : mCurrentSlicedDurationTrackerMap) { |
| 159 | auto newKey = |
| 160 | MetricDimensionKey(pair.first.getDimensionKeyInWhat(), conditionDimensionsKey); |
| 161 | if (newKeys.find(newKey) == newKeys.end()) { |
| 162 | mCurrentSlicedDurationTrackerMap[newKey] = pair.second->clone(eventTime); |
| 163 | mCurrentSlicedDurationTrackerMap[newKey]->setEventKey(newKey); |
| 164 | mCurrentSlicedDurationTrackerMap[newKey]->onSlicedConditionMayChange(eventTime); |
| 165 | } |
| 166 | newKeys.insert(newKey); |
| 167 | } |
| 168 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 171 | void DurationMetricProducer::onConditionChangedLocked(const bool conditionMet, |
| 172 | const uint64_t eventTime) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 173 | VLOG("Metric %lld onConditionChanged", (long long)mMetricId); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 174 | mCondition = conditionMet; |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 175 | flushIfNeededLocked(eventTime); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 176 | // TODO: need to populate the condition change time from the event which triggers the condition |
| 177 | // change, instead of using current time. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 178 | for (auto& pair : mCurrentSlicedDurationTrackerMap) { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 179 | pair.second->onConditionChanged(conditionMet, eventTime); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 183 | void DurationMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs, |
| 184 | ProtoOutputStream* protoOutput) { |
| 185 | flushIfNeededLocked(dumpTimeNs); |
Yangster-mac | 635b4b3 | 2018-01-23 20:17:35 -0800 | [diff] [blame] | 186 | if (mPastBuckets.empty()) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 187 | VLOG(" Duration metric, empty return"); |
Yangster-mac | 635b4b3 | 2018-01-23 20:17:35 -0800 | [diff] [blame] | 188 | return; |
| 189 | } |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 190 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 191 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 192 | long long protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_DURATION_METRICS); |
| 193 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 194 | VLOG("Duration metric %lld dump report now...", (long long)mMetricId); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 195 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 196 | for (const auto& pair : mPastBuckets) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 197 | const MetricDimensionKey& dimensionKey = pair.first; |
| 198 | VLOG(" dimension key %s", dimensionKey.c_str()); |
Yao Chen | 1ff4f43 | 2017-11-16 17:01:40 -0800 | [diff] [blame] | 199 | |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 200 | long long wrapperToken = |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 201 | protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 202 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 203 | // First fill dimension. |
| 204 | long long dimensionToken = protoOutput->start( |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 205 | FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_WHAT); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 206 | writeDimensionToProto(dimensionKey.getDimensionKeyInWhat(), protoOutput); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 207 | protoOutput->end(dimensionToken); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 208 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 209 | if (dimensionKey.hasDimensionKeyInCondition()) { |
| 210 | long long dimensionInConditionToken = protoOutput->start( |
| 211 | FIELD_TYPE_MESSAGE | FIELD_ID_DIMENSION_IN_CONDITION); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 212 | writeDimensionToProto(dimensionKey.getDimensionKeyInCondition(), protoOutput); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 213 | protoOutput->end(dimensionInConditionToken); |
| 214 | } |
| 215 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 216 | // Then fill bucket_info (DurationBucketInfo). |
| 217 | for (const auto& bucket : pair.second) { |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 218 | long long bucketInfoToken = protoOutput->start( |
| 219 | FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 220 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_ELAPSED_NANOS, |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 221 | (long long)bucket.mBucketStartNs); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 222 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_ELAPSED_NANOS, |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 223 | (long long)bucket.mBucketEndNs); |
| 224 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_DURATION, (long long)bucket.mDuration); |
| 225 | protoOutput->end(bucketInfoToken); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 226 | VLOG("\t bucket [%lld - %lld] duration: %lld", (long long)bucket.mBucketStartNs, |
| 227 | (long long)bucket.mBucketEndNs, (long long)bucket.mDuration); |
| 228 | } |
| 229 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 230 | protoOutput->end(wrapperToken); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 231 | } |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 232 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 233 | protoOutput->end(protoToken); |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 234 | mPastBuckets.clear(); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 235 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 236 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 237 | void DurationMetricProducer::flushIfNeededLocked(const uint64_t& eventTimeNs) { |
| 238 | uint64_t currentBucketEndTimeNs = getCurrentBucketEndTimeNs(); |
| 239 | |
| 240 | if (currentBucketEndTimeNs > eventTimeNs) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 241 | return; |
| 242 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 243 | VLOG("flushing..........."); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 244 | for (auto it = mCurrentSlicedDurationTrackerMap.begin(); |
| 245 | it != mCurrentSlicedDurationTrackerMap.end();) { |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 246 | if (it->second->flushIfNeeded(eventTimeNs, &mPastBuckets)) { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 247 | VLOG("erase bucket for key %s", it->first.c_str()); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 248 | it = mCurrentSlicedDurationTrackerMap.erase(it); |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 249 | } else { |
| 250 | ++it; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 251 | } |
| 252 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 253 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 254 | int numBucketsForward = 1 + (eventTimeNs - currentBucketEndTimeNs) / mBucketSizeNs; |
| 255 | mCurrentBucketStartTimeNs = currentBucketEndTimeNs + (numBucketsForward - 1) * mBucketSizeNs; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 256 | mCurrentBucketNum += numBucketsForward; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 257 | } |
| 258 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 259 | void DurationMetricProducer::flushCurrentBucketLocked(const uint64_t& eventTimeNs) { |
| 260 | for (auto it = mCurrentSlicedDurationTrackerMap.begin(); |
| 261 | it != mCurrentSlicedDurationTrackerMap.end();) { |
| 262 | if (it->second->flushCurrentBucket(eventTimeNs, &mPastBuckets)) { |
| 263 | VLOG("erase bucket for key %s", it->first.c_str()); |
| 264 | it = mCurrentSlicedDurationTrackerMap.erase(it); |
| 265 | } else { |
| 266 | ++it; |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 271 | void DurationMetricProducer::dumpStatesLocked(FILE* out, bool verbose) const { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 272 | if (mCurrentSlicedDurationTrackerMap.size() == 0) { |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 273 | return; |
| 274 | } |
| 275 | |
| 276 | fprintf(out, "DurationMetric %lld dimension size %lu\n", (long long)mMetricId, |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 277 | (unsigned long)mCurrentSlicedDurationTrackerMap.size()); |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 278 | if (verbose) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 279 | for (const auto& slice : mCurrentSlicedDurationTrackerMap) { |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 280 | fprintf(out, "\t%s\n", slice.first.c_str()); |
| 281 | slice.second->dumpStates(out, verbose); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 286 | bool DurationMetricProducer::hitGuardRailLocked(const MetricDimensionKey& newKey) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 287 | // the key is not new, we are good. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 288 | if (mCurrentSlicedDurationTrackerMap.find(newKey) != mCurrentSlicedDurationTrackerMap.end()) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 289 | return false; |
| 290 | } |
| 291 | // 1. Report the tuple count if the tuple count > soft limit |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 292 | if (mCurrentSlicedDurationTrackerMap.size() > StatsdStats::kDimensionKeySizeSoftLimit - 1) { |
| 293 | size_t newTupleCount = mCurrentSlicedDurationTrackerMap.size() + 1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 294 | StatsdStats::getInstance().noteMetricDimensionSize(mConfigKey, mMetricId, newTupleCount); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 295 | // 2. Don't add more tuples, we are above the allowed threshold. Drop the data. |
| 296 | if (newTupleCount > StatsdStats::kDimensionKeySizeHardLimit) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 297 | ALOGE("DurationMetric %lld dropping data for dimension key %s", |
| 298 | (long long)mMetricId, newKey.c_str()); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 299 | return true; |
| 300 | } |
| 301 | } |
| 302 | return false; |
| 303 | } |
| 304 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 305 | void DurationMetricProducer::onMatchedLogEventInternalLocked( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 306 | const size_t matcherIndex, const MetricDimensionKey& eventKey, |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 307 | const ConditionKey& conditionKeys, bool condition, |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 308 | const LogEvent& event) { |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 309 | flushIfNeededLocked(event.GetElapsedTimestampNs()); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 310 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 311 | if (matcherIndex == mStopAllIndex) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 312 | for (auto& pair : mCurrentSlicedDurationTrackerMap) { |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 313 | pair.second->noteStopAll(event.GetElapsedTimestampNs()); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 314 | } |
| 315 | return; |
| 316 | } |
| 317 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 318 | if (mCurrentSlicedDurationTrackerMap.find(eventKey) == mCurrentSlicedDurationTrackerMap.end()) { |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 319 | if (hitGuardRailLocked(eventKey)) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 320 | return; |
| 321 | } |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 322 | mCurrentSlicedDurationTrackerMap[eventKey] = createDurationTracker(eventKey); |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 323 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 324 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 325 | auto it = mCurrentSlicedDurationTrackerMap.find(eventKey); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 326 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 327 | std::vector<HashableDimensionKey> values; |
| 328 | filterValues(mInternalDimensions, event.getValues(), &values); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 329 | if (values.empty()) { |
| 330 | if (matcherIndex == mStartIndex) { |
| 331 | it->second->noteStart(DEFAULT_DIMENSION_KEY, condition, |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 332 | event.GetElapsedTimestampNs(), conditionKeys); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 333 | } else if (matcherIndex == mStopIndex) { |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 334 | it->second->noteStop(DEFAULT_DIMENSION_KEY, event.GetElapsedTimestampNs(), false); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 335 | } |
| 336 | } else { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 337 | for (const auto& value : values) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 338 | if (matcherIndex == mStartIndex) { |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 339 | it->second->noteStart(value, condition, event.GetElapsedTimestampNs(), conditionKeys); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 340 | } else if (matcherIndex == mStopIndex) { |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 341 | it->second->noteStop(value, event.GetElapsedTimestampNs(), false); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 342 | } |
| 343 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 344 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 345 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 348 | size_t DurationMetricProducer::byteSizeLocked() const { |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 349 | size_t totalSize = 0; |
| 350 | for (const auto& pair : mPastBuckets) { |
| 351 | totalSize += pair.second.size() * kBucketSize; |
| 352 | } |
| 353 | return totalSize; |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 356 | } // namespace statsd |
| 357 | } // namespace os |
| 358 | } // namespace android |