blob: a18e406b74ca2b2c7d24c74b22271ec1061c29dc [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;
Yao Chen5110bed2017-10-23 12:50:02 -070052
Yao Chenb3561512017-11-21 18:07:17 -080053EventMetricProducer::EventMetricProducer(const ConfigKey& key, const EventMetric& metric,
54 const int conditionIndex,
Yao Chen93fe3a32017-11-02 13:52:59 -070055 const sp<ConditionWizard>& wizard,
Yangster-macb142cc82018-03-30 15:22:08 -070056 const int64_t startTimeNs)
Yangster-mac94e197c2018-01-02 16:03:03 -080057 : MetricProducer(metric.id(), key, startTimeNs, conditionIndex, wizard) {
Yao Chen5110bed2017-10-23 12:50:02 -070058 if (metric.links().size() > 0) {
Yao Chen8a8d16c2018-02-08 14:50:40 -080059 for (const auto& link : metric.links()) {
60 Metric2Condition mc;
61 mc.conditionId = link.condition();
62 translateFieldMatcher(link.fields_in_what(), &mc.metricFields);
63 translateFieldMatcher(link.fields_in_condition(), &mc.conditionFields);
64 mMetric2ConditionLinks.push_back(mc);
65 }
Yao Chen5110bed2017-10-23 12:50:02 -070066 mConditionSliced = true;
67 }
Yi Jin7f9e63b2018-02-02 16:25:11 -080068 mProto = std::make_unique<ProtoOutputStream>();
Yangster-mac94e197c2018-01-02 16:03:03 -080069 VLOG("metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(),
Yangster-mac15f6bbc2018-04-08 11:52:26 -070070 (long long)mBucketSizeNs, (long long)mTimeBaseNs);
Yao Chen5110bed2017-10-23 12:50:02 -070071}
72
73EventMetricProducer::~EventMetricProducer() {
74 VLOG("~EventMetricProducer() called");
75}
76
Yangster-macb142cc82018-03-30 15:22:08 -070077void EventMetricProducer::dropDataLocked(const int64_t dropTimeNs) {
Yao Chen06dba5d2018-01-26 13:38:16 -080078 mProto->clear();
79}
80
Yao Chen427d3722018-03-22 15:21:52 -070081void EventMetricProducer::onSlicedConditionMayChangeLocked(bool overallCondition,
Yangster-macb142cc82018-03-30 15:22:08 -070082 const int64_t eventTime) {
Yao Chen5110bed2017-10-23 12:50:02 -070083}
84
Yao Chen288c6002017-12-12 13:43:18 -080085std::unique_ptr<std::vector<uint8_t>> serializeProtoLocked(ProtoOutputStream& protoOutput) {
86 size_t bufferSize = protoOutput.size();
87
88 std::unique_ptr<std::vector<uint8_t>> buffer(new std::vector<uint8_t>(bufferSize));
89
90 size_t pos = 0;
91 auto it = protoOutput.data();
92 while (it.readBuffer() != NULL) {
93 size_t toRead = it.currentToRead();
94 std::memcpy(&((*buffer)[pos]), it.readBuffer(), toRead);
95 pos += toRead;
96 it.rp()->move(toRead);
97 }
98
99 return buffer;
100}
101
Yangster-maca802d732018-04-24 07:50:38 -0700102void EventMetricProducer::clearPastBucketsLocked(const int64_t dumpTimeNs) {
103 mProto->clear();
104}
105
Yangster-macb142cc82018-03-30 15:22:08 -0700106void EventMetricProducer::onDumpReportLocked(const int64_t dumpTimeNs,
Yangster-mace68f3a52018-04-04 00:01:43 -0700107 const bool include_current_partial_bucket,
Bookatzff71cad2018-09-20 17:17:49 -0700108 const bool erase_data,
Yangster-mac9def8e32018-04-17 13:55:51 -0700109 std::set<string> *str_set,
Yao Chen288c6002017-12-12 13:43:18 -0800110 ProtoOutputStream* protoOutput) {
Yangster-mac635b4b32018-01-23 20:17:35 -0800111 if (mProto->size() <= 0) {
112 return;
113 }
Yangster-mac94e197c2018-01-02 16:03:03 -0800114 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
Yao Chen5110bed2017-10-23 12:50:02 -0700115
Yao Chen6a8c7992017-11-29 20:02:07 +0000116 size_t bufferSize = mProto->size();
Yangster-mac94e197c2018-01-02 16:03:03 -0800117 VLOG("metric %lld dump report now... proto size: %zu ",
118 (long long)mMetricId, bufferSize);
Yao Chen288c6002017-12-12 13:43:18 -0800119 std::unique_ptr<std::vector<uint8_t>> buffer = serializeProtoLocked(*mProto);
Yao Chen5110bed2017-10-23 12:50:02 -0700120
Yao Chen288c6002017-12-12 13:43:18 -0800121 protoOutput->write(FIELD_TYPE_MESSAGE | FIELD_ID_EVENT_METRICS,
122 reinterpret_cast<char*>(buffer.get()->data()), buffer.get()->size());
Yao Chen5110bed2017-10-23 12:50:02 -0700123
Bookatzff71cad2018-09-20 17:17:49 -0700124 if (erase_data) {
125 mProto->clear();
126 }
Yao Chen5110bed2017-10-23 12:50:02 -0700127}
128
Yangsterf2bee6f2017-11-29 12:01:05 -0800129void EventMetricProducer::onConditionChangedLocked(const bool conditionMet,
Yangster-macb142cc82018-03-30 15:22:08 -0700130 const int64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800131 VLOG("Metric %lld onConditionChanged", (long long)mMetricId);
Yao Chen5110bed2017-10-23 12:50:02 -0700132 mCondition = conditionMet;
133}
134
Yangsterf2bee6f2017-11-29 12:01:05 -0800135void EventMetricProducer::onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -0800136 const size_t matcherIndex, const MetricDimensionKey& eventKey,
Yangster-mac20877162017-12-22 17:19:39 -0800137 const ConditionKey& conditionKey, bool condition,
Chenjie Yua7259ab2017-12-10 08:31:05 -0800138 const LogEvent& event) {
Yao Chen5110bed2017-10-23 12:50:02 -0700139 if (!condition) {
140 return;
141 }
142
Yi Jin5ee07872018-03-05 18:18:27 -0800143 uint64_t wrapperToken =
yrob0378b02017-11-09 20:36:25 -0800144 mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
Yangster-macd5c35622018-02-02 10:33:25 -0800145 const bool truncateTimestamp =
Yao Chenc40a19d2018-03-15 16:48:25 -0700146 android::util::AtomsInfo::kNotTruncatingTimestampAtomWhiteList.find(event.GetTagId()) ==
147 android::util::AtomsInfo::kNotTruncatingTimestampAtomWhiteList.end();
Yangster-macd5c35622018-02-02 10:33:25 -0800148 if (truncateTimestamp) {
149 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_ELAPSED_TIMESTAMP_NANOS,
150 (long long)truncateTimestampNsToFiveMinutes(event.GetElapsedTimestampNs()));
Yangster-macd5c35622018-02-02 10:33:25 -0800151 } else {
152 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_ELAPSED_TIMESTAMP_NANOS,
153 (long long)event.GetElapsedTimestampNs());
Yangster-macd5c35622018-02-02 10:33:25 -0800154 }
Yangstere1faa032018-02-21 14:08:17 -0800155
Yi Jin5ee07872018-03-05 18:18:27 -0800156 uint64_t eventToken = mProto->start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOMS);
Yao Chen5110bed2017-10-23 12:50:02 -0700157 event.ToProto(*mProto);
158 mProto->end(eventToken);
159 mProto->end(wrapperToken);
160}
161
Yangsterf2bee6f2017-11-29 12:01:05 -0800162size_t EventMetricProducer::byteSizeLocked() const {
yro75f0a4d2017-11-17 17:20:45 -0800163 return mProto->bytesWritten();
yro69007c82017-10-26 20:42:57 -0700164}
165
Yao Chen5110bed2017-10-23 12:50:02 -0700166} // namespace statsd
167} // namespace os
168} // namespace android