blob: 5cffec1b1bcf5d5a9fcbf696535b0b6066512b76 [file] [log] [blame]
Chenjie Yub3dda412017-10-24 13:41:59 -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 // STOPSHIP if true
18#include "Log.h"
19
20#include "ValueMetricProducer.h"
21
22#include <cutils/log.h>
23#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;
32using android::util::ProtoOutputStream;
Chenjie Yub3dda412017-10-24 13:41:59 -070033using std::list;
Chenjie Yu6736c892017-11-09 10:50:09 -080034using std::make_pair;
Chenjie Yub3dda412017-10-24 13:41:59 -070035using std::make_shared;
Yao Chen93fe3a32017-11-02 13:52:59 -070036using std::map;
Chenjie Yub3dda412017-10-24 13:41:59 -070037using std::shared_ptr;
38using std::unique_ptr;
Yao Chen93fe3a32017-11-02 13:52:59 -070039using std::unordered_map;
Chenjie Yub3dda412017-10-24 13:41:59 -070040
41namespace android {
42namespace os {
43namespace statsd {
44
yro2b0f8862017-11-06 14:27:31 -080045// for StatsLogReport
46const int FIELD_ID_METRIC_ID = 1;
47const int FIELD_ID_START_REPORT_NANOS = 2;
48const int FIELD_ID_END_REPORT_NANOS = 3;
49const int FIELD_ID_VALUE_METRICS = 7;
50// for ValueMetricDataWrapper
51const int FIELD_ID_DATA = 1;
52// for ValueMetricData
53const int FIELD_ID_DIMENSION = 1;
54const int FIELD_ID_BUCKET_INFO = 2;
55// for KeyValuePair
56const int FIELD_ID_KEY = 1;
57const int FIELD_ID_VALUE_STR = 2;
58const int FIELD_ID_VALUE_INT = 3;
59const int FIELD_ID_VALUE_BOOL = 4;
60const int FIELD_ID_VALUE_FLOAT = 5;
61// for ValueBucketInfo
62const int FIELD_ID_START_BUCKET_NANOS = 1;
63const int FIELD_ID_END_BUCKET_NANOS = 2;
64const int FIELD_ID_VALUE = 3;
65
Chenjie Yu6736c892017-11-09 10:50:09 -080066static const uint64_t kDefaultBucketSizeMillis = 60 * 60 * 1000L;
67
Chenjie Yub3dda412017-10-24 13:41:59 -070068// ValueMetric has a minimum bucket size of 10min so that we don't pull too frequently
69ValueMetricProducer::ValueMetricProducer(const ValueMetric& metric, const int conditionIndex,
Yao Chen93fe3a32017-11-02 13:52:59 -070070 const sp<ConditionWizard>& wizard, const int pullTagId,
Chenjie Yu6736c892017-11-09 10:50:09 -080071 const uint64_t startTimeNs,
72 shared_ptr<StatsPullerManager> statsPullerManager)
73 : MetricProducer(startTimeNs, conditionIndex, wizard),
74 mMetric(metric),
75 mStatsPullerManager(statsPullerManager),
76 mPullTagId(pullTagId) {
Yao Chen93fe3a32017-11-02 13:52:59 -070077 // TODO: valuemetric for pushed events may need unlimited bucket length
Chenjie Yu6736c892017-11-09 10:50:09 -080078 if (metric.has_bucket() && metric.bucket().has_bucket_size_millis()) {
79 mBucketSizeNs = mMetric.bucket().bucket_size_millis() * 1000 * 1000;
80 } else {
81 mBucketSizeNs = kDefaultBucketSizeMillis * 1000 * 1000;
82 }
Chenjie Yub3dda412017-10-24 13:41:59 -070083
Yao Chen93fe3a32017-11-02 13:52:59 -070084 mDimension.insert(mDimension.begin(), metric.dimension().begin(), metric.dimension().end());
Chenjie Yub3dda412017-10-24 13:41:59 -070085
Yao Chen93fe3a32017-11-02 13:52:59 -070086 if (metric.links().size() > 0) {
87 mConditionLinks.insert(mConditionLinks.begin(), metric.links().begin(),
88 metric.links().end());
89 mConditionSliced = true;
90 }
Chenjie Yub3dda412017-10-24 13:41:59 -070091
Yao Chen93fe3a32017-11-02 13:52:59 -070092 if (!metric.has_condition() && mPullTagId != -1) {
Chenjie Yu6736c892017-11-09 10:50:09 -080093 VLOG("Setting up periodic pulling for %d", mPullTagId);
94 mStatsPullerManager->RegisterReceiver(mPullTagId, this,
95 metric.bucket().bucket_size_millis());
Yao Chen93fe3a32017-11-02 13:52:59 -070096 }
Chenjie Yub3dda412017-10-24 13:41:59 -070097
yro2b0f8862017-11-06 14:27:31 -080098 startNewProtoOutputStream(mStartTimeNs);
99
Yao Chen93fe3a32017-11-02 13:52:59 -0700100 VLOG("value metric %lld created. bucket size %lld start_time: %lld", metric.metric_id(),
101 (long long)mBucketSizeNs, (long long)mStartTimeNs);
Chenjie Yub3dda412017-10-24 13:41:59 -0700102}
103
Chenjie Yu6736c892017-11-09 10:50:09 -0800104// for testing
105ValueMetricProducer::ValueMetricProducer(const ValueMetric& metric, const int conditionIndex,
106 const sp<ConditionWizard>& wizard, const int pullTagId,
107 const uint64_t startTimeNs)
108 : ValueMetricProducer(metric, conditionIndex, wizard, pullTagId, startTimeNs,
109 make_shared<StatsPullerManager>()) {
110}
111
Chenjie Yub3dda412017-10-24 13:41:59 -0700112ValueMetricProducer::~ValueMetricProducer() {
Yao Chen93fe3a32017-11-02 13:52:59 -0700113 VLOG("~ValueMetricProducer() called");
Chenjie Yu6736c892017-11-09 10:50:09 -0800114 if (mPullTagId != -1) {
115 mStatsPullerManager->UnRegisterReceiver(mPullTagId, this);
116 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700117}
118
yro2b0f8862017-11-06 14:27:31 -0800119void ValueMetricProducer::startNewProtoOutputStream(long long startTime) {
120 mProto = std::make_unique<ProtoOutputStream>();
121 mProto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_ID, mMetric.metric_id());
122 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, startTime);
123 mProtoToken = mProto->start(FIELD_TYPE_MESSAGE | FIELD_ID_VALUE_METRICS);
124}
125
Chenjie Yub3dda412017-10-24 13:41:59 -0700126void ValueMetricProducer::finish() {
Yao Chen93fe3a32017-11-02 13:52:59 -0700127 // TODO: write the StatsLogReport to dropbox using
128 // DropboxWriter.
Chenjie Yub3dda412017-10-24 13:41:59 -0700129}
130
Chenjie Yub3dda412017-10-24 13:41:59 -0700131void ValueMetricProducer::onSlicedConditionMayChange(const uint64_t eventTime) {
132 VLOG("Metric %lld onSlicedConditionMayChange", mMetric.metric_id());
133}
134
yro17adac92017-11-08 23:16:29 -0800135std::unique_ptr<std::vector<uint8_t>> ValueMetricProducer::onDumpReport() {
Yao Chen93fe3a32017-11-02 13:52:59 -0700136 VLOG("metric %lld dump report now...", mMetric.metric_id());
Chenjie Yub3dda412017-10-24 13:41:59 -0700137
Yao Chen93fe3a32017-11-02 13:52:59 -0700138 for (const auto& pair : mPastBuckets) {
139 const HashableDimensionKey& hashableKey = pair.first;
yro2b0f8862017-11-06 14:27:31 -0800140 VLOG(" dimension key %s", hashableKey.c_str());
Yao Chen93fe3a32017-11-02 13:52:59 -0700141 auto it = mDimensionKeyMap.find(hashableKey);
142 if (it == mDimensionKeyMap.end()) {
143 ALOGE("Dimension key %s not found?!?! skip...", hashableKey.c_str());
144 continue;
145 }
yrob0378b02017-11-09 20:36:25 -0800146 long long wrapperToken =
147 mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
Chenjie Yub3dda412017-10-24 13:41:59 -0700148
yro2b0f8862017-11-06 14:27:31 -0800149 // First fill dimension (KeyValuePairs).
150 for (const auto& kv : it->second) {
yrob0378b02017-11-09 20:36:25 -0800151 long long dimensionToken =
152 mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DIMENSION);
yro2b0f8862017-11-06 14:27:31 -0800153 mProto->write(FIELD_TYPE_INT32 | FIELD_ID_KEY, kv.key());
154 if (kv.has_value_str()) {
155 mProto->write(FIELD_TYPE_INT32 | FIELD_ID_VALUE_STR, kv.value_str());
156 } else if (kv.has_value_int()) {
157 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE_INT, kv.value_int());
158 } else if (kv.has_value_bool()) {
159 mProto->write(FIELD_TYPE_BOOL | FIELD_ID_VALUE_BOOL, kv.value_bool());
160 } else if (kv.has_value_float()) {
161 mProto->write(FIELD_TYPE_FLOAT | FIELD_ID_VALUE_FLOAT, kv.value_float());
162 }
163 mProto->end(dimensionToken);
164 }
165
166 // Then fill bucket_info (ValueBucketInfo).
167 for (const auto& bucket : pair.second) {
yrob0378b02017-11-09 20:36:25 -0800168 long long bucketInfoToken =
169 mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_BUCKET_INFO);
yro2b0f8862017-11-06 14:27:31 -0800170 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_START_BUCKET_NANOS,
171 (long long)bucket.mBucketStartNs);
172 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_END_BUCKET_NANOS,
173 (long long)bucket.mBucketEndNs);
174 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_VALUE, (long long)bucket.mValue);
175 mProto->end(bucketInfoToken);
176 VLOG("\t bucket [%lld - %lld] count: %lld", (long long)bucket.mBucketStartNs,
177 (long long)bucket.mBucketEndNs, (long long)bucket.mValue);
178 }
179 mProto->end(wrapperToken);
Chenjie Yub3dda412017-10-24 13:41:59 -0700180 }
yro2b0f8862017-11-06 14:27:31 -0800181 mProto->end(mProtoToken);
182 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_END_REPORT_NANOS,
183 (long long)mCurrentBucketStartTimeNs);
184
185 VLOG("metric %lld dump report now...", mMetric.metric_id());
yro17adac92017-11-08 23:16:29 -0800186 std::unique_ptr<std::vector<uint8_t>> buffer = serializeProto();
yro2b0f8862017-11-06 14:27:31 -0800187
188 startNewProtoOutputStream(time(nullptr) * NS_PER_SEC);
189 mPastBuckets.clear();
190 mByteSize = 0;
191
yro17adac92017-11-08 23:16:29 -0800192 return buffer;
yro2b0f8862017-11-06 14:27:31 -0800193
194 // TODO: Clear mDimensionKeyMap once the report is dumped.
Chenjie Yub3dda412017-10-24 13:41:59 -0700195}
196
197void ValueMetricProducer::onConditionChanged(const bool condition, const uint64_t eventTime) {
Chenjie Yu7f8def92017-11-03 09:33:15 -0700198 AutoMutex _l(mLock);
Chenjie Yub3dda412017-10-24 13:41:59 -0700199 mCondition = condition;
200
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700201 if (mPullTagId != -1) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700202 if (mCondition == true) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800203 mStatsPullerManager->RegisterReceiver(mPullTagId, this,
204 mMetric.bucket().bucket_size_millis());
205 } else if (mCondition == false) {
206 mStatsPullerManager->UnRegisterReceiver(mPullTagId, this);
Chenjie Yub3dda412017-10-24 13:41:59 -0700207 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700208
209 vector<shared_ptr<LogEvent>> allData;
Chenjie Yu6736c892017-11-09 10:50:09 -0800210 if (mStatsPullerManager->Pull(mPullTagId, &allData)) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700211 if (allData.size() == 0) {
212 return;
213 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700214 for (const auto& data : allData) {
215 onMatchedLogEvent(0, *data, false);
216 }
217 flush_if_needed(eventTime);
Chenjie Yub3dda412017-10-24 13:41:59 -0700218 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700219 return;
Chenjie Yub3dda412017-10-24 13:41:59 -0700220 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700221}
222
223void ValueMetricProducer::onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData) {
Chenjie Yu7f8def92017-11-03 09:33:15 -0700224 AutoMutex _l(mLock);
Chenjie Yu6736c892017-11-09 10:50:09 -0800225 if (mCondition == true || !mMetric.has_condition()) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700226 if (allData.size() == 0) {
227 return;
228 }
229 uint64_t eventTime = allData.at(0)->GetTimestampNs();
Chenjie Yu6736c892017-11-09 10:50:09 -0800230 // alarm is not accurate and might drift.
231 if (eventTime > mCurrentBucketStartTimeNs + mBucketSizeNs * 3 / 2) {
232 flush_if_needed(eventTime);
233 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700234 for (const auto& data : allData) {
235 onMatchedLogEvent(0, *data, true);
236 }
237 flush_if_needed(eventTime);
238 }
239}
240
241void ValueMetricProducer::onMatchedLogEventInternal(
Yao Chen93fe3a32017-11-02 13:52:59 -0700242 const size_t matcherIndex, const HashableDimensionKey& eventKey,
243 const map<string, HashableDimensionKey>& conditionKey, bool condition,
244 const LogEvent& event, bool scheduledPull) {
245 uint64_t eventTimeNs = event.GetTimestampNs();
246 if (eventTimeNs < mCurrentBucketStartTimeNs) {
247 VLOG("Skip event due to late arrival: %lld vs %lld", (long long)eventTimeNs,
248 (long long)mCurrentBucketStartTimeNs);
249 return;
Chenjie Yub3dda412017-10-24 13:41:59 -0700250 }
Yao Chen93fe3a32017-11-02 13:52:59 -0700251
252 Interval& interval = mCurrentSlicedBucket[eventKey];
253
254 long value = get_value(event);
255
Chenjie Yu6736c892017-11-09 10:50:09 -0800256 if (mPullTagId != -1) {
257 if (scheduledPull) {
258 // scheduled pull always sets beginning of current bucket and end
259 // of next bucket
260 if (interval.raw.size() > 0) {
Yao Chen93fe3a32017-11-02 13:52:59 -0700261 interval.raw.back().second = value;
Chenjie Yu6736c892017-11-09 10:50:09 -0800262 } else {
263 interval.raw.push_back(make_pair(value, value));
264 }
265 Interval& nextInterval = mNextSlicedBucket[eventKey];
266 if (nextInterval.raw.size() == 0) {
267 nextInterval.raw.push_back(make_pair(value, 0));
268 } else {
269 nextInterval.raw.front().first = value;
270 }
271 } else {
272 if (mCondition == true) {
273 interval.raw.push_back(make_pair(value, 0));
274 } else {
275 if (interval.raw.size() != 0) {
276 interval.raw.back().second = value;
277 } else {
278 interval.tainted = true;
279 VLOG("Data on condition true missing!");
280 }
Yao Chen93fe3a32017-11-02 13:52:59 -0700281 }
282 }
Chenjie Yu6736c892017-11-09 10:50:09 -0800283 } else {
Yao Chen93fe3a32017-11-02 13:52:59 -0700284 flush_if_needed(eventTimeNs);
Chenjie Yu6736c892017-11-09 10:50:09 -0800285 interval.raw.push_back(make_pair(value, 0));
Yao Chen93fe3a32017-11-02 13:52:59 -0700286 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700287}
288
289long ValueMetricProducer::get_value(const LogEvent& event) {
Yao Chen93fe3a32017-11-02 13:52:59 -0700290 status_t err = NO_ERROR;
291 long val = event.GetLong(mMetric.value_field(), &err);
292 if (err == NO_ERROR) {
293 return val;
294 } else {
Chenjie Yu6736c892017-11-09 10:50:09 -0800295 VLOG("Can't find value in message. %s", event.ToString().c_str());
Yao Chen93fe3a32017-11-02 13:52:59 -0700296 return 0;
297 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700298}
299
300void ValueMetricProducer::flush_if_needed(const uint64_t eventTimeNs) {
301 if (mCurrentBucketStartTimeNs + mBucketSizeNs > eventTimeNs) {
302 VLOG("eventTime is %lld, less than next bucket start time %lld", (long long)eventTimeNs,
303 (long long)(mCurrentBucketStartTimeNs + mBucketSizeNs));
304 return;
305 }
306
307 VLOG("finalizing bucket for %ld, dumping %d slices", (long)mCurrentBucketStartTimeNs,
308 (int)mCurrentSlicedBucket.size());
yro2b0f8862017-11-06 14:27:31 -0800309 ValueBucket info;
310 info.mBucketStartNs = mCurrentBucketStartTimeNs;
311 info.mBucketEndNs = mCurrentBucketStartTimeNs + mBucketSizeNs;
Chenjie Yub3dda412017-10-24 13:41:59 -0700312
Chenjie Yu6736c892017-11-09 10:50:09 -0800313 int tainted = 0;
Chenjie Yub3dda412017-10-24 13:41:59 -0700314 for (const auto& slice : mCurrentSlicedBucket) {
Yao Chen93fe3a32017-11-02 13:52:59 -0700315 long value = 0;
Chenjie Yu6736c892017-11-09 10:50:09 -0800316 if (mPullTagId != -1) {
317 for (const auto& pair : slice.second.raw) {
318 value += (pair.second - pair.first);
319 }
320 } else {
321 for (const auto& pair : slice.second.raw) {
322 value += pair.first;
323 }
Yao Chen93fe3a32017-11-02 13:52:59 -0700324 }
Chenjie Yu6736c892017-11-09 10:50:09 -0800325 tainted += slice.second.tainted;
yro2b0f8862017-11-06 14:27:31 -0800326 info.mValue = value;
Chenjie Yu6736c892017-11-09 10:50:09 -0800327 VLOG(" %s, %ld, %d", slice.first.c_str(), value, tainted);
Yao Chen93fe3a32017-11-02 13:52:59 -0700328 // it will auto create new vector of ValuebucketInfo if the key is not found.
329 auto& bucketList = mPastBuckets[slice.first];
330 bucketList.push_back(info);
yro2b0f8862017-11-06 14:27:31 -0800331 mByteSize += sizeof(info);
Chenjie Yub3dda412017-10-24 13:41:59 -0700332 }
333
334 // Reset counters
335 mCurrentSlicedBucket.swap(mNextSlicedBucket);
336 mNextSlicedBucket.clear();
337 int64_t numBucketsForward = (eventTimeNs - mCurrentBucketStartTimeNs) / mBucketSizeNs;
Yao Chen93fe3a32017-11-02 13:52:59 -0700338 if (numBucketsForward > 1) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700339 VLOG("Skipping forward %lld buckets", (long long)numBucketsForward);
340 }
341 mCurrentBucketStartTimeNs = mCurrentBucketStartTimeNs + numBucketsForward * mBucketSizeNs;
342 VLOG("metric %lld: new bucket start time: %lld", mMetric.metric_id(),
343 (long long)mCurrentBucketStartTimeNs);
344}
345
yro2b0f8862017-11-06 14:27:31 -0800346size_t ValueMetricProducer::byteSize() {
347 return mByteSize;
348}
349
Chenjie Yub3dda412017-10-24 13:41:59 -0700350} // namespace statsd
351} // namespace os
Yao Chen93fe3a32017-11-02 13:52:59 -0700352} // namespace android