blob: 95a18f7241f0e6f24fb62781a19eb07e42ddfe4c [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;
Yangster-macd1815dc2017-11-13 21:43:15 -080031using android::util::FIELD_TYPE_STRING;
yro2b0f8862017-11-06 14:27:31 -080032using android::util::FIELD_TYPE_MESSAGE;
Yao Chen5110bed2017-10-23 12:50:02 -070033using android::util::ProtoOutputStream;
34using std::map;
35using std::string;
36using std::unordered_map;
37using std::vector;
38
39namespace android {
40namespace os {
41namespace statsd {
42
43// for StatsLogReport
Yangster-macd1815dc2017-11-13 21:43:15 -080044const int FIELD_ID_NAME = 1;
Yao Chen5110bed2017-10-23 12:50:02 -070045const int FIELD_ID_START_REPORT_NANOS = 2;
yro24809bd2017-10-31 23:06:53 -070046const int FIELD_ID_END_REPORT_NANOS = 3;
Yao Chen5110bed2017-10-23 12:50:02 -070047const int FIELD_ID_EVENT_METRICS = 4;
yro24809bd2017-10-31 23:06:53 -070048// for EventMetricDataWrapper
49const int FIELD_ID_DATA = 1;
Yao Chen5110bed2017-10-23 12:50:02 -070050// for EventMetricData
51const int FIELD_ID_TIMESTAMP_NANOS = 1;
Stefan Lafonae2df012017-11-14 09:17:21 -080052const int FIELD_ID_ATOMS = 2;
Yao Chen5110bed2017-10-23 12:50:02 -070053
Yao Chenb3561512017-11-21 18:07:17 -080054EventMetricProducer::EventMetricProducer(const ConfigKey& key, const EventMetric& metric,
55 const int conditionIndex,
Yao Chen93fe3a32017-11-02 13:52:59 -070056 const sp<ConditionWizard>& wizard,
57 const uint64_t startTimeNs)
Yao Chenb3561512017-11-21 18:07:17 -080058 : MetricProducer(key, startTimeNs, conditionIndex, wizard), mMetric(metric) {
Yao Chen5110bed2017-10-23 12:50:02 -070059 if (metric.links().size() > 0) {
60 mConditionLinks.insert(mConditionLinks.begin(), metric.links().begin(),
61 metric.links().end());
62 mConditionSliced = true;
63 }
64
65 startNewProtoOutputStream(mStartTimeNs);
66
Yangster-macd1815dc2017-11-13 21:43:15 -080067 VLOG("metric %s created. bucket size %lld start_time: %lld", metric.name().c_str(),
Yao Chen5110bed2017-10-23 12:50:02 -070068 (long long)mBucketSizeNs, (long long)mStartTimeNs);
69}
70
71EventMetricProducer::~EventMetricProducer() {
72 VLOG("~EventMetricProducer() called");
73}
74
75void EventMetricProducer::startNewProtoOutputStream(long long startTime) {
76 mProto = std::make_unique<ProtoOutputStream>();
77 // TODO: We need to auto-generate the field IDs for StatsLogReport, EventMetricData,
78 // and StatsEvent.
Yangster-macd1815dc2017-11-13 21:43:15 -080079 mProto->write(FIELD_TYPE_STRING | FIELD_ID_NAME, mMetric.name());
yro24809bd2017-10-31 23:06:53 -070080 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, startTime);
81 mProtoToken = mProto->start(FIELD_TYPE_MESSAGE | FIELD_ID_EVENT_METRICS);
Yao Chen5110bed2017-10-23 12:50:02 -070082}
83
84void EventMetricProducer::finish() {
85}
86
Yao Chen5154a372017-10-30 22:57:06 -070087void EventMetricProducer::onSlicedConditionMayChange(const uint64_t eventTime) {
Yao Chen5110bed2017-10-23 12:50:02 -070088}
89
yro17adac92017-11-08 23:16:29 -080090std::unique_ptr<std::vector<uint8_t>> EventMetricProducer::onDumpReport() {
Yao Chen93fe3a32017-11-02 13:52:59 -070091 long long endTime = time(nullptr) * NS_PER_SEC;
Yao Chen5110bed2017-10-23 12:50:02 -070092 mProto->end(mProtoToken);
yro24809bd2017-10-31 23:06:53 -070093 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_END_REPORT_NANOS, endTime);
Yao Chen5110bed2017-10-23 12:50:02 -070094
95 size_t bufferSize = mProto->size();
Yangster-macd1815dc2017-11-13 21:43:15 -080096 VLOG("metric %s dump report now... proto size: %zu ", mMetric.name().c_str(), bufferSize);
yro17adac92017-11-08 23:16:29 -080097 std::unique_ptr<std::vector<uint8_t>> buffer = serializeProto();
Yao Chen5110bed2017-10-23 12:50:02 -070098
99 startNewProtoOutputStream(endTime);
100
yro17adac92017-11-08 23:16:29 -0800101 return buffer;
Yao Chen5110bed2017-10-23 12:50:02 -0700102}
103
Yao Chen5154a372017-10-30 22:57:06 -0700104void EventMetricProducer::onConditionChanged(const bool conditionMet, const uint64_t eventTime) {
Yangster-macd1815dc2017-11-13 21:43:15 -0800105 VLOG("Metric %s onConditionChanged", mMetric.name().c_str());
Yao Chen5110bed2017-10-23 12:50:02 -0700106 mCondition = conditionMet;
107}
108
109void EventMetricProducer::onMatchedLogEventInternal(
110 const size_t matcherIndex, const HashableDimensionKey& eventKey,
111 const std::map<std::string, HashableDimensionKey>& conditionKey, bool condition,
Chenjie Yub3dda412017-10-24 13:41:59 -0700112 const LogEvent& event, bool scheduledPull) {
Yao Chen5110bed2017-10-23 12:50:02 -0700113 if (!condition) {
114 return;
115 }
116
yrob0378b02017-11-09 20:36:25 -0800117 long long wrapperToken =
118 mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
yro24809bd2017-10-31 23:06:53 -0700119 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_TIMESTAMP_NANOS, (long long)event.GetTimestampNs());
Stefan Lafonae2df012017-11-14 09:17:21 -0800120 long long eventToken = mProto->start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOMS);
Yao Chen5110bed2017-10-23 12:50:02 -0700121 event.ToProto(*mProto);
122 mProto->end(eventToken);
123 mProto->end(wrapperToken);
124}
125
Yangster7c334a12017-11-22 14:24:24 -0800126size_t EventMetricProducer::byteSize() const {
yro75f0a4d2017-11-17 17:20:45 -0800127 return mProto->bytesWritten();
yro69007c82017-10-26 20:42:57 -0700128}
129
Yao Chen5110bed2017-10-23 12:50:02 -0700130} // namespace statsd
131} // namespace os
132} // namespace android