|
15 | 15 | #include <benchmark/benchmark.h> |
16 | 16 |
|
17 | 17 | #include <algorithm> |
| 18 | +#include <cstring> |
18 | 19 | #include <string> |
| 20 | +#include <vector> |
19 | 21 |
|
| 22 | +#include "perfetto/base/compiler.h" |
20 | 23 | #include "perfetto/ext/base/file_utils.h" |
21 | 24 | #include "perfetto/ext/protozero/proto_ring_buffer.h" |
| 25 | +#include "perfetto/protozero/proto_utils.h" |
22 | 26 | #include "src/base/test/utils.h" |
23 | 27 |
|
| 28 | +namespace { |
| 29 | + |
| 30 | +std::string LoadTestTrace() { |
| 31 | + std::string trace_data; |
| 32 | + static const char kTestTrace[] = "test/data/example_android_trace_30s.pb"; |
| 33 | + perfetto::base::ReadFile(perfetto::base::GetTestDataPath(kTestTrace), |
| 34 | + &trace_data); |
| 35 | + PERFETTO_CHECK(!trace_data.empty()); |
| 36 | + return trace_data; |
| 37 | +} |
| 38 | + |
| 39 | +// Stands in for the read(2)/recv(2) that brings bytes in from the outside |
| 40 | +// world: like those, it fills a buffer chosen by the caller. That is the one |
| 41 | +// copy no scheme avoids; the benchmarks below differ only in whether the buffer |
| 42 | +// belongs to the transport or to the ring buffer. |
| 43 | +PERFETTO_NO_INLINE void ProduceBytes(void* dst, const void* src, size_t size) { |
| 44 | + memcpy(dst, src, size); |
| 45 | +} |
| 46 | + |
| 47 | +// Ingestion as the transports used to do it: read into a staging buffer of the |
| 48 | +// transport's own, then hand those bytes to Append(), which copies them again. |
| 49 | +void BM_ProtoRingBufferIngestStaged(benchmark::State& state) { |
| 50 | + const std::string trace_data = LoadTestTrace(); |
| 51 | + const auto read_size = static_cast<size_t>(state.range(0)); |
| 52 | + std::vector<uint8_t> staging(read_size); |
| 53 | + |
| 54 | + protozero::ProtoRingBuffer buffer; |
| 55 | + size_t offset = 0, bytes_ingested = 0, total_packet_size = 0; |
| 56 | + for (auto _ : state) { |
| 57 | + size_t n = std::min(read_size, trace_data.size() - offset); |
| 58 | + ProduceBytes(staging.data(), trace_data.data() + offset, n); |
| 59 | + buffer.Append(staging.data(), n); |
| 60 | + // The trace holds whole messages only, so by the time we get back to the |
| 61 | + // start the buffer has drained and the wrap is seamless. |
| 62 | + offset = (offset + n) % trace_data.size(); |
| 63 | + bytes_ingested += n; |
| 64 | + for (;;) { |
| 65 | + auto msg = buffer.ReadMessage(); |
| 66 | + if (!msg.valid()) |
| 67 | + break; |
| 68 | + total_packet_size += msg.len; |
| 69 | + } |
| 70 | + } |
| 71 | + benchmark::DoNotOptimize(total_packet_size); |
| 72 | + state.SetBytesProcessed(static_cast<int64_t>(bytes_ingested)); |
| 73 | +} |
| 74 | + |
| 75 | +// Ingestion as they do it now: read straight into the ring buffer, so the bytes |
| 76 | +// are copied exactly once. |
| 77 | +void BM_ProtoRingBufferIngestZeroCopy(benchmark::State& state) { |
| 78 | + const std::string trace_data = LoadTestTrace(); |
| 79 | + const auto read_size = static_cast<size_t>(state.range(0)); |
| 80 | + |
| 81 | + protozero::ProtoRingBuffer buffer; |
| 82 | + size_t offset = 0, bytes_ingested = 0, total_packet_size = 0; |
| 83 | + for (auto _ : state) { |
| 84 | + size_t n = std::min(read_size, trace_data.size() - offset); |
| 85 | + ProduceBytes(buffer.BeginWrite(n), trace_data.data() + offset, n); |
| 86 | + buffer.EndWrite(n); |
| 87 | + offset = (offset + n) % trace_data.size(); |
| 88 | + bytes_ingested += n; |
| 89 | + for (;;) { |
| 90 | + auto msg = buffer.ReadMessage(); |
| 91 | + if (!msg.valid()) |
| 92 | + break; |
| 93 | + total_packet_size += msg.len; |
| 94 | + } |
| 95 | + } |
| 96 | + benchmark::DoNotOptimize(total_packet_size); |
| 97 | + state.SetBytesProcessed(static_cast<int64_t>(bytes_ingested)); |
| 98 | +} |
| 99 | + |
| 100 | +// unixd used to tokenize each message out of a per-connection ProtoRingBuffer, |
| 101 | +// re-serialize its TraceProcessorRpcStream preamble, and push preamble + |
| 102 | +// payload through a second ProtoRingBuffer inside Rpc, so every message was |
| 103 | +// copied and tokenized twice. It now reads straight into Rpc's tokenizer. |
| 104 | +void RunDispatch(benchmark::State& state, bool reframe) { |
| 105 | + namespace pu = protozero::proto_utils; |
| 106 | + const std::string trace_data = LoadTestTrace(); |
| 107 | + constexpr size_t kReadSize = 4096; |
| 108 | + |
| 109 | + protozero::ProtoRingBuffer conn_buf, rpc_buf; |
| 110 | + size_t offset = 0, bytes_ingested = 0, total_packet_size = 0; |
| 111 | + for (auto _ : state) { |
| 112 | + size_t n = std::min(kReadSize, trace_data.size() - offset); |
| 113 | + ProduceBytes(conn_buf.BeginWrite(n), trace_data.data() + offset, n); |
| 114 | + conn_buf.EndWrite(n); |
| 115 | + offset = (offset + n) % trace_data.size(); |
| 116 | + bytes_ingested += n; |
| 117 | + for (;;) { |
| 118 | + auto msg = conn_buf.ReadMessage(); |
| 119 | + if (!msg.valid()) |
| 120 | + break; |
| 121 | + if (!reframe) { |
| 122 | + total_packet_size += msg.len; |
| 123 | + continue; |
| 124 | + } |
| 125 | + uint8_t preamble[16]; |
| 126 | + uint8_t* end = preamble; |
| 127 | + end = pu::WriteVarInt(pu::MakeTagLengthDelimited(1), end); |
| 128 | + end = pu::WriteVarInt(msg.len, end); |
| 129 | + rpc_buf.Append(preamble, static_cast<size_t>(end - preamble)); |
| 130 | + rpc_buf.Append(msg.start, msg.len); |
| 131 | + for (;;) { |
| 132 | + auto inner = rpc_buf.ReadMessage(); |
| 133 | + if (!inner.valid()) |
| 134 | + break; |
| 135 | + total_packet_size += inner.len; |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + benchmark::DoNotOptimize(total_packet_size); |
| 140 | + state.SetBytesProcessed(static_cast<int64_t>(bytes_ingested)); |
| 141 | +} |
| 142 | + |
| 143 | +void BM_ProtoRingBufferDispatchReframed(benchmark::State& state) { |
| 144 | + RunDispatch(state, /*reframe=*/true); |
| 145 | +} |
| 146 | + |
| 147 | +void BM_ProtoRingBufferDispatchDirect(benchmark::State& state) { |
| 148 | + RunDispatch(state, /*reframe=*/false); |
| 149 | +} |
| 150 | + |
| 151 | +} // namespace |
| 152 | + |
| 153 | +// 4096 is what the socket transports read; 1MB is what traceconv reads. |
| 154 | +BENCHMARK(BM_ProtoRingBufferIngestStaged)->Arg(4096)->Arg(1024 * 1024); |
| 155 | +BENCHMARK(BM_ProtoRingBufferIngestZeroCopy)->Arg(4096)->Arg(1024 * 1024); |
| 156 | +BENCHMARK(BM_ProtoRingBufferDispatchReframed); |
| 157 | +BENCHMARK(BM_ProtoRingBufferDispatchDirect); |
| 158 | + |
24 | 159 | static void BM_ProtoRingBufferReadLargeChunks(benchmark::State& state) { |
25 | 160 | std::string trace_data; |
26 | 161 | static const char kTestTrace[] = "test/data/example_android_trace_30s.pb"; |
|
0 commit comments