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_(4096, 4096), stream_(&delegate_) {
29 delegate_.set_writer(&stream_);
30 cur_packet_.reset(new protozero::RootMessage<protos::pbzero::TracePacket>());
31 cur_packet_->Finalize(); // To avoid the DCHECK in NewTracePacket().
32 }
33
~TraceWriterForTesting()34 TraceWriterForTesting::~TraceWriterForTesting() {}
35
Flush(std::function<void ()> callback)36 void TraceWriterForTesting::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 std::vector<protos::gen::TracePacket>
GetAllTracePackets()45 TraceWriterForTesting::GetAllTracePackets() {
46 PERFETTO_CHECK(cur_packet_->is_finalized());
47
48 std::vector<uint8_t> buffer = delegate_.StitchSlices();
49 protozero::ProtoDecoder trace(buffer.data(), buffer.size());
50 std::vector<protos::gen::TracePacket> ret;
51 for (auto fld = trace.ReadField(); fld.valid(); fld = trace.ReadField()) {
52 PERFETTO_CHECK(fld.id() == protos::pbzero::Trace::kPacketFieldNumber);
53 protos::gen::TracePacket packet;
54 packet.ParseFromArray(fld.data(), fld.size());
55 ret.emplace_back(std::move(packet));
56 }
57 PERFETTO_CHECK(trace.bytes_left() == 0);
58 return ret;
59 }
60
GetOnlyTracePacket()61 protos::gen::TracePacket TraceWriterForTesting::GetOnlyTracePacket() {
62 auto packets = GetAllTracePackets();
63 PERFETTO_CHECK(packets.size() == 1);
64 return packets[0];
65 }
66
67 TraceWriterForTesting::TracePacketHandle
NewTracePacket()68 TraceWriterForTesting::NewTracePacket() {
69 // If we hit this, the caller is calling NewTracePacket() without having
70 // finalized the previous packet.
71 PERFETTO_DCHECK(cur_packet_->is_finalized());
72 cur_packet_->Reset(&stream_);
73
74 // Instead of storing the contents of the TracePacket directly in the backing
75 // buffer like the real trace writers, we prepend the proto preamble to make
76 // the buffer contents parsable as a sequence of TracePacket protos.
77
78 uint8_t data[protozero::proto_utils::kMaxTagEncodedSize];
79 uint8_t* data_end = protozero::proto_utils::WriteVarInt(
80 protozero::proto_utils::MakeTagLengthDelimited(
81 protos::pbzero::Trace::kPacketFieldNumber),
82 data);
83 stream_.WriteBytes(data, static_cast<uint32_t>(data_end - data));
84
85 auto packet = TraceWriter::TracePacketHandle(cur_packet_.get());
86 packet->set_size_field(
87 stream_.ReserveBytes(protozero::proto_utils::kMessageLengthFieldSize));
88 return packet;
89 }
90
writer_id() const91 WriterID TraceWriterForTesting::writer_id() const {
92 return 0;
93 }
94
written() const95 uint64_t TraceWriterForTesting::written() const {
96 return 0;
97 }
98
99 } // namespace perfetto
100