blob: 96d0cfcc5897861b62d6a1edf9fe90d6556e14da [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"
Yangster-mac330af582018-02-08 15:24:38 -080022#include "stats_log_util.h"
Yao Chen5110bed2017-10-23 12:50:02 -070023
Yao Chen5110bed2017-10-23 12:50:02 -070024#include <limits.h>
25#include <stdlib.h>
26
yrob0378b02017-11-09 20:36:25 -080027using android::util::FIELD_COUNT_REPEATED;
yro2b0f8862017-11-06 14:27:31 -080028using android::util::FIELD_TYPE_BOOL;
29using android::util::FIELD_TYPE_FLOAT;
30using android::util::FIELD_TYPE_INT32;
31using android::util::FIELD_TYPE_INT64;
Yangster-macd1815dc2017-11-13 21:43:15 -080032using android::util::FIELD_TYPE_STRING;
yro2b0f8862017-11-06 14:27:31 -080033using android::util::FIELD_TYPE_MESSAGE;
Yao Chen5110bed2017-10-23 12:50:02 -070034using android::util::ProtoOutputStream;
35using std::map;
36using std::string;
37using std::unordered_map;
38using std::vector;
39
40namespace android {
41namespace os {
42namespace statsd {
43
44// for StatsLogReport
Yangster-mac94e197c2018-01-02 16:03:03 -080045const int FIELD_ID_ID = 1;
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
Yangster-mac330af582018-02-08 15:24:38 -080050const int FIELD_ID_ELAPSED_TIMESTAMP_NANOS = 1;
Stefan Lafonae2df012017-11-14 09:17:21 -080051const int FIELD_ID_ATOMS = 2;
Yangster-mac330af582018-02-08 15:24:38 -080052const int FIELD_ID_WALL_CLOCK_TIMESTAMP_NANOS = 3;
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)
Yangster-mac94e197c2018-01-02 16:03:03 -080058 : MetricProducer(metric.id(), key, startTimeNs, conditionIndex, wizard) {
Yao Chen5110bed2017-10-23 12:50:02 -070059 if (metric.links().size() > 0) {
Yao Chen8a8d16c2018-02-08 14:50:40 -080060 for (const auto& link : metric.links()) {
61 Metric2Condition mc;
62 mc.conditionId = link.condition();
63 translateFieldMatcher(link.fields_in_what(), &mc.metricFields);
64 translateFieldMatcher(link.fields_in_condition(), &mc.conditionFields);
65 mMetric2ConditionLinks.push_back(mc);
66 }
Yao Chen5110bed2017-10-23 12:50:02 -070067 mConditionSliced = true;
68 }
Yi Jin7f9e63b2018-02-02 16:25:11 -080069 mProto = std::make_unique<ProtoOutputStream>();
Yangster-mac94e197c2018-01-02 16:03:03 -080070 VLOG("metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(),
Yao Chen5110bed2017-10-23 12:50:02 -070071 (long long)mBucketSizeNs, (long long)mStartTimeNs);
72}
73
74EventMetricProducer::~EventMetricProducer() {
75 VLOG("~EventMetricProducer() called");
76}
77
Yangsterf2bee6f2017-11-29 12:01:05 -080078void EventMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) {
Yao Chen5110bed2017-10-23 12:50:02 -070079}
80
Yao Chen288c6002017-12-12 13:43:18 -080081std::unique_ptr<std::vector<uint8_t>> serializeProtoLocked(ProtoOutputStream& protoOutput) {
82 size_t bufferSize = protoOutput.size();
83
84 std::unique_ptr<std::vector<uint8_t>> buffer(new std::vector<uint8_t>(bufferSize));
85
86 size_t pos = 0;
87 auto it = protoOutput.data();
88 while (it.readBuffer() != NULL) {
89 size_t toRead = it.currentToRead();
90 std::memcpy(&((*buffer)[pos]), it.readBuffer(), toRead);
91 pos += toRead;
92 it.rp()->move(toRead);
93 }
94
95 return buffer;
96}
97
98void EventMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs,
99 ProtoOutputStream* protoOutput) {
Yangster-mac635b4b32018-01-23 20:17:35 -0800100 if (mProto->size() <= 0) {
101 return;
102 }
Yangster-mac94e197c2018-01-02 16:03:03 -0800103 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
Yao Chen5110bed2017-10-23 12:50:02 -0700104
Yao Chen6a8c7992017-11-29 20:02:07 +0000105 size_t bufferSize = mProto->size();
Yangster-mac94e197c2018-01-02 16:03:03 -0800106 VLOG("metric %lld dump report now... proto size: %zu ",
107 (long long)mMetricId, bufferSize);
Yao Chen288c6002017-12-12 13:43:18 -0800108 std::unique_ptr<std::vector<uint8_t>> buffer = serializeProtoLocked(*mProto);
Yao Chen5110bed2017-10-23 12:50:02 -0700109
Yao Chen288c6002017-12-12 13:43:18 -0800110 protoOutput->write(FIELD_TYPE_MESSAGE | FIELD_ID_EVENT_METRICS,
111 reinterpret_cast<char*>(buffer.get()->data()), buffer.get()->size());
Yao Chen5110bed2017-10-23 12:50:02 -0700112
Yi Jin7f9e63b2018-02-02 16:25:11 -0800113 mProto->clear();
Yao Chen5110bed2017-10-23 12:50:02 -0700114}
115
Yangsterf2bee6f2017-11-29 12:01:05 -0800116void EventMetricProducer::onConditionChangedLocked(const bool conditionMet,
117 const uint64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800118 VLOG("Metric %lld onConditionChanged", (long long)mMetricId);
Yao Chen5110bed2017-10-23 12:50:02 -0700119 mCondition = conditionMet;
120}
121
Yangsterf2bee6f2017-11-29 12:01:05 -0800122void EventMetricProducer::onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -0800123 const size_t matcherIndex, const MetricDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -0800124 const ConditionKey& conditionKey, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800125 const LogEvent& event) {
Yao Chen5110bed2017-10-23 12:50:02 -0700126 if (!condition) {
127 return;
128 }
129
yrob0378b02017-11-09 20:36:25 -0800130 long long wrapperToken =
131 mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
Yangster-macd5c35622018-02-02 10:33:25 -0800132 const bool truncateTimestamp =
133 android::util::kNotTruncatingTimestampAtomWhiteList.find(event.GetTagId()) ==
134 android::util::kNotTruncatingTimestampAtomWhiteList.end();
135 if (truncateTimestamp) {
136 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_ELAPSED_TIMESTAMP_NANOS,
137 (long long)truncateTimestampNsToFiveMinutes(event.GetElapsedTimestampNs()));
138 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_WALL_CLOCK_TIMESTAMP_NANOS,
139 (long long)truncateTimestampNsToFiveMinutes(getWallClockNs()));
140 } else {
141 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_ELAPSED_TIMESTAMP_NANOS,
142 (long long)event.GetElapsedTimestampNs());
143 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_WALL_CLOCK_TIMESTAMP_NANOS,
144 (long long)getWallClockNs());
145 }
Yangstere1faa032018-02-21 14:08:17 -0800146
147 long long eventToken = mProto->start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOMS);
Yao Chen5110bed2017-10-23 12:50:02 -0700148 event.ToProto(*mProto);
149 mProto->end(eventToken);
150 mProto->end(wrapperToken);
151}
152
Yangsterf2bee6f2017-11-29 12:01:05 -0800153size_t EventMetricProducer::byteSizeLocked() const {
yro75f0a4d2017-11-17 17:20:45 -0800154 return mProto->bytesWritten();
yro69007c82017-10-26 20:42:57 -0700155}
156
Yao Chen5110bed2017-10-23 12:50:02 -0700157} // namespace statsd
158} // namespace os
159} // namespace android