blob: 0578e0682687fb8417a1b33e35f9929570361378 [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
Yao Chen3c0b95c2017-12-16 14:34:20 -080017#define DEBUG false // STOPSHIP if true
Yao Chen5110bed2017-10-23 12:50:02 -070018#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-mac94e197c2018-01-02 16:03:03 -080044const int FIELD_ID_ID = 1;
Yao Chen5110bed2017-10-23 12:50:02 -070045const int FIELD_ID_EVENT_METRICS = 4;
yro24809bd2017-10-31 23:06:53 -070046// for EventMetricDataWrapper
47const int FIELD_ID_DATA = 1;
Yao Chen5110bed2017-10-23 12:50:02 -070048// for EventMetricData
49const int FIELD_ID_TIMESTAMP_NANOS = 1;
Stefan Lafonae2df012017-11-14 09:17:21 -080050const int FIELD_ID_ATOMS = 2;
Yao Chen5110bed2017-10-23 12:50:02 -070051
Yao Chenb3561512017-11-21 18:07:17 -080052EventMetricProducer::EventMetricProducer(const ConfigKey& key, const EventMetric& metric,
53 const int conditionIndex,
Yao Chen93fe3a32017-11-02 13:52:59 -070054 const sp<ConditionWizard>& wizard,
55 const uint64_t startTimeNs)
Yangster-mac94e197c2018-01-02 16:03:03 -080056 : MetricProducer(metric.id(), key, startTimeNs, conditionIndex, wizard) {
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
Yao Chen288c6002017-12-12 13:43:18 -080063 startNewProtoOutputStreamLocked();
Yao Chen5110bed2017-10-23 12:50:02 -070064
Yangster-mac94e197c2018-01-02 16:03:03 -080065 VLOG("metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(),
Yao Chen5110bed2017-10-23 12:50:02 -070066 (long long)mBucketSizeNs, (long long)mStartTimeNs);
67}
68
69EventMetricProducer::~EventMetricProducer() {
70 VLOG("~EventMetricProducer() called");
71}
72
Yao Chen288c6002017-12-12 13:43:18 -080073void EventMetricProducer::startNewProtoOutputStreamLocked() {
Yao Chen5110bed2017-10-23 12:50:02 -070074 mProto = std::make_unique<ProtoOutputStream>();
Yao Chen5110bed2017-10-23 12:50:02 -070075}
76
Yangsterf2bee6f2017-11-29 12:01:05 -080077void EventMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) {
Yao Chen5110bed2017-10-23 12:50:02 -070078}
79
Yao Chen288c6002017-12-12 13:43:18 -080080std::unique_ptr<std::vector<uint8_t>> serializeProtoLocked(ProtoOutputStream& protoOutput) {
81 size_t bufferSize = protoOutput.size();
82
83 std::unique_ptr<std::vector<uint8_t>> buffer(new std::vector<uint8_t>(bufferSize));
84
85 size_t pos = 0;
86 auto it = protoOutput.data();
87 while (it.readBuffer() != NULL) {
88 size_t toRead = it.currentToRead();
89 std::memcpy(&((*buffer)[pos]), it.readBuffer(), toRead);
90 pos += toRead;
91 it.rp()->move(toRead);
92 }
93
94 return buffer;
95}
96
Yangster-mac20877162017-12-22 17:19:39 -080097void EventMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs, StatsLogReport* report) {
98
99}
100
Yao Chen288c6002017-12-12 13:43:18 -0800101void EventMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs,
102 ProtoOutputStream* protoOutput) {
Yangster-mac635b4b32018-01-23 20:17:35 -0800103 if (mProto->size() <= 0) {
104 return;
105 }
Yangster-mac94e197c2018-01-02 16:03:03 -0800106 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
Yao Chen5110bed2017-10-23 12:50:02 -0700107
Yao Chen6a8c7992017-11-29 20:02:07 +0000108 size_t bufferSize = mProto->size();
Yangster-mac94e197c2018-01-02 16:03:03 -0800109 VLOG("metric %lld dump report now... proto size: %zu ",
110 (long long)mMetricId, bufferSize);
Yao Chen288c6002017-12-12 13:43:18 -0800111 std::unique_ptr<std::vector<uint8_t>> buffer = serializeProtoLocked(*mProto);
Yao Chen5110bed2017-10-23 12:50:02 -0700112
Yao Chen288c6002017-12-12 13:43:18 -0800113 protoOutput->write(FIELD_TYPE_MESSAGE | FIELD_ID_EVENT_METRICS,
114 reinterpret_cast<char*>(buffer.get()->data()), buffer.get()->size());
Yao Chen5110bed2017-10-23 12:50:02 -0700115
Yao Chen288c6002017-12-12 13:43:18 -0800116 startNewProtoOutputStreamLocked();
Yao Chen5110bed2017-10-23 12:50:02 -0700117}
118
Yangsterf2bee6f2017-11-29 12:01:05 -0800119void EventMetricProducer::onConditionChangedLocked(const bool conditionMet,
120 const uint64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800121 VLOG("Metric %lld onConditionChanged", (long long)mMetricId);
Yao Chen5110bed2017-10-23 12:50:02 -0700122 mCondition = conditionMet;
123}
124
Yangsterf2bee6f2017-11-29 12:01:05 -0800125void EventMetricProducer::onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -0800126 const size_t matcherIndex, const MetricDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -0800127 const ConditionKey& conditionKey, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800128 const LogEvent& event) {
Yao Chen5110bed2017-10-23 12:50:02 -0700129 if (!condition) {
130 return;
131 }
132
yrob0378b02017-11-09 20:36:25 -0800133 long long wrapperToken =
134 mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
yro24809bd2017-10-31 23:06:53 -0700135 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_TIMESTAMP_NANOS, (long long)event.GetTimestampNs());
Stefan Lafonae2df012017-11-14 09:17:21 -0800136 long long eventToken = mProto->start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOMS);
Yao Chen5110bed2017-10-23 12:50:02 -0700137 event.ToProto(*mProto);
138 mProto->end(eventToken);
139 mProto->end(wrapperToken);
140}
141
Yangsterf2bee6f2017-11-29 12:01:05 -0800142size_t EventMetricProducer::byteSizeLocked() const {
yro75f0a4d2017-11-17 17:20:45 -0800143 return mProto->bytesWritten();
yro69007c82017-10-26 20:42:57 -0700144}
145
Yao Chen5110bed2017-10-23 12:50:02 -0700146} // namespace statsd
147} // namespace os
148} // namespace android