1 /*
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 STATSD_DEBUG false // STOPSHIP if true
18 #include "Log.h"
19
20 #include "EventMetricProducer.h"
21
22 #include <limits.h>
23 #include <stdlib.h>
24
25 #include "metrics/parsing_utils/metrics_manager_util.h"
26 #include "stats_log_util.h"
27 #include "stats_util.h"
28
29 using android::util::FIELD_COUNT_REPEATED;
30 using android::util::FIELD_TYPE_BOOL;
31 using android::util::FIELD_TYPE_FLOAT;
32 using android::util::FIELD_TYPE_INT32;
33 using android::util::FIELD_TYPE_INT64;
34 using android::util::FIELD_TYPE_STRING;
35 using android::util::FIELD_TYPE_MESSAGE;
36 using android::util::ProtoOutputStream;
37 using std::map;
38 using std::string;
39 using std::unordered_map;
40 using std::vector;
41 using std::shared_ptr;
42
43 namespace android {
44 namespace os {
45 namespace statsd {
46
47 // for StatsLogReport
48 const int FIELD_ID_ID = 1;
49 const int FIELD_ID_EVENT_METRICS = 4;
50 const int FIELD_ID_IS_ACTIVE = 14;
51 // for EventMetricDataWrapper
52 const int FIELD_ID_DATA = 1;
53 // for EventMetricData
54 const int FIELD_ID_AGGREGATED_ATOM = 4;
55 // for AggregatedAtomInfo
56 const int FIELD_ID_ATOM = 1;
57 const int FIELD_ID_ATOM_TIMESTAMPS = 2;
58
EventMetricProducer(const ConfigKey & key,const EventMetric & metric,const int conditionIndex,const vector<ConditionState> & initialConditionCache,const sp<ConditionWizard> & wizard,const uint64_t protoHash,const int64_t startTimeNs,const unordered_map<int,shared_ptr<Activation>> & eventActivationMap,const unordered_map<int,vector<shared_ptr<Activation>>> & eventDeactivationMap,const vector<int> & slicedStateAtoms,const unordered_map<int,unordered_map<int,int64_t>> & stateGroupMap)59 EventMetricProducer::EventMetricProducer(
60 const ConfigKey& key, const EventMetric& metric, const int conditionIndex,
61 const vector<ConditionState>& initialConditionCache, const sp<ConditionWizard>& wizard,
62 const uint64_t protoHash, const int64_t startTimeNs,
63 const unordered_map<int, shared_ptr<Activation>>& eventActivationMap,
64 const unordered_map<int, vector<shared_ptr<Activation>>>& eventDeactivationMap,
65 const vector<int>& slicedStateAtoms,
66 const unordered_map<int, unordered_map<int, int64_t>>& stateGroupMap)
67 : MetricProducer(metric.id(), key, startTimeNs, conditionIndex, initialConditionCache, wizard,
68 protoHash, eventActivationMap, eventDeactivationMap, slicedStateAtoms,
69 stateGroupMap, /*splitBucketForAppUpgrade=*/nullopt) {
70 if (metric.links().size() > 0) {
71 for (const auto& link : metric.links()) {
72 Metric2Condition mc;
73 mc.conditionId = link.condition();
74 translateFieldMatcher(link.fields_in_what(), &mc.metricFields);
75 translateFieldMatcher(link.fields_in_condition(), &mc.conditionFields);
76 mMetric2ConditionLinks.push_back(mc);
77 }
78 mConditionSliced = true;
79 }
80 mTotalSize = 0;
81 VLOG("metric %lld created. bucket size %lld start_time: %lld", (long long)mMetricId,
82 (long long)mBucketSizeNs, (long long)mTimeBaseNs);
83 }
84
~EventMetricProducer()85 EventMetricProducer::~EventMetricProducer() {
86 VLOG("~EventMetricProducer() called");
87 }
88
onConfigUpdatedLocked(const StatsdConfig & config,const int configIndex,const int metricIndex,const vector<sp<AtomMatchingTracker>> & allAtomMatchingTrackers,const unordered_map<int64_t,int> & oldAtomMatchingTrackerMap,const unordered_map<int64_t,int> & newAtomMatchingTrackerMap,const sp<EventMatcherWizard> & matcherWizard,const vector<sp<ConditionTracker>> & allConditionTrackers,const unordered_map<int64_t,int> & conditionTrackerMap,const sp<ConditionWizard> & wizard,const unordered_map<int64_t,int> & metricToActivationMap,unordered_map<int,vector<int>> & trackerToMetricMap,unordered_map<int,vector<int>> & conditionToMetricMap,unordered_map<int,vector<int>> & activationAtomTrackerToMetricMap,unordered_map<int,vector<int>> & deactivationAtomTrackerToMetricMap,vector<int> & metricsWithActivation)89 optional<InvalidConfigReason> EventMetricProducer::onConfigUpdatedLocked(
90 const StatsdConfig& config, const int configIndex, const int metricIndex,
91 const vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers,
92 const unordered_map<int64_t, int>& oldAtomMatchingTrackerMap,
93 const unordered_map<int64_t, int>& newAtomMatchingTrackerMap,
94 const sp<EventMatcherWizard>& matcherWizard,
95 const vector<sp<ConditionTracker>>& allConditionTrackers,
96 const unordered_map<int64_t, int>& conditionTrackerMap, const sp<ConditionWizard>& wizard,
97 const unordered_map<int64_t, int>& metricToActivationMap,
98 unordered_map<int, vector<int>>& trackerToMetricMap,
99 unordered_map<int, vector<int>>& conditionToMetricMap,
100 unordered_map<int, vector<int>>& activationAtomTrackerToMetricMap,
101 unordered_map<int, vector<int>>& deactivationAtomTrackerToMetricMap,
102 vector<int>& metricsWithActivation) {
103 optional<InvalidConfigReason> invalidConfigReason = MetricProducer::onConfigUpdatedLocked(
104 config, configIndex, metricIndex, allAtomMatchingTrackers, oldAtomMatchingTrackerMap,
105 newAtomMatchingTrackerMap, matcherWizard, allConditionTrackers, conditionTrackerMap,
106 wizard, metricToActivationMap, trackerToMetricMap, conditionToMetricMap,
107 activationAtomTrackerToMetricMap, deactivationAtomTrackerToMetricMap,
108 metricsWithActivation);
109 if (invalidConfigReason.has_value()) {
110 return invalidConfigReason;
111 }
112
113 const EventMetric& metric = config.event_metric(configIndex);
114 int trackerIndex;
115 // Update appropriate indices, specifically mConditionIndex and MetricsManager maps.
116 invalidConfigReason = handleMetricWithAtomMatchingTrackers(
117 metric.what(), mMetricId, metricIndex, false, allAtomMatchingTrackers,
118 newAtomMatchingTrackerMap, trackerToMetricMap, trackerIndex);
119 if (invalidConfigReason.has_value()) {
120 return invalidConfigReason;
121 }
122
123 if (metric.has_condition()) {
124 invalidConfigReason = handleMetricWithConditions(
125 metric.condition(), mMetricId, metricIndex, conditionTrackerMap, metric.links(),
126 allConditionTrackers, mConditionTrackerIndex, conditionToMetricMap);
127 if (invalidConfigReason.has_value()) {
128 return invalidConfigReason;
129 }
130 }
131 return nullopt;
132 }
133
dropDataLocked(const int64_t dropTimeNs)134 void EventMetricProducer::dropDataLocked(const int64_t dropTimeNs) {
135 mAggregatedAtoms.clear();
136 mTotalSize = 0;
137 StatsdStats::getInstance().noteBucketDropped(mMetricId);
138 }
139
onSlicedConditionMayChangeLocked(bool overallCondition,const int64_t eventTime)140 void EventMetricProducer::onSlicedConditionMayChangeLocked(bool overallCondition,
141 const int64_t eventTime) {
142 }
143
serializeProtoLocked(ProtoOutputStream & protoOutput)144 std::unique_ptr<std::vector<uint8_t>> serializeProtoLocked(ProtoOutputStream& protoOutput) {
145 size_t bufferSize = protoOutput.size();
146
147 std::unique_ptr<std::vector<uint8_t>> buffer(new std::vector<uint8_t>(bufferSize));
148
149 size_t pos = 0;
150 sp<android::util::ProtoReader> reader = protoOutput.data();
151 while (reader->readBuffer() != NULL) {
152 size_t toRead = reader->currentToRead();
153 std::memcpy(&((*buffer)[pos]), reader->readBuffer(), toRead);
154 pos += toRead;
155 reader->move(toRead);
156 }
157
158 return buffer;
159 }
160
clearPastBucketsLocked(const int64_t dumpTimeNs)161 void EventMetricProducer::clearPastBucketsLocked(const int64_t dumpTimeNs) {
162 mAggregatedAtoms.clear();
163 mTotalSize = 0;
164 }
165
onDumpReportLocked(const int64_t dumpTimeNs,const bool include_current_partial_bucket,const bool erase_data,const DumpLatency dumpLatency,std::set<string> * str_set,ProtoOutputStream * protoOutput)166 void EventMetricProducer::onDumpReportLocked(const int64_t dumpTimeNs,
167 const bool include_current_partial_bucket,
168 const bool erase_data,
169 const DumpLatency dumpLatency,
170 std::set<string> *str_set,
171 ProtoOutputStream* protoOutput) {
172 protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId);
173 protoOutput->write(FIELD_TYPE_BOOL | FIELD_ID_IS_ACTIVE, isActiveLocked());
174 uint64_t protoToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_EVENT_METRICS);
175 for (const auto& [atomDimensionKey, elapsedTimestampsNs] : mAggregatedAtoms) {
176 uint64_t wrapperToken =
177 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA);
178
179 uint64_t aggregatedToken =
180 protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_AGGREGATED_ATOM);
181
182 uint64_t atomToken = protoOutput->start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOM);
183 writeFieldValueTreeToStream(atomDimensionKey.getAtomTag(),
184 atomDimensionKey.getAtomFieldValues().getValues(), protoOutput);
185 protoOutput->end(atomToken);
186 for (int64_t timestampNs : elapsedTimestampsNs) {
187 protoOutput->write(FIELD_TYPE_INT64 | FIELD_COUNT_REPEATED | FIELD_ID_ATOM_TIMESTAMPS,
188 (long long)timestampNs);
189 }
190 protoOutput->end(aggregatedToken);
191 protoOutput->end(wrapperToken);
192 }
193 protoOutput->end(protoToken);
194 if (erase_data) {
195 mAggregatedAtoms.clear();
196 mTotalSize = 0;
197 }
198 }
199
onConditionChangedLocked(const bool conditionMet,const int64_t eventTime)200 void EventMetricProducer::onConditionChangedLocked(const bool conditionMet,
201 const int64_t eventTime) {
202 VLOG("Metric %lld onConditionChanged", (long long)mMetricId);
203 mCondition = conditionMet ? ConditionState::kTrue : ConditionState::kFalse;
204 }
205
onMatchedLogEventInternalLocked(const size_t matcherIndex,const MetricDimensionKey & eventKey,const ConditionKey & conditionKey,bool condition,const LogEvent & event,const map<int,HashableDimensionKey> & statePrimaryKeys)206 void EventMetricProducer::onMatchedLogEventInternalLocked(
207 const size_t matcherIndex, const MetricDimensionKey& eventKey,
208 const ConditionKey& conditionKey, bool condition, const LogEvent& event,
209 const map<int, HashableDimensionKey>& statePrimaryKeys) {
210 if (!condition) {
211 return;
212 }
213
214 const int64_t elapsedTimeNs = truncateTimestampIfNecessary(event);
215 AtomDimensionKey key(event.GetTagId(), HashableDimensionKey(event.getValues()));
216
217 std::vector<int64_t>& aggregatedTimestampsNs = mAggregatedAtoms[key];
218 if (aggregatedTimestampsNs.empty()) {
219 mTotalSize += getSize(key.getAtomFieldValues().getValues());
220 }
221 aggregatedTimestampsNs.push_back(elapsedTimeNs);
222 mTotalSize += sizeof(int64_t); // Add the size of the event timestamp
223 }
224
byteSizeLocked() const225 size_t EventMetricProducer::byteSizeLocked() const {
226 return mTotalSize;
227 }
228
229 } // namespace statsd
230 } // namespace os
231 } // namespace android
232