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 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 19 | #include <gtest/gtest_prod.h> |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 20 | #include <utils/threads.h> |
| 21 | #include <list> |
| 22 | #include "../condition/ConditionTracker.h" |
| 23 | #include "../external/PullDataReceiver.h" |
| 24 | #include "../external/StatsPullerManager.h" |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 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; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 36 | uint64_t mBucketNum; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 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: |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 41 | ValueMetricProducer(const ConfigKey& key, const ValueMetric& valueMetric, |
| 42 | const int conditionIndex, const sp<ConditionWizard>& wizard, |
| 43 | const int pullTagId, const uint64_t startTimeNs); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 44 | |
| 45 | virtual ~ValueMetricProducer(); |
| 46 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 47 | void finish() override; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 48 | |
| 49 | void onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& data) override; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 50 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 51 | // TODO: Implement this later. |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame^] | 52 | virtual void notifyAppUpgrade(const string& apk, const int uid, const int64_t version) |
| 53 | override{}; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 54 | // TODO: Implement this later. |
| 55 | virtual void notifyAppRemoved(const string& apk, const int uid) override{}; |
| 56 | |
| 57 | protected: |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 58 | void onMatchedLogEventInternalLocked( |
| 59 | const size_t matcherIndex, const HashableDimensionKey& eventKey, |
| 60 | const std::map<std::string, HashableDimensionKey>& conditionKey, bool condition, |
| 61 | const LogEvent& event, bool scheduledPull) override; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 62 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 63 | private: |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 64 | // TODO: Pass a timestamp as a parameter in onDumpReport. |
| 65 | std::unique_ptr<std::vector<uint8_t>> onDumpReportLocked() override; |
| 66 | |
| 67 | // Internal interface to handle condition change. |
| 68 | void onConditionChangedLocked(const bool conditionMet, const uint64_t eventTime) override; |
| 69 | |
| 70 | // Internal interface to handle sliced condition change. |
| 71 | void onSlicedConditionMayChangeLocked(const uint64_t eventTime) override; |
| 72 | |
| 73 | // Internal function to calculate the current used bytes. |
| 74 | size_t byteSizeLocked() const override; |
| 75 | |
| 76 | // Util function to flush the old packet. |
| 77 | void flushIfNeededLocked(const uint64_t& eventTime); |
| 78 | |
| 79 | // Util function to init/reset the proto output stream. |
| 80 | void startNewProtoOutputStreamLocked(long long timestamp); |
| 81 | |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 82 | const ValueMetric mMetric; |
| 83 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 84 | std::shared_ptr<StatsPullerManager> mStatsPullerManager; |
| 85 | |
| 86 | // for testing |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 87 | ValueMetricProducer(const ConfigKey& key, const ValueMetric& valueMetric, |
| 88 | const int conditionIndex, const sp<ConditionWizard>& wizard, |
| 89 | const int pullTagId, const uint64_t startTimeNs, |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 90 | std::shared_ptr<StatsPullerManager> statsPullerManager); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 91 | |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 92 | // tagId for pulled data. -1 if this is not pulled |
| 93 | const int mPullTagId; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 94 | |
| 95 | // internal state of a bucket. |
| 96 | typedef struct { |
| 97 | std::vector<std::pair<long, long>> raw; |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 98 | bool tainted; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 99 | } Interval; |
| 100 | |
| 101 | std::unordered_map<HashableDimensionKey, Interval> mCurrentSlicedBucket; |
| 102 | // If condition is true and pulling on schedule, the previous bucket value needs to be carried |
| 103 | // over to the next bucket. |
| 104 | std::unordered_map<HashableDimensionKey, Interval> mNextSlicedBucket; |
| 105 | |
| 106 | // Save the past buckets and we can clear when the StatsLogReport is dumped. |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 107 | // TODO: Add a lock to mPastBuckets. |
| 108 | std::unordered_map<HashableDimensionKey, std::vector<ValueBucket>> mPastBuckets; |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 109 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 110 | long get_value(const LogEvent& event); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 111 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 112 | // Util function to check whether the specified dimension hits the guardrail. |
| 113 | bool hitGuardRailLocked(const HashableDimensionKey& newKey); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 114 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 115 | static const size_t kBucketSize = sizeof(ValueBucket{}); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 116 | |
| 117 | FRIEND_TEST(ValueMetricProducerTest, TestNonDimensionalEvents); |
| 118 | FRIEND_TEST(ValueMetricProducerTest, TestEventsWithNonSlicedCondition); |
| 119 | FRIEND_TEST(ValueMetricProducerTest, TestPushedEventsWithoutCondition); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | } // namespace statsd |
| 123 | } // namespace os |
| 124 | } // namespace android |