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