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