xref: /aosp_15_r20/external/perfetto/src/tracing/service/packet_stream_validator_benchmark.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 // Copyright (C) 2018 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <benchmark/benchmark.h>
16 
17 #include "src/tracing/service/packet_stream_validator.h"
18 
19 #include "perfetto/ext/tracing/core/slice.h"
20 #include "perfetto/protozero/scattered_heap_buffer.h"
21 
22 #include "protos/perfetto/trace/ftrace/ftrace_event.pbzero.h"
23 #include "protos/perfetto/trace/ftrace/ftrace_event_bundle.pbzero.h"
24 #include "protos/perfetto/trace/ftrace/sched.pbzero.h"
25 #include "protos/perfetto/trace/test_event.pbzero.h"
26 #include "protos/perfetto/trace/trace_packet.pbzero.h"
27 
28 namespace {
29 
BM_PacketStreamValidator(benchmark::State & state)30 static void BM_PacketStreamValidator(benchmark::State& state) {
31   using namespace perfetto;
32 
33   // Create a packet that resembles a ftrace sched bundle. A ftrace page is
34   // 4KB and typically contains ~64 sched events of 64 bytes each.
35   protozero::HeapBuffered<protos::pbzero::TracePacket> packet;
36   auto* bundle = packet->set_ftrace_events();
37   bundle->set_cpu(1);
38   for (size_t events = 0; events < 64; events++) {
39     auto* ftrace_evt = bundle->add_event();
40     ftrace_evt->set_pid(12345);
41     ftrace_evt->set_timestamp(1000ull * 1000 * 1000 * 3600 * 24 * 365);
42     auto* sched_switch = ftrace_evt->set_sched_switch();
43     sched_switch->set_prev_comm("thread_name_1");
44     sched_switch->set_prev_pid(12345);
45     sched_switch->set_prev_state(42);
46     sched_switch->set_next_comm("thread_name_2");
47     sched_switch->set_next_pid(67890);
48   }
49   std::vector<uint8_t> buf = packet.SerializeAsArray();
50 
51   // Append 10 packets like the one above, splitting each packet into slices
52   // of 512B each.
53   Slices slices;
54   static constexpr size_t kSliceSize = 512;
55   for (size_t num_packets = 0; num_packets < 10; num_packets++) {
56     for (size_t pos = 0; pos < buf.size(); pos += kSliceSize) {
57       size_t slice_size = std::min(kSliceSize, buf.size() - pos);
58       Slice slice = Slice::Allocate(slice_size);
59       memcpy(slice.own_data(), &buf[pos], slice_size);
60       slices.emplace_back(std::move(slice));
61     }
62   }
63 
64   bool res = true;
65   while (state.KeepRunning()) {
66     res &= PacketStreamValidator::Validate(slices);
67   }
68   PERFETTO_CHECK(res);
69 }
70 
71 }  // namespace
72 
73 BENCHMARK(BM_PacketStreamValidator);
74