blob: 68d0a30403b0e43d26cafa32f741d1099f570516 [file] [log] [blame]
Yao Chen44cf27c2017-09-14 22:32:50 -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
Yao Chen44cf27c2017-09-14 22:32:50 -070017#define DEBUG true // STOPSHIP if true
Joe Onorato9fc9edf2017-10-15 20:08:52 -070018#include "Log.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070019
20#include "CountMetricProducer.h"
Bookatza4bc9c42017-10-04 11:45:57 -070021#include "CountAnomalyTracker.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070022
23#include <cutils/log.h>
24#include <limits.h>
25#include <stdlib.h>
26
27using std::unordered_map;
28
29namespace android {
30namespace os {
31namespace statsd {
32
Yao Chencaf339d2017-10-06 16:01:10 -070033CountMetricProducer::CountMetricProducer(const CountMetric& metric, const bool hasCondition)
Yao Chen44cf27c2017-09-14 22:32:50 -070034 : mMetric(metric),
Yao Chencaf339d2017-10-06 16:01:10 -070035 mStartTime(time(nullptr)),
Yao Chen44cf27c2017-09-14 22:32:50 -070036 mCounter(0),
Bookatza4bc9c42017-10-04 11:45:57 -070037 mCurrentBucketStartTime(mStartTime),
38 // TODO: read mAnomalyTracker parameters from config file.
Yao Chencaf339d2017-10-06 16:01:10 -070039 mAnomalyTracker(6, 10),
40 mCondition(hasCondition ? ConditionState::kUnknown : ConditionState::kTrue) {
Yao Chen44cf27c2017-09-14 22:32:50 -070041 // TODO: evaluate initial conditions. and set mConditionMet.
42 if (metric.has_bucket() && metric.bucket().has_bucket_size_millis()) {
43 mBucketSize_sec = metric.bucket().bucket_size_millis() / 1000;
44 } else {
45 mBucketSize_sec = LONG_MAX;
46 }
47
48 VLOG("created. bucket size %lu start_time: %lu", mBucketSize_sec, mStartTime);
49}
50
Yao Chen44cf27c2017-09-14 22:32:50 -070051CountMetricProducer::~CountMetricProducer() {
52 VLOG("~CountMetricProducer() called");
53}
54
55void CountMetricProducer::finish() {
56 // TODO: write the StatsLogReport to dropbox using
57 // DropboxWriter.
Yao Chen44cf27c2017-09-14 22:32:50 -070058}
59
60void CountMetricProducer::onDumpReport() {
61 VLOG("dump report now...");
62}
63
Yao Chencaf339d2017-10-06 16:01:10 -070064void CountMetricProducer::onConditionChanged(const bool conditionMet) {
65 VLOG("onConditionChanged");
66 mCondition = conditionMet;
67}
68
Yao Chen44cf27c2017-09-14 22:32:50 -070069void CountMetricProducer::onMatchedLogEvent(const LogEventWrapper& event) {
70 time_t eventTime = event.timestamp_ns / 1000000000;
71
72 // this is old event, maybe statsd restarted?
73 if (eventTime < mStartTime) {
74 return;
75 }
76
Yao Chencaf339d2017-10-06 16:01:10 -070077 if (mCondition == ConditionState::kTrue) {
Yao Chen44cf27c2017-09-14 22:32:50 -070078 flushCounterIfNeeded(eventTime);
79 mCounter++;
Bookatza4bc9c42017-10-04 11:45:57 -070080 mAnomalyTracker.checkAnomaly(mCounter);
Yao Chencaf339d2017-10-06 16:01:10 -070081 VLOG("metric %lld count %d", mMetric.metric_id(), mCounter);
Yao Chen44cf27c2017-09-14 22:32:50 -070082 }
83}
84
Yao Chencaf339d2017-10-06 16:01:10 -070085// When a new matched event comes in, we check if it falls into the current
86// bucket. And flush the counter to the StatsLogReport and adjust the bucket if
87// needed.
Yao Chen44cf27c2017-09-14 22:32:50 -070088void CountMetricProducer::flushCounterIfNeeded(const time_t& eventTime) {
89 if (mCurrentBucketStartTime + mBucketSize_sec > eventTime) {
90 return;
91 }
92
93 // TODO: add a KeyValuePair to StatsLogReport.
Yao Chencaf339d2017-10-06 16:01:10 -070094 ALOGD("%lld: dump counter %d", mMetric.metric_id(), mCounter);
Yao Chen44cf27c2017-09-14 22:32:50 -070095
Yao Chen44cf27c2017-09-14 22:32:50 -070096 // adjust the bucket start time
Bookatza4bc9c42017-10-04 11:45:57 -070097 time_t numBucketsForward = (eventTime - mCurrentBucketStartTime)
98 / mBucketSize_sec;
99
100 mCurrentBucketStartTime = mCurrentBucketStartTime +
101 (numBucketsForward) * mBucketSize_sec;
102
103 // reset counter
104 mAnomalyTracker.addPastBucket(mCounter, numBucketsForward);
105 mCounter = 0;
Yao Chen44cf27c2017-09-14 22:32:50 -0700106
Yao Chencaf339d2017-10-06 16:01:10 -0700107 VLOG("%lld: new bucket start time: %lu", mMetric.metric_id(), mCurrentBucketStartTime);
Yao Chen44cf27c2017-09-14 22:32:50 -0700108}
109
110} // namespace statsd
111} // namespace os
112} // namespace android