blob: a2efd3fa976bd75a236ab0359a3dada3f21fba58 [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;
Yangster-mace2cd6d52017-11-09 20:38:30 -080036 uint64_t mBucketNum;
yro2b0f8862017-11-06 14:27:31 -080037};
38
Chenjie Yub3dda412017-10-24 13:41:59 -070039class ValueMetricProducer : public virtual MetricProducer, public virtual PullDataReceiver {
40public:
Yao Chenb3561512017-11-21 18:07:17 -080041 ValueMetricProducer(const ConfigKey& key, const ValueMetric& valueMetric,
42 const int conditionIndex, const sp<ConditionWizard>& wizard,
43 const int pullTagId, const uint64_t startTimeNs);
Chenjie Yub3dda412017-10-24 13:41:59 -070044
45 virtual ~ValueMetricProducer();
46
Chenjie Yub3dda412017-10-24 13:41:59 -070047 void finish() override;
Chenjie Yub3dda412017-10-24 13:41:59 -070048
49 void onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& data) override;
yro2b0f8862017-11-06 14:27:31 -080050
Chenjie Yub3dda412017-10-24 13:41:59 -070051 // TODO: Implement this later.
Dianne Hackborn3accca02013-09-20 09:32:11 -070052 virtual void notifyAppUpgrade(const string& apk, const int uid, const int64_t version)
53 override{};
Chenjie Yub3dda412017-10-24 13:41:59 -070054 // TODO: Implement this later.
55 virtual void notifyAppRemoved(const string& apk, const int uid) override{};
56
57protected:
Yangsterf2bee6f2017-11-29 12:01:05 -080058 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;
yro2b0f8862017-11-06 14:27:31 -080062
Chenjie Yub3dda412017-10-24 13:41:59 -070063private:
Yangsterf2bee6f2017-11-29 12:01:05 -080064 // 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 Yub3dda412017-10-24 13:41:59 -070082 const ValueMetric mMetric;
83
Chenjie Yu6736c892017-11-09 10:50:09 -080084 std::shared_ptr<StatsPullerManager> mStatsPullerManager;
85
86 // for testing
Yao Chenb3561512017-11-21 18:07:17 -080087 ValueMetricProducer(const ConfigKey& key, const ValueMetric& valueMetric,
88 const int conditionIndex, const sp<ConditionWizard>& wizard,
89 const int pullTagId, const uint64_t startTimeNs,
Chenjie Yu6736c892017-11-09 10:50:09 -080090 std::shared_ptr<StatsPullerManager> statsPullerManager);
Chenjie Yub3dda412017-10-24 13:41:59 -070091
Chenjie Yu5305e1d2017-10-31 13:49:36 -070092 // tagId for pulled data. -1 if this is not pulled
93 const int mPullTagId;
Chenjie Yub3dda412017-10-24 13:41:59 -070094
95 // internal state of a bucket.
96 typedef struct {
97 std::vector<std::pair<long, long>> raw;
Chenjie Yu6736c892017-11-09 10:50:09 -080098 bool tainted;
Chenjie Yub3dda412017-10-24 13:41:59 -070099 } 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.
yro2b0f8862017-11-06 14:27:31 -0800107 // TODO: Add a lock to mPastBuckets.
108 std::unordered_map<HashableDimensionKey, std::vector<ValueBucket>> mPastBuckets;
Chenjie Yub3dda412017-10-24 13:41:59 -0700109
Yao Chen6a8c7992017-11-29 20:02:07 +0000110 long get_value(const LogEvent& event);
Chenjie Yub3dda412017-10-24 13:41:59 -0700111
Yangsterf2bee6f2017-11-29 12:01:05 -0800112 // Util function to check whether the specified dimension hits the guardrail.
113 bool hitGuardRailLocked(const HashableDimensionKey& newKey);
Yao Chenb3561512017-11-21 18:07:17 -0800114
Yangster-mace2cd6d52017-11-09 20:38:30 -0800115 static const size_t kBucketSize = sizeof(ValueBucket{});
Chenjie Yu6736c892017-11-09 10:50:09 -0800116
117 FRIEND_TEST(ValueMetricProducerTest, TestNonDimensionalEvents);
118 FRIEND_TEST(ValueMetricProducerTest, TestEventsWithNonSlicedCondition);
119 FRIEND_TEST(ValueMetricProducerTest, TestPushedEventsWithoutCondition);
Chenjie Yub3dda412017-10-24 13:41:59 -0700120};
121
122} // namespace statsd
123} // namespace os
124} // namespace android