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