blob: f78a199de103364c4b27e7b4e22b49e806921b51 [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 Chen5154a3792017-10-30 22:57:06 -070051 void onConditionChanged(const bool conditionMet, const uint64_t eventTime) override;
Yao Chencaf339d2017-10-06 16:01:10 -070052
Yao Chen44cf27c2017-09-14 22:32:50 -070053 void finish() override;
54
Yangster-mace2cd6d52017-11-09 20:38:30 -080055 void flushIfNeeded(const uint64_t newEventTime) override;
56
yro2b0f8862017-11-06 14:27:31 -080057 // TODO: Pass a timestamp as a parameter in onDumpReport.
yro17adac92017-11-08 23:16:29 -080058 std::unique_ptr<std::vector<uint8_t>> onDumpReport() override;
Yao Chen729093d2017-10-16 10:33:26 -070059
Yao Chen5154a3792017-10-30 22:57:06 -070060 void onSlicedConditionMayChange(const uint64_t eventTime) override;
Yao Chen44cf27c2017-09-14 22:32:50 -070061
Yangster7c334a12017-11-22 14:24:24 -080062 size_t byteSize() const override;
yro69007c82017-10-26 20:42:57 -070063
David Chende701692017-10-05 13:16:02 -070064 // TODO: Implement this later.
Yao Chen729093d2017-10-16 10:33:26 -070065 virtual void notifyAppUpgrade(const string& apk, const int uid, const int version) override{};
David Chend6896892017-10-25 11:49:03 -070066 // TODO: Implement this later.
67 virtual void notifyAppRemoved(const string& apk, const int uid) override{};
David Chende701692017-10-05 13:16:02 -070068
Yao Chenb7041772017-10-20 16:59:25 -070069protected:
70 void onMatchedLogEventInternal(const size_t matcherIndex, const HashableDimensionKey& eventKey,
71 const std::map<std::string, HashableDimensionKey>& conditionKey,
Chenjie Yub3dda412017-10-24 13:41:59 -070072 bool condition, const LogEvent& event,
73 bool scheduledPull) override;
Yao Chenb7041772017-10-20 16:59:25 -070074
yro2b0f8862017-11-06 14:27:31 -080075 void startNewProtoOutputStream(long long timestamp) override;
76
Yao Chen44cf27c2017-09-14 22:32:50 -070077private:
78 const CountMetric mMetric;
79
yro2b0f8862017-11-06 14:27:31 -080080 // TODO: Add a lock to mPastBuckets.
Yao Chen93fe3a32017-11-02 13:52:59 -070081 std::unordered_map<HashableDimensionKey, std::vector<CountBucket>> mPastBuckets;
yro24809bd2017-10-31 23:06:53 -070082
Yao Chen729093d2017-10-16 10:33:26 -070083 // The current bucket.
Yang Lu3eba6212017-10-25 19:54:45 -070084 std::shared_ptr<DimToValMap> mCurrentSlicedCounter = std::make_shared<DimToValMap>();
Yao Chen729093d2017-10-16 10:33:26 -070085
Yangster-mace2cd6d52017-11-09 20:38:30 -080086 static const size_t kBucketSize = sizeof(CountBucket{});
yro24809bd2017-10-31 23:06:53 -070087
Yao Chenb3561512017-11-21 18:07:17 -080088 bool hitGuardRail(const HashableDimensionKey& newKey);
89
Yao Chen93fe3a32017-11-02 13:52:59 -070090 FRIEND_TEST(CountMetricProducerTest, TestNonDimensionalEvents);
91 FRIEND_TEST(CountMetricProducerTest, TestEventsWithNonSlicedCondition);
92 FRIEND_TEST(CountMetricProducerTest, TestEventsWithSlicedCondition);
Yangster-mace2cd6d52017-11-09 20:38:30 -080093 FRIEND_TEST(CountMetricProducerTest, TestAnomalyDetection);
Yao Chen44cf27c2017-09-14 22:32:50 -070094};
95
96} // namespace statsd
97} // namespace os
98} // namespace android
99#endif // COUNT_METRIC_PRODUCER_H