2023-04-03 17:23:12

by Ian Rogers

[permalink] [raw]
Subject: [PATCH v1] perf pmu: Make parser reentrant

By default bison uses global state for compatibility with yacc. Make
the parser reentrant so that it may be used in asynchronous and
multithreaded situations.

Signed-off-by: Ian Rogers <[email protected]>
---
tools/perf/util/pmu.c | 17 ++++++++++++-----
tools/perf/util/pmu.h | 2 +-
tools/perf/util/pmu.l | 17 ++++++++++++-----
tools/perf/util/pmu.y | 5 ++++-
4 files changed, 29 insertions(+), 12 deletions(-)

diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
index e3aae731bd6f..8ef2532428a4 100644
--- a/tools/perf/util/pmu.c
+++ b/tools/perf/util/pmu.c
@@ -24,6 +24,8 @@
#include "evsel.h"
#include "pmu.h"
#include "pmus.h"
+#include "pmu-bison.h"
+#include "pmu-flex.h"
#include "parse-events.h"
#include "print-events.h"
#include "header.h"
@@ -57,9 +59,6 @@ struct perf_pmu_format {
struct list_head list;
};

-int perf_pmu_parse(struct list_head *list, char *name);
-extern FILE *perf_pmu_in;
-
static bool hybrid_scanned;

/*
@@ -79,6 +78,7 @@ int perf_pmu__format_parse(char *dir, struct list_head *head)
while (!ret && (evt_ent = readdir(format_dir))) {
char path[PATH_MAX];
char *name = evt_ent->d_name;
+ void *scanner;
FILE *file;

if (!strcmp(name, ".") || !strcmp(name, ".."))
@@ -91,8 +91,15 @@ int perf_pmu__format_parse(char *dir, struct list_head *head)
if (!file)
break;

- perf_pmu_in = file;
- ret = perf_pmu_parse(head, name);
+ ret = perf_pmu_lex_init(&scanner);
+ if (ret) {
+ fclose(file);
+ break;
+ }
+
+ perf_pmu_set_in(file, scanner);
+ ret = perf_pmu_parse(head, name, scanner);
+ perf_pmu_lex_destroy(scanner);
fclose(file);
}

diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h
index 24cf69ab32cd..52c37081c880 100644
--- a/tools/perf/util/pmu.h
+++ b/tools/perf/util/pmu.h
@@ -206,7 +206,7 @@ int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
struct perf_pmu_info *info);
struct list_head *perf_pmu__alias(struct perf_pmu *pmu,
struct list_head *head_terms);
-void perf_pmu_error(struct list_head *list, char *name, char const *msg);
+void perf_pmu_error(struct list_head *list, char *name, void *scanner, char const *msg);

int perf_pmu__new_format(struct list_head *list, char *name,
int config, unsigned long *bits);
diff --git a/tools/perf/util/pmu.l b/tools/perf/util/pmu.l
index 58b4926cfaca..67b247be693b 100644
--- a/tools/perf/util/pmu.l
+++ b/tools/perf/util/pmu.l
@@ -1,4 +1,6 @@
%option prefix="perf_pmu_"
+%option reentrant
+%option bison-bridge

%{
#include <stdlib.h>
@@ -6,16 +8,21 @@
#include "pmu.h"
#include "pmu-bison.h"

-static int value(int base)
+char *perf_pmu_get_text(yyscan_t yyscanner);
+YYSTYPE *perf_pmu_get_lval(yyscan_t yyscanner);
+
+static int value(yyscan_t scanner, int base)
{
+ YYSTYPE *yylval = perf_pmu_get_lval(scanner);
+ char *text = perf_pmu_get_text(scanner);
long num;

errno = 0;
- num = strtoul(perf_pmu_text, NULL, base);
+ num = strtoul(text, NULL, base);
if (errno)
return PP_ERROR;

- perf_pmu_lval.num = num;
+ yylval->num = num;
return PP_VALUE;
}

@@ -25,7 +32,7 @@ num_dec [0-9]+

%%

-{num_dec} { return value(10); }
+{num_dec} { return value(yyscanner, 10); }
config { return PP_CONFIG; }
- { return '-'; }
: { return ':'; }
@@ -35,7 +42,7 @@ config { return PP_CONFIG; }

%%

-int perf_pmu_wrap(void)
+int perf_pmu_wrap(void *scanner __maybe_unused)
{
return 1;
}
diff --git a/tools/perf/util/pmu.y b/tools/perf/util/pmu.y
index e675d79a0274..dff4e892ac4d 100644
--- a/tools/perf/util/pmu.y
+++ b/tools/perf/util/pmu.y
@@ -1,6 +1,8 @@
-
+%define api.pure full
%parse-param {struct list_head *format}
%parse-param {char *name}
+%parse-param {void *scanner}
+%lex-param {void* scanner}

%{

@@ -78,6 +80,7 @@ PP_VALUE

void perf_pmu_error(struct list_head *list __maybe_unused,
char *name __maybe_unused,
+ void *scanner __maybe_unused,
char const *msg __maybe_unused)
{
}
--
2.40.0.348.gf938b09366-goog


2023-04-04 07:49:48

by Jiri Olsa

[permalink] [raw]
Subject: Re: [PATCH v1] perf pmu: Make parser reentrant

On Mon, Apr 03, 2023 at 10:20:31AM -0700, Ian Rogers wrote:
> By default bison uses global state for compatibility with yacc. Make
> the parser reentrant so that it may be used in asynchronous and
> multithreaded situations.
>
> Signed-off-by: Ian Rogers <[email protected]>

Acked-by: Jiri Olsa <[email protected]>

jirka

> ---
> tools/perf/util/pmu.c | 17 ++++++++++++-----
> tools/perf/util/pmu.h | 2 +-
> tools/perf/util/pmu.l | 17 ++++++++++++-----
> tools/perf/util/pmu.y | 5 ++++-
> 4 files changed, 29 insertions(+), 12 deletions(-)
>
> diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
> index e3aae731bd6f..8ef2532428a4 100644
> --- a/tools/perf/util/pmu.c
> +++ b/tools/perf/util/pmu.c
> @@ -24,6 +24,8 @@
> #include "evsel.h"
> #include "pmu.h"
> #include "pmus.h"
> +#include "pmu-bison.h"
> +#include "pmu-flex.h"
> #include "parse-events.h"
> #include "print-events.h"
> #include "header.h"
> @@ -57,9 +59,6 @@ struct perf_pmu_format {
> struct list_head list;
> };
>
> -int perf_pmu_parse(struct list_head *list, char *name);
> -extern FILE *perf_pmu_in;
> -
> static bool hybrid_scanned;
>
> /*
> @@ -79,6 +78,7 @@ int perf_pmu__format_parse(char *dir, struct list_head *head)
> while (!ret && (evt_ent = readdir(format_dir))) {
> char path[PATH_MAX];
> char *name = evt_ent->d_name;
> + void *scanner;
> FILE *file;
>
> if (!strcmp(name, ".") || !strcmp(name, ".."))
> @@ -91,8 +91,15 @@ int perf_pmu__format_parse(char *dir, struct list_head *head)
> if (!file)
> break;
>
> - perf_pmu_in = file;
> - ret = perf_pmu_parse(head, name);
> + ret = perf_pmu_lex_init(&scanner);
> + if (ret) {
> + fclose(file);
> + break;
> + }
> +
> + perf_pmu_set_in(file, scanner);
> + ret = perf_pmu_parse(head, name, scanner);
> + perf_pmu_lex_destroy(scanner);
> fclose(file);
> }
>
> diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h
> index 24cf69ab32cd..52c37081c880 100644
> --- a/tools/perf/util/pmu.h
> +++ b/tools/perf/util/pmu.h
> @@ -206,7 +206,7 @@ int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
> struct perf_pmu_info *info);
> struct list_head *perf_pmu__alias(struct perf_pmu *pmu,
> struct list_head *head_terms);
> -void perf_pmu_error(struct list_head *list, char *name, char const *msg);
> +void perf_pmu_error(struct list_head *list, char *name, void *scanner, char const *msg);
>
> int perf_pmu__new_format(struct list_head *list, char *name,
> int config, unsigned long *bits);
> diff --git a/tools/perf/util/pmu.l b/tools/perf/util/pmu.l
> index 58b4926cfaca..67b247be693b 100644
> --- a/tools/perf/util/pmu.l
> +++ b/tools/perf/util/pmu.l
> @@ -1,4 +1,6 @@
> %option prefix="perf_pmu_"
> +%option reentrant
> +%option bison-bridge
>
> %{
> #include <stdlib.h>
> @@ -6,16 +8,21 @@
> #include "pmu.h"
> #include "pmu-bison.h"
>
> -static int value(int base)
> +char *perf_pmu_get_text(yyscan_t yyscanner);
> +YYSTYPE *perf_pmu_get_lval(yyscan_t yyscanner);
> +
> +static int value(yyscan_t scanner, int base)
> {
> + YYSTYPE *yylval = perf_pmu_get_lval(scanner);
> + char *text = perf_pmu_get_text(scanner);
> long num;
>
> errno = 0;
> - num = strtoul(perf_pmu_text, NULL, base);
> + num = strtoul(text, NULL, base);
> if (errno)
> return PP_ERROR;
>
> - perf_pmu_lval.num = num;
> + yylval->num = num;
> return PP_VALUE;
> }
>
> @@ -25,7 +32,7 @@ num_dec [0-9]+
>
> %%
>
> -{num_dec} { return value(10); }
> +{num_dec} { return value(yyscanner, 10); }
> config { return PP_CONFIG; }
> - { return '-'; }
> : { return ':'; }
> @@ -35,7 +42,7 @@ config { return PP_CONFIG; }
>
> %%
>
> -int perf_pmu_wrap(void)
> +int perf_pmu_wrap(void *scanner __maybe_unused)
> {
> return 1;
> }
> diff --git a/tools/perf/util/pmu.y b/tools/perf/util/pmu.y
> index e675d79a0274..dff4e892ac4d 100644
> --- a/tools/perf/util/pmu.y
> +++ b/tools/perf/util/pmu.y
> @@ -1,6 +1,8 @@
> -
> +%define api.pure full
> %parse-param {struct list_head *format}
> %parse-param {char *name}
> +%parse-param {void *scanner}
> +%lex-param {void* scanner}
>
> %{
>
> @@ -78,6 +80,7 @@ PP_VALUE
>
> void perf_pmu_error(struct list_head *list __maybe_unused,
> char *name __maybe_unused,
> + void *scanner __maybe_unused,
> char const *msg __maybe_unused)
> {
> }
> --
> 2.40.0.348.gf938b09366-goog
>

2023-04-04 12:41:37

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH v1] perf pmu: Make parser reentrant

Em Tue, Apr 04, 2023 at 09:38:32AM +0200, Jiri Olsa escreveu:
> On Mon, Apr 03, 2023 at 10:20:31AM -0700, Ian Rogers wrote:
> > By default bison uses global state for compatibility with yacc. Make
> > the parser reentrant so that it may be used in asynchronous and
> > multithreaded situations.
> >
> > Signed-off-by: Ian Rogers <[email protected]>
>
> Acked-by: Jiri Olsa <[email protected]>

So this clashed with Namhyung's recent series, please update it using
what is in tmp.perf-tools-next.

- Arnaldo

2023-04-04 13:48:37

by Ian Rogers

[permalink] [raw]
Subject: Re: [PATCH v1] perf pmu: Make parser reentrant

On Tue, Apr 4, 2023 at 5:39 AM Arnaldo Carvalho de Melo <[email protected]> wrote:
>
> Em Tue, Apr 04, 2023 at 09:38:32AM +0200, Jiri Olsa escreveu:
> > On Mon, Apr 03, 2023 at 10:20:31AM -0700, Ian Rogers wrote:
> > > By default bison uses global state for compatibility with yacc. Make
> > > the parser reentrant so that it may be used in asynchronous and
> > > multithreaded situations.
> > >
> > > Signed-off-by: Ian Rogers <[email protected]>
> >
> > Acked-by: Jiri Olsa <[email protected]>
>
> So this clashed with Namhyung's recent series, please update it using
> what is in tmp.perf-tools-next.
>
> - Arnaldo

I dealt with the merge conflict in v2 [1]. I didn't add Jiri's
acked-by just because there was a small change in
perf_pmu__format_parse.

Thanks,
Ian

[1] https://lore.kernel.org/lkml/[email protected]/