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 | |
| 17 | #define DEBUG true // STOPSHIP if true |
| 18 | #include "Log.h" |
| 19 | |
| 20 | #include "EventMetricProducer.h" |
| 21 | #include "stats_util.h" |
| 22 | |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 23 | #include <limits.h> |
| 24 | #include <stdlib.h> |
| 25 | |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 26 | using namespace android::util; |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 27 | using android::util::ProtoOutputStream; |
| 28 | using std::map; |
| 29 | using std::string; |
| 30 | using std::unordered_map; |
| 31 | using std::vector; |
| 32 | |
| 33 | namespace android { |
| 34 | namespace os { |
| 35 | namespace statsd { |
| 36 | |
| 37 | // for StatsLogReport |
| 38 | const int FIELD_ID_METRIC_ID = 1; |
| 39 | const int FIELD_ID_START_REPORT_NANOS = 2; |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 40 | const int FIELD_ID_END_REPORT_NANOS = 3; |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 41 | const int FIELD_ID_EVENT_METRICS = 4; |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 42 | // for EventMetricDataWrapper |
| 43 | const int FIELD_ID_DATA = 1; |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 44 | // for EventMetricData |
| 45 | const int FIELD_ID_TIMESTAMP_NANOS = 1; |
| 46 | const int FIELD_ID_STATS_EVENTS = 2; |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 47 | |
| 48 | EventMetricProducer::EventMetricProducer(const EventMetric& metric, const int conditionIndex, |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame^] | 49 | const sp<ConditionWizard>& wizard, |
| 50 | const uint64_t startTimeNs) |
| 51 | : MetricProducer(startTimeNs, conditionIndex, wizard), mMetric(metric) { |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 52 | if (metric.links().size() > 0) { |
| 53 | mConditionLinks.insert(mConditionLinks.begin(), metric.links().begin(), |
| 54 | metric.links().end()); |
| 55 | mConditionSliced = true; |
| 56 | } |
| 57 | |
| 58 | startNewProtoOutputStream(mStartTimeNs); |
| 59 | |
| 60 | VLOG("metric %lld created. bucket size %lld start_time: %lld", metric.metric_id(), |
| 61 | (long long)mBucketSizeNs, (long long)mStartTimeNs); |
| 62 | } |
| 63 | |
| 64 | EventMetricProducer::~EventMetricProducer() { |
| 65 | VLOG("~EventMetricProducer() called"); |
| 66 | } |
| 67 | |
| 68 | void EventMetricProducer::startNewProtoOutputStream(long long startTime) { |
| 69 | mProto = std::make_unique<ProtoOutputStream>(); |
| 70 | // TODO: We need to auto-generate the field IDs for StatsLogReport, EventMetricData, |
| 71 | // and StatsEvent. |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 72 | mProto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_ID, mMetric.metric_id()); |
| 73 | mProto->write(FIELD_TYPE_INT64 | FIELD_ID_START_REPORT_NANOS, startTime); |
| 74 | mProtoToken = mProto->start(FIELD_TYPE_MESSAGE | FIELD_ID_EVENT_METRICS); |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void EventMetricProducer::finish() { |
| 78 | } |
| 79 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 80 | void EventMetricProducer::onSlicedConditionMayChange(const uint64_t eventTime) { |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | StatsLogReport EventMetricProducer::onDumpReport() { |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame^] | 84 | long long endTime = time(nullptr) * NS_PER_SEC; |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 85 | mProto->end(mProtoToken); |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 86 | mProto->write(FIELD_TYPE_INT64 | FIELD_ID_END_REPORT_NANOS, endTime); |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 87 | |
| 88 | size_t bufferSize = mProto->size(); |
| 89 | VLOG("metric %lld dump report now... proto size: %zu ", mMetric.metric_id(), bufferSize); |
| 90 | std::unique_ptr<uint8_t[]> buffer(new uint8_t[bufferSize]); |
| 91 | size_t pos = 0; |
| 92 | auto it = mProto->data(); |
| 93 | while (it.readBuffer() != NULL) { |
| 94 | size_t toRead = it.currentToRead(); |
| 95 | std::memcpy(&buffer[pos], it.readBuffer(), toRead); |
| 96 | pos += toRead; |
| 97 | it.rp()->move(toRead); |
| 98 | } |
| 99 | |
| 100 | startNewProtoOutputStream(endTime); |
| 101 | |
| 102 | // TODO: Once we migrate all MetricProducers to use ProtoOutputStream, we should return this: |
| 103 | // return std::move(buffer); |
| 104 | return StatsLogReport(); |
| 105 | } |
| 106 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 107 | void EventMetricProducer::onConditionChanged(const bool conditionMet, const uint64_t eventTime) { |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 108 | VLOG("Metric %lld onConditionChanged", mMetric.metric_id()); |
| 109 | mCondition = conditionMet; |
| 110 | } |
| 111 | |
| 112 | void EventMetricProducer::onMatchedLogEventInternal( |
| 113 | const size_t matcherIndex, const HashableDimensionKey& eventKey, |
| 114 | const std::map<std::string, HashableDimensionKey>& conditionKey, bool condition, |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 115 | const LogEvent& event, bool scheduledPull) { |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 116 | if (!condition) { |
| 117 | return; |
| 118 | } |
| 119 | |
yro | 24809bd | 2017-10-31 23:06:53 -0700 | [diff] [blame] | 120 | long long wrapperToken = mProto->start(FIELD_TYPE_MESSAGE | FIELD_ID_DATA); |
| 121 | mProto->write(FIELD_TYPE_INT64 | FIELD_ID_TIMESTAMP_NANOS, (long long)event.GetTimestampNs()); |
| 122 | long long eventToken = mProto->start(FIELD_TYPE_MESSAGE | FIELD_ID_STATS_EVENTS); |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 123 | event.ToProto(*mProto); |
| 124 | mProto->end(eventToken); |
| 125 | mProto->end(wrapperToken); |
| 126 | } |
| 127 | |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 128 | size_t EventMetricProducer::byteSize() { |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame^] | 129 | return mProto->size(); |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Yao Chen | 5110bed | 2017-10-23 12:50:02 -0700 | [diff] [blame] | 132 | } // namespace statsd |
| 133 | } // namespace os |
| 134 | } // namespace android |