From nobody Mon Oct 6 03:13:57 2025 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.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 F10A225F997 for ; Fri, 25 Jul 2025 18:52:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753469565; cv=none; b=U9Rt90gf0ITlwL7auxE85mwhJYH6YAbdTQYEQX1JmQse5qGlO9SkFWgD4ANR5ZsK58BJiC8MkqikLMN7j3PtNYpAa+G/Kdd5Cj7Ec+7iw2xQ7PLmEVNd3jvW/JuSa9Wc1wiCkuFbvHRaYoKm23iHhQb1OP/wqY8TnmE+WtzwzZY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753469565; c=relaxed/simple; bh=QNymXMsh0+PDwJgBEIf87BOhNAAwdyMjmuDCswOESpQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ukTLqT4vqEWq/MJA+nI5sb24iCZgyoUwczK5/Ejsy3+tNO3M/0mxKbSg2SzllPqgWN4RDy6PYNOq2bKIBzfyBrSxvToHlTrAYZyjWht3SBhl3RmBaELbiujaBkXnKKB6poEEFmppx3/XN6SnWjHd1mBqS0LGV2bhsr1FKB+GZY4= 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=rK2w7CHB; arc=none smtp.client-ip=209.85.214.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="rK2w7CHB" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2354ba59eb6so38072345ad.1 for ; Fri, 25 Jul 2025 11:52:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1753469563; x=1754074363; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=eIGuclGiABh3QC0DEVnjdGmWupx/QA5phZrLpy71AEk=; b=rK2w7CHBc4mLbXjEwd84LikxU5TZzq5BD0cVY1NWQ1lEH4uZLmqawGLPd2hTVS1AS8 GAO7jOCnG0OE/SlFGpsh7KdKloqH4p24DuLacmZ5l/m5MzH/jgiEy14wwM5AC7+h2SBr rBzLBq9HbioPgWmd7GOmOYEBJ8nAA/1oAO6GlSzdShofipsVZ06duu54Gy+RM9ovecgX C2v2DKjP4LGCQCbLEsdtipSTDfar5nltUDdaIwnjJUisY0Q+HOCEGYkC0TzZUGzVCUsH YO1xHvea8iypx5DgGg9xHux6wu3f56AB6uyi+2lg+lHYa/aFFqrPawy6BTqjlndtm46y PzOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753469563; x=1754074363; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=eIGuclGiABh3QC0DEVnjdGmWupx/QA5phZrLpy71AEk=; b=Fk57O8GuI/2BIQ0LRwCM3o9GsAP+potpVG5mfrzG2o77N5/kfwief7kigRZ0Le09Y2 7ZF1qpazGzYMe575zOCwRNvR0arMQbNbj4Tsr3SjFjEkY5eXfAXvy0Gu8GuTeDwfjddc Q5bqS3D6KFlj+wAUYWtuZfhuRSv72WaOWW1YQHa1yjZQLKqobe66wXhp4GvGN4rGj0qA HH1ZWt4gRtbJIgJQUNXnK+7yt52f7m9hJwDPpm0QN6hLTJ09q1HtXptYLA5dQz460FMY 1uYYDD2wywkKYNr60thN+FESW8lI/MMlTTcjYU22+04j+YBWnLGfQKdSOo8SYd1fV9se ZURA== X-Forwarded-Encrypted: i=1; AJvYcCUfZNkQDt2azU/CbUpIWjEc0nAtAtQvlHbnZnXBInCbHqqJFpNTZ5zVgTOljKLRN9yz4xbkY/A+k/by3Tg=@vger.kernel.org X-Gm-Message-State: AOJu0YzPmmfwVkUBPJGXpqiBKXggL3H11+21iYKCWCmtIB6boGi9hoov FGuX/v88M29L8C+igzMo5Sj2brv3Nsh4YBz39z1QxDNstqjO3q7x3XjkPSuAwFb/MKiUyaEdE7L TGCpqFBlzLA== X-Google-Smtp-Source: AGHT+IHs6tlNj+93w5GJ6/PacV7fKa7xzryQVfnW20CZNa7JKwjGoSHXoYrDrxU6vRx2aNJFJLb3d0AbBEfS X-Received: from plor11.prod.google.com ([2002:a17:902:8bcb:b0:220:ca3c:96bc]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:e541:b0:23f:8bcc:e4f3 with SMTP id d9443c01a7336-23fb30ab8bbmr49052845ad.32.1753469563354; Fri, 25 Jul 2025 11:52:43 -0700 (PDT) Date: Fri, 25 Jul 2025 11:52:00 -0700 In-Reply-To: <20250725185202.68671-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250725185202.68671-1-irogers@google.com> X-Mailer: git-send-email 2.50.1.552.g942d659e1b-goog Message-ID: <20250725185202.68671-15-irogers@google.com> Subject: [PATCH v9 14/16] perf python: Add evlist compute_metric 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 , Xu Yang , "Masami Hiramatsu (Google)" , Collin Funk , Howard Chu , Weilin Wang , Andi Kleen , "Dr. David Alan Gilbert" , Thomas Richter , Tiezhu Yang , Gautam Menghani , Thomas Falcon , Chun-Tse Shao , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Cc: Arnaldo Carvalho de Melo Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a compute_metric function that computes a metric double value for a given evlist, metric name, CPU and thread. For example: ``` >>> import perf >>> x =3D perf.parse_metrics("TopdownL1") >>> x.open() >>> x.enable() >>> x.disable() >>> x.metrics() ['tma_bad_speculation', 'tma_frontend_bound', 'tma_backend_bound', 'tma_ret= iring'] >>> x.compute_metric('tma_bad_speculation', 0, -1) 0.08605342847131037 ``` Signed-off-by: Ian Rogers Tested-by: Arnaldo Carvalho de Melo --- tools/perf/util/python.c | 125 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c index ccfa1ac4ad4b..bee7c8a69bad 100644 --- a/tools/perf/util/python.c +++ b/tools/perf/util/python.c @@ -14,6 +14,7 @@ #include "evlist.h" #include "evsel.h" #include "event.h" +#include "expr.h" #include "print_binary.h" #include "record.h" #include "strbuf.h" @@ -1320,6 +1321,124 @@ static PyObject *pyrf_evlist__metrics(struct pyrf_e= vlist *pevlist) return list; } =20 +static int prepare_metric(const struct metric_expr *mexp, + const struct evsel *evsel, + struct expr_parse_ctx *pctx, + int cpu_idx, int thread_idx) +{ + struct evsel * const *metric_events =3D mexp->metric_events; + struct metric_ref *metric_refs =3D mexp->metric_refs; + + for (int i =3D 0; metric_events[i]; i++) { + char *n =3D strdup(evsel__metric_id(metric_events[i])); + double val, ena, run; + int source_count =3D evsel__source_count(metric_events[i]); + int ret; + struct perf_counts_values *old_count, *new_count; + + if (!n) + return -ENOMEM; + + if (source_count =3D=3D 0) + source_count =3D 1; + + ret =3D evsel__ensure_counts(metric_events[i]); + if (ret) + return ret; + + /* Set up pointers to the old and newly read counter values. */ + old_count =3D perf_counts(metric_events[i]->prev_raw_counts, cpu_idx, th= read_idx); + new_count =3D perf_counts(metric_events[i]->counts, cpu_idx, thread_idx); + /* Update the value in metric_events[i]->counts. */ + evsel__read_counter(metric_events[i], cpu_idx, thread_idx); + + val =3D new_count->val - old_count->val; + ena =3D new_count->ena - old_count->ena; + run =3D new_count->run - old_count->run; + + if (ena !=3D run && run !=3D 0) + val =3D val * ena / run; + ret =3D expr__add_id_val_source_count(pctx, n, val, source_count); + if (ret) + return ret; + } + + for (int i =3D 0; metric_refs && metric_refs[i].metric_name; i++) { + int ret =3D expr__add_ref(pctx, &metric_refs[i]); + + if (ret) + return ret; + } + + return 0; +} + +static PyObject *pyrf_evlist__compute_metric(struct pyrf_evlist *pevlist, + PyObject *args, PyObject *kwargs) +{ + int ret, cpu =3D 0, cpu_idx, thread =3D 0, thread_idx; + const char *metric; + struct rb_node *node; + struct metric_expr *mexp =3D NULL; + struct expr_parse_ctx *pctx; + double result =3D 0; + + if (!PyArg_ParseTuple(args, "sii", &metric, &cpu, &thread)) + return NULL; + + for (node =3D rb_first_cached(&pevlist->evlist.metric_events.entries); + mexp =3D=3D NULL && node; + node =3D rb_next(node)) { + struct metric_event *me =3D container_of(node, struct metric_event, nd); + struct list_head *pos; + + list_for_each(pos, &me->head) { + struct metric_expr *e =3D container_of(pos, struct metric_expr, nd); + + if (strcmp(e->metric_name, metric)) + continue; + + if (e->metric_events[0] =3D=3D NULL) + continue; + + cpu_idx =3D perf_cpu_map__idx(e->metric_events[0]->core.cpus, + (struct perf_cpu){.cpu =3D cpu}); + if (cpu_idx < 0) + continue; + + thread_idx =3D perf_thread_map__idx(e->metric_events[0]->core.threads, + thread); + if (thread_idx < 0) + continue; + + mexp =3D e; + break; + } + } + if (!mexp) { + PyErr_Format(PyExc_TypeError, "Unknown metric '%s' for CPU '%d' and thre= ad '%d'", + metric, cpu, thread); + return NULL; + } + + pctx =3D expr__ctx_new(); + if (!pctx) + return PyErr_NoMemory(); + + ret =3D prepare_metric(mexp, mexp->metric_events[0], pctx, cpu_idx, threa= d_idx); + if (ret) { + expr__ctx_free(pctx); + errno =3D -ret; + PyErr_SetFromErrno(PyExc_OSError); + return NULL; + } + if (expr__parse(&result, pctx, mexp->metric_expr)) + result =3D 0.0; + + expr__ctx_free(pctx); + return PyFloat_FromDouble(result); +} + static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist, PyObject *args, PyObject *kwargs) { @@ -1554,6 +1673,12 @@ static PyMethodDef pyrf_evlist__methods[] =3D { .ml_flags =3D METH_NOARGS, .ml_doc =3D PyDoc_STR("List of metric names within the evlist.") }, + { + .ml_name =3D "compute_metric", + .ml_meth =3D (PyCFunction)pyrf_evlist__compute_metric, + .ml_flags =3D METH_VARARGS | METH_KEYWORDS, + .ml_doc =3D PyDoc_STR("compute metric for given name, cpu and thread") + }, { .ml_name =3D "mmap", .ml_meth =3D (PyCFunction)pyrf_evlist__mmap, --=20 2.50.1.552.g942d659e1b-goog