Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright (C) 2018 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 "src/tracing/core/null_trace_writer.h"
     18 
     19 #include "perfetto/base/logging.h"
     20 #include "perfetto/base/utils.h"
     21 
     22 #include "perfetto/protozero/message.h"
     23 
     24 #include "perfetto/trace/trace_packet.pbzero.h"
     25 
     26 namespace perfetto {
     27 
     28 NullTraceWriter::NullTraceWriter()
     29     : delegate_(base::kPageSize), stream_(&delegate_) {
     30   cur_packet_.reset(new protos::pbzero::TracePacket());
     31   cur_packet_->Finalize();  // To avoid the DCHECK in NewTracePacket().
     32 }
     33 
     34 NullTraceWriter::~NullTraceWriter() {}
     35 
     36 void NullTraceWriter::Flush(std::function<void()> callback) {
     37   // Flush() cannot be called in the middle of a TracePacket.
     38   PERFETTO_CHECK(cur_packet_->is_finalized());
     39 
     40   if (callback)
     41     callback();
     42 }
     43 
     44 NullTraceWriter::TracePacketHandle NullTraceWriter::NewTracePacket() {
     45   // If we hit this, the caller is calling NewTracePacket() without having
     46   // finalized the previous packet.
     47   PERFETTO_DCHECK(cur_packet_->is_finalized());
     48   cur_packet_->Reset(&stream_);
     49   return TraceWriter::TracePacketHandle(cur_packet_.get());
     50 }
     51 
     52 WriterID NullTraceWriter::writer_id() const {
     53   return 0;
     54 }
     55 
     56 }  // namespace perfetto
     57