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; |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 47 | // for EventMetricDataWrapper |
| 48 | const int FIELD_ID_DATA = 1; |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 49 | // for EventMetricData |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 50 | const int FIELD_ID_ELAPSED_TIMESTAMP_NANOS = 1; |
Stefan Lafon | ae2df01 | 2017-11-14 09:17:21 -0800 | [diff] [blame] | 51 | const int FIELD_ID_ATOMS = 2; |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 52 | const int FIELD_ID_WALL_CLOCK_TIMESTAMP_NANOS = 3; |
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, |
| 57 | const uint64_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(), |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 71 | (long long)mBucketSizeNs, (long long)mStartTimeNs); |
| 72 | } |
| 73 | |
| 74 | EventMetricProducer::~EventMetricProducer() { |
| 75 | VLOG("~EventMetricProducer() called"); |
| 76 | } |
| 77 | |
Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 78 | void EventMetricProducer::dropDataLocked(const uint64_t dropTimeNs) { |
| 79 | mProto->clear(); |
| 80 | } |
| 81 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 82 | void EventMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) { |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 85 | std::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 | |
| 102 | void EventMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs, |
| 103 | ProtoOutputStream* protoOutput) { |
Yangster-mac | 635b4b3 | 2018-01-23 20:17:35 -0800 | [diff] [blame] | 104 | if (mProto->size() <= 0) { |
| 105 | return; |
| 106 | } |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 107 | protoOutput->write(FIELD_TYPE_INT64 | FIELD_ID_ID, (long long)mMetricId); |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 108 | |
Yao Chen | 6a8c799 | 2017-11-29 20:02:07 +0000 | [diff] [blame] | 109 | size_t bufferSize = mProto->size(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 110 | VLOG("metric %lld dump report now... proto size: %zu ", |
| 111 | (long long)mMetricId, bufferSize); |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 112 | std::unique_ptr<std::vector<uint8_t>> buffer = serializeProtoLocked(*mProto); |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 113 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 114 | protoOutput->write(FIELD_TYPE_MESSAGE | FIELD_ID_EVENT_METRICS, |
| 115 | reinterpret_cast<char*>(buffer.get()->data()), buffer.get()->size()); |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 116 | |
Yi Jin | 7f9e63b | 2018-02-02 16:25:11 -0800 | [diff] [blame] | 117 | mProto->clear(); |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 120 | void EventMetricProducer::onConditionChangedLocked(const bool conditionMet, |
| 121 | const uint64_t eventTime) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 122 | VLOG("Metric %lld onConditionChanged", (long long)mMetricId); |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 123 | mCondition = conditionMet; |
| 124 | } |
| 125 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 126 | void EventMetricProducer::onMatchedLogEventInternalLocked( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 127 | const size_t matcherIndex, const MetricDimensionKey& eventKey, |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 128 | const ConditionKey& conditionKey, bool condition, |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 129 | const LogEvent& event) { |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 130 | if (!condition) { |
| 131 | return; |
| 132 | } |
| 133 | |
yro | b0378b0 | 2017-11-09 20:36:25 -0800 | [diff] [blame] | 134 | long long wrapperToken = |
| 135 | mProto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_DATA); |
Yangster-mac | d5c3562 | 2018-02-02 10:33:25 -0800 | [diff] [blame] | 136 | const bool truncateTimestamp = |
| 137 | android::util::kNotTruncatingTimestampAtomWhiteList.find(event.GetTagId()) == |
| 138 | android::util::kNotTruncatingTimestampAtomWhiteList.end(); |
| 139 | if (truncateTimestamp) { |
| 140 | mProto->write(FIELD_TYPE_INT64 | FIELD_ID_ELAPSED_TIMESTAMP_NANOS, |
| 141 | (long long)truncateTimestampNsToFiveMinutes(event.GetElapsedTimestampNs())); |
| 142 | mProto->write(FIELD_TYPE_INT64 | FIELD_ID_WALL_CLOCK_TIMESTAMP_NANOS, |
| 143 | (long long)truncateTimestampNsToFiveMinutes(getWallClockNs())); |
| 144 | } else { |
| 145 | mProto->write(FIELD_TYPE_INT64 | FIELD_ID_ELAPSED_TIMESTAMP_NANOS, |
| 146 | (long long)event.GetElapsedTimestampNs()); |
| 147 | mProto->write(FIELD_TYPE_INT64 | FIELD_ID_WALL_CLOCK_TIMESTAMP_NANOS, |
| 148 | (long long)getWallClockNs()); |
| 149 | } |
Yangster | e1faa03 | 2018-02-21 14:08:17 -0800 | [diff] [blame] | 150 | |
| 151 | long long eventToken = mProto->start(FIELD_TYPE_MESSAGE | FIELD_ID_ATOMS); |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 152 | event.ToProto(*mProto); |
| 153 | mProto->end(eventToken); |
| 154 | mProto->end(wrapperToken); |
| 155 | } |
| 156 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 157 | size_t EventMetricProducer::byteSizeLocked() const { |
yro | 75f0a4d | 2017-11-17 17:20:45 -0800 | [diff] [blame] | 158 | return mProto->bytesWritten(); |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 161 | } // namespace statsd |
| 162 | } // namespace os |
| 163 | } // namespace android |