blob: 1ac4426468d89494f3e0f988b47c9abe02ff4da3 [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#ifndef COUNT_METRIC_PRODUCER_H
18#define COUNT_METRIC_PRODUCER_H
19
Yao Chen44cf27c2017-09-14 22:32:50 -070020#include <unordered_map>
Yao Chencaf339d2017-10-06 16:01:10 -070021
yro24809bd2017-10-31 23:06:53 -070022#include <android/util/ProtoOutputStream.h>
Yao Chen93fe3a32017-11-02 13:52:59 -070023#include <gtest/gtest_prod.h>
Yangster-mace2cd6d52017-11-09 20:38:30 -080024#include "../anomaly/AnomalyTracker.h"
Yao Chencaf339d2017-10-06 16:01:10 -070025#include "../condition/ConditionTracker.h"
26#include "../matchers/matcher_util.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070027#include "MetricProducer.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070028#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
Yao Chen729093d2017-10-16 10:33:26 -070029#include "stats_util.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070030
31namespace android {
32namespace os {
33namespace statsd {
34
Yao Chen93fe3a32017-11-02 13:52:59 -070035struct CountBucket {
36 int64_t mBucketStartNs;
37 int64_t mBucketEndNs;
38 int64_t mCount;
39};
40
Yao Chen44cf27c2017-09-14 22:32:50 -070041class CountMetricProducer : public MetricProducer {
42public:
Yao Chenb3561512017-11-21 18:07:17 -080043 CountMetricProducer(const ConfigKey& key, const CountMetric& countMetric,
44 const int conditionIndex, const sp<ConditionWizard>& wizard,
Chenjie Yue1361ed2018-07-23 17:33:09 -070045 const int64_t timeBaseNs, const int64_t startTimeNs);
Yao Chen44cf27c2017-09-14 22:32:50 -070046
47 virtual ~CountMetricProducer();
48
Yao Chenb7041772017-10-20 16:59:25 -070049protected:
Yangsterf2bee6f2017-11-29 12:01:05 -080050 void onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -080051 const size_t matcherIndex, const MetricDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -080052 const ConditionKey& conditionKey, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -080053 const LogEvent& event) override;
yro2b0f8862017-11-06 14:27:31 -080054
Yao Chen44cf27c2017-09-14 22:32:50 -070055private:
Yangster-mace68f3a52018-04-04 00:01:43 -070056
Yangster-macb142cc82018-03-30 15:22:08 -070057 void onDumpReportLocked(const int64_t dumpTimeNs,
Yangster-mace68f3a52018-04-04 00:01:43 -070058 const bool include_current_partial_bucket,
Bookatzff71cad2018-09-20 17:17:49 -070059 const bool erase_data,
Yangster-mac9def8e32018-04-17 13:55:51 -070060 std::set<string> *str_set,
Yao Chen288c6002017-12-12 13:43:18 -080061 android::util::ProtoOutputStream* protoOutput) override;
Yangsterf2bee6f2017-11-29 12:01:05 -080062
Yangster-maca802d732018-04-24 07:50:38 -070063 void clearPastBucketsLocked(const int64_t dumpTimeNs) override;
64
Yangsterf2bee6f2017-11-29 12:01:05 -080065 // Internal interface to handle condition change.
Yangster-macb142cc82018-03-30 15:22:08 -070066 void onConditionChangedLocked(const bool conditionMet, const int64_t eventTime) override;
Yangsterf2bee6f2017-11-29 12:01:05 -080067
68 // Internal interface to handle sliced condition change.
Yangster-macb142cc82018-03-30 15:22:08 -070069 void onSlicedConditionMayChangeLocked(bool overallCondition, const int64_t eventTime) override;
Yangsterf2bee6f2017-11-29 12:01:05 -080070
71 // Internal function to calculate the current used bytes.
72 size_t byteSizeLocked() const override;
73
Yangster-maca78d0082018-03-12 12:02:56 -070074 void dumpStatesLocked(FILE* out, bool verbose) const override;
Yao Chen884c8c12018-01-26 10:36:25 -080075
Yangster-macb142cc82018-03-30 15:22:08 -070076 void dropDataLocked(const int64_t dropTimeNs) override;
Yao Chen06dba5d2018-01-26 13:38:16 -080077
Yangsterf2bee6f2017-11-29 12:01:05 -080078 // Util function to flush the old packet.
Yangster-macb142cc82018-03-30 15:22:08 -070079 void flushIfNeededLocked(const int64_t& newEventTime) override;
David Chen27785a82018-01-19 17:06:45 -080080
Yangster-macb142cc82018-03-30 15:22:08 -070081 void flushCurrentBucketLocked(const int64_t& eventTimeNs) override;
Yangsterf2bee6f2017-11-29 12:01:05 -080082
Yangster-mac93694462018-01-22 20:49:31 -080083 std::unordered_map<MetricDimensionKey, std::vector<CountBucket>> mPastBuckets;
yro24809bd2017-10-31 23:06:53 -070084
David Chen27785a82018-01-19 17:06:45 -080085 // The current bucket (may be a partial bucket).
Yang Lu3eba6212017-10-25 19:54:45 -070086 std::shared_ptr<DimToValMap> mCurrentSlicedCounter = std::make_shared<DimToValMap>();
Yao Chen729093d2017-10-16 10:33:26 -070087
David Chen27785a82018-01-19 17:06:45 -080088 // The sum of previous partial buckets in the current full bucket (excluding the current
89 // partial bucket). This is only updated while flushing the current bucket.
90 std::shared_ptr<DimToValMap> mCurrentFullCounters = std::make_shared<DimToValMap>();
91
Yangster-mace2cd6d52017-11-09 20:38:30 -080092 static const size_t kBucketSize = sizeof(CountBucket{});
yro24809bd2017-10-31 23:06:53 -070093
Yangster-mac93694462018-01-22 20:49:31 -080094 bool hitGuardRailLocked(const MetricDimensionKey& newKey);
Yao Chenb3561512017-11-21 18:07:17 -080095
Yao Chen93fe3a32017-11-02 13:52:59 -070096 FRIEND_TEST(CountMetricProducerTest, TestNonDimensionalEvents);
97 FRIEND_TEST(CountMetricProducerTest, TestEventsWithNonSlicedCondition);
98 FRIEND_TEST(CountMetricProducerTest, TestEventsWithSlicedCondition);
Bookatz1bf94382018-01-04 11:43:20 -080099 FRIEND_TEST(CountMetricProducerTest, TestAnomalyDetectionUnSliced);
David Chen27785a82018-01-19 17:06:45 -0800100 FRIEND_TEST(CountMetricProducerTest, TestEventWithAppUpgrade);
101 FRIEND_TEST(CountMetricProducerTest, TestEventWithAppUpgradeInNextBucket);
Chenjie Yue1361ed2018-07-23 17:33:09 -0700102 FRIEND_TEST(CountMetricProducerTest, TestFirstBucket);
Yao Chen44cf27c2017-09-14 22:32:50 -0700103};
104
105} // namespace statsd
106} // namespace os
107} // namespace android
108#endif // COUNT_METRIC_PRODUCER_H