blob: ef9868b613b223250f4423afdf63def53030a3ec [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
19#include <utils/threads.h>
20#include <list>
21#include "../condition/ConditionTracker.h"
22#include "../external/PullDataReceiver.h"
23#include "../external/StatsPullerManager.h"
Chenjie Yub3dda412017-10-24 13:41:59 -070024#include "MetricProducer.h"
Chenjie Yub3dda412017-10-24 13:41:59 -070025#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
26
27namespace android {
28namespace os {
29namespace statsd {
30
yro2b0f8862017-11-06 14:27:31 -080031struct ValueBucket {
32 int64_t mBucketStartNs;
33 int64_t mBucketEndNs;
34 int64_t mValue;
35};
36
Chenjie Yub3dda412017-10-24 13:41:59 -070037class ValueMetricProducer : public virtual MetricProducer, public virtual PullDataReceiver {
38public:
39 ValueMetricProducer(const ValueMetric& valueMetric, const int conditionIndex,
Yao Chen93fe3a32017-11-02 13:52:59 -070040 const sp<ConditionWizard>& wizard, const int pullTagId,
41 const uint64_t startTimeNs);
Chenjie Yub3dda412017-10-24 13:41:59 -070042
43 virtual ~ValueMetricProducer();
44
45 void onConditionChanged(const bool condition, const uint64_t eventTime) override;
46
47 void finish() override;
48
yro2b0f8862017-11-06 14:27:31 -080049 // TODO: Pass a timestamp as a parameter in onDumpReport.
yro17adac92017-11-08 23:16:29 -080050 std::unique_ptr<std::vector<uint8_t>> onDumpReport() override;
Chenjie Yub3dda412017-10-24 13:41:59 -070051
52 void onSlicedConditionMayChange(const uint64_t eventTime);
53
54 void onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& data) override;
yro2b0f8862017-11-06 14:27:31 -080055
56 size_t byteSize() override;
Chenjie Yub3dda412017-10-24 13:41:59 -070057
58 // TODO: Implement this later.
59 virtual void notifyAppUpgrade(const string& apk, const int uid, const int version) override{};
60 // TODO: Implement this later.
61 virtual void notifyAppRemoved(const string& apk, const int uid) override{};
62
63protected:
64 void onMatchedLogEventInternal(const size_t matcherIndex, const HashableDimensionKey& eventKey,
65 const std::map<std::string, HashableDimensionKey>& conditionKey,
66 bool condition, const LogEvent& event,
67 bool scheduledPull) override;
68
yro2b0f8862017-11-06 14:27:31 -080069 void startNewProtoOutputStream(long long timestamp) override;
70
Chenjie Yub3dda412017-10-24 13:41:59 -070071private:
72 const ValueMetric mMetric;
73
74 StatsPullerManager& mStatsPullerManager = StatsPullerManager::GetInstance();
75
76 Mutex mLock;
77
Chenjie Yu5305e1d2017-10-31 13:49:36 -070078 // tagId for pulled data. -1 if this is not pulled
79 const int mPullTagId;
Chenjie Yub3dda412017-10-24 13:41:59 -070080
81 // internal state of a bucket.
82 typedef struct {
83 std::vector<std::pair<long, long>> raw;
84 } Interval;
85
86 std::unordered_map<HashableDimensionKey, Interval> mCurrentSlicedBucket;
87 // If condition is true and pulling on schedule, the previous bucket value needs to be carried
88 // over to the next bucket.
89 std::unordered_map<HashableDimensionKey, Interval> mNextSlicedBucket;
90
91 // Save the past buckets and we can clear when the StatsLogReport is dumped.
yro2b0f8862017-11-06 14:27:31 -080092 // TODO: Add a lock to mPastBuckets.
93 std::unordered_map<HashableDimensionKey, std::vector<ValueBucket>> mPastBuckets;
Chenjie Yub3dda412017-10-24 13:41:59 -070094
95 long get_value(const LogEvent& event);
96
97 void flush_if_needed(const uint64_t eventTimeNs);
yro2b0f8862017-11-06 14:27:31 -080098
99 size_t mByteSize;
Chenjie Yub3dda412017-10-24 13:41:59 -0700100};
101
102} // namespace statsd
103} // namespace os
104} // namespace android