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; |
| 39 | |
| 40 | namespace android { |
| 41 | namespace os { |
| 42 | namespace statsd { |
| 43 | |
| 44 | // for StatsLogReport |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 45 | const int FIELD_ID_ID = 1; |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 46 | const int FIELD_ID_EVENT_METRICS = 4; |
Howard Ro | 9440e09 | 2018-12-16 19:15:21 -0800 | [diff] [blame] | 47 | const int FIELD_ID_IS_ACTIVE = 14; |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 48 | // for EventMetricDataWrapper |
| 49 | const int FIELD_ID_DATA = 1; |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 50 | // for EventMetricData |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 51 | const int FIELD_ID_ELAPSED_TIMESTAMP_NANOS = 1; |
Stefan Lafon | ae2df01 | 2017-11-14 09:17:21 -0800 | [diff] [blame] | 52 | const int FIELD_ID_ATOMS = 2; |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 53 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 54 | EventMetricProducer::EventMetricProducer(const ConfigKey& key, const EventMetric& metric, |
| 55 | const int conditionIndex, |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 56 | const sp<ConditionWizard>& wizard, |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 57 | const int64_t startTimeNs) |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 58 | : MetricProducer(metric.id(), key, startTimeNs, conditionIndex, wizard) { |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 59 | if (metric.links().size() > 0) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 60 | 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 Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 67 | mConditionSliced = true; |
| 68 | } |
Yi Jin | 7f9e63b | 2018-02-02 16:25:11 -0800 | [diff] [blame] | 69 | mProto = std::make_unique<ProtoOutputStream>(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 70 | 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] | 71 | (long long)mBucketSizeNs, (long long)mTimeBaseNs); |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | EventMetricProducer::~EventMetricProducer() { |
| 75 | VLOG("~EventMetricProducer() called"); |
| 76 | } |
| 77 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 78 | void EventMetricProducer::dropDataLocked(const int64_t dropTimeNs) { |
Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 79 | mProto->clear(); |
Olivier Gaillard | 320952b | 2019-02-06 13:57:24 +0000 | [diff] [blame] | 80 | StatsdStats::getInstance().noteBucketDropped(mMetricId); |
Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 83 | void EventMetricProducer::onSlicedConditionMayChangeLocked(bool overallCondition, |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 84 | const int64_t eventTime) { |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 87 | std::unique_ptr<std::vector<uint8_t>> serializeProtoLocked(ProtoOutputStream& protoOutput) { |
| 88 | size_t bufferSize = protoOutput.size(); |
| 89 | |
| 90 | std::unique_ptr<std::vector<uint8_t>> buffer(new std::vector<uint8_t>(bufferSize)); |
| 91 | |
| 92 | size_t pos = 0; |
| 93 | auto it = protoOutput.data(); |
| 94 | while (it.readBuffer() != NULL) { |
| 95 | size_t toRead = it.currentToRead(); |
| 96 | std::memcpy(&((*buffer)[pos]), it.readBuffer(), toRead); |
| 97 | pos += toRead; |
| 98 | it.rp()->move(toRead); |
| 99 | } |
| 100 | |
| 101 | return buffer; |
| 102 | } |
| 103 | |
Yangster-mac | a802d73 | 2018-04-24 07:50:38 -0700 | [diff] [blame] | 104 | void EventMetricProducer::clearPastBucketsLocked(const int64_t dumpTimeNs) { |
| 105 | mProto->clear(); |
| 106 | } |
| 107 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 108 | void EventMetricProducer::onDumpReportLocked(const int64_t dumpTimeNs, |
Yangster-mac | e68f3a5 | 2018-04-04 00:01:43 -0700 | [diff] [blame] | 109 | const bool include_current_partial_bucket, |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 110 | const bool erase_data, |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 111 | std::set<string> *str_set, |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 112 | ProtoOutputStream* protoOutput) { |
Yang Lu | b472291 | 2018-11-15 11:02:03 -0800 | [diff] [blame] | 113 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId); |
Howard Ro | 07e23ff | 2018-12-17 17:28:07 -0800 | [diff] [blame] | 114 | protoOutput->write(FIELD_TYPE_BOOL | FIELD_ID_IS_ACTIVE, isActiveLocked()); |
Yangster-mac | 635b4b3 | 2018-01-23 20:17:35 -0800 | [diff] [blame] | 115 | if (mProto->size() <= 0) { |
| 116 | return; |
| 117 | } |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 118 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 119 | size_t bufferSize = mProto->size(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 120 | VLOG("metric %lld dump report now... proto size: %zu ", |
| 121 | (long long)mMetricId, bufferSize); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 122 | std::unique_ptr<std::vector<uint8_t>> buffer = serializeProtoLocked(*mProto); |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 123 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 124 | protoOutput->write(FIELD_TYPE_MESSAGE | FIELD_ID_EVENT_METRICS, |
| 125 | reinterpret_cast<char*>(buffer.get()->data()), buffer.get()->size()); |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 126 | |
Bookatz | ff71cad | 2018-09-20 17:17:49 -0700 | [diff] [blame] | 127 | if (erase_data) { |
| 128 | mProto->clear(); |
| 129 | } |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 132 | void EventMetricProducer::onConditionChangedLocked(const bool conditionMet, |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 133 | const int64_t eventTime) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 134 | VLOG("Metric %lld onConditionChanged", (long long)mMetricId); |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 135 | mCondition = conditionMet; |
| 136 | } |
| 137 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 138 | void EventMetricProducer::onMatchedLogEventInternalLocked( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 139 | const size_t matcherIndex, const MetricDimensionKey& eventKey, |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 140 | const ConditionKey& conditionKey, bool condition, |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 141 | const LogEvent& event) { |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 142 | if (!condition) { |
| 143 | return; |
| 144 | } |
| 145 | |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 146 | uint64_t wrapperToken = |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 147 | mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA); |
Yangster-mac | d5c3562 | 2018-02-02 10:33:25 -0800 | [diff] [blame] | 148 | const bool truncateTimestamp = |
Yao Chen | c40a19d | 2018-03-15 16:48:25 -0700 | [diff] [blame] | 149 | android::util::AtomsInfo::kNotTruncatingTimestampAtomWhiteList.find(event.GetTagId()) == |
| 150 | android::util::AtomsInfo::kNotTruncatingTimestampAtomWhiteList.end(); |
Yangster-mac | d5c3562 | 2018-02-02 10:33:25 -0800 | [diff] [blame] | 151 | if (truncateTimestamp) { |
| 152 | mProto->write(FIELD_TYPE_INT64 | FIELD_ID_ELAPSED_TIMESTAMP_NANOS, |
| 153 | (long long)truncateTimestampNsToFiveMinutes(event.GetElapsedTimestampNs())); |
Yangster-mac | d5c3562 | 2018-02-02 10:33:25 -0800 | [diff] [blame] | 154 | } else { |
| 155 | mProto->write(FIELD_TYPE_INT64 | FIELD_ID_ELAPSED_TIMESTAMP_NANOS, |
| 156 | (long long)event.GetElapsedTimestampNs()); |
Yangster-mac | d5c3562 | 2018-02-02 10:33:25 -0800 | [diff] [blame] | 157 | } |
Yangster | e1faa03 | 2018-02-21 14:08:17 -0800 | [diff] [blame] | 158 | |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 159 | uint64_t eventToken = mProto->start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOMS); |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 160 | event.ToProto(*mProto); |
| 161 | mProto->end(eventToken); |
| 162 | mProto->end(wrapperToken); |
| 163 | } |
| 164 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 165 | size_t EventMetricProducer::byteSizeLocked() const { |
yro | 75f0a4d | 2017-11-17 17:20:45 -0800 | [diff] [blame] | 166 | return mProto->bytesWritten(); |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 169 | } // namespace statsd |
| 170 | } // namespace os |
| 171 | } // namespace android |