[PATCH v2 08/11] perf python: Avoid duplicated code in get_tracepoint_field

Ian Rogers posted 11 patches 9 months, 3 weeks ago
[PATCH v2 08/11] perf python: Avoid duplicated code in get_tracepoint_field
Posted by Ian Rogers 9 months, 3 weeks ago
The code replicates computations done in evsel__tp_format, reuse
evsel__tp_format to simplify the python C code.

Reviewed-by: Howard Chu <howardchu95@gmail.com>
Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/python.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)

diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
index e244cc74f16d..7f2513ffe866 100644
--- a/tools/perf/util/python.c
+++ b/tools/perf/util/python.c
@@ -342,23 +342,14 @@ get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name)
 {
 	const char *str = _PyUnicode_AsString(PyObject_Str(attr_name));
 	struct evsel *evsel = pevent->evsel;
+	struct tep_event *tp_format = evsel__tp_format(evsel);
 	struct tep_format_field *field;
 
-	if (!evsel->tp_format) {
-		struct tep_event *tp_format;
-
-		tp_format = trace_event__tp_format_id(evsel->core.attr.config);
-		if (IS_ERR_OR_NULL(tp_format))
-			return NULL;
-
-		evsel->tp_format = tp_format;
-	}
-
-	field = tep_find_any_field(evsel->tp_format, str);
-	if (!field)
+	if (IS_ERR_OR_NULL(tp_format))
 		return NULL;
 
-	return tracepoint_field(pevent, field);
+	field = tep_find_any_field(tp_format, str);
+	return field ? tracepoint_field(pevent, field) : NULL;
 }
 #endif /* HAVE_LIBTRACEEVENT */
 
-- 
2.48.1.711.g2feabab25a-goog
Re: [PATCH v2 08/11] perf python: Avoid duplicated code in get_tracepoint_field
Posted by Arnaldo Carvalho de Melo 9 months, 1 week ago
On Fri, Feb 28, 2025 at 02:23:05PM -0800, Ian Rogers wrote:
> The code replicates computations done in evsel__tp_format, reuse
> evsel__tp_format to simplify the python C code.

Reviewed-by: Arnaldo Carvalho de Melo <acme@redhat.com>

- Arnaldo
 
> Reviewed-by: Howard Chu <howardchu95@gmail.com>
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
>  tools/perf/util/python.c | 17 ++++-------------
>  1 file changed, 4 insertions(+), 13 deletions(-)
> 
> diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
> index e244cc74f16d..7f2513ffe866 100644
> --- a/tools/perf/util/python.c
> +++ b/tools/perf/util/python.c
> @@ -342,23 +342,14 @@ get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name)
>  {
>  	const char *str = _PyUnicode_AsString(PyObject_Str(attr_name));
>  	struct evsel *evsel = pevent->evsel;
> +	struct tep_event *tp_format = evsel__tp_format(evsel);
>  	struct tep_format_field *field;
>  
> -	if (!evsel->tp_format) {
> -		struct tep_event *tp_format;
> -
> -		tp_format = trace_event__tp_format_id(evsel->core.attr.config);
> -		if (IS_ERR_OR_NULL(tp_format))
> -			return NULL;
> -
> -		evsel->tp_format = tp_format;
> -	}
> -
> -	field = tep_find_any_field(evsel->tp_format, str);
> -	if (!field)
> +	if (IS_ERR_OR_NULL(tp_format))
>  		return NULL;
>  
> -	return tracepoint_field(pevent, field);
> +	field = tep_find_any_field(tp_format, str);
> +	return field ? tracepoint_field(pevent, field) : NULL;
>  }
>  #endif /* HAVE_LIBTRACEEVENT */
>  
> -- 
> 2.48.1.711.g2feabab25a-goog