Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -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 | #pragma once |
| 18 | |
| 19 | #include <utils/threads.h> |
| 20 | #include <list> |
| 21 | #include "../condition/ConditionTracker.h" |
| 22 | #include "../external/PullDataReceiver.h" |
| 23 | #include "../external/StatsPullerManager.h" |
| 24 | #include "CountAnomalyTracker.h" |
| 25 | #include "MetricProducer.h" |
| 26 | #include "frameworks/base/cmds/statsd/src/stats_log.pb.h" |
| 27 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
| 28 | |
| 29 | namespace android { |
| 30 | namespace os { |
| 31 | namespace statsd { |
| 32 | |
| 33 | class ValueMetricProducer : public virtual MetricProducer, public virtual PullDataReceiver { |
| 34 | public: |
| 35 | ValueMetricProducer(const ValueMetric& valueMetric, const int conditionIndex, |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame^] | 36 | const sp<ConditionWizard>& wizard, const int pullTagId); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 37 | |
| 38 | virtual ~ValueMetricProducer(); |
| 39 | |
| 40 | void onConditionChanged(const bool condition, const uint64_t eventTime) override; |
| 41 | |
| 42 | void finish() override; |
| 43 | |
| 44 | StatsLogReport onDumpReport() override; |
| 45 | |
| 46 | void onSlicedConditionMayChange(const uint64_t eventTime); |
| 47 | |
| 48 | void onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& data) override; |
| 49 | // TODO: Implement this later. |
| 50 | size_t byteSize() override{return 0;}; |
| 51 | |
| 52 | // TODO: Implement this later. |
| 53 | virtual void notifyAppUpgrade(const string& apk, const int uid, const int version) override{}; |
| 54 | // TODO: Implement this later. |
| 55 | virtual void notifyAppRemoved(const string& apk, const int uid) override{}; |
| 56 | |
| 57 | protected: |
| 58 | void onMatchedLogEventInternal(const size_t matcherIndex, const HashableDimensionKey& eventKey, |
| 59 | const std::map<std::string, HashableDimensionKey>& conditionKey, |
| 60 | bool condition, const LogEvent& event, |
| 61 | bool scheduledPull) override; |
| 62 | |
| 63 | private: |
| 64 | const ValueMetric mMetric; |
| 65 | |
| 66 | StatsPullerManager& mStatsPullerManager = StatsPullerManager::GetInstance(); |
| 67 | |
| 68 | Mutex mLock; |
| 69 | |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame^] | 70 | // tagId for pulled data. -1 if this is not pulled |
| 71 | const int mPullTagId; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 72 | |
| 73 | // internal state of a bucket. |
| 74 | typedef struct { |
| 75 | std::vector<std::pair<long, long>> raw; |
| 76 | } Interval; |
| 77 | |
| 78 | std::unordered_map<HashableDimensionKey, Interval> mCurrentSlicedBucket; |
| 79 | // If condition is true and pulling on schedule, the previous bucket value needs to be carried |
| 80 | // over to the next bucket. |
| 81 | std::unordered_map<HashableDimensionKey, Interval> mNextSlicedBucket; |
| 82 | |
| 83 | // Save the past buckets and we can clear when the StatsLogReport is dumped. |
| 84 | std::unordered_map<HashableDimensionKey, std::vector<ValueBucketInfo>> mPastBuckets; |
| 85 | |
| 86 | long get_value(const LogEvent& event); |
| 87 | |
| 88 | void flush_if_needed(const uint64_t eventTimeNs); |
| 89 | }; |
| 90 | |
| 91 | } // namespace statsd |
| 92 | } // namespace os |
| 93 | } // namespace android |