Yao Chen | 44cf27c | 2017-09-14 22:32:50 -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 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 17 | #define DEBUG true // STOPSHIP if true |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame^] | 18 | #include "Log.h" |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 19 | |
| 20 | #include "CountMetricProducer.h" |
Bookatz | a4bc9c4 | 2017-10-04 11:45:57 -0700 | [diff] [blame] | 21 | #include "CountAnomalyTracker.h" |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 22 | |
| 23 | #include <cutils/log.h> |
| 24 | #include <limits.h> |
| 25 | #include <stdlib.h> |
| 26 | |
| 27 | using std::unordered_map; |
| 28 | |
| 29 | namespace android { |
| 30 | namespace os { |
| 31 | namespace statsd { |
| 32 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 33 | CountMetricProducer::CountMetricProducer(const CountMetric& metric, const bool hasCondition) |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 34 | : mMetric(metric), |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 35 | mStartTime(time(nullptr)), |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 36 | mCounter(0), |
Bookatz | a4bc9c4 | 2017-10-04 11:45:57 -0700 | [diff] [blame] | 37 | mCurrentBucketStartTime(mStartTime), |
| 38 | // TODO: read mAnomalyTracker parameters from config file. |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 39 | mAnomalyTracker(6, 10), |
| 40 | mCondition(hasCondition ? ConditionState::kUnknown : ConditionState::kTrue) { |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 41 | // 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 Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 51 | CountMetricProducer::~CountMetricProducer() { |
| 52 | VLOG("~CountMetricProducer() called"); |
| 53 | } |
| 54 | |
| 55 | void CountMetricProducer::finish() { |
| 56 | // TODO: write the StatsLogReport to dropbox using |
| 57 | // DropboxWriter. |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void CountMetricProducer::onDumpReport() { |
| 61 | VLOG("dump report now..."); |
| 62 | } |
| 63 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 64 | void CountMetricProducer::onConditionChanged(const bool conditionMet) { |
| 65 | VLOG("onConditionChanged"); |
| 66 | mCondition = conditionMet; |
| 67 | } |
| 68 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 69 | void 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 Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 77 | if (mCondition == ConditionState::kTrue) { |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 78 | flushCounterIfNeeded(eventTime); |
| 79 | mCounter++; |
Bookatz | a4bc9c4 | 2017-10-04 11:45:57 -0700 | [diff] [blame] | 80 | mAnomalyTracker.checkAnomaly(mCounter); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 81 | VLOG("metric %lld count %d", mMetric.metric_id(), mCounter); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 85 | // 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 Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 88 | void CountMetricProducer::flushCounterIfNeeded(const time_t& eventTime) { |
| 89 | if (mCurrentBucketStartTime + mBucketSize_sec > eventTime) { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | // TODO: add a KeyValuePair to StatsLogReport. |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 94 | ALOGD("%lld: dump counter %d", mMetric.metric_id(), mCounter); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 95 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 96 | // adjust the bucket start time |
Bookatz | a4bc9c4 | 2017-10-04 11:45:57 -0700 | [diff] [blame] | 97 | 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 Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 106 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 107 | VLOG("%lld: new bucket start time: %lu", mMetric.metric_id(), mCurrentBucketStartTime); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | } // namespace statsd |
| 111 | } // namespace os |
| 112 | } // namespace android |