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