From nobody Mon Nov 25 07:48:03 2024 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7C1CF20C005 for ; Tue, 29 Oct 2024 22:45:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730241917; cv=none; b=Lt1Km0sEtJZK3WTERyQjMs0nfV0IG6xBP7/DqvBr2bt8xC0Nh7bKHpan473kBqps4c8FP500coOFEnSe6TRkt47IxDMUXxZK1SgeIOa3mbWHtklQQKCuGvnc3oWytt5bNSCGCr7DQwD/wcTfr0EcWmAnwVbBAgsAJ8g/DpxAIuk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730241917; c=relaxed/simple; bh=wFg8rXKhwSk+/WnE2Ybco7HPsV496JVgIBfe+G5oj+I=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=jd5zmDMgTCz1f6RzxqcsARoGT0Pz2qiU5/RNVcOw7gKA+jfopxSnzR850A2eB2kiqKS74A/mCaPbAu4iHnqkx/PYQ8xycOtmNl8OEB1zLFYd/rBA9EOgK7lDv7bJpoJaM1ylCLICUL8Ke+nXfGZpTxiDRi0TXgfi96MgofagMGU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=lVk1HH/G; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="lVk1HH/G" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6e31d9c8efcso116511087b3.0 for ; Tue, 29 Oct 2024 15:45:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1730241914; x=1730846714; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=ACBWh/O4/X4qiB3oXOqHhx7Z60zaZQtrFXt6UTS1Yas=; b=lVk1HH/G04RNpalDAb5ZxZPb0029+qThAA8htVxpr8ZWj79Er0aDAv0VSGp2GMVQp0 5wkjSLri36LlYrx1ZOU6OXkIKPenrU9a3nali3ZRxTRLV6bLFx9R9NoTAbN/RfxTcDCT Y2aS2qn4GsNbEMfuIjP4rEUpg6KYA0as4sqyW+kkOQHv3iSUCWn2H8q7FgKblPbQe7Vr afrLRNdnFyy9kFNqIu9r9hTes0wNv+QmV3++UBdLo/j5kMxcgNOk4mzZdvrYWbc8Bpg7 zD4ff//8SBUpoXVprtFPT2tg+9dlwHDRBeI9zIX2ezAEQVNGILrgJvzEX0XoBnwV9xm2 fIMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730241914; x=1730846714; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ACBWh/O4/X4qiB3oXOqHhx7Z60zaZQtrFXt6UTS1Yas=; b=pbRBrsvBlmKWEf0ZA4wgyLB6drDhC5YK2ieZ5BtYoBqwwCeym/CELc69dcGGLHe3yC YMmSQdHsjYqv7l3DuQClou7pzljaD4vkTUhUJllK7eMFrHM4CSpY0sNBwSj7bINMIiMX dyed7dVzZo2/FXOWZ0ZBlEvUN0cMIQLWy1BUig4OgK51JOcJCqKuHKB3okkU29EXPbil n1yUkt6ql2tmkiDz9P/+mBlWmSWNkkIaQ6rF/KleDJiShbvP4D/U/rwU/4qweDyqwcuC Gz5yqcQ88s9c0ffM+FkinTCCwerYxP9+ppnhlPVj/GhP+PL9QAX84CoZf9Kb4wp0yiCn 8kkg== X-Forwarded-Encrypted: i=1; AJvYcCX7QTtr4PbksluXaLRXnfvxGSsCcHpfz4I17M3a9dQElWVvyBTx+j627gvW+3pzjT1EuaIkfdQGPyDG8JY=@vger.kernel.org X-Gm-Message-State: AOJu0Yyvmos4l8w9nHlhPWsZMkc4bDNCowj2OnuhBU+y3rS7vN6PJlHE RFt9UIRQ2eKP/9ulw0FTTLlEgDyUiufVOY1nkjqKa19sqTVyEMWzfEmDUi7i7JjseNefeyA9fcb 5xEjQrw== X-Google-Smtp-Source: AGHT+IHMWAy13SQ+8+uIKYT+wWyqA6f4LfReuZMtkZDb3SMdJ6/mM831/a5FCW0qA1CcmYiBgEyIDuXzCQXZ X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:8991:d3fd:38a7:c282]) (user=irogers job=sendgmr) by 2002:a81:be02:0:b0:6a9:3d52:79e9 with SMTP id 00721157ae682-6ea3b94f7famr436597b3.4.1730241914417; Tue, 29 Oct 2024 15:45:14 -0700 (PDT) Date: Tue, 29 Oct 2024 15:44:28 -0700 In-Reply-To: <20241029224431.167623-1-irogers@google.com> Message-Id: <20241029224431.167623-17-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241029224431.167623-1-irogers@google.com> X-Mailer: git-send-email 2.47.0.163.g1226f6d8fa-goog Subject: [PATCH v4 16/19] perf python: Add parse_events function From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , James Clark , Howard Chu , Athira Jajeev , Michael Petlan , Veronika Molnarova , Dapeng Mi , Thomas Richter , Ilya Leoshkevich , Colin Ian King , Weilin Wang , Andi Kleen , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add basic parse_events function that takes a string and returns an evlist. As the python evlist is embedded in a pyrf_evlist, and the evsels are embedded in pyrf_evsels, copy the parsed data into those structs and update evsel__clone to enable this. Signed-off-by: Ian Rogers --- tools/perf/util/cgroup.c | 2 +- tools/perf/util/evsel.c | 19 ++++++++----- tools/perf/util/evsel.h | 2 +- tools/perf/util/python.c | 61 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 9 deletions(-) diff --git a/tools/perf/util/cgroup.c b/tools/perf/util/cgroup.c index 0f759dd96db7..fbcc0626f9ce 100644 --- a/tools/perf/util/cgroup.c +++ b/tools/perf/util/cgroup.c @@ -473,7 +473,7 @@ int evlist__expand_cgroup(struct evlist *evlist, const = char *str, =20 leader =3D NULL; evlist__for_each_entry(orig_list, pos) { - evsel =3D evsel__clone(pos); + evsel =3D evsel__clone(/*dest=3D*/NULL, pos); if (evsel =3D=3D NULL) goto out_err; =20 diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index f780e30aa259..4213ffacdc3c 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -453,7 +453,7 @@ static int evsel__copy_config_terms(struct evsel *dst, = struct evsel *src) * The assumption is that @orig is not configured nor opened yet. * So we only care about the attributes that can be set while it's parsed. */ -struct evsel *evsel__clone(struct evsel *orig) +struct evsel *evsel__clone(struct evsel *dest, struct evsel *orig) { struct evsel *evsel; =20 @@ -466,7 +466,11 @@ struct evsel *evsel__clone(struct evsel *orig) if (orig->bpf_obj) return NULL; =20 - evsel =3D evsel__new(&orig->core.attr); + if (dest) + evsel =3D dest; + else + evsel =3D evsel__new(&orig->core.attr); + if (evsel =3D=3D NULL) return NULL; =20 @@ -511,11 +515,12 @@ struct evsel *evsel__clone(struct evsel *orig) evsel->core.leader =3D orig->core.leader; =20 evsel->max_events =3D orig->max_events; - free((char *)evsel->unit); - evsel->unit =3D strdup(orig->unit); - if (evsel->unit =3D=3D NULL) - goto out_err; - + zfree(&evsel->unit); + if (orig->unit) { + evsel->unit =3D strdup(orig->unit); + if (evsel->unit =3D=3D NULL) + goto out_err; + } evsel->scale =3D orig->scale; evsel->snapshot =3D orig->snapshot; evsel->per_pkg =3D orig->per_pkg; diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index 04934a7af174..7f41d6f8cf6f 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -241,7 +241,7 @@ static inline struct evsel *evsel__new(struct perf_even= t_attr *attr) return evsel__new_idx(attr, 0); } =20 -struct evsel *evsel__clone(struct evsel *orig); +struct evsel *evsel__clone(struct evsel *dest, struct evsel *orig); =20 int copy_config_terms(struct list_head *dst, struct list_head *src); void free_config_terms(struct list_head *config_terms); diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c index 6e2ff0076daa..1a53a97c513d 100644 --- a/tools/perf/util/python.c +++ b/tools/perf/util/python.c @@ -13,6 +13,7 @@ #include "evsel.h" #include "event.h" #include "print_binary.h" +#include "strbuf.h" #include "thread_map.h" #include "trace-event.h" #include "mmap.h" @@ -1201,6 +1202,60 @@ static PyObject *pyrf__tracepoint(struct pyrf_evsel = *pevsel, #endif // HAVE_LIBTRACEEVENT } =20 +static PyObject *pyrf_evsel__from_evsel(struct evsel *evsel) +{ + struct pyrf_evsel *pevsel =3D PyObject_New(struct pyrf_evsel, &pyrf_evsel= __type); + + if (!pevsel) + return NULL; + + memset(&pevsel->evsel, 0, sizeof(pevsel->evsel)); + evsel__init(&pevsel->evsel, &evsel->core.attr, evsel->core.idx); + + evsel__clone(&pevsel->evsel, evsel); + return (PyObject *)pevsel; +} + +static PyObject *pyrf_evlist__from_evlist(struct evlist *evlist) +{ + struct pyrf_evlist *pevlist =3D PyObject_New(struct pyrf_evlist, &pyrf_ev= list__type); + struct evsel *pos; + + if (!pevlist) + return NULL; + + memset(&pevlist->evlist, 0, sizeof(pevlist->evlist)); + evlist__init(&pevlist->evlist, evlist->core.all_cpus, evlist->core.thread= s); + evlist__for_each_entry(evlist, pos) { + struct pyrf_evsel *pevsel =3D (void *)pyrf_evsel__from_evsel(pos); + + evlist__add(&pevlist->evlist, &pevsel->evsel); + } + return (PyObject *)pevlist; +} + +static PyObject *pyrf__parse_events(PyObject *self, PyObject *args) +{ + const char *input; + struct evlist evlist =3D {}; + struct parse_events_error err; + PyObject *result; + + if (!PyArg_ParseTuple(args, "s", &input)) + return NULL; + + parse_events_error__init(&err); + evlist__init(&evlist, NULL, NULL); + if (parse_events(&evlist, input, &err)) { + parse_events_error__print(&err, input); + PyErr_SetFromErrno(PyExc_OSError); + return NULL; + } + result =3D pyrf_evlist__from_evlist(&evlist); + evlist__exit(&evlist); + return result; +} + static PyMethodDef perf__methods[] =3D { { .ml_name =3D "tracepoint", @@ -1208,6 +1263,12 @@ static PyMethodDef perf__methods[] =3D { .ml_flags =3D METH_VARARGS | METH_KEYWORDS, .ml_doc =3D PyDoc_STR("Get tracepoint config.") }, + { + .ml_name =3D "parse_events", + .ml_meth =3D (PyCFunction) pyrf__parse_events, + .ml_flags =3D METH_VARARGS, + .ml_doc =3D PyDoc_STR("Parse a string of events and return an evlist.") + }, { .ml_name =3D NULL, } }; =20 --=20 2.47.0.163.g1226f6d8fa-goog