• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/trace_writer_for_testing.h"
18 
19 #include "perfetto/base/logging.h"
20 #include "perfetto/ext/base/utils.h"
21 #include "perfetto/protozero/message.h"
22 #include "protos/perfetto/trace/trace.pbzero.h"
23 #include "protos/perfetto/trace/trace_packet.pbzero.h"
24 
25 namespace perfetto {
26 
TraceWriterForTesting()27 TraceWriterForTesting::TraceWriterForTesting()
28     : delegate_(static_cast<size_t>(base::kPageSize),
29                 static_cast<size_t>(base::kPageSize)),
30       stream_(&delegate_) {
31   delegate_.set_writer(&stream_);
32   cur_packet_.reset(new protos::pbzero::TracePacket());
33   cur_packet_->Finalize();  // To avoid the DCHECK in NewTracePacket().
34 }
35 
~TraceWriterForTesting()36 TraceWriterForTesting::~TraceWriterForTesting() {}
37 
Flush(std::function<void ()> callback)38 void TraceWriterForTesting::Flush(std::function<void()> callback) {
39   // Flush() cannot be called in the middle of a TracePacket.
40   PERFETTO_CHECK(cur_packet_->is_finalized());
41 
42   if (callback)
43     callback();
44 }
45 
46 std::vector<protos::gen::TracePacket>
GetAllTracePackets()47 TraceWriterForTesting::GetAllTracePackets() {
48   PERFETTO_CHECK(cur_packet_->is_finalized());
49 
50   std::vector<uint8_t> buffer = delegate_.StitchSlices();
51   protozero::ProtoDecoder trace(buffer.data(), buffer.size());
52   std::vector<protos::gen::TracePacket> ret;
53   for (auto fld = trace.ReadField(); fld.valid(); fld = trace.ReadField()) {
54     PERFETTO_CHECK(fld.id() == protos::pbzero::Trace::kPacketFieldNumber);
55     protos::gen::TracePacket packet;
56     packet.ParseFromArray(fld.data(), fld.size());
57     ret.emplace_back(std::move(packet));
58   }
59   PERFETTO_CHECK(trace.bytes_left() == 0);
60   return ret;
61 }
62 
GetOnlyTracePacket()63 protos::gen::TracePacket TraceWriterForTesting::GetOnlyTracePacket() {
64   auto packets = GetAllTracePackets();
65   PERFETTO_CHECK(packets.size() == 1);
66   return packets[0];
67 }
68 
69 TraceWriterForTesting::TracePacketHandle
NewTracePacket()70 TraceWriterForTesting::NewTracePacket() {
71   // If we hit this, the caller is calling NewTracePacket() without having
72   // finalized the previous packet.
73   PERFETTO_DCHECK(cur_packet_->is_finalized());
74   cur_packet_->Reset(&stream_);
75 
76   // Instead of storing the contents of the TracePacket directly in the backing
77   // buffer like the real trace writers, we prepend the proto preamble to make
78   // the buffer contents parsable as a sequence of TracePacket protos.
79 
80   uint8_t data[protozero::proto_utils::kMaxTagEncodedSize];
81   uint8_t* data_end = protozero::proto_utils::WriteVarInt(
82       protozero::proto_utils::MakeTagLengthDelimited(
83           protos::pbzero::Trace::kPacketFieldNumber),
84       data);
85   stream_.WriteBytes(data, static_cast<uint32_t>(data_end - data));
86 
87   auto packet = TraceWriter::TracePacketHandle(cur_packet_.get());
88   packet->set_size_field(
89       stream_.ReserveBytes(protozero::proto_utils::kMessageLengthFieldSize));
90   return packet;
91 }
92 
writer_id() const93 WriterID TraceWriterForTesting::writer_id() const {
94   return 0;
95 }
96 
written() const97 uint64_t TraceWriterForTesting::written() const {
98   return 0;
99 }
100 
101 }  // namespace perfetto
102