blob: c6c87f5b15f67695fa9f8be63d5aaf0f3adf5119 [file] [log] [blame]
Chenjie Yub3dda412017-10-24 13:41:59 -07001/*
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 Yu6736c892017-11-09 10:50:09 -080019#include <gtest/gtest_prod.h>
Chenjie Yub3dda412017-10-24 13:41:59 -070020#include <utils/threads.h>
21#include <list>
22#include "../condition/ConditionTracker.h"
23#include "../external/PullDataReceiver.h"
24#include "../external/StatsPullerManager.h"
Chenjie Yub3dda412017-10-24 13:41:59 -070025#include "MetricProducer.h"
Chenjie Yub3dda412017-10-24 13:41:59 -070026#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
27
28namespace android {
29namespace os {
30namespace statsd {
31
yro2b0f8862017-11-06 14:27:31 -080032struct ValueBucket {
33 int64_t mBucketStartNs;
34 int64_t mBucketEndNs;
35 int64_t mValue;
36};
37
Chenjie Yub3dda412017-10-24 13:41:59 -070038class ValueMetricProducer : public virtual MetricProducer, public virtual PullDataReceiver {
39public:
40 ValueMetricProducer(const ValueMetric& valueMetric, const int conditionIndex,
Yao Chen93fe3a32017-11-02 13:52:59 -070041 const sp<ConditionWizard>& wizard, const int pullTagId,
42 const uint64_t startTimeNs);
Chenjie Yub3dda412017-10-24 13:41:59 -070043
44 virtual ~ValueMetricProducer();
45
46 void onConditionChanged(const bool condition, const uint64_t eventTime) override;
47
48 void finish() override;
49
yro2b0f8862017-11-06 14:27:31 -080050 // TODO: Pass a timestamp as a parameter in onDumpReport.
yro17adac92017-11-08 23:16:29 -080051 std::unique_ptr<std::vector<uint8_t>> onDumpReport() override;
Chenjie Yub3dda412017-10-24 13:41:59 -070052
53 void onSlicedConditionMayChange(const uint64_t eventTime);
54
55 void onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& data) override;
yro2b0f8862017-11-06 14:27:31 -080056
57 size_t byteSize() override;
Chenjie Yub3dda412017-10-24 13:41:59 -070058
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
64protected:
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
yro2b0f8862017-11-06 14:27:31 -080070 void startNewProtoOutputStream(long long timestamp) override;
71
Chenjie Yub3dda412017-10-24 13:41:59 -070072private:
73 const ValueMetric mMetric;
74
Chenjie Yu6736c892017-11-09 10:50:09 -080075 std::shared_ptr<StatsPullerManager> mStatsPullerManager;
76
77 // for testing
78 ValueMetricProducer(const ValueMetric& valueMetric, const int conditionIndex,
79 const sp<ConditionWizard>& wizard, const int pullTagId,
80 const uint64_t startTimeNs,
81 std::shared_ptr<StatsPullerManager> statsPullerManager);
Chenjie Yub3dda412017-10-24 13:41:59 -070082
83 Mutex mLock;
84
Chenjie Yu5305e1d2017-10-31 13:49:36 -070085 // tagId for pulled data. -1 if this is not pulled
86 const int mPullTagId;
Chenjie Yub3dda412017-10-24 13:41:59 -070087
88 // internal state of a bucket.
89 typedef struct {
90 std::vector<std::pair<long, long>> raw;
Chenjie Yu6736c892017-11-09 10:50:09 -080091 bool tainted;
Chenjie Yub3dda412017-10-24 13:41:59 -070092 } Interval;
93
94 std::unordered_map<HashableDimensionKey, Interval> mCurrentSlicedBucket;
95 // If condition is true and pulling on schedule, the previous bucket value needs to be carried
96 // over to the next bucket.
97 std::unordered_map<HashableDimensionKey, Interval> mNextSlicedBucket;
98
99 // Save the past buckets and we can clear when the StatsLogReport is dumped.
yro2b0f8862017-11-06 14:27:31 -0800100 // TODO: Add a lock to mPastBuckets.
101 std::unordered_map<HashableDimensionKey, std::vector<ValueBucket>> mPastBuckets;
Chenjie Yub3dda412017-10-24 13:41:59 -0700102
103 long get_value(const LogEvent& event);
104
105 void flush_if_needed(const uint64_t eventTimeNs);
yro2b0f8862017-11-06 14:27:31 -0800106
107 size_t mByteSize;
Chenjie Yu6736c892017-11-09 10:50:09 -0800108
109 FRIEND_TEST(ValueMetricProducerTest, TestNonDimensionalEvents);
110 FRIEND_TEST(ValueMetricProducerTest, TestEventsWithNonSlicedCondition);
111 FRIEND_TEST(ValueMetricProducerTest, TestPushedEventsWithoutCondition);
Chenjie Yub3dda412017-10-24 13:41:59 -0700112};
113
114} // namespace statsd
115} // namespace os
116} // namespace android