Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 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 | |
Yao Chen | 3c0b95c | 2017-12-16 14:34:20 -0800 | [diff] [blame] | 17 | #define DEBUG false // STOPSHIP if true |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 18 | #include "Log.h" |
| 19 | |
| 20 | #include "EventMetricProducer.h" |
| 21 | #include "stats_util.h" |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 22 | #include "stats_log_util.h" |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 23 | |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 24 | #include <limits.h> |
| 25 | #include <stdlib.h> |
| 26 | |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 27 | using android::util::FIELD_COUNT_REPEATED; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 28 | using android::util::FIELD_TYPE_BOOL; |
| 29 | using android::util::FIELD_TYPE_FLOAT; |
| 30 | using android::util::FIELD_TYPE_INT32; |
| 31 | using android::util::FIELD_TYPE_INT64; |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 32 | using android::util::FIELD_TYPE_STRING; |
yro | 2b0f886 | 2017-11-06 14:27:31 -0800 | [diff] [blame] | 33 | using android::util::FIELD_TYPE_MESSAGE; |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 34 | using android::util::ProtoOutputStream; |
| 35 | using std::map; |
| 36 | using std::string; |
| 37 | using std::unordered_map; |
| 38 | using std::vector; |
Ruchir Rastogi | 21a287b | 2019-10-02 12:04:33 -0700 | [diff] [blame] | 39 | using std::shared_ptr; |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 40 | |
| 41 | namespace android { |
| 42 | namespace os { |
| 43 | namespace statsd { |
| 44 | |
| 45 | // for StatsLogReport |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 46 | const int FIELD_ID_ID = 1; |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 47 | const int FIELD_ID_EVENT_METRICS = 4; |
Howard Ro | 9440e09 | 2018-12-16 19:15:21 -0800 | [diff] [blame] | 48 | const int FIELD_ID_IS_ACTIVE = 14; |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 49 | // for EventMetricDataWrapper |
| 50 | const int FIELD_ID_DATA = 1; |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 51 | // for EventMetricData |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 52 | const int FIELD_ID_ELAPSED_TIMESTAMP_NANOS = 1; |
Stefan Lafon | ae2df01 | 2017-11-14 09:17:21 -0800 | [diff] [blame] | 53 | const int FIELD_ID_ATOMS = 2; |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 54 | |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 55 | EventMetricProducer::EventMetricProducer( |
| 56 | const ConfigKey& key, const EventMetric& metric, const int conditionIndex, |
| 57 | const sp<ConditionWizard>& wizard, const int64_t startTimeNs, |
| 58 | const unordered_map<int, shared_ptr<Activation>>& eventActivationMap, |
| 59 | const unordered_map<int, vector<shared_ptr<Activation>>>& eventDeactivationMap, |
| 60 | const vector<int>& slicedStateAtoms, |
| 61 | const unordered_map<int, unordered_map<int, int64_t>>& stateGroupMap) |
Ruchir Rastogi | 21a287b | 2019-10-02 12:04:33 -0700 | [diff] [blame] | 62 | : MetricProducer(metric.id(), key, startTimeNs, conditionIndex, wizard, eventActivationMap, |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 63 | eventDeactivationMap, slicedStateAtoms, stateGroupMap) { |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 64 | if (metric.links().size() > 0) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 65 | for (const auto& link : metric.links()) { |
| 66 | Metric2Condition mc; |
| 67 | mc.conditionId = link.condition(); |
| 68 | translateFieldMatcher(link.fields_in_what(), &mc.metricFields); |
| 69 | translateFieldMatcher(link.fields_in_condition(), &mc.conditionFields); |
| 70 | mMetric2ConditionLinks.push_back(mc); |
| 71 | } |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 72 | mConditionSliced = true; |
| 73 | } |
Yi Jin | 7f9e63b | 2018-02-02 16:25:11 -0800 | [diff] [blame] | 74 | mProto = std::make_unique<ProtoOutputStream>(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 75 | VLOG("metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(), |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 76 | (long long)mBucketSizeNs, (long long)mTimeBaseNs); |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | EventMetricProducer::~EventMetricProducer() { |
| 80 | VLOG("~EventMetricProducer() called"); |
| 81 | } |
| 82 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 83 | void EventMetricProducer::dropDataLocked(const int64_t dropTimeNs) { |
Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 84 | mProto->clear(); |
Olivier Gaillard | 320952b | 2019-02-06 13:57:24 +0000 | [diff] [blame] | 85 | StatsdStats::getInstance().noteBucketDropped(mMetricId); |
Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 88 | void EventMetricProducer::onSlicedConditionMayChangeLocked(bool overallCondition, |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 89 | const int64_t eventTime) { |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 92 | std::unique_ptr<std::vector<uint8_t>> serializeProtoLocked(ProtoOutputStream& protoOutput) { |
| 93 | size_t bufferSize = protoOutput.size(); |
| 94 | |
| 95 | std::unique_ptr<std::vector<uint8_t>> buffer(new std::vector<uint8_t>(bufferSize)); |
| 96 | |
| 97 | size_t pos = 0; |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 98 | sp<android::util::ProtoReader> reader = protoOutput.data(); |
| 99 | while (reader->readBuffer() != NULL) { |
| 100 | size_t toRead = reader->currentToRead(); |
| 101 | std::memcpy(&((*buffer)[pos]), reader->readBuffer(), toRead); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 102 | pos += toRead; |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 103 | reader->move(toRead); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | return buffer; |
| 107 | } |
| 108 | |
Yangster-mac | a802d73 | 2018-04-24 07:50:38 -0700 | [diff] [blame] | 109 | void EventMetricProducer::clearPastBucketsLocked(const int64_t dumpTimeNs) { |
| 110 | mProto->clear(); |
| 111 | } |
| 112 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 113 | void EventMetricProducer::onDumpReportLocked(const int64_t dumpTimeNs, |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 114 | const bool include_current_partial_bucket, |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 115 | const bool erase_data, |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 116 | const DumpLatency dumpLatency, |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 117 | std::set<string> *str_set, |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 118 | ProtoOutputStream* protoOutput) { |
Yang Lu | b472291 | 2018-11-15 11:02:03 -0800 | [diff] [blame] | 119 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId); |
Howard Ro | 07e23ff | 2018-12-17 17:28:07 -0800 | [diff] [blame] | 120 | protoOutput->write(FIELD_TYPE_BOOL | FIELD_ID_IS_ACTIVE, isActiveLocked()); |
Yangster-mac | 635b4b3 | 2018-01-23 20:17:35 -0800 | [diff] [blame] | 121 | if (mProto->size() <= 0) { |
| 122 | return; |
| 123 | } |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 124 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 125 | size_t bufferSize = mProto->size(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 126 | VLOG("metric %lld dump report now... proto size: %zu ", |
| 127 | (long long)mMetricId, bufferSize); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 128 | std::unique_ptr<std::vector<uint8_t>> buffer = serializeProtoLocked(*mProto); |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 129 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 130 | protoOutput->write(FIELD_TYPE_MESSAGE | FIELD_ID_EVENT_METRICS, |
| 131 | reinterpret_cast<char*>(buffer.get()->data()), buffer.get()->size()); |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 132 | |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 133 | if (erase_data) { |
| 134 | mProto->clear(); |
| 135 | } |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 138 | void EventMetricProducer::onConditionChangedLocked(const bool conditionMet, |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 139 | const int64_t eventTime) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 140 | VLOG("Metric %lld onConditionChanged", (long long)mMetricId); |
Olivier Gaillard | e63d9e0 | 2019-02-12 14:43:59 +0000 | [diff] [blame] | 141 | mCondition = conditionMet ? ConditionState::kTrue : ConditionState::kFalse; |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 144 | void EventMetricProducer::onMatchedLogEventInternalLocked( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 145 | const size_t matcherIndex, const MetricDimensionKey& eventKey, |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 146 | const ConditionKey& conditionKey, bool condition, const LogEvent& event, |
| 147 | const map<int, HashableDimensionKey>& statePrimaryKeys) { |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 148 | if (!condition) { |
| 149 | return; |
| 150 | } |
| 151 | |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 152 | uint64_t wrapperToken = |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 153 | mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA); |
Muhammad Qureshi | a7de000 | 2020-04-15 11:34:35 -0700 | [diff] [blame] | 154 | const int64_t elapsedTimeNs = truncateTimestampIfNecessary(event); |
Tej Singh | 29ac601 | 2019-06-05 18:10:12 -0700 | [diff] [blame] | 155 | mProto->write(FIELD_TYPE_INT64 | FIELD_ID_ELAPSED_TIMESTAMP_NANOS, (long long) elapsedTimeNs); |
Yangster | e1faa03 | 2018-02-21 14:08:17 -0800 | [diff] [blame] | 156 | |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 157 | uint64_t eventToken = mProto->start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOMS); |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 158 | event.ToProto(*mProto); |
| 159 | mProto->end(eventToken); |
| 160 | mProto->end(wrapperToken); |
| 161 | } |
| 162 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 163 | size_t EventMetricProducer::byteSizeLocked() const { |
yro | 75f0a4d | 2017-11-17 17:20:45 -0800 | [diff] [blame] | 164 | return mProto->bytesWritten(); |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 167 | } // namespace statsd |
| 168 | } // namespace os |
| 169 | } // namespace android |