blob: 16fc7ee3c6e9dd624912a719731a18d32c66e4c0 [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;
Yangster-mace2cd6d52017-11-09 20:38:30 -080039 uint64_t mBucketNum;
Yao Chen93fe3a32017-11-02 13:52:59 -070040};
41
Yao Chen44cf27c2017-09-14 22:32:50 -070042class CountMetricProducer : public MetricProducer {
43public:
Yao Chen729093d2017-10-16 10:33:26 -070044 // TODO: Pass in the start time from MetricsManager, it should be consistent for all metrics.
Yao Chenb3561512017-11-21 18:07:17 -080045 CountMetricProducer(const ConfigKey& key, const CountMetric& countMetric,
46 const int conditionIndex, const sp<ConditionWizard>& wizard,
47 const uint64_t startTimeNs);
Yao Chen44cf27c2017-09-14 22:32:50 -070048
49 virtual ~CountMetricProducer();
50
Yao Chenb7041772017-10-20 16:59:25 -070051protected:
Yangsterf2bee6f2017-11-29 12:01:05 -080052 void onMatchedLogEventInternalLocked(
53 const size_t matcherIndex, const HashableDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -080054 const ConditionKey& conditionKey, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -080055 const LogEvent& event) override;
yro2b0f8862017-11-06 14:27:31 -080056
Yao Chen44cf27c2017-09-14 22:32:50 -070057private:
Yao Chen288c6002017-12-12 13:43:18 -080058 void onDumpReportLocked(const uint64_t dumpTimeNs,
59 android::util::ProtoOutputStream* protoOutput) override;
Yangster-mac20877162017-12-22 17:19:39 -080060 void onDumpReportLocked(const uint64_t dumpTimeNs, StatsLogReport* report) override;
Yangsterf2bee6f2017-11-29 12:01:05 -080061
62 // Internal interface to handle condition change.
63 void onConditionChangedLocked(const bool conditionMet, const uint64_t eventTime) override;
64
65 // Internal interface to handle sliced condition change.
66 void onSlicedConditionMayChangeLocked(const uint64_t eventTime) override;
67
68 // Internal function to calculate the current used bytes.
69 size_t byteSizeLocked() const override;
70
71 // Util function to flush the old packet.
72 void flushIfNeededLocked(const uint64_t& newEventTime);
73
yro2b0f8862017-11-06 14:27:31 -080074 // TODO: Add a lock to mPastBuckets.
Yao Chen93fe3a32017-11-02 13:52:59 -070075 std::unordered_map<HashableDimensionKey, std::vector<CountBucket>> mPastBuckets;
yro24809bd2017-10-31 23:06:53 -070076
Yao Chen729093d2017-10-16 10:33:26 -070077 // The current bucket.
Yang Lu3eba6212017-10-25 19:54:45 -070078 std::shared_ptr<DimToValMap> mCurrentSlicedCounter = std::make_shared<DimToValMap>();
Yao Chen729093d2017-10-16 10:33:26 -070079
Yangster-mace2cd6d52017-11-09 20:38:30 -080080 static const size_t kBucketSize = sizeof(CountBucket{});
yro24809bd2017-10-31 23:06:53 -070081
Yangsterf2bee6f2017-11-29 12:01:05 -080082 bool hitGuardRailLocked(const HashableDimensionKey& newKey);
Yao Chenb3561512017-11-21 18:07:17 -080083
Yao Chen93fe3a32017-11-02 13:52:59 -070084 FRIEND_TEST(CountMetricProducerTest, TestNonDimensionalEvents);
85 FRIEND_TEST(CountMetricProducerTest, TestEventsWithNonSlicedCondition);
86 FRIEND_TEST(CountMetricProducerTest, TestEventsWithSlicedCondition);
Bookatz1bf94382018-01-04 11:43:20 -080087 FRIEND_TEST(CountMetricProducerTest, TestAnomalyDetectionUnSliced);
Yao Chen44cf27c2017-09-14 22:32:50 -070088};
89
90} // namespace statsd
91} // namespace os
92} // namespace android
93#endif // COUNT_METRIC_PRODUCER_H