blob: f9a8842efc3d801bfa1ed4c975d9fbf86c392d9c [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
yro24809bd2017-10-31 23:06:53 -070020#include <android/util/ProtoOutputStream.h>
Yao Chen93fe3a32017-11-02 13:52:59 -070021#include <gtest/gtest_prod.h>
tsaichristined21aacf2019-10-07 14:47:38 -070022
23#include <unordered_map>
24
Yao Chen44cf27c2017-09-14 22:32:50 -070025#include "MetricProducer.h"
tsaichristined21aacf2019-10-07 14:47:38 -070026#include "anomaly/AnomalyTracker.h"
27#include "condition/ConditionTracker.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070028#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
tsaichristined21aacf2019-10-07 14:47:38 -070029#include "matchers/matcher_util.h"
Yao Chen729093d2017-10-16 10:33:26 -070030#include "stats_util.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070031
32namespace android {
33namespace os {
34namespace statsd {
35
Yao Chen93fe3a32017-11-02 13:52:59 -070036struct CountBucket {
37 int64_t mBucketStartNs;
38 int64_t mBucketEndNs;
39 int64_t mCount;
40};
41
Yao Chen44cf27c2017-09-14 22:32:50 -070042class CountMetricProducer : public MetricProducer {
43public:
tsaichristined21aacf2019-10-07 14:47:38 -070044 CountMetricProducer(
45 const ConfigKey& key, const CountMetric& countMetric, const int conditionIndex,
46 const sp<ConditionWizard>& wizard, const int64_t timeBaseNs, const int64_t startTimeNs,
47 const std::unordered_map<int, std::shared_ptr<Activation>>& eventActivationMap = {},
48 const std::unordered_map<int, std::vector<std::shared_ptr<Activation>>>&
49 eventDeactivationMap = {},
50 const vector<int>& slicedStateAtoms = {},
51 const unordered_map<int, unordered_map<int, int64_t>>& stateGroupMap = {});
Yao Chen44cf27c2017-09-14 22:32:50 -070052
53 virtual ~CountMetricProducer();
54
tsaichristine8d73dc92019-12-06 02:11:02 -080055 void onStateChanged(const int64_t eventTimeNs, const int32_t atomId,
56 const HashableDimensionKey& primaryKey, int oldState,
tsaichristined21aacf2019-10-07 14:47:38 -070057 int newState) override;
58
Yao Chenb7041772017-10-20 16:59:25 -070059protected:
Yangsterf2bee6f2017-11-29 12:01:05 -080060 void onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -080061 const size_t matcherIndex, const MetricDimensionKey& eventKey,
tsaichristinec876b492019-12-10 13:47:05 -080062 const ConditionKey& conditionKey, bool condition, const LogEvent& event,
63 const std::map<int, HashableDimensionKey>& statePrimaryKeys) override;
yro2b0f8862017-11-06 14:27:31 -080064
Yao Chen44cf27c2017-09-14 22:32:50 -070065private:
Yangster-mace68f3a52018-04-04 00:01:43 -070066
Yangster-macb142cc82018-03-30 15:22:08 -070067 void onDumpReportLocked(const int64_t dumpTimeNs,
Yangster-mace68f3a52018-04-04 00:01:43 -070068 const bool include_current_partial_bucket,
Bookatzff71cad2018-09-20 17:17:49 -070069 const bool erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +000070 const DumpLatency dumpLatency,
Yangster-mac9def8e32018-04-17 13:55:51 -070071 std::set<string> *str_set,
Yao Chen288c6002017-12-12 13:43:18 -080072 android::util::ProtoOutputStream* protoOutput) override;
Yangsterf2bee6f2017-11-29 12:01:05 -080073
Yangster-maca802d732018-04-24 07:50:38 -070074 void clearPastBucketsLocked(const int64_t dumpTimeNs) override;
75
Yangsterf2bee6f2017-11-29 12:01:05 -080076 // Internal interface to handle condition change.
Yangster-macb142cc82018-03-30 15:22:08 -070077 void onConditionChangedLocked(const bool conditionMet, const int64_t eventTime) override;
Yangsterf2bee6f2017-11-29 12:01:05 -080078
79 // Internal interface to handle sliced condition change.
Yangster-macb142cc82018-03-30 15:22:08 -070080 void onSlicedConditionMayChangeLocked(bool overallCondition, const int64_t eventTime) override;
Yangsterf2bee6f2017-11-29 12:01:05 -080081
82 // Internal function to calculate the current used bytes.
83 size_t byteSizeLocked() const override;
84
Yangster-maca78d0082018-03-12 12:02:56 -070085 void dumpStatesLocked(FILE* out, bool verbose) const override;
Yao Chen884c8c12018-01-26 10:36:25 -080086
Yangster-macb142cc82018-03-30 15:22:08 -070087 void dropDataLocked(const int64_t dropTimeNs) override;
Yao Chen06dba5d2018-01-26 13:38:16 -080088
Yangsterf2bee6f2017-11-29 12:01:05 -080089 // Util function to flush the old packet.
Yangster-macb142cc82018-03-30 15:22:08 -070090 void flushIfNeededLocked(const int64_t& newEventTime) override;
David Chen27785a82018-01-19 17:06:45 -080091
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +000092 void flushCurrentBucketLocked(const int64_t& eventTimeNs,
93 const int64_t& nextBucketStartTimeNs) override;
Yangsterf2bee6f2017-11-29 12:01:05 -080094
Yangster-mac93694462018-01-22 20:49:31 -080095 std::unordered_map<MetricDimensionKey, std::vector<CountBucket>> mPastBuckets;
yro24809bd2017-10-31 23:06:53 -070096
David Chen27785a82018-01-19 17:06:45 -080097 // The current bucket (may be a partial bucket).
Yang Lu3eba6212017-10-25 19:54:45 -070098 std::shared_ptr<DimToValMap> mCurrentSlicedCounter = std::make_shared<DimToValMap>();
Yao Chen729093d2017-10-16 10:33:26 -070099
David Chen27785a82018-01-19 17:06:45 -0800100 // The sum of previous partial buckets in the current full bucket (excluding the current
101 // partial bucket). This is only updated while flushing the current bucket.
102 std::shared_ptr<DimToValMap> mCurrentFullCounters = std::make_shared<DimToValMap>();
103
Yangster-mace2cd6d52017-11-09 20:38:30 -0800104 static const size_t kBucketSize = sizeof(CountBucket{});
yro24809bd2017-10-31 23:06:53 -0700105
Yangster-mac93694462018-01-22 20:49:31 -0800106 bool hitGuardRailLocked(const MetricDimensionKey& newKey);
Yao Chenb3561512017-11-21 18:07:17 -0800107
Yao Chen93fe3a32017-11-02 13:52:59 -0700108 FRIEND_TEST(CountMetricProducerTest, TestNonDimensionalEvents);
109 FRIEND_TEST(CountMetricProducerTest, TestEventsWithNonSlicedCondition);
110 FRIEND_TEST(CountMetricProducerTest, TestEventsWithSlicedCondition);
Bookatz1bf94382018-01-04 11:43:20 -0800111 FRIEND_TEST(CountMetricProducerTest, TestAnomalyDetectionUnSliced);
Chenjie Yue1361ed2018-07-23 17:33:09 -0700112 FRIEND_TEST(CountMetricProducerTest, TestFirstBucket);
Ruchir Rastogi060fbfc2019-10-11 13:16:51 -0700113 FRIEND_TEST(CountMetricProducerTest, TestOneWeekTimeUnit);
Tej Singhe678cb72020-04-14 16:23:30 -0700114
115 FRIEND_TEST(CountMetricProducerTest_PartialBucket, TestSplitInCurrentBucket);
116 FRIEND_TEST(CountMetricProducerTest_PartialBucket, TestSplitInNextBucket);
Yao Chen44cf27c2017-09-14 22:32:50 -0700117};
118
119} // namespace statsd
120} // namespace os
121} // namespace android
122#endif // COUNT_METRIC_PRODUCER_H