Skip to content

Commit

Permalink
Merge "ftrace: Add filter for ftrace/print buf"
Browse files Browse the repository at this point in the history
  • Loading branch information
ddiproietto authored and Gerrit Code Review committed Oct 10, 2022
2 parents 5c42ac9 + 5c4b443 commit fec36b9
Show file tree
Hide file tree
Showing 15 changed files with 525 additions and 14 deletions.
2 changes: 2 additions & 0 deletions Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -9949,6 +9949,7 @@ filegroup {
"src/traced/probes/ftrace/ftrace_config_utils.cc",
"src/traced/probes/ftrace/ftrace_controller.cc",
"src/traced/probes/ftrace/ftrace_data_source.cc",
"src/traced/probes/ftrace/ftrace_print_filter.cc",
"src/traced/probes/ftrace/ftrace_stats.cc",
"src/traced/probes/ftrace/printk_formats_parser.cc",
"src/traced/probes/ftrace/proto_translation_table.cc",
Expand Down Expand Up @@ -10096,6 +10097,7 @@ filegroup {
"src/traced/probes/ftrace/ftrace_config_muxer_unittest.cc",
"src/traced/probes/ftrace/ftrace_config_unittest.cc",
"src/traced/probes/ftrace/ftrace_controller_unittest.cc",
"src/traced/probes/ftrace/ftrace_print_filter_unittest.cc",
"src/traced/probes/ftrace/ftrace_procfs_unittest.cc",
"src/traced/probes/ftrace/printk_formats_parser_unittest.cc",
"src/traced/probes/ftrace/proto_translation_table_unittest.cc",
Expand Down
2 changes: 2 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1995,6 +1995,8 @@ perfetto_filegroup(
"src/traced/probes/ftrace/ftrace_data_source.cc",
"src/traced/probes/ftrace/ftrace_data_source.h",
"src/traced/probes/ftrace/ftrace_metadata.h",
"src/traced/probes/ftrace/ftrace_print_filter.cc",
"src/traced/probes/ftrace/ftrace_print_filter.h",
"src/traced/probes/ftrace/ftrace_stats.cc",
"src/traced/probes/ftrace/ftrace_stats.h",
"src/traced/probes/ftrace/printk_formats_parser.cc",
Expand Down
20 changes: 19 additions & 1 deletion protos/perfetto/config/ftrace/ftrace_config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ syntax = "proto2";

package perfetto.protos;

// Next id: 22.
// Next id: 23.
message FtraceConfig {
repeated string ftrace_events = 1;
repeated string atrace_categories = 2;
Expand All @@ -37,6 +37,24 @@ message FtraceConfig {
}
optional CompactSchedConfig compact_sched = 12;

// Optional filter for "ftrace/print" events.
//
// The filter consists of multiple rules. A rule matches if its prefix matches
// exactly with the beginning of the "ftrace/print" "buf" field. As soon as a
// rule matches (the rules are processed in order), its `allow` field will be
// used as the outcome: if `allow` is true, the event will be included in the
// trace, otherwise it will be discarded. If an event does not match any rule,
// it will be allowed by default (a rule with an empty prefix and allow=false,
// disallows everything by default).
message PrintFilter {
message Rule {
optional string prefix = 1;
optional bool allow = 2;
}
repeated Rule rules = 1;
}
optional PrintFilter print_filter = 22;

// Enables symbol name resolution against /proc/kallsyms.
// It requires that either traced_probes is running as root or that
// kptr_restrict has been manually lowered.
Expand Down
20 changes: 19 additions & 1 deletion protos/perfetto/config/perfetto_config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ message ChromeConfig {

// Begin of protos/perfetto/config/ftrace/ftrace_config.proto

// Next id: 22.
// Next id: 23.
message FtraceConfig {
repeated string ftrace_events = 1;
repeated string atrace_categories = 2;
Expand All @@ -485,6 +485,24 @@ message FtraceConfig {
}
optional CompactSchedConfig compact_sched = 12;

// Optional filter for "ftrace/print" events.
//
// The filter consists of multiple rules. A rule matches if its prefix matches
// exactly with the beginning of the "ftrace/print" "buf" field. As soon as a
// rule matches (the rules are processed in order), its `allow` field will be
// used as the outcome: if `allow` is true, the event will be included in the
// trace, otherwise it will be discarded. If an event does not match any rule,
// it will be allowed by default (a rule with an empty prefix and allow=false,
// disallows everything by default).
message PrintFilter {
message Rule {
optional string prefix = 1;
optional bool allow = 2;
}
repeated Rule rules = 1;
}
optional PrintFilter print_filter = 22;

// Enables symbol name resolution against /proc/kallsyms.
// It requires that either traced_probes is running as root or that
// kptr_restrict has been manually lowered.
Expand Down
20 changes: 19 additions & 1 deletion protos/perfetto/trace/perfetto_trace.proto
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ message ChromeConfig {

// Begin of protos/perfetto/config/ftrace/ftrace_config.proto

// Next id: 22.
// Next id: 23.
message FtraceConfig {
repeated string ftrace_events = 1;
repeated string atrace_categories = 2;
Expand All @@ -485,6 +485,24 @@ message FtraceConfig {
}
optional CompactSchedConfig compact_sched = 12;

// Optional filter for "ftrace/print" events.
//
// The filter consists of multiple rules. A rule matches if its prefix matches
// exactly with the beginning of the "ftrace/print" "buf" field. As soon as a
// rule matches (the rules are processed in order), its `allow` field will be
// used as the outcome: if `allow` is true, the event will be included in the
// trace, otherwise it will be discarded. If an event does not match any rule,
// it will be allowed by default (a rule with an empty prefix and allow=false,
// disallows everything by default).
message PrintFilter {
message Rule {
optional string prefix = 1;
optional bool allow = 2;
}
repeated Rule rules = 1;
}
optional PrintFilter print_filter = 22;

// Enables symbol name resolution against /proc/kallsyms.
// It requires that either traced_probes is running as root or that
// kptr_restrict has been manually lowered.
Expand Down
9 changes: 6 additions & 3 deletions src/traced/probes/ftrace/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@ perfetto_unittest_source_set("unittests") {
sources = [
"cpu_reader_unittest.cc",
"cpu_stats_parser_unittest.cc",
"vendor_tracepoints_unittest.cc",
"event_info_unittest.cc",
"ftrace_config_muxer_unittest.cc",
"ftrace_config_unittest.cc",
"ftrace_controller_unittest.cc",
"ftrace_print_filter_unittest.cc",
"ftrace_procfs_unittest.cc",
"printk_formats_parser_unittest.cc",
"proto_translation_table_unittest.cc",
"vendor_tracepoints_unittest.cc",
]
}

Expand Down Expand Up @@ -137,8 +138,6 @@ source_set("ftrace") {
"cpu_reader.h",
"cpu_stats_parser.cc",
"cpu_stats_parser.h",
"vendor_tracepoints.cc",
"vendor_tracepoints.h",
"event_info.cc",
"event_info.h",
"event_info_constants.cc",
Expand All @@ -152,12 +151,16 @@ source_set("ftrace") {
"ftrace_data_source.cc",
"ftrace_data_source.h",
"ftrace_metadata.h",
"ftrace_print_filter.cc",
"ftrace_print_filter.h",
"ftrace_stats.cc",
"ftrace_stats.h",
"printk_formats_parser.cc",
"printk_formats_parser.h",
"proto_translation_table.cc",
"proto_translation_table.h",
"vendor_tracepoints.cc",
"vendor_tracepoints.h",
]
}

Expand Down
13 changes: 13 additions & 0 deletions src/traced/probes/ftrace/cpu_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "src/traced/probes/ftrace/ftrace_config_muxer.h"
#include "src/traced/probes/ftrace/ftrace_controller.h"
#include "src/traced/probes/ftrace/ftrace_data_source.h"
#include "src/traced/probes/ftrace/ftrace_print_filter.h"
#include "src/traced/probes/ftrace/proto_translation_table.h"

#include "protos/perfetto/trace/ftrace/ftrace_event.pbzero.h"
Expand Down Expand Up @@ -622,6 +623,9 @@ size_t CpuReader::ParsePagePayload(const uint8_t* start_of_payload,
const CompactSchedWakingFormat& sched_waking_format =
table->compact_sched_format().sched_waking;

bool ftrace_print_filter_enabled =
ds_config->print_filter.has_value();

// compact sched_switch
if (compact_sched_enabled &&
ftrace_event_id == sched_switch_format.event_id) {
Expand All @@ -640,6 +644,15 @@ size_t CpuReader::ParsePagePayload(const uint8_t* start_of_payload,
ParseSchedWakingCompact(start, timestamp, &sched_waking_format,
compact_sched_buffer, metadata);

} else if (ftrace_print_filter_enabled &&
ftrace_event_id == ds_config->print_filter->event_id()) {
if (ds_config->print_filter->IsEventInteresting(start, next)) {
protos::pbzero::FtraceEvent* event = bundle->add_event();
event->set_timestamp(timestamp);
if (!ParseEvent(ftrace_event_id, start, next, table, event,
metadata))
return 0;
}
} else {
// Common case: parse all other types of enabled events.
protos::pbzero::FtraceEvent* event = bundle->add_event();
Expand Down
25 changes: 23 additions & 2 deletions src/traced/probes/ftrace/cpu_reader_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "protos/perfetto/trace/ftrace/ftrace_event_bundle.pbzero.h"
#include "src/traced/probes/ftrace/cpu_reader.h"
#include "src/traced/probes/ftrace/ftrace_config_muxer.h"
#include "src/traced/probes/ftrace/ftrace_print_filter.h"
#include "src/traced/probes/ftrace/proto_translation_table.h"
#include "src/traced/probes/ftrace/test/cpu_reader_support.h"

Expand Down Expand Up @@ -559,6 +560,7 @@ ExamplePage g_full_page_print{

void DoParse(const ExamplePage& test_case,
const std::vector<GroupAndName>& enabled_events,
base::Optional<FtraceConfig::PrintFilter> print_filter,
benchmark::State& state) {
ScatteredStreamWriterNullDelegate delegate(base::kPageSize);
ScatteredStreamWriter stream(&delegate);
Expand All @@ -570,9 +572,15 @@ void DoParse(const ExamplePage& test_case,
FtraceDataSourceConfig ds_config{EventFilter{},
EventFilter{},
DisabledCompactSchedConfigForTesting(),
base::nullopt,
{},
{},
false /*symbolize_ksyms*/};
if (print_filter.has_value()) {
ds_config.print_filter =
FtracePrintFilterConfig::Create(print_filter.value(), table);
PERFETTO_CHECK(ds_config.print_filter.has_value());
}
for (const GroupAndName& enabled_event : enabled_events) {
ds_config.event_filter.AddEnabledEvent(
table->EventToFtraceId(enabled_event));
Expand Down Expand Up @@ -601,14 +609,27 @@ void DoParse(const ExamplePage& test_case,

void BM_ParsePageFullOfSchedSwitch(benchmark::State& state) {
DoParse(g_full_page_sched_switch, {GroupAndName("sched", "sched_switch")},
state);
base::nullopt, state);
}
BENCHMARK(BM_ParsePageFullOfSchedSwitch);

void BM_ParsePageFullOfPrint(benchmark::State& state) {
DoParse(g_full_page_print, {GroupAndName("ftrace", "print")}, state);
DoParse(g_full_page_print, {GroupAndName("ftrace", "print")}, base::nullopt,
state);
}
BENCHMARK(BM_ParsePageFullOfPrint);

void BM_ParsePageFullOfPrintWithFilterRules(benchmark::State& state) {
FtraceConfig::PrintFilter filter_conf;
for (int i = 0; i < state.range(0); i++) {
auto* rule = filter_conf.add_rules();
rule->set_prefix("X"); // This rule will not match
rule->set_allow(false);
}
DoParse(g_full_page_print, {GroupAndName("ftrace", "print")}, filter_conf,
state);
}
BENCHMARK(BM_ParsePageFullOfPrintWithFilterRules)->DenseRange(0, 16, 1);

} // namespace
} // namespace perfetto
1 change: 1 addition & 0 deletions src/traced/probes/ftrace/cpu_reader_fuzzer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ void FuzzCpuReaderProcessPagesForDataSource(const uint8_t* data, size_t size) {
FtraceDataSourceConfig ds_config{EventFilter{},
EventFilter{},
DisabledCompactSchedConfigForTesting(),
base::nullopt,
{},
{},
/*symbolize_ksyms=*/false};
Expand Down
87 changes: 84 additions & 3 deletions src/traced/probes/ftrace/cpu_reader_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ using testing::Return;
using testing::StartsWith;

namespace perfetto {

namespace {

FtraceDataSourceConfig EmptyConfig() {
return FtraceDataSourceConfig{EventFilter{},
EventFilter{},
DisabledCompactSchedConfigForTesting(),
base::nullopt,
{},
{},
false /*symbolize_ksyms*/};
Expand Down Expand Up @@ -186,8 +186,6 @@ class BinaryWriter {
uint8_t* ptr_;
};

} // namespace

TEST(PageFromXxdTest, OneLine) {
std::string text = R"(
00000000: 0000 0000 0000 0000 0000 0000 0000 0000 ................
Expand Down Expand Up @@ -756,6 +754,87 @@ TEST(CpuReaderTest, ParseThreePrint) {
}
}

TEST(CpuReaderTest, ParsePrintWithAndWithoutFilter) {
const ExamplePage* test_case = &g_three_prints;

ProtoTranslationTable* table = GetTable(test_case->name);
auto page = PageFromXxd(test_case->data);

FtraceMetadata metadata{};
std::unique_ptr<CompactSchedBuffer> compact_buffer(new CompactSchedBuffer());
const uint8_t* parse_pos = page.get();
base::Optional<CpuReader::PageHeader> page_header =
CpuReader::ParsePageHeader(&parse_pos, table->page_header_size_len());

const uint8_t* page_end = page.get() + base::kPageSize;
ASSERT_TRUE(page_header.has_value());
ASSERT_FALSE(page_header->lost_events);
ASSERT_LE(parse_pos + page_header->size, page_end);

{
FtraceDataSourceConfig ds_config_no_filter = EmptyConfig();
ds_config_no_filter.event_filter.AddEnabledEvent(
table->EventToFtraceId(GroupAndName("ftrace", "print")));

BundleProvider bundle_provider(base::kPageSize);
size_t evt_bytes = CpuReader::ParsePagePayload(
parse_pos, &page_header.value(), table, &ds_config_no_filter,
compact_buffer.get(), bundle_provider.writer(), &metadata);
ASSERT_GE(evt_bytes, 0u);

auto bundle = bundle_provider.ParseProto();
using ::testing::Pointee;
using ::testing::Property;
EXPECT_THAT(
bundle,
Pointee(Property(
&protos::gen::FtraceEventBundle::event,
ElementsAre(Property(&protos::gen::FtraceEvent::print,
Property(&protos::gen::PrintFtraceEvent::buf,
"Hello, world!\n")),
Property(&protos::gen::FtraceEvent::print,
Property(&protos::gen::PrintFtraceEvent::buf,
"Good afternoon, world!\n")),
Property(&protos::gen::FtraceEvent::print,
Property(&protos::gen::PrintFtraceEvent::buf,
"Goodbye, world!\n"))))));
}

{
FtraceDataSourceConfig ds_config_with_filter = EmptyConfig();
ds_config_with_filter.event_filter.AddEnabledEvent(
table->EventToFtraceId(GroupAndName("ftrace", "print")));

FtraceConfig::PrintFilter conf;
auto* rule = conf.add_rules();
rule->set_prefix("Good ");
rule->set_allow(false);
ds_config_with_filter.print_filter =
FtracePrintFilterConfig::Create(conf, table);
ASSERT_TRUE(ds_config_with_filter.print_filter.has_value());

BundleProvider bundle_provider(base::kPageSize);
size_t evt_bytes = CpuReader::ParsePagePayload(
parse_pos, &page_header.value(), table, &ds_config_with_filter,
compact_buffer.get(), bundle_provider.writer(), &metadata);
ASSERT_GE(evt_bytes, 0u);

auto bundle = bundle_provider.ParseProto();
using ::testing::Pointee;
using ::testing::Property;
EXPECT_THAT(
bundle,
Pointee(Property(
&protos::gen::FtraceEventBundle::event,
ElementsAre(Property(&protos::gen::FtraceEvent::print,
Property(&protos::gen::PrintFtraceEvent::buf,
"Hello, world!\n")),
Property(&protos::gen::FtraceEvent::print,
Property(&protos::gen::PrintFtraceEvent::buf,
"Goodbye, world!\n"))))));
}
}

// clang-format off
// # tracer: nop
// #
Expand Down Expand Up @@ -865,6 +944,7 @@ TEST(CpuReaderTest, ParseSixSchedSwitchCompactFormat) {
FtraceDataSourceConfig ds_config{EventFilter{},
EventFilter{},
EnabledCompactSchedConfigForTesting(),
base::nullopt,
{},
{},
false /* symbolize_ksyms*/};
Expand Down Expand Up @@ -2982,4 +3062,5 @@ TEST(CpuReaderTest, ZeroPaddedPageWorkaround) {
ASSERT_TRUE(bundle);
}

} // namespace
} // namespace perfetto
Loading

0 comments on commit fec36b9

Please sign in to comment.