blob: dc0036a687f317e5ac4f689f9861b0c2d14a19ea [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;
Ruchir Rastogi21a287b2019-10-02 12:04:33 -070039using std::shared_ptr;
Yao Chen5110bed2017-10-23 12:50:02 -070040
41namespace android {
42namespace os {
43namespace statsd {
44
45// for StatsLogReport
Yangster-mac94e197c2018-01-02 16:03:03 -080046const int FIELD_ID_ID = 1;
Yao Chen5110bed2017-10-23 12:50:02 -070047const int FIELD_ID_EVENT_METRICS = 4;
Howard Ro9440e092018-12-16 19:15:21 -080048const int FIELD_ID_IS_ACTIVE = 14;
yro24809bd2017-10-31 23:06:53 -070049// for EventMetricDataWrapper
50const int FIELD_ID_DATA = 1;
Yao Chen5110bed2017-10-23 12:50:02 -070051// for EventMetricData
Yangster-mac330af582018-02-08 15:24:38 -080052const int FIELD_ID_ELAPSED_TIMESTAMP_NANOS = 1;
Stefan Lafonae2df012017-11-14 09:17:21 -080053const int FIELD_ID_ATOMS = 2;
Yao Chen5110bed2017-10-23 12:50:02 -070054
tsaichristined21aacf2019-10-07 14:47:38 -070055EventMetricProducer::EventMetricProducer(
56 const ConfigKey& key, const EventMetric& metric, const int conditionIndex,
tsaichristine6e2e92d2020-05-18 14:39:45 -070057 const vector<ConditionState>& initialConditionCache, const sp<ConditionWizard>& wizard,
58 const int64_t startTimeNs,
tsaichristined21aacf2019-10-07 14:47:38 -070059 const unordered_map<int, shared_ptr<Activation>>& eventActivationMap,
60 const unordered_map<int, vector<shared_ptr<Activation>>>& eventDeactivationMap,
61 const vector<int>& slicedStateAtoms,
62 const unordered_map<int, unordered_map<int, int64_t>>& stateGroupMap)
tsaichristine6e2e92d2020-05-18 14:39:45 -070063 : MetricProducer(metric.id(), key, startTimeNs, conditionIndex, initialConditionCache, wizard,
64 eventActivationMap, eventDeactivationMap, slicedStateAtoms, stateGroupMap) {
Yao Chen5110bed2017-10-23 12:50:02 -070065 if (metric.links().size() > 0) {
Yao Chen8a8d16c2018-02-08 14:50:40 -080066 for (const auto& link : metric.links()) {
67 Metric2Condition mc;
68 mc.conditionId = link.condition();
69 translateFieldMatcher(link.fields_in_what(), &mc.metricFields);
70 translateFieldMatcher(link.fields_in_condition(), &mc.conditionFields);
71 mMetric2ConditionLinks.push_back(mc);
72 }
Yao Chen5110bed2017-10-23 12:50:02 -070073 mConditionSliced = true;
74 }
Yi Jin7f9e63b2018-02-02 16:25:11 -080075 mProto = std::make_unique<ProtoOutputStream>();
Yangster-mac94e197c2018-01-02 16:03:03 -080076 VLOG("metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(),
Yangster-mac15f6bbc2018-04-08 11:52:26 -070077 (long long)mBucketSizeNs, (long long)mTimeBaseNs);
Yao Chen5110bed2017-10-23 12:50:02 -070078}
79
80EventMetricProducer::~EventMetricProducer() {
81 VLOG("~EventMetricProducer() called");
82}
83
Yangster-macb142cc82018-03-30 15:22:08 -070084void EventMetricProducer::dropDataLocked(const int64_t dropTimeNs) {
Yao Chen06dba5d2018-01-26 13:38:16 -080085 mProto->clear();
Olivier Gaillard320952b2019-02-06 13:57:24 +000086 StatsdStats::getInstance().noteBucketDropped(mMetricId);
Yao Chen06dba5d2018-01-26 13:38:16 -080087}
88
Yao Chen427d3722018-03-22 15:21:52 -070089void EventMetricProducer::onSlicedConditionMayChangeLocked(bool overallCondition,
Yangster-macb142cc82018-03-30 15:22:08 -070090 const int64_t eventTime) {
Yao Chen5110bed2017-10-23 12:50:02 -070091}
92
Yao Chen288c6002017-12-12 13:43:18 -080093std::unique_ptr<std::vector<uint8_t>> serializeProtoLocked(ProtoOutputStream& protoOutput) {
94 size_t bufferSize = protoOutput.size();
95
96 std::unique_ptr<std::vector<uint8_t>> buffer(new std::vector<uint8_t>(bufferSize));
97
98 size_t pos = 0;
Joe Onorato99598ee2019-02-11 15:55:13 +000099 sp<android::util::ProtoReader> reader = protoOutput.data();
100 while (reader->readBuffer() != NULL) {
101 size_t toRead = reader->currentToRead();
102 std::memcpy(&((*buffer)[pos]), reader->readBuffer(), toRead);
Yao Chen288c6002017-12-12 13:43:18 -0800103 pos += toRead;
Joe Onorato99598ee2019-02-11 15:55:13 +0000104 reader->move(toRead);
Yao Chen288c6002017-12-12 13:43:18 -0800105 }
106
107 return buffer;
108}
109
Yangster-maca802d732018-04-24 07:50:38 -0700110void EventMetricProducer::clearPastBucketsLocked(const int64_t dumpTimeNs) {
111 mProto->clear();
112}
113
Yangster-macb142cc82018-03-30 15:22:08 -0700114void EventMetricProducer::onDumpReportLocked(const int64_t dumpTimeNs,
Yangster-mace68f3a52018-04-04 00:01:43 -0700115 const bool include_current_partial_bucket,
Bookatzff71cad2018-09-20 17:17:49 -0700116 const bool erase_data,
Olivier Gaillard6c75ecd2019-02-20 09:57:33 +0000117 const DumpLatency dumpLatency,
Yangster-mac9def8e32018-04-17 13:55:51 -0700118 std::set<string> *str_set,
Yao Chen288c6002017-12-12 13:43:18 -0800119 ProtoOutputStream* protoOutput) {
Yang Lub4722912018-11-15 11:02:03 -0800120 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
Howard Ro07e23ff2018-12-17 17:28:07 -0800121 protoOutput->write(FIELD_TYPE_BOOL | FIELD_ID_IS_ACTIVE, isActiveLocked());
Yangster-mac635b4b32018-01-23 20:17:35 -0800122 if (mProto->size() <= 0) {
123 return;
124 }
Yao Chen5110bed2017-10-23 12:50:02 -0700125
Yao Chen6a8c7992017-11-29 20:02:07 +0000126 size_t bufferSize = mProto->size();
Yangster-mac94e197c2018-01-02 16:03:03 -0800127 VLOG("metric %lld dump report now... proto size: %zu ",
128 (long long)mMetricId, bufferSize);
Yao Chen288c6002017-12-12 13:43:18 -0800129 std::unique_ptr<std::vector<uint8_t>> buffer = serializeProtoLocked(*mProto);
Yao Chen5110bed2017-10-23 12:50:02 -0700130
Yao Chen288c6002017-12-12 13:43:18 -0800131 protoOutput->write(FIELD_TYPE_MESSAGE | FIELD_ID_EVENT_METRICS,
132 reinterpret_cast<char*>(buffer.get()->data()), buffer.get()->size());
Yao Chen5110bed2017-10-23 12:50:02 -0700133
Bookatzff71cad2018-09-20 17:17:49 -0700134 if (erase_data) {
135 mProto->clear();
136 }
Yao Chen5110bed2017-10-23 12:50:02 -0700137}
138
Yangsterf2bee6f2017-11-29 12:01:05 -0800139void EventMetricProducer::onConditionChangedLocked(const bool conditionMet,
Yangster-macb142cc82018-03-30 15:22:08 -0700140 const int64_t eventTime) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800141 VLOG("Metric %lld onConditionChanged", (long long)mMetricId);
Olivier Gaillarde63d9e02019-02-12 14:43:59 +0000142 mCondition = conditionMet ? ConditionState::kTrue : ConditionState::kFalse;
Yao Chen5110bed2017-10-23 12:50:02 -0700143}
144
Yangsterf2bee6f2017-11-29 12:01:05 -0800145void EventMetricProducer::onMatchedLogEventInternalLocked(
Yangster-mac93694462018-01-22 20:49:31 -0800146 const size_t matcherIndex, const MetricDimensionKey& eventKey,
tsaichristinec876b492019-12-10 13:47:05 -0800147 const ConditionKey& conditionKey, bool condition, const LogEvent& event,
148 const map<int, HashableDimensionKey>& statePrimaryKeys) {
Yao Chen5110bed2017-10-23 12:50:02 -0700149 if (!condition) {
150 return;
151 }
152
Yi Jin5ee07872018-03-05 18:18:27 -0800153 uint64_t wrapperToken =
yrob0378b02017-11-09 20:36:25 -0800154 mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
Muhammad Qureshia7de0002020-04-15 11:34:35 -0700155 const int64_t elapsedTimeNs = truncateTimestampIfNecessary(event);
Tej Singh29ac6012019-06-05 18:10:12 -0700156 mProto->write(FIELD_TYPE_INT64 | FIELD_ID_ELAPSED_TIMESTAMP_NANOS, (long long) elapsedTimeNs);
Yangstere1faa032018-02-21 14:08:17 -0800157
Yi Jin5ee07872018-03-05 18:18:27 -0800158 uint64_t eventToken = mProto->start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOMS);
Yao Chen5110bed2017-10-23 12:50:02 -0700159 event.ToProto(*mProto);
160 mProto->end(eventToken);
161 mProto->end(wrapperToken);
162}
163
Yangsterf2bee6f2017-11-29 12:01:05 -0800164size_t EventMetricProducer::byteSizeLocked() const {
yro75f0a4d2017-11-17 17:20:45 -0800165 return mProto->bytesWritten();
yro69007c82017-10-26 20:42:57 -0700166}
167
Yao Chen5110bed2017-10-23 12:50:02 -0700168} // namespace statsd
169} // namespace os
170} // namespace android