From 6de79a03558b2911d2766792b0650db888bc3ee7 Mon Sep 17 00:00:00 2001 From: Yabin Cui Date: Fri, 3 Nov 2023 16:36:56 -0700 Subject: simpleperf: Add --tp-filter option in the stat cmd It is to filter records based on tracepoint fields. Bug: 160630060 Test: run simpleperf_unit_test Change-Id: Ife803e7f3c07e1667e44f6b79baaaa5336578c97 --- simpleperf/cmd_record.cpp | 3 ++- simpleperf/cmd_stat.cpp | 10 ++++++++++ simpleperf/cmd_stat_impl.h | 1 + simpleperf/cmd_stat_test.cpp | 7 +++++++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/simpleperf/cmd_record.cpp b/simpleperf/cmd_record.cpp index 14d157b8..97f96ac7 100644 --- a/simpleperf/cmd_record.cpp +++ b/simpleperf/cmd_record.cpp @@ -1252,7 +1252,8 @@ bool RecordCommand::ParseOptions(const std::vector& args, return false; } } else { - CHECK(false) << "unprocessed option: " << name; + LOG(ERROR) << "unprocessed option: " << name; + return false; } } diff --git a/simpleperf/cmd_stat.cpp b/simpleperf/cmd_stat.cpp index d4bcbd0a..a23fec1f 100644 --- a/simpleperf/cmd_stat.cpp +++ b/simpleperf/cmd_stat.cpp @@ -402,6 +402,9 @@ class StatCommand : public Command { " Stat events on existing processes. Processes are searched either by pid\n" " or process name regex. Mutually exclusive with -a.\n" "-t tid1,tid2,... Stat events on existing threads. Mutually exclusive with -a.\n" +"--tp-filter filter_string Set filter_string for the previous tracepoint event.\n" +" Format is in Documentation/trace/events.rst in the kernel.\n" +" An example: 'prev_comm != \"simpleperf\" && (prev_pid > 1)'.\n" "--print-hw-counter Test and print CPU PMU hardware counters available on the device.\n" "--sort key1,key2,... Select keys used to sort the report, used when --per-thread\n" " or --per-core appears. The appearance order of keys decides\n" @@ -763,6 +766,13 @@ bool StatCommand::ParseOptions(const std::vector& args, if (!event_selection_set_.AddEventGroup(event_types)) { return false; } + } else if (name == "--tp-filter") { + if (!event_selection_set_.SetTracepointFilter(*value.str_value)) { + return false; + } + } else { + LOG(ERROR) << "unprocessed option: " << name; + return false; } } diff --git a/simpleperf/cmd_stat_impl.h b/simpleperf/cmd_stat_impl.h index d337fd5a..a7397477 100644 --- a/simpleperf/cmd_stat_impl.h +++ b/simpleperf/cmd_stat_impl.h @@ -327,6 +327,7 @@ inline const OptionFormatMap& GetStatCmdOptionFormats() { {"--sort", {OptionValueType::STRING, OptionType::SINGLE, AppRunnerType::ALLOWED}}, {"--stop-signal-fd", {OptionValueType::UINT, OptionType::SINGLE, AppRunnerType::CHECK_FD}}, {"-t", {OptionValueType::STRING, OptionType::MULTIPLE, AppRunnerType::ALLOWED}}, + {"--tp-filter", {OptionValueType::STRING, OptionType::ORDERED, AppRunnerType::ALLOWED}}, {"--tracepoint-events", {OptionValueType::STRING, OptionType::SINGLE, AppRunnerType::CHECK_PATH}}, {"--use-devfreq-counters", diff --git a/simpleperf/cmd_stat_test.cpp b/simpleperf/cmd_stat_test.cpp index e6701992..86c7ae8c 100644 --- a/simpleperf/cmd_stat_test.cpp +++ b/simpleperf/cmd_stat_test.cpp @@ -427,6 +427,13 @@ TEST(stat_cmd, kprobe_option) { ASSERT_TRUE(StatCmd()->Run({"--group", "kprobes:do_sys_openat2", "-a", "--duration", SLEEP_SEC})); } +TEST(stat_cmd, tp_filter_option) { + TEST_REQUIRE_HOST_ROOT(); + TEST_REQUIRE_TRACEPOINT_EVENTS(); + ASSERT_TRUE(StatCmd()->Run( + {"-e", "sched:sched_switch", "--tp-filter", "prev_comm != sleep", "sleep", SLEEP_SEC})); +} + class StatCmdSummaryBuilderTest : public ::testing::Test { protected: struct CounterArg { -- cgit v1.2.3