Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 1 | /* |
| 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 Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 20 | #include <unordered_map> |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 21 | |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 22 | #include <android/util/ProtoOutputStream.h> |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 23 | #include <gtest/gtest_prod.h> |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 24 | #include "../anomaly/AnomalyTracker.h" |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 25 | #include "../condition/ConditionTracker.h" |
| 26 | #include "../matchers/matcher_util.h" |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 27 | #include "MetricProducer.h" |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 28 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 29 | #include "stats_util.h" |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | namespace os { |
| 33 | namespace statsd { |
| 34 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 35 | struct CountBucket { |
| 36 | int64_t mBucketStartNs; |
| 37 | int64_t mBucketEndNs; |
| 38 | int64_t mCount; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 39 | uint64_t mBucketNum; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 42 | class CountMetricProducer : public MetricProducer { |
| 43 | public: |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 44 | // TODO: Pass in the start time from MetricsManager, it should be consistent for all metrics. |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame^] | 45 | CountMetricProducer(const ConfigKey& key, const CountMetric& countMetric, |
| 46 | const int conditionIndex, const sp<ConditionWizard>& wizard, |
| 47 | const uint64_t startTimeNs); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 48 | |
| 49 | virtual ~CountMetricProducer(); |
| 50 | |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 51 | void onConditionChanged(const bool conditionMet, const uint64_t eventTime) override; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 52 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 53 | void finish() override; |
| 54 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 55 | void flushIfNeeded(const uint64_t newEventTime) override; |
| 56 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 57 | // TODO: Pass a timestamp as a parameter in onDumpReport. |
yro | 17adac9 | 2017-11-08 23:16:29 -0800 | [diff] [blame] | 58 | std::unique_ptr<std::vector<uint8_t>> onDumpReport() override; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 59 | |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 60 | void onSlicedConditionMayChange(const uint64_t eventTime) override; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 61 | |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 62 | size_t byteSize() const override; |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 63 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 64 | // TODO: Implement this later. |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 65 | virtual void notifyAppUpgrade(const string& apk, const int uid, const int version) override{}; |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 66 | // TODO: Implement this later. |
| 67 | virtual void notifyAppRemoved(const string& apk, const int uid) override{}; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 68 | |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 69 | protected: |
| 70 | void onMatchedLogEventInternal(const size_t matcherIndex, const HashableDimensionKey& eventKey, |
| 71 | const std::map<std::string, HashableDimensionKey>& conditionKey, |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 72 | bool condition, const LogEvent& event, |
| 73 | bool scheduledPull) override; |
Yao Chen | b704177 | 2017-10-20 16:59:25 -0700 | [diff] [blame] | 74 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 75 | void startNewProtoOutputStream(long long timestamp) override; |
| 76 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 77 | private: |
| 78 | const CountMetric mMetric; |
| 79 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 80 | // TODO: Add a lock to mPastBuckets. |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 81 | std::unordered_map<HashableDimensionKey, std::vector<CountBucket>> mPastBuckets; |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 82 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 83 | // The current bucket. |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 84 | std::shared_ptr<DimToValMap> mCurrentSlicedCounter = std::make_shared<DimToValMap>(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 85 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 86 | static const size_t kBucketSize = sizeof(CountBucket{}); |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 87 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame^] | 88 | bool hitGuardRail(const HashableDimensionKey& newKey); |
| 89 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 90 | FRIEND_TEST(CountMetricProducerTest, TestNonDimensionalEvents); |
| 91 | FRIEND_TEST(CountMetricProducerTest, TestEventsWithNonSlicedCondition); |
| 92 | FRIEND_TEST(CountMetricProducerTest, TestEventsWithSlicedCondition); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 93 | FRIEND_TEST(CountMetricProducerTest, TestAnomalyDetection); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | } // namespace statsd |
| 97 | } // namespace os |
| 98 | } // namespace android |
| 99 | #endif // COUNT_METRIC_PRODUCER_H |