blob: a65092fb3b60addbffef5727f1dcbaf4f90ef327 [file] [log] [blame]
Yao Chen5110bed2017-10-23 12:50:02 -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#define DEBUG true // STOPSHIP if true
18#include "Log.h"
19
20#include "EventMetricProducer.h"
21#include "stats_util.h"
22
Yao Chen5110bed2017-10-23 12:50:02 -070023#include <limits.h>
24#include <stdlib.h>
25
yrob0378b02017-11-09 20:36:25 -080026using android::util::FIELD_COUNT_REPEATED;
yro2b0f8862017-11-06 14:27:31 -080027using android::util::FIELD_TYPE_BOOL;
28using android::util::FIELD_TYPE_FLOAT;
29using android::util::FIELD_TYPE_INT32;
30using android::util::FIELD_TYPE_INT64;
31using android::util::FIELD_TYPE_MESSAGE;
Yao Chen5110bed2017-10-23 12:50:02 -070032using android::util::ProtoOutputStream;
33using std::map;
34using std::string;
35using std::unordered_map;
36using std::vector;
37
38namespace android {
39namespace os {
40namespace statsd {
41
42// for StatsLogReport
43const int FIELD_ID_METRIC_ID = 1;
44const int FIELD_ID_START_REPORT_NANOS = 2;
yro24809bd2017-10-31 23:06:53 -070045const int FIELD_ID_END_REPORT_NANOS = 3;
Yao Chen5110bed2017-10-23 12:50:02 -070046const int FIELD_ID_EVENT_METRICS = 4;
yro24809bd2017-10-31 23:06:53 -070047// for EventMetricDataWrapper
48const int FIELD_ID_DATA = 1;
Yao Chen5110bed2017-10-23 12:50:02 -070049// for EventMetricData
50const int FIELD_ID_TIMESTAMP_NANOS = 1;
51const int FIELD_ID_STATS_EVENTS = 2;
Yao Chen5110bed2017-10-23 12:50:02 -070052
53EventMetricProducer::EventMetricProducer(const EventMetric& metric, const int conditionIndex,
Yao Chen93fe3a32017-11-02 13:52:59 -070054 const sp<ConditionWizard>& wizard,
55 const uint64_t startTimeNs)
56 : MetricProducer(startTimeNs, conditionIndex, wizard), mMetric(metric) {
Yao Chen5110bed2017-10-23 12:50:02 -070057 if (metric.links().size() > 0) {
58 mConditionLinks.insert(mConditionLinks.begin(), metric.links().begin(),
59 metric.links().end());
60 mConditionSliced = true;
61 }
62
63 startNewProtoOutputStream(mStartTimeNs);
64
65 VLOG("metric %lld created. bucket size %lld start_time: %lld", metric.metric_id(),
66 (long long)mBucketSizeNs, (long long)mStartTimeNs);
67}
68
69EventMetricProducer::~EventMetricProducer() {
70 VLOG("~EventMetricProducer() called");
71}
72
73void EventMetricProducer::startNewProtoOutputStream(long long startTime) {
74 mProto = std::make_unique<ProtoOutputStream>();
75 // TODO: We need to auto-generate the field IDs for StatsLogReport, EventMetricData,
76 // and StatsEvent.
yro24809bd2017-10-31 23:06:53 -070077 mProto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_ID, mMetric.metric_id());
78 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, startTime);
79 mProtoToken = mProto->start(FIELD_TYPE_MESSAGE | FIELD_ID_EVENT_METRICS);
Yao Chen5110bed2017-10-23 12:50:02 -070080}
81
82void EventMetricProducer::finish() {
83}
84
Yao Chen5154a372017-10-30 22:57:06 -070085void EventMetricProducer::onSlicedConditionMayChange(const uint64_t eventTime) {
Yao Chen5110bed2017-10-23 12:50:02 -070086}
87
yro17adac92017-11-08 23:16:29 -080088std::unique_ptr<std::vector<uint8_t>> EventMetricProducer::onDumpReport() {
Yao Chen93fe3a32017-11-02 13:52:59 -070089 long long endTime = time(nullptr) * NS_PER_SEC;
Yao Chen5110bed2017-10-23 12:50:02 -070090 mProto->end(mProtoToken);
yro24809bd2017-10-31 23:06:53 -070091 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_END_REPORT_NANOS, endTime);
Yao Chen5110bed2017-10-23 12:50:02 -070092
93 size_t bufferSize = mProto->size();
94 VLOG("metric %lld dump report now... proto size: %zu ", mMetric.metric_id(), bufferSize);
yro17adac92017-11-08 23:16:29 -080095 std::unique_ptr<std::vector<uint8_t>> buffer = serializeProto();
Yao Chen5110bed2017-10-23 12:50:02 -070096
97 startNewProtoOutputStream(endTime);
yrob0378b02017-11-09 20:36:25 -080098 mByteSize = 0;
Yao Chen5110bed2017-10-23 12:50:02 -070099
yro17adac92017-11-08 23:16:29 -0800100 return buffer;
Yao Chen5110bed2017-10-23 12:50:02 -0700101}
102
Yao Chen5154a372017-10-30 22:57:06 -0700103void EventMetricProducer::onConditionChanged(const bool conditionMet, const uint64_t eventTime) {
Yao Chen5110bed2017-10-23 12:50:02 -0700104 VLOG("Metric %lld onConditionChanged", mMetric.metric_id());
105 mCondition = conditionMet;
106}
107
108void EventMetricProducer::onMatchedLogEventInternal(
109 const size_t matcherIndex, const HashableDimensionKey& eventKey,
110 const std::map<std::string, HashableDimensionKey>& conditionKey, bool condition,
Chenjie Yub3dda412017-10-24 13:41:59 -0700111 const LogEvent& event, bool scheduledPull) {
Yao Chen5110bed2017-10-23 12:50:02 -0700112 if (!condition) {
113 return;
114 }
115
yrob0378b02017-11-09 20:36:25 -0800116 long long wrapperToken =
117 mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
yro24809bd2017-10-31 23:06:53 -0700118 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_TIMESTAMP_NANOS, (long long)event.GetTimestampNs());
119 long long eventToken = mProto->start(FIELD_TYPE_MESSAGE | FIELD_ID_STATS_EVENTS);
Yao Chen5110bed2017-10-23 12:50:02 -0700120 event.ToProto(*mProto);
121 mProto->end(eventToken);
122 mProto->end(wrapperToken);
yrob0378b02017-11-09 20:36:25 -0800123 // TODO: Find a proper way to derive the size of incoming LogEvent.
Yao Chen5110bed2017-10-23 12:50:02 -0700124}
125
yro69007c82017-10-26 20:42:57 -0700126size_t EventMetricProducer::byteSize() {
yrob0378b02017-11-09 20:36:25 -0800127 return mByteSize;
yro69007c82017-10-26 20:42:57 -0700128}
129
Yao Chen5110bed2017-10-23 12:50:02 -0700130} // namespace statsd
131} // namespace os
132} // namespace android