diff options
author | Yi Jin <jinyithu@google.com> | 2018-02-02 16:25:11 -0800 |
---|---|---|
committer | Yi Jin <jinyithu@google.com> | 2018-02-09 14:20:48 -0800 |
commit | 7f9e63b63269baa41a36fc31acd6fc071309c26c (patch) | |
tree | 783f5e5b55341f7dad48db2fba706295f9b25ca8 | |
parent | 421822ce0f3bb581d36cdc5273d4c345c634f9c0 (diff) |
Add clear method to ProtoOutputStream so it is reusable by just
rewinding its internal pointer.
Bug: 72319159
Test: atest incidentd_test and atest statsd_test
Change-Id: Id046b987a7460d68300e5d4d46ae56aec1eba2d0
-rw-r--r-- | cmds/incidentd/src/PrivacyBuffer.cpp | 2 | ||||
-rw-r--r-- | cmds/statsd/AndroidTest.xml | 26 | ||||
-rw-r--r-- | cmds/statsd/src/anomaly/AnomalyTracker.cpp | 1 | ||||
-rw-r--r-- | cmds/statsd/src/metrics/EventMetricProducer.cpp | 10 | ||||
-rw-r--r-- | cmds/statsd/src/metrics/EventMetricProducer.h | 3 | ||||
-rw-r--r-- | libs/protoutil/include/android/util/EncodedBuffer.h | 5 | ||||
-rw-r--r-- | libs/protoutil/include/android/util/ProtoOutputStream.h | 5 | ||||
-rw-r--r-- | libs/protoutil/src/EncodedBuffer.cpp | 7 | ||||
-rw-r--r-- | libs/protoutil/src/ProtoOutputStream.cpp | 12 |
9 files changed, 58 insertions, 13 deletions
diff --git a/cmds/incidentd/src/PrivacyBuffer.cpp b/cmds/incidentd/src/PrivacyBuffer.cpp index 03faa9290666..f53befefab93 100644 --- a/cmds/incidentd/src/PrivacyBuffer.cpp +++ b/cmds/incidentd/src/PrivacyBuffer.cpp @@ -142,7 +142,7 @@ void PrivacyBuffer::clear() { mSize = 0; - mProto = ProtoOutputStream(); + mProto.clear(); } size_t diff --git a/cmds/statsd/AndroidTest.xml b/cmds/statsd/AndroidTest.xml new file mode 100644 index 000000000000..afe30a269093 --- /dev/null +++ b/cmds/statsd/AndroidTest.xml @@ -0,0 +1,26 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2017 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<configuration description="Config for statsd_test"> + <target_preparer class="com.android.tradefed.targetprep.PushFilePreparer"> + <option name="cleanup" value="true" /> + <option name="push" value="statsd_test->/data/nativetest/statsd_test" /> + </target_preparer> + <option name="test-suite-tag" value="apct" /> + <test class="com.android.tradefed.testtype.GTest" > + <option name="native-test-device-path" value="/data/nativetest" /> + <option name="module-name" value="statsd_test" /> + </test> +</configuration>
\ No newline at end of file diff --git a/cmds/statsd/src/anomaly/AnomalyTracker.cpp b/cmds/statsd/src/anomaly/AnomalyTracker.cpp index 63f6e2acd9df..ba16ec83a5c5 100644 --- a/cmds/statsd/src/anomaly/AnomalyTracker.cpp +++ b/cmds/statsd/src/anomaly/AnomalyTracker.cpp @@ -19,7 +19,6 @@ #include "AnomalyTracker.h" #include "external/Perfetto.h" -#include "frameworks/base/libs/incident/proto/android/os/header.pb.h" #include "guardrail/StatsdStats.h" #include "subscriber/IncidentdReporter.h" #include "subscriber/SubscriberReporter.h" diff --git a/cmds/statsd/src/metrics/EventMetricProducer.cpp b/cmds/statsd/src/metrics/EventMetricProducer.cpp index 0578e0682687..936a2ef1fde4 100644 --- a/cmds/statsd/src/metrics/EventMetricProducer.cpp +++ b/cmds/statsd/src/metrics/EventMetricProducer.cpp @@ -59,9 +59,7 @@ EventMetricProducer::EventMetricProducer(const ConfigKey& key, const EventMetric metric.links().end()); mConditionSliced = true; } - - startNewProtoOutputStreamLocked(); - + mProto = std::make_unique<ProtoOutputStream>(); VLOG("metric %lld created. bucket size %lld start_time: %lld", (long long)metric.id(), (long long)mBucketSizeNs, (long long)mStartTimeNs); } @@ -70,10 +68,6 @@ EventMetricProducer::~EventMetricProducer() { VLOG("~EventMetricProducer() called"); } -void EventMetricProducer::startNewProtoOutputStreamLocked() { - mProto = std::make_unique<ProtoOutputStream>(); -} - void EventMetricProducer::onSlicedConditionMayChangeLocked(const uint64_t eventTime) { } @@ -113,7 +107,7 @@ void EventMetricProducer::onDumpReportLocked(const uint64_t dumpTimeNs, protoOutput->write(FIELD_TYPE_MESSAGE | FIELD_ID_EVENT_METRICS, reinterpret_cast<char*>(buffer.get()->data()), buffer.get()->size()); - startNewProtoOutputStreamLocked(); + mProto->clear(); } void EventMetricProducer::onConditionChangedLocked(const bool conditionMet, diff --git a/cmds/statsd/src/metrics/EventMetricProducer.h b/cmds/statsd/src/metrics/EventMetricProducer.h index 935f206017fa..394ed234f02e 100644 --- a/cmds/statsd/src/metrics/EventMetricProducer.h +++ b/cmds/statsd/src/metrics/EventMetricProducer.h @@ -40,9 +40,6 @@ public: virtual ~EventMetricProducer(); -protected: - void startNewProtoOutputStreamLocked(); - private: void onMatchedLogEventInternalLocked( const size_t matcherIndex, const MetricDimensionKey& eventKey, diff --git a/libs/protoutil/include/android/util/EncodedBuffer.h b/libs/protoutil/include/android/util/EncodedBuffer.h index e568e4cf02fc..0a8a5aa347ba 100644 --- a/libs/protoutil/include/android/util/EncodedBuffer.h +++ b/libs/protoutil/include/android/util/EncodedBuffer.h @@ -64,6 +64,11 @@ public: size_t mOffset; }; + /** + * Clears the buffer by rewinding its write pointer to avoid de/allocate buffers in heap. + */ + void clear(); + /******************************** Write APIs ************************************************/ /** diff --git a/libs/protoutil/include/android/util/ProtoOutputStream.h b/libs/protoutil/include/android/util/ProtoOutputStream.h index faea9b21311d..52830d397772 100644 --- a/libs/protoutil/include/android/util/ProtoOutputStream.h +++ b/libs/protoutil/include/android/util/ProtoOutputStream.h @@ -123,6 +123,11 @@ public: EncodedBuffer::iterator data(); // Get the reader apis of the data. bool flush(int fd); // Flush data directly to a file descriptor. + /** + * Clears the ProtoOutputStream so the buffer can be reused instead of deallocation/allocation again. + */ + void clear(); + // Please don't use the following functions to dump protos unless you are familiar with protobuf encoding. void writeRawVarint(uint64_t varint); void writeLengthDelimitedHeader(uint32_t id, size_t size); diff --git a/libs/protoutil/src/EncodedBuffer.cpp b/libs/protoutil/src/EncodedBuffer.cpp index 435ae8836217..3a5e2e9ef5d0 100644 --- a/libs/protoutil/src/EncodedBuffer.cpp +++ b/libs/protoutil/src/EncodedBuffer.cpp @@ -106,6 +106,13 @@ EncodedBuffer::at(const Pointer& p) const return mBuffers[p.index()] + p.offset(); } +void +EncodedBuffer::clear() +{ + mWp.rewind(); + mEp.rewind(); +} + /******************************** Write APIs ************************************************/ size_t EncodedBuffer::size() const diff --git a/libs/protoutil/src/ProtoOutputStream.cpp b/libs/protoutil/src/ProtoOutputStream.cpp index f24abaed2dce..9d9ffec4c588 100644 --- a/libs/protoutil/src/ProtoOutputStream.cpp +++ b/libs/protoutil/src/ProtoOutputStream.cpp @@ -36,6 +36,18 @@ ProtoOutputStream::~ProtoOutputStream() { } + +void +ProtoOutputStream::clear() +{ + mBuffer.clear(); + mCopyBegin = 0; + mCompact = false; + mDepth = 0; + mObjectId = 0; + mExpectedObjectToken = 0LL; +} + bool ProtoOutputStream::write(uint64_t fieldId, double val) { |