blob: e98999e732238a53e583e4ad8209871bdf0db3eb [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
17#define LOG_TAG "CountMetric"
18#define DEBUG true // STOPSHIP if true
19#define VLOG(...) \
20 if (DEBUG) ALOGD(__VA_ARGS__);
21
22#include "CountMetricProducer.h"
Bookatza4bc9c42017-10-04 11:45:57 -070023#include "CountAnomalyTracker.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070024
25#include <cutils/log.h>
26#include <limits.h>
27#include <stdlib.h>
28
29using std::unordered_map;
30
31namespace android {
32namespace os {
33namespace statsd {
34
Yao Chencaf339d2017-10-06 16:01:10 -070035CountMetricProducer::CountMetricProducer(const CountMetric& metric, const bool hasCondition)
Yao Chen44cf27c2017-09-14 22:32:50 -070036 : mMetric(metric),
Yao Chencaf339d2017-10-06 16:01:10 -070037 mStartTime(time(nullptr)),
Yao Chen44cf27c2017-09-14 22:32:50 -070038 mCounter(0),
Bookatza4bc9c42017-10-04 11:45:57 -070039 mCurrentBucketStartTime(mStartTime),
40 // TODO: read mAnomalyTracker parameters from config file.
Yao Chencaf339d2017-10-06 16:01:10 -070041 mAnomalyTracker(6, 10),
42 mCondition(hasCondition ? ConditionState::kUnknown : ConditionState::kTrue) {
Yao Chen44cf27c2017-09-14 22:32:50 -070043 // TODO: evaluate initial conditions. and set mConditionMet.
44 if (metric.has_bucket() && metric.bucket().has_bucket_size_millis()) {
45 mBucketSize_sec = metric.bucket().bucket_size_millis() / 1000;
46 } else {
47 mBucketSize_sec = LONG_MAX;
48 }
49
50 VLOG("created. bucket size %lu start_time: %lu", mBucketSize_sec, mStartTime);
51}
52
Yao Chen44cf27c2017-09-14 22:32:50 -070053CountMetricProducer::~CountMetricProducer() {
54 VLOG("~CountMetricProducer() called");
55}
56
57void CountMetricProducer::finish() {
58 // TODO: write the StatsLogReport to dropbox using
59 // DropboxWriter.
Yao Chen44cf27c2017-09-14 22:32:50 -070060}
61
62void CountMetricProducer::onDumpReport() {
63 VLOG("dump report now...");
64}
65
Yao Chencaf339d2017-10-06 16:01:10 -070066void CountMetricProducer::onConditionChanged(const bool conditionMet) {
67 VLOG("onConditionChanged");
68 mCondition = conditionMet;
69}
70
Yao Chen44cf27c2017-09-14 22:32:50 -070071void CountMetricProducer::onMatchedLogEvent(const LogEventWrapper& event) {
72 time_t eventTime = event.timestamp_ns / 1000000000;
73
74 // this is old event, maybe statsd restarted?
75 if (eventTime < mStartTime) {
76 return;
77 }
78
Yao Chencaf339d2017-10-06 16:01:10 -070079 if (mCondition == ConditionState::kTrue) {
Yao Chen44cf27c2017-09-14 22:32:50 -070080 flushCounterIfNeeded(eventTime);
81 mCounter++;
Bookatza4bc9c42017-10-04 11:45:57 -070082 mAnomalyTracker.checkAnomaly(mCounter);
Yao Chencaf339d2017-10-06 16:01:10 -070083 VLOG("metric %lld count %d", mMetric.metric_id(), mCounter);
Yao Chen44cf27c2017-09-14 22:32:50 -070084 }
85}
86
Yao Chencaf339d2017-10-06 16:01:10 -070087// When a new matched event comes in, we check if it falls into the current
88// bucket. And flush the counter to the StatsLogReport and adjust the bucket if
89// needed.
Yao Chen44cf27c2017-09-14 22:32:50 -070090void CountMetricProducer::flushCounterIfNeeded(const time_t& eventTime) {
91 if (mCurrentBucketStartTime + mBucketSize_sec > eventTime) {
92 return;
93 }
94
95 // TODO: add a KeyValuePair to StatsLogReport.
Yao Chencaf339d2017-10-06 16:01:10 -070096 ALOGD("%lld: dump counter %d", mMetric.metric_id(), mCounter);
Yao Chen44cf27c2017-09-14 22:32:50 -070097
Yao Chen44cf27c2017-09-14 22:32:50 -070098 // adjust the bucket start time
Bookatza4bc9c42017-10-04 11:45:57 -070099 time_t numBucketsForward = (eventTime - mCurrentBucketStartTime)
100 / mBucketSize_sec;
101
102 mCurrentBucketStartTime = mCurrentBucketStartTime +
103 (numBucketsForward) * mBucketSize_sec;
104
105 // reset counter
106 mAnomalyTracker.addPastBucket(mCounter, numBucketsForward);
107 mCounter = 0;
Yao Chen44cf27c2017-09-14 22:32:50 -0700108
Yao Chencaf339d2017-10-06 16:01:10 -0700109 VLOG("%lld: new bucket start time: %lu", mMetric.metric_id(), mCurrentBucketStartTime);
Yao Chen44cf27c2017-09-14 22:32:50 -0700110}
111
112} // namespace statsd
113} // namespace os
114} // namespace android