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 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 33 | struct ValueBucket { |
| 34 | int64_t mBucketStartNs; |
| 35 | int64_t mBucketEndNs; |
| 36 | int64_t mValue; |
| 37 | }; |
| 38 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 39 | class ValueMetricProducer : public virtual MetricProducer, public virtual PullDataReceiver { |
| 40 | public: |
| 41 | ValueMetricProducer(const ValueMetric& valueMetric, const int conditionIndex, |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 42 | const sp<ConditionWizard>& wizard, const int pullTagId, |
| 43 | const uint64_t startTimeNs); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 44 | |
| 45 | virtual ~ValueMetricProducer(); |
| 46 | |
| 47 | void onConditionChanged(const bool condition, const uint64_t eventTime) override; |
| 48 | |
| 49 | void finish() override; |
| 50 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 51 | // TODO: Pass a timestamp as a parameter in onDumpReport. |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 52 | StatsLogReport onDumpReport() override; |
| 53 | |
| 54 | void onSlicedConditionMayChange(const uint64_t eventTime); |
| 55 | |
| 56 | void onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& data) override; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 57 | |
| 58 | size_t byteSize() override; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 59 | |
| 60 | // TODO: Implement this later. |
| 61 | virtual void notifyAppUpgrade(const string& apk, const int uid, const int version) override{}; |
| 62 | // TODO: Implement this later. |
| 63 | virtual void notifyAppRemoved(const string& apk, const int uid) override{}; |
| 64 | |
| 65 | protected: |
| 66 | void onMatchedLogEventInternal(const size_t matcherIndex, const HashableDimensionKey& eventKey, |
| 67 | const std::map<std::string, HashableDimensionKey>& conditionKey, |
| 68 | bool condition, const LogEvent& event, |
| 69 | bool scheduledPull) override; |
| 70 | |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 71 | void startNewProtoOutputStream(long long timestamp) override; |
| 72 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 73 | private: |
| 74 | const ValueMetric mMetric; |
| 75 | |
| 76 | StatsPullerManager& mStatsPullerManager = StatsPullerManager::GetInstance(); |
| 77 | |
| 78 | Mutex mLock; |
| 79 | |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 80 | // tagId for pulled data. -1 if this is not pulled |
| 81 | const int mPullTagId; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 82 | |
| 83 | // internal state of a bucket. |
| 84 | typedef struct { |
| 85 | std::vector<std::pair<long, long>> raw; |
| 86 | } Interval; |
| 87 | |
| 88 | std::unordered_map<HashableDimensionKey, Interval> mCurrentSlicedBucket; |
| 89 | // If condition is true and pulling on schedule, the previous bucket value needs to be carried |
| 90 | // over to the next bucket. |
| 91 | std::unordered_map<HashableDimensionKey, Interval> mNextSlicedBucket; |
| 92 | |
| 93 | // Save the past buckets and we can clear when the StatsLogReport is dumped. |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 94 | // TODO: Add a lock to mPastBuckets. |
| 95 | std::unordered_map<HashableDimensionKey, std::vector<ValueBucket>> mPastBuckets; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 96 | |
| 97 | long get_value(const LogEvent& event); |
| 98 | |
| 99 | void flush_if_needed(const uint64_t eventTimeNs); |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 100 | |
| 101 | size_t mByteSize; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | } // namespace statsd |
| 105 | } // namespace os |
| 106 | } // namespace android |