From: Jiri Olsa <jolsa@redhat.com>
To: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>,
Ian Rogers <irogers@google.com>,
linux-perf-users@vger.kernel.org
Subject: [PATCH 58/59] libperf: Add libperf_parse_events function
Date: Mon, 8 Nov 2021 14:37:09 +0100 [thread overview]
Message-ID: <20211108133710.1352822-59-jolsa@kernel.org> (raw)
In-Reply-To: <20211108133710.1352822-1-jolsa@kernel.org>
Adding libperf_parse_events function.
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
tools/lib/perf/include/perf/evlist.h | 2 +
tools/lib/perf/libperf.map | 1 +
tools/lib/perf/parse-events.c | 171 +++++++++++++++++++++++++++
3 files changed, 174 insertions(+)
diff --git a/tools/lib/perf/include/perf/evlist.h b/tools/lib/perf/include/perf/evlist.h
index 9ca399d49bb4..360443dd4135 100644
--- a/tools/lib/perf/include/perf/evlist.h
+++ b/tools/lib/perf/include/perf/evlist.h
@@ -47,4 +47,6 @@ LIBPERF_API struct perf_mmap *perf_evlist__next_mmap(struct perf_evlist *evlist,
(pos) = perf_evlist__next_mmap((evlist), (pos), overwrite))
LIBPERF_API void perf_evlist__set_leader(struct perf_evlist *evlist);
+
+LIBPERF_API int libperf_parse_events(struct perf_evlist *evlist, const char *str);
#endif /* __LIBPERF_EVLIST_H */
diff --git a/tools/lib/perf/libperf.map b/tools/lib/perf/libperf.map
index 71468606e8a7..8971431071f2 100644
--- a/tools/lib/perf/libperf.map
+++ b/tools/lib/perf/libperf.map
@@ -50,6 +50,7 @@ LIBPERF_0.0.1 {
perf_mmap__read_init;
perf_mmap__read_done;
perf_mmap__read_event;
+ libperf_parse_events;
local:
*;
};
diff --git a/tools/lib/perf/parse-events.c b/tools/lib/perf/parse-events.c
index 1c1fbbeb7182..d165c33fed83 100644
--- a/tools/lib/perf/parse-events.c
+++ b/tools/lib/perf/parse-events.c
@@ -10,6 +10,8 @@
#include <internal/evlist.h>
#include <internal/evsel.h>
#include <perf/cpumap.h>
+#include <perf/evlist.h>
+#include <perf/evsel.h>
#include <stdlib.h>
#include <errno.h>
#include <asm/bug.h>
@@ -702,3 +704,172 @@ int parse_events_add_breakpoint(struct parse_events_state *parse_state,
/*pmu=*/NULL, /*config_terms=*/NULL,
/*auto_merge_stats=*/false, /*cpu_list=*/NULL) ? 0 : -ENOENT;
}
+
+static struct perf_evsel*
+perf_evsel__new_idx(struct perf_event_attr *attr, int idx, bool init_attr __maybe_unused)
+{
+ struct perf_evsel *evsel = perf_evsel__new(attr);
+
+ if (evsel)
+ perf_evsel__init(evsel, attr, idx);
+ return evsel;
+}
+
+static struct perf_evsel*
+perf_evsel__newtp_idx(const char *sys __maybe_unused, const char *name __maybe_unused,
+ int idx __maybe_unused)
+{
+ return NULL;
+}
+
+static void perf_evsel__delete_helper(struct perf_evsel *evsel)
+{
+ perf_evsel__delete(evsel);
+}
+
+static
+int parse_events_add_pmu(struct parse_events_state *parse_state __maybe_unused,
+ struct list_head *list __maybe_unused,
+ char *pmu_name __maybe_unused,
+ struct list_head *head_config __maybe_unused,
+ struct list_head *orig_terms __maybe_unused,
+ bool auto_merge_stats __maybe_unused,
+ bool use_alias __maybe_unused)
+{
+ return -ENOTSUP;
+}
+
+static int
+parse_events_multi_pmu_add(struct parse_events_state *parse_state __maybe_unused,
+ char *str __maybe_unused,
+ struct list_head *head __maybe_unused,
+ struct list_head **listp __maybe_unused)
+{
+ return -ENOTSUP;
+}
+
+static int
+parse_events_add_numeric(struct parse_events_state *parse_state __maybe_unused,
+ struct list_head *list __maybe_unused,
+ u32 type __maybe_unused, u64 config __maybe_unused,
+ struct list_head *head_config __maybe_unused)
+{
+ return -ENOTSUP;
+}
+
+static int
+parse_events_add_cache(struct parse_events_state *parse_state __maybe_unused,
+ struct list_head *list __maybe_unused,
+ char *type __maybe_unused, char *op_result1 __maybe_unused,
+ char *op_result2 __maybe_unused,
+ struct parse_events_error *err __maybe_unused,
+ struct list_head *head_config __maybe_unused)
+{
+ return -ENOTSUP;
+}
+
+static int
+parse_events_add_tracepoint(struct parse_events_state *parse_state __maybe_unused,
+ struct list_head *list __maybe_unused,
+ const char *sys __maybe_unused, const char *event __maybe_unused,
+ struct parse_events_error *err __maybe_unused,
+ struct list_head *head_config __maybe_unused)
+{
+ return -ENOTSUP;
+}
+
+static int
+parse_events_load_bpf(struct parse_events_state *parse_state __maybe_unused,
+ struct list_head *list __maybe_unused,
+ char *bpf_file_name __maybe_unused,
+ bool source __maybe_unused,
+ struct list_head *head_config __maybe_unused)
+{
+ return -ENOTSUP;
+}
+
+static void
+parse_events_set_leader(char *name, struct list_head *list,
+ struct parse_events_state *parse_state)
+{
+ struct perf_evsel *leader;
+
+ if (list_empty(list))
+ return;
+
+ __perf_evlist__set_leader(list);
+ leader = list_entry(list->next, struct perf_evsel, node);
+ leader->group_name = name ? strdup(name) : NULL;
+}
+
+static enum perf_pmu_event_symbol_type
+perf_pmu__parse_check(const char *name __maybe_unused)
+{
+ return PMU_EVENT_SYMBOL_ERR;
+}
+
+static struct parse_events_ops parse_state_ops = {
+ .perf_evsel__new = perf_evsel__new_idx,
+ .perf_evsel__new_tp = perf_evsel__newtp_idx,
+ .perf_evsel__delete = perf_evsel__delete_helper,
+ .add_pmu = parse_events_add_pmu,
+ .add_pmu_multi = parse_events_multi_pmu_add,
+ .add_numeric = parse_events_add_numeric,
+ .add_cache = parse_events_add_cache,
+ .add_breakpoint = parse_events_add_breakpoint,
+ .add_tracepoint = parse_events_add_tracepoint,
+ .add_bpf = parse_events_load_bpf,
+ .set_leader = parse_events_set_leader,
+ .parse_check = perf_pmu__parse_check,
+};
+
+static bool perf_evsel__has_leader(struct perf_evsel *evsel, struct perf_evsel *leader)
+{
+ return evsel->leader == leader;
+}
+
+static void perf_evlist__splice_list_tail(struct perf_evlist *evlist, struct list_head *list)
+{
+ while (!list_empty(list)) {
+ struct perf_evsel *evsel, *temp, *leader = NULL;
+
+ __perf_evlist__for_each_entry_safe(list, temp, evsel) {
+ list_del_init(&evsel->node);
+ perf_evlist__add(evlist, evsel);
+ leader = evsel;
+ break;
+ }
+
+ __perf_evlist__for_each_entry_safe(list, temp, evsel) {
+ if (perf_evsel__has_leader(evsel, leader)) {
+ list_del_init(&evsel->node);
+ perf_evlist__add(evlist, evsel);
+ }
+ }
+ }
+}
+
+int libperf_parse_events(struct perf_evlist *evlist, const char *str)
+{
+ struct parse_events_state parse_state = {
+ .list = LIST_HEAD_INIT(parse_state.list),
+ .idx = evlist->nr_entries,
+ .evlist = evlist,
+ .ops = &parse_state_ops,
+ };
+ int err;
+
+ err = parse_events__scanner(str, &parse_state, false);
+
+ if (!err && list_empty(&parse_state.list)) {
+ WARN_ONCE(true, "WARNING: event parser found nothing\n");
+ return -1;
+ }
+
+ perf_evlist__splice_list_tail(evlist, &parse_state.list);
+
+ if (!err)
+ evlist->nr_groups += parse_state.nr_groups;
+
+ return err;
+}
--
2.31.1
next prev parent reply other threads:[~2021-11-08 13:43 UTC|newest]
Thread overview: 78+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-11-08 13:36 [RFC 00/59] libperf: Move in event parse code Jiri Olsa
2021-11-08 13:36 ` [PATCH 01/59] libperf: Move pmu-events.h file to libperf Jiri Olsa
2021-11-08 13:36 ` [PATCH 03/59] libperf: Move pmu-events build " Jiri Olsa
2021-11-08 13:36 ` [PATCH 04/59] libperf: Move perf_pmu__format_parse " Jiri Olsa
2021-11-08 13:36 ` [PATCH 05/59] tools api fs: Move in the fncache from perf Jiri Olsa
2021-11-08 17:46 ` Ian Rogers
2021-11-08 21:15 ` Jiri Olsa
2021-11-08 13:36 ` [PATCH 06/59] libperf: Move in the pmu hybrid support Jiri Olsa
2021-11-08 13:36 ` [PATCH 07/59] libperf: Move name to perf_evsel Jiri Olsa
2021-11-08 13:36 ` [PATCH 08/59] libperf: Move auto_merge_stats " Jiri Olsa
2021-11-08 13:36 ` [PATCH 09/59] libperf: Move config_terms " Jiri Olsa
2021-11-08 13:36 ` [PATCH 10/59] libperf: Move metric_id " Jiri Olsa
2021-11-08 13:36 ` [PATCH 11/59] libperf: Move tool_event " Jiri Olsa
2021-11-08 13:36 ` [PATCH 12/59] libperf: Move unit " Jiri Olsa
2021-11-08 13:36 ` [PATCH 13/59] libperf: Move exclude_GH " Jiri Olsa
2021-11-08 17:53 ` Ian Rogers
2021-11-08 21:16 ` Jiri Olsa
2021-11-08 13:36 ` [PATCH 14/59] libperf: Move sample_read " Jiri Olsa
2021-11-08 13:36 ` [PATCH 15/59] libperf: Move precise_max " Jiri Olsa
2021-11-08 13:36 ` [PATCH 16/59] libperf: Move weak_group " Jiri Olsa
2021-11-08 13:36 ` [PATCH 17/59] libperf: Move bpf_counter " Jiri Olsa
2021-11-08 13:36 ` [PATCH 18/59] libperf: Move group_name " Jiri Olsa
2021-11-08 17:58 ` Ian Rogers
2021-11-08 18:07 ` Arnaldo Carvalho de Melo
2021-11-08 21:19 ` Jiri Olsa
2021-11-08 13:36 ` [PATCH 19/59] perf tools: Fix parse_events_term__num call Jiri Olsa
2021-11-08 18:15 ` Ian Rogers
2021-11-08 21:21 ` Jiri Olsa
2021-11-08 13:36 ` [PATCH 20/59] perf tools: Pass parse_state all the way down to __add_event Jiri Olsa
2021-11-08 13:36 ` [PATCH 21/59] perf tools: Pass parse_state all the way down to add_tracepoint Jiri Olsa
2021-11-08 13:36 ` [PATCH 22/59] perf tools: Add evsel__new callback to parse_state_ops Jiri Olsa
2021-11-08 13:36 ` [PATCH 23/59] perf tools: Add evsel__new_tp " Jiri Olsa
2021-11-08 13:36 ` [PATCH 24/59] perf tools: Add loc_term and loc_val helpers to parse_events_term__str Jiri Olsa
2021-11-08 13:36 ` [PATCH 25/59] perf tools: Add loc_term and loc_val helpers to parse_events_term__num Jiri Olsa
2021-11-08 13:36 ` [PATCH 26/59] libperf: Move in the event_symbols_hw/event_symbols_sw Jiri Olsa
2021-11-08 13:36 ` [PATCH 27/59] libperf: Move in struct parse_events_term code Jiri Olsa
2021-11-08 13:36 ` [PATCH 28/59] perf tools: Add perf_evsel__add_event function Jiri Olsa
2021-11-08 13:36 ` [PATCH 29/59] perf tools: Change struct parse_events_state::evlist to perf_evlist Jiri Olsa
2021-11-08 13:36 ` [PATCH 30/59] libperf: Move in struct parse_events_state Jiri Olsa
2021-11-08 18:21 ` Ian Rogers
2021-11-08 21:24 ` Jiri Olsa
2021-11-08 13:36 ` [PATCH 31/59] perf tools: Move event_attr_init in evsel__new_idx function Jiri Olsa
2021-11-08 13:36 ` [PATCH 32/59] libperf: Move in perf_pmu__warn_invalid_config function Jiri Olsa
2021-11-08 13:36 ` [PATCH 33/59] libperf: Move in perf_evsel__add_event function Jiri Olsa
2021-11-08 13:36 ` [PATCH 34/59] perf tools: Move parse_events_update_lists to parser unit Jiri Olsa
2021-11-08 13:36 ` [PATCH 35/59] libperf: Add perf_evsel__is_group_leader function Jiri Olsa
2021-11-08 13:36 ` [PATCH 36/59] perf tools: Make parse_events__modifier_event work over perf_evsel Jiri Olsa
2021-11-08 13:36 ` [PATCH 37/59] perf tool: Pass perf_guest in struct parse_events_state Jiri Olsa
2021-11-08 13:36 ` [PATCH 38/59] libperf: Move in parse_events__modifier_group/event functions Jiri Olsa
2021-11-08 13:36 ` [PATCH 39/59] libperf: Move in parse_events__handle_error function Jiri Olsa
2021-11-08 13:36 ` [PATCH 40/59] libperf: Move in parse_events_evlist_error function Jiri Olsa
2021-11-08 13:36 ` [PATCH 41/59] perf tools: Add perf_evsel__delete callback to struct parse_events_ops Jiri Olsa
2021-11-08 13:36 ` [PATCH 42/59] libperf: Move in parse_events_name function Jiri Olsa
2021-11-08 18:23 ` Ian Rogers
2021-11-08 21:24 ` Jiri Olsa
2021-11-08 13:36 ` [PATCH 43/59] perf tools: Move out parse_events_add_pmu fallback from parser code Jiri Olsa
2021-11-08 13:36 ` [PATCH 44/59] perf tools: Add add_pmu callback to struct parse_events_ops Jiri Olsa
2021-11-08 13:36 ` [PATCH 45/59] perf tools: Add add_pmu_multi " Jiri Olsa
2021-11-08 13:36 ` [PATCH 46/59] perf tools: Add add_numeric " Jiri Olsa
2021-11-08 18:27 ` Ian Rogers
2021-11-08 21:34 ` Jiri Olsa
2021-11-08 13:36 ` [PATCH 47/59] perf tools: Add add_cache " Jiri Olsa
2021-11-08 13:36 ` [PATCH 48/59] perf tools: Add add_breakpoint " Jiri Olsa
2021-11-08 13:37 ` [PATCH 49/59] perf tools: Add add_tracepoint " Jiri Olsa
2021-11-08 13:37 ` [PATCH 50/59] perf tools: Add add_bpf " Jiri Olsa
2021-11-08 13:37 ` [PATCH 51/59] perf tools: Add add_tool " Jiri Olsa
2021-11-08 13:37 ` [PATCH 52/59] perf tools: Add set_leader " Jiri Olsa
2021-11-08 13:37 ` [PATCH 53/59] perf tools: Add parse_check " Jiri Olsa
2021-11-08 13:37 ` [PATCH 54/59] perf tools: Move PE_* enums in parse_events__scanner Jiri Olsa
2021-11-08 13:37 ` [PATCH 55/59] libperf: Move in parse-events flex/bison parser Jiri Olsa
2021-11-08 13:37 ` [PATCH 56/59] libperf: Move in parse_events_add_breakpoint function Jiri Olsa
2021-11-08 13:37 ` [PATCH 57/59] libperf: Move in some lib objects from perf Jiri Olsa
2021-11-08 13:37 ` Jiri Olsa [this message]
2021-11-08 13:37 ` [PATCH 59/59] libperf: Add parse-events test Jiri Olsa
2021-11-08 18:32 ` Ian Rogers
2021-11-08 21:37 ` Jiri Olsa
2021-11-08 18:50 ` [RFC 00/59] libperf: Move in event parse code Ian Rogers
2021-11-08 21:50 ` Jiri Olsa
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211108133710.1352822-59-jolsa@kernel.org \
--to=jolsa@redhat.com \
--cc=acme@kernel.org \
--cc=irogers@google.com \
--cc=linux-perf-users@vger.kernel.org \
--cc=namhyung@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).