blob: 293e5b971fa5d9721857e9429826b8d6c21648f0 [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.
45 CountMetricProducer(const CountMetric& countMetric, const int conditionIndex,
Yao Chen93fe3a32017-11-02 13:52:59 -070046 const sp<ConditionWizard>& wizard, const uint64_t startTimeNs);
Yao Chen44cf27c2017-09-14 22:32:50 -070047
48 virtual ~CountMetricProducer();
49
Yao Chen5154a3792017-10-30 22:57:06 -070050 void onConditionChanged(const bool conditionMet, const uint64_t eventTime) override;
Yao Chencaf339d2017-10-06 16:01:10 -070051
Yao Chen44cf27c2017-09-14 22:32:50 -070052 void finish() override;
53
Yangster-mace2cd6d52017-11-09 20:38:30 -080054 void flushIfNeeded(const uint64_t newEventTime) override;
55
yro2b0f8862017-11-06 14:27:31 -080056 // TODO: Pass a timestamp as a parameter in onDumpReport.
yro17adac92017-11-08 23:16:29 -080057 std::unique_ptr<std::vector<uint8_t>> onDumpReport() override;
Yao Chen729093d2017-10-16 10:33:26 -070058
Yao Chen5154a3792017-10-30 22:57:06 -070059 void onSlicedConditionMayChange(const uint64_t eventTime) override;
Yao Chen44cf27c2017-09-14 22:32:50 -070060
Yangster7c334a12017-11-22 14:24:24 -080061 size_t byteSize() const override;
yro69007c82017-10-26 20:42:57 -070062
David Chende701692017-10-05 13:16:02 -070063 // TODO: Implement this later.
Yao Chen729093d2017-10-16 10:33:26 -070064 virtual void notifyAppUpgrade(const string& apk, const int uid, const int version) override{};
David Chend6896892017-10-25 11:49:03 -070065 // TODO: Implement this later.
66 virtual void notifyAppRemoved(const string& apk, const int uid) override{};
David Chende701692017-10-05 13:16:02 -070067
Yao Chenb7041772017-10-20 16:59:25 -070068protected:
69 void onMatchedLogEventInternal(const size_t matcherIndex, const HashableDimensionKey& eventKey,
70 const std::map<std::string, HashableDimensionKey>& conditionKey,
Chenjie Yub3dda412017-10-24 13:41:59 -070071 bool condition, const LogEvent& event,
72 bool scheduledPull) override;
Yao Chenb7041772017-10-20 16:59:25 -070073
yro2b0f8862017-11-06 14:27:31 -080074 void startNewProtoOutputStream(long long timestamp) override;
75
Yao Chen44cf27c2017-09-14 22:32:50 -070076private:
77 const CountMetric mMetric;
78
yro2b0f8862017-11-06 14:27:31 -080079 // TODO: Add a lock to mPastBuckets.
Yao Chen93fe3a32017-11-02 13:52:59 -070080 std::unordered_map<HashableDimensionKey, std::vector<CountBucket>> mPastBuckets;
yro24809bd2017-10-31 23:06:53 -070081
Yao Chen729093d2017-10-16 10:33:26 -070082 // The current bucket.
Yang Lu3eba6212017-10-25 19:54:45 -070083 std::shared_ptr<DimToValMap> mCurrentSlicedCounter = std::make_shared<DimToValMap>();
Yao Chen729093d2017-10-16 10:33:26 -070084
Yangster-mace2cd6d52017-11-09 20:38:30 -080085 static const size_t kBucketSize = sizeof(CountBucket{});
yro24809bd2017-10-31 23:06:53 -070086
Yao Chen93fe3a32017-11-02 13:52:59 -070087 FRIEND_TEST(CountMetricProducerTest, TestNonDimensionalEvents);
88 FRIEND_TEST(CountMetricProducerTest, TestEventsWithNonSlicedCondition);
89 FRIEND_TEST(CountMetricProducerTest, TestEventsWithSlicedCondition);
Yangster-mace2cd6d52017-11-09 20:38:30 -080090 FRIEND_TEST(CountMetricProducerTest, TestAnomalyDetection);
Yao Chen44cf27c2017-09-14 22:32:50 -070091};
92
93} // namespace statsd
94} // namespace os
95} // namespace android
96#endif // COUNT_METRIC_PRODUCER_H