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 | |
| 17 | #define DEBUG true |
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 | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 21 | #include "stats_util.h" |
| 22 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 23 | #include <limits.h> |
| 24 | #include <stdlib.h> |
| 25 | |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 26 | using android::util::FIELD_COUNT_REPEATED; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 27 | using android::util::FIELD_TYPE_BOOL; |
| 28 | using android::util::FIELD_TYPE_FLOAT; |
| 29 | using android::util::FIELD_TYPE_INT32; |
| 30 | using android::util::FIELD_TYPE_INT64; |
| 31 | using android::util::FIELD_TYPE_MESSAGE; |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 32 | using android::util::FIELD_TYPE_STRING; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 33 | using android::util::ProtoOutputStream; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 34 | using std::string; |
| 35 | using std::unordered_map; |
| 36 | using std::vector; |
| 37 | |
| 38 | namespace android { |
| 39 | namespace os { |
| 40 | namespace statsd { |
| 41 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 42 | // for StatsLogReport |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 43 | const int FIELD_ID_NAME = 1; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 44 | const int FIELD_ID_START_REPORT_NANOS = 2; |
| 45 | const int FIELD_ID_END_REPORT_NANOS = 3; |
| 46 | const int FIELD_ID_DURATION_METRICS = 6; |
| 47 | // for DurationMetricDataWrapper |
| 48 | const int FIELD_ID_DATA = 1; |
| 49 | // for DurationMetricData |
| 50 | const int FIELD_ID_DIMENSION = 1; |
| 51 | const int FIELD_ID_BUCKET_INFO = 2; |
| 52 | // for KeyValuePair |
| 53 | const int FIELD_ID_KEY = 1; |
| 54 | const int FIELD_ID_VALUE_STR = 2; |
| 55 | const int FIELD_ID_VALUE_INT = 3; |
| 56 | const int FIELD_ID_VALUE_BOOL = 4; |
| 57 | const int FIELD_ID_VALUE_FLOAT = 5; |
| 58 | // for DurationBucketInfo |
| 59 | const int FIELD_ID_START_BUCKET_NANOS = 1; |
| 60 | const int FIELD_ID_END_BUCKET_NANOS = 2; |
| 61 | const int FIELD_ID_DURATION = 3; |
| 62 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 63 | DurationMetricProducer::DurationMetricProducer(const DurationMetric& metric, |
| 64 | const int conditionIndex, const size_t startIndex, |
| 65 | const size_t stopIndex, const size_t stopAllIndex, |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 66 | const bool nesting, |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 67 | const sp<ConditionWizard>& wizard, |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 68 | const vector<KeyMatcher>& internalDimension, |
| 69 | const uint64_t startTimeNs) |
| 70 | : MetricProducer(startTimeNs, conditionIndex, wizard), |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 71 | mMetric(metric), |
| 72 | mStartIndex(startIndex), |
| 73 | mStopIndex(stopIndex), |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 74 | mStopAllIndex(stopAllIndex), |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 75 | mNested(nesting), |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 76 | mInternalDimension(internalDimension) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 77 | // TODO: The following boiler plate code appears in all MetricProducers, but we can't abstract |
| 78 | // them in the base class, because the proto generated CountMetric, and DurationMetric are |
| 79 | // not related. Maybe we should add a template in the future?? |
| 80 | if (metric.has_bucket() && metric.bucket().has_bucket_size_millis()) { |
| 81 | mBucketSizeNs = metric.bucket().bucket_size_millis() * 1000000; |
| 82 | } else { |
| 83 | mBucketSizeNs = LLONG_MAX; |
| 84 | } |
| 85 | |
| 86 | // TODO: use UidMap if uid->pkg_name is required |
| 87 | mDimension.insert(mDimension.begin(), metric.dimension().begin(), metric.dimension().end()); |
| 88 | |
| 89 | if (metric.links().size() > 0) { |
| 90 | mConditionLinks.insert(mConditionLinks.begin(), metric.links().begin(), |
| 91 | metric.links().end()); |
| 92 | mConditionSliced = true; |
| 93 | } |
| 94 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 95 | startNewProtoOutputStream(mStartTimeNs); |
| 96 | |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 97 | VLOG("metric %s created. bucket size %lld start_time: %lld", metric.name().c_str(), |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 98 | (long long)mBucketSizeNs, (long long)mStartTimeNs); |
| 99 | } |
| 100 | |
| 101 | DurationMetricProducer::~DurationMetricProducer() { |
| 102 | VLOG("~DurationMetric() called"); |
| 103 | } |
| 104 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 105 | void DurationMetricProducer::startNewProtoOutputStream(long long startTime) { |
| 106 | mProto = std::make_unique<ProtoOutputStream>(); |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 107 | mProto->write(FIELD_TYPE_STRING | FIELD_ID_NAME, mMetric.name()); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 108 | mProto->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, startTime); |
| 109 | mProtoToken = mProto->start(FIELD_TYPE_MESSAGE | FIELD_ID_DURATION_METRICS); |
| 110 | } |
| 111 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 112 | unique_ptr<DurationTracker> DurationMetricProducer::createDurationTracker( |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame^] | 113 | const HashableDimensionKey& eventKey, vector<DurationBucket>& bucket) { |
Stefan Lafon | cfed20b | 2017-11-18 09:26:53 -0800 | [diff] [blame] | 114 | switch (mMetric.aggregation_type()) { |
| 115 | case DurationMetric_AggregationType_SUM: |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame^] | 116 | return make_unique<OringDurationTracker>(eventKey, mWizard, mConditionTrackerIndex, |
| 117 | mNested, mCurrentBucketStartTimeNs, |
| 118 | mBucketSizeNs, mAnomalyTrackers, bucket); |
Stefan Lafon | cfed20b | 2017-11-18 09:26:53 -0800 | [diff] [blame] | 119 | case DurationMetric_AggregationType_MAX_SPARSE: |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame^] | 120 | return make_unique<MaxDurationTracker>(eventKey, mWizard, mConditionTrackerIndex, |
| 121 | mNested, mCurrentBucketStartTimeNs, |
| 122 | mBucketSizeNs, mAnomalyTrackers, bucket); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 126 | void DurationMetricProducer::finish() { |
| 127 | // TODO: write the StatsLogReport to dropbox using |
| 128 | // DropboxWriter. |
| 129 | } |
| 130 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 131 | void DurationMetricProducer::onSlicedConditionMayChange(const uint64_t eventTime) { |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 132 | VLOG("Metric %s onSlicedConditionMayChange", mMetric.name().c_str()); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 133 | // Now for each of the on-going event, check if the condition has changed for them. |
| 134 | for (auto& pair : mCurrentSlicedDuration) { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 135 | pair.second->onSlicedConditionMayChange(eventTime); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 139 | void DurationMetricProducer::onConditionChanged(const bool conditionMet, const uint64_t eventTime) { |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 140 | VLOG("Metric %s onConditionChanged", mMetric.name().c_str()); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 141 | mCondition = conditionMet; |
| 142 | // TODO: need to populate the condition change time from the event which triggers the condition |
| 143 | // change, instead of using current time. |
| 144 | for (auto& pair : mCurrentSlicedDuration) { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 145 | pair.second->onConditionChanged(conditionMet, eventTime); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 149 | std::unique_ptr<std::vector<uint8_t>> DurationMetricProducer::onDumpReport() { |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 150 | long long endTime = time(nullptr) * NS_PER_SEC; |
| 151 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 152 | // Dump current bucket if it's stale. |
| 153 | // If current bucket is still on-going, don't force dump current bucket. |
| 154 | // In finish(), We can force dump current bucket. |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 155 | flushIfNeeded(endTime); |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 156 | VLOG("metric %s dump report now...", mMetric.name().c_str()); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 157 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 158 | for (const auto& pair : mPastBuckets) { |
| 159 | const HashableDimensionKey& hashableKey = pair.first; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 160 | VLOG(" dimension key %s", hashableKey.c_str()); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 161 | auto it = mDimensionKeyMap.find(hashableKey); |
| 162 | if (it == mDimensionKeyMap.end()) { |
| 163 | ALOGW("Dimension key %s not found?!?! skip...", hashableKey.c_str()); |
| 164 | continue; |
| 165 | } |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 166 | long long wrapperToken = |
| 167 | mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 168 | |
| 169 | // First fill dimension (KeyValuePairs). |
| 170 | for (const auto& kv : it->second) { |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 171 | long long dimensionToken = |
| 172 | mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DIMENSION); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 173 | mProto->write(FIELD_TYPE_INT32 | FIELD_ID_KEY, kv.key()); |
| 174 | if (kv.has_value_str()) { |
| 175 | mProto->write(FIELD_TYPE_INT32 | FIELD_ID_VALUE_STR, kv.value_str()); |
| 176 | } else if (kv.has_value_int()) { |
| 177 | mProto->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE_INT, kv.value_int()); |
| 178 | } else if (kv.has_value_bool()) { |
| 179 | mProto->write(FIELD_TYPE_BOOL | FIELD_ID_VALUE_BOOL, kv.value_bool()); |
| 180 | } else if (kv.has_value_float()) { |
| 181 | mProto->write(FIELD_TYPE_FLOAT | FIELD_ID_VALUE_FLOAT, kv.value_float()); |
| 182 | } |
| 183 | mProto->end(dimensionToken); |
| 184 | } |
| 185 | |
| 186 | // Then fill bucket_info (DurationBucketInfo). |
| 187 | for (const auto& bucket : pair.second) { |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 188 | long long bucketInfoToken = |
| 189 | mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 190 | mProto->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS, |
| 191 | (long long)bucket.mBucketStartNs); |
| 192 | mProto->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS, |
| 193 | (long long)bucket.mBucketEndNs); |
| 194 | mProto->write(FIELD_TYPE_INT64 | FIELD_ID_DURATION, (long long)bucket.mDuration); |
| 195 | mProto->end(bucketInfoToken); |
| 196 | VLOG("\t bucket [%lld - %lld] duration: %lld", (long long)bucket.mBucketStartNs, |
| 197 | (long long)bucket.mBucketEndNs, (long long)bucket.mDuration); |
| 198 | } |
| 199 | |
| 200 | mProto->end(wrapperToken); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 201 | } |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 202 | |
| 203 | mProto->end(mProtoToken); |
| 204 | mProto->write(FIELD_TYPE_INT64 | FIELD_ID_END_REPORT_NANOS, |
| 205 | (long long)mCurrentBucketStartTimeNs); |
| 206 | |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 207 | std::unique_ptr<std::vector<uint8_t>> buffer = serializeProto(); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 208 | startNewProtoOutputStream(endTime); |
Yao Chen | 30b6a20 | 2017-11-18 14:14:38 -0800 | [diff] [blame] | 209 | // TODO: Properly clear the old buckets. |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 210 | return buffer; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 211 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 212 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 213 | void DurationMetricProducer::flushIfNeeded(uint64_t eventTime) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 214 | if (mCurrentBucketStartTimeNs + mBucketSizeNs > eventTime) { |
| 215 | return; |
| 216 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 217 | VLOG("flushing..........."); |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 218 | for (auto it = mCurrentSlicedDuration.begin(); it != mCurrentSlicedDuration.end();) { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 219 | if (it->second->flushIfNeeded(eventTime)) { |
| 220 | VLOG("erase bucket for key %s", it->first.c_str()); |
Yao Chen | d41c422 | 2017-11-15 19:26:14 -0800 | [diff] [blame] | 221 | it = mCurrentSlicedDuration.erase(it); |
| 222 | } else { |
| 223 | ++it; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 226 | |
| 227 | int numBucketsForward = (eventTime - mCurrentBucketStartTimeNs) / mBucketSizeNs; |
| 228 | mCurrentBucketStartTimeNs += numBucketsForward * mBucketSizeNs; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame^] | 229 | mCurrentBucketNum += numBucketsForward; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void DurationMetricProducer::onMatchedLogEventInternal( |
| 233 | const size_t matcherIndex, const HashableDimensionKey& eventKey, |
| 234 | const map<string, HashableDimensionKey>& conditionKeys, bool condition, |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 235 | const LogEvent& event, bool scheduledPull) { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 236 | flushIfNeeded(event.GetTimestampNs()); |
| 237 | |
| 238 | if (matcherIndex == mStopAllIndex) { |
| 239 | for (auto& pair : mCurrentSlicedDuration) { |
| 240 | pair.second->noteStopAll(event.GetTimestampNs()); |
| 241 | } |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | HashableDimensionKey atomKey = getHashableKey(getDimensionKey(event, mInternalDimension)); |
| 246 | |
| 247 | if (mCurrentSlicedDuration.find(eventKey) == mCurrentSlicedDuration.end()) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame^] | 248 | mCurrentSlicedDuration[eventKey] = createDurationTracker(eventKey, mPastBuckets[eventKey]); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | auto it = mCurrentSlicedDuration.find(eventKey); |
| 252 | |
| 253 | if (matcherIndex == mStartIndex) { |
| 254 | it->second->noteStart(atomKey, condition, event.GetTimestampNs(), conditionKeys); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 255 | } else if (matcherIndex == mStopIndex) { |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 256 | it->second->noteStop(atomKey, event.GetTimestampNs(), false); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 257 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 258 | } |
| 259 | |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 260 | size_t DurationMetricProducer::byteSize() { |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 261 | size_t totalSize = 0; |
| 262 | for (const auto& pair : mPastBuckets) { |
| 263 | totalSize += pair.second.size() * kBucketSize; |
| 264 | } |
| 265 | return totalSize; |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 268 | } // namespace statsd |
| 269 | } // namespace os |
| 270 | } // namespace android |