blob: 0ee7dcdf3c2ecdc59c506832f9b192aae360da6a [file] [log] [blame]
Yao Chen729093d2017-10-16 10:33:26 -07001/*
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 Chen5154a372017-10-30 22:57:06 -070018
Yao Chen729093d2017-10-16 10:33:26 -070019#include "Log.h"
Yao Chen5154a372017-10-30 22:57:06 -070020#include "DurationMetricProducer.h"
Yao Chen729093d2017-10-16 10:33:26 -070021#include "stats_util.h"
22
Yao Chen729093d2017-10-16 10:33:26 -070023#include <limits.h>
24#include <stdlib.h>
25
yrob0378b02017-11-09 20:36:25 -080026using android::util::FIELD_COUNT_REPEATED;
yro2b0f8862017-11-06 14:27:31 -080027using android::util::FIELD_TYPE_BOOL;
28using android::util::FIELD_TYPE_FLOAT;
29using android::util::FIELD_TYPE_INT32;
30using android::util::FIELD_TYPE_INT64;
31using android::util::FIELD_TYPE_MESSAGE;
Yangster-macd1815dc2017-11-13 21:43:15 -080032using android::util::FIELD_TYPE_STRING;
yro2b0f8862017-11-06 14:27:31 -080033using android::util::ProtoOutputStream;
Yao Chen729093d2017-10-16 10:33:26 -070034using std::string;
35using std::unordered_map;
36using std::vector;
37
38namespace android {
39namespace os {
40namespace statsd {
41
yro2b0f8862017-11-06 14:27:31 -080042// for StatsLogReport
Yangster-macd1815dc2017-11-13 21:43:15 -080043const int FIELD_ID_NAME = 1;
yro2b0f8862017-11-06 14:27:31 -080044const int FIELD_ID_START_REPORT_NANOS = 2;
45const int FIELD_ID_END_REPORT_NANOS = 3;
46const int FIELD_ID_DURATION_METRICS = 6;
47// for DurationMetricDataWrapper
48const int FIELD_ID_DATA = 1;
49// for DurationMetricData
50const int FIELD_ID_DIMENSION = 1;
51const int FIELD_ID_BUCKET_INFO = 2;
52// for KeyValuePair
53const int FIELD_ID_KEY = 1;
54const int FIELD_ID_VALUE_STR = 2;
55const int FIELD_ID_VALUE_INT = 3;
56const int FIELD_ID_VALUE_BOOL = 4;
57const int FIELD_ID_VALUE_FLOAT = 5;
58// for DurationBucketInfo
59const int FIELD_ID_START_BUCKET_NANOS = 1;
60const int FIELD_ID_END_BUCKET_NANOS = 2;
61const int FIELD_ID_DURATION = 3;
62
Yao Chen729093d2017-10-16 10:33:26 -070063DurationMetricProducer::DurationMetricProducer(const DurationMetric& metric,
64 const int conditionIndex, const size_t startIndex,
65 const size_t stopIndex, const size_t stopAllIndex,
Yao Chen0ea19902017-11-15 15:44:45 -080066 const bool nesting,
Yao Chen5154a372017-10-30 22:57:06 -070067 const sp<ConditionWizard>& wizard,
Yao Chen93fe3a32017-11-02 13:52:59 -070068 const vector<KeyMatcher>& internalDimension,
69 const uint64_t startTimeNs)
70 : MetricProducer(startTimeNs, conditionIndex, wizard),
Yao Chen729093d2017-10-16 10:33:26 -070071 mMetric(metric),
72 mStartIndex(startIndex),
73 mStopIndex(stopIndex),
Yao Chen5154a372017-10-30 22:57:06 -070074 mStopAllIndex(stopAllIndex),
Yao Chen0ea19902017-11-15 15:44:45 -080075 mNested(nesting),
Yao Chen5154a372017-10-30 22:57:06 -070076 mInternalDimension(internalDimension) {
Yao Chen729093d2017-10-16 10:33:26 -070077 // 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
yro2b0f8862017-11-06 14:27:31 -080095 startNewProtoOutputStream(mStartTimeNs);
96
Yangster-macd1815dc2017-11-13 21:43:15 -080097 VLOG("metric %s created. bucket size %lld start_time: %lld", metric.name().c_str(),
Yao Chen729093d2017-10-16 10:33:26 -070098 (long long)mBucketSizeNs, (long long)mStartTimeNs);
99}
100
101DurationMetricProducer::~DurationMetricProducer() {
102 VLOG("~DurationMetric() called");
103}
104
yro2b0f8862017-11-06 14:27:31 -0800105void DurationMetricProducer::startNewProtoOutputStream(long long startTime) {
106 mProto = std::make_unique<ProtoOutputStream>();
Yangster-macd1815dc2017-11-13 21:43:15 -0800107 mProto->write(FIELD_TYPE_STRING | FIELD_ID_NAME, mMetric.name());
yro2b0f8862017-11-06 14:27:31 -0800108 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 Chen5154a372017-10-30 22:57:06 -0700112unique_ptr<DurationTracker> DurationMetricProducer::createDurationTracker(
Yangster-mace2cd6d52017-11-09 20:38:30 -0800113 const HashableDimensionKey& eventKey, vector<DurationBucket>& bucket) {
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800114 switch (mMetric.aggregation_type()) {
115 case DurationMetric_AggregationType_SUM:
Yangster-mace2cd6d52017-11-09 20:38:30 -0800116 return make_unique<OringDurationTracker>(eventKey, mWizard, mConditionTrackerIndex,
117 mNested, mCurrentBucketStartTimeNs,
118 mBucketSizeNs, mAnomalyTrackers, bucket);
Stefan Lafoncfed20b2017-11-18 09:26:53 -0800119 case DurationMetric_AggregationType_MAX_SPARSE:
Yangster-mace2cd6d52017-11-09 20:38:30 -0800120 return make_unique<MaxDurationTracker>(eventKey, mWizard, mConditionTrackerIndex,
121 mNested, mCurrentBucketStartTimeNs,
122 mBucketSizeNs, mAnomalyTrackers, bucket);
Yao Chen5154a372017-10-30 22:57:06 -0700123 }
124}
125
Yao Chen729093d2017-10-16 10:33:26 -0700126void DurationMetricProducer::finish() {
127 // TODO: write the StatsLogReport to dropbox using
128 // DropboxWriter.
129}
130
Yao Chen5154a372017-10-30 22:57:06 -0700131void DurationMetricProducer::onSlicedConditionMayChange(const uint64_t eventTime) {
Yangster-macd1815dc2017-11-13 21:43:15 -0800132 VLOG("Metric %s onSlicedConditionMayChange", mMetric.name().c_str());
Yao Chen729093d2017-10-16 10:33:26 -0700133 // Now for each of the on-going event, check if the condition has changed for them.
134 for (auto& pair : mCurrentSlicedDuration) {
Yao Chen5154a372017-10-30 22:57:06 -0700135 pair.second->onSlicedConditionMayChange(eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700136 }
137}
138
Yao Chen5154a372017-10-30 22:57:06 -0700139void DurationMetricProducer::onConditionChanged(const bool conditionMet, const uint64_t eventTime) {
Yangster-macd1815dc2017-11-13 21:43:15 -0800140 VLOG("Metric %s onConditionChanged", mMetric.name().c_str());
Yao Chen729093d2017-10-16 10:33:26 -0700141 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 Chen5154a372017-10-30 22:57:06 -0700145 pair.second->onConditionChanged(conditionMet, eventTime);
Yao Chen729093d2017-10-16 10:33:26 -0700146 }
147}
148
yro17adac92017-11-08 23:16:29 -0800149std::unique_ptr<std::vector<uint8_t>> DurationMetricProducer::onDumpReport() {
yro2b0f8862017-11-06 14:27:31 -0800150 long long endTime = time(nullptr) * NS_PER_SEC;
151
Yao Chen729093d2017-10-16 10:33:26 -0700152 // 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.
yro2b0f8862017-11-06 14:27:31 -0800155 flushIfNeeded(endTime);
Yangster-macd1815dc2017-11-13 21:43:15 -0800156 VLOG("metric %s dump report now...", mMetric.name().c_str());
Yao Chen729093d2017-10-16 10:33:26 -0700157
Yao Chen729093d2017-10-16 10:33:26 -0700158 for (const auto& pair : mPastBuckets) {
159 const HashableDimensionKey& hashableKey = pair.first;
yro2b0f8862017-11-06 14:27:31 -0800160 VLOG(" dimension key %s", hashableKey.c_str());
Yao Chen729093d2017-10-16 10:33:26 -0700161 auto it = mDimensionKeyMap.find(hashableKey);
162 if (it == mDimensionKeyMap.end()) {
163 ALOGW("Dimension key %s not found?!?! skip...", hashableKey.c_str());
164 continue;
165 }
Yao Chen1ff4f432017-11-16 17:01:40 -0800166
167 // If there is no duration bucket info for this key, don't include it in the report.
168 // For example, duration started, but condition is never turned to true.
169 // TODO: Only add the key to the map when we add duration buckets info for it.
170 if (pair.second.size() == 0) {
171 continue;
172 }
173
yrob0378b02017-11-09 20:36:25 -0800174 long long wrapperToken =
175 mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
yro2b0f8862017-11-06 14:27:31 -0800176
177 // First fill dimension (KeyValuePairs).
178 for (const auto& kv : it->second) {
yrob0378b02017-11-09 20:36:25 -0800179 long long dimensionToken =
180 mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DIMENSION);
yro2b0f8862017-11-06 14:27:31 -0800181 mProto->write(FIELD_TYPE_INT32 | FIELD_ID_KEY, kv.key());
182 if (kv.has_value_str()) {
Yao Chen1ff4f432017-11-16 17:01:40 -0800183 mProto->write(FIELD_TYPE_STRING | FIELD_ID_VALUE_STR, kv.value_str());
yro2b0f8862017-11-06 14:27:31 -0800184 } else if (kv.has_value_int()) {
185 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE_INT, kv.value_int());
186 } else if (kv.has_value_bool()) {
187 mProto->write(FIELD_TYPE_BOOL | FIELD_ID_VALUE_BOOL, kv.value_bool());
188 } else if (kv.has_value_float()) {
189 mProto->write(FIELD_TYPE_FLOAT | FIELD_ID_VALUE_FLOAT, kv.value_float());
190 }
191 mProto->end(dimensionToken);
192 }
193
194 // Then fill bucket_info (DurationBucketInfo).
195 for (const auto& bucket : pair.second) {
yrob0378b02017-11-09 20:36:25 -0800196 long long bucketInfoToken =
197 mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
yro2b0f8862017-11-06 14:27:31 -0800198 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS,
199 (long long)bucket.mBucketStartNs);
200 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS,
201 (long long)bucket.mBucketEndNs);
202 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_DURATION, (long long)bucket.mDuration);
203 mProto->end(bucketInfoToken);
204 VLOG("\t bucket [%lld - %lld] duration: %lld", (long long)bucket.mBucketStartNs,
205 (long long)bucket.mBucketEndNs, (long long)bucket.mDuration);
206 }
207
208 mProto->end(wrapperToken);
Yao Chen729093d2017-10-16 10:33:26 -0700209 }
yro2b0f8862017-11-06 14:27:31 -0800210
211 mProto->end(mProtoToken);
212 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_END_REPORT_NANOS,
213 (long long)mCurrentBucketStartTimeNs);
yro17adac92017-11-08 23:16:29 -0800214 std::unique_ptr<std::vector<uint8_t>> buffer = serializeProto();
yro2b0f8862017-11-06 14:27:31 -0800215 startNewProtoOutputStream(endTime);
Yao Chen30b6a202017-11-18 14:14:38 -0800216 // TODO: Properly clear the old buckets.
yro17adac92017-11-08 23:16:29 -0800217 return buffer;
yro2b0f8862017-11-06 14:27:31 -0800218}
Yao Chen729093d2017-10-16 10:33:26 -0700219
Yao Chen5154a372017-10-30 22:57:06 -0700220void DurationMetricProducer::flushIfNeeded(uint64_t eventTime) {
Yao Chen729093d2017-10-16 10:33:26 -0700221 if (mCurrentBucketStartTimeNs + mBucketSizeNs > eventTime) {
222 return;
223 }
Yao Chen5154a372017-10-30 22:57:06 -0700224 VLOG("flushing...........");
Yao Chend41c4222017-11-15 19:26:14 -0800225 for (auto it = mCurrentSlicedDuration.begin(); it != mCurrentSlicedDuration.end();) {
Yao Chen5154a372017-10-30 22:57:06 -0700226 if (it->second->flushIfNeeded(eventTime)) {
227 VLOG("erase bucket for key %s", it->first.c_str());
Yao Chend41c4222017-11-15 19:26:14 -0800228 it = mCurrentSlicedDuration.erase(it);
229 } else {
230 ++it;
Yao Chen729093d2017-10-16 10:33:26 -0700231 }
232 }
Yao Chen5154a372017-10-30 22:57:06 -0700233
234 int numBucketsForward = (eventTime - mCurrentBucketStartTimeNs) / mBucketSizeNs;
235 mCurrentBucketStartTimeNs += numBucketsForward * mBucketSizeNs;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800236 mCurrentBucketNum += numBucketsForward;
Yao Chen5154a372017-10-30 22:57:06 -0700237}
238
239void DurationMetricProducer::onMatchedLogEventInternal(
240 const size_t matcherIndex, const HashableDimensionKey& eventKey,
241 const map<string, HashableDimensionKey>& conditionKeys, bool condition,
Chenjie Yub3dda412017-10-24 13:41:59 -0700242 const LogEvent& event, bool scheduledPull) {
Yao Chen5154a372017-10-30 22:57:06 -0700243 flushIfNeeded(event.GetTimestampNs());
244
245 if (matcherIndex == mStopAllIndex) {
246 for (auto& pair : mCurrentSlicedDuration) {
247 pair.second->noteStopAll(event.GetTimestampNs());
248 }
249 return;
250 }
251
252 HashableDimensionKey atomKey = getHashableKey(getDimensionKey(event, mInternalDimension));
253
254 if (mCurrentSlicedDuration.find(eventKey) == mCurrentSlicedDuration.end()) {
Yangster-mace2cd6d52017-11-09 20:38:30 -0800255 mCurrentSlicedDuration[eventKey] = createDurationTracker(eventKey, mPastBuckets[eventKey]);
Yao Chen5154a372017-10-30 22:57:06 -0700256 }
257
258 auto it = mCurrentSlicedDuration.find(eventKey);
259
260 if (matcherIndex == mStartIndex) {
261 it->second->noteStart(atomKey, condition, event.GetTimestampNs(), conditionKeys);
Yao Chen5154a372017-10-30 22:57:06 -0700262 } else if (matcherIndex == mStopIndex) {
Yao Chen0ea19902017-11-15 15:44:45 -0800263 it->second->noteStop(atomKey, event.GetTimestampNs(), false);
Yao Chen5154a372017-10-30 22:57:06 -0700264 }
Yao Chen729093d2017-10-16 10:33:26 -0700265}
266
Yangster7c334a12017-11-22 14:24:24 -0800267size_t DurationMetricProducer::byteSize() const {
268 size_t totalSize = 0;
269 for (const auto& pair : mPastBuckets) {
270 totalSize += pair.second.size() * kBucketSize;
271 }
272 return totalSize;
yro69007c82017-10-26 20:42:57 -0700273}
274
Yao Chen729093d2017-10-16 10:33:26 -0700275} // namespace statsd
276} // namespace os
277} // namespace android