Home | History | Annotate | Download | only in core
      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 #include "perfetto/tracing/core/trace_packet.h"
     18 
     19 #include "perfetto/base/logging.h"
     20 #include "perfetto/protozero/proto_utils.h"
     21 #include "src/tracing/core/sliced_protobuf_input_stream.h"
     22 
     23 namespace perfetto {
     24 
     25 TracePacket::TracePacket() = default;
     26 TracePacket::~TracePacket() = default;
     27 
     28 TracePacket::TracePacket(TracePacket&& other) noexcept {
     29   *this = std::move(other);
     30 }
     31 
     32 TracePacket& TracePacket::operator=(TracePacket&& other) {
     33   slices_ = std::move(other.slices_);
     34   other.slices_.clear();
     35   size_ = other.size_;
     36   other.size_ = 0;
     37   return *this;
     38 }
     39 
     40 void TracePacket::AddSlice(Slice slice) {
     41   size_ += slice.size;
     42   slices_.push_back(std::move(slice));
     43 }
     44 
     45 void TracePacket::AddSlice(const void* start, size_t size) {
     46   size_ += size;
     47   slices_.emplace_back(start, size);
     48 }
     49 
     50 std::tuple<char*, size_t> TracePacket::GetProtoPreamble() {
     51   using protozero::proto_utils::MakeTagLengthDelimited;
     52   using protozero::proto_utils::WriteVarInt;
     53   uint8_t* ptr = reinterpret_cast<uint8_t*>(&preamble_[0]);
     54 
     55   constexpr uint8_t tag = MakeTagLengthDelimited(kPacketFieldNumber);
     56   static_assert(tag < 0x80, "TracePacket tag should fit in one byte");
     57   *(ptr++) = tag;
     58 
     59   ptr = WriteVarInt(size(), ptr);
     60   size_t preamble_size = reinterpret_cast<uintptr_t>(ptr) -
     61                          reinterpret_cast<uintptr_t>(&preamble_[0]);
     62   PERFETTO_DCHECK(preamble_size <= sizeof(preamble_));
     63   return std::make_tuple(&preamble_[0], preamble_size);
     64 }
     65 
     66 std::unique_ptr<TracePacket::ZeroCopyInputStream>
     67 TracePacket::CreateSlicedInputStream() const {
     68   return std::unique_ptr<ZeroCopyInputStream>(
     69       new SlicedProtobufInputStream(&slices_));
     70 }
     71 
     72 }  // namespace perfetto
     73